TerminalManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "TerminalManager.h"
  5. #include "CustomScpExplorer.h"
  6. #include "LogMemo.h"
  7. #include "NonVisual.h"
  8. #include "WinConfiguration.h"
  9. #include <Log.h>
  10. #include <OperationStatus.h>
  11. #include <Common.h>
  12. #include <ScpMain.h>
  13. #include <TextsWin.h>
  14. #include <Progress.h>
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. //---------------------------------------------------------------------------
  18. TTerminalManager * TTerminalManager::FInstance = NULL;
  19. //---------------------------------------------------------------------------
  20. TTerminalManager * __fastcall TTerminalManager::Instance(bool ForceCreation)
  21. {
  22. if (!FInstance && ForceCreation)
  23. {
  24. FInstance = new TTerminalManager();
  25. }
  26. return FInstance;
  27. }
  28. //---------------------------------------------------------------------------
  29. void __fastcall TTerminalManager::DestroyInstance()
  30. {
  31. assert(FInstance);
  32. SAFE_DESTROY(FInstance);
  33. }
  34. //---------------------------------------------------------------------------
  35. __fastcall TTerminalManager::TTerminalManager() :
  36. TTerminalList(Configuration)
  37. {
  38. FLogMemo = NULL;
  39. FActiveTerminal = NULL;
  40. FScpExplorer = NULL;
  41. FDestroying = false;
  42. FTerminalPendingAction = tpNull;
  43. FProgress = -1;
  44. assert(Application && !Application->OnException);
  45. Application->OnException = ApplicationException;
  46. assert(Configuration && !Configuration->OnChange);
  47. Configuration->OnChange = ConfigurationChange;
  48. FOnLastTerminalClosed = NULL;
  49. FOnTerminalListChanged = NULL;
  50. FTerminalList = new TStringList();
  51. }
  52. //---------------------------------------------------------------------------
  53. __fastcall TTerminalManager::~TTerminalManager()
  54. {
  55. FreeAll();
  56. assert(!ScpExplorer);
  57. assert(Configuration->OnChange == ConfigurationChange);
  58. Configuration->OnChange = NULL;
  59. assert(Application && (Application->OnException == ApplicationException));
  60. Application->OnException = NULL;
  61. delete FTerminalList;
  62. }
  63. //---------------------------------------------------------------------------
  64. TTerminal * __fastcall TTerminalManager::NewTerminal(TSessionData * Data)
  65. {
  66. FTerminalList->Clear();
  67. TTerminal * Terminal = TTerminalList::NewTerminal(Data);
  68. try
  69. {
  70. Terminal->OnQueryUser = TerminalQueryUser;
  71. Terminal->OnProgress = OperationProgress;
  72. Terminal->OnFinished = OperationFinished;
  73. Terminal->OnDeleteLocalFile = DeleteLocalFile;
  74. if (!ActiveTerminal)
  75. {
  76. ActiveTerminal = Terminal;
  77. }
  78. }
  79. catch(...)
  80. {
  81. FreeTerminal(Terminal);
  82. throw;
  83. }
  84. if (OnTerminalListChanged)
  85. {
  86. OnTerminalListChanged(this);
  87. }
  88. return Terminal;
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TTerminalManager::FreeActiveTerminal()
  92. {
  93. if (FTerminalPendingAction == tpNull)
  94. {
  95. assert(ActiveTerminal);
  96. FreeTerminal(ActiveTerminal);
  97. }
  98. else
  99. {
  100. assert(FTerminalPendingAction == tpNone);
  101. FTerminalPendingAction = tpFree;
  102. }
  103. }
  104. //---------------------------------------------------------------------------
  105. bool __fastcall TTerminalManager::ConnectActiveTerminal()
  106. {
  107. TTerminalPendingAction Action;
  108. bool Result;
  109. do
  110. {
  111. Action = tpNull;
  112. Result = false;
  113. try
  114. {
  115. assert(ActiveTerminal);
  116. bool ShowLogPending = false;
  117. if (Configuration->Logging && (WinConfiguration->LogView == lvWindow))
  118. {
  119. if (WinConfiguration->LogWindowOnStartup)
  120. {
  121. RequireLogForm(LogMemo);
  122. }
  123. else
  124. {
  125. ShowLogPending = true;
  126. }
  127. }
  128. TOperationStatusForm * Form = new TOperationStatusForm(Application);
  129. Busy(true);
  130. try
  131. {
  132. Form->SecureShell = ActiveTerminal;
  133. Form->ShowAsModal();
  134. ActiveTerminal->Open();
  135. ActiveTerminal->DoStartup();
  136. }
  137. __finally
  138. {
  139. Busy(false);
  140. delete Form;
  141. }
  142. if (ScpExplorer)
  143. {
  144. assert(ActiveTerminal->Status == sshReady);
  145. TerminalReady();
  146. }
  147. WinConfiguration->ClearTemporaryLoginData();
  148. if (LogForm && (WinConfiguration->LogView != lvWindow))
  149. {
  150. FreeLogForm();
  151. }
  152. if (ShowLogPending)
  153. {
  154. RequireLogForm(LogMemo);
  155. }
  156. Result = true;
  157. }
  158. catch(Exception & E)
  159. {
  160. assert(FTerminalPendingAction == tpNull);
  161. FTerminalPendingAction = tpNone;
  162. try
  163. {
  164. ShowExtendedException(&E, this);
  165. Action = FTerminalPendingAction;
  166. }
  167. __finally
  168. {
  169. FTerminalPendingAction = tpNull;
  170. }
  171. }
  172. }
  173. while (Action == tpReconnect);
  174. if (Action == tpFree)
  175. {
  176. FreeActiveTerminal();
  177. }
  178. return Result;
  179. }
  180. //---------------------------------------------------------------------------
  181. void __fastcall TTerminalManager::ReconnectActiveTerminal()
  182. {
  183. assert(ActiveTerminal);
  184. TTerminal * Terminal;
  185. if (ScpExplorer)
  186. {
  187. TSessionData * Data = new TSessionData(ActiveTerminal->SessionData->Name);
  188. try
  189. {
  190. Data->Assign(ActiveTerminal->SessionData);
  191. if (ScpExplorer->Terminal == ActiveTerminal)
  192. {
  193. ScpExplorer->UpdateSessionData(Data);
  194. }
  195. Terminal = NewTerminal(Data);
  196. }
  197. __finally
  198. {
  199. delete Data;
  200. }
  201. }
  202. else
  203. {
  204. // otherwise the ScpExplorer would be created already
  205. assert(ActiveTerminal->Status < sshReady);
  206. Terminal = NewTerminal(ActiveTerminal->SessionData);
  207. }
  208. try
  209. {
  210. FreeTerminal(ActiveTerminal);
  211. ActiveTerminal = Terminal;
  212. if (FTerminalPendingAction == tpNull)
  213. {
  214. ConnectActiveTerminal();
  215. }
  216. else
  217. {
  218. FTerminalPendingAction = tpReconnect;
  219. }
  220. }
  221. catch(...)
  222. {
  223. FreeTerminal(Terminal);
  224. throw;
  225. }
  226. }
  227. //---------------------------------------------------------------------------
  228. void __fastcall TTerminalManager::FreeAll()
  229. {
  230. FDestroying = true;
  231. try
  232. {
  233. while (Count)
  234. {
  235. FreeTerminal(Terminals[0]);
  236. }
  237. }
  238. __finally
  239. {
  240. FDestroying = false;
  241. }
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TTerminalManager::FreeTerminal(TTerminal * Terminal)
  245. {
  246. try
  247. {
  248. Terminal->Active = false;
  249. }
  250. __finally
  251. {
  252. int Index = IndexOf(Terminal);
  253. FTerminalList->Clear();
  254. Extract(Terminal);
  255. if (ActiveTerminal && (Terminal == ActiveTerminal))
  256. {
  257. if ((Count > 0) && !FDestroying)
  258. {
  259. for (int i = 0; i < Count; i++)
  260. {
  261. if (Terminals[i]->Status == sshReady)
  262. {
  263. ActiveTerminal = Terminals[i];
  264. break;
  265. }
  266. }
  267. if (ActiveTerminal == Terminal)
  268. {
  269. ActiveTerminal = Terminals[Index];
  270. }
  271. }
  272. else
  273. {
  274. ActiveTerminal = NULL;
  275. }
  276. }
  277. else
  278. {
  279. SaveTerminal(Terminal);
  280. }
  281. delete Terminal;
  282. if (OnTerminalListChanged)
  283. {
  284. OnTerminalListChanged(this);
  285. }
  286. }
  287. }
  288. //---------------------------------------------------------------------------
  289. void __fastcall TTerminalManager::SetScpExplorer(TCustomScpExplorerForm * value)
  290. {
  291. if (ScpExplorer != value)
  292. {
  293. // changing explorer is not supported yet
  294. assert(!ScpExplorer || !value);
  295. FScpExplorer = value;
  296. if (FScpExplorer)
  297. {
  298. assert(!OnChangeTerminal);
  299. FScpExplorer->Terminal = ActiveTerminal;
  300. FOnLastTerminalClosed = FScpExplorer->LastTerminalClosed;
  301. FOnTerminalListChanged = FScpExplorer->TerminalListChanged;
  302. }
  303. else
  304. {
  305. FOnLastTerminalClosed = NULL;
  306. FOnTerminalListChanged = NULL;
  307. }
  308. }
  309. }
  310. //---------------------------------------------------------------------------
  311. void __fastcall TTerminalManager::SetActiveTerminal(TTerminal * value)
  312. {
  313. if (ActiveTerminal != value)
  314. {
  315. if (ActiveTerminal && ScpExplorer)
  316. {
  317. assert(!ScpExplorer->Terminal || (ScpExplorer->Terminal == ActiveTerminal));
  318. if (ScpExplorer->Terminal == ActiveTerminal)
  319. {
  320. ScpExplorer->UpdateSessionData();
  321. }
  322. }
  323. TTerminal * PActiveTerminal = ActiveTerminal;
  324. FActiveTerminal = NULL;
  325. if (OnChangeTerminal)
  326. {
  327. OnChangeTerminal(this);
  328. }
  329. FActiveTerminal = value;
  330. // moved from else block of next if (ActiveTerminal) statement
  331. // so ScpExplorer can update its caption
  332. UpdateAppTitle();
  333. if (ScpExplorer)
  334. {
  335. if (ActiveTerminal && (ActiveTerminal->Status == sshReady))
  336. {
  337. TerminalReady();
  338. }
  339. else
  340. {
  341. ScpExplorer->Terminal = NULL;
  342. }
  343. }
  344. if (PActiveTerminal && !PActiveTerminal->Active)
  345. {
  346. SaveTerminal(PActiveTerminal);
  347. }
  348. if (ActiveTerminal)
  349. {
  350. if (!PActiveTerminal)
  351. {
  352. CreateLogMemo();
  353. }
  354. assert(LogMemo);
  355. LogMemo->SessionLog = ActiveTerminal->Log;
  356. }
  357. else
  358. {
  359. if (LogForm)
  360. {
  361. FreeLogForm();
  362. }
  363. FreeLogMemo();
  364. if (OnLastTerminalClosed)
  365. {
  366. OnLastTerminalClosed(this);
  367. }
  368. }
  369. }
  370. }
  371. //---------------------------------------------------------------------------
  372. void __fastcall TTerminalManager::UpdateAppTitle()
  373. {
  374. AnsiString NewTitle;
  375. if (ActiveTerminal)
  376. {
  377. NewTitle = FMTLOAD(APP_CAPTION, (ActiveTerminalTitle, AppName));
  378. }
  379. else
  380. {
  381. NewTitle = AppName;
  382. }
  383. if (FProgress >= 0)
  384. {
  385. NewTitle = FORMAT("%d%% %s - %s",
  386. (FProgress, TProgressForm::OperationName(FOperation), NewTitle));
  387. }
  388. Application->Title = NewTitle;
  389. }
  390. //---------------------------------------------------------------------------
  391. void __fastcall TTerminalManager::SaveTerminal(TTerminal * Terminal)
  392. {
  393. if (!Terminal->SessionData->Name.IsEmpty())
  394. {
  395. TSessionData * Data;
  396. Data = (TSessionData *)StoredSessions->FindByName(Terminal->SessionData->Name);
  397. if (Data)
  398. {
  399. bool Changed = false;
  400. if (Terminal->SessionData->UpdateDirectories)
  401. {
  402. Data->LocalDirectory = Terminal->SessionData->LocalDirectory;
  403. Data->RemoteDirectory = Terminal->SessionData->RemoteDirectory;
  404. Changed = true;
  405. }
  406. if (Changed)
  407. {
  408. StoredSessions->Save();
  409. }
  410. }
  411. }
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TTerminalManager::CreateLogMemo()
  415. {
  416. assert(!FLogMemo);
  417. assert(ActiveTerminal);
  418. FLogMemo = new TLogMemo(Application);
  419. try
  420. {
  421. FLogMemo->SessionLog = ActiveTerminal->Log;
  422. FLogMemo->PopupMenu = NonVisualDataModule->LogMemoPopup;
  423. }
  424. catch (...)
  425. {
  426. delete FLogMemo;
  427. throw;
  428. }
  429. }
  430. //---------------------------------------------------------------------------
  431. void __fastcall TTerminalManager::FreeLogMemo()
  432. {
  433. assert(LogMemo);
  434. LogMemo->PopupMenu = NULL;
  435. SAFE_DESTROY(FLogMemo);
  436. }
  437. //---------------------------------------------------------------------------
  438. void __fastcall TTerminalManager::ApplicationException(TObject * Sender, Exception * E)
  439. {
  440. ShowExtendedException(E, Sender);
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TTerminalManager::DeleteLocalFile(const AnsiString FileName)
  444. {
  445. if (!RecursiveDeleteFile(FileName, WinConfiguration->DeleteToRecycleBin))
  446. {
  447. throw Exception(FMTLOAD(DELETE_LOCAL_FILE_ERROR, (FileName)));
  448. }
  449. }
  450. //---------------------------------------------------------------------------
  451. void __fastcall TTerminalManager::TerminalQueryUser(TObject * /*Sender*/,
  452. const AnsiString Query, TStrings * MoreMessages, int Answers,
  453. int Params, int & Answer, TQueryType Type)
  454. {
  455. AnsiString AQuery = Query;
  456. if (Params & qpFatalAbort)
  457. {
  458. AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
  459. }
  460. int MessageParams = 0;
  461. if (Params & qpNeverAskAgainCheck)
  462. {
  463. MessageParams |= mpNeverAskAgainCheck;
  464. }
  465. if (Params & qpAllowContinueOnError)
  466. {
  467. MessageParams |= mpAllowContinueOnError;
  468. }
  469. if (ScpExplorer)
  470. {
  471. Answer = ScpExplorer->MoreMessageDialog(AQuery, MoreMessages, Type, Answers, 0, MessageParams);
  472. }
  473. else
  474. {
  475. Answer = MoreMessageDialog(AQuery, MoreMessages, Type, Answers, 0, MessageParams);
  476. }
  477. }
  478. //---------------------------------------------------------------------------
  479. void __fastcall TTerminalManager::OperationFinished(::TFileOperation Operation,
  480. TOperationSide Side, bool DragDrop, const AnsiString FileName, bool Success,
  481. bool & DisconnectWhenFinished)
  482. {
  483. assert(ScpExplorer);
  484. ScpExplorer->OperationFinished(Operation, Side, DragDrop, FileName, Success,
  485. DisconnectWhenFinished);
  486. }
  487. //---------------------------------------------------------------------------
  488. void __fastcall TTerminalManager::OperationProgress(
  489. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  490. {
  491. FProgress = ProgressData.InProgress ? ProgressData.OverallProgress() : -1;
  492. FOperation = ProgressData.Operation;
  493. UpdateAppTitle();
  494. assert(ScpExplorer);
  495. ScpExplorer->OperationProgress(ProgressData, Cancel);
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TTerminalManager::ConfigurationChange(TObject * /*Sender*/)
  499. {
  500. assert(Configuration);
  501. assert(Configuration == WinConfiguration);
  502. if (!Application->Terminated && Configuration->Logging &&
  503. (WinConfiguration->LogView == lvWindow))
  504. {
  505. if (ActiveTerminal)
  506. {
  507. RequireLogForm(LogMemo);
  508. }
  509. }
  510. else
  511. {
  512. FreeLogForm();
  513. }
  514. if (ActiveTerminal)
  515. {
  516. assert(ActiveTerminal->Log);
  517. ActiveTerminal->Log->ReflectSettings();
  518. }
  519. if (ScpExplorer)
  520. {
  521. ScpExplorer->ConfigurationChanged();
  522. }
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TTerminalManager::TerminalReady()
  526. {
  527. ScpExplorer->Terminal = ActiveTerminal;
  528. if (OnChangeTerminal)
  529. {
  530. OnChangeTerminal(this);
  531. }
  532. }
  533. //---------------------------------------------------------------------------
  534. TStrings * __fastcall TTerminalManager::GetTerminalList()
  535. {
  536. if (FTerminalList->Count != Count)
  537. {
  538. for (int i = 0; i < Count; i++)
  539. {
  540. AnsiString NameN;
  541. AnsiString Name = Terminals[i]->SessionData->SessionName;
  542. int Number = 1;
  543. NameN = Name;
  544. while (FTerminalList->IndexOf(NameN) >= 0)
  545. {
  546. Number++;
  547. NameN = FORMAT("%s (%d)", (Name, Number));
  548. }
  549. if (Number > 1)
  550. {
  551. Name = FORMAT("%s (%d)", (Name, Number));
  552. }
  553. FTerminalList->AddObject(Name, Terminals[i]);
  554. }
  555. }
  556. return FTerminalList;
  557. }
  558. //---------------------------------------------------------------------------
  559. int __fastcall TTerminalManager::GetActiveTerminalIndex()
  560. {
  561. return ActiveTerminal ? IndexOf(ActiveTerminal) : -1;
  562. }
  563. //---------------------------------------------------------------------------
  564. void __fastcall TTerminalManager::SetActiveTerminalIndex(int value)
  565. {
  566. ActiveTerminal = Terminals[value];
  567. }
  568. //---------------------------------------------------------------------------
  569. AnsiString __fastcall TTerminalManager::GetActiveTerminalTitle()
  570. {
  571. AnsiString Result = ActiveTerminal ?
  572. TerminalList->Strings[IndexOf(ActiveTerminal)] : AnsiString("");
  573. return Result;
  574. }
  575. //---------------------------------------------------------------------------
  576. void __fastcall TTerminalManager::CycleTerminals(bool Forward)
  577. {
  578. int Index = ActiveTerminalIndex;
  579. Index += Forward ? 1 : -1;
  580. if (Index < 0)
  581. {
  582. Index = Count-1;
  583. }
  584. else if (Index >= Count)
  585. {
  586. Index = 0;
  587. }
  588. ActiveTerminalIndex = Index;
  589. }