EditorManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <CoreMain.h>
  6. #include <TextsWin.h>
  7. #include "WinConfiguration.h"
  8. #include "EditorManager.h"
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. //---------------------------------------------------------------------------
  12. __fastcall TEditorManager::TEditorManager()
  13. {
  14. FOnFileChange = NULL;
  15. FOnFileReload = NULL;
  16. FOnFileEarlyClosed = NULL;
  17. }
  18. //---------------------------------------------------------------------------
  19. __fastcall TEditorManager::~TEditorManager()
  20. {
  21. for (unsigned int i = FFiles.size(); i > 0; i--)
  22. {
  23. TFileData * FileData = &FFiles[i - 1];
  24. // pending should be only external editors and files being uploaded
  25. assert(FileData->Closed || FileData->External);
  26. if (!FileData->Closed)
  27. {
  28. CloseFile(i - 1, true, true);
  29. }
  30. }
  31. }
  32. //---------------------------------------------------------------------------
  33. bool __fastcall TEditorManager::Empty(bool IgnoreClosed)
  34. {
  35. bool Result;
  36. if (!IgnoreClosed)
  37. {
  38. Result = (FFiles.size() == 0);
  39. }
  40. else
  41. {
  42. Result = true;
  43. for (unsigned int i = 0; i < FFiles.size(); i++)
  44. {
  45. if (!FFiles[i].Closed)
  46. {
  47. Result = false;
  48. break;
  49. }
  50. }
  51. }
  52. return Result;
  53. }
  54. //---------------------------------------------------------------------------
  55. bool __fastcall TEditorManager::CanAddFile(const AnsiString RemoteDirectory,
  56. const AnsiString OriginalFileName, const AnsiString SessionName,
  57. TObject *& Token, AnsiString & ExistingLocalRootDirectory,
  58. AnsiString & ExistingLocalDirectory)
  59. {
  60. bool Result = true;
  61. Token = NULL;
  62. for (unsigned int i = 0; i < FFiles.size(); i++)
  63. {
  64. TFileData * FileData = &FFiles[i];
  65. // include even "closed" (=being uploaded) files as it is nonsense
  66. // to download file being uploaded
  67. if ((FileData->Data.RemoteDirectory == RemoteDirectory) &&
  68. (FileData->Data.OriginalFileName == OriginalFileName) &&
  69. (FileData->Data.SessionName == SessionName))
  70. {
  71. if (!FileData->External)
  72. {
  73. Result = false;
  74. if (FileData->Token != NULL)
  75. {
  76. Token = FileData->Token;
  77. }
  78. }
  79. else
  80. {
  81. // MDI editor?
  82. if (FileData->Process == INVALID_HANDLE_VALUE)
  83. {
  84. // file is just being uploaded, do not allow new editor instance
  85. if (FileData->Closed)
  86. {
  87. Result = false;
  88. }
  89. else
  90. {
  91. // get directory where the file already is so we download it there again
  92. ExistingLocalRootDirectory = FileData->Data.LocalRootDirectory;
  93. ExistingLocalDirectory = ExtractFilePath(FileData->FileName);
  94. CloseFile(i, false, false); // do not delete file
  95. Result = true;
  96. }
  97. }
  98. else
  99. {
  100. Result = false;
  101. }
  102. }
  103. break;
  104. }
  105. }
  106. if (Result)
  107. {
  108. if (FFiles.size() >= WinConfiguration->Editor.MaxEditors)
  109. {
  110. throw Exception(LoadStr(TOO_MANY_EDITORS));
  111. }
  112. }
  113. return Result;
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TEditorManager::ProcessFiles(TEditedFileProcessEvent Callback, void * Arg)
  117. {
  118. for (unsigned int i = 0; i < FFiles.size(); i++)
  119. {
  120. TFileData * FileData = &FFiles[i];
  121. Callback(FileData->FileName, FileData->Data, FileData->Token, Arg);
  122. }
  123. }
  124. //---------------------------------------------------------------------------
  125. bool __fastcall TEditorManager::CloseInternalEditors(TNotifyEvent CloseCallback)
  126. {
  127. // Traverse from end, as closing internal editor causes deletion of
  128. // respective file vector element.
  129. TObject * PrevToken = NULL;
  130. for (unsigned int i = FFiles.size(); i > 0; i--)
  131. {
  132. // Note that element may be deleted by external cause (like if external editor
  133. // is closed while "save confirmation" message is displayed).
  134. if (i <= FFiles.size())
  135. {
  136. TFileData * FileData = &FFiles[i - 1];
  137. // PrevToken is simple check not to ask same editor twice, however
  138. // it does not solve all posibilities
  139. if (!FileData->Closed && (FileData->Token != NULL) &&
  140. (FileData->Token != PrevToken))
  141. {
  142. CloseCallback(FileData->Token);
  143. }
  144. }
  145. }
  146. bool Result = true;
  147. for (unsigned int i = 0; i < FFiles.size(); i++)
  148. {
  149. TFileData * FileData = &FFiles[i];
  150. if (!FileData->Closed && (FileData->Token != NULL))
  151. {
  152. Result = false;
  153. break;
  154. }
  155. }
  156. return Result;
  157. }
  158. //---------------------------------------------------------------------------
  159. bool __fastcall TEditorManager::CloseExternalFilesWithoutProcess()
  160. {
  161. for (unsigned int i = FFiles.size(); i > 0; i--)
  162. {
  163. TFileData * FileData = &FFiles[i - 1];
  164. if (!FileData->Closed && FileData->External &&
  165. (FileData->Process == INVALID_HANDLE_VALUE))
  166. {
  167. CloseFile(i - 1, true, true);
  168. }
  169. }
  170. return true;
  171. }
  172. //---------------------------------------------------------------------------
  173. void __fastcall TEditorManager::AddFileInternal(const AnsiString FileName,
  174. const TEditedFileData & Data, TObject * Token)
  175. {
  176. TFileData FileData;
  177. FileData.FileName = FileName;
  178. FileData.External = false;
  179. FileData.Process = INVALID_HANDLE_VALUE;
  180. FileData.Token = Token;
  181. FileData.Data = Data;
  182. FileData.Monitor = INVALID_HANDLE_VALUE;
  183. AddFile(FileData);
  184. }
  185. //---------------------------------------------------------------------------
  186. void __fastcall TEditorManager::AddFileExternal(const AnsiString FileName,
  187. const TEditedFileData & Data, HANDLE Process)
  188. {
  189. TFileData FileData;
  190. FileData.FileName = FileName;
  191. FileData.External = true;
  192. FileData.Process = Process;
  193. FileData.Token = NULL;
  194. FileData.Data = Data;
  195. FileData.Monitor = FindFirstChangeNotification(
  196. ExtractFilePath(FileData.FileName).c_str(), false,
  197. FILE_NOTIFY_CHANGE_LAST_WRITE);
  198. if (FileData.Monitor == INVALID_HANDLE_VALUE)
  199. {
  200. throw Exception(FMTLOAD(FILE_WATCH_ERROR, (FileData.FileName)));
  201. }
  202. FMonitors.push_back(FileData.Monitor);
  203. if (Process != INVALID_HANDLE_VALUE)
  204. {
  205. FProcesses.push_back(Process);
  206. }
  207. AddFile(FileData);
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TEditorManager::Check()
  211. {
  212. int Index;
  213. if (FMonitors.size() > 0)
  214. {
  215. do
  216. {
  217. Index = WaitFor(FMonitors.size(), &(FMonitors[0]), MONITOR);
  218. if (Index >= 0)
  219. {
  220. TFileData * FileData = &FFiles[Index];
  221. FindNextChangeNotification(FileData->Monitor);
  222. CheckFileChange(Index, false);
  223. }
  224. }
  225. while (Index >= 0);
  226. }
  227. if (FProcesses.size() > 0)
  228. {
  229. do
  230. {
  231. Index = WaitFor(FProcesses.size(), &(FProcesses[0]), PROCESS);
  232. if (Index >= 0)
  233. {
  234. try
  235. {
  236. CheckFileChange(Index, false);
  237. }
  238. __finally
  239. {
  240. if (!EarlyClose(Index))
  241. {
  242. // CheckFileChange may fail (file is already being uploaded),
  243. // but we want to close handles anyway
  244. CloseFile(Index, false, true);
  245. }
  246. }
  247. }
  248. }
  249. while ((Index >= 0) && (FProcesses.size() > 0));
  250. }
  251. if (FUploadCompleteEvents.size() > 0)
  252. {
  253. do
  254. {
  255. Index = WaitFor(FUploadCompleteEvents.size(), &(FUploadCompleteEvents[0]),
  256. EVENT);
  257. if (Index >= 0)
  258. {
  259. UploadComplete(Index);
  260. }
  261. }
  262. while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
  263. }
  264. }
  265. //---------------------------------------------------------------------------
  266. bool __fastcall TEditorManager::EarlyClose(int Index)
  267. {
  268. TFileData * FileData = &FFiles[Index];
  269. bool Result =
  270. (FileData->Process != INVALID_HANDLE_VALUE) &&
  271. (Now() - FileData->Opened <=
  272. TDateTime(0, 0, static_cast<unsigned short>(WinConfiguration->Editor.EarlyClose), 0)) &&
  273. (FOnFileEarlyClosed != NULL);
  274. if (Result)
  275. {
  276. Result = false;
  277. FOnFileEarlyClosed(FileData->Data, Result);
  278. if (Result)
  279. {
  280. // forget the associated process
  281. CloseProcess(Index);
  282. }
  283. }
  284. return Result;
  285. }
  286. //---------------------------------------------------------------------------
  287. void __fastcall TEditorManager::FileChanged(TObject * Token)
  288. {
  289. int Index = FindFile(Token);
  290. assert(Index >= 0);
  291. assert(!FFiles[Index].External);
  292. CheckFileChange(Index, true);
  293. }
  294. //---------------------------------------------------------------------------
  295. void __fastcall TEditorManager::FileReload(TObject * Token)
  296. {
  297. int Index = FindFile(Token);
  298. assert(Index >= 0);
  299. TFileData * FileData = &FFiles[Index];
  300. assert(!FileData->External);
  301. OnFileReload(FileData->FileName, FileData->Data);
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TEditorManager::FileClosed(TObject * Token)
  305. {
  306. int Index = FindFile(Token);
  307. assert(Index >= 0);
  308. assert(!FFiles[Index].External);
  309. CheckFileChange(Index, false);
  310. CloseFile(Index, false, true);
  311. }
  312. //---------------------------------------------------------------------------
  313. void __fastcall TEditorManager::AddFile(TFileData & FileData)
  314. {
  315. FileData.Timestamp = FileAge(FileData.FileName);
  316. FileData.Closed = false;
  317. FileData.UploadCompleteEvent = INVALID_HANDLE_VALUE;
  318. FileData.Opened = Now();
  319. FileData.Reupload = false;
  320. FFiles.push_back(FileData);
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TEditorManager::UploadComplete(int Index)
  324. {
  325. TFileData * FileData = &FFiles[Index];
  326. assert(FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE);
  327. CloseHandle(FileData->UploadCompleteEvent);
  328. FUploadCompleteEvents.erase(std::find(FUploadCompleteEvents.begin(),
  329. FUploadCompleteEvents.end(), FileData->UploadCompleteEvent));
  330. FileData->UploadCompleteEvent = INVALID_HANDLE_VALUE;
  331. if (FileData->Closed)
  332. {
  333. CloseFile(Index, false, true);
  334. }
  335. else if (FileData->Reupload)
  336. {
  337. FileData->Reupload = false;
  338. CheckFileChange(Index, true);
  339. }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TEditorManager::CloseProcess(int Index)
  343. {
  344. TFileData * FileData = &FFiles[Index];
  345. FProcesses.erase(std::find(FProcesses.begin(), FProcesses.end(), FileData->Process));
  346. CHECK(CloseHandle(FileData->Process));
  347. FileData->Process = INVALID_HANDLE_VALUE;
  348. }
  349. //---------------------------------------------------------------------------
  350. void __fastcall TEditorManager::CloseFile(int Index, bool IgnoreErrors, bool Delete)
  351. {
  352. TFileData * FileData = &FFiles[Index];
  353. if (FileData->Process != INVALID_HANDLE_VALUE)
  354. {
  355. CloseProcess(Index);
  356. }
  357. if (FileData->Monitor != INVALID_HANDLE_VALUE)
  358. {
  359. CHECK(FindCloseChangeNotification(FileData->Monitor));
  360. FMonitors.erase(std::find(FMonitors.begin(), FMonitors.end(), FileData->Monitor));
  361. FileData->Monitor = INVALID_HANDLE_VALUE;
  362. }
  363. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  364. {
  365. FileData->Closed = true;
  366. }
  367. else
  368. {
  369. AnsiString FileName = FileData->FileName;
  370. AnsiString LocalRootDirectory = FileData->Data.LocalRootDirectory;
  371. FFiles.erase(FFiles.begin() + Index);
  372. if (Delete)
  373. {
  374. if (!RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false) &&
  375. !IgnoreErrors)
  376. {
  377. throw Exception(FMTLOAD(DELETE_TEMP_EXECUTE_FILE_ERROR, (LocalRootDirectory)));
  378. }
  379. }
  380. }
  381. }
  382. //---------------------------------------------------------------------------
  383. void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
  384. {
  385. TFileData * FileData = &FFiles[Index];
  386. int NewTimestamp = FileAge(FileData->FileName);
  387. if (Force || (FileData->Timestamp != NewTimestamp))
  388. {
  389. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  390. {
  391. FileData->Reupload = true;
  392. Abort();
  393. }
  394. FileData->UploadCompleteEvent = CreateEvent(NULL, false, false, NULL);
  395. FUploadCompleteEvents.push_back(FileData->UploadCompleteEvent);
  396. FileData->Timestamp = NewTimestamp;
  397. try
  398. {
  399. assert(OnFileChange != NULL);
  400. OnFileChange(FileData->FileName, FileData->Data,
  401. FileData->UploadCompleteEvent);
  402. }
  403. catch(...)
  404. {
  405. // upload failed (was not even started)
  406. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  407. {
  408. UploadComplete(Index);
  409. }
  410. throw;
  411. }
  412. }
  413. }
  414. //---------------------------------------------------------------------------
  415. int __fastcall TEditorManager::FindFile(const TObject * Token)
  416. {
  417. int Index = -1;
  418. for (unsigned int i = 0; i < FFiles.size(); i++)
  419. {
  420. if (FFiles[i].Token == Token)
  421. {
  422. Index = i;
  423. break;
  424. }
  425. }
  426. return Index;
  427. }
  428. //---------------------------------------------------------------------------
  429. int __fastcall TEditorManager::WaitFor(unsigned int Count, const HANDLE * Handles,
  430. TWaitHandle WaitFor)
  431. {
  432. static const unsigned int Offset = MAXIMUM_WAIT_OBJECTS;
  433. int Result = -1;
  434. unsigned int Start = 0;
  435. while ((Result < 0) && (Start < Count))
  436. {
  437. unsigned int C = (Count - Start > Offset ? Offset : Count - Start);
  438. unsigned int WaitResult = WaitForMultipleObjects(C, Handles + Start, false, 0);
  439. if (WaitResult == WAIT_FAILED)
  440. {
  441. throw Exception(LoadStr(WATCH_ERROR_GENERAL));
  442. }
  443. else if (WaitResult != WAIT_TIMEOUT)
  444. {
  445. // WAIT_OBJECT_0 is zero
  446. assert(WaitResult < WAIT_OBJECT_0 + Count);
  447. HANDLE Handle = Handles[WaitResult - WAIT_OBJECT_0];
  448. for (unsigned int i = 0; i < FFiles.size(); i++)
  449. {
  450. TFileData * Data = &FFiles[i];
  451. HANDLE FHandle;
  452. switch (WaitFor)
  453. {
  454. case MONITOR:
  455. FHandle = Data->Monitor;
  456. break;
  457. case PROCESS:
  458. FHandle = Data->Process;
  459. break;
  460. case EVENT:
  461. FHandle = Data->UploadCompleteEvent;
  462. break;
  463. };
  464. if (FHandle == Handle)
  465. {
  466. Result = Start + i;
  467. break;
  468. }
  469. }
  470. assert(Result >= 0);
  471. }
  472. Start += C;
  473. }
  474. return Result;
  475. }
  476. //---------------------------------------------------------------------------