TerminalManager.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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 "NonVisual.h"
  9. #include "WinConfiguration.h"
  10. #include "Tools.h"
  11. #include <Common.h>
  12. #include <CoreMain.h>
  13. #include <GUITools.h>
  14. #include <TextsWin.h>
  15. #include <TextsCore.h>
  16. #include <Progress.h>
  17. #include <Exceptions.h>
  18. #include <VCLCommon.h>
  19. #include <WinApi.h>
  20. #include <PuttyTools.h>
  21. #include <HelpWin.h>
  22. #include <System.IOUtils.hpp>
  23. #include <StrUtils.hpp>
  24. //---------------------------------------------------------------------------
  25. #pragma package(smart_init)
  26. //---------------------------------------------------------------------------
  27. TTerminalManager * TTerminalManager::FInstance = NULL;
  28. //---------------------------------------------------------------------------
  29. __fastcall TManagedTerminal::TManagedTerminal(TSessionData * SessionData,
  30. TConfiguration * Configuration) :
  31. TTerminal(SessionData, Configuration),
  32. LocalExplorerState(NULL), RemoteExplorerState(NULL),
  33. ReopenStart(0), DirectoryLoaded(Now()), TerminalThread(NULL)
  34. {
  35. StateData = new TSessionData(L"");
  36. StateData->Assign(SessionData);
  37. StateData->LocalDirectory = ::ExpandFileName(StateData->LocalDirectory);
  38. }
  39. //---------------------------------------------------------------------------
  40. __fastcall TManagedTerminal::~TManagedTerminal()
  41. {
  42. delete StateData;
  43. delete LocalExplorerState;
  44. delete RemoteExplorerState;
  45. }
  46. //---------------------------------------------------------------------------
  47. //---------------------------------------------------------------------------
  48. TTerminalManager * __fastcall TTerminalManager::Instance(bool ForceCreation)
  49. {
  50. if (!FInstance && ForceCreation)
  51. {
  52. FInstance = new TTerminalManager();
  53. }
  54. return FInstance;
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TTerminalManager::DestroyInstance()
  58. {
  59. DebugAssert(FInstance);
  60. SAFE_DESTROY(FInstance);
  61. }
  62. //---------------------------------------------------------------------------
  63. __fastcall TTerminalManager::TTerminalManager() :
  64. TTerminalList(Configuration)
  65. {
  66. FQueueSection = new TCriticalSection();
  67. FActiveTerminal = NULL;
  68. FScpExplorer = NULL;
  69. FDestroying = false;
  70. FTerminalPendingAction = tpNull;
  71. FDirectoryReadingStart = 0;
  72. FAuthenticateForm = NULL;
  73. FTaskbarList = NULL;
  74. FAuthenticating = 0;
  75. FMainThread = GetCurrentThreadId();
  76. FChangeSection.reset(new TCriticalSection());
  77. FPendingConfigurationChange = 0;
  78. FKeepAuthenticateForm = false;
  79. FApplicationsEvents.reset(new TApplicationEvents(Application));
  80. FApplicationsEvents->OnException = ApplicationException;
  81. FApplicationsEvents->OnShowHint = ApplicationShowHint;
  82. FApplicationsEvents->OnMessage = ApplicationMessage;
  83. FApplicationsEvents->OnModalBegin = ApplicationModalBegin;
  84. FApplicationsEvents->OnModalEnd = ApplicationModalEnd;
  85. DebugAssert(WinConfiguration->OnMasterPasswordPrompt == NULL);
  86. WinConfiguration->OnMasterPasswordPrompt = MasterPasswordPrompt;
  87. InitTaskbarButtonCreatedMessage();
  88. DebugAssert(Configuration && !Configuration->OnChange);
  89. Configuration->OnChange = ConfigurationChange;
  90. FOnLastTerminalClosed = NULL;
  91. FOnTerminalListChanged = NULL;
  92. FTerminalList = new TStringList();
  93. FQueues = new TList();
  94. FTerminationMessages = new TStringList();
  95. std::unique_ptr<TSessionData> DummyData(new TSessionData(L""));
  96. FLocalTerminal = CreateTerminal(DummyData.get());
  97. SetupTerminal(FLocalTerminal);
  98. }
  99. //---------------------------------------------------------------------------
  100. __fastcall TTerminalManager::~TTerminalManager()
  101. {
  102. FreeAll();
  103. DebugAssert(!ScpExplorer);
  104. DebugAssert(Configuration->OnChange == ConfigurationChange);
  105. Configuration->OnChange = NULL;
  106. FApplicationsEvents.reset(NULL);
  107. DebugAssert(WinConfiguration->OnMasterPasswordPrompt == MasterPasswordPrompt);
  108. WinConfiguration->OnMasterPasswordPrompt = NULL;
  109. delete FLocalTerminal;
  110. delete FQueues;
  111. delete FTerminationMessages;
  112. delete FTerminalList;
  113. CloseAutheticateForm();
  114. delete FQueueSection;
  115. ReleaseTaskbarList();
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall TTerminalManager::SetQueueConfiguration(TTerminalQueue * Queue)
  119. {
  120. Queue->TransfersLimit = GUIConfiguration->QueueTransfersLimit;
  121. Queue->KeepDoneItemsFor =
  122. (GUIConfiguration->QueueKeepDoneItems ? GUIConfiguration->QueueKeepDoneItemsFor : 0);
  123. }
  124. //---------------------------------------------------------------------------
  125. TTerminalQueue * __fastcall TTerminalManager::NewQueue(TTerminal * Terminal)
  126. {
  127. TTerminalQueue * Queue = new TTerminalQueue(Terminal, Configuration);
  128. SetQueueConfiguration(Queue);
  129. Queue->Enabled = WinConfiguration->EnableQueueByDefault;
  130. Queue->OnQueryUser = TerminalQueryUser;
  131. Queue->OnPromptUser = TerminalPromptUser;
  132. Queue->OnShowExtendedException = TerminalShowExtendedException;
  133. Queue->OnEvent = QueueEvent;
  134. return Queue;
  135. }
  136. //---------------------------------------------------------------------------
  137. TTerminal * __fastcall TTerminalManager::CreateTerminal(TSessionData * Data)
  138. {
  139. return new TManagedTerminal(Data, Configuration);
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TTerminalManager::SetupTerminal(TTerminal * Terminal)
  143. {
  144. Terminal->OnQueryUser = TerminalQueryUser;
  145. Terminal->OnPromptUser = TerminalPromptUser;
  146. Terminal->OnDisplayBanner = TerminalDisplayBanner;
  147. Terminal->OnShowExtendedException = TerminalShowExtendedException;
  148. Terminal->OnProgress = OperationProgress;
  149. Terminal->OnFinished = OperationFinished;
  150. Terminal->OnDeleteLocalFile = DeleteLocalFile;
  151. Terminal->OnReadDirectoryProgress = TerminalReadDirectoryProgress;
  152. Terminal->OnInformation = TerminalInformation;
  153. Terminal->OnCustomCommand = TerminalCustomCommand;
  154. }
  155. //---------------------------------------------------------------------------
  156. TTerminal * __fastcall TTerminalManager::DoNewTerminal(TSessionData * Data)
  157. {
  158. TTerminal * Terminal = TTerminalList::NewTerminal(Data);
  159. try
  160. {
  161. FQueues->Add(NewQueue(Terminal));
  162. FTerminationMessages->Add(L"");
  163. SetupTerminal(Terminal);
  164. }
  165. catch(...)
  166. {
  167. if (Terminal != NULL)
  168. {
  169. FreeTerminal(Terminal);
  170. }
  171. throw;
  172. }
  173. return Terminal;
  174. }
  175. //---------------------------------------------------------------------------
  176. TTerminal * __fastcall TTerminalManager::NewTerminal(TSessionData * Data)
  177. {
  178. TTerminal * Terminal = DoNewTerminal(Data);
  179. DoTerminalListChanged();
  180. return Terminal;
  181. }
  182. //---------------------------------------------------------------------------
  183. TTerminal * __fastcall TTerminalManager::NewTerminals(TList * DataList)
  184. {
  185. TTerminal * Result = NULL;
  186. for (int Index = 0; Index < DataList->Count; Index++)
  187. {
  188. TTerminal * Terminal =
  189. NewTerminal(reinterpret_cast<TSessionData *>(DataList->Items[Index]));
  190. if (Index == 0)
  191. {
  192. Result = Terminal;
  193. }
  194. }
  195. DoTerminalListChanged();
  196. return Result;
  197. }
  198. //---------------------------------------------------------------------------
  199. void __fastcall TTerminalManager::FreeActiveTerminal()
  200. {
  201. if (FTerminalPendingAction == tpNull)
  202. {
  203. DebugAssert(ActiveTerminal);
  204. FreeTerminal(ActiveTerminal);
  205. }
  206. else
  207. {
  208. DebugAssert(FTerminalPendingAction == ::tpNone);
  209. FTerminalPendingAction = tpFree;
  210. }
  211. }
  212. //---------------------------------------------------------------------------
  213. void __fastcall TTerminalManager::DoConnectTerminal(TTerminal * Terminal, bool Reopen, bool AdHoc)
  214. {
  215. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  216. // it must be managed terminal, unless it is secondary terminal (of managed terminal)
  217. DebugAssert((ManagedTerminal != NULL) || (dynamic_cast<TSecondaryTerminal *>(Terminal) != NULL));
  218. // particularly when we are reconnecting RemoteDirectory of managed terminal
  219. // hold the last used remote directory as opposite to session data, which holds
  220. // the default remote directory.
  221. // make sure the last used directory is used, but the default is preserved too
  222. UnicodeString OrigRemoteDirectory = Terminal->SessionData->RemoteDirectory;
  223. try
  224. {
  225. TTerminalThread * TerminalThread = new TTerminalThread(Terminal);
  226. TerminalThread->AllowAbandon = (Terminal == FActiveTerminal);
  227. try
  228. {
  229. if (ManagedTerminal != NULL)
  230. {
  231. Terminal->SessionData->RemoteDirectory = ManagedTerminal->StateData->RemoteDirectory;
  232. if ((double)ManagedTerminal->ReopenStart == 0)
  233. {
  234. ManagedTerminal->ReopenStart = Now();
  235. }
  236. DebugAssert(ManagedTerminal->TerminalThread == NULL);
  237. ManagedTerminal->TerminalThread = TerminalThread;
  238. }
  239. TNotifyEvent OnIdle;
  240. ((TMethod*)&OnIdle)->Code = TerminalThreadIdle;
  241. TerminalThread->OnIdle = OnIdle;
  242. if (Reopen)
  243. {
  244. TerminalThread->TerminalReopen();
  245. }
  246. else
  247. {
  248. TerminalThread->TerminalOpen();
  249. }
  250. }
  251. __finally
  252. {
  253. TerminalThread->OnIdle = NULL;
  254. if (!TerminalThread->Release())
  255. {
  256. if (!AdHoc && (DebugAlwaysTrue(Terminal == FActiveTerminal)))
  257. {
  258. // terminal was abandoned, must create a new one to replace it
  259. Terminal = CreateTerminal(new TSessionData(L""));
  260. SetupTerminal(Terminal);
  261. OwnsObjects = false;
  262. Items[ActiveTerminalIndex] = Terminal;
  263. OwnsObjects = true;
  264. FActiveTerminal = Terminal;
  265. }
  266. // when abandoning cancelled terminal, the form remains open
  267. CloseAutheticateForm();
  268. }
  269. else
  270. {
  271. if (ManagedTerminal != NULL)
  272. {
  273. ManagedTerminal->TerminalThread = NULL;
  274. }
  275. }
  276. }
  277. }
  278. __finally
  279. {
  280. Terminal->SessionData->RemoteDirectory = OrigRemoteDirectory;
  281. if (Terminal->Active && (ManagedTerminal != NULL))
  282. {
  283. ManagedTerminal->ReopenStart = 0;
  284. }
  285. }
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TTerminalManager::CloseAutheticateForm()
  289. {
  290. SAFE_DESTROY(FAuthenticateForm);
  291. }
  292. //---------------------------------------------------------------------------
  293. bool __fastcall TTerminalManager::ConnectTerminal(TTerminal * Terminal)
  294. {
  295. bool Result = true;
  296. // were it an active terminal, it would allow abandoning, what this API cannot deal with
  297. DebugAssert(Terminal != FActiveTerminal);
  298. try
  299. {
  300. DoConnectTerminal(Terminal, false, false);
  301. }
  302. catch (Exception & E)
  303. {
  304. ShowExtendedExceptionEx(Terminal, &E);
  305. Result = false;
  306. }
  307. return Result;
  308. }
  309. //---------------------------------------------------------------------------
  310. void __fastcall TTerminalManager::TerminalThreadIdle(void * /*Data*/, TObject * /*Sender*/)
  311. {
  312. Application->ProcessMessages();
  313. }
  314. //---------------------------------------------------------------------------
  315. bool __fastcall TTerminalManager::ConnectActiveTerminalImpl(bool Reopen)
  316. {
  317. TTerminalPendingAction Action;
  318. bool Result;
  319. do
  320. {
  321. Action = tpNull;
  322. Result = false;
  323. try
  324. {
  325. DebugAssert(ActiveTerminal);
  326. DoConnectTerminal(ActiveTerminal, Reopen, false);
  327. if (ScpExplorer)
  328. {
  329. DebugAssert(ActiveTerminal->Status == ssOpened);
  330. TerminalReady();
  331. }
  332. WinConfiguration->ClearTemporaryLoginData();
  333. Result = true;
  334. }
  335. catch (Exception & E)
  336. {
  337. DebugAssert(FTerminalPendingAction == tpNull);
  338. FTerminalPendingAction = ::tpNone;
  339. try
  340. {
  341. DebugAssert(ActiveTerminal != NULL);
  342. ActiveTerminal->ShowExtendedException(&E);
  343. Action = FTerminalPendingAction;
  344. }
  345. __finally
  346. {
  347. FTerminalPendingAction = tpNull;
  348. }
  349. }
  350. }
  351. while (Action == tpReconnect);
  352. if (Action == tpFree)
  353. {
  354. FreeActiveTerminal();
  355. }
  356. return Result;
  357. }
  358. //---------------------------------------------------------------------------
  359. bool __fastcall TTerminalManager::ConnectActiveTerminal()
  360. {
  361. ActiveTerminal->CollectUsage();
  362. // add only stored sessions to the jump list,
  363. // ad-hoc session cannot be reproduced from just a session name
  364. if (StoredSessions->FindSame(ActiveTerminal->SessionData) != NULL)
  365. {
  366. WinConfiguration->AddSessionToJumpList(ActiveTerminal->SessionData->SessionName);
  367. }
  368. FAuthenticationCancelled = false;
  369. bool Result = ConnectActiveTerminalImpl(false);
  370. UnicodeString DateStamp = StandardDatestamp();
  371. if (Result)
  372. {
  373. if (Configuration->Usage->Get(L"OpenedSessionsFailedLastDate") == DateStamp)
  374. {
  375. Configuration->Usage->Inc(L"OpenedSessionsFailedRecovered");
  376. }
  377. }
  378. else
  379. {
  380. Configuration->Usage->Inc(L"OpenedSessionsFailed");
  381. Configuration->Usage->Set(L"OpenedSessionsFailedLastDate", DateStamp);
  382. if (FAuthenticationCancelled)
  383. {
  384. Configuration->Usage->Inc(L"OpenedSessionsFailedAfterCancel");
  385. }
  386. }
  387. if (Result && WinConfiguration->AutoOpenInPutty && CanOpenInPutty())
  388. {
  389. try
  390. {
  391. OpenInPutty();
  392. }
  393. catch(Exception & E)
  394. {
  395. ShowExtendedExceptionEx(NULL, &E);
  396. }
  397. }
  398. return Result;
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TTerminalManager::DisconnectActiveTerminal()
  402. {
  403. DebugAssert(ActiveTerminal);
  404. int Index = IndexOf(ActiveTerminal);
  405. TTerminalQueue * OldQueue;
  406. TTerminalQueue * NewQueue;
  407. OldQueue = reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  408. NewQueue = this->NewQueue(ActiveTerminal);
  409. FQueues->Items[Index] = NewQueue;
  410. ScpExplorer->Queue = NewQueue;
  411. delete OldQueue;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TTerminalManager::ReconnectActiveTerminal()
  415. {
  416. DebugAssert(ActiveTerminal);
  417. if (ScpExplorer)
  418. {
  419. if (ScpExplorer->Terminal == ActiveTerminal)
  420. {
  421. ScpExplorer->UpdateTerminal(ActiveTerminal);
  422. }
  423. }
  424. try
  425. {
  426. if (FTerminalPendingAction == tpNull)
  427. {
  428. ConnectActiveTerminalImpl(true);
  429. }
  430. else
  431. {
  432. FTerminalPendingAction = tpReconnect;
  433. }
  434. }
  435. catch(...)
  436. {
  437. FreeActiveTerminal();
  438. throw;
  439. }
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TTerminalManager::FreeAll()
  443. {
  444. FDestroying = true;
  445. try
  446. {
  447. while (Count)
  448. {
  449. FreeTerminal(Terminals[0]);
  450. }
  451. }
  452. __finally
  453. {
  454. FDestroying = false;
  455. }
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TTerminalManager::FreeTerminal(TTerminal * Terminal)
  459. {
  460. try
  461. {
  462. // we want the Login dialog to open on auto-workspace name,
  463. // as set in TCustomScpExplorerForm::FormClose
  464. if (!FDestroying || !WinConfiguration->AutoSaveWorkspace)
  465. {
  466. if (StoredSessions->FindSame(Terminal->SessionData) != NULL)
  467. {
  468. WinConfiguration->LastStoredSession = Terminal->SessionData->Name;
  469. }
  470. }
  471. if (ScpExplorer != NULL)
  472. {
  473. ScpExplorer->TerminalRemoved(Terminal);
  474. }
  475. if (Terminal->Active)
  476. {
  477. Terminal->Close();
  478. }
  479. }
  480. __finally
  481. {
  482. int Index = IndexOf(Terminal);
  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. ActiveTerminal = Terminals[Index < Count ? Index : Index - 1];
  493. }
  494. else
  495. {
  496. ActiveTerminal = NULL;
  497. }
  498. }
  499. else
  500. {
  501. SaveTerminal(Terminal);
  502. }
  503. // only now all references to/from queue (particularly events to explorer)
  504. // are cleared
  505. delete Queue;
  506. delete Terminal;
  507. DoTerminalListChanged();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TTerminalManager::SetScpExplorer(TCustomScpExplorerForm * value)
  512. {
  513. if (ScpExplorer != value)
  514. {
  515. // changing explorer is not supported yet
  516. DebugAssert(!ScpExplorer || !value);
  517. FScpExplorer = value;
  518. if (FScpExplorer)
  519. {
  520. FScpExplorer->Terminal = ActiveTerminal;
  521. FScpExplorer->Queue = ActiveQueue;
  522. FOnLastTerminalClosed = FScpExplorer->LastTerminalClosed;
  523. FOnTerminalListChanged = FScpExplorer->TerminalListChanged;
  524. UpdateTaskbarList();
  525. }
  526. else
  527. {
  528. FOnLastTerminalClosed = NULL;
  529. FOnTerminalListChanged = NULL;
  530. }
  531. }
  532. }
  533. //---------------------------------------------------------------------------
  534. void __fastcall TTerminalManager::SetActiveTerminal(TTerminal * value)
  535. {
  536. DoSetActiveTerminal(value, false);
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TTerminalManager::SetActiveTerminalWithAutoReconnect(TTerminal * value)
  540. {
  541. DoSetActiveTerminal(value, true);
  542. }
  543. //---------------------------------------------------------------------------
  544. void __fastcall TTerminalManager::DoSetActiveTerminal(TTerminal * value, bool AutoReconnect)
  545. {
  546. if (ActiveTerminal != value)
  547. {
  548. // here used to be call to TCustomScpExporer::UpdateSessionData (now UpdateTerminal)
  549. // but it seems to be duplicate to call from TCustomScpExporer::TerminalChanging
  550. TTerminal * PActiveTerminal = ActiveTerminal;
  551. FActiveTerminal = value;
  552. // moved from else block of next if (ActiveTerminal) statement
  553. // so ScpExplorer can update its caption
  554. UpdateAppTitle();
  555. if (ScpExplorer)
  556. {
  557. if (ActiveTerminal && (ActiveTerminal->Status == ssOpened))
  558. {
  559. TerminalReady();
  560. }
  561. else
  562. {
  563. ScpExplorer->Terminal = NULL;
  564. ScpExplorer->Queue = NULL;
  565. }
  566. }
  567. if (PActiveTerminal && !PActiveTerminal->Active)
  568. {
  569. SaveTerminal(PActiveTerminal);
  570. }
  571. if (ActiveTerminal)
  572. {
  573. int Index = ActiveTerminalIndex;
  574. if (!ActiveTerminal->Active && !FTerminationMessages->Strings[Index].IsEmpty())
  575. {
  576. UnicodeString Message = FTerminationMessages->Strings[Index];
  577. FTerminationMessages->Strings[Index] = L"";
  578. if (AutoReconnect)
  579. {
  580. ReconnectActiveTerminal();
  581. }
  582. else
  583. {
  584. Exception * E = new ESshFatal(NULL, Message);
  585. try
  586. {
  587. // finally show pending terminal message,
  588. // this gives user also possibility to reconnect
  589. ActiveTerminal->ShowExtendedException(E);
  590. }
  591. __finally
  592. {
  593. delete E;
  594. }
  595. }
  596. }
  597. }
  598. else
  599. {
  600. if (OnLastTerminalClosed)
  601. {
  602. OnLastTerminalClosed(this);
  603. }
  604. }
  605. if ((ActiveTerminal != NULL) && !ActiveTerminal->Active)
  606. {
  607. ConnectActiveTerminal();
  608. }
  609. }
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TTerminalManager::QueueStatusUpdated()
  613. {
  614. UpdateAppTitle();
  615. }
  616. //---------------------------------------------------------------------------
  617. bool __fastcall TTerminalManager::ShouldDisplayQueueStatusOnAppTitle()
  618. {
  619. bool Result = IsApplicationMinimized();
  620. if (!Result && (ScpExplorer != NULL))
  621. {
  622. HWND Window = GetActiveWindow();
  623. Window = GetAncestor(Window, GA_ROOTOWNER);
  624. Result = (ScpExplorer->Handle != Window);
  625. }
  626. return Result;
  627. }
  628. //---------------------------------------------------------------------------
  629. void __fastcall TTerminalManager::UpdateAppTitle()
  630. {
  631. if (ScpExplorer)
  632. {
  633. UnicodeString NewTitle = FormatMainFormCaption(GetActiveTerminalTitle(false));
  634. UnicodeString QueueProgressTitle;
  635. if (!FForegroundProgressTitle.IsEmpty())
  636. {
  637. NewTitle = FForegroundProgressTitle + L" - " + NewTitle;
  638. }
  639. else if (!FProgressTitle.IsEmpty() && !ForegroundTask())
  640. {
  641. NewTitle = FProgressTitle + L" - " + NewTitle;
  642. }
  643. else if (ShouldDisplayQueueStatusOnAppTitle() &&
  644. !(QueueProgressTitle = ScpExplorer->GetQueueProgressTitle()).IsEmpty())
  645. {
  646. NewTitle = QueueProgressTitle + L" - " + NewTitle;
  647. }
  648. else if (ActiveTerminal && (ScpExplorer != NULL))
  649. {
  650. AddToList(NewTitle, ScpExplorer->PathForCaption(), L" - ");
  651. }
  652. ScpExplorer->Caption = NewTitle;
  653. ScpExplorer->ApplicationTitleChanged();
  654. }
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TTerminalManager::SaveTerminal(TTerminal * Terminal)
  658. {
  659. TSessionData * Data = StoredSessions->FindSame(Terminal->SessionData);
  660. if (Data != NULL)
  661. {
  662. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  663. DebugAssert(ManagedTerminal != NULL);
  664. bool Changed = false;
  665. if (Terminal->SessionData->UpdateDirectories)
  666. {
  667. Data->CopyDirectoriesStateData(ManagedTerminal->StateData);
  668. Changed = true;
  669. }
  670. if (Changed)
  671. {
  672. // modified only, implicit
  673. StoredSessions->Save(false, false);
  674. }
  675. }
  676. }
  677. //---------------------------------------------------------------------------
  678. void __fastcall TTerminalManager::HandleException(Exception * E)
  679. {
  680. // can be null for example when exception is thrown on login dialog
  681. if (ActiveTerminal != NULL)
  682. {
  683. ActiveTerminal->ShowExtendedException(E);
  684. }
  685. else
  686. {
  687. ShowExtendedException(E);
  688. }
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TTerminalManager::ApplicationException(TObject * /*Sender*/,
  692. Exception * E)
  693. {
  694. HandleException(E);
  695. }
  696. //---------------------------------------------------------------------------
  697. void __fastcall TTerminalManager::ApplicationShowHint(UnicodeString & HintStr,
  698. bool & /*CanShow*/, THintInfo & HintInfo)
  699. {
  700. HintInfo.HintData = HintInfo.HintControl;
  701. TLabel * HintLabel = dynamic_cast<TLabel *>(HintInfo.HintControl);
  702. if ((HintLabel != NULL) && HasLabelHintPopup(HintLabel, HintStr))
  703. {
  704. // Hack for transfer setting labels.
  705. // Should be converted to something like HintLabel()
  706. HintInfo.HideTimeout = 100000; // "almost" never
  707. }
  708. else if (dynamic_cast<TProgressBar *>(HintInfo.HintControl) != NULL)
  709. {
  710. // Hint is forcibly hidden in TProgressForm::FormHide
  711. HintInfo.HideTimeout = 100000; // "almost" never
  712. HintInfo.ReshowTimeout = 500; // updated each 0.5s
  713. }
  714. else
  715. {
  716. int HintMaxWidth = 300;
  717. TControl * ScaleControl = HintInfo.HintControl;
  718. if (DebugAlwaysFalse(HintInfo.HintControl == NULL) ||
  719. (GetParentForm(HintInfo.HintControl) == NULL))
  720. {
  721. ScaleControl = ScpExplorer;
  722. }
  723. HintMaxWidth = ScaleByTextHeight(ScaleControl, HintMaxWidth);
  724. HintInfo.HintMaxWidth = HintMaxWidth;
  725. }
  726. }
  727. //---------------------------------------------------------------------------
  728. bool __fastcall TTerminalManager::HandleMouseWheel(WPARAM WParam, LPARAM LParam)
  729. {
  730. // WORKAROUND This is no longer necessary on Windows 10
  731. bool Result = false;
  732. if (Application->Active)
  733. {
  734. TPoint Point(LOWORD(LParam), HIWORD(LParam));
  735. TWinControl * Control = FindVCLWindow(Point);
  736. if (Control != NULL)
  737. {
  738. TCustomForm * Form = GetParentForm(Control);
  739. // Only case we expect the parent form to be NULL is on the Find/Replace dialog,
  740. // which is owned by VCL's internal TRedirectorWindow.
  741. DebugAssert((Form != NULL) || (Control->ClassName() == L"TRedirectorWindow"));
  742. if ((Form != NULL) && Form->Active)
  743. {
  744. // Send it only to windows we tested it with.
  745. // Though we should sooner or later remove this test and pass it to all our windows.
  746. if (Form->Perform(WM_WANTS_MOUSEWHEEL, 0, 0) == 1)
  747. {
  748. SendMessage(Control->Handle, WM_MOUSEWHEEL, WParam, LParam);
  749. Result = true;
  750. }
  751. }
  752. }
  753. }
  754. return Result;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TTerminalManager::ApplicationMessage(TMsg & Msg, bool & Handled)
  758. {
  759. if (Msg.message == FTaskbarButtonCreatedMessage)
  760. {
  761. CreateTaskbarList();
  762. }
  763. if (Msg.message == WM_MOUSEWHEEL)
  764. {
  765. Handled = HandleMouseWheel(Msg.wParam, Msg.lParam);
  766. }
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TTerminalManager::ApplicationModalBegin(TObject * /*Sender*/)
  770. {
  771. NonVisualDataModule->StartBusy();
  772. if (ScpExplorer != NULL)
  773. {
  774. ScpExplorer->SuspendWindowLock();
  775. }
  776. }
  777. //---------------------------------------------------------------------------
  778. void __fastcall TTerminalManager::ApplicationModalEnd(TObject * /*Sender*/)
  779. {
  780. NonVisualDataModule->EndBusy();
  781. if (ScpExplorer != NULL)
  782. {
  783. ScpExplorer->ResumeWindowLock();
  784. }
  785. }
  786. //---------------------------------------------------------------------------
  787. void __fastcall TTerminalManager::InitTaskbarButtonCreatedMessage()
  788. {
  789. // XE6 VCL already handles TaskbarButtonCreated, but does not call ChangeWindowMessageFilterEx.
  790. // So we keep our implementation.
  791. // See also https://stackoverflow.com/q/14614823/850848#14618587
  792. FTaskbarButtonCreatedMessage = RegisterWindowMessage(L"TaskbarButtonCreated");
  793. HINSTANCE User32Library = LoadLibrary(L"user32.dll");
  794. ChangeWindowMessageFilterExProc ChangeWindowMessageFilterEx =
  795. (ChangeWindowMessageFilterExProc)GetProcAddress(User32Library, "ChangeWindowMessageFilterEx");
  796. if (ChangeWindowMessageFilterEx != NULL)
  797. {
  798. // without this we won't get TaskbarButtonCreated, when app is running elevated
  799. ChangeWindowMessageFilterEx(
  800. Application->Handle, FTaskbarButtonCreatedMessage, MSGFLT_ALLOW, NULL);
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TTerminalManager::CreateTaskbarList()
  805. {
  806. ReleaseTaskbarList();
  807. if(SUCCEEDED(CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL,
  808. IID_ITaskbarList3, (void **) &FTaskbarList)))
  809. {
  810. if (ScpExplorer != NULL)
  811. {
  812. UpdateTaskbarList();
  813. }
  814. }
  815. }
  816. //---------------------------------------------------------------------------
  817. void __fastcall TTerminalManager::ReleaseTaskbarList()
  818. {
  819. if (FTaskbarList != NULL)
  820. {
  821. FTaskbarList->Release();
  822. }
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TTerminalManager::UpdateTaskbarList()
  826. {
  827. ScpExplorer->UpdateTaskbarList(FTaskbarList);
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TTerminalManager::DeleteLocalFile(const UnicodeString FileName, bool Alternative)
  831. {
  832. RecursiveDeleteFileChecked(FileName, (WinConfiguration->DeleteToRecycleBin != Alternative));
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TTerminalManager::TerminalQueryUser(TObject * Sender,
  836. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  837. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * /*Arg*/)
  838. {
  839. UnicodeString HelpKeyword;
  840. TMessageParams MessageParams(Params);
  841. UnicodeString AQuery = Query;
  842. if (Params != NULL)
  843. {
  844. HelpKeyword = Params->HelpKeyword;
  845. if (FLAGSET(Params->Params, qpFatalAbort))
  846. {
  847. AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
  848. if (!MessageParams.TimerMessage.IsEmpty())
  849. {
  850. MessageParams.TimerMessage = FMTLOAD(WARN_FATAL_ERROR, (MessageParams.TimerMessage));
  851. }
  852. }
  853. }
  854. if (ScpExplorer)
  855. {
  856. Answer = ScpExplorer->MoreMessageDialog(AQuery, MoreMessages, Type, Answers,
  857. HelpKeyword, &MessageParams, dynamic_cast<TTerminal *>(Sender));
  858. }
  859. else
  860. {
  861. Answer = MoreMessageDialog(AQuery, MoreMessages, Type, Answers, HelpKeyword,
  862. &MessageParams);
  863. }
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TTerminalManager::AuthenticateFormCancel(TObject * Sender)
  867. {
  868. TAuthenticateForm * Form = dynamic_cast<TAuthenticateForm *>(Sender);
  869. DebugAssert(Form != NULL);
  870. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Form->Terminal);
  871. // will be null e.g. for background transfers
  872. if (ManagedTerminal != NULL)
  873. {
  874. TTerminalThread * TerminalThread = ManagedTerminal->TerminalThread;
  875. // can be NULL for reconnects from transfers
  876. if ((TerminalThread != NULL) && !TerminalThread->Cancelling)
  877. {
  878. Form->Log(LoadStr(AUTH_CANCELLING));
  879. TerminalThread->Cancel();
  880. }
  881. }
  882. FAuthenticationCancelled = true;
  883. }
  884. //---------------------------------------------------------------------------
  885. TAuthenticateForm * __fastcall TTerminalManager::MakeAuthenticateForm(
  886. TTerminal * Terminal)
  887. {
  888. TAuthenticateForm * Dialog = SafeFormCreate<TAuthenticateForm>();
  889. Dialog->Init(Terminal);
  890. DebugAssert(Dialog->OnCancel == NULL);
  891. Dialog->OnCancel = AuthenticateFormCancel;
  892. return Dialog;
  893. }
  894. //---------------------------------------------------------------------------
  895. void __fastcall TTerminalManager::FileNameInputDialogInitializeRenameBaseName(
  896. TObject * /*Sender*/, TInputDialogData * Data)
  897. {
  898. EditSelectBaseName(Data->Edit->Handle);
  899. }
  900. //---------------------------------------------------------------------------
  901. void __fastcall TTerminalManager::TerminalPromptUser(
  902. TTerminal * Terminal, TPromptKind Kind, UnicodeString Name, UnicodeString Instructions,
  903. TStrings * Prompts, TStrings * Results, bool & Result, void * /*Arg*/)
  904. {
  905. if (((Kind == pkPrompt) || (Kind == pkFileName)) && (FAuthenticateForm == NULL) &&
  906. (Terminal->Status != ssOpening))
  907. {
  908. DebugAssert(Instructions.IsEmpty());
  909. DebugAssert(Prompts->Count == 1);
  910. DebugAssert(FLAGSET(int(Prompts->Objects[0]), pupEcho));
  911. UnicodeString AResult = Results->Strings[0];
  912. TInputDialogInitialize InputDialogInitialize = NULL;
  913. if ((Kind == pkFileName) && !WinConfiguration->RenameWholeName)
  914. {
  915. InputDialogInitialize = FileNameInputDialogInitializeRenameBaseName;
  916. }
  917. Result = InputDialog(Name, Prompts->Strings[0], AResult, L"", NULL, false, InputDialogInitialize);
  918. if (Result)
  919. {
  920. Results->Strings[0] = AResult;
  921. }
  922. }
  923. else
  924. {
  925. TAuthenticateForm * AuthenticateForm = FAuthenticateForm;
  926. if (AuthenticateForm == NULL)
  927. {
  928. AuthenticateForm = MakeAuthenticateForm(Terminal);
  929. }
  930. try
  931. {
  932. Result = AuthenticateForm->PromptUser(Kind, Name, Instructions, Prompts, Results,
  933. (FAuthenticateForm != NULL), Terminal->StoredCredentialsTried);
  934. }
  935. __finally
  936. {
  937. if (FAuthenticateForm == NULL)
  938. {
  939. delete AuthenticateForm;
  940. }
  941. }
  942. }
  943. }
  944. //---------------------------------------------------------------------------
  945. void __fastcall TTerminalManager::TerminalDisplayBanner(
  946. TTerminal * Terminal, UnicodeString SessionName,
  947. const UnicodeString & Banner, bool & NeverShowAgain, int Options, unsigned int & Params)
  948. {
  949. DebugAssert(FAuthenticateForm != NULL);
  950. TAuthenticateForm * AuthenticateForm = FAuthenticateForm;
  951. if (AuthenticateForm == NULL)
  952. {
  953. AuthenticateForm = MakeAuthenticateForm(Terminal);
  954. }
  955. try
  956. {
  957. AuthenticateForm->Banner(Banner, NeverShowAgain, Options, Params);
  958. }
  959. __finally
  960. {
  961. if (FAuthenticateForm == NULL)
  962. {
  963. delete AuthenticateForm;
  964. }
  965. }
  966. }
  967. //---------------------------------------------------------------------------
  968. void __fastcall TTerminalManager::TerminalShowExtendedException(
  969. TTerminal * Terminal, Exception * E, void * /*Arg*/)
  970. {
  971. if (ScpExplorer)
  972. {
  973. ScpExplorer->ShowExtendedException(Terminal, E);
  974. }
  975. else
  976. {
  977. ShowExtendedExceptionEx(Terminal, E);
  978. }
  979. }
  980. //---------------------------------------------------------------------------
  981. static TDateTime DirectoryReadingProgressDelay(0, 0, 1, 500);
  982. //---------------------------------------------------------------------------
  983. void __fastcall TTerminalManager::TerminalReadDirectoryProgress(
  984. TObject * /*Sender*/, int Progress, int ResolvedLinks, bool & Cancel)
  985. {
  986. if (Progress == 0)
  987. {
  988. if (ScpExplorer != NULL)
  989. {
  990. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  991. ScpExplorer->LockWindow();
  992. }
  993. FDirectoryReadingStart = Now();
  994. if (!FProgressTitle.IsEmpty() || !FForegroundProgressTitle.IsEmpty())
  995. {
  996. FProgressTitle = L"";
  997. FForegroundProgressTitle = L"";
  998. UpdateAppTitle();
  999. }
  1000. // Reset "was ESC ever pressed?" state
  1001. GetAsyncKeyState(VK_ESCAPE);
  1002. }
  1003. else if (Progress < 0)
  1004. {
  1005. if (Progress == -2)
  1006. {
  1007. // cancelled
  1008. if (ScpExplorer != NULL)
  1009. {
  1010. ScpExplorer->ReadDirectoryCancelled();
  1011. }
  1012. }
  1013. else
  1014. {
  1015. if (ScpExplorer != NULL)
  1016. {
  1017. ScpExplorer->UnlockWindow();
  1018. }
  1019. FProgressTitle = L"";
  1020. FForegroundProgressTitle = L"";
  1021. UpdateAppTitle();
  1022. }
  1023. }
  1024. else
  1025. {
  1026. // If the least significant bit is set,
  1027. // the key was pressed after the previous call to GetAsyncKeyState.
  1028. int KeyState = GetAsyncKeyState(VK_ESCAPE);
  1029. if (FLAGSET(KeyState, 0x01))
  1030. {
  1031. Cancel = true;
  1032. }
  1033. if ((Now() - FDirectoryReadingStart) >= DirectoryReadingProgressDelay)
  1034. {
  1035. // 4 is arbitrary number
  1036. FForegroundProgressTitle =
  1037. FMTLOAD(ResolvedLinks >= 4 ? DIRECTORY_READING_AND_RESOLVING_PROGRESS : DIRECTORY_READING_PROGRESS,
  1038. (Progress));
  1039. UpdateAppTitle();
  1040. }
  1041. }
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. void __fastcall TTerminalManager::TerminalCustomCommand(
  1045. TTerminal * /*Terminal*/, const UnicodeString & Command, bool & Handled)
  1046. {
  1047. // Implementation has to be thread-safe
  1048. Handled = CopyCommandToClipboard(Command);
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TTerminalManager::TerminalInformation(
  1052. TTerminal * Terminal, const UnicodeString & Str, bool /*Status*/, int Phase)
  1053. {
  1054. if (Phase == 1)
  1055. {
  1056. if (FAuthenticating == 0)
  1057. {
  1058. FBusyToken = BusyStart();
  1059. }
  1060. FAuthenticating++;
  1061. }
  1062. else if (Phase == 0)
  1063. {
  1064. DebugAssert(FAuthenticating > 0);
  1065. FAuthenticating--;
  1066. if (FAuthenticating == 0)
  1067. {
  1068. BusyEnd(FBusyToken);
  1069. FBusyToken = NULL;
  1070. }
  1071. if (!FKeepAuthenticateForm)
  1072. {
  1073. CloseAutheticateForm();
  1074. }
  1075. }
  1076. else
  1077. {
  1078. if (FAuthenticating > 0)
  1079. {
  1080. bool ShowPending = false;
  1081. if (FAuthenticateForm == NULL)
  1082. {
  1083. FAuthenticateForm = MakeAuthenticateForm(Terminal);
  1084. ShowPending = true;
  1085. }
  1086. FAuthenticateForm->Log(Str);
  1087. if (ShowPending)
  1088. {
  1089. FAuthenticateForm->ShowAsModal();
  1090. }
  1091. }
  1092. }
  1093. }
  1094. //---------------------------------------------------------------------------
  1095. void __fastcall TTerminalManager::OperationFinished(::TFileOperation Operation,
  1096. TOperationSide Side, bool Temp, const UnicodeString & FileName, bool Success,
  1097. TOnceDoneOperation & OnceDoneOperation)
  1098. {
  1099. DebugAssert(ScpExplorer);
  1100. ScpExplorer->OperationFinished(Operation, Side, Temp, FileName, Success,
  1101. OnceDoneOperation);
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TTerminalManager::OperationProgress(
  1105. TFileOperationProgressType & ProgressData)
  1106. {
  1107. if (ProgressData.InProgress)
  1108. {
  1109. FProgressTitle = TProgressForm::ProgressStr(&ProgressData);
  1110. }
  1111. else
  1112. {
  1113. FProgressTitle = L"";
  1114. }
  1115. UpdateAppTitle();
  1116. DebugAssert(ScpExplorer);
  1117. ScpExplorer->OperationProgress(ProgressData);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TTerminalManager::QueueEvent(TTerminalQueue * Queue, TQueueEvent Event)
  1121. {
  1122. TGuard Guard(FQueueSection);
  1123. FQueueEvents.push_back(std::make_pair(Queue, Event));
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. void __fastcall TTerminalManager::DoConfigurationChange()
  1127. {
  1128. DebugAssert(Configuration);
  1129. DebugAssert(Configuration == WinConfiguration);
  1130. TTerminalQueue * Queue;
  1131. for (int Index = 0; Index < Count; Index++)
  1132. {
  1133. DebugAssert(Terminals[Index]->Log);
  1134. Terminals[Index]->Log->ReflectSettings();
  1135. Terminals[Index]->ActionLog->ReflectSettings();
  1136. Queue = reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  1137. SetQueueConfiguration(Queue);
  1138. }
  1139. if (ScpExplorer)
  1140. {
  1141. ScpExplorer->ConfigurationChanged();
  1142. }
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TTerminalManager::ConfigurationChange(TObject * /*Sender*/)
  1146. {
  1147. if (FMainThread == GetCurrentThreadId())
  1148. {
  1149. DoConfigurationChange();
  1150. }
  1151. else
  1152. {
  1153. TGuard Guard(FChangeSection.get());
  1154. FPendingConfigurationChange++;
  1155. }
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TTerminalManager::TerminalReady()
  1159. {
  1160. ScpExplorer->Terminal = ActiveTerminal;
  1161. ScpExplorer->Queue = ActiveQueue;
  1162. ScpExplorer->TerminalReady();
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. TStrings * __fastcall TTerminalManager::GetTerminalList()
  1166. {
  1167. FTerminalList->Clear();
  1168. for (int i = 0; i < Count; i++)
  1169. {
  1170. TTerminal * Terminal = Terminals[i];
  1171. UnicodeString Name = GetTerminalTitle(Terminal, true);
  1172. FTerminalList->AddObject(Name, Terminal);
  1173. }
  1174. return FTerminalList;
  1175. }
  1176. //---------------------------------------------------------------------------
  1177. int __fastcall TTerminalManager::GetActiveTerminalIndex()
  1178. {
  1179. return ActiveTerminal ? IndexOf(ActiveTerminal) : -1;
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. void __fastcall TTerminalManager::SetActiveTerminalIndex(int value)
  1183. {
  1184. ActiveTerminal = Terminals[value];
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. UnicodeString __fastcall TTerminalManager::GetTerminalShortPath(TTerminal * Terminal)
  1188. {
  1189. UnicodeString Result = UnixExtractFileName(Terminal->CurrentDirectory);
  1190. if (Result.IsEmpty())
  1191. {
  1192. Result = Terminal->CurrentDirectory;
  1193. }
  1194. return Result;
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. UnicodeString __fastcall TTerminalManager::GetTerminalTitle(TTerminal * Terminal, bool Unique)
  1198. {
  1199. UnicodeString Result = Terminal->SessionData->SessionName;
  1200. if (Unique)
  1201. {
  1202. int Index = IndexOf(Terminal);
  1203. // not for background transfer sessions
  1204. if (Index >= 0)
  1205. {
  1206. for (int Index2 = 0; Index2 < Count; Index2++)
  1207. {
  1208. UnicodeString Name = Terminals[Index2]->SessionData->SessionName;
  1209. if ((Terminals[Index2] != Terminal) &&
  1210. SameText(Name, Result))
  1211. {
  1212. UnicodeString Path = GetTerminalShortPath(Terminal);
  1213. if (!Path.IsEmpty())
  1214. {
  1215. Result = FORMAT(L"%s (%s)", (Result, Path));
  1216. }
  1217. break;
  1218. }
  1219. }
  1220. }
  1221. }
  1222. return Result;
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. UnicodeString __fastcall TTerminalManager::GetActiveTerminalTitle(bool Unique)
  1226. {
  1227. UnicodeString Result;
  1228. if (ActiveTerminal != NULL)
  1229. {
  1230. Result = GetTerminalTitle(ActiveTerminal, Unique);
  1231. }
  1232. return Result;
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. TTerminalQueue * __fastcall TTerminalManager::GetActiveQueue()
  1236. {
  1237. TTerminalQueue * Result = NULL;
  1238. if (ActiveTerminal != NULL)
  1239. {
  1240. Result = reinterpret_cast<TTerminalQueue *>(FQueues->Items[ActiveTerminalIndex]);
  1241. }
  1242. return Result;
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. void __fastcall TTerminalManager::CycleTerminals(bool Forward)
  1246. {
  1247. if (Count > 0)
  1248. {
  1249. int Index = ActiveTerminalIndex;
  1250. Index += Forward ? 1 : -1;
  1251. if (Index < 0)
  1252. {
  1253. Index = Count-1;
  1254. }
  1255. else if (Index >= Count)
  1256. {
  1257. Index = 0;
  1258. }
  1259. ActiveTerminalIndex = Index;
  1260. }
  1261. }
  1262. //---------------------------------------------------------------------------
  1263. bool __fastcall TTerminalManager::CanOpenInPutty()
  1264. {
  1265. return (ActiveTerminal != NULL) && !GUIConfiguration->PuttyPath.Trim().IsEmpty();
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. void __fastcall TTerminalManager::UpdateSessionCredentials(TSessionData * Data)
  1269. {
  1270. Data->UserName = ActiveTerminal->UserName;
  1271. Data->Password = ActiveTerminal->Password;
  1272. }
  1273. //---------------------------------------------------------------------------
  1274. void __fastcall TTerminalManager::OpenInPutty()
  1275. {
  1276. Configuration->Usage->Inc(L"OpenInPutty");
  1277. TSessionData * Data = NULL;
  1278. try
  1279. {
  1280. // Is NULL on the first session when called from ConnectActiveTerminal()
  1281. // due to WinConfiguration->AutoOpenInPutty
  1282. if (ScpExplorer != NULL)
  1283. {
  1284. Data = ScpExplorer->CloneCurrentSessionData();
  1285. }
  1286. else
  1287. {
  1288. Data = new TSessionData(L"");
  1289. DebugAssert(ActiveTerminal != NULL);
  1290. Data->Assign(ActiveTerminal->SessionData);
  1291. UpdateSessionCredentials(Data);
  1292. }
  1293. // putty does not support resolving environment variables in session settings
  1294. Data->ExpandEnvironmentVariables();
  1295. if (ActiveTerminal->TunnelLocalPortNumber != 0)
  1296. {
  1297. Data->ConfigureTunnel(ActiveTerminal->TunnelLocalPortNumber);
  1298. }
  1299. OpenSessionInPutty(GUIConfiguration->PuttyPath, Data);
  1300. }
  1301. __finally
  1302. {
  1303. delete Data;
  1304. }
  1305. }
  1306. //---------------------------------------------------------------------------
  1307. void __fastcall TTerminalManager::NewSession(bool /*FromSite*/, const UnicodeString & SessionUrl, bool ReloadSessions, TForm * LinkedForm)
  1308. {
  1309. if (ReloadSessions)
  1310. {
  1311. StoredSessions->Load();
  1312. }
  1313. UnicodeString DownloadFile; // unused
  1314. std::unique_ptr<TObjectList> DataList(new TObjectList());
  1315. GetLoginData(SessionUrl, NULL, DataList.get(), DownloadFile, true, LinkedForm);
  1316. if (DataList->Count > 0)
  1317. {
  1318. ActiveTerminal = NewTerminals(DataList.get());
  1319. }
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. void __fastcall TTerminalManager::Idle(bool SkipCurrentTerminal)
  1323. {
  1324. if (FPendingConfigurationChange > 0) // optimization
  1325. {
  1326. bool Changed = false;
  1327. {
  1328. TGuard Guard(FChangeSection.get());
  1329. if (DebugAlwaysTrue(FPendingConfigurationChange > 0))
  1330. {
  1331. FPendingConfigurationChange--;
  1332. Changed = true;
  1333. }
  1334. }
  1335. if (Changed)
  1336. {
  1337. DoConfigurationChange();
  1338. }
  1339. }
  1340. for (int Index = 0; Index < Count; Index++)
  1341. {
  1342. TTerminal * Terminal = Terminals[Index];
  1343. try
  1344. {
  1345. if (!SkipCurrentTerminal || (Terminal != ActiveTerminal))
  1346. {
  1347. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  1348. DebugAssert(ManagedTerminal != NULL);
  1349. // make sure Idle is called on the thread that runs the terminal
  1350. if (ManagedTerminal->TerminalThread != NULL)
  1351. {
  1352. ManagedTerminal->TerminalThread->Idle();
  1353. }
  1354. else
  1355. {
  1356. if (Terminal->Active)
  1357. {
  1358. Terminal->Idle();
  1359. }
  1360. }
  1361. if (Terminal->Active)
  1362. {
  1363. DebugAssert(Index < FQueues->Count);
  1364. if (Index < FQueues->Count)
  1365. {
  1366. reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index])->Idle();
  1367. }
  1368. }
  1369. }
  1370. }
  1371. catch(Exception & E)
  1372. {
  1373. if (Terminal == ActiveTerminal)
  1374. {
  1375. // throw further, so that the exception is handled in proper place
  1376. // (particularly in broken-transfer reconnect handler, bug 72)
  1377. throw;
  1378. }
  1379. else
  1380. {
  1381. // we may not have inactive terminal, unless there is a explorer,
  1382. // also Idle is called from explorer anyway
  1383. DebugAssert(ScpExplorer != NULL);
  1384. if (ScpExplorer != NULL)
  1385. {
  1386. ScpExplorer->InactiveTerminalException(Terminal, &E);
  1387. }
  1388. if (!Terminal->Active)
  1389. {
  1390. // if session is lost, save the error message and rethrow it
  1391. // once the terminal gets activated
  1392. FTerminationMessages->Strings[Index] = E.Message;
  1393. }
  1394. }
  1395. }
  1396. }
  1397. TTerminalQueue * QueueWithEvent;
  1398. TQueueEvent QueueEvent;
  1399. do
  1400. {
  1401. QueueWithEvent = NULL;
  1402. {
  1403. TGuard Guard(FQueueSection);
  1404. if (!FQueueEvents.empty())
  1405. {
  1406. QueueWithEvent = FQueueEvents[0].first;
  1407. QueueEvent = FQueueEvents[0].second;
  1408. FQueueEvents.erase(FQueueEvents.begin());
  1409. }
  1410. }
  1411. if (QueueWithEvent != NULL)
  1412. {
  1413. int Index = FQueues->IndexOf(QueueWithEvent);
  1414. // the session may not exist anymore
  1415. if (Index >= 0)
  1416. {
  1417. TTerminal * Terminal = Terminals[Index];
  1418. // we can hardly have a queue event without explorer
  1419. DebugAssert(ScpExplorer != NULL);
  1420. if (ScpExplorer != NULL)
  1421. {
  1422. ScpExplorer->QueueEvent(Terminal, QueueWithEvent, QueueEvent);
  1423. }
  1424. }
  1425. }
  1426. }
  1427. while (QueueWithEvent != NULL);
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. void __fastcall TTerminalManager::MasterPasswordPrompt()
  1431. {
  1432. if (GetCurrentThreadId() == MainThreadID)
  1433. {
  1434. if (!DoMasterPasswordDialog())
  1435. {
  1436. Abort();
  1437. }
  1438. }
  1439. else
  1440. {
  1441. // this can happen only when we keep cancelling all master password prompts
  1442. // as long as the sessing finally connects (session password has to be
  1443. // explictly typed in), and background transfer is started
  1444. Abort();
  1445. }
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. void __fastcall TTerminalManager::Move(TTerminal * Source, TTerminal * Target)
  1449. {
  1450. int SourceIndex = IndexOf(Source);
  1451. int TargetIndex = IndexOf(Target);
  1452. TTerminalList::Move(SourceIndex, TargetIndex);
  1453. FQueues->Move(SourceIndex, TargetIndex);
  1454. DoTerminalListChanged();
  1455. // when there are indexed sessions with the same name,
  1456. // the index may change when reordering the sessions
  1457. UpdateAppTitle();
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. void __fastcall TTerminalManager::DoTerminalListChanged()
  1461. {
  1462. if (OnTerminalListChanged)
  1463. {
  1464. OnTerminalListChanged(this);
  1465. }
  1466. }
  1467. //---------------------------------------------------------------------------
  1468. void __fastcall TTerminalManager::SaveWorkspace(TList * DataList)
  1469. {
  1470. for (int Index = 0; Index < Count; Index++)
  1471. {
  1472. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminals[Index]);
  1473. TSessionData * Data = StoredSessions->SaveWorkspaceData(ManagedTerminal->StateData);
  1474. DataList->Add(Data);
  1475. Data->Name = IntToHex(Index, 4);
  1476. }
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. TTerminal * __fastcall TTerminalManager::FindActiveTerminalForSite(TSessionData * Data)
  1480. {
  1481. TTerminal * Result = NULL;
  1482. for (int Index = 0; (Result == NULL) && (Index < Count); Index++)
  1483. {
  1484. TTerminal * Terminal = Terminals[Index];
  1485. if (Terminal->Active &&
  1486. Terminal->SessionData->IsSameSite(Data))
  1487. {
  1488. Result = Terminal;
  1489. }
  1490. }
  1491. return Result;
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. TTerminalQueue * __fastcall TTerminalManager::FindQueueForTerminal(TTerminal * Terminal)
  1495. {
  1496. int Index = IndexOf(Terminal);
  1497. return reinterpret_cast<TTerminalQueue *>(FQueues->Items[Index]);
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. TRemoteFile * __fastcall TTerminalManager::CheckRights(
  1501. TTerminal * Terminal, const UnicodeString & EntryType, const UnicodeString & FileName, bool & WrongRights)
  1502. {
  1503. std::unique_ptr<TRemoteFile> FileOwner;
  1504. TRemoteFile * File;
  1505. try
  1506. {
  1507. Terminal->LogEvent(FORMAT(L"Checking %s \"%s\"...", (LowerCase(EntryType), FileName)));
  1508. Terminal->ReadFile(FileName, File);
  1509. FileOwner.reset(File);
  1510. int ForbiddenRights = TRights::rfGroupWrite | TRights::rfOtherWrite;
  1511. if ((File->Rights->Number & ForbiddenRights) != 0)
  1512. {
  1513. Terminal->LogEvent(FORMAT(L"%s \"%s\" exists, but has incorrect permissions %s.", (EntryType, FileName, File->Rights->Octal)));
  1514. WrongRights = true;
  1515. }
  1516. else
  1517. {
  1518. Terminal->LogEvent(FORMAT(L"%s \"%s\" exists and has correct permissions %s.", (EntryType, FileName, File->Rights->Octal)));
  1519. }
  1520. }
  1521. catch (Exception & E)
  1522. {
  1523. }
  1524. return FileOwner.release();
  1525. }
  1526. //---------------------------------------------------------------------------
  1527. bool __fastcall TTerminalManager::UploadPublicKey(
  1528. TTerminal * Terminal, TSessionData * Data, UnicodeString & FileName)
  1529. {
  1530. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  1531. OpenDialog->Title = LoadStr(LOGIN_PUBLIC_KEY_TITLE);
  1532. OpenDialog->Filter = LoadStr(LOGIN_PUBLIC_KEY_FILTER);
  1533. OpenDialog->DefaultExt = PuttyKeyExt;
  1534. OpenDialog->FileName = FileName;
  1535. bool Result = OpenDialog->Execute();
  1536. if (Result)
  1537. {
  1538. Configuration->Usage->Inc(L"PublicKeyInstallation");
  1539. FileName = OpenDialog->FileName;
  1540. bool AutoReadDirectory;
  1541. bool ExceptionOnFail;
  1542. UnicodeString TemporaryDir;
  1543. bool WrongRights = false;
  1544. const UnicodeString SshFolder = L".ssh";
  1545. const UnicodeString AuthorizedKeysFile = L"authorized_keys";
  1546. UnicodeString AuthorizedKeysFilePath = FORMAT(L"%s/%s", (SshFolder, AuthorizedKeysFile));
  1547. VerifyAndConvertKey(FileName, ssh2only, false);
  1548. UnicodeString Comment;
  1549. UnicodeString Line = GetPublicKeyLine(FileName, Comment);
  1550. bool AdHocTerminal = (Terminal == NULL);
  1551. std::unique_ptr<TTerminal> TerminalOwner;
  1552. if (AdHocTerminal)
  1553. {
  1554. DebugAssert(Data != NULL);
  1555. TAutoFlag KeepAuthenticateFormFlag(FKeepAuthenticateForm);
  1556. try
  1557. {
  1558. TerminalOwner.reset(CreateTerminal(Data));
  1559. Terminal = TerminalOwner.get();
  1560. SetupTerminal(Terminal);
  1561. Terminal->OnProgress = NULL;
  1562. Terminal->OnFinished = NULL;
  1563. DoConnectTerminal(Terminal, false, true);
  1564. }
  1565. catch (Exception & E)
  1566. {
  1567. CloseAutheticateForm();
  1568. throw;
  1569. }
  1570. }
  1571. AutoReadDirectory = Terminal->AutoReadDirectory;
  1572. ExceptionOnFail = Terminal->ExceptionOnFail;
  1573. try
  1574. {
  1575. Terminal->AutoReadDirectory = false;
  1576. Terminal->ExceptionOnFail = true;
  1577. UnicodeString SshImplementation = Terminal->GetSessionInfo().SshImplementation;
  1578. UnicodeString NotOpenSSHMessage = FMTLOAD(LOGIN_NOT_OPENSSH, (SshImplementation));
  1579. if (IsOpenSSH(SshImplementation) ||
  1580. (MessageDialog(NotOpenSSHMessage, qtConfirmation, qaOK | qaCancel, HELP_LOGIN_AUTHORIZED_KEYS) == qaOK))
  1581. {
  1582. Terminal->Log->AddSeparator();
  1583. Terminal->LogEvent(FORMAT(L"Adding public key line to \"%s\" file:\n%s", (AuthorizedKeysFilePath, Line)));
  1584. // Ad-hoc terminal
  1585. if (FAuthenticateForm != NULL)
  1586. {
  1587. FAuthenticateForm->Log(FMTLOAD(LOGIN_PUBLIC_KEY_UPLOAD, (Comment)));
  1588. }
  1589. UnicodeString SshFolderAbsolutePath = UnixIncludeTrailingBackslash(Terminal->GetHomeDirectory()) + SshFolder;
  1590. std::unique_ptr<TRemoteFile> SshFolderFile(CheckRights(Terminal, L"Folder", SshFolderAbsolutePath, WrongRights));
  1591. if (SshFolderFile.get() == NULL)
  1592. {
  1593. TRights SshFolderRights;
  1594. SshFolderRights.Number = TRights::rfUserRead | TRights::rfUserWrite | TRights::rfUserExec;
  1595. TRemoteProperties SshFolderProperties;
  1596. SshFolderProperties.Rights = SshFolderRights;
  1597. SshFolderProperties.Valid = TValidProperties() << vpRights;
  1598. Terminal->LogEvent(FORMAT(L"Trying to create \"%s\" folder with permissions %s...", (SshFolder, SshFolderRights.Octal)));
  1599. Terminal->CreateDirectory(SshFolderAbsolutePath, &SshFolderProperties);
  1600. }
  1601. TemporaryDir = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  1602. if (!ForceDirectories(ApiPath(TemporaryDir)))
  1603. {
  1604. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TemporaryDir)));
  1605. }
  1606. UnicodeString TemporaryAuthorizedKeysFile = IncludeTrailingBackslash(TemporaryDir) + AuthorizedKeysFile;
  1607. UnicodeString AuthorizedKeysFileAbsolutePath = UnixIncludeTrailingBackslash(SshFolderAbsolutePath) + AuthorizedKeysFile;
  1608. bool Updated = true;
  1609. TCopyParamType CopyParam; // Use factory defaults
  1610. CopyParam.ResumeSupport = rsOff; // not to break the permissions
  1611. CopyParam.PreserveTime = false; // not needed
  1612. UnicodeString AuthorizedKeys;
  1613. std::unique_ptr<TRemoteFile> AuthorizedKeysFileFile(CheckRights(Terminal, L"File", AuthorizedKeysFileAbsolutePath, WrongRights));
  1614. if (AuthorizedKeysFileFile.get() != NULL)
  1615. {
  1616. AuthorizedKeysFileFile->FullFileName = AuthorizedKeysFileAbsolutePath;
  1617. std::unique_ptr<TStrings> Files(new TStringList());
  1618. Files->AddObject(AuthorizedKeysFileAbsolutePath, AuthorizedKeysFileFile.get());
  1619. Terminal->LogEvent(FORMAT(L"Downloading current \"%s\" file...", (AuthorizedKeysFile)));
  1620. Terminal->CopyToLocal(Files.get(), TemporaryDir, &CopyParam, cpNoConfirmation, NULL);
  1621. // Overload with Encoding parameter work incorrectly, when used on a file without BOM
  1622. AuthorizedKeys = TFile::ReadAllText(TemporaryAuthorizedKeysFile);
  1623. std::unique_ptr<TStrings> AuthorizedKeysLines(TextToStringList(AuthorizedKeys));
  1624. int P = Line.Pos(L" ");
  1625. if (DebugAlwaysTrue(P > 0))
  1626. {
  1627. P = PosEx(L" ", Line, P + 1);
  1628. }
  1629. UnicodeString Prefix = Line.SubString(1, P); // including the space
  1630. for (int Index = 0; Index < AuthorizedKeysLines->Count; Index++)
  1631. {
  1632. if (StartsStr(Prefix, AuthorizedKeysLines->Strings[Index]))
  1633. {
  1634. Terminal->LogEvent(FORMAT(L"\"%s\" file already contains public key line:\n%s", (AuthorizedKeysFile, AuthorizedKeysLines->Strings[Index])));
  1635. Updated = false;
  1636. }
  1637. }
  1638. if (Updated)
  1639. {
  1640. Terminal->LogEvent(FORMAT(L"\"%s\" file does not contain the public key line yet.", (AuthorizedKeysFile)));
  1641. if (!EndsStr(L"\n", AuthorizedKeys))
  1642. {
  1643. Terminal->LogEvent(FORMAT(L"Adding missing trailing new line to \"%s\" file...", (AuthorizedKeysFile)));
  1644. AuthorizedKeys += L"\n";
  1645. }
  1646. }
  1647. }
  1648. else
  1649. {
  1650. Terminal->LogEvent(FORMAT(L"Creating new \"%s\" file...", (AuthorizedKeysFile)));
  1651. CopyParam.PreserveRights = true;
  1652. CopyParam.Rights.Number = TRights::rfUserRead | TRights::rfUserWrite;
  1653. }
  1654. if (Updated)
  1655. {
  1656. AuthorizedKeys += Line + L"\n";
  1657. // Overload without Encoding parameter uses TEncoding::UTF8, but does not write BOM, what we want
  1658. TFile::WriteAllText(TemporaryAuthorizedKeysFile, AuthorizedKeys);
  1659. std::unique_ptr<TStrings> Files(new TStringList());
  1660. Files->Add(TemporaryAuthorizedKeysFile);
  1661. Terminal->LogEvent(FORMAT(L"Uploading updated \"%s\" file...", (AuthorizedKeysFile)));
  1662. Terminal->CopyToRemote(Files.get(), SshFolderAbsolutePath, &CopyParam, cpNoConfirmation, NULL);
  1663. }
  1664. }
  1665. }
  1666. __finally
  1667. {
  1668. Terminal->AutoReadDirectory = AutoReadDirectory;
  1669. Terminal->ExceptionOnFail = ExceptionOnFail;
  1670. if (!TemporaryDir.IsEmpty())
  1671. {
  1672. RecursiveDeleteFile(ExcludeTrailingBackslash(TemporaryDir), false);
  1673. }
  1674. CloseAutheticateForm(); // When uploading from Login dialog
  1675. }
  1676. Terminal->LogEvent(L"Public key installation done.");
  1677. if (AdHocTerminal)
  1678. {
  1679. TerminalOwner.reset(NULL);
  1680. }
  1681. else
  1682. {
  1683. Terminal->Log->AddSeparator();
  1684. }
  1685. UnicodeString Message = FMTLOAD(LOGIN_PUBLIC_KEY_UPLOADED, (Comment));
  1686. if (WrongRights)
  1687. {
  1688. Message += L"\n\n" + FMTLOAD(LOGIN_PUBLIC_KEY_PERMISSIONS, (AuthorizedKeysFilePath));
  1689. }
  1690. MessageDialog(Message, qtInformation, qaOK, HELP_LOGIN_AUTHORIZED_KEYS);
  1691. }
  1692. return Result;
  1693. }