TerminalManager.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include "TerminalManager.h"
  6. #include <Authenticate.h>
  7. #include "CustomScpExplorer.h"
  8. #include "LogMemo.h"
  9. #include "NonVisual.h"
  10. #include "WinConfiguration.h"
  11. #include "Tools.h"
  12. #include <Log.h>
  13. #include <Common.h>
  14. #include <CoreMain.h>
  15. #include <GUITools.h>
  16. #include <TextsWin.h>
  17. #include <TextsCore.h>
  18. #include <Progress.h>
  19. #include <Exceptions.h>
  20. #include <VCLCommon.h>
  21. #include <WinApi.h>
  22. //---------------------------------------------------------------------------
  23. #pragma package(smart_init)
  24. //---------------------------------------------------------------------------
  25. TTerminalManager * TTerminalManager::FInstance = NULL;
  26. //---------------------------------------------------------------------------
  27. __fastcall TManagedTerminal::TManagedTerminal(TSessionData * SessionData,
  28. TConfiguration * Configuration) :
  29. TTerminal(SessionData, Configuration),
  30. Color((TColor)SessionData->Color), SynchronizeBrowsing(SessionData->SynchronizeBrowsing),
  31. LocalDirectory(::ExpandFileName(SessionData->LocalDirectory)),
  32. RemoteDirectory(SessionData->RemoteDirectory),
  33. LocalExplorerState(NULL), RemoteExplorerState(NULL),
  34. ReopenStart(0), DirectoryLoaded(Now()), TerminalThread(NULL)
  35. {
  36. }
  37. //---------------------------------------------------------------------------
  38. __fastcall TManagedTerminal::~TManagedTerminal()
  39. {
  40. delete LocalExplorerState;
  41. delete RemoteExplorerState;
  42. }
  43. //---------------------------------------------------------------------------
  44. //---------------------------------------------------------------------------
  45. TTerminalManager * __fastcall TTerminalManager::Instance(bool ForceCreation)
  46. {
  47. if (!FInstance && ForceCreation)
  48. {
  49. FInstance = new TTerminalManager();
  50. }
  51. return FInstance;
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TTerminalManager::DestroyInstance()
  55. {
  56. assert(FInstance);
  57. SAFE_DESTROY(FInstance);
  58. }
  59. //---------------------------------------------------------------------------
  60. __fastcall TTerminalManager::TTerminalManager() :
  61. TTerminalList(Configuration)
  62. {
  63. FQueueSection = new TCriticalSection();
  64. FLogMemo = NULL;
  65. FActiveTerminal = NULL;
  66. FScpExplorer = NULL;
  67. FDestroying = false;
  68. FTerminalPendingAction = tpNull;
  69. FDirectoryReadingStart = 0;
  70. FAuthenticateForm = NULL;
  71. FQueueWithEvent = NULL;
  72. FTaskbarList = NULL;
  73. FAuthenticating = 0;
  74. assert(Application && !Application->OnException);
  75. Application->OnException = ApplicationException;
  76. assert(Application->OnShowHint == NULL);
  77. Application->OnShowHint = ApplicationShowHint;
  78. assert(Application->OnMessage == NULL);
  79. Application->OnMessage = ApplicationMessage;
  80. assert(WinConfiguration->OnMasterPasswordPrompt == NULL);
  81. WinConfiguration->OnMasterPasswordPrompt = MasterPasswordPrompt;
  82. InitTaskbarButtonCreatedMessage();
  83. assert(Configuration && !Configuration->OnChange);
  84. Configuration->OnChange = ConfigurationChange;
  85. FOnLastTerminalClosed = NULL;
  86. FOnTerminalListChanged = NULL;
  87. FTerminalList = new TStringList();
  88. FQueues = new TList();
  89. FTerminationMessages = new TStringList();
  90. }
  91. //---------------------------------------------------------------------------
  92. __fastcall TTerminalManager::~TTerminalManager()
  93. {
  94. FreeAll();
  95. assert(!ScpExplorer);
  96. assert(Configuration->OnChange == ConfigurationChange);
  97. Configuration->OnChange = NULL;
  98. assert(Application && (Application->OnException == ApplicationException));
  99. Application->OnException = NULL;
  100. assert(Application->OnShowHint == ApplicationShowHint);
  101. Application->OnShowHint = ApplicationShowHint;
  102. assert(Application->OnMessage == ApplicationMessage);
  103. Application->OnMessage = NULL;
  104. assert(WinConfiguration->OnMasterPasswordPrompt == MasterPasswordPrompt);
  105. WinConfiguration->OnMasterPasswordPrompt = NULL;
  106. delete FQueues;
  107. delete FTerminationMessages;
  108. delete FTerminalList;
  109. delete FAuthenticateForm;
  110. delete FQueueSection;
  111. ReleaseTaskbarList();
  112. }
  113. //---------------------------------------------------------------------------
  114. TTerminalQueue * __fastcall TTerminalManager::NewQueue(TTerminal * Terminal)
  115. {
  116. TTerminalQueue * Queue = new TTerminalQueue(Terminal, Configuration);
  117. Queue->TransfersLimit = GUIConfiguration->QueueTransfersLimit;
  118. Queue->Enabled = WinConfiguration->EnableQueueByDefault;
  119. Queue->OnQueryUser = TerminalQueryUser;
  120. Queue->OnPromptUser = TerminalPromptUser;
  121. Queue->OnShowExtendedException = TerminalShowExtendedException;
  122. Queue->OnEvent = QueueEvent;
  123. return Queue;
  124. }
  125. //---------------------------------------------------------------------------
  126. TTerminal * __fastcall TTerminalManager::CreateTerminal(TSessionData * Data)
  127. {
  128. return new TManagedTerminal(Data, Configuration);
  129. }
  130. //---------------------------------------------------------------------------
  131. TTerminal * __fastcall TTerminalManager::NewTerminal(TSessionData * Data)
  132. {
  133. FTerminalList->Clear();
  134. TTerminal * Terminal = TTerminalList::NewTerminal(Data);
  135. try
  136. {
  137. FQueues->Add(NewQueue(Terminal));
  138. FTerminationMessages->Add(L"");
  139. Terminal->OnQueryUser = TerminalQueryUser;
  140. Terminal->OnPromptUser = TerminalPromptUser;
  141. Terminal->OnDisplayBanner = TerminalDisplayBanner;
  142. Terminal->OnShowExtendedException = TerminalShowExtendedException;
  143. Terminal->OnProgress = OperationProgress;
  144. Terminal->OnFinished = OperationFinished;
  145. Terminal->OnDeleteLocalFile = DeleteLocalFile;
  146. Terminal->OnReadDirectoryProgress = TerminalReadDirectoryProgress;
  147. Terminal->OnInformation = TerminalInformation;
  148. if (!ActiveTerminal)
  149. {
  150. ActiveTerminal = Terminal;
  151. }
  152. }
  153. catch(...)
  154. {
  155. if (Terminal != NULL)
  156. {
  157. FreeTerminal(Terminal);
  158. }
  159. throw;
  160. }
  161. DoTerminalListChanged();
  162. return Terminal;
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TTerminalManager::FreeActiveTerminal()
  166. {
  167. if (FTerminalPendingAction == tpNull)
  168. {
  169. assert(ActiveTerminal);
  170. FreeTerminal(ActiveTerminal);
  171. }
  172. else
  173. {
  174. assert(FTerminalPendingAction == ::tpNone);
  175. FTerminalPendingAction = tpFree;
  176. }
  177. }
  178. //---------------------------------------------------------------------------
  179. void TTerminalManager::ConnectTerminal(TTerminal * Terminal, bool Reopen)
  180. {
  181. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  182. // it must be managed terminal, unless it is secondary terminal (of managed terminal)
  183. assert((ManagedTerminal != NULL) || (dynamic_cast<TSecondaryTerminal *>(Terminal) != NULL));
  184. // particularly when we are reconnecting RemoteDirectory of managed terminal
  185. // hold the last used remote directory as opposite to session data, which holds
  186. // the default remote directory.
  187. // make sure the last used directory is used, but the default is preserved too
  188. UnicodeString OrigRemoteDirectory = Terminal->SessionData->RemoteDirectory;
  189. try
  190. {
  191. TTerminalThread * TerminalThread = new TTerminalThread(Terminal);
  192. try
  193. {
  194. if (ManagedTerminal != NULL)
  195. {
  196. Terminal->SessionData->RemoteDirectory = ManagedTerminal->RemoteDirectory;
  197. if ((double)ManagedTerminal->ReopenStart == 0)
  198. {
  199. ManagedTerminal->ReopenStart = Now();
  200. }
  201. assert(ManagedTerminal->TerminalThread == NULL);
  202. ManagedTerminal->TerminalThread = TerminalThread;
  203. }
  204. TNotifyEvent OnIdle;
  205. ((TMethod*)&OnIdle)->Code = TerminalThreadIdle;
  206. TerminalThread->OnIdle = OnIdle;
  207. if (Reopen)
  208. {
  209. TerminalThread->TerminalReopen();
  210. }
  211. else
  212. {
  213. TerminalThread->TerminalOpen();
  214. }
  215. }
  216. __finally
  217. {
  218. if (ManagedTerminal != NULL)
  219. {
  220. ManagedTerminal->TerminalThread = NULL;
  221. }
  222. delete TerminalThread;
  223. }
  224. }
  225. __finally
  226. {
  227. Terminal->SessionData->RemoteDirectory = OrigRemoteDirectory;
  228. if (Terminal->Active && (ManagedTerminal != NULL))
  229. {
  230. ManagedTerminal->ReopenStart = 0;
  231. }
  232. }
  233. }
  234. //---------------------------------------------------------------------------
  235. void __fastcall TTerminalManager::TerminalThreadIdle(void * /*Data*/, TObject * /*Sender*/)
  236. {
  237. Application->ProcessMessages();
  238. }
  239. //---------------------------------------------------------------------------
  240. bool __fastcall TTerminalManager::ConnectActiveTerminalImpl(bool Reopen)
  241. {
  242. TTerminalPendingAction Action;
  243. bool Result;
  244. do
  245. {
  246. Action = tpNull;
  247. Result = false;
  248. try
  249. {
  250. assert(ActiveTerminal);
  251. bool ShowLogPending = false;
  252. if (Configuration->Logging && (WinConfiguration->LogView == lvWindow))
  253. {
  254. if (WinConfiguration->LogWindowOnStartup)
  255. {
  256. RequireLogForm(LogMemo);
  257. }
  258. else
  259. {
  260. ShowLogPending = true;
  261. }
  262. }
  263. ConnectTerminal(ActiveTerminal, Reopen);
  264. if (ScpExplorer)
  265. {
  266. assert(ActiveTerminal->Status == ssOpened);
  267. TerminalReady();
  268. }
  269. WinConfiguration->ClearTemporaryLoginData();
  270. if (LogForm && (WinConfiguration->LogView != lvWindow))
  271. {
  272. FreeLogForm();
  273. }
  274. if (ShowLogPending)
  275. {
  276. RequireLogForm(LogMemo);
  277. }
  278. Result = true;
  279. }
  280. catch(Exception & E)
  281. {
  282. assert(FTerminalPendingAction == tpNull);
  283. FTerminalPendingAction = ::tpNone;
  284. try
  285. {
  286. assert(ActiveTerminal != NULL);
  287. ActiveTerminal->ShowExtendedException(&E);
  288. Action = FTerminalPendingAction;
  289. }
  290. __finally
  291. {
  292. FTerminalPendingAction = tpNull;
  293. }
  294. }
  295. }
  296. while (Action == tpReconnect);
  297. if (Action == tpFree)
  298. {
  299. FreeActiveTerminal();
  300. }
  301. return Result;
  302. }
  303. //---------------------------------------------------------------------------
  304. bool __fastcall TTerminalManager::ConnectActiveTerminal()
  305. {
  306. switch (ActiveTerminal->SessionData->FSProtocol)
  307. {
  308. case fsSCPonly:
  309. Configuration->Usage->Inc(L"OpenedSessionsSCP");
  310. break;
  311. case fsSFTP:
  312. case fsSFTPonly:
  313. Configuration->Usage->Inc(L"OpenedSessionsSFTP");
  314. break;
  315. case fsFTP:
  316. if (ActiveTerminal->SessionData->Ftps == ftpsNone)
  317. {
  318. Configuration->Usage->Inc(L"OpenedSessionsFTP");
  319. }
  320. else
  321. {
  322. Configuration->Usage->Inc(L"OpenedSessionsFTPS");
  323. }
  324. break;
  325. }
  326. if (Configuration->LogToFile)
  327. {
  328. Configuration->Usage->Inc(L"OpenedSessionsLogToFile");
  329. }
  330. if (Configuration->LogActions)
  331. {
  332. Configuration->Usage->Inc(L"OpenedSessionsXmlLog");
  333. }
  334. std::auto_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  335. if (!ActiveTerminal->SessionData->IsSame(FactoryDefaults.get(), true))
  336. {
  337. Configuration->Usage->Inc(L"OpenedSessionsAdvanced");
  338. }
  339. // add only stored sessions to the jump list,
  340. // ad-hoc session cannot be reproduced from just a session name
  341. if (StoredSessions->FindSame(ActiveTerminal->SessionData) != NULL)
  342. {
  343. WinConfiguration->AddSessionToJumpList(ActiveTerminal->SessionData->SessionName);
  344. }
  345. bool Result = ConnectActiveTerminalImpl(false);
  346. if (Result && WinConfiguration->AutoOpenInPutty && CanOpenInPutty())
  347. {
  348. try
  349. {
  350. OpenInPutty();
  351. }
  352. catch(Exception & E)
  353. {
  354. ShowExtendedExceptionEx(NULL, &E);
  355. }
  356. }
  357. return Result;
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TTerminalManager::DisconnectActiveTerminal()
  361. {
  362. assert(ActiveTerminal);
  363. int Index = IndexOf(ActiveTerminal);
  364. TTerminalQueue * OldQueue;
  365. TTerminalQueue * NewQueue;
  366. OldQueue = reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  367. NewQueue = this->NewQueue(ActiveTerminal);
  368. FQueues->Items[Index] = NewQueue;
  369. ScpExplorer->Queue = NewQueue;
  370. delete OldQueue;
  371. }
  372. //---------------------------------------------------------------------------
  373. void __fastcall TTerminalManager::ReconnectActiveTerminal()
  374. {
  375. assert(ActiveTerminal);
  376. if (ScpExplorer)
  377. {
  378. if (ScpExplorer->Terminal == ActiveTerminal)
  379. {
  380. ScpExplorer->UpdateTerminal(ActiveTerminal);
  381. }
  382. }
  383. try
  384. {
  385. if (FTerminalPendingAction == tpNull)
  386. {
  387. ConnectActiveTerminalImpl(true);
  388. }
  389. else
  390. {
  391. FTerminalPendingAction = tpReconnect;
  392. }
  393. }
  394. catch(...)
  395. {
  396. FreeActiveTerminal();
  397. throw;
  398. }
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TTerminalManager::FreeAll()
  402. {
  403. FDestroying = true;
  404. try
  405. {
  406. while (Count)
  407. {
  408. FreeTerminal(Terminals[0]);
  409. }
  410. }
  411. __finally
  412. {
  413. FDestroying = false;
  414. }
  415. }
  416. //---------------------------------------------------------------------------
  417. void __fastcall TTerminalManager::FreeTerminal(TTerminal * Terminal)
  418. {
  419. try
  420. {
  421. if (ScpExplorer != NULL)
  422. {
  423. ScpExplorer->TerminalRemoved(Terminal);
  424. }
  425. if (Terminal->Active)
  426. {
  427. Terminal->Close();
  428. }
  429. }
  430. __finally
  431. {
  432. int Index = IndexOf(Terminal);
  433. FTerminalList->Clear();
  434. Extract(Terminal);
  435. TTerminalQueue * Queue;
  436. Queue = reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  437. FQueues->Delete(Index);
  438. FTerminationMessages->Delete(Index);
  439. if (ActiveTerminal && (Terminal == ActiveTerminal))
  440. {
  441. if ((Count > 0) && !FDestroying)
  442. {
  443. for (int i = 0; i < Count; i++)
  444. {
  445. if (Terminals[i]->Status == ssOpened)
  446. {
  447. ActiveTerminal = Terminals[i];
  448. break;
  449. }
  450. }
  451. if (ActiveTerminal == Terminal)
  452. {
  453. ActiveTerminal = Terminals[Index < Count ? Index : 0];
  454. }
  455. }
  456. else
  457. {
  458. ActiveTerminal = NULL;
  459. }
  460. }
  461. else
  462. {
  463. SaveTerminal(Terminal);
  464. }
  465. // only now all references to/from queue (particularly events to explorer)
  466. // are cleared
  467. delete Queue;
  468. delete Terminal;
  469. DoTerminalListChanged();
  470. }
  471. }
  472. //---------------------------------------------------------------------------
  473. void __fastcall TTerminalManager::SetScpExplorer(TCustomScpExplorerForm * value)
  474. {
  475. if (ScpExplorer != value)
  476. {
  477. // changing explorer is not supported yet
  478. assert(!ScpExplorer || !value);
  479. FScpExplorer = value;
  480. if (FScpExplorer)
  481. {
  482. FScpExplorer->Terminal = ActiveTerminal;
  483. FScpExplorer->Queue = ActiveQueue;
  484. FOnLastTerminalClosed = FScpExplorer->LastTerminalClosed;
  485. FOnTerminalListChanged = FScpExplorer->TerminalListChanged;
  486. UpdateTaskbarList();
  487. }
  488. else
  489. {
  490. FOnLastTerminalClosed = NULL;
  491. FOnTerminalListChanged = NULL;
  492. }
  493. }
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TTerminalManager::SetActiveTerminal(TTerminal * value)
  497. {
  498. if (ActiveTerminal != value)
  499. {
  500. // here used to be call to TCustomScpExporer::UpdateSessionData (now UpdateTerminal)
  501. // but it seems to be duplicate to call from TCustomScpExporer::TerminalChanging
  502. TTerminal * PActiveTerminal = ActiveTerminal;
  503. FActiveTerminal = NULL;
  504. FActiveTerminal = value;
  505. // moved from else block of next if (ActiveTerminal) statement
  506. // so ScpExplorer can update its caption
  507. UpdateAppTitle();
  508. if (ScpExplorer)
  509. {
  510. if (ActiveTerminal && (ActiveTerminal->Status == ssOpened))
  511. {
  512. TerminalReady();
  513. }
  514. else
  515. {
  516. ScpExplorer->Terminal = NULL;
  517. ScpExplorer->Queue = NULL;
  518. }
  519. }
  520. if (PActiveTerminal && !PActiveTerminal->Active)
  521. {
  522. SaveTerminal(PActiveTerminal);
  523. }
  524. if (ActiveTerminal)
  525. {
  526. if (!PActiveTerminal)
  527. {
  528. CreateLogMemo();
  529. }
  530. assert(LogMemo);
  531. LogMemo->SessionLog = ActiveTerminal->Log;
  532. int Index = ActiveTerminalIndex;
  533. if (!ActiveTerminal->Active && !FTerminationMessages->Strings[Index].IsEmpty())
  534. {
  535. UnicodeString Message = FTerminationMessages->Strings[Index];
  536. FTerminationMessages->Strings[Index] = L"";
  537. Exception * E = new ESshFatal(NULL, Message);
  538. try
  539. {
  540. // finally show pending terminal message,
  541. // this gives user also possibility to reconnect
  542. ActiveTerminal->ShowExtendedException(E);
  543. }
  544. __finally
  545. {
  546. delete E;
  547. }
  548. }
  549. }
  550. else
  551. {
  552. if (LogForm)
  553. {
  554. FreeLogForm();
  555. }
  556. FreeLogMemo();
  557. if (OnLastTerminalClosed)
  558. {
  559. OnLastTerminalClosed(this);
  560. }
  561. }
  562. }
  563. }
  564. //---------------------------------------------------------------------------
  565. void __fastcall TTerminalManager::UpdateAppTitle()
  566. {
  567. if (ScpExplorer)
  568. {
  569. UnicodeString NewTitle;
  570. if (ActiveTerminal)
  571. {
  572. NewTitle = FMTLOAD(APP_CAPTION, (ActiveTerminalTitle, AppName));
  573. }
  574. else
  575. {
  576. NewTitle = AppName;
  577. }
  578. if (!FProgressTitle.IsEmpty() && !ForegroundTask())
  579. {
  580. NewTitle = FProgressTitle + L" - " + NewTitle;
  581. }
  582. else if (ActiveTerminal && (ScpExplorer != NULL))
  583. {
  584. UnicodeString Path = ScpExplorer->PathForCaption();
  585. if (!Path.IsEmpty())
  586. {
  587. NewTitle = Path + L" - " + NewTitle;
  588. }
  589. }
  590. ScpExplorer->Caption = NewTitle;
  591. ScpExplorer->ApplicationTitleChanged();
  592. }
  593. }
  594. //---------------------------------------------------------------------------
  595. void __fastcall TTerminalManager::SaveTerminal(TTerminal * Terminal)
  596. {
  597. TSessionData * Data = StoredSessions->FindSame(Terminal->SessionData);
  598. if (Data != NULL)
  599. {
  600. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  601. assert(ManagedTerminal != NULL);
  602. bool Changed = false;
  603. if (Terminal->SessionData->UpdateDirectories)
  604. {
  605. Data->LocalDirectory = ManagedTerminal->LocalDirectory;
  606. Data->RemoteDirectory = ManagedTerminal->RemoteDirectory;
  607. Changed = true;
  608. }
  609. if (Data->SynchronizeBrowsing != ManagedTerminal->SynchronizeBrowsing)
  610. {
  611. Data->SynchronizeBrowsing = ManagedTerminal->SynchronizeBrowsing;
  612. Changed = true;
  613. }
  614. if (Changed)
  615. {
  616. // modified only, implicit
  617. StoredSessions->Save(false, false);
  618. }
  619. }
  620. }
  621. //---------------------------------------------------------------------------
  622. void __fastcall TTerminalManager::CreateLogMemo()
  623. {
  624. assert(!FLogMemo);
  625. assert(ActiveTerminal);
  626. FLogMemo = new TLogMemo(Application);
  627. try
  628. {
  629. FLogMemo->SessionLog = ActiveTerminal->Log;
  630. FLogMemo->PopupMenu = NonVisualDataModule->LogMemoPopup;
  631. }
  632. catch (...)
  633. {
  634. delete FLogMemo;
  635. throw;
  636. }
  637. }
  638. //---------------------------------------------------------------------------
  639. void __fastcall TTerminalManager::FreeLogMemo()
  640. {
  641. assert(LogMemo);
  642. LogMemo->PopupMenu = NULL;
  643. SAFE_DESTROY(FLogMemo);
  644. }
  645. //---------------------------------------------------------------------------
  646. void __fastcall TTerminalManager::HandleException(Exception * E)
  647. {
  648. // can be null for example when exception is thrown on login dialog
  649. if (ActiveTerminal != NULL)
  650. {
  651. ActiveTerminal->ShowExtendedException(E);
  652. }
  653. else
  654. {
  655. ShowExtendedException(E);
  656. }
  657. }
  658. //---------------------------------------------------------------------------
  659. void __fastcall TTerminalManager::ApplicationException(TObject * /*Sender*/,
  660. Exception * E)
  661. {
  662. HandleException(E);
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TTerminalManager::ApplicationShowHint(UnicodeString & HintStr,
  666. bool & /*CanShow*/, THintInfo & HintInfo)
  667. {
  668. TLabel * HintLabel = dynamic_cast<TLabel *>(HintInfo.HintControl);
  669. if ((HintLabel != NULL) && (HintLabel->Caption == HintStr))
  670. {
  671. // Hack for transfer setting labels.
  672. // Should be converted to something like HintLabel()
  673. HintInfo.HintPos = HintLabel->ClientToScreen(TPoint(0, 0));
  674. HintInfo.HintMaxWidth = HintLabel->Width;
  675. HintInfo.HideTimeout = 100000; // "almost" never
  676. }
  677. else if (dynamic_cast<TProgressBar *>(HintInfo.HintControl) != NULL)
  678. {
  679. // Hint is forcibly hidden in TProgressForm::FormHide
  680. HintInfo.HideTimeout = 100000; // "almost" never
  681. HintInfo.ReshowTimeout = 500; // updated each 0.5s
  682. }
  683. else
  684. {
  685. HintInfo.HintMaxWidth = 300;
  686. }
  687. }
  688. //---------------------------------------------------------------------------
  689. void __fastcall TTerminalManager::ApplicationMessage(TMsg & Msg, bool & /*Handled*/)
  690. {
  691. if (Msg.message == FTaskbarButtonCreatedMessage)
  692. {
  693. CreateTaskbarList();
  694. }
  695. }
  696. //---------------------------------------------------------------------------
  697. void __fastcall TTerminalManager::InitTaskbarButtonCreatedMessage()
  698. {
  699. FTaskbarButtonCreatedMessage = RegisterWindowMessage(L"TaskbarButtonCreated");
  700. HINSTANCE User32Library = LoadLibrary(L"user32.dll");
  701. ChangeWindowMessageFilterExProc ChangeWindowMessageFilterEx =
  702. (ChangeWindowMessageFilterExProc)GetProcAddress(User32Library, "ChangeWindowMessageFilterEx");
  703. if (ChangeWindowMessageFilterEx != NULL)
  704. {
  705. // without this we won't get TaskbarButtonCreated, when app is running elevated
  706. ChangeWindowMessageFilterEx(
  707. Application->Handle, FTaskbarButtonCreatedMessage, MSGFLT_ALLOW, NULL);
  708. }
  709. }
  710. //---------------------------------------------------------------------------
  711. void __fastcall TTerminalManager::CreateTaskbarList()
  712. {
  713. ReleaseTaskbarList();
  714. if(SUCCEEDED(CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL,
  715. IID_ITaskbarList3, (void **) &FTaskbarList)))
  716. {
  717. if (ScpExplorer != NULL)
  718. {
  719. UpdateTaskbarList();
  720. }
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TTerminalManager::ReleaseTaskbarList()
  725. {
  726. if (FTaskbarList != NULL)
  727. {
  728. FTaskbarList->Release();
  729. }
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TTerminalManager::UpdateTaskbarList()
  733. {
  734. ScpExplorer->UpdateTaskbarList(FTaskbarList);
  735. }
  736. //---------------------------------------------------------------------------
  737. void __fastcall TTerminalManager::DeleteLocalFile(const UnicodeString FileName, bool Alternative)
  738. {
  739. if (!RecursiveDeleteFile(FileName, (WinConfiguration->DeleteToRecycleBin != Alternative)))
  740. {
  741. throw EOSExtException(FMTLOAD(DELETE_LOCAL_FILE_ERROR, (FileName)));
  742. }
  743. }
  744. //---------------------------------------------------------------------------
  745. void __fastcall TTerminalManager::TerminalQueryUser(TObject * Sender,
  746. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  747. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * /*Arg*/)
  748. {
  749. UnicodeString HelpKeyword;
  750. TMessageParams MessageParams(Params);
  751. UnicodeString AQuery = Query;
  752. if (Params != NULL)
  753. {
  754. HelpKeyword = Params->HelpKeyword;
  755. if (FLAGSET(Params->Params, qpFatalAbort))
  756. {
  757. AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
  758. if (!MessageParams.TimerMessage.IsEmpty())
  759. {
  760. MessageParams.TimerMessage = FMTLOAD(WARN_FATAL_ERROR, (MessageParams.TimerMessage));
  761. }
  762. }
  763. }
  764. if (ScpExplorer)
  765. {
  766. Answer = ScpExplorer->MoreMessageDialog(AQuery, MoreMessages, Type, Answers,
  767. HelpKeyword, &MessageParams, dynamic_cast<TTerminal *>(Sender));
  768. }
  769. else
  770. {
  771. Answer = MoreMessageDialog(AQuery, MoreMessages, Type, Answers, HelpKeyword,
  772. &MessageParams);
  773. }
  774. }
  775. //---------------------------------------------------------------------------
  776. void __fastcall TTerminalManager::AuthenticateFormCancel(TObject * Sender)
  777. {
  778. TAuthenticateForm * Form = dynamic_cast<TAuthenticateForm *>(Sender);
  779. assert(Form != NULL);
  780. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Form->Terminal);
  781. // will be null e.g. for background transfers
  782. if (ManagedTerminal != NULL)
  783. {
  784. TTerminalThread * TerminalThread = ManagedTerminal->TerminalThread;
  785. // can be NULL for reconnects from transfers
  786. if ((TerminalThread != NULL) && !TerminalThread->Cancelling)
  787. {
  788. Form->Log(LoadStr(AUTH_CANCELLING));
  789. TerminalThread->Cancel();
  790. }
  791. }
  792. }
  793. //---------------------------------------------------------------------------
  794. TAuthenticateForm * __fastcall TTerminalManager::MakeAuthenticateForm(
  795. TTerminal * Terminal)
  796. {
  797. TAuthenticateForm * Dialog = SafeFormCreate<TAuthenticateForm>();
  798. Dialog->Init(Terminal);
  799. assert(Dialog->OnCancel == NULL);
  800. Dialog->OnCancel = AuthenticateFormCancel;
  801. return Dialog;
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TTerminalManager::FileNameInputDialogInitializeRenameBaseName(
  805. TObject * /*Sender*/, TInputDialogData * Data)
  806. {
  807. EditSelectBaseName(Data->Edit->Handle);
  808. }
  809. //---------------------------------------------------------------------------
  810. void __fastcall TTerminalManager::TerminalPromptUser(
  811. TTerminal * Terminal, TPromptKind Kind, UnicodeString Name, UnicodeString Instructions,
  812. TStrings * Prompts, TStrings * Results, bool & Result, void * /*Arg*/)
  813. {
  814. if (((Kind == pkPrompt) || (Kind == pkFileName)) && (FAuthenticateForm == NULL) &&
  815. (Terminal->Status != ssOpening))
  816. {
  817. assert(Instructions.IsEmpty());
  818. assert(Prompts->Count == 1);
  819. assert(bool(Prompts->Objects[0]));
  820. UnicodeString AResult = Results->Strings[0];
  821. TInputDialogInitialize InputDialogInitialize = NULL;
  822. if ((Kind == pkFileName) && !WinConfiguration->RenameWholeName)
  823. {
  824. InputDialogInitialize = FileNameInputDialogInitializeRenameBaseName;
  825. }
  826. Result = InputDialog(Name, Prompts->Strings[0], AResult, L"", NULL, false, InputDialogInitialize);
  827. if (Result)
  828. {
  829. Results->Strings[0] = AResult;
  830. }
  831. }
  832. else
  833. {
  834. TAuthenticateForm * AuthenticateForm = FAuthenticateForm;
  835. if (AuthenticateForm == NULL)
  836. {
  837. AuthenticateForm = MakeAuthenticateForm(Terminal);
  838. }
  839. try
  840. {
  841. Result = AuthenticateForm->PromptUser(Kind, Name, Instructions, Prompts, Results,
  842. (FAuthenticateForm != NULL), Terminal->StoredCredentialsTried);
  843. }
  844. __finally
  845. {
  846. if (FAuthenticateForm == NULL)
  847. {
  848. delete AuthenticateForm;
  849. }
  850. }
  851. }
  852. }
  853. //---------------------------------------------------------------------------
  854. void __fastcall TTerminalManager::TerminalDisplayBanner(
  855. TTerminal * Terminal, UnicodeString SessionName,
  856. const UnicodeString & Banner, bool & NeverShowAgain, int Options)
  857. {
  858. assert(FAuthenticateForm != NULL);
  859. TAuthenticateForm * AuthenticateForm = FAuthenticateForm;
  860. if (AuthenticateForm == NULL)
  861. {
  862. AuthenticateForm = MakeAuthenticateForm(Terminal);
  863. }
  864. try
  865. {
  866. AuthenticateForm->Banner(Banner, NeverShowAgain, Options);
  867. }
  868. __finally
  869. {
  870. if (FAuthenticateForm == NULL)
  871. {
  872. delete AuthenticateForm;
  873. }
  874. }
  875. }
  876. //---------------------------------------------------------------------------
  877. void __fastcall TTerminalManager::TerminalShowExtendedException(
  878. TTerminal * Terminal, Exception * E, void * /*Arg*/)
  879. {
  880. if (ScpExplorer)
  881. {
  882. ScpExplorer->ShowExtendedException(Terminal, E);
  883. }
  884. else
  885. {
  886. ShowExtendedExceptionEx(Terminal, E);
  887. }
  888. }
  889. //---------------------------------------------------------------------------
  890. static TDateTime DirectoryReadingProgressDelay(0, 0, 1, 500);
  891. //---------------------------------------------------------------------------
  892. void __fastcall TTerminalManager::TerminalReadDirectoryProgress(
  893. TObject * /*Sender*/, int Progress, bool & Cancel)
  894. {
  895. if (Progress == 0)
  896. {
  897. if (ScpExplorer != NULL)
  898. {
  899. ScpExplorer->LockWindow();
  900. }
  901. FDirectoryReadingStart = Now();
  902. if (!FProgressTitle.IsEmpty())
  903. {
  904. FProgressTitle = L"";
  905. UpdateAppTitle();
  906. }
  907. // Reset "was ESC ever pressed?" state
  908. GetAsyncKeyState(VK_ESCAPE);
  909. }
  910. else if (Progress < 0)
  911. {
  912. if (Progress == -2)
  913. {
  914. // cancelled
  915. if (ScpExplorer != NULL)
  916. {
  917. ScpExplorer->ReadDirectoryCancelled();
  918. }
  919. }
  920. else
  921. {
  922. if (ScpExplorer != NULL)
  923. {
  924. ScpExplorer->UnlockWindow();
  925. }
  926. FProgressTitle = L"";
  927. UpdateAppTitle();
  928. }
  929. }
  930. else
  931. {
  932. // If the least significant bit is set,
  933. // the key was pressed after the previous call to GetAsyncKeyState.
  934. int KeyState = GetAsyncKeyState(VK_ESCAPE);
  935. if (FLAGSET(KeyState, 0x01))
  936. {
  937. Cancel = true;
  938. }
  939. if ((Now() - FDirectoryReadingStart) >= DirectoryReadingProgressDelay)
  940. {
  941. FProgressTitle = FMTLOAD(DIRECTORY_READING_PROGRESS, (Progress));
  942. UpdateAppTitle();
  943. }
  944. }
  945. }
  946. //---------------------------------------------------------------------------
  947. void __fastcall TTerminalManager::TerminalInformation(
  948. TTerminal * Terminal, const UnicodeString & Str, bool /*Status*/, int Phase)
  949. {
  950. if (Phase == 1)
  951. {
  952. if (FAuthenticating == 0)
  953. {
  954. Busy(true);
  955. }
  956. FAuthenticating++;
  957. }
  958. else if (Phase == 0)
  959. {
  960. assert(FAuthenticating > 0);
  961. FAuthenticating--;
  962. if (FAuthenticating == 0)
  963. {
  964. Busy(false);
  965. }
  966. SAFE_DESTROY(FAuthenticateForm);
  967. }
  968. else
  969. {
  970. if (FAuthenticating > 0)
  971. {
  972. bool ShowPending = false;
  973. if (FAuthenticateForm == NULL)
  974. {
  975. FAuthenticateForm = MakeAuthenticateForm(Terminal);
  976. ShowPending = true;
  977. }
  978. FAuthenticateForm->Log(Str);
  979. if (ShowPending)
  980. {
  981. FAuthenticateForm->ShowAsModal();
  982. }
  983. }
  984. }
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall TTerminalManager::OperationFinished(::TFileOperation Operation,
  988. TOperationSide Side, bool Temp, const UnicodeString & FileName, bool Success,
  989. TOnceDoneOperation & OnceDoneOperation)
  990. {
  991. assert(ScpExplorer);
  992. ScpExplorer->OperationFinished(Operation, Side, Temp, FileName, Success,
  993. OnceDoneOperation);
  994. }
  995. //---------------------------------------------------------------------------
  996. void __fastcall TTerminalManager::OperationProgress(
  997. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  998. {
  999. if (ProgressData.InProgress)
  1000. {
  1001. FProgressTitle = FORMAT(L"%d%% %s",
  1002. (ProgressData.OverallProgress(),
  1003. TProgressForm::OperationName(ProgressData.Operation)));
  1004. }
  1005. else
  1006. {
  1007. FProgressTitle = L"";
  1008. }
  1009. UpdateAppTitle();
  1010. assert(ScpExplorer);
  1011. ScpExplorer->OperationProgress(ProgressData, Cancel);
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TTerminalManager::QueueEvent(TTerminalQueue * Queue, TQueueEvent Event)
  1015. {
  1016. TGuard Guard(FQueueSection);
  1017. FQueueWithEvent = Queue;
  1018. FQueueEvent = Event;
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. void __fastcall TTerminalManager::ConfigurationChange(TObject * /*Sender*/)
  1022. {
  1023. assert(Configuration);
  1024. assert(Configuration == WinConfiguration);
  1025. if (!Application->Terminated && Configuration->Logging &&
  1026. (WinConfiguration->LogView == lvWindow))
  1027. {
  1028. if (ActiveTerminal)
  1029. {
  1030. RequireLogForm(LogMemo);
  1031. }
  1032. }
  1033. else
  1034. {
  1035. FreeLogForm();
  1036. }
  1037. TTerminalQueue * Queue;
  1038. for (int Index = 0; Index < Count; Index++)
  1039. {
  1040. assert(Terminals[Index]->Log);
  1041. Terminals[Index]->Log->ReflectSettings();
  1042. Terminals[Index]->ActionLog->ReflectSettings();
  1043. Queue = reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  1044. Queue->TransfersLimit = GUIConfiguration->QueueTransfersLimit;
  1045. }
  1046. if (ScpExplorer)
  1047. {
  1048. ScpExplorer->ConfigurationChanged();
  1049. }
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. void __fastcall TTerminalManager::TerminalReady()
  1053. {
  1054. ScpExplorer->Terminal = ActiveTerminal;
  1055. ScpExplorer->Queue = ActiveQueue;
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. TStrings * __fastcall TTerminalManager::GetTerminalList()
  1059. {
  1060. if (FTerminalList->Count != Count)
  1061. {
  1062. for (int i = 0; i < Count; i++)
  1063. {
  1064. UnicodeString NameN;
  1065. UnicodeString Name = Terminals[i]->SessionData->SessionName;
  1066. int Number = 1;
  1067. NameN = Name;
  1068. while (FTerminalList->IndexOf(NameN) >= 0)
  1069. {
  1070. Number++;
  1071. NameN = FORMAT(L"%s (%d)", (Name, Number));
  1072. }
  1073. if (Number > 1)
  1074. {
  1075. Name = FORMAT(L"%s (%d)", (Name, Number));
  1076. }
  1077. FTerminalList->AddObject(Name, Terminals[i]);
  1078. }
  1079. }
  1080. return FTerminalList;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. int __fastcall TTerminalManager::GetActiveTerminalIndex()
  1084. {
  1085. return ActiveTerminal ? IndexOf(ActiveTerminal) : -1;
  1086. }
  1087. //---------------------------------------------------------------------------
  1088. void __fastcall TTerminalManager::SetActiveTerminalIndex(int value)
  1089. {
  1090. ActiveTerminal = Terminals[value];
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. UnicodeString __fastcall TTerminalManager::TerminalTitle(TTerminal * Terminal)
  1094. {
  1095. int Index = IndexOf(Terminal);
  1096. UnicodeString Result;
  1097. if (Index >= 0)
  1098. {
  1099. Result = TerminalList->Strings[Index];
  1100. }
  1101. else
  1102. {
  1103. // this is the case of background transfer sessions
  1104. Result = Terminal->SessionData->SessionName;
  1105. }
  1106. return Result;
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. UnicodeString __fastcall TTerminalManager::GetActiveTerminalTitle()
  1110. {
  1111. UnicodeString Result = ActiveTerminal ?
  1112. TerminalTitle(ActiveTerminal) : UnicodeString(L"");
  1113. return Result;
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. TTerminalQueue * __fastcall TTerminalManager::GetActiveQueue()
  1117. {
  1118. TTerminalQueue * Result = NULL;
  1119. if (ActiveTerminal != NULL)
  1120. {
  1121. Result = reinterpret_cast<TTerminalQueue *>(FQueues->Items[ActiveTerminalIndex]);
  1122. }
  1123. return Result;
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. void __fastcall TTerminalManager::CycleTerminals(bool Forward)
  1127. {
  1128. int Index = ActiveTerminalIndex;
  1129. Index += Forward ? 1 : -1;
  1130. if (Index < 0)
  1131. {
  1132. Index = Count-1;
  1133. }
  1134. else if (Index >= Count)
  1135. {
  1136. Index = 0;
  1137. }
  1138. ActiveTerminalIndex = Index;
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. bool __fastcall TTerminalManager::CanOpenInPutty()
  1142. {
  1143. return (ActiveTerminal != NULL) && !GUIConfiguration->PuttyPath.Trim().IsEmpty();
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TTerminalManager::OpenInPutty()
  1147. {
  1148. Configuration->Usage->Inc(L"OpenInPutty");
  1149. TSessionData * Data = new TSessionData(L"");
  1150. try
  1151. {
  1152. Data->Assign(ActiveTerminal->SessionData);
  1153. // putty does not support resolving environment variables in session settings
  1154. Data->ExpandEnvironmentVariables();
  1155. if (ActiveTerminal->TunnelLocalPortNumber != 0)
  1156. {
  1157. Data->ConfigureTunnel(ActiveTerminal->TunnelLocalPortNumber);
  1158. }
  1159. assert(ActiveTerminal != NULL);
  1160. OpenSessionInPutty(GUIConfiguration->PuttyPath, Data,
  1161. GUIConfiguration->PuttyPassword ? ActiveTerminal->Password : UnicodeString());
  1162. }
  1163. __finally
  1164. {
  1165. delete Data;
  1166. }
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. bool __fastcall TTerminalManager::NewSession()
  1170. {
  1171. bool Result;
  1172. TSessionData * Data = new TSessionData(L"");
  1173. try
  1174. {
  1175. Data->Assign(StoredSessions->DefaultSettings);
  1176. if (DoLoginDialog(StoredSessions, Data, loAddSession))
  1177. {
  1178. assert(Data->CanLogin);
  1179. ActiveTerminal = NewTerminal(Data);
  1180. Result = ConnectActiveTerminal();
  1181. }
  1182. }
  1183. __finally
  1184. {
  1185. delete Data;
  1186. }
  1187. return Result;
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TTerminalManager::Idle()
  1191. {
  1192. for (int Index = 0; Index < Count; Index++)
  1193. {
  1194. TTerminal * Terminal = Terminals[Index];
  1195. try
  1196. {
  1197. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  1198. assert(ManagedTerminal != NULL);
  1199. // make sure Idle is called on the thread that runs the terminal
  1200. if (ManagedTerminal->TerminalThread != NULL)
  1201. {
  1202. ManagedTerminal->TerminalThread->Idle();
  1203. }
  1204. else
  1205. {
  1206. if (Terminal->Active)
  1207. {
  1208. Terminal->Idle();
  1209. }
  1210. }
  1211. if (Terminal->Active)
  1212. {
  1213. assert(Index < FQueues->Count);
  1214. if (Index < FQueues->Count)
  1215. {
  1216. reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index])->Idle();
  1217. }
  1218. }
  1219. }
  1220. catch(Exception & E)
  1221. {
  1222. if (Terminal == ActiveTerminal)
  1223. {
  1224. // throw further, so that the exception is handled in proper place
  1225. // (particularly in broken-transfer reconnect handler, bug 72)
  1226. throw;
  1227. }
  1228. else
  1229. {
  1230. // we may not have inactive terminal, unless there is a explorer,
  1231. // also Idle is called from explorer anyway
  1232. assert(ScpExplorer != NULL);
  1233. if (ScpExplorer != NULL)
  1234. {
  1235. ScpExplorer->InactiveTerminalException(Terminal, &E);
  1236. }
  1237. if (!Terminal->Active)
  1238. {
  1239. // if session is lost, save the error message and rethrow it
  1240. // once the terminal gets activated
  1241. FTerminationMessages->Strings[Index] = E.Message;
  1242. }
  1243. }
  1244. }
  1245. }
  1246. TTerminalQueue * QueueWithEvent;
  1247. TQueueEvent QueueEvent;
  1248. {
  1249. TGuard Guard(FQueueSection);
  1250. QueueWithEvent = FQueueWithEvent;
  1251. FQueueWithEvent = NULL;
  1252. QueueEvent = FQueueEvent;
  1253. }
  1254. if (QueueWithEvent != NULL)
  1255. {
  1256. int Index = FQueues->IndexOf(QueueWithEvent);
  1257. // the session may not exist anymore
  1258. if (Index >= 0)
  1259. {
  1260. TTerminal * Terminal = Terminals[Index];
  1261. // we can hardly have a queue event without explorer
  1262. assert(ScpExplorer != NULL);
  1263. if (ScpExplorer != NULL)
  1264. {
  1265. ScpExplorer->QueueEvent(Terminal, QueueWithEvent, QueueEvent);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. //---------------------------------------------------------------------------
  1271. void __fastcall TTerminalManager::MasterPasswordPrompt()
  1272. {
  1273. if (GetCurrentThreadId() == MainThreadID)
  1274. {
  1275. if (!DoMasterPasswordDialog())
  1276. {
  1277. Abort();
  1278. }
  1279. }
  1280. else
  1281. {
  1282. // this can happen only when we keep cancelling all master password prompts
  1283. // as long as the sessing finally connects (session password has to be
  1284. // explictly typed in), and background transfer is started
  1285. Abort();
  1286. }
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. void __fastcall TTerminalManager::Move(TTerminal * Source, TTerminal * Target)
  1290. {
  1291. FTerminalList->Clear();
  1292. int SourceIndex = IndexOf(Source);
  1293. int TargetIndex = IndexOf(Target);
  1294. TTerminalList::Move(SourceIndex, TargetIndex);
  1295. FQueues->Move(SourceIndex, TargetIndex);
  1296. DoTerminalListChanged();
  1297. // when there are indexed sessions with the same name,
  1298. // the index may change when reordering the sessions
  1299. UpdateAppTitle();
  1300. }
  1301. //---------------------------------------------------------------------------
  1302. void __fastcall TTerminalManager::DoTerminalListChanged()
  1303. {
  1304. if (OnTerminalListChanged)
  1305. {
  1306. OnTerminalListChanged(this);
  1307. }
  1308. }