EditorManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. FileData.Monitor = INVALID_HANDLE_VALUE;
  204. AddFile(FileData, Data.release());
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TEditorManager::AddFileExternal(const UnicodeString FileName,
  208. TEditedFileData * AData, HANDLE Process)
  209. {
  210. std::unique_ptr<TEditedFileData> Data(AData);
  211. TFileData FileData;
  212. FileData.FileName = FileName;
  213. FileData.External = true;
  214. FileData.Process = Process;
  215. FileData.Token = NULL;
  216. UnicodeString FilePath = ExtractFilePath(FileData.FileName);
  217. FileData.Monitor =
  218. FindFirstChangeNotification(
  219. FilePath.c_str(), false, FILE_NOTIFY_CHANGE_LAST_WRITE);
  220. if (FileData.Monitor == INVALID_HANDLE_VALUE)
  221. {
  222. throw Exception(FMTLOAD(FILE_WATCH_ERROR, (FileData.FileName)));
  223. }
  224. FMonitors.push_back(FileData.Monitor);
  225. if (Process != INVALID_HANDLE_VALUE)
  226. {
  227. FProcesses.push_back(Process);
  228. }
  229. AddFile(FileData, Data.release());
  230. }
  231. //---------------------------------------------------------------------------
  232. void __fastcall TEditorManager::Check()
  233. {
  234. int Index;
  235. if (FMonitors.size() > 0)
  236. {
  237. do
  238. {
  239. Index = WaitFor(FMonitors.size(), &(FMonitors[0]), MONITOR);
  240. if (Index >= 0)
  241. {
  242. // let the editor finish writing to the file
  243. // (first to avoid uploading partially saved file, second
  244. // because the timestamp may change more than once during saving)
  245. Sleep(GUIConfiguration->KeepUpToDateChangeDelay);
  246. TFileData * FileData = &FFiles[Index];
  247. FindNextChangeNotification(FileData->Monitor);
  248. CheckFileChange(Index, false);
  249. }
  250. }
  251. while (Index >= 0);
  252. }
  253. if (FProcesses.size() > 0)
  254. {
  255. do
  256. {
  257. Index = WaitFor(FProcesses.size(), &(FProcesses[0]), PROCESS);
  258. if (Index >= 0)
  259. {
  260. try
  261. {
  262. CheckFileChange(Index, false);
  263. }
  264. __finally
  265. {
  266. if (!EarlyClose(Index))
  267. {
  268. // CheckFileChange may fail (file is already being uploaded),
  269. // but we want to close handles anyway
  270. CloseFile(Index, false, true);
  271. }
  272. }
  273. }
  274. }
  275. while ((Index >= 0) && (FProcesses.size() > 0));
  276. }
  277. if (FUploadCompleteEvents.size() > 0)
  278. {
  279. do
  280. {
  281. Index = WaitFor(FUploadCompleteEvents.size(), &(FUploadCompleteEvents[0]),
  282. EVENT);
  283. if (Index >= 0)
  284. {
  285. UploadComplete(Index);
  286. }
  287. }
  288. while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
  289. }
  290. }
  291. //---------------------------------------------------------------------------
  292. bool __fastcall TEditorManager::EarlyClose(int Index)
  293. {
  294. TFileData * FileData = &FFiles[Index];
  295. bool Result =
  296. (FileData->Process != INVALID_HANDLE_VALUE) &&
  297. (Now() - FileData->Opened <=
  298. TDateTime(0, 0, static_cast<unsigned short>(WinConfiguration->Editor.EarlyClose), 0)) &&
  299. (FOnFileEarlyClosed != NULL);
  300. if (Result)
  301. {
  302. Result = false;
  303. FOnFileEarlyClosed(FileData->Data, Result);
  304. if (Result)
  305. {
  306. // forget the associated process
  307. CloseProcess(Index);
  308. }
  309. }
  310. return Result;
  311. }
  312. //---------------------------------------------------------------------------
  313. void __fastcall TEditorManager::FileChanged(TObject * Token)
  314. {
  315. int Index = FindFile(Token);
  316. DebugAssert(Index >= 0);
  317. DebugAssert(!FFiles[Index].External);
  318. CheckFileChange(Index, true);
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TEditorManager::FileReload(TObject * Token)
  322. {
  323. int Index = FindFile(Token);
  324. DebugAssert(Index >= 0);
  325. TFileData * FileData = &FFiles[Index];
  326. DebugAssert(!FileData->External);
  327. OnFileReload(FileData->FileName, FileData->Data);
  328. FileAge(FileData->FileName, FileData->Timestamp);
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TEditorManager::FileClosed(TObject * Token, bool Forced)
  332. {
  333. int Index = FindFile(Token);
  334. DebugAssert(Index >= 0);
  335. DebugAssert(!FFiles[Index].External);
  336. CheckFileChange(Index, false);
  337. CloseFile(Index, false, !Forced);
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TEditorManager::AddFile(TFileData & FileData, TEditedFileData * AData)
  341. {
  342. std::unique_ptr<TEditedFileData> Data(AData);
  343. FileAge(FileData.FileName, FileData.Timestamp);
  344. FileData.Closed = false;
  345. FileData.UploadCompleteEvent = INVALID_HANDLE_VALUE;
  346. FileData.Opened = Now();
  347. FileData.Reupload = false;
  348. FileData.Saves = 0;
  349. FileData.Data = Data.get();
  350. FFiles.push_back(FileData);
  351. Data.release(); // ownership passed
  352. }
  353. //---------------------------------------------------------------------------
  354. void __fastcall TEditorManager::UploadComplete(int Index)
  355. {
  356. TFileData * FileData = &FFiles[Index];
  357. DebugAssert(FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE);
  358. CloseHandle(FileData->UploadCompleteEvent);
  359. FUploadCompleteEvents.erase(std::find(FUploadCompleteEvents.begin(),
  360. FUploadCompleteEvents.end(), FileData->UploadCompleteEvent));
  361. FileData->UploadCompleteEvent = INVALID_HANDLE_VALUE;
  362. if (FileData->Closed)
  363. {
  364. CloseFile(Index, false, true);
  365. }
  366. else
  367. {
  368. if (FileData->Reupload)
  369. {
  370. FileData->Reupload = false;
  371. CheckFileChange(Index, true);
  372. }
  373. else if ((FileData->Token != NULL) && (FOnFileUploadComplete != NULL))
  374. {
  375. FOnFileUploadComplete(FileData->Token);
  376. }
  377. }
  378. }
  379. //---------------------------------------------------------------------------
  380. void __fastcall TEditorManager::CloseProcess(int Index)
  381. {
  382. TFileData * FileData = &FFiles[Index];
  383. FProcesses.erase(std::find(FProcesses.begin(), FProcesses.end(), FileData->Process));
  384. DebugCheck(CloseHandle(FileData->Process));
  385. FileData->Process = INVALID_HANDLE_VALUE;
  386. }
  387. //---------------------------------------------------------------------------
  388. void __fastcall TEditorManager::ReleaseFile(int Index)
  389. {
  390. TFileData * FileData = &FFiles[Index];
  391. delete FileData->Data;
  392. FileData->Data = NULL;
  393. }
  394. //---------------------------------------------------------------------------
  395. bool __fastcall TEditorManager::CloseFile(int Index, bool IgnoreErrors, bool Delete)
  396. {
  397. bool Result = false;
  398. TFileData * FileData = &FFiles[Index];
  399. if (FileData->Process != INVALID_HANDLE_VALUE)
  400. {
  401. CloseProcess(Index);
  402. }
  403. if (FileData->Monitor != INVALID_HANDLE_VALUE)
  404. {
  405. DebugCheck(FindCloseChangeNotification(FileData->Monitor));
  406. FMonitors.erase(std::find(FMonitors.begin(), FMonitors.end(), FileData->Monitor));
  407. FileData->Monitor = INVALID_HANDLE_VALUE;
  408. }
  409. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  410. {
  411. FileData->Closed = true;
  412. }
  413. else
  414. {
  415. UnicodeString FileName = FileData->FileName;
  416. UnicodeString LocalRootDirectory = FileData->Data->LocalRootDirectory;
  417. ReleaseFile(Index);
  418. FFiles.erase(FFiles.begin() + Index);
  419. Result = true;
  420. if (Delete)
  421. {
  422. if (!RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false) &&
  423. !IgnoreErrors)
  424. {
  425. throw Exception(FMTLOAD(DELETE_TEMP_EXECUTE_FILE_ERROR, (LocalRootDirectory)));
  426. }
  427. }
  428. }
  429. return Result;
  430. }
  431. //---------------------------------------------------------------------------
  432. void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
  433. {
  434. TFileData * FileData = &FFiles[Index];
  435. TDateTime NewTimestamp;
  436. FileAge(FileData->FileName, NewTimestamp);
  437. if (Force || (FileData->Timestamp != NewTimestamp))
  438. {
  439. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  440. {
  441. FileData->Reupload = true;
  442. Abort();
  443. }
  444. FileData->UploadCompleteEvent = CreateEvent(NULL, false, false, NULL);
  445. FUploadCompleteEvents.push_back(FileData->UploadCompleteEvent);
  446. FileData->Timestamp = NewTimestamp;
  447. FileData->Saves++;
  448. if (FileData->Saves == 1)
  449. {
  450. Configuration->Usage->Inc(L"RemoteFilesSaved");
  451. }
  452. Configuration->Usage->Inc(L"RemoteFileSaves");
  453. try
  454. {
  455. DebugAssert(OnFileChange != NULL);
  456. OnFileChange(FileData->FileName, FileData->Data,
  457. FileData->UploadCompleteEvent);
  458. }
  459. catch(...)
  460. {
  461. // upload failed (was not even started)
  462. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  463. {
  464. UploadComplete(Index);
  465. }
  466. throw;
  467. }
  468. }
  469. }
  470. //---------------------------------------------------------------------------
  471. int __fastcall TEditorManager::FindFile(const TObject * Token)
  472. {
  473. int Index = -1;
  474. for (unsigned int i = 0; i < FFiles.size(); i++)
  475. {
  476. if (!FFiles[i].Closed && (FFiles[i].Token == Token))
  477. {
  478. Index = i;
  479. break;
  480. }
  481. }
  482. return Index;
  483. }
  484. //---------------------------------------------------------------------------
  485. int __fastcall TEditorManager::WaitFor(unsigned int Count, const HANDLE * Handles,
  486. TWaitHandle WaitFor)
  487. {
  488. static const unsigned int Offset = MAXIMUM_WAIT_OBJECTS;
  489. int Result = -1;
  490. unsigned int Start = 0;
  491. while ((Result < 0) && (Start < Count))
  492. {
  493. unsigned int C = (Count - Start > Offset ? Offset : Count - Start);
  494. unsigned int WaitResult = WaitForMultipleObjects(C, Handles + Start, false, 0);
  495. if (WaitResult == WAIT_FAILED)
  496. {
  497. throw Exception(LoadStr(WATCH_ERROR_GENERAL));
  498. }
  499. else if (WaitResult != WAIT_TIMEOUT)
  500. {
  501. // WAIT_OBJECT_0 is zero
  502. DebugAssert(WaitResult < WAIT_OBJECT_0 + Count);
  503. HANDLE Handle = Handles[WaitResult - WAIT_OBJECT_0];
  504. for (unsigned int i = 0; i < FFiles.size(); i++)
  505. {
  506. TFileData * Data = &FFiles[i];
  507. HANDLE FHandle;
  508. switch (WaitFor)
  509. {
  510. case MONITOR:
  511. FHandle = Data->Monitor;
  512. break;
  513. case PROCESS:
  514. FHandle = Data->Process;
  515. break;
  516. case EVENT:
  517. FHandle = Data->UploadCompleteEvent;
  518. break;
  519. };
  520. if (FHandle == Handle)
  521. {
  522. Result = Start + i;
  523. break;
  524. }
  525. }
  526. DebugAssert(Result >= 0);
  527. }
  528. Start += C;
  529. }
  530. return Result;
  531. }
  532. //---------------------------------------------------------------------------