EditorManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. TFileData * FileData = &FFiles[Index];
  222. FindNextChangeNotification(FileData->Monitor);
  223. CheckFileChange(Index, false);
  224. }
  225. }
  226. while (Index >= 0);
  227. }
  228. if (FProcesses.size() > 0)
  229. {
  230. do
  231. {
  232. Index = WaitFor(FProcesses.size(), &(FProcesses[0]), PROCESS);
  233. if (Index >= 0)
  234. {
  235. try
  236. {
  237. CheckFileChange(Index, false);
  238. }
  239. __finally
  240. {
  241. if (!EarlyClose(Index))
  242. {
  243. // CheckFileChange may fail (file is already being uploaded),
  244. // but we want to close handles anyway
  245. CloseFile(Index, false, true);
  246. }
  247. }
  248. }
  249. }
  250. while ((Index >= 0) && (FProcesses.size() > 0));
  251. }
  252. if (FUploadCompleteEvents.size() > 0)
  253. {
  254. do
  255. {
  256. Index = WaitFor(FUploadCompleteEvents.size(), &(FUploadCompleteEvents[0]),
  257. EVENT);
  258. if (Index >= 0)
  259. {
  260. UploadComplete(Index);
  261. }
  262. }
  263. while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
  264. }
  265. }
  266. //---------------------------------------------------------------------------
  267. bool __fastcall TEditorManager::EarlyClose(int Index)
  268. {
  269. TFileData * FileData = &FFiles[Index];
  270. bool Result =
  271. (FileData->Process != INVALID_HANDLE_VALUE) &&
  272. (Now() - FileData->Opened <=
  273. TDateTime(0, 0, static_cast<unsigned short>(WinConfiguration->Editor.EarlyClose), 0)) &&
  274. (FOnFileEarlyClosed != NULL);
  275. if (Result)
  276. {
  277. Result = false;
  278. FOnFileEarlyClosed(FileData->Data, Result);
  279. if (Result)
  280. {
  281. // forget the associated process
  282. CloseProcess(Index);
  283. }
  284. }
  285. return Result;
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TEditorManager::FileChanged(TObject * Token)
  289. {
  290. int Index = FindFile(Token);
  291. assert(Index >= 0);
  292. assert(!FFiles[Index].External);
  293. CheckFileChange(Index, true);
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TEditorManager::FileReload(TObject * Token)
  297. {
  298. int Index = FindFile(Token);
  299. assert(Index >= 0);
  300. TFileData * FileData = &FFiles[Index];
  301. assert(!FileData->External);
  302. OnFileReload(FileData->FileName, FileData->Data);
  303. FileData->Timestamp = FileAge(FileData->FileName);
  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.Closed = false;
  319. FileData.UploadCompleteEvent = INVALID_HANDLE_VALUE;
  320. FileData.Opened = Now();
  321. FileData.Reupload = false;
  322. FFiles.push_back(FileData);
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TEditorManager::UploadComplete(int Index)
  326. {
  327. TFileData * FileData = &FFiles[Index];
  328. assert(FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE);
  329. CloseHandle(FileData->UploadCompleteEvent);
  330. FUploadCompleteEvents.erase(std::find(FUploadCompleteEvents.begin(),
  331. FUploadCompleteEvents.end(), FileData->UploadCompleteEvent));
  332. FileData->UploadCompleteEvent = INVALID_HANDLE_VALUE;
  333. if (FileData->Closed)
  334. {
  335. CloseFile(Index, false, true);
  336. }
  337. else if (FileData->Reupload)
  338. {
  339. FileData->Reupload = false;
  340. CheckFileChange(Index, true);
  341. }
  342. }
  343. //---------------------------------------------------------------------------
  344. void __fastcall TEditorManager::CloseProcess(int Index)
  345. {
  346. TFileData * FileData = &FFiles[Index];
  347. FProcesses.erase(std::find(FProcesses.begin(), FProcesses.end(), FileData->Process));
  348. CHECK(CloseHandle(FileData->Process));
  349. FileData->Process = INVALID_HANDLE_VALUE;
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TEditorManager::CloseFile(int Index, bool IgnoreErrors, bool Delete)
  353. {
  354. TFileData * FileData = &FFiles[Index];
  355. if (FileData->Process != INVALID_HANDLE_VALUE)
  356. {
  357. CloseProcess(Index);
  358. }
  359. if (FileData->Monitor != INVALID_HANDLE_VALUE)
  360. {
  361. CHECK(FindCloseChangeNotification(FileData->Monitor));
  362. FMonitors.erase(std::find(FMonitors.begin(), FMonitors.end(), FileData->Monitor));
  363. FileData->Monitor = INVALID_HANDLE_VALUE;
  364. }
  365. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  366. {
  367. FileData->Closed = true;
  368. }
  369. else
  370. {
  371. AnsiString FileName = FileData->FileName;
  372. AnsiString LocalRootDirectory = FileData->Data.LocalRootDirectory;
  373. FFiles.erase(FFiles.begin() + Index);
  374. if (Delete)
  375. {
  376. if (!RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false) &&
  377. !IgnoreErrors)
  378. {
  379. throw Exception(FMTLOAD(DELETE_TEMP_EXECUTE_FILE_ERROR, (LocalRootDirectory)));
  380. }
  381. }
  382. }
  383. }
  384. //---------------------------------------------------------------------------
  385. void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
  386. {
  387. TFileData * FileData = &FFiles[Index];
  388. int NewTimestamp = FileAge(FileData->FileName);
  389. if (Force || (FileData->Timestamp != NewTimestamp))
  390. {
  391. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  392. {
  393. FileData->Reupload = true;
  394. Abort();
  395. }
  396. FileData->UploadCompleteEvent = CreateEvent(NULL, false, false, NULL);
  397. FUploadCompleteEvents.push_back(FileData->UploadCompleteEvent);
  398. FileData->Timestamp = NewTimestamp;
  399. try
  400. {
  401. assert(OnFileChange != NULL);
  402. OnFileChange(FileData->FileName, FileData->Data,
  403. FileData->UploadCompleteEvent);
  404. }
  405. catch(...)
  406. {
  407. // upload failed (was not even started)
  408. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  409. {
  410. UploadComplete(Index);
  411. }
  412. throw;
  413. }
  414. }
  415. }
  416. //---------------------------------------------------------------------------
  417. int __fastcall TEditorManager::FindFile(const TObject * Token)
  418. {
  419. int Index = -1;
  420. for (unsigned int i = 0; i < FFiles.size(); i++)
  421. {
  422. if (!FFiles[i].Closed && (FFiles[i].Token == Token))
  423. {
  424. Index = i;
  425. break;
  426. }
  427. }
  428. return Index;
  429. }
  430. //---------------------------------------------------------------------------
  431. int __fastcall TEditorManager::WaitFor(unsigned int Count, const HANDLE * Handles,
  432. TWaitHandle WaitFor)
  433. {
  434. static const unsigned int Offset = MAXIMUM_WAIT_OBJECTS;
  435. int Result = -1;
  436. unsigned int Start = 0;
  437. while ((Result < 0) && (Start < Count))
  438. {
  439. unsigned int C = (Count - Start > Offset ? Offset : Count - Start);
  440. unsigned int WaitResult = WaitForMultipleObjects(C, Handles + Start, false, 0);
  441. if (WaitResult == WAIT_FAILED)
  442. {
  443. throw Exception(LoadStr(WATCH_ERROR_GENERAL));
  444. }
  445. else if (WaitResult != WAIT_TIMEOUT)
  446. {
  447. // WAIT_OBJECT_0 is zero
  448. assert(WaitResult < WAIT_OBJECT_0 + Count);
  449. HANDLE Handle = Handles[WaitResult - WAIT_OBJECT_0];
  450. for (unsigned int i = 0; i < FFiles.size(); i++)
  451. {
  452. TFileData * Data = &FFiles[i];
  453. HANDLE FHandle;
  454. switch (WaitFor)
  455. {
  456. case MONITOR:
  457. FHandle = Data->Monitor;
  458. break;
  459. case PROCESS:
  460. FHandle = Data->Process;
  461. break;
  462. case EVENT:
  463. FHandle = Data->UploadCompleteEvent;
  464. break;
  465. };
  466. if (FHandle == Handle)
  467. {
  468. Result = Start + i;
  469. break;
  470. }
  471. }
  472. assert(Result >= 0);
  473. }
  474. Start += C;
  475. }
  476. return Result;
  477. }
  478. //---------------------------------------------------------------------------