EditorManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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->Closed && (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,
  122. (FileData->Closed ? NULL : FileData->Token), Arg);
  123. }
  124. }
  125. //---------------------------------------------------------------------------
  126. bool __fastcall TEditorManager::CloseInternalEditors(TNotifyEvent CloseCallback)
  127. {
  128. // Traverse from end, as closing internal editor causes deletion of
  129. // respective file vector element.
  130. TObject * PrevToken = NULL;
  131. for (unsigned int i = FFiles.size(); i > 0; i--)
  132. {
  133. // Note that element may be deleted by external cause (like if external editor
  134. // is closed while "save confirmation" message is displayed).
  135. if (i <= FFiles.size())
  136. {
  137. TFileData * FileData = &FFiles[i - 1];
  138. // PrevToken is simple check not to ask same editor twice, however
  139. // it does not solve all posibilities
  140. if (!FileData->Closed && (FileData->Token != NULL) &&
  141. (FileData->Token != PrevToken))
  142. {
  143. CloseCallback(FileData->Token);
  144. }
  145. }
  146. }
  147. bool Result = true;
  148. for (unsigned int i = 0; i < FFiles.size(); i++)
  149. {
  150. TFileData * FileData = &FFiles[i];
  151. if (!FileData->Closed && (FileData->Token != NULL))
  152. {
  153. Result = false;
  154. break;
  155. }
  156. }
  157. return Result;
  158. }
  159. //---------------------------------------------------------------------------
  160. bool __fastcall TEditorManager::CloseExternalFilesWithoutProcess()
  161. {
  162. for (unsigned int i = FFiles.size(); i > 0; i--)
  163. {
  164. TFileData * FileData = &FFiles[i - 1];
  165. if (!FileData->Closed && FileData->External &&
  166. (FileData->Process == INVALID_HANDLE_VALUE))
  167. {
  168. CloseFile(i - 1, true, true);
  169. }
  170. }
  171. return true;
  172. }
  173. //---------------------------------------------------------------------------
  174. void __fastcall TEditorManager::AddFileInternal(const AnsiString FileName,
  175. const TEditedFileData & Data, TObject * Token)
  176. {
  177. TFileData FileData;
  178. FileData.FileName = FileName;
  179. FileData.External = false;
  180. FileData.Process = INVALID_HANDLE_VALUE;
  181. FileData.Token = Token;
  182. FileData.Data = Data;
  183. FileData.Monitor = INVALID_HANDLE_VALUE;
  184. AddFile(FileData);
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TEditorManager::AddFileExternal(const AnsiString FileName,
  188. const TEditedFileData & Data, HANDLE Process)
  189. {
  190. TFileData FileData;
  191. FileData.FileName = FileName;
  192. FileData.External = true;
  193. FileData.Process = Process;
  194. FileData.Token = NULL;
  195. FileData.Data = Data;
  196. FileData.Monitor = FindFirstChangeNotification(
  197. ExtractFilePath(FileData.FileName).c_str(), false,
  198. FILE_NOTIFY_CHANGE_LAST_WRITE);
  199. if (FileData.Monitor == INVALID_HANDLE_VALUE)
  200. {
  201. throw Exception(FMTLOAD(FILE_WATCH_ERROR, (FileData.FileName)));
  202. }
  203. FMonitors.push_back(FileData.Monitor);
  204. if (Process != INVALID_HANDLE_VALUE)
  205. {
  206. FProcesses.push_back(Process);
  207. }
  208. AddFile(FileData);
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TEditorManager::Check()
  212. {
  213. int Index;
  214. if (FMonitors.size() > 0)
  215. {
  216. do
  217. {
  218. Index = WaitFor(FMonitors.size(), &(FMonitors[0]), MONITOR);
  219. if (Index >= 0)
  220. {
  221. // let the editor finish writting to the file
  222. // (first to avoid uploading particually saved file, second
  223. // because the timestamp may change more than once during saving)
  224. Sleep(GUIConfiguration->KeepUpToDateChangeDelay);
  225. TFileData * FileData = &FFiles[Index];
  226. FindNextChangeNotification(FileData->Monitor);
  227. CheckFileChange(Index, false);
  228. }
  229. }
  230. while (Index >= 0);
  231. }
  232. if (FProcesses.size() > 0)
  233. {
  234. do
  235. {
  236. Index = WaitFor(FProcesses.size(), &(FProcesses[0]), PROCESS);
  237. if (Index >= 0)
  238. {
  239. try
  240. {
  241. CheckFileChange(Index, false);
  242. }
  243. __finally
  244. {
  245. if (!EarlyClose(Index))
  246. {
  247. // CheckFileChange may fail (file is already being uploaded),
  248. // but we want to close handles anyway
  249. CloseFile(Index, false, true);
  250. }
  251. }
  252. }
  253. }
  254. while ((Index >= 0) && (FProcesses.size() > 0));
  255. }
  256. if (FUploadCompleteEvents.size() > 0)
  257. {
  258. do
  259. {
  260. Index = WaitFor(FUploadCompleteEvents.size(), &(FUploadCompleteEvents[0]),
  261. EVENT);
  262. if (Index >= 0)
  263. {
  264. UploadComplete(Index);
  265. }
  266. }
  267. while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
  268. }
  269. }
  270. //---------------------------------------------------------------------------
  271. bool __fastcall TEditorManager::EarlyClose(int Index)
  272. {
  273. TFileData * FileData = &FFiles[Index];
  274. bool Result =
  275. (FileData->Process != INVALID_HANDLE_VALUE) &&
  276. (Now() - FileData->Opened <=
  277. TDateTime(0, 0, static_cast<unsigned short>(WinConfiguration->Editor.EarlyClose), 0)) &&
  278. (FOnFileEarlyClosed != NULL);
  279. if (Result)
  280. {
  281. Result = false;
  282. FOnFileEarlyClosed(FileData->Data, Result);
  283. if (Result)
  284. {
  285. // forget the associated process
  286. CloseProcess(Index);
  287. }
  288. }
  289. return Result;
  290. }
  291. //---------------------------------------------------------------------------
  292. void __fastcall TEditorManager::FileChanged(TObject * Token)
  293. {
  294. int Index = FindFile(Token);
  295. assert(Index >= 0);
  296. assert(!FFiles[Index].External);
  297. CheckFileChange(Index, true);
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall TEditorManager::FileReload(TObject * Token)
  301. {
  302. int Index = FindFile(Token);
  303. assert(Index >= 0);
  304. TFileData * FileData = &FFiles[Index];
  305. assert(!FileData->External);
  306. OnFileReload(FileData->FileName, FileData->Data);
  307. FileData->Timestamp = FileAge(FileData->FileName);
  308. }
  309. //---------------------------------------------------------------------------
  310. void __fastcall TEditorManager::FileClosed(TObject * Token)
  311. {
  312. int Index = FindFile(Token);
  313. assert(Index >= 0);
  314. assert(!FFiles[Index].External);
  315. CheckFileChange(Index, false);
  316. CloseFile(Index, false, true);
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TEditorManager::AddFile(TFileData & FileData)
  320. {
  321. FileData.Timestamp = FileAge(FileData.FileName);
  322. FileData.Closed = false;
  323. FileData.UploadCompleteEvent = INVALID_HANDLE_VALUE;
  324. FileData.Opened = Now();
  325. FileData.Reupload = false;
  326. FFiles.push_back(FileData);
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TEditorManager::UploadComplete(int Index)
  330. {
  331. TFileData * FileData = &FFiles[Index];
  332. assert(FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE);
  333. CloseHandle(FileData->UploadCompleteEvent);
  334. FUploadCompleteEvents.erase(std::find(FUploadCompleteEvents.begin(),
  335. FUploadCompleteEvents.end(), FileData->UploadCompleteEvent));
  336. FileData->UploadCompleteEvent = INVALID_HANDLE_VALUE;
  337. if (FileData->Closed)
  338. {
  339. CloseFile(Index, false, true);
  340. }
  341. else if (FileData->Reupload)
  342. {
  343. FileData->Reupload = false;
  344. CheckFileChange(Index, true);
  345. }
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TEditorManager::CloseProcess(int Index)
  349. {
  350. TFileData * FileData = &FFiles[Index];
  351. FProcesses.erase(std::find(FProcesses.begin(), FProcesses.end(), FileData->Process));
  352. CHECK(CloseHandle(FileData->Process));
  353. FileData->Process = INVALID_HANDLE_VALUE;
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall TEditorManager::CloseFile(int Index, bool IgnoreErrors, bool Delete)
  357. {
  358. TFileData * FileData = &FFiles[Index];
  359. if (FileData->Process != INVALID_HANDLE_VALUE)
  360. {
  361. CloseProcess(Index);
  362. }
  363. if (FileData->Monitor != INVALID_HANDLE_VALUE)
  364. {
  365. CHECK(FindCloseChangeNotification(FileData->Monitor));
  366. FMonitors.erase(std::find(FMonitors.begin(), FMonitors.end(), FileData->Monitor));
  367. FileData->Monitor = INVALID_HANDLE_VALUE;
  368. }
  369. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  370. {
  371. FileData->Closed = true;
  372. }
  373. else
  374. {
  375. AnsiString FileName = FileData->FileName;
  376. AnsiString LocalRootDirectory = FileData->Data.LocalRootDirectory;
  377. FFiles.erase(FFiles.begin() + Index);
  378. if (Delete)
  379. {
  380. if (!RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false) &&
  381. !IgnoreErrors)
  382. {
  383. throw Exception(FMTLOAD(DELETE_TEMP_EXECUTE_FILE_ERROR, (LocalRootDirectory)));
  384. }
  385. }
  386. }
  387. }
  388. //---------------------------------------------------------------------------
  389. void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
  390. {
  391. TFileData * FileData = &FFiles[Index];
  392. int NewTimestamp = FileAge(FileData->FileName);
  393. if (Force || (FileData->Timestamp != NewTimestamp))
  394. {
  395. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  396. {
  397. FileData->Reupload = true;
  398. Abort();
  399. }
  400. FileData->UploadCompleteEvent = CreateEvent(NULL, false, false, NULL);
  401. FUploadCompleteEvents.push_back(FileData->UploadCompleteEvent);
  402. FileData->Timestamp = NewTimestamp;
  403. try
  404. {
  405. assert(OnFileChange != NULL);
  406. OnFileChange(FileData->FileName, FileData->Data,
  407. FileData->UploadCompleteEvent);
  408. }
  409. catch(...)
  410. {
  411. // upload failed (was not even started)
  412. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  413. {
  414. UploadComplete(Index);
  415. }
  416. throw;
  417. }
  418. }
  419. }
  420. //---------------------------------------------------------------------------
  421. int __fastcall TEditorManager::FindFile(const TObject * Token)
  422. {
  423. int Index = -1;
  424. for (unsigned int i = 0; i < FFiles.size(); i++)
  425. {
  426. if (!FFiles[i].Closed && (FFiles[i].Token == Token))
  427. {
  428. Index = i;
  429. break;
  430. }
  431. }
  432. return Index;
  433. }
  434. //---------------------------------------------------------------------------
  435. int __fastcall TEditorManager::WaitFor(unsigned int Count, const HANDLE * Handles,
  436. TWaitHandle WaitFor)
  437. {
  438. static const unsigned int Offset = MAXIMUM_WAIT_OBJECTS;
  439. int Result = -1;
  440. unsigned int Start = 0;
  441. while ((Result < 0) && (Start < Count))
  442. {
  443. unsigned int C = (Count - Start > Offset ? Offset : Count - Start);
  444. unsigned int WaitResult = WaitForMultipleObjects(C, Handles + Start, false, 0);
  445. if (WaitResult == WAIT_FAILED)
  446. {
  447. throw Exception(LoadStr(WATCH_ERROR_GENERAL));
  448. }
  449. else if (WaitResult != WAIT_TIMEOUT)
  450. {
  451. // WAIT_OBJECT_0 is zero
  452. assert(WaitResult < WAIT_OBJECT_0 + Count);
  453. HANDLE Handle = Handles[WaitResult - WAIT_OBJECT_0];
  454. for (unsigned int i = 0; i < FFiles.size(); i++)
  455. {
  456. TFileData * Data = &FFiles[i];
  457. HANDLE FHandle;
  458. switch (WaitFor)
  459. {
  460. case MONITOR:
  461. FHandle = Data->Monitor;
  462. break;
  463. case PROCESS:
  464. FHandle = Data->Process;
  465. break;
  466. case EVENT:
  467. FHandle = Data->UploadCompleteEvent;
  468. break;
  469. };
  470. if (FHandle == Handle)
  471. {
  472. Result = Start + i;
  473. break;
  474. }
  475. }
  476. assert(Result >= 0);
  477. }
  478. Start += C;
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------