TerminalManager.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. ActiveTerminal = Terminals[Index < Count ? Index : Index - 1];
  458. }
  459. else
  460. {
  461. ActiveTerminal = NULL;
  462. }
  463. }
  464. else
  465. {
  466. SaveTerminal(Terminal);
  467. }
  468. // only now all references to/from queue (particularly events to explorer)
  469. // are cleared
  470. delete Queue;
  471. delete Terminal;
  472. DoTerminalListChanged();
  473. }
  474. }
  475. //---------------------------------------------------------------------------
  476. void __fastcall TTerminalManager::SetScpExplorer(TCustomScpExplorerForm * value)
  477. {
  478. if (ScpExplorer != value)
  479. {
  480. // changing explorer is not supported yet
  481. assert(!ScpExplorer || !value);
  482. FScpExplorer = value;
  483. if (FScpExplorer)
  484. {
  485. FScpExplorer->Terminal = ActiveTerminal;
  486. FScpExplorer->Queue = ActiveQueue;
  487. FOnLastTerminalClosed = FScpExplorer->LastTerminalClosed;
  488. FOnTerminalListChanged = FScpExplorer->TerminalListChanged;
  489. UpdateTaskbarList();
  490. }
  491. else
  492. {
  493. FOnLastTerminalClosed = NULL;
  494. FOnTerminalListChanged = NULL;
  495. }
  496. }
  497. }
  498. //---------------------------------------------------------------------------
  499. void __fastcall TTerminalManager::SetActiveTerminal(TTerminal * value)
  500. {
  501. DoSetActiveTerminal(value, false);
  502. }
  503. //---------------------------------------------------------------------------
  504. void __fastcall TTerminalManager::SetActiveTerminalWithAutoReconnect(TTerminal * value)
  505. {
  506. DoSetActiveTerminal(value, true);
  507. }
  508. //---------------------------------------------------------------------------
  509. void __fastcall TTerminalManager::DoSetActiveTerminal(TTerminal * value, bool AutoReconnect)
  510. {
  511. if (ActiveTerminal != value)
  512. {
  513. // here used to be call to TCustomScpExporer::UpdateSessionData (now UpdateTerminal)
  514. // but it seems to be duplicate to call from TCustomScpExporer::TerminalChanging
  515. TTerminal * PActiveTerminal = ActiveTerminal;
  516. FActiveTerminal = value;
  517. // moved from else block of next if (ActiveTerminal) statement
  518. // so ScpExplorer can update its caption
  519. UpdateAppTitle();
  520. if (ScpExplorer)
  521. {
  522. if (ActiveTerminal && (ActiveTerminal->Status == ssOpened))
  523. {
  524. TerminalReady();
  525. }
  526. else
  527. {
  528. ScpExplorer->Terminal = NULL;
  529. ScpExplorer->Queue = NULL;
  530. }
  531. }
  532. if (PActiveTerminal && !PActiveTerminal->Active)
  533. {
  534. SaveTerminal(PActiveTerminal);
  535. }
  536. if (ActiveTerminal)
  537. {
  538. if (!PActiveTerminal)
  539. {
  540. CreateLogMemo();
  541. }
  542. assert(LogMemo);
  543. LogMemo->SessionLog = ActiveTerminal->Log;
  544. int Index = ActiveTerminalIndex;
  545. if (!ActiveTerminal->Active && !FTerminationMessages->Strings[Index].IsEmpty())
  546. {
  547. UnicodeString Message = FTerminationMessages->Strings[Index];
  548. FTerminationMessages->Strings[Index] = L"";
  549. if (AutoReconnect)
  550. {
  551. ReconnectActiveTerminal();
  552. }
  553. else
  554. {
  555. Exception * E = new ESshFatal(NULL, Message);
  556. try
  557. {
  558. // finally show pending terminal message,
  559. // this gives user also possibility to reconnect
  560. ActiveTerminal->ShowExtendedException(E);
  561. }
  562. __finally
  563. {
  564. delete E;
  565. }
  566. }
  567. }
  568. }
  569. else
  570. {
  571. if (LogForm)
  572. {
  573. FreeLogForm();
  574. }
  575. FreeLogMemo();
  576. if (OnLastTerminalClosed)
  577. {
  578. OnLastTerminalClosed(this);
  579. }
  580. }
  581. if ((ActiveTerminal != NULL) && !ActiveTerminal->Active)
  582. {
  583. ConnectActiveTerminal();
  584. }
  585. }
  586. }
  587. //---------------------------------------------------------------------------
  588. void __fastcall TTerminalManager::QueueStatusUpdated()
  589. {
  590. UpdateAppTitle();
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TTerminalManager::UpdateAppTitle()
  594. {
  595. if (ScpExplorer)
  596. {
  597. UnicodeString NewTitle;
  598. if (ActiveTerminal)
  599. {
  600. NewTitle = FMTLOAD(APP_CAPTION, (ActiveTerminalTitle, AppName));
  601. }
  602. else
  603. {
  604. NewTitle = AppName;
  605. }
  606. UnicodeString QueueProgressTitle;
  607. if (!FForegroundProgressTitle.IsEmpty())
  608. {
  609. NewTitle = FForegroundProgressTitle + L" - " + NewTitle;
  610. }
  611. else if (!FProgressTitle.IsEmpty() && !ForegroundTask())
  612. {
  613. NewTitle = FProgressTitle + L" - " + NewTitle;
  614. }
  615. else if ((ScpExplorer != NULL) && (ScpExplorer->Handle != GetAncestor(GetActiveWindow(), GA_ROOTOWNER)) &&
  616. !(QueueProgressTitle = ScpExplorer->GetQueueProgressTitle()).IsEmpty())
  617. {
  618. NewTitle = QueueProgressTitle + L" - " + NewTitle;
  619. }
  620. else if (ActiveTerminal && (ScpExplorer != NULL))
  621. {
  622. UnicodeString Path = ScpExplorer->PathForCaption();
  623. if (!Path.IsEmpty())
  624. {
  625. NewTitle = Path + L" - " + NewTitle;
  626. }
  627. }
  628. ScpExplorer->Caption = NewTitle;
  629. ScpExplorer->ApplicationTitleChanged();
  630. }
  631. }
  632. //---------------------------------------------------------------------------
  633. void __fastcall TTerminalManager::SaveTerminal(TTerminal * Terminal)
  634. {
  635. TSessionData * Data = StoredSessions->FindSame(Terminal->SessionData);
  636. if (Data != NULL)
  637. {
  638. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  639. assert(ManagedTerminal != NULL);
  640. bool Changed = false;
  641. if (Terminal->SessionData->UpdateDirectories)
  642. {
  643. Data->LocalDirectory = ManagedTerminal->LocalDirectory;
  644. Data->RemoteDirectory = ManagedTerminal->RemoteDirectory;
  645. Data->SynchronizeBrowsing = ManagedTerminal->SynchronizeBrowsing;
  646. Changed = true;
  647. }
  648. if (Changed)
  649. {
  650. // modified only, implicit
  651. StoredSessions->Save(false, false);
  652. }
  653. }
  654. }
  655. //---------------------------------------------------------------------------
  656. void __fastcall TTerminalManager::CreateLogMemo()
  657. {
  658. assert(!FLogMemo);
  659. assert(ActiveTerminal);
  660. FLogMemo = new TLogMemo(Application);
  661. try
  662. {
  663. FLogMemo->SessionLog = ActiveTerminal->Log;
  664. FLogMemo->PopupMenu = NonVisualDataModule->LogMemoPopup;
  665. }
  666. catch (...)
  667. {
  668. delete FLogMemo;
  669. throw;
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TTerminalManager::FreeLogMemo()
  674. {
  675. assert(LogMemo);
  676. LogMemo->PopupMenu = NULL;
  677. SAFE_DESTROY(FLogMemo);
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TTerminalManager::HandleException(Exception * E)
  681. {
  682. // can be null for example when exception is thrown on login dialog
  683. if (ActiveTerminal != NULL)
  684. {
  685. ActiveTerminal->ShowExtendedException(E);
  686. }
  687. else
  688. {
  689. ShowExtendedException(E);
  690. }
  691. }
  692. //---------------------------------------------------------------------------
  693. void __fastcall TTerminalManager::ApplicationException(TObject * /*Sender*/,
  694. Exception * E)
  695. {
  696. HandleException(E);
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TTerminalManager::ApplicationShowHint(UnicodeString & HintStr,
  700. bool & /*CanShow*/, THintInfo & HintInfo)
  701. {
  702. TLabel * HintLabel = dynamic_cast<TLabel *>(HintInfo.HintControl);
  703. if ((HintLabel != NULL) && (HintLabel->Caption == HintStr))
  704. {
  705. // Hack for transfer setting labels.
  706. // Should be converted to something like HintLabel()
  707. HintInfo.HintPos = HintLabel->ClientToScreen(TPoint(0, 0));
  708. HintInfo.HintMaxWidth = HintLabel->Width;
  709. HintInfo.HideTimeout = 100000; // "almost" never
  710. }
  711. else if (dynamic_cast<TProgressBar *>(HintInfo.HintControl) != NULL)
  712. {
  713. // Hint is forcibly hidden in TProgressForm::FormHide
  714. HintInfo.HideTimeout = 100000; // "almost" never
  715. HintInfo.ReshowTimeout = 500; // updated each 0.5s
  716. }
  717. else
  718. {
  719. HintInfo.HintMaxWidth = 300;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TTerminalManager::ApplicationMessage(TMsg & Msg, bool & Handled)
  724. {
  725. if (Msg.message == FTaskbarButtonCreatedMessage)
  726. {
  727. CreateTaskbarList();
  728. }
  729. if ((Msg.message == WM_MOUSEWHEEL) && (ScpExplorer != NULL))
  730. {
  731. Handled = ScpExplorer->HandleMouseWheel(Msg.wParam, Msg.lParam);
  732. }
  733. }
  734. //---------------------------------------------------------------------------
  735. void __fastcall TTerminalManager::ApplicationModalBegin(TObject * /*Sender*/)
  736. {
  737. NonVisualDataModule->StartBusy();
  738. if (ScpExplorer != NULL)
  739. {
  740. ScpExplorer->SuspendWindowLock();
  741. }
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TTerminalManager::ApplicationModalEnd(TObject * /*Sender*/)
  745. {
  746. NonVisualDataModule->EndBusy();
  747. if (ScpExplorer != NULL)
  748. {
  749. ScpExplorer->ResumeWindowLock();
  750. }
  751. }
  752. //---------------------------------------------------------------------------
  753. void __fastcall TTerminalManager::InitTaskbarButtonCreatedMessage()
  754. {
  755. FTaskbarButtonCreatedMessage = RegisterWindowMessage(L"TaskbarButtonCreated");
  756. HINSTANCE User32Library = LoadLibrary(L"user32.dll");
  757. ChangeWindowMessageFilterExProc ChangeWindowMessageFilterEx =
  758. (ChangeWindowMessageFilterExProc)GetProcAddress(User32Library, "ChangeWindowMessageFilterEx");
  759. if (ChangeWindowMessageFilterEx != NULL)
  760. {
  761. // without this we won't get TaskbarButtonCreated, when app is running elevated
  762. ChangeWindowMessageFilterEx(
  763. Application->Handle, FTaskbarButtonCreatedMessage, MSGFLT_ALLOW, NULL);
  764. }
  765. }
  766. //---------------------------------------------------------------------------
  767. void __fastcall TTerminalManager::CreateTaskbarList()
  768. {
  769. ReleaseTaskbarList();
  770. if(SUCCEEDED(CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL,
  771. IID_ITaskbarList3, (void **) &FTaskbarList)))
  772. {
  773. if (ScpExplorer != NULL)
  774. {
  775. UpdateTaskbarList();
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TTerminalManager::ReleaseTaskbarList()
  781. {
  782. if (FTaskbarList != NULL)
  783. {
  784. FTaskbarList->Release();
  785. }
  786. }
  787. //---------------------------------------------------------------------------
  788. void __fastcall TTerminalManager::UpdateTaskbarList()
  789. {
  790. ScpExplorer->UpdateTaskbarList(FTaskbarList);
  791. }
  792. //---------------------------------------------------------------------------
  793. void __fastcall TTerminalManager::DeleteLocalFile(const UnicodeString FileName, bool Alternative)
  794. {
  795. if (!RecursiveDeleteFile(FileName, (WinConfiguration->DeleteToRecycleBin != Alternative)))
  796. {
  797. throw EOSExtException(FMTLOAD(DELETE_LOCAL_FILE_ERROR, (FileName)));
  798. }
  799. }
  800. //---------------------------------------------------------------------------
  801. void __fastcall TTerminalManager::TerminalQueryUser(TObject * Sender,
  802. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  803. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * /*Arg*/)
  804. {
  805. UnicodeString HelpKeyword;
  806. TMessageParams MessageParams(Params);
  807. UnicodeString AQuery = Query;
  808. if (Params != NULL)
  809. {
  810. HelpKeyword = Params->HelpKeyword;
  811. if (FLAGSET(Params->Params, qpFatalAbort))
  812. {
  813. AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
  814. if (!MessageParams.TimerMessage.IsEmpty())
  815. {
  816. MessageParams.TimerMessage = FMTLOAD(WARN_FATAL_ERROR, (MessageParams.TimerMessage));
  817. }
  818. }
  819. }
  820. if (ScpExplorer)
  821. {
  822. Answer = ScpExplorer->MoreMessageDialog(AQuery, MoreMessages, Type, Answers,
  823. HelpKeyword, &MessageParams, dynamic_cast<TTerminal *>(Sender));
  824. }
  825. else
  826. {
  827. Answer = MoreMessageDialog(AQuery, MoreMessages, Type, Answers, HelpKeyword,
  828. &MessageParams);
  829. }
  830. }
  831. //---------------------------------------------------------------------------
  832. void __fastcall TTerminalManager::AuthenticateFormCancel(TObject * Sender)
  833. {
  834. TAuthenticateForm * Form = dynamic_cast<TAuthenticateForm *>(Sender);
  835. assert(Form != NULL);
  836. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Form->Terminal);
  837. // will be null e.g. for background transfers
  838. if (ManagedTerminal != NULL)
  839. {
  840. TTerminalThread * TerminalThread = ManagedTerminal->TerminalThread;
  841. // can be NULL for reconnects from transfers
  842. if ((TerminalThread != NULL) && !TerminalThread->Cancelling)
  843. {
  844. Form->Log(LoadStr(AUTH_CANCELLING));
  845. TerminalThread->Cancel();
  846. }
  847. }
  848. }
  849. //---------------------------------------------------------------------------
  850. TAuthenticateForm * __fastcall TTerminalManager::MakeAuthenticateForm(
  851. TTerminal * Terminal)
  852. {
  853. TAuthenticateForm * Dialog = SafeFormCreate<TAuthenticateForm>();
  854. Dialog->Init(Terminal);
  855. assert(Dialog->OnCancel == NULL);
  856. Dialog->OnCancel = AuthenticateFormCancel;
  857. return Dialog;
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TTerminalManager::FileNameInputDialogInitializeRenameBaseName(
  861. TObject * /*Sender*/, TInputDialogData * Data)
  862. {
  863. EditSelectBaseName(Data->Edit->Handle);
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TTerminalManager::TerminalPromptUser(
  867. TTerminal * Terminal, TPromptKind Kind, UnicodeString Name, UnicodeString Instructions,
  868. TStrings * Prompts, TStrings * Results, bool & Result, void * /*Arg*/)
  869. {
  870. if (((Kind == pkPrompt) || (Kind == pkFileName)) && (FAuthenticateForm == NULL) &&
  871. (Terminal->Status != ssOpening))
  872. {
  873. assert(Instructions.IsEmpty());
  874. assert(Prompts->Count == 1);
  875. assert(FLAGSET(int(Prompts->Objects[0]), pupEcho));
  876. UnicodeString AResult = Results->Strings[0];
  877. TInputDialogInitialize InputDialogInitialize = NULL;
  878. if ((Kind == pkFileName) && !WinConfiguration->RenameWholeName)
  879. {
  880. InputDialogInitialize = FileNameInputDialogInitializeRenameBaseName;
  881. }
  882. Result = InputDialog(Name, Prompts->Strings[0], AResult, L"", NULL, false, InputDialogInitialize);
  883. if (Result)
  884. {
  885. Results->Strings[0] = AResult;
  886. }
  887. }
  888. else
  889. {
  890. TAuthenticateForm * AuthenticateForm = FAuthenticateForm;
  891. if (AuthenticateForm == NULL)
  892. {
  893. AuthenticateForm = MakeAuthenticateForm(Terminal);
  894. }
  895. try
  896. {
  897. Result = AuthenticateForm->PromptUser(Kind, Name, Instructions, Prompts, Results,
  898. (FAuthenticateForm != NULL), Terminal->StoredCredentialsTried);
  899. }
  900. __finally
  901. {
  902. if (FAuthenticateForm == NULL)
  903. {
  904. delete AuthenticateForm;
  905. }
  906. }
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TTerminalManager::TerminalDisplayBanner(
  911. TTerminal * Terminal, UnicodeString SessionName,
  912. const UnicodeString & Banner, bool & NeverShowAgain, int Options)
  913. {
  914. assert(FAuthenticateForm != NULL);
  915. TAuthenticateForm * AuthenticateForm = FAuthenticateForm;
  916. if (AuthenticateForm == NULL)
  917. {
  918. AuthenticateForm = MakeAuthenticateForm(Terminal);
  919. }
  920. try
  921. {
  922. AuthenticateForm->Banner(Banner, NeverShowAgain, Options);
  923. }
  924. __finally
  925. {
  926. if (FAuthenticateForm == NULL)
  927. {
  928. delete AuthenticateForm;
  929. }
  930. }
  931. }
  932. //---------------------------------------------------------------------------
  933. void __fastcall TTerminalManager::TerminalShowExtendedException(
  934. TTerminal * Terminal, Exception * E, void * /*Arg*/)
  935. {
  936. if (ScpExplorer)
  937. {
  938. ScpExplorer->ShowExtendedException(Terminal, E);
  939. }
  940. else
  941. {
  942. ShowExtendedExceptionEx(Terminal, E);
  943. }
  944. }
  945. //---------------------------------------------------------------------------
  946. static TDateTime DirectoryReadingProgressDelay(0, 0, 1, 500);
  947. //---------------------------------------------------------------------------
  948. void __fastcall TTerminalManager::TerminalReadDirectoryProgress(
  949. TObject * /*Sender*/, int Progress, bool & Cancel)
  950. {
  951. if (Progress == 0)
  952. {
  953. if (ScpExplorer != NULL)
  954. {
  955. ScpExplorer->LockWindow();
  956. }
  957. FDirectoryReadingStart = Now();
  958. if (!FProgressTitle.IsEmpty() || !FForegroundProgressTitle.IsEmpty())
  959. {
  960. FProgressTitle = L"";
  961. FForegroundProgressTitle = L"";
  962. UpdateAppTitle();
  963. }
  964. // Reset "was ESC ever pressed?" state
  965. GetAsyncKeyState(VK_ESCAPE);
  966. }
  967. else if (Progress < 0)
  968. {
  969. if (Progress == -2)
  970. {
  971. // cancelled
  972. if (ScpExplorer != NULL)
  973. {
  974. ScpExplorer->ReadDirectoryCancelled();
  975. }
  976. }
  977. else
  978. {
  979. if (ScpExplorer != NULL)
  980. {
  981. ScpExplorer->UnlockWindow();
  982. }
  983. FProgressTitle = L"";
  984. FForegroundProgressTitle = L"";
  985. UpdateAppTitle();
  986. }
  987. }
  988. else
  989. {
  990. // If the least significant bit is set,
  991. // the key was pressed after the previous call to GetAsyncKeyState.
  992. int KeyState = GetAsyncKeyState(VK_ESCAPE);
  993. if (FLAGSET(KeyState, 0x01))
  994. {
  995. Cancel = true;
  996. }
  997. if ((Now() - FDirectoryReadingStart) >= DirectoryReadingProgressDelay)
  998. {
  999. FForegroundProgressTitle = FMTLOAD(DIRECTORY_READING_PROGRESS, (Progress));
  1000. UpdateAppTitle();
  1001. }
  1002. }
  1003. }
  1004. //---------------------------------------------------------------------------
  1005. void __fastcall TTerminalManager::TerminalInformation(
  1006. TTerminal * Terminal, const UnicodeString & Str, bool /*Status*/, int Phase)
  1007. {
  1008. if (Phase == 1)
  1009. {
  1010. if (FAuthenticating == 0)
  1011. {
  1012. FBusyToken = BusyStart();
  1013. }
  1014. FAuthenticating++;
  1015. }
  1016. else if (Phase == 0)
  1017. {
  1018. assert(FAuthenticating > 0);
  1019. FAuthenticating--;
  1020. if (FAuthenticating == 0)
  1021. {
  1022. BusyEnd(FBusyToken);
  1023. FBusyToken = NULL;
  1024. }
  1025. SAFE_DESTROY(FAuthenticateForm);
  1026. }
  1027. else
  1028. {
  1029. if (FAuthenticating > 0)
  1030. {
  1031. bool ShowPending = false;
  1032. if (FAuthenticateForm == NULL)
  1033. {
  1034. FAuthenticateForm = MakeAuthenticateForm(Terminal);
  1035. ShowPending = true;
  1036. }
  1037. FAuthenticateForm->Log(Str);
  1038. if (ShowPending)
  1039. {
  1040. FAuthenticateForm->ShowAsModal();
  1041. }
  1042. }
  1043. }
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TTerminalManager::OperationFinished(::TFileOperation Operation,
  1047. TOperationSide Side, bool Temp, const UnicodeString & FileName, bool Success,
  1048. TOnceDoneOperation & OnceDoneOperation)
  1049. {
  1050. assert(ScpExplorer);
  1051. ScpExplorer->OperationFinished(Operation, Side, Temp, FileName, Success,
  1052. OnceDoneOperation);
  1053. }
  1054. //---------------------------------------------------------------------------
  1055. UnicodeString __fastcall TTerminalManager::ProgressTitle(TFileOperationProgressType * ProgressData)
  1056. {
  1057. return
  1058. FORMAT(L"%d%% %s",
  1059. (ProgressData->OverallProgress(),
  1060. TProgressForm::OperationName(ProgressData->Operation, ProgressData->Side)));
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. void __fastcall TTerminalManager::OperationProgress(
  1064. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  1065. {
  1066. if (ProgressData.InProgress)
  1067. {
  1068. FProgressTitle = ProgressTitle(&ProgressData);
  1069. }
  1070. else
  1071. {
  1072. FProgressTitle = L"";
  1073. }
  1074. UpdateAppTitle();
  1075. assert(ScpExplorer);
  1076. ScpExplorer->OperationProgress(ProgressData, Cancel);
  1077. }
  1078. //---------------------------------------------------------------------------
  1079. void __fastcall TTerminalManager::QueueEvent(TTerminalQueue * Queue, TQueueEvent Event)
  1080. {
  1081. TGuard Guard(FQueueSection);
  1082. FQueueWithEvent = Queue;
  1083. FQueueEvent = Event;
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TTerminalManager::ConfigurationChange(TObject * /*Sender*/)
  1087. {
  1088. assert(Configuration);
  1089. assert(Configuration == WinConfiguration);
  1090. if (!Application->Terminated && Configuration->Logging &&
  1091. (WinConfiguration->LogView == lvWindow))
  1092. {
  1093. if (ActiveTerminal)
  1094. {
  1095. RequireLogForm(LogMemo);
  1096. }
  1097. }
  1098. else
  1099. {
  1100. FreeLogForm();
  1101. }
  1102. TTerminalQueue * Queue;
  1103. for (int Index = 0; Index < Count; Index++)
  1104. {
  1105. assert(Terminals[Index]->Log);
  1106. Terminals[Index]->Log->ReflectSettings();
  1107. Terminals[Index]->ActionLog->ReflectSettings();
  1108. Queue = reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  1109. SetQueueConfiguration(Queue);
  1110. }
  1111. if (ScpExplorer)
  1112. {
  1113. ScpExplorer->ConfigurationChanged();
  1114. }
  1115. }
  1116. //---------------------------------------------------------------------------
  1117. void __fastcall TTerminalManager::TerminalReady()
  1118. {
  1119. ScpExplorer->Terminal = ActiveTerminal;
  1120. ScpExplorer->Queue = ActiveQueue;
  1121. ScpExplorer->TerminalReady();
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. TStrings * __fastcall TTerminalManager::GetTerminalList()
  1125. {
  1126. if (FTerminalList->Count != Count)
  1127. {
  1128. for (int i = 0; i < Count; i++)
  1129. {
  1130. UnicodeString NameN;
  1131. UnicodeString Name = Terminals[i]->SessionData->SessionName;
  1132. int Number = 1;
  1133. NameN = Name;
  1134. while (FTerminalList->IndexOf(NameN) >= 0)
  1135. {
  1136. Number++;
  1137. NameN = FORMAT(L"%s (%d)", (Name, Number));
  1138. }
  1139. if (Number > 1)
  1140. {
  1141. Name = FORMAT(L"%s (%d)", (Name, Number));
  1142. }
  1143. FTerminalList->AddObject(Name, Terminals[i]);
  1144. }
  1145. }
  1146. return FTerminalList;
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. int __fastcall TTerminalManager::GetActiveTerminalIndex()
  1150. {
  1151. return ActiveTerminal ? IndexOf(ActiveTerminal) : -1;
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. void __fastcall TTerminalManager::SetActiveTerminalIndex(int value)
  1155. {
  1156. ActiveTerminal = Terminals[value];
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. UnicodeString __fastcall TTerminalManager::TerminalTitle(TTerminal * Terminal)
  1160. {
  1161. int Index = IndexOf(Terminal);
  1162. UnicodeString Result;
  1163. if (Index >= 0)
  1164. {
  1165. Result = TerminalList->Strings[Index];
  1166. }
  1167. else
  1168. {
  1169. // this is the case of background transfer sessions
  1170. Result = Terminal->SessionData->SessionName;
  1171. }
  1172. return Result;
  1173. }
  1174. //---------------------------------------------------------------------------
  1175. UnicodeString __fastcall TTerminalManager::GetActiveTerminalTitle()
  1176. {
  1177. UnicodeString Result = ActiveTerminal ?
  1178. TerminalTitle(ActiveTerminal) : UnicodeString(L"");
  1179. return Result;
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. TTerminalQueue * __fastcall TTerminalManager::GetActiveQueue()
  1183. {
  1184. TTerminalQueue * Result = NULL;
  1185. if (ActiveTerminal != NULL)
  1186. {
  1187. Result = reinterpret_cast<TTerminalQueue *>(FQueues->Items[ActiveTerminalIndex]);
  1188. }
  1189. return Result;
  1190. }
  1191. //---------------------------------------------------------------------------
  1192. void __fastcall TTerminalManager::CycleTerminals(bool Forward)
  1193. {
  1194. int Index = ActiveTerminalIndex;
  1195. Index += Forward ? 1 : -1;
  1196. if (Index < 0)
  1197. {
  1198. Index = Count-1;
  1199. }
  1200. else if (Index >= Count)
  1201. {
  1202. Index = 0;
  1203. }
  1204. ActiveTerminalIndex = Index;
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. bool __fastcall TTerminalManager::CanOpenInPutty()
  1208. {
  1209. return (ActiveTerminal != NULL) && !GUIConfiguration->PuttyPath.Trim().IsEmpty();
  1210. }
  1211. //---------------------------------------------------------------------------
  1212. void __fastcall TTerminalManager::OpenInPutty()
  1213. {
  1214. Configuration->Usage->Inc(L"OpenInPutty");
  1215. TSessionData * Data = new TSessionData(L"");
  1216. try
  1217. {
  1218. assert(ActiveTerminal != NULL);
  1219. Data->Assign(ActiveTerminal->SessionData);
  1220. if (ScpExplorer != NULL)
  1221. {
  1222. ScpExplorer->UpdateTerminal(ActiveTerminal);
  1223. Data->RemoteDirectory =
  1224. NOT_NULL(dynamic_cast<TManagedTerminal *>(ActiveTerminal))->RemoteDirectory;
  1225. }
  1226. // putty does not support resolving environment variables in session settings
  1227. Data->ExpandEnvironmentVariables();
  1228. if (ActiveTerminal->TunnelLocalPortNumber != 0)
  1229. {
  1230. Data->ConfigureTunnel(ActiveTerminal->TunnelLocalPortNumber);
  1231. }
  1232. OpenSessionInPutty(GUIConfiguration->PuttyPath, Data,
  1233. ActiveTerminal->UserName,
  1234. GUIConfiguration->PuttyPassword ? ActiveTerminal->Password : UnicodeString());
  1235. }
  1236. __finally
  1237. {
  1238. delete Data;
  1239. }
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TTerminalManager::NewSession(bool /*FromSite*/)
  1243. {
  1244. TObjectList * DataList = new TObjectList();
  1245. try
  1246. {
  1247. TSessionData * Data = new TSessionData(L"");
  1248. Data->Assign(StoredSessions->DefaultSettings);
  1249. DataList->Add(Data);
  1250. int Options = loAddSession;
  1251. if (DoLoginDialog(StoredSessions, DataList, Options))
  1252. {
  1253. ActiveTerminal = NewTerminals(DataList);
  1254. }
  1255. }
  1256. __finally
  1257. {
  1258. delete DataList;
  1259. }
  1260. }
  1261. //---------------------------------------------------------------------------
  1262. void __fastcall TTerminalManager::Idle()
  1263. {
  1264. for (int Index = 0; Index < Count; Index++)
  1265. {
  1266. TTerminal * Terminal = Terminals[Index];
  1267. try
  1268. {
  1269. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  1270. assert(ManagedTerminal != NULL);
  1271. // make sure Idle is called on the thread that runs the terminal
  1272. if (ManagedTerminal->TerminalThread != NULL)
  1273. {
  1274. ManagedTerminal->TerminalThread->Idle();
  1275. }
  1276. else
  1277. {
  1278. if (Terminal->Active)
  1279. {
  1280. Terminal->Idle();
  1281. }
  1282. }
  1283. if (Terminal->Active)
  1284. {
  1285. assert(Index < FQueues->Count);
  1286. if (Index < FQueues->Count)
  1287. {
  1288. reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index])->Idle();
  1289. }
  1290. }
  1291. }
  1292. catch(Exception & E)
  1293. {
  1294. if (Terminal == ActiveTerminal)
  1295. {
  1296. // throw further, so that the exception is handled in proper place
  1297. // (particularly in broken-transfer reconnect handler, bug 72)
  1298. throw;
  1299. }
  1300. else
  1301. {
  1302. // we may not have inactive terminal, unless there is a explorer,
  1303. // also Idle is called from explorer anyway
  1304. assert(ScpExplorer != NULL);
  1305. if (ScpExplorer != NULL)
  1306. {
  1307. ScpExplorer->InactiveTerminalException(Terminal, &E);
  1308. }
  1309. if (!Terminal->Active)
  1310. {
  1311. // if session is lost, save the error message and rethrow it
  1312. // once the terminal gets activated
  1313. FTerminationMessages->Strings[Index] = E.Message;
  1314. }
  1315. }
  1316. }
  1317. }
  1318. TTerminalQueue * QueueWithEvent;
  1319. TQueueEvent QueueEvent;
  1320. {
  1321. TGuard Guard(FQueueSection);
  1322. QueueWithEvent = FQueueWithEvent;
  1323. FQueueWithEvent = NULL;
  1324. QueueEvent = FQueueEvent;
  1325. }
  1326. if (QueueWithEvent != NULL)
  1327. {
  1328. int Index = FQueues->IndexOf(QueueWithEvent);
  1329. // the session may not exist anymore
  1330. if (Index >= 0)
  1331. {
  1332. TTerminal * Terminal = Terminals[Index];
  1333. // we can hardly have a queue event without explorer
  1334. assert(ScpExplorer != NULL);
  1335. if (ScpExplorer != NULL)
  1336. {
  1337. ScpExplorer->QueueEvent(Terminal, QueueWithEvent, QueueEvent);
  1338. }
  1339. }
  1340. }
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TTerminalManager::MasterPasswordPrompt()
  1344. {
  1345. if (GetCurrentThreadId() == MainThreadID)
  1346. {
  1347. if (!DoMasterPasswordDialog())
  1348. {
  1349. Abort();
  1350. }
  1351. }
  1352. else
  1353. {
  1354. // this can happen only when we keep cancelling all master password prompts
  1355. // as long as the sessing finally connects (session password has to be
  1356. // explictly typed in), and background transfer is started
  1357. Abort();
  1358. }
  1359. }
  1360. //---------------------------------------------------------------------------
  1361. void __fastcall TTerminalManager::Move(TTerminal * Source, TTerminal * Target)
  1362. {
  1363. FTerminalList->Clear();
  1364. int SourceIndex = IndexOf(Source);
  1365. int TargetIndex = IndexOf(Target);
  1366. TTerminalList::Move(SourceIndex, TargetIndex);
  1367. FQueues->Move(SourceIndex, TargetIndex);
  1368. DoTerminalListChanged();
  1369. // when there are indexed sessions with the same name,
  1370. // the index may change when reordering the sessions
  1371. UpdateAppTitle();
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. void __fastcall TTerminalManager::DoTerminalListChanged()
  1375. {
  1376. if (OnTerminalListChanged)
  1377. {
  1378. OnTerminalListChanged(this);
  1379. }
  1380. }
  1381. //---------------------------------------------------------------------------
  1382. void __fastcall TTerminalManager::SaveWorkspace(TList * DataList)
  1383. {
  1384. for (int Index = 0; Index < Count; Index++)
  1385. {
  1386. TSessionData * Data = Terminals[Index]->SessionData;
  1387. TSessionData * Data2 = new TSessionData(L"");
  1388. DataList->Add(Data2);
  1389. if (StoredSessions->FindSame(Data))
  1390. {
  1391. Data2->Link = Data->Name;
  1392. }
  1393. else
  1394. {
  1395. Data2->Assign(Data);
  1396. }
  1397. Data2->Name = IntToHex(Index, 4);
  1398. Data2->IsWorkspace = true;
  1399. }
  1400. }
  1401. //---------------------------------------------------------------------------
  1402. TTerminal * __fastcall TTerminalManager::FindActiveTerminalForSite(TSessionData * Data)
  1403. {
  1404. TTerminal * Result = NULL;
  1405. for (int Index = 0; (Result == NULL) && (Index < Count); Index++)
  1406. {
  1407. TTerminal * Terminal = Terminals[Index];
  1408. if (Terminal->Active &&
  1409. Terminal->SessionData->IsSameSite(Data))
  1410. {
  1411. Result = Terminal;
  1412. }
  1413. }
  1414. return Result;
  1415. }
  1416. //---------------------------------------------------------------------------
  1417. TTerminalQueue * __fastcall TTerminalManager::FindQueueForTerminal(TTerminal * Terminal)
  1418. {
  1419. int Index = IndexOf(Terminal);
  1420. return reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  1421. }