TerminalManager.cpp 44 KB

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