EditorManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. assert(FileData->CloseFlag == NULL);
  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 AnsiString RemoteDirectory,
  57. const AnsiString OriginalFileName, const AnsiString SessionName,
  58. TObject *& Token, 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 temp directory where the file already is so we download
  92. // it there again
  93. ExistingLocalDirectory = FileData->LocalDirectory;
  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, bool * CloseFlag, 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.CloseFlag = CloseFlag;
  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, bool * CloseFlag, HANDLE Process)
  189. {
  190. TFileData FileData;
  191. FileData.FileName = FileName;
  192. FileData.External = true;
  193. FileData.Process = Process;
  194. FileData.Token = NULL;
  195. FileData.CloseFlag = CloseFlag;
  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. TFileData * FileData = &FFiles[Index];
  223. FindNextChangeNotification(FileData->Monitor);
  224. CheckFileChange(Index, false);
  225. }
  226. }
  227. while (Index >= 0);
  228. }
  229. if (FProcesses.size() > 0)
  230. {
  231. do
  232. {
  233. Index = WaitFor(FProcesses.size(), &(FProcesses[0]), PROCESS);
  234. if (Index >= 0)
  235. {
  236. try
  237. {
  238. CheckFileChange(Index, false);
  239. }
  240. __finally
  241. {
  242. if (!EarlyClose(Index))
  243. {
  244. // CheckFileChange may fail (file is already being uploaded),
  245. // but we want to close handles anyway
  246. CloseFile(Index, false, true);
  247. }
  248. }
  249. }
  250. }
  251. while ((Index >= 0) && (FProcesses.size() > 0));
  252. }
  253. if (FUploadCompleteEvents.size() > 0)
  254. {
  255. do
  256. {
  257. Index = WaitFor(FUploadCompleteEvents.size(), &(FUploadCompleteEvents[0]),
  258. EVENT);
  259. if (Index >= 0)
  260. {
  261. UploadComplete(Index);
  262. }
  263. }
  264. while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
  265. }
  266. }
  267. //---------------------------------------------------------------------------
  268. bool __fastcall TEditorManager::EarlyClose(int Index)
  269. {
  270. TFileData * FileData = &FFiles[Index];
  271. bool Result =
  272. (FileData->Process != INVALID_HANDLE_VALUE) &&
  273. (Now() - FileData->Opened <=
  274. TDateTime(0, 0, static_cast<unsigned short>(WinConfiguration->Editor.EarlyClose), 0)) &&
  275. (FOnFileEarlyClosed != NULL);
  276. if (Result)
  277. {
  278. Result = false;
  279. FOnFileEarlyClosed(FileData->Data, FileData->CloseFlag, Result);
  280. if (Result)
  281. {
  282. // forget the associated process
  283. CloseProcess(Index);
  284. }
  285. }
  286. return Result;
  287. }
  288. //---------------------------------------------------------------------------
  289. void __fastcall TEditorManager::FileChanged(TObject * Token)
  290. {
  291. int Index = FindFile(Token);
  292. assert(Index >= 0);
  293. assert(!FFiles[Index].External);
  294. CheckFileChange(Index, true);
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TEditorManager::FileReload(TObject * Token)
  298. {
  299. int Index = FindFile(Token);
  300. assert(Index >= 0);
  301. TFileData * FileData = &FFiles[Index];
  302. assert(!FileData->External);
  303. OnFileReload(FileData->FileName, FileData->Data);
  304. }
  305. //---------------------------------------------------------------------------
  306. void __fastcall TEditorManager::FileClosed(TObject * Token)
  307. {
  308. int Index = FindFile(Token);
  309. assert(Index >= 0);
  310. assert(!FFiles[Index].External);
  311. CheckFileChange(Index, false);
  312. CloseFile(Index, false, true);
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TEditorManager::AddFile(TFileData & FileData)
  316. {
  317. FileData.Timestamp = FileAge(FileData.FileName);
  318. FileData.LocalDirectory = ExtractFilePath(FileData.FileName);
  319. FileData.Closed = false;
  320. FileData.UploadCompleteEvent = INVALID_HANDLE_VALUE;
  321. FileData.Opened = Now();
  322. FileData.Reupload = false;
  323. FFiles.push_back(FileData);
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TEditorManager::UploadComplete(int Index)
  327. {
  328. TFileData * FileData = &FFiles[Index];
  329. assert(FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE);
  330. CloseHandle(FileData->UploadCompleteEvent);
  331. FUploadCompleteEvents.erase(std::find(FUploadCompleteEvents.begin(),
  332. FUploadCompleteEvents.end(), FileData->UploadCompleteEvent));
  333. FileData->UploadCompleteEvent = INVALID_HANDLE_VALUE;
  334. if (FileData->Closed)
  335. {
  336. CloseFile(Index, false, true);
  337. }
  338. else if (FileData->Reupload)
  339. {
  340. FileData->Reupload = false;
  341. CheckFileChange(Index, true);
  342. }
  343. }
  344. //---------------------------------------------------------------------------
  345. void __fastcall TEditorManager::CloseProcess(int Index)
  346. {
  347. TFileData * FileData = &FFiles[Index];
  348. FProcesses.erase(std::find(FProcesses.begin(), FProcesses.end(), FileData->Process));
  349. CHECK(CloseHandle(FileData->Process));
  350. FileData->Process = INVALID_HANDLE_VALUE;
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TEditorManager::CloseFile(int Index, bool IgnoreErrors, bool Delete)
  354. {
  355. TFileData * FileData = &FFiles[Index];
  356. if (FileData->Process != INVALID_HANDLE_VALUE)
  357. {
  358. CloseProcess(Index);
  359. }
  360. if (FileData->Monitor != INVALID_HANDLE_VALUE)
  361. {
  362. CHECK(FindCloseChangeNotification(FileData->Monitor));
  363. FMonitors.erase(std::find(FMonitors.begin(), FMonitors.end(), FileData->Monitor));
  364. FileData->Monitor = INVALID_HANDLE_VALUE;
  365. }
  366. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  367. {
  368. FileData->Closed = true;
  369. }
  370. else
  371. {
  372. AnsiString FileName = FileData->FileName;
  373. AnsiString LocalDirectory = FileData->LocalDirectory;
  374. if (FileData->CloseFlag != NULL)
  375. {
  376. *FileData->CloseFlag = true;
  377. }
  378. FFiles.erase(FFiles.begin() + Index);
  379. if (Delete)
  380. {
  381. bool Deleted;
  382. if (WinConfiguration->ForceDeleteTempFolder)
  383. {
  384. Deleted = RecursiveDeleteFile(ExcludeTrailingBackslash(LocalDirectory), false);
  385. }
  386. else
  387. {
  388. Deleted = DeleteFile(FileName) && RemoveDir(LocalDirectory);
  389. }
  390. if (!Deleted && !IgnoreErrors)
  391. {
  392. throw Exception(FMTLOAD(DELETE_TEMP_EXECUTE_FILE_ERROR, (LocalDirectory)));
  393. }
  394. }
  395. }
  396. }
  397. //---------------------------------------------------------------------------
  398. void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
  399. {
  400. TFileData * FileData = &FFiles[Index];
  401. int NewTimestamp = FileAge(FileData->FileName);
  402. if (Force || (FileData->Timestamp != NewTimestamp))
  403. {
  404. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  405. {
  406. FileData->Reupload = true;
  407. Abort();
  408. }
  409. FileData->UploadCompleteEvent = CreateEvent(NULL, false, false, NULL);
  410. FUploadCompleteEvents.push_back(FileData->UploadCompleteEvent);
  411. FileData->Timestamp = NewTimestamp;
  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].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. //---------------------------------------------------------------------------