EditorManager.cpp 15 KB

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