TerminalManager.cpp 43 KB

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