EditorManager.cpp 16 KB

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