Login.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <StrUtils.hpp>
  5. #include <CoreMain.h>
  6. #include <Common.h>
  7. #include <PuttyTools.h>
  8. #include <TextsWin.h>
  9. #include <TextsCore.h>
  10. #include <HelpWin.h>
  11. #include <VCLCommon.h>
  12. #include "WinInterface.h"
  13. #include "Login.h"
  14. #include "GUITools.h"
  15. #include "Tools.h"
  16. #include "Setup.h"
  17. #include "CustomWinConfiguration.h"
  18. //---------------------------------------------------------------------
  19. #pragma link "ComboEdit"
  20. #pragma link "LogSettings"
  21. #pragma link "GeneralSettings"
  22. #pragma link "UpDownEdit"
  23. #pragma link "PasswordEdit"
  24. #ifndef NO_RESOURCES
  25. #pragma resource "*.dfm"
  26. #endif
  27. //---------------------------------------------------------------------------
  28. // Sheet tag:
  29. // 01 top, 02 indented
  30. //---------------------------------------------------------------------------
  31. bool __fastcall DoLoginDialog(TStoredSessionList *SessionList,
  32. TSessionData * Data, int Options)
  33. {
  34. assert(Data);
  35. TLoginDialog * LoginDialog = SafeFormCreate<TLoginDialog>();
  36. bool Result;
  37. try
  38. {
  39. LoginDialog->StoredSessions = SessionList;
  40. LoginDialog->SessionData = Data;
  41. LoginDialog->Options = Options;
  42. Result = LoginDialog->Execute();
  43. if (Result)
  44. {
  45. Data->Assign(LoginDialog->SessionData);
  46. };
  47. }
  48. __finally
  49. {
  50. delete LoginDialog;
  51. }
  52. return Result;
  53. }
  54. //---------------------------------------------------------------------
  55. static const TFSProtocol FSOrder[] = { fsSFTPonly, fsSCPonly, fsFTP };
  56. //---------------------------------------------------------------------
  57. __fastcall TLoginDialog::TLoginDialog(TComponent* AOwner)
  58. : TForm(AOwner)
  59. {
  60. FSessionData = new TSessionData("");
  61. NoUpdate = 0;
  62. FLanguagesPopupMenu = NULL;
  63. FInitialized = false;
  64. FSavedTab = NULL;
  65. FSavedSession = -1;
  66. FOptions = loStartup;
  67. FLocaleChanging = false;
  68. FColor = (TColor)0;
  69. FEditingSessionData = NULL;
  70. FTreeLabels = new TStringList();
  71. FRecycleBinSheetVisible = false;
  72. FHintNode = NULL;
  73. FScrollOnDragOver = new TTreeViewScrollOnDragOver(SessionTree, true);
  74. // we need to make sure that window procedure is set asap
  75. // (so that CM_SHOWINGCHANGED handling is applied)
  76. UseSystemSettingsPre(this, &FSystemSettings);
  77. InitControls();
  78. }
  79. //---------------------------------------------------------------------
  80. __fastcall TLoginDialog::~TLoginDialog()
  81. {
  82. delete FScrollOnDragOver;
  83. assert(FSystemSettings);
  84. DeleteSystemSettings(this, FSystemSettings);
  85. FSystemSettings = NULL;
  86. delete FTreeLabels;
  87. delete FSessionData;
  88. delete FLanguagesPopupMenu;
  89. }
  90. //---------------------------------------------------------------------
  91. void __fastcall TLoginDialog::InitControls()
  92. {
  93. LoggingFrame->Init();
  94. ComboAutoSwitchInitialize(UtfCombo);
  95. ComboAutoSwitchInitialize(BugIgnore1Combo);
  96. ComboAutoSwitchInitialize(BugPlainPW1Combo);
  97. ComboAutoSwitchInitialize(BugRSA1Combo);
  98. ComboAutoSwitchInitialize(BugHMAC2Combo);
  99. ComboAutoSwitchInitialize(BugDeriveKey2Combo);
  100. ComboAutoSwitchInitialize(BugRSAPad2Combo);
  101. ComboAutoSwitchInitialize(BugRekey2Combo);
  102. ComboAutoSwitchInitialize(BugPKSessID2Combo);
  103. ComboAutoSwitchInitialize(BugMaxPkt2Combo);
  104. ComboAutoSwitchInitialize(SFTPBugSymlinkCombo);
  105. ComboAutoSwitchInitialize(SFTPBugSignedTSCombo);
  106. ComboAutoSwitchInitialize(FtpListAllCombo);
  107. InstallPathWordBreakProc(RemoteDirectoryEdit);
  108. InstallPathWordBreakProc(LocalDirectoryEdit);
  109. InstallPathWordBreakProc(PrivateKeyEdit);
  110. InstallPathWordBreakProc(RecycleBinPathEdit);
  111. TunnelLocalPortNumberEdit->Items->BeginUpdate();
  112. try
  113. {
  114. AnsiString TunnelLocalPortNumberAutoassign = TunnelLocalPortNumberEdit->Items->Strings[0];
  115. TunnelLocalPortNumberEdit->Items->Clear();
  116. TunnelLocalPortNumberEdit->Items->Add(TunnelLocalPortNumberAutoassign);
  117. for (int Index = Configuration->TunnelLocalPortNumberLow;
  118. Index <= Configuration->TunnelLocalPortNumberHigh; Index++)
  119. {
  120. TunnelLocalPortNumberEdit->Items->Add(IntToStr(Index));
  121. }
  122. }
  123. __finally
  124. {
  125. TunnelLocalPortNumberEdit->Items->EndUpdate();
  126. }
  127. HintLabel(ProxyTelnetCommandHintText, LoadStr(LOGIN_PROXY_COMMAND_PATTERNS_HINT));
  128. HintLabel(ProxyLocalCommandHintText, LoadStr(LOGIN_PROXY_COMMAND_PATTERNS_HINT));
  129. if (SessionTree->WindowProc != SessionTreeProc)
  130. {
  131. FOldSessionTreeProc = SessionTree->WindowProc;
  132. SessionTree->WindowProc = SessionTreeProc;
  133. }
  134. FtpsCombo->Items->Strings[1] = LoadStr(FTPS_IMPLICIT);
  135. FtpsCombo->Items->Strings[2] = LoadStr(FTPS_EXPLICIT_SSL);
  136. FtpsCombo->Items->Strings[3] = LoadStr(FTPS_EXPLICIT_TLS);
  137. }
  138. //---------------------------------------------------------------------
  139. void __fastcall TLoginDialog::Init()
  140. {
  141. if (!FInitialized)
  142. {
  143. UseSystemSettingsPost(this, FSystemSettings);
  144. }
  145. else
  146. {
  147. UseSystemSettings(this, &FSystemSettings);
  148. }
  149. FInitialized = true;
  150. Caption = FORMAT("%s %s", (AppName, Caption));
  151. LinkLabel(RecycleBinLinkLabel);
  152. InitControls();
  153. FTreeLabels->Clear();
  154. int Index = 0;
  155. while (Index < PageControl->PageCount)
  156. {
  157. FTreeLabels->Add(PageControl->Pages[Index]->Hint);
  158. PageControl->Pages[Index]->Hint = "";
  159. Index++;
  160. }
  161. UpdateNavigationTree();
  162. if ((Options & loLocalDirectory) == 0)
  163. {
  164. LocalDirectoryLabel->Visible = false;
  165. LocalDirectoryEdit->Visible = false;
  166. LocalDirectoryDescLabel->Visible = false;
  167. DirectoriesGroup->Height = RemoteDirectoryEdit->Top + RemoteDirectoryEdit->Height + 12;
  168. DirectoryOptionsGroup->Top = DirectoriesGroup->Top + DirectoriesGroup->Height + 8;
  169. }
  170. #ifdef NO_FILEZILLA
  171. assert(TransferProtocolCombo->Items->Count == FSPROTOCOL_COUNT - 2 - 1);
  172. TransferProtocolCombo->Items->Delete(TransferProtocolCombo->Items->Count - 1);
  173. #endif
  174. if (StoredSessions && StoredSessions->Count &&
  175. (FSessionData->Name == StoredSessions->DefaultSettings->Name))
  176. {
  177. ChangePage(SessionListSheet);
  178. ActiveControl = SessionTree;
  179. assert(SessionTree->Items->Count > 0);
  180. if (SessionTree->Items->Count > 0)
  181. {
  182. SessionTree->Selected = SessionTree->Items->GetFirstNode();
  183. }
  184. }
  185. else
  186. {
  187. EditSession();
  188. }
  189. UpdateControls();
  190. }
  191. //---------------------------------------------------------------------
  192. TTreeNode * __fastcall TLoginDialog::AddSessionPath(AnsiString Path)
  193. {
  194. TTreeNode * Parent = NULL;
  195. while (!Path.IsEmpty())
  196. {
  197. AnsiString Folder = CutToChar(Path, '/', false);
  198. TTreeNode * Node =
  199. ((Parent == NULL) ? SessionTree->Items->GetFirstNode() : Parent->getFirstChild());
  200. // note that we allow folder with the same name as existing session
  201. // on the same level (see also SessionTreeEdited)
  202. while ((Node != NULL) && ((Node->Data != NULL) || !AnsiSameText(Node->Text, Folder)))
  203. {
  204. Node = Node->getNextSibling();
  205. }
  206. if (Node == NULL)
  207. {
  208. TTreeNode * AParent = Parent;
  209. Parent = SessionTree->Items->AddChild(Parent, Folder);
  210. UpdateFolderNode(Parent);
  211. // folders seem not to be sorted automatically (not having set the data property)
  212. if (AParent == NULL)
  213. {
  214. SessionTree->Items->AlphaSort();
  215. }
  216. else
  217. {
  218. AParent->AlphaSort();
  219. }
  220. }
  221. else
  222. {
  223. Parent = Node;
  224. }
  225. }
  226. return Parent;
  227. }
  228. //---------------------------------------------------------------------
  229. TTreeNode * __fastcall TLoginDialog::AddSession(TSessionData * Data)
  230. {
  231. TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Data->Name));
  232. TTreeNode * Node = SessionTree->Items->AddChild(Parent, UnixExtractFileName(Data->Name));
  233. Node->Data = Data;
  234. return Node;
  235. }
  236. //---------------------------------------------------------------------
  237. void __fastcall TLoginDialog::DestroySession(TSessionData * Data)
  238. {
  239. if (FEditingSessionData == Data)
  240. {
  241. FEditingSessionData = NULL;
  242. }
  243. StoredSessions->Remove(Data);
  244. }
  245. //---------------------------------------------------------------------
  246. void __fastcall TLoginDialog::LoadSessions()
  247. {
  248. SessionTree->Items->BeginUpdate();
  249. try
  250. {
  251. SessionTree->Items->Clear();
  252. assert(StoredSessions != NULL);
  253. for (int Index = 0; Index < StoredSessions->Count; Index++)
  254. {
  255. AddSession(StoredSessions->Sessions[Index]);
  256. }
  257. }
  258. __finally
  259. {
  260. // folders seem not to be sorted automatically (not having set the data property)
  261. SessionTree->AlphaSort();
  262. SessionTree->Items->EndUpdate();
  263. }
  264. SessionTree->Selected = SessionTree->Items->GetFirstNode();
  265. UpdateControls();
  266. }
  267. //---------------------------------------------------------------------------
  268. void __fastcall TLoginDialog::UpdateFolderNode(TTreeNode * Node)
  269. {
  270. Node->StateIndex = (Node->Expanded ? 2 : 3);
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall TLoginDialog::Default()
  274. {
  275. if (StoredSessions)
  276. {
  277. FSessionData->Assign(StoredSessions->DefaultSettings);
  278. }
  279. else
  280. {
  281. FSessionData->Default();
  282. }
  283. FEditingSessionData = NULL;
  284. LoadSession(FSessionData);
  285. FCurrentSessionName = "";
  286. }
  287. //---------------------------------------------------------------------
  288. void __fastcall TLoginDialog::LoadSession(TSessionData * aSessionData)
  289. {
  290. // it was always true
  291. assert(aSessionData == FSessionData);
  292. NoUpdate++;
  293. try
  294. {
  295. // Basic tab
  296. UserNameEdit->Text = aSessionData->UserName;
  297. PortNumberEdit->AsInteger = aSessionData->PortNumber;
  298. HostNameEdit->Text = aSessionData->HostName;
  299. PasswordEdit->Text = aSessionData->Password;
  300. PrivateKeyEdit->Text = aSessionData->PublicKeyFile;
  301. FtpsCombo->ItemIndex = aSessionData->Ftps;
  302. FColor = (TColor)aSessionData->Color;
  303. bool AllowScpFallback;
  304. TransferProtocolCombo->ItemIndex =
  305. FSProtocolToIndex(aSessionData->FSProtocol, AllowScpFallback);
  306. AllowScpFallbackCheck->Checked = AllowScpFallback;
  307. FDefaultPort = DefaultPort();
  308. // Directories tab
  309. LocalDirectoryEdit->Text = aSessionData->LocalDirectory;
  310. RemoteDirectoryEdit->Text = aSessionData->RemoteDirectory;
  311. UpdateDirectoriesCheck->Checked = aSessionData->UpdateDirectories;
  312. CacheDirectoriesCheck->Checked = aSessionData->CacheDirectories;
  313. CacheDirectoryChangesCheck->Checked = aSessionData->CacheDirectoryChanges;
  314. PreserveDirectoryChangesCheck->Checked = aSessionData->PreserveDirectoryChanges;
  315. ResolveSymlinksCheck->Checked = aSessionData->ResolveSymlinks;
  316. // Environment tab
  317. switch (aSessionData->DSTMode)
  318. {
  319. case dstmWin:
  320. DSTModeWinCheck->Checked = true;
  321. break;
  322. case dstmKeep:
  323. DSTModeKeepCheck->Checked = true;
  324. break;
  325. default:
  326. case dstmUnix:
  327. DSTModeUnixCheck->Checked = true;
  328. break;
  329. }
  330. if (aSessionData->EOLType == eolLF)
  331. {
  332. EOLTypeCombo->ItemIndex = 0;
  333. }
  334. else
  335. {
  336. EOLTypeCombo->ItemIndex = 1;
  337. }
  338. switch (aSessionData->NotUtf)
  339. {
  340. case asOn:
  341. UtfCombo->ItemIndex = 1;
  342. break;
  343. case asOff:
  344. UtfCombo->ItemIndex = 2;
  345. break;
  346. default:
  347. UtfCombo->ItemIndex = 0;
  348. break;
  349. }
  350. int TimeDifferenceMin = DateTimeToTimeStamp(aSessionData->TimeDifference).Time / 60000;
  351. if (double(aSessionData->TimeDifference) < 0)
  352. {
  353. TimeDifferenceMin = -TimeDifferenceMin;
  354. }
  355. TimeDifferenceEdit->AsInteger = TimeDifferenceMin / 60;
  356. TimeDifferenceMinutesEdit->AsInteger = TimeDifferenceMin % 60;
  357. // Environment/Recycle bin tab
  358. DeleteToRecycleBinCheck->Checked = aSessionData->DeleteToRecycleBin;
  359. OverwrittenToRecycleBinCheck->Checked = aSessionData->OverwrittenToRecycleBin;
  360. RecycleBinPathEdit->Text = aSessionData->RecycleBinPath;
  361. // SFTP tab
  362. if (aSessionData->SftpServer.IsEmpty())
  363. {
  364. SftpServerEdit->Text = SftpServerEdit->Items->Strings[0];
  365. }
  366. else
  367. {
  368. SftpServerEdit->Text = aSessionData->SftpServer;
  369. }
  370. // hide selection, which is wrongly shown initially even when the box has not focus
  371. SftpServerEdit->SelLength = 0;
  372. SFTPMaxVersionCombo->ItemIndex = aSessionData->SFTPMaxVersion;
  373. #define LOAD_SFTP_BUG_COMBO(BUG) \
  374. ComboAutoSwitchLoad(SFTPBug ## BUG ## Combo, aSessionData->SFTPBug[sb ## BUG])
  375. LOAD_SFTP_BUG_COMBO(Symlink);
  376. LOAD_SFTP_BUG_COMBO(SignedTS);
  377. #undef LOAD_SFTP_BUG_COMBO
  378. // FTP tab
  379. PostLoginCommandsMemo->Lines->Text = aSessionData->PostLoginCommands;
  380. ComboAutoSwitchLoad(FtpListAllCombo, aSessionData->FtpListAll);
  381. FtpForcePasvIpCheck->Checked = aSessionData->FtpForcePasvIp;
  382. // Authentication tab
  383. SshNoUserAuthCheck->Checked = aSessionData->SshNoUserAuth;
  384. TryAgentCheck->Checked = aSessionData->TryAgent;
  385. AuthTISCheck->Checked = aSessionData->AuthTIS;
  386. AuthKICheck->Checked = aSessionData->AuthKI;
  387. AuthKIPasswordCheck->Checked = aSessionData->AuthKIPassword;
  388. AuthGSSAPICheck2->Checked = aSessionData->AuthGSSAPI;
  389. AgentFwdCheck->Checked = aSessionData->AgentFwd;
  390. // SSH tab
  391. Ssh2LegacyDESCheck->Checked = aSessionData->Ssh2DES;
  392. CompressionCheck->Checked = aSessionData->Compression;
  393. switch (aSessionData->SshProt) {
  394. case ssh1only: SshProt1onlyButton->Checked = true; break;
  395. case ssh1: SshProt1Button->Checked = true; break;
  396. case ssh2: SshProt2Button->Checked = true; break;
  397. case ssh2only: SshProt2onlyButton->Checked = true; break;
  398. }
  399. CipherListBox->Items->Clear();
  400. assert(CIPHER_NAME_WARN+CIPHER_COUNT-1 == CIPHER_NAME_ARCFOUR);
  401. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  402. {
  403. CipherListBox->Items->AddObject(
  404. LoadStr(CIPHER_NAME_WARN+aSessionData->Cipher[Index]),
  405. (TObject*)aSessionData->Cipher[Index]);
  406. }
  407. // KEX tab
  408. KexListBox->Items->Clear();
  409. assert(KEX_NAME_WARN+KEX_COUNT-1 == KEX_NAME_RSA);
  410. for (int Index = 0; Index < KEX_COUNT; Index++)
  411. {
  412. KexListBox->Items->AddObject(
  413. LoadStr(KEX_NAME_WARN+aSessionData->Kex[Index]),
  414. (TObject*)aSessionData->Kex[Index]);
  415. }
  416. RekeyTimeEdit->AsInteger = aSessionData->RekeyTime;
  417. RekeyDataEdit->Text = aSessionData->RekeyData;
  418. // Connection tab
  419. FtpPasvModeCheck->Checked = aSessionData->FtpPasvMode;
  420. switch (aSessionData->PingType)
  421. {
  422. case ptNullPacket:
  423. PingNullPacketButton->Checked = true;
  424. break;
  425. case ptDummyCommand:
  426. PingDummyCommandButton->Checked = true;
  427. break;
  428. default:
  429. PingOffButton->Checked = true;
  430. break;
  431. }
  432. PingIntervalSecEdit->AsInteger = aSessionData->PingInterval;
  433. switch (aSessionData->FtpPingType)
  434. {
  435. case ptDummyCommand:
  436. FtpPingDummyCommandButton->Checked = true;
  437. break;
  438. default:
  439. FtpPingOffButton->Checked = true;
  440. break;
  441. }
  442. FtpPingIntervalSecEdit->AsInteger = aSessionData->FtpPingInterval;
  443. TimeoutEdit->AsInteger = aSessionData->Timeout;
  444. switch (aSessionData->AddressFamily)
  445. {
  446. case afIPv4:
  447. IPv4Button->Checked = true;
  448. break;
  449. case afIPv6:
  450. IPv6Button->Checked = true;
  451. break;
  452. case afAuto:
  453. default:
  454. IPAutoButton->Checked = true;
  455. break;
  456. }
  457. // Shell tab
  458. if (aSessionData->DefaultShell)
  459. {
  460. ShellEdit->Text = ShellEdit->Items->Strings[0];
  461. }
  462. else
  463. {
  464. ShellEdit->Text = aSessionData->Shell;
  465. }
  466. // hide selection, which is wrongly shown initially even when the box has not focus
  467. ShellEdit->SelLength = 0;
  468. if (aSessionData->DetectReturnVar)
  469. {
  470. ReturnVarEdit->Text = ReturnVarEdit->Items->Strings[0];
  471. }
  472. else
  473. {
  474. ReturnVarEdit->Text = aSessionData->ReturnVar;
  475. }
  476. ReturnVarEdit->SelLength = 0;
  477. ListingCommandEdit->Text = aSessionData->ListingCommand;
  478. ListingCommandEdit->SelLength = 0;
  479. LookupUserGroupsCheck->Checked = aSessionData->LookupUserGroups;
  480. ClearAliasesCheck->Checked = aSessionData->ClearAliases;
  481. IgnoreLsWarningsCheck->Checked = aSessionData->IgnoreLsWarnings;
  482. Scp1CompatibilityCheck->Checked = aSessionData->Scp1Compatibility;
  483. UnsetNationalVarsCheck->Checked = aSessionData->UnsetNationalVars;
  484. SCPLsFullTimeAutoCheck->Checked = (aSessionData->SCPLsFullTime != asOff);
  485. // Proxy tab
  486. SshProxyMethodCombo->ItemIndex = aSessionData->ProxyMethod;
  487. if (SupportedFtpProxyMethod(aSessionData->ProxyMethod))
  488. {
  489. FtpProxyMethodCombo->ItemIndex = aSessionData->ProxyMethod;
  490. }
  491. else
  492. {
  493. FtpProxyMethodCombo->ItemIndex = pmNone;
  494. }
  495. if (aSessionData->FtpProxyLogonType != 0)
  496. {
  497. FtpProxyMethodCombo->ItemIndex = LastSupportedFtpProxyMethod() + aSessionData->FtpProxyLogonType;
  498. }
  499. ProxyHostEdit->Text = aSessionData->ProxyHost;
  500. ProxyPortEdit->AsInteger = aSessionData->ProxyPort;
  501. ProxyUsernameEdit->Text = aSessionData->ProxyUsername;
  502. ProxyPasswordEdit->Text = aSessionData->ProxyPassword;
  503. ProxyTelnetCommandEdit->Text = aSessionData->ProxyTelnetCommand;
  504. ProxyLocalCommandEdit->Text = aSessionData->ProxyLocalCommand;
  505. ProxyLocalhostCheck->Checked = aSessionData->ProxyLocalhost;
  506. ProxyDNSCombo->ItemIndex = 2 - aSessionData->ProxyDNS;
  507. // Bugs tab
  508. #define LOAD_BUG_COMBO(BUG) \
  509. ComboAutoSwitchLoad(Bug ## BUG ## Combo, aSessionData->Bug[sb ## BUG])
  510. LOAD_BUG_COMBO(Ignore1);
  511. LOAD_BUG_COMBO(PlainPW1);
  512. LOAD_BUG_COMBO(RSA1);
  513. LOAD_BUG_COMBO(HMAC2);
  514. LOAD_BUG_COMBO(DeriveKey2);
  515. LOAD_BUG_COMBO(RSAPad2);
  516. LOAD_BUG_COMBO(Rekey2);
  517. LOAD_BUG_COMBO(PKSessID2);
  518. LOAD_BUG_COMBO(MaxPkt2);
  519. #undef LOAD_BUG_COMBO
  520. // Tunnel tab
  521. TunnelCheck->Checked = aSessionData->Tunnel;
  522. TunnelUserNameEdit->Text = aSessionData->TunnelUserName;
  523. TunnelPortNumberEdit->AsInteger = aSessionData->TunnelPortNumber;
  524. TunnelHostNameEdit->Text = aSessionData->TunnelHostName;
  525. TunnelPasswordEdit->Text = aSessionData->TunnelPassword;
  526. TunnelPrivateKeyEdit->Text = aSessionData->TunnelPublicKeyFile;
  527. if (aSessionData->TunnelAutoassignLocalPortNumber)
  528. {
  529. TunnelLocalPortNumberEdit->Text = TunnelLocalPortNumberEdit->Items->Strings[0];
  530. }
  531. else
  532. {
  533. TunnelLocalPortNumberEdit->Text = IntToStr(aSessionData->TunnelLocalPortNumber);
  534. }
  535. // hide selection, which is wrongly shown initially even when the box has not focus
  536. TunnelLocalPortNumberEdit->SelLength = 0;
  537. }
  538. __finally
  539. {
  540. NoUpdate--;
  541. UpdateControls();
  542. }
  543. if (UnixExtractFileName(aSessionData->Name) != aSessionData->DefaultSessionName)
  544. {
  545. FCurrentSessionName = aSessionData->Name;
  546. }
  547. else
  548. {
  549. FCurrentSessionName = "";
  550. }
  551. }
  552. //---------------------------------------------------------------------
  553. void __fastcall TLoginDialog::SaveSession(TSessionData * aSessionData)
  554. {
  555. // it was always true
  556. assert(aSessionData == FSessionData);
  557. // Basic tab
  558. aSessionData->UserName = UserNameEdit->Text.Trim();
  559. aSessionData->PortNumber = PortNumberEdit->AsInteger;
  560. // must be loaded after UserName, because HostName may be in format user@host
  561. aSessionData->HostName = HostNameEdit->Text.Trim();
  562. aSessionData->Password = PasswordEdit->Text;
  563. aSessionData->PublicKeyFile = PrivateKeyEdit->Text;
  564. aSessionData->Ftps = (TFtps)FtpsCombo->ItemIndex;
  565. aSessionData->Color = FColor;
  566. aSessionData->FSProtocol = IndexToFSProtocol(
  567. TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked);
  568. // SSH tab
  569. aSessionData->Compression = CompressionCheck->Checked;
  570. aSessionData->Ssh2DES = Ssh2LegacyDESCheck->Checked;
  571. if (SshProt1onlyButton->Checked) aSessionData->SshProt = ssh1only;
  572. else
  573. if (SshProt1Button->Checked) aSessionData->SshProt = ssh1;
  574. else
  575. if (SshProt2Button->Checked) aSessionData->SshProt = ssh2;
  576. else aSessionData->SshProt = ssh2only;
  577. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  578. {
  579. aSessionData->Cipher[Index] = (TCipher)CipherListBox->Items->Objects[Index];
  580. }
  581. // Kex tab
  582. for (int Index = 0; Index < KEX_COUNT; Index++)
  583. {
  584. aSessionData->Kex[Index] = (TKex)KexListBox->Items->Objects[Index];
  585. }
  586. aSessionData->RekeyTime = RekeyTimeEdit->AsInteger;
  587. aSessionData->RekeyData = RekeyDataEdit->Text;
  588. // Authentication tab
  589. aSessionData->SshNoUserAuth = SshNoUserAuthCheck->Checked;
  590. aSessionData->TryAgent = TryAgentCheck->Checked;
  591. aSessionData->AuthTIS = AuthTISCheck->Checked;
  592. aSessionData->AuthKI = AuthKICheck->Checked;
  593. aSessionData->AuthKIPassword = AuthKIPasswordCheck->Checked;
  594. aSessionData->AuthGSSAPI = AuthGSSAPICheck2->Checked;
  595. aSessionData->AgentFwd = AgentFwdCheck->Checked;
  596. // Connection tab
  597. aSessionData->FtpPasvMode = FtpPasvModeCheck->Checked;
  598. if (PingNullPacketButton->Checked)
  599. {
  600. aSessionData->PingType = ptNullPacket;
  601. }
  602. else if (PingDummyCommandButton->Checked)
  603. {
  604. aSessionData->PingType = ptDummyCommand;
  605. }
  606. else
  607. {
  608. aSessionData->PingType = ptOff;
  609. }
  610. aSessionData->PingInterval = PingIntervalSecEdit->AsInteger;
  611. aSessionData->FtpPingType = (FtpPingDummyCommandButton->Checked ? ptDummyCommand : ptOff);
  612. aSessionData->FtpPingInterval = FtpPingIntervalSecEdit->AsInteger;
  613. aSessionData->Timeout = TimeoutEdit->AsInteger;
  614. if (IPv4Button->Checked)
  615. {
  616. aSessionData->AddressFamily = afIPv4;
  617. }
  618. else if (IPv6Button->Checked)
  619. {
  620. aSessionData->AddressFamily = afIPv6;
  621. }
  622. else
  623. {
  624. aSessionData->AddressFamily = afAuto;
  625. }
  626. // Directories tab
  627. aSessionData->LocalDirectory = LocalDirectoryEdit->Text;
  628. aSessionData->RemoteDirectory = RemoteDirectoryEdit->Text;
  629. aSessionData->UpdateDirectories = UpdateDirectoriesCheck->Checked;
  630. aSessionData->CacheDirectories = CacheDirectoriesCheck->Checked;
  631. aSessionData->CacheDirectoryChanges = CacheDirectoryChangesCheck->Checked;
  632. aSessionData->PreserveDirectoryChanges = PreserveDirectoryChangesCheck->Checked;
  633. aSessionData->ResolveSymlinks = ResolveSymlinksCheck->Checked;
  634. // Environment tab
  635. if (DSTModeUnixCheck->Checked) aSessionData->DSTMode = dstmUnix;
  636. else
  637. if (DSTModeKeepCheck->Checked) aSessionData->DSTMode = dstmKeep;
  638. else aSessionData->DSTMode = dstmWin;
  639. if (EOLTypeCombo->ItemIndex == 0) aSessionData->EOLType = eolLF;
  640. else aSessionData->EOLType = eolCRLF;
  641. switch (UtfCombo->ItemIndex)
  642. {
  643. case 1:
  644. aSessionData->NotUtf = asOn;
  645. break;
  646. case 2:
  647. aSessionData->NotUtf = asOff;
  648. break;
  649. default:
  650. aSessionData->NotUtf = asAuto;
  651. break;
  652. }
  653. aSessionData->TimeDifference =
  654. (double(TimeDifferenceEdit->AsInteger) / 24) +
  655. (double(TimeDifferenceMinutesEdit->AsInteger) / 24 / 60);
  656. // Environment/Recycle bin tab
  657. aSessionData->DeleteToRecycleBin = DeleteToRecycleBinCheck->Checked;
  658. aSessionData->OverwrittenToRecycleBin = OverwrittenToRecycleBinCheck->Checked;
  659. aSessionData->RecycleBinPath = RecycleBinPathEdit->Text;
  660. // SCP tab
  661. aSessionData->DefaultShell = (ShellEdit->Text == ShellEdit->Items->Strings[0]);
  662. aSessionData->Shell = (aSessionData->DefaultShell ? AnsiString() : ShellEdit->Text);
  663. aSessionData->DetectReturnVar = (ReturnVarEdit->Text == ReturnVarEdit->Items->Strings[0]);
  664. aSessionData->ReturnVar = (aSessionData->DetectReturnVar ? AnsiString() : ReturnVarEdit->Text);
  665. aSessionData->ListingCommand = ListingCommandEdit->Text;
  666. aSessionData->LookupUserGroups = LookupUserGroupsCheck->Checked;
  667. aSessionData->ClearAliases = ClearAliasesCheck->Checked;
  668. aSessionData->IgnoreLsWarnings = IgnoreLsWarningsCheck->Checked;
  669. aSessionData->Scp1Compatibility = Scp1CompatibilityCheck->Checked;
  670. aSessionData->UnsetNationalVars = UnsetNationalVarsCheck->Checked;
  671. aSessionData->SCPLsFullTime = SCPLsFullTimeAutoCheck->Checked ? asAuto : asOff;
  672. // SFTP tab
  673. aSessionData->SftpServer =
  674. ((SftpServerEdit->Text == SftpServerEdit->Items->Strings[0]) ?
  675. AnsiString() : SftpServerEdit->Text);
  676. aSessionData->SFTPMaxVersion = SFTPMaxVersionCombo->ItemIndex;
  677. #define SAVE_SFTP_BUG_COMBO(BUG) aSessionData->SFTPBug[sb ## BUG] = ComboAutoSwitchSave(SFTPBug ## BUG ## Combo);
  678. SAVE_SFTP_BUG_COMBO(Symlink);
  679. SAVE_SFTP_BUG_COMBO(SignedTS);
  680. #undef SAVE_SFTP_BUG_COMBO
  681. // FTP tab
  682. aSessionData->PostLoginCommands = PostLoginCommandsMemo->Lines->Text;
  683. aSessionData->FtpListAll = ComboAutoSwitchSave(FtpListAllCombo);
  684. aSessionData->FtpForcePasvIp = FtpForcePasvIpCheck->Checked;
  685. // Proxy tab
  686. aSessionData->ProxyMethod = GetProxyMethod();
  687. aSessionData->FtpProxyLogonType = GetFtpProxyLogonType();
  688. aSessionData->ProxyHost = ProxyHostEdit->Text;
  689. aSessionData->ProxyPort = ProxyPortEdit->AsInteger;
  690. aSessionData->ProxyUsername = ProxyUsernameEdit->Text;
  691. aSessionData->ProxyPassword = ProxyPasswordEdit->Text;
  692. aSessionData->ProxyTelnetCommand = ProxyTelnetCommandEdit->Text;
  693. aSessionData->ProxyLocalCommand = ProxyLocalCommandEdit->Text;
  694. aSessionData->ProxyLocalhost = ProxyLocalhostCheck->Checked;
  695. aSessionData->ProxyDNS = (TAutoSwitch)(2 - ProxyDNSCombo->ItemIndex);
  696. // Bugs tab
  697. #define SAVE_BUG_COMBO(BUG) aSessionData->Bug[sb ## BUG] = ComboAutoSwitchSave(Bug ## BUG ## Combo)
  698. SAVE_BUG_COMBO(Ignore1);
  699. SAVE_BUG_COMBO(PlainPW1);
  700. SAVE_BUG_COMBO(RSA1);
  701. SAVE_BUG_COMBO(HMAC2);
  702. SAVE_BUG_COMBO(DeriveKey2);
  703. SAVE_BUG_COMBO(RSAPad2);
  704. SAVE_BUG_COMBO(Rekey2);
  705. SAVE_BUG_COMBO(PKSessID2);
  706. SAVE_BUG_COMBO(MaxPkt2);
  707. #undef SAVE_BUG_COMBO
  708. // Tunnel tab
  709. aSessionData->Tunnel = TunnelCheck->Checked;
  710. aSessionData->TunnelUserName = TunnelUserNameEdit->Text;
  711. aSessionData->TunnelPortNumber = TunnelPortNumberEdit->AsInteger;
  712. aSessionData->TunnelHostName = TunnelHostNameEdit->Text;
  713. aSessionData->TunnelPassword = TunnelPasswordEdit->Text;
  714. aSessionData->TunnelPublicKeyFile = TunnelPrivateKeyEdit->Text;
  715. if (TunnelLocalPortNumberEdit->Text == TunnelLocalPortNumberEdit->Items->Strings[0])
  716. {
  717. aSessionData->TunnelLocalPortNumber = 0;
  718. }
  719. else
  720. {
  721. aSessionData->TunnelLocalPortNumber = StrToIntDef(TunnelLocalPortNumberEdit->Text, 0);
  722. }
  723. AnsiString Name = FCurrentSessionName;
  724. if (Name.IsEmpty())
  725. {
  726. Name = UnixIncludeTrailingBackslash(SessionNodePath(CurrentSessionFolderNode())) +
  727. aSessionData->DefaultSessionName;
  728. }
  729. aSessionData->Name = Name;
  730. }
  731. //---------------------------------------------------------------------
  732. void __fastcall TLoginDialog::UpdateNavigationTree()
  733. {
  734. TTreeNode * ActiveNode = NULL;
  735. NoUpdate++;
  736. try
  737. {
  738. int Index = 0;
  739. TTreeNode * PrevNode = NULL;
  740. while (Index < PageControl->PageCount)
  741. {
  742. TTabSheet * Tab = PageControl->Pages[Index];
  743. if (Tab->Enabled)
  744. {
  745. bool Indented = ((Tab->Tag % 100) == 2);
  746. AnsiString Label = FTreeLabels->Strings[Index];
  747. TTreeNode * Node;
  748. if (PrevNode == NULL)
  749. {
  750. assert(!Indented);
  751. Node = NavigationTree->Items->GetFirstNode();
  752. }
  753. else
  754. {
  755. if (Indented)
  756. {
  757. if (PrevNode->Level == 0)
  758. {
  759. Node = PrevNode->getFirstChild();
  760. if (Node == NULL)
  761. {
  762. Node = NavigationTree->Items->AddChild(PrevNode, Label);
  763. }
  764. }
  765. else
  766. {
  767. Node = PrevNode->getNextSibling();
  768. if (Node == NULL)
  769. {
  770. Node = NavigationTree->Items->Add(PrevNode, Label);
  771. }
  772. }
  773. }
  774. else
  775. {
  776. if (PrevNode->Level == 0)
  777. {
  778. // delete all excess children of previous top level node
  779. while ((Node = PrevNode->GetNext()) != PrevNode->getNextSibling())
  780. {
  781. Node->Delete();
  782. }
  783. Node = PrevNode->getNextSibling();
  784. if (Node == NULL)
  785. {
  786. Node = NavigationTree->Items->Add(PrevNode, Label);
  787. }
  788. }
  789. else
  790. {
  791. // delete all excess children of previous top level node
  792. while ((Node = PrevNode->getNextSibling()) != NULL)
  793. {
  794. Node->Delete();
  795. }
  796. Node = PrevNode->GetNext();
  797. if (Node == NULL)
  798. {
  799. Node = NavigationTree->Items->Add(NULL, Label);
  800. }
  801. }
  802. }
  803. }
  804. Node->Text = Label;
  805. Node->SelectedIndex = reinterpret_cast<int>(Tab);
  806. PrevNode = Node;
  807. if (PageControl->ActivePage == Tab)
  808. {
  809. Node->Selected = true;
  810. ActiveNode = Node;
  811. }
  812. }
  813. Index++;
  814. }
  815. TTreeNode * Node;
  816. while ((Node = PrevNode->GetNext()) != NULL)
  817. {
  818. Node->Delete();
  819. }
  820. NavigationTree->FullExpand();
  821. }
  822. __finally
  823. {
  824. NoUpdate--;
  825. }
  826. // node of active page was hidden
  827. if (ActiveNode == NULL)
  828. {
  829. ChangePage(BasicSheet);
  830. }
  831. }
  832. //---------------------------------------------------------------------
  833. void __fastcall TLoginDialog::UpdateControls()
  834. {
  835. if (Visible && FInitialized)
  836. {
  837. NoUpdate++;
  838. try
  839. {
  840. TFSProtocol FSProtocol = IndexToFSProtocol(
  841. TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked);
  842. bool SshProtocol =
  843. (FSProtocol == fsSFTPonly) || (FSProtocol == fsSFTP) || (FSProtocol == fsSCPonly);
  844. bool FtpProtocol = (FSProtocol == fsFTP);
  845. assert(SshProtocol != FtpProtocol); // xor, some of the code below rely on this
  846. bool Advanced = ShowAdvancedLoginOptionsCheck->Checked;
  847. // basic/session sheet
  848. AllowScpFallbackCheck->Visible =
  849. (IndexToFSProtocol(TransferProtocolCombo->ItemIndex, false) == fsSFTPonly);
  850. FtpsCombo->Visible = (FSProtocol == fsFTP);
  851. EnableControl(PrivateKeyEdit, SshProtocol);
  852. EnableControl(PrivateKeyLabel, PrivateKeyEdit->Enabled);
  853. // log sheet
  854. LogSheet->Enabled = Advanced;
  855. // connection sheet
  856. ConnSheet->Enabled = Advanced;
  857. EnableControl(FtpPasvModeCheck, FtpProtocol);
  858. if (FtpProtocol &&
  859. (FtpProxyMethodCombo->ItemIndex != pmNone) &&
  860. SupportedFtpProxyMethod(FtpProxyMethodCombo->ItemIndex) &&
  861. !FtpPasvModeCheck->Checked)
  862. {
  863. FtpPasvModeCheck->Checked = true;
  864. MessageDialog(LoadStr(FTP_PASV_MODE_REQUIRED), qtInformation, qaOK);
  865. }
  866. PingGroup->Visible = SshProtocol;
  867. EnableControl(PingIntervalSecEdit, !PingOffButton->Checked);
  868. EnableControl(PingIntervalLabel, PingIntervalSecEdit->Enabled);
  869. FtpPingGroup->Visible = FtpProtocol;
  870. EnableControl(FtpPingIntervalSecEdit, !FtpPingOffButton->Checked);
  871. EnableControl(FtpPingIntervalLabel, FtpPingIntervalSecEdit->Enabled);
  872. EnableControl(IPAutoButton, SshProtocol);
  873. // stored sessions sheet
  874. EnableControl(SessionTree, SessionTree->Items->Count > 0);
  875. // ssh/authentication sheet
  876. AuthSheet->Enabled = SshProtocol && Advanced;
  877. EnableControl(SshNoUserAuthCheck, !SshProt1onlyButton->Checked);
  878. EnableControl(AuthenticationGroup,
  879. !SshNoUserAuthCheck->Enabled || !SshNoUserAuthCheck->Checked);
  880. EnableControl(AuthTISCheck, AuthenticationGroup->Enabled && !SshProt2onlyButton->Checked);
  881. EnableControl(AuthKICheck, AuthenticationGroup->Enabled && !SshProt1onlyButton->Checked);
  882. EnableControl(AuthKIPasswordCheck,
  883. AuthenticationGroup->Enabled &&
  884. ((AuthTISCheck->Enabled && AuthTISCheck->Checked) ||
  885. (AuthKICheck->Enabled && AuthKICheck->Checked)));
  886. EnableControl(AuthGSSAPICheck2,
  887. AuthenticationGroup->Enabled && !SshProt1onlyButton->Checked);
  888. // ssh sheet
  889. AdvancedSheet->Enabled = SshProtocol;
  890. EnableControl(CipherUpButton, CipherListBox->ItemIndex > 0);
  891. EnableControl(CipherDownButton, CipherListBox->ItemIndex >= 0 &&
  892. CipherListBox->ItemIndex < CipherListBox->Items->Count-1);
  893. EnableControl(Ssh2LegacyDESCheck, !SshProt1onlyButton->Checked);
  894. // ssh/kex sheet
  895. KexSheet->Enabled = SshProtocol && Advanced && !SshProt1onlyButton->Checked &&
  896. (BugRekey2Combo->ItemIndex != 2);
  897. EnableControl(KexUpButton, KexListBox->ItemIndex > 0);
  898. EnableControl(KexDownButton, KexListBox->ItemIndex >= 0 &&
  899. KexListBox->ItemIndex < KexListBox->Items->Count-1);
  900. // ssh/bugs sheet
  901. BugsSheet->Enabled = SshProtocol && Advanced;
  902. EnableControl(BugIgnore1Combo, !SshProt2onlyButton->Checked);
  903. EnableControl(BugIgnore1Label, BugIgnore1Combo->Enabled);
  904. EnableControl(BugPlainPW1Combo, !SshProt2onlyButton->Checked);
  905. EnableControl(BugPlainPW1Label, BugPlainPW1Combo->Enabled);
  906. EnableControl(BugRSA1Combo, !SshProt2onlyButton->Checked);
  907. EnableControl(BugRSA1Label, BugRSA1Combo->Enabled);
  908. EnableControl(BugHMAC2Combo, !SshProt1onlyButton->Checked);
  909. EnableControl(BugHMAC2Label, BugHMAC2Combo->Enabled);
  910. EnableControl(BugDeriveKey2Combo, !SshProt1onlyButton->Checked);
  911. EnableControl(BugDeriveKey2Label, BugDeriveKey2Combo->Enabled);
  912. EnableControl(BugRSAPad2Combo, !SshProt1onlyButton->Checked);
  913. EnableControl(BugRSAPad2Label, BugRSAPad2Combo->Enabled);
  914. EnableControl(BugPKSessID2Combo, !SshProt1onlyButton->Checked);
  915. EnableControl(BugPKSessID2Label, BugPKSessID2Combo->Enabled);
  916. EnableControl(BugRekey2Combo, !SshProt1onlyButton->Checked);
  917. EnableControl(BugRekey2Label, BugRekey2Combo->Enabled);
  918. EnableControl(BugMaxPkt2Combo, !SshProt1onlyButton->Checked);
  919. EnableControl(BugMaxPkt2Label, BugMaxPkt2Combo->Enabled);
  920. // connection/proxy sheet
  921. TComboBox * ProxyMethodCombo = (SshProtocol ? SshProxyMethodCombo : FtpProxyMethodCombo);
  922. ProxyMethodCombo->Visible = true;
  923. ProxyMethodLabel->FocusControl = ProxyMethodCombo;
  924. TComboBox * OtherProxyMethodCombo = (!SshProtocol ? SshProxyMethodCombo : FtpProxyMethodCombo);
  925. OtherProxyMethodCombo->Visible = false;
  926. if (SupportedFtpProxyMethod(ProxyMethodCombo->ItemIndex))
  927. {
  928. OtherProxyMethodCombo->ItemIndex = ProxyMethodCombo->ItemIndex;
  929. }
  930. else
  931. {
  932. OtherProxyMethodCombo->ItemIndex = pmNone;
  933. }
  934. ProxySheet->Enabled = Advanced;
  935. TProxyMethod ProxyMethod = GetProxyMethod();
  936. int FtpProxyLogonType = GetFtpProxyLogonType();
  937. AnsiString ProxyCommand =
  938. ((ProxyMethod == pmCmd) ?
  939. ProxyLocalCommandEdit->Text : ProxyTelnetCommandEdit->Text);
  940. EnableControl(ProxyHostEdit,
  941. (ProxyMethod == pmSocks4) ||
  942. (ProxyMethod == pmSocks5) ||
  943. (ProxyMethod == pmHTTP) ||
  944. (ProxyMethod == pmTelnet) ||
  945. ((ProxyMethod == pmCmd) && AnsiContainsText(ProxyCommand, "%proxyhost")) ||
  946. (FtpProxyLogonType > 0));
  947. EnableControl(ProxyHostLabel, ProxyHostEdit->Enabled);
  948. EnableControl(ProxyPortEdit,
  949. (ProxyMethod == pmSocks4) ||
  950. (ProxyMethod == pmSocks5) ||
  951. (ProxyMethod == pmHTTP) ||
  952. (ProxyMethod == pmTelnet) ||
  953. ((ProxyMethod == pmCmd) && AnsiContainsText(ProxyCommand, "%proxyport")) ||
  954. (FtpProxyLogonType > 0));
  955. EnableControl(ProxyPortLabel, ProxyPortEdit->Enabled);
  956. EnableControl(ProxyUsernameEdit,
  957. // FZAPI does not support username for SOCKS4
  958. ((ProxyMethod == pmSocks4) && SshProtocol) ||
  959. (ProxyMethod == pmSocks5) ||
  960. (ProxyMethod == pmHTTP) ||
  961. (((ProxyMethod == pmTelnet) ||
  962. (ProxyMethod == pmCmd)) &&
  963. AnsiContainsText(ProxyCommand, "%user")) ||
  964. ((FtpProxyLogonType > 0) && (FtpProxyLogonType != 3) && (FtpProxyLogonType != 5)));
  965. EnableControl(ProxyUsernameLabel, ProxyUsernameEdit->Enabled);
  966. EnableControl(ProxyPasswordEdit,
  967. (ProxyMethod == pmSocks5) ||
  968. (ProxyMethod == pmHTTP) ||
  969. (((ProxyMethod == pmTelnet) ||
  970. (ProxyMethod == pmCmd)) &&
  971. AnsiContainsText(ProxyCommand, "%pass")) ||
  972. ((FtpProxyLogonType > 0) && (FtpProxyLogonType != 3) && (FtpProxyLogonType != 5)));
  973. EnableControl(ProxyPasswordLabel, ProxyPasswordEdit->Enabled);
  974. bool ProxySettings = (ProxyMethod != pmNone) && SshProtocol;
  975. EnableControl(ProxySettingsGroup, ProxySettings);
  976. EnableControl(ProxyTelnetCommandEdit,
  977. ProxySettings && (ProxyMethod == pmTelnet));
  978. EnableControl(ProxyTelnetCommandLabel, ProxyTelnetCommandEdit->Enabled);
  979. EnableControl(ProxyTelnetCommandHintText, ProxyTelnetCommandEdit->Enabled);
  980. ProxyLocalCommandEdit->Visible = (ProxyMethod == pmCmd);
  981. ProxyLocalCommandLabel->Visible = ProxyLocalCommandEdit->Visible;
  982. ProxyLocalCommandBrowseButton->Visible = ProxyLocalCommandEdit->Visible;
  983. ProxyLocalCommandHintText->Visible = ProxyLocalCommandEdit->Visible;
  984. ProxyTelnetCommandEdit->Visible = !ProxyLocalCommandEdit->Visible;
  985. ProxyTelnetCommandLabel->Visible = ProxyTelnetCommandEdit->Visible;
  986. ProxyTelnetCommandHintText->Visible = ProxyTelnetCommandEdit->Visible;
  987. // environment/directories sheet
  988. DirectoryOptionsGroup->Visible = Advanced;
  989. EnableControl(CacheDirectoryChangesCheck,
  990. ((FSProtocol != fsSCPonly) || CacheDirectoriesCheck->Checked) && DirectoriesSheet->Enabled);
  991. EnableControl(PreserveDirectoryChangesCheck,
  992. CacheDirectoryChangesCheck->Enabled && CacheDirectoryChangesCheck->Checked &&
  993. DirectoriesSheet->Enabled);
  994. EnableControl(ResolveSymlinksCheck, (FSProtocol != fsFTP) && DirectoriesSheet->Enabled);
  995. // environment sheet
  996. EnableControl(EOLTypeCombo, (FSProtocol != fsFTP) && EnvironmentSheet->Enabled);
  997. EnableControl(EOLTypeLabel, EOLTypeCombo->Enabled);
  998. EnableControl(DSTModeGroup, (FSProtocol != fsFTP) && EnvironmentSheet->Enabled);
  999. EnableControl(DSTModeKeepCheck, UsesDaylightHack() && DSTModeGroup->Enabled);
  1000. EnableControl(UtfCombo, (FSProtocol != fsSCPonly) && EnvironmentSheet->Enabled);
  1001. EnableControl(UtfLabel, UtfCombo->Enabled);
  1002. // should be enabled for fsSFTP (SCP fallback) too, but it would cause confusion
  1003. EnableControl(TimeDifferenceEdit, (((FSProtocol == fsFTP) || (FSProtocol == fsSCPonly)) && EnvironmentSheet->Enabled));
  1004. EnableControl(TimeDifferenceLabel, TimeDifferenceEdit->Enabled);
  1005. EnableControl(TimeDifferenceHoursLabel, TimeDifferenceEdit->Enabled);
  1006. EnableControl(TimeDifferenceMinutesEdit, TimeDifferenceEdit->Enabled);
  1007. EnableControl(TimeDifferenceMinutesLabel, TimeDifferenceEdit->Enabled);
  1008. RecycleBinLinkLabel->Visible = ShowAdvancedLoginOptionsCheck->Checked;
  1009. EnvironmentOtherLabel->Visible = RecycleBinLinkLabel->Visible;
  1010. // environment/recycle bin sheet
  1011. RecycleBinSheet->Enabled =
  1012. ShowAdvancedLoginOptionsCheck->Checked && FRecycleBinSheetVisible;
  1013. EnableControl(OverwrittenToRecycleBinCheck, (FSProtocol != fsSCPonly) &&
  1014. (FSProtocol != fsFTP) && RecycleBinSheet->Enabled);
  1015. EnableControl(RecycleBinPathEdit,
  1016. (DeleteToRecycleBinCheck->Enabled && DeleteToRecycleBinCheck->Checked) ||
  1017. (OverwrittenToRecycleBinCheck->Enabled && OverwrittenToRecycleBinCheck->Checked) &&
  1018. RecycleBinSheet->Enabled);
  1019. EnableControl(RecycleBinPathLabel, RecycleBinPathEdit->Enabled &&
  1020. RecycleBinSheet->Enabled);
  1021. // environment/sftp sheet
  1022. SftpSheet->Enabled = Advanced && ((FSProtocol == fsSFTPonly) || (FSProtocol == fsSFTP));
  1023. // environment/scp/shell
  1024. ScpSheet->Enabled = Advanced && SshProtocol;
  1025. // disable also for SFTP with SCP fallback, as if someone wants to configure
  1026. // these he/she probably intends to use SCP and should explicitly select it.
  1027. // (note that these are not used for secondary shell session)
  1028. EnableControl(ScpLsOptionsGroup, (FSProtocol == fsSCPonly));
  1029. EnableControl(OtherShellOptionsGroup, (FSProtocol == fsSCPonly));
  1030. // environment/ftp
  1031. FtpSheet->Enabled = Advanced && FtpProtocol;
  1032. EnableControl(FtpForcePasvIpCheck,
  1033. FtpPasvModeCheck->Checked &&
  1034. (IPAutoButton->Checked || IPv4Button->Checked));
  1035. // tunnel sheet
  1036. TunnelSheet->Enabled = Advanced && SshProtocol;
  1037. // probably needless
  1038. EnableControl(TunnelSessionGroup, TunnelCheck->Enabled && TunnelCheck->Checked);
  1039. EnableControl(TunnelOptionsGroup, TunnelSessionGroup->Enabled);
  1040. // preferences sheet
  1041. GeneralSheet->Enabled = FLAGSET(Options, loPreferences);
  1042. // this methods saves us from calling GetSessionData,
  1043. // which breaks changing locale (session data are not preserved,
  1044. // as we would overwrite them with non-initialized control data)
  1045. if (PageControl->ActivePage == SessionListSheet)
  1046. {
  1047. LoginButton->Default = (SelectedSession != NULL) && SelectedSession->CanLogin;
  1048. }
  1049. else
  1050. {
  1051. LoginButton->Default = true;
  1052. }
  1053. LoadButton->Default = !LoginButton->Default;
  1054. AboutButton->Visible = (Options & loAbout);
  1055. LanguagesButton->Visible = (Options & loLanguage);
  1056. ShellIconsButton->Visible = (Options & loTools);
  1057. ToolsMenuButton->Visible = (Options & loTools);
  1058. LoggingFrame->EnableLogWindow = (Options & loLogWindow);
  1059. ColorButton->Visible = (Options & loColor);
  1060. UpdateNavigationTree();
  1061. }
  1062. __finally
  1063. {
  1064. NoUpdate--;
  1065. }
  1066. }
  1067. }
  1068. //---------------------------------------------------------------------------
  1069. void __fastcall TLoginDialog::DataChange(TObject * /*Sender*/)
  1070. {
  1071. if (!NoUpdate) UpdateControls();
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TLoginDialog::FormShow(TObject * /*Sender*/)
  1075. {
  1076. if (!FInitialized)
  1077. {
  1078. Init();
  1079. TSessionData * Data = GetSessionData();
  1080. if (Data == FSessionData)
  1081. {
  1082. LoadSession(Data);
  1083. }
  1084. else
  1085. {
  1086. Default();
  1087. }
  1088. }
  1089. if (FLocaleChanging)
  1090. {
  1091. Init();
  1092. LoadSession(FSessionData);
  1093. ChangePage(FSavedTab);
  1094. if (FSavedSession >= 0)
  1095. {
  1096. SessionTree->Selected = SessionTree->Items->Item[FSavedSession];
  1097. SessionTree->Selected->MakeVisible();
  1098. }
  1099. LoadConfiguration();
  1100. }
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. void __fastcall TLoginDialog::SessionTreeChange(TObject * /*Sender*/,
  1104. TTreeNode * /*Node*/)
  1105. {
  1106. UpdateControls();
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. void __fastcall TLoginDialog::SetSessionData(TSessionData * value)
  1110. {
  1111. FSessionData->Assign(value);
  1112. FSessionData->Special = false;
  1113. FEditingSessionData = value;
  1114. LoadSession(FSessionData);
  1115. }
  1116. //---------------------------------------------------------------------------
  1117. TSessionData * __fastcall TLoginDialog::GetSessionData()
  1118. {
  1119. if (PageControl->ActivePage == SessionListSheet)
  1120. {
  1121. return SelectedSession;
  1122. }
  1123. else
  1124. {
  1125. SaveSession(FSessionData);
  1126. return FSessionData;
  1127. }
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TLoginDialog::SetStoredSessions(TStoredSessionList * value)
  1131. {
  1132. if (FStoredSessions != value)
  1133. {
  1134. FStoredSessions = value;
  1135. LoadSessions();
  1136. }
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. void __fastcall TLoginDialog::SessionTreeDblClick(TObject * /*Sender*/)
  1140. {
  1141. TPoint P = SessionTree->ScreenToClient(Mouse->CursorPos);
  1142. if (SelectedSession && (SessionTree->GetNodeAt(P.x, P.y) == SessionTree->Selected))
  1143. {
  1144. if (SelectedSession->CanLogin)
  1145. {
  1146. ModalResult = mrOk;
  1147. }
  1148. else
  1149. {
  1150. SessionData = SelectedSession;
  1151. EditSession();
  1152. }
  1153. }
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. TSessionData * __fastcall TLoginDialog::GetSelectedSession()
  1157. {
  1158. if (SessionTree->Selected != NULL)
  1159. {
  1160. return (TSessionData *)SessionTree->Selected->Data;
  1161. }
  1162. else
  1163. {
  1164. return NULL;
  1165. }
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. void __fastcall TLoginDialog::SessionTreeKeyDown(TObject * /*Sender*/,
  1169. WORD & Key, TShiftState /*Shift*/)
  1170. {
  1171. if (!SessionTree->IsEditing())
  1172. {
  1173. if (Key == VK_DELETE)
  1174. {
  1175. DeleteSessionAction->Execute();
  1176. Key = 0;
  1177. }
  1178. else if (Key == VK_F2)
  1179. {
  1180. RenameSessionAction->Execute();
  1181. Key = 0;
  1182. }
  1183. }
  1184. }
  1185. //---------------------------------------------------------------------------
  1186. void __fastcall TLoginDialog::EditSession()
  1187. {
  1188. ChangePage(BasicSheet);
  1189. HostNameEdit->SetFocus();
  1190. }
  1191. //---------------------------------------------------------------------------
  1192. void __fastcall TLoginDialog::EditSessionActionExecute(TObject * /*Sender*/)
  1193. {
  1194. if (SelectedSession)
  1195. {
  1196. SessionData = SelectedSession;
  1197. EditSession();
  1198. }
  1199. }
  1200. //---------------------------------------------------------------------------
  1201. void __fastcall TLoginDialog::SaveSessionActionExecute(TObject * /*Sender*/)
  1202. {
  1203. SaveSession(FSessionData);
  1204. bool SavePassword;
  1205. bool * PSavePassword;
  1206. if (Configuration->DisablePasswordStoring ||
  1207. !FSessionData->HasAnyPassword())
  1208. {
  1209. PSavePassword = NULL;
  1210. }
  1211. else
  1212. {
  1213. PSavePassword = &SavePassword;
  1214. SavePassword =
  1215. ((FEditingSessionData != NULL) &&
  1216. (FEditingSessionData->Password == FSessionData->Password)) ||
  1217. CustomWinConfiguration->UseMasterPassword;
  1218. }
  1219. AnsiString SessionName = FSessionData->SessionName;
  1220. if (DoSaveSessionDialog(SessionName, PSavePassword, FEditingSessionData))
  1221. {
  1222. if ((PSavePassword != NULL) && !*PSavePassword)
  1223. {
  1224. FSessionData->Password = "";
  1225. }
  1226. TSessionData * NewSession =
  1227. StoredSessions->NewSession(SessionName, FSessionData);
  1228. // modified only, explicit
  1229. StoredSessions->Save(false, true);
  1230. SaveConfiguration();
  1231. TTreeNode * Node = SessionTree->Items->GetFirstNode();
  1232. while ((Node != NULL) && (Node->Data != NewSession))
  1233. {
  1234. Node = Node->GetNext();
  1235. }
  1236. if (Node == NULL)
  1237. {
  1238. Node = AddSession(NewSession);
  1239. }
  1240. SessionTree->Selected = Node;
  1241. SessionData = NewSession;
  1242. ChangePage(SessionListSheet);
  1243. SessionTree->SetFocus();
  1244. }
  1245. }
  1246. //---------------------------------------------------------------------------
  1247. AnsiString __fastcall TLoginDialog::SessionNodePath(TTreeNode * Node)
  1248. {
  1249. AnsiString Path;
  1250. if (Node != NULL)
  1251. {
  1252. Path = Node->Text;
  1253. Node = Node->Parent;
  1254. while (Node != NULL)
  1255. {
  1256. Path.Insert(UnixIncludeTrailingBackslash(Node->Text), 1);
  1257. Node = Node->Parent;
  1258. }
  1259. }
  1260. return Path;
  1261. }
  1262. //---------------------------------------------------------------------------
  1263. void __fastcall TLoginDialog::DeleteSessionActionExecute(TObject * /*Sender*/)
  1264. {
  1265. assert(SessionTree->Selected != NULL);
  1266. TSessionData * Session = SelectedSession;
  1267. if (Session != NULL)
  1268. {
  1269. if (MessageDialog(FMTLOAD(CONFIRM_DELETE_SESSION, (Session->SessionName)),
  1270. qtConfirmation, qaOK | qaCancel, HELP_DELETE_SESSION) == qaOK)
  1271. {
  1272. Session->Remove();
  1273. DestroySession(Session);
  1274. SessionTree->Selected->Delete();
  1275. }
  1276. }
  1277. else
  1278. {
  1279. TTreeNode * Node = SessionTree->Selected;
  1280. int Sessions = 0;
  1281. TTreeNode * ANode = Node->GetNext();
  1282. while ((ANode != NULL) && ANode->HasAsParent(Node))
  1283. {
  1284. if (ANode->Data != NULL)
  1285. {
  1286. TSessionData * Session = static_cast<TSessionData *>(ANode->Data);
  1287. if (Session->Special)
  1288. {
  1289. SessionTree->Selected = ANode;
  1290. ANode->MakeVisible();
  1291. throw Exception(FMTLOAD(LOGIN_DELETE_SPECIAL_SESSION, (Session->SessionName)));
  1292. }
  1293. Sessions++;
  1294. }
  1295. ANode = ANode->GetNext();
  1296. }
  1297. AnsiString Path = SessionNodePath(Node);
  1298. if ((Sessions == 0) ||
  1299. (MessageDialog(FMTLOAD(LOGIN_DELETE_SESSION_FOLDER, (Path, Sessions)),
  1300. qtConfirmation, qaOK | qaCancel, HELP_DELETE_SESSION_FOLDER) == qaOK))
  1301. {
  1302. Node = SessionTree->Selected;
  1303. TTreeNode * ANode = Node->GetNext();
  1304. while ((ANode != NULL) && ANode->HasAsParent(Node))
  1305. {
  1306. if (ANode->Data != NULL)
  1307. {
  1308. TSessionData * Session = static_cast<TSessionData *>(ANode->Data);
  1309. Session->Remove();
  1310. DestroySession(Session);
  1311. }
  1312. ANode = ANode->GetNext();
  1313. }
  1314. SessionTree->Selected->Delete();
  1315. }
  1316. }
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. void __fastcall TLoginDialog::ImportSessionsActionExecute(TObject * /*Sender*/)
  1320. {
  1321. if (DoImportSessionsDialog(StoredSessions))
  1322. {
  1323. LoadSessions();
  1324. if (SessionTree->Items->Count > 0)
  1325. {
  1326. SessionTree->Items->GetFirstNode()->MakeVisible();
  1327. }
  1328. }
  1329. }
  1330. //---------------------------------------------------------------------------
  1331. void __fastcall TLoginDialog::CleanUpActionExecute(TObject * /*Sender*/)
  1332. {
  1333. if (DoCleanupDialog(StoredSessions, Configuration))
  1334. LoadSessions();
  1335. }
  1336. //---------------------------------------------------------------------------
  1337. void __fastcall TLoginDialog::AboutActionExecute(TObject * /*Sender*/)
  1338. {
  1339. DoAboutDialog(Configuration);
  1340. }
  1341. //---------------------------------------------------------------------------
  1342. void __fastcall TLoginDialog::ActionListUpdate(TBasicAction *Action,
  1343. bool &Handled)
  1344. {
  1345. bool SessionSelected =
  1346. (SessionTree->Selected != NULL) && (SessionTree->Selected->Data != NULL);
  1347. if (Action == EditSessionAction)
  1348. {
  1349. EditSessionAction->Enabled = SessionSelected;
  1350. }
  1351. else if (Action == DeleteSessionAction)
  1352. {
  1353. TSessionData * Data = SessionData;
  1354. DeleteSessionAction->Enabled =
  1355. (SessionSelected && !Data->Special) ||
  1356. ((SessionTree->Selected != NULL) && (SessionTree->Selected->Data == NULL));
  1357. }
  1358. else if (Action == RenameSessionAction)
  1359. {
  1360. TSessionData * Data = SessionData;
  1361. RenameSessionAction->Enabled =
  1362. (SessionSelected && !Data->Special) ||
  1363. ((SessionTree->Selected != NULL) && (SessionTree->Selected->Data == NULL));
  1364. }
  1365. else if (Action == DesktopIconAction)
  1366. {
  1367. DesktopIconAction->Enabled = SessionSelected;
  1368. }
  1369. else if (Action == SendToHookAction)
  1370. {
  1371. SendToHookAction->Enabled = SessionSelected;
  1372. }
  1373. else if (Action == LoginAction)
  1374. {
  1375. TSessionData * Data = SessionData;
  1376. LoginAction->Enabled = Data && Data->CanLogin;
  1377. }
  1378. else if (Action == SaveSessionAction)
  1379. {
  1380. SaveSessionAction->Enabled = (PageControl->ActivePage != SessionListSheet);
  1381. }
  1382. else if (Action == ShellIconSessionAction)
  1383. {
  1384. ShellIconSessionAction->Enabled = SessionSelected;
  1385. }
  1386. else if (Action == NewSessionFolderAction)
  1387. {
  1388. NewSessionFolderAction->Enabled = true;
  1389. }
  1390. Handled = true;
  1391. if (!SessionTree->IsEditing())
  1392. {
  1393. // default property setter does not have guard for "the same value"
  1394. if (!LoginButton->Default)
  1395. {
  1396. LoginButton->Default = true;
  1397. }
  1398. CloseButton->Cancel = true;
  1399. }
  1400. }
  1401. //---------------------------------------------------------------------------
  1402. bool __fastcall TLoginDialog::Execute()
  1403. {
  1404. LoadConfiguration();
  1405. bool Result = (ShowModal() == mrOk);
  1406. if (Result)
  1407. {
  1408. SaveConfiguration();
  1409. }
  1410. return Result;
  1411. }
  1412. //---------------------------------------------------------------------------
  1413. void __fastcall TLoginDialog::SaveConfiguration()
  1414. {
  1415. assert(CustomWinConfiguration);
  1416. CustomWinConfiguration->BeginUpdate();
  1417. try
  1418. {
  1419. LoggingFrame->SaveConfiguration();
  1420. GeneralSettingsFrame->SaveConfiguration();
  1421. CustomWinConfiguration->ShowAdvancedLoginOptions = ShowAdvancedLoginOptionsCheck->Checked;
  1422. }
  1423. __finally
  1424. {
  1425. CustomWinConfiguration->EndUpdate();
  1426. }
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. void __fastcall TLoginDialog::LoadConfiguration()
  1430. {
  1431. assert(CustomWinConfiguration);
  1432. LoggingFrame->LoadConfiguration();
  1433. GeneralSettingsFrame->LoadConfiguration();
  1434. ShowAdvancedLoginOptionsCheck->Checked = CustomWinConfiguration->ShowAdvancedLoginOptions;
  1435. UpdateControls();
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. void __fastcall TLoginDialog::PreferencesButtonClick(TObject * /*Sender*/)
  1439. {
  1440. ShowPreferencesDialog();
  1441. UpdateControls();
  1442. }
  1443. //---------------------------------------------------------------------------
  1444. void __fastcall TLoginDialog::MasterPasswordRecrypt(TObject * /*Sender*/)
  1445. {
  1446. FSessionData->RecryptPasswords();
  1447. }
  1448. //---------------------------------------------------------------------------
  1449. void __fastcall TLoginDialog::ShowPreferencesDialog()
  1450. {
  1451. assert(CustomWinConfiguration->OnMasterPasswordRecrypt == NULL);
  1452. CustomWinConfiguration->OnMasterPasswordRecrypt = MasterPasswordRecrypt;
  1453. try
  1454. {
  1455. DoPreferencesDialog(pmLogin);
  1456. }
  1457. __finally
  1458. {
  1459. assert(CustomWinConfiguration->OnMasterPasswordRecrypt == MasterPasswordRecrypt);
  1460. CustomWinConfiguration->OnMasterPasswordRecrypt = NULL;
  1461. }
  1462. }
  1463. //---------------------------------------------------------------------------
  1464. void __fastcall TLoginDialog::NewSessionActionExecute(TObject * /*Sender*/)
  1465. {
  1466. Default();
  1467. EditSession();
  1468. }
  1469. //---------------------------------------------------------------------------
  1470. void __fastcall TLoginDialog::NavigationTreeChange(TObject * /*Sender*/,
  1471. TTreeNode *Node)
  1472. {
  1473. if (NoUpdate == 0)
  1474. {
  1475. TTabSheet * Tab = reinterpret_cast<TTabSheet *>(Node->SelectedIndex);
  1476. // should happen only while loading language
  1477. // (UpdateNavigationTree may not be called yet)
  1478. if (Tab != NULL)
  1479. {
  1480. PageControl->ActivePage = Tab;
  1481. // reshow the accelerators, etc
  1482. ResetSystemSettings(this);
  1483. }
  1484. }
  1485. }
  1486. //---------------------------------------------------------------------------
  1487. void __fastcall TLoginDialog::ChangePage(TTabSheet * Tab)
  1488. {
  1489. PageControl->ActivePage = Tab;
  1490. PageControlChange(PageControl);
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. void __fastcall TLoginDialog::PageControlChange(TObject *Sender)
  1494. {
  1495. bool Found = false;
  1496. if (PageControl->ActivePage)
  1497. {
  1498. for (int Index = 0; Index < NavigationTree->Items->Count; Index++)
  1499. {
  1500. if (NavigationTree->Items->Item[Index]->SelectedIndex ==
  1501. reinterpret_cast<int>(PageControl->ActivePage))
  1502. {
  1503. NavigationTree->Items->Item[Index]->Selected = true;
  1504. Found = true;
  1505. }
  1506. }
  1507. }
  1508. if (!Found)
  1509. {
  1510. assert(false);
  1511. ChangePage(BasicSheet);
  1512. }
  1513. else
  1514. {
  1515. DataChange(Sender);
  1516. }
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. void __fastcall TLoginDialog::CMDialogKey(TWMKeyDown & Message)
  1520. {
  1521. if (Message.CharCode == VK_TAB)
  1522. {
  1523. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  1524. if (Shift.Contains(ssCtrl))
  1525. {
  1526. TTreeNode * Node = NavigationTree->Selected;
  1527. if (!Shift.Contains(ssShift))
  1528. {
  1529. Node = Node->GetNext();
  1530. if (!Node) Node = NavigationTree->Items->GetFirstNode();
  1531. }
  1532. else
  1533. {
  1534. if (Node->GetPrev()) Node = Node->GetPrev();
  1535. else
  1536. while (Node->GetNext()) Node = Node->GetNext();
  1537. }
  1538. Node->Selected = True;
  1539. Message.Result = 1;
  1540. return;
  1541. }
  1542. }
  1543. TForm::Dispatch(&Message);
  1544. }
  1545. //---------------------------------------------------------------------------
  1546. void __fastcall TLoginDialog::WMHelp(TWMHelp & Message)
  1547. {
  1548. assert(Message.HelpInfo != NULL);
  1549. if (Message.HelpInfo->iContextType == HELPINFO_WINDOW)
  1550. {
  1551. // invoke help for active page (not for whole form), regardless of focus
  1552. // (e.g. even if focus is on control outside pagecontrol)
  1553. Message.HelpInfo->hItemHandle = PageControl->ActivePage->Handle;
  1554. }
  1555. TForm::Dispatch(&Message);
  1556. }
  1557. //---------------------------------------------------------------------------
  1558. void __fastcall TLoginDialog::Dispatch(void * Message)
  1559. {
  1560. TMessage * M = reinterpret_cast<TMessage*>(Message);
  1561. assert(M);
  1562. if (M->Msg == CM_DIALOGKEY)
  1563. {
  1564. CMDialogKey(*((TWMKeyDown *)Message));
  1565. }
  1566. else if (M->Msg == WM_LOCALE_CHANGE)
  1567. {
  1568. if (M->WParam == 0)
  1569. {
  1570. SaveConfiguration();
  1571. SaveSession(FSessionData);
  1572. FSavedTab = PageControl->ActivePage;
  1573. FSavedSession = ((SessionTree->Selected != NULL) ?
  1574. SessionTree->Selected->AbsoluteIndex : -1);
  1575. assert(FSystemSettings);
  1576. RevokeSystemSettings(this, FSystemSettings);
  1577. FSystemSettings = NULL;
  1578. Hide();
  1579. }
  1580. else
  1581. {
  1582. FLocaleChanging = true;
  1583. try
  1584. {
  1585. Show();
  1586. }
  1587. __finally
  1588. {
  1589. FLocaleChanging = false;
  1590. }
  1591. }
  1592. }
  1593. else if (M->Msg == WM_HELP)
  1594. {
  1595. WMHelp(*((TWMHelp *)Message));
  1596. }
  1597. else
  1598. {
  1599. TForm::Dispatch(Message);
  1600. }
  1601. }
  1602. //---------------------------------------------------------------------------
  1603. void __fastcall TLoginDialog::AlgListBoxStartDrag(TObject * Sender,
  1604. TDragObject *& /*DragObject*/)
  1605. {
  1606. FAlgDragSource = dynamic_cast<TListBox*>(Sender)->ItemIndex;
  1607. FAlgDragDest = -1;
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. void __fastcall TLoginDialog::AlgListBoxDragOver(TObject * Sender,
  1611. TObject *Source, int X, int Y, TDragState /*State*/, bool &Accept)
  1612. {
  1613. if (Source == Sender)
  1614. {
  1615. Accept = AllowAlgDrag(dynamic_cast<TListBox*>(Sender), X, Y);
  1616. }
  1617. else
  1618. {
  1619. Accept = false;
  1620. }
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. void __fastcall TLoginDialog::AlgListBoxDragDrop(TObject * Sender,
  1624. TObject * Source, int X, int Y)
  1625. {
  1626. if (Source == Sender)
  1627. {
  1628. TListBox * AlgListBox = dynamic_cast<TListBox*>(Sender);
  1629. if (AllowAlgDrag(AlgListBox, X, Y))
  1630. {
  1631. AlgMove(AlgListBox, FAlgDragSource, FAlgDragDest);
  1632. }
  1633. }
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. void __fastcall TLoginDialog::CipherButtonClick(TObject *Sender)
  1637. {
  1638. AlgMove(CipherListBox, CipherListBox->ItemIndex,
  1639. CipherListBox->ItemIndex + (Sender == CipherUpButton ? -1 : 1));
  1640. }
  1641. //---------------------------------------------------------------------------
  1642. void __fastcall TLoginDialog::KexButtonClick(TObject *Sender)
  1643. {
  1644. AlgMove(KexListBox, KexListBox->ItemIndex,
  1645. KexListBox->ItemIndex + (Sender == KexUpButton ? -1 : 1));
  1646. }
  1647. //---------------------------------------------------------------------------
  1648. bool __fastcall TLoginDialog::AllowAlgDrag(TListBox * AlgListBox, int X, int Y)
  1649. {
  1650. FAlgDragDest = AlgListBox->ItemAtPos(TPoint(X, Y), true);
  1651. return (FAlgDragDest >= 0) && (FAlgDragDest != FAlgDragSource);
  1652. }
  1653. //---------------------------------------------------------------------------
  1654. void __fastcall TLoginDialog::AlgMove(TListBox * AlgListBox, int Source, int Dest)
  1655. {
  1656. if (Source >= 0 && Source < AlgListBox->Items->Count &&
  1657. Dest >= 0 && Dest < AlgListBox->Items->Count)
  1658. {
  1659. AlgListBox->Items->Move(Source, Dest);
  1660. AlgListBox->ItemIndex = Dest;
  1661. AlgListBox->SetFocus();
  1662. }
  1663. UpdateControls();
  1664. }
  1665. //---------------------------------------------------------------------------
  1666. void __fastcall TLoginDialog::SetDefaultSessionActionExecute(
  1667. TObject * /*Sender*/)
  1668. {
  1669. if (MessageDialog(LoadStr(SET_DEFAULT_SESSION_SETTINGS), qtConfirmation,
  1670. qaOK | qaCancel, HELP_SESSION_SAVE_DEFAULT) == qaOK)
  1671. {
  1672. SaveSession(FSessionData);
  1673. if (!Configuration->DisablePasswordStoring &&
  1674. FSessionData->HasAnyPassword() &&
  1675. CustomWinConfiguration->UseMasterPassword)
  1676. {
  1677. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  1678. }
  1679. StoredSessions->DefaultSettings = FSessionData;
  1680. }
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. void __fastcall TLoginDialog::ToolsMenuButtonClick(TObject * /*Sender*/)
  1684. {
  1685. MenuPopup(ToolsPopupMenu, ToolsMenuButton);
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. void __fastcall TLoginDialog::ShellIconSessionActionExecute(
  1689. TObject * /*Sender*/)
  1690. {
  1691. MenuPopup(IconsPopupMenu, ShellIconsButton);
  1692. }
  1693. //---------------------------------------------------------------------------
  1694. void __fastcall TLoginDialog::DesktopIconActionExecute(TObject * /*Sender*/)
  1695. {
  1696. if (MessageDialog(FMTLOAD(CONFIRM_CREATE_SHORTCUT, (SelectedSession->Name)),
  1697. qtConfirmation, qaYes | qaNo, HELP_CREATE_SHORTCUT) == qaYes)
  1698. {
  1699. assert(SelectedSession);
  1700. CreateDesktopShortCut(SelectedSession->LocalName, Application->ExeName,
  1701. FORMAT("\"%s\" /UploadIfAny", (SelectedSession->Name)),
  1702. FMTLOAD(SHORTCUT_INFO_TIP, (SelectedSession->Name, SelectedSession->InfoTip)));
  1703. }
  1704. }
  1705. //---------------------------------------------------------------------------
  1706. void __fastcall TLoginDialog::SendToHookActionExecute(TObject * /*Sender*/)
  1707. {
  1708. if (MessageDialog(FMTLOAD(CONFIRM_CREATE_SENDTO, (SelectedSession->Name)),
  1709. qtConfirmation, qaYes | qaNo, HELP_CREATE_SENDTO) == qaYes)
  1710. {
  1711. assert(SelectedSession);
  1712. CreateDesktopShortCut(FMTLOAD(SESSION_SENDTO_HOOK_NAME, (SelectedSession->LocalName)),
  1713. Application->ExeName,
  1714. FORMAT("\"%s\" /Upload", (SelectedSession->Name)), "",
  1715. CSIDL_SENDTO);
  1716. }
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. void __fastcall TLoginDialog::SessionTreeCustomDrawItem(
  1720. TCustomTreeView * Sender, TTreeNode * Node, TCustomDrawState State,
  1721. bool & DefaultDraw)
  1722. {
  1723. TSessionData * Data = (TSessionData *)Node->Data;
  1724. TFontStyles Styles = Sender->Canvas->Font->Style;
  1725. if ((Data != NULL) && Data->Special)
  1726. {
  1727. Styles = Styles << fsBold << fsUnderline;
  1728. }
  1729. else
  1730. {
  1731. if ((Data == NULL) && State.Empty() && !Node->DropTarget)
  1732. {
  1733. bool AnySession = false;
  1734. TTreeNode * ANode = Node->GetNext();
  1735. while (!AnySession && (ANode != NULL) && ANode->HasAsParent(Node))
  1736. {
  1737. AnySession = (ANode->Data != NULL);
  1738. ANode = ANode->GetNext();
  1739. }
  1740. if (!AnySession)
  1741. {
  1742. Sender->Canvas->Font->Color = clGrayText;
  1743. }
  1744. }
  1745. Styles = Styles >> fsBold >> fsUnderline;
  1746. }
  1747. Sender->Canvas->Font->Style = Styles;
  1748. DefaultDraw = true;
  1749. }
  1750. //---------------------------------------------------------------------------
  1751. void __fastcall TLoginDialog::CheckForUpdatesActionExecute(TObject * /*Sender*/)
  1752. {
  1753. CheckForUpdates(false);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TLoginDialog::SetOptions(int value)
  1757. {
  1758. if (Options != value)
  1759. {
  1760. FOptions = value;
  1761. UpdateControls();
  1762. }
  1763. }
  1764. //---------------------------------------------------------------------------
  1765. void __fastcall TLoginDialog::LanguagesButtonClick(TObject * /*Sender*/)
  1766. {
  1767. delete FLanguagesPopupMenu;
  1768. FLanguagesPopupMenu = new TPopupMenu(this);
  1769. TMenuItem * Item;
  1770. TStrings * Locales = GUIConfiguration->Locales;
  1771. for (int Index = 0; Index < Locales->Count; Index++)
  1772. {
  1773. Item = new TMenuItem(FLanguagesPopupMenu);
  1774. FLanguagesPopupMenu->Items->Add(Item);
  1775. Item->Caption = Locales->Strings[Index];
  1776. Item->Tag = reinterpret_cast<int>(Locales->Objects[Index]);
  1777. Item->OnClick = LocaleClick;
  1778. Item->Checked = (reinterpret_cast<LCID>(Locales->Objects[Index]) ==
  1779. GUIConfiguration->Locale);
  1780. }
  1781. Item = new TMenuItem(FLanguagesPopupMenu);
  1782. FLanguagesPopupMenu->Items->Add(Item);
  1783. Item->Caption = "-";
  1784. Item = new TMenuItem(FLanguagesPopupMenu);
  1785. FLanguagesPopupMenu->Items->Add(Item);
  1786. Item->Caption = LoadStr(LOGIN_GET_LOCALES);
  1787. Item->OnClick = LocaleGetClick;
  1788. MenuPopup(FLanguagesPopupMenu, LanguagesButton);
  1789. }
  1790. //---------------------------------------------------------------------------
  1791. void __fastcall TLoginDialog::LocaleClick(TObject * Sender)
  1792. {
  1793. assert(Sender);
  1794. GUIConfiguration->Locale =
  1795. static_cast<LCID>(dynamic_cast<TComponent*>(Sender)->Tag);
  1796. LanguagesButton->SetFocus();
  1797. }
  1798. //---------------------------------------------------------------------------
  1799. void __fastcall TLoginDialog::LocaleGetClick(TObject * /*Sender*/)
  1800. {
  1801. OpenBrowser(LoadStr(LOCALES_URL));
  1802. }
  1803. //---------------------------------------------------------------------------
  1804. void __fastcall TLoginDialog::AuthGSSAPICheck2Click(TObject * /*Sender*/)
  1805. {
  1806. if (!NoUpdate)
  1807. {
  1808. UpdateControls();
  1809. if (AuthGSSAPICheck2->Checked && !Configuration->GSSAPIInstalled)
  1810. {
  1811. throw Exception(LoadStr(GSSAPI_NOT_INSTALLED2));
  1812. }
  1813. }
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. void __fastcall TLoginDialog::HelpButtonClick(TObject * /*Sender*/)
  1817. {
  1818. FormHelp(this);
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. void __fastcall TLoginDialog::VerifyKey(AnsiString FileName, bool TypeOnly)
  1822. {
  1823. if (!FileName.Trim().IsEmpty())
  1824. {
  1825. FileName = ExpandEnvironmentVariables(FileName);
  1826. TKeyType Type = KeyType(FileName);
  1827. AnsiString Message;
  1828. switch (Type)
  1829. {
  1830. case ktOpenSSH:
  1831. Message = FMTLOAD(KEY_TYPE_UNSUPPORTED, (FileName, "OpenSSH SSH-2"));
  1832. break;
  1833. case ktSSHCom:
  1834. Message = FMTLOAD(KEY_TYPE_UNSUPPORTED, (FileName, "ssh.com SSH-2"));
  1835. break;
  1836. case ktSSH1:
  1837. case ktSSH2:
  1838. // on file select do not check for SSH version as user may
  1839. // intend to change it only after he/she selects key file
  1840. if (!TypeOnly)
  1841. {
  1842. TSessionData * Data = SessionData;
  1843. if ((Type == ktSSH1) !=
  1844. ((Data->SshProt == ssh1only) || (Data->SshProt == ssh1)))
  1845. {
  1846. Message = FMTLOAD(KEY_TYPE_DIFFERENT_SSH,
  1847. (FileName, (Type == ktSSH1 ? "SSH-1" : "PuTTY SSH-2")));
  1848. }
  1849. }
  1850. break;
  1851. default:
  1852. assert(false);
  1853. // fallthru
  1854. case ktUnopenable:
  1855. case ktUnknown:
  1856. Message = FMTLOAD(KEY_TYPE_UNKNOWN, (FileName));
  1857. break;
  1858. }
  1859. if (!Message.IsEmpty())
  1860. {
  1861. if (MessageDialog(Message, qtWarning, qaIgnore | qaAbort,
  1862. HELP_LOGIN_KEY_TYPE) == qaAbort)
  1863. {
  1864. Abort();
  1865. }
  1866. }
  1867. }
  1868. }
  1869. //---------------------------------------------------------------------------
  1870. void __fastcall TLoginDialog::PrivateKeyEditAfterDialog(TObject * Sender,
  1871. AnsiString & Name, bool & /*Action*/)
  1872. {
  1873. if (CompareFileName(Name, ExpandEnvironmentVariables(FBeforeDialogPath)))
  1874. {
  1875. Name = FBeforeDialogPath;
  1876. }
  1877. TFilenameEdit * Edit = dynamic_cast<TFilenameEdit *>(Sender);
  1878. if (Name != Edit->Text)
  1879. {
  1880. VerifyKey(Name, true);
  1881. }
  1882. }
  1883. //---------------------------------------------------------------------------
  1884. void __fastcall TLoginDialog::FormCloseQuery(TObject * /*Sender*/,
  1885. bool & /*CanClose*/)
  1886. {
  1887. if (ModalResult != mrCancel)
  1888. {
  1889. VerifyKey(SessionData->PublicKeyFile, false);
  1890. // for tunnel key do not check SSH version as it is not configurable
  1891. VerifyKey(SessionData->TunnelPublicKeyFile, true);
  1892. }
  1893. }
  1894. //---------------------------------------------------------------------------
  1895. void __fastcall TLoginDialog::ColorButtonClick(TObject * /*Sender*/)
  1896. {
  1897. ColorDefaultItem->Checked = (FColor == 0);
  1898. PickColorItem->Checked = (FColor != 0);
  1899. PickColorItem->ImageIndex = (FColor != 0 ? 0 : -1);
  1900. ColorImageList->BkColor = FColor;
  1901. MenuPopup(ColorPopupMenu, ColorButton);
  1902. }
  1903. //---------------------------------------------------------------------------
  1904. void __fastcall TLoginDialog::ColorDefaultItemClick(TObject * /*Sender*/)
  1905. {
  1906. FColor = (TColor)0;
  1907. }
  1908. //---------------------------------------------------------------------------
  1909. void __fastcall TLoginDialog::PickColorItemClick(TObject * /*Sender*/)
  1910. {
  1911. TColorDialog * Dialog = new TColorDialog(this);
  1912. try
  1913. {
  1914. Dialog->Options = Dialog->Options << cdFullOpen;
  1915. Dialog->Color = (FColor != 0 ? FColor : clSkyBlue);
  1916. if (Dialog->Execute())
  1917. {
  1918. FColor = Dialog->Color;
  1919. }
  1920. }
  1921. __finally
  1922. {
  1923. delete Dialog;
  1924. }
  1925. }
  1926. //---------------------------------------------------------------------------
  1927. void __fastcall TLoginDialog::SessionTreeEditing(TObject * /*Sender*/,
  1928. TTreeNode * Node, bool & AllowEdit)
  1929. {
  1930. TSessionData * Data = static_cast<TSessionData *>(Node->Data);
  1931. AllowEdit = (Data == NULL) || !Data->Special;
  1932. if (AllowEdit)
  1933. {
  1934. LoginButton->Default = false;
  1935. CloseButton->Cancel = false;
  1936. }
  1937. }
  1938. //---------------------------------------------------------------------------
  1939. void __fastcall TLoginDialog::RenameSessionActionExecute(TObject * /*Sender*/)
  1940. {
  1941. if (SessionTree->Selected != NULL)
  1942. {
  1943. SessionTree->Selected->EditText();
  1944. }
  1945. }
  1946. //---------------------------------------------------------------------------
  1947. void __fastcall TLoginDialog::CheckDuplicateFolder(TTreeNode * Parent,
  1948. AnsiString Text, TTreeNode * Node)
  1949. {
  1950. TTreeNode * ANode =
  1951. ((Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1952. Parent->getFirstChild());
  1953. // note that we allow folder with the same name as existing session
  1954. // on the same level (see also AddSession)
  1955. while ((ANode != NULL) &&
  1956. ((ANode == Node) || (ANode->Data != NULL) || !AnsiSameText(ANode->Text, Text)))
  1957. {
  1958. ANode = ANode->getNextSibling();
  1959. }
  1960. if (ANode != NULL)
  1961. {
  1962. throw Exception(FMTLOAD(LOGIN_DUPLICATE_SESSION_FOLDER, (Text)));
  1963. }
  1964. }
  1965. //---------------------------------------------------------------------------
  1966. void __fastcall TLoginDialog::SessionTreeEdited(TObject * /*Sender*/,
  1967. TTreeNode * Node, AnsiString & S)
  1968. {
  1969. if (Node->Text != S)
  1970. {
  1971. TSessionData * Session = SelectedSession;
  1972. TSessionData::ValidateName(S);
  1973. if (Session != NULL)
  1974. {
  1975. AnsiString Path = UnixExtractFilePath(Session->Name) + S;
  1976. SessionNameValidate(Path, Session);
  1977. // remove from storage
  1978. Session->Remove();
  1979. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  1980. // modified, only explicit
  1981. StoredSessions->Save(false, true);
  1982. // the session may be the same, if only letter case has changed
  1983. if (Session != NewSession)
  1984. {
  1985. // if we overwrite existing session, remove the original item
  1986. // (we must not delete the node we are editing)
  1987. TTreeNode * ANode =
  1988. ((Node->Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1989. Node->Parent->getFirstChild());
  1990. while ((ANode != NULL) && (ANode->Data != NewSession))
  1991. {
  1992. ANode = ANode->getNextSibling();
  1993. }
  1994. if (ANode != NULL)
  1995. {
  1996. ANode->Delete();
  1997. }
  1998. Node->Data = NewSession;
  1999. DestroySession(Session);
  2000. }
  2001. SessionData = NewSession;
  2002. }
  2003. else
  2004. {
  2005. CheckDuplicateFolder(Node->Parent, S, Node);
  2006. AnsiString ParentPath = UnixIncludeTrailingBackslash(SessionNodePath(Node->Parent));
  2007. AnsiString OldRoot = ParentPath + Node->Text;
  2008. AnsiString NewRoot = ParentPath + S;
  2009. bool AnySession = false;
  2010. TSortType PrevSortType = SessionTree->SortType;
  2011. // temporarily disable automatic sorting, so that nodes are kept in order
  2012. // while we traverse them. otherwise it may happen that we omit some.
  2013. SessionTree->SortType = Comctrls::stNone;
  2014. try
  2015. {
  2016. TTreeNode * ANode = Node->GetNext();
  2017. while ((ANode != NULL) && ANode->HasAsParent(Node))
  2018. {
  2019. if (ANode->Data != NULL)
  2020. {
  2021. AnySession = true;
  2022. TSessionData * Session = static_cast<TSessionData *>(ANode->Data);
  2023. // remove from storage
  2024. Session->Remove();
  2025. AnsiString Path = Session->Name;
  2026. assert(Path.SubString(1, OldRoot.Length()) == OldRoot);
  2027. Path.Delete(1, OldRoot.Length());
  2028. Path.Insert(NewRoot, 1);
  2029. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  2030. // the session may be the same, if only letter case has changed
  2031. if (NewSession != Session)
  2032. {
  2033. ANode->Data = NewSession;
  2034. DestroySession(Session);
  2035. }
  2036. }
  2037. ANode = ANode->GetNext();
  2038. }
  2039. }
  2040. __finally
  2041. {
  2042. SessionTree->SortType = PrevSortType;
  2043. }
  2044. if (AnySession)
  2045. {
  2046. // modified, only explicit
  2047. StoredSessions->Save(false, true);
  2048. }
  2049. }
  2050. }
  2051. }
  2052. //---------------------------------------------------------------------------
  2053. void __fastcall TLoginDialog::PathEditBeforeDialog(TObject * /*Sender*/,
  2054. AnsiString & Name, bool & /*Action*/)
  2055. {
  2056. FBeforeDialogPath = Name;
  2057. Name = ExpandEnvironmentVariables(Name);
  2058. }
  2059. //---------------------------------------------------------------------------
  2060. int __fastcall TLoginDialog::FSProtocolToIndex(TFSProtocol FSProtocol,
  2061. bool & AllowScpFallback)
  2062. {
  2063. if (FSProtocol == fsSFTP)
  2064. {
  2065. AllowScpFallback = true;
  2066. bool Dummy;
  2067. return FSProtocolToIndex(fsSFTPonly, Dummy);
  2068. }
  2069. else
  2070. {
  2071. AllowScpFallback = false;
  2072. for (int Index = 0; Index < TransferProtocolCombo->Items->Count; Index++)
  2073. {
  2074. if (FSOrder[Index] == FSProtocol)
  2075. {
  2076. return Index;
  2077. }
  2078. }
  2079. // SFTP is always present
  2080. return FSProtocolToIndex(fsSFTP, AllowScpFallback);
  2081. }
  2082. }
  2083. //---------------------------------------------------------------------------
  2084. TFSProtocol __fastcall TLoginDialog::IndexToFSProtocol(int Index, bool AllowScpFallback)
  2085. {
  2086. bool InBounds = (Index >= 0) && (Index < LENOF(FSOrder));
  2087. // can be temporary "unselected" while new language is being loaded
  2088. assert(InBounds || (Index == -1));
  2089. TFSProtocol Result = fsSFTP;
  2090. if (InBounds)
  2091. {
  2092. Result = FSOrder[Index];
  2093. if ((Result == fsSFTPonly) && AllowScpFallback)
  2094. {
  2095. Result = fsSFTP;
  2096. }
  2097. }
  2098. return Result;
  2099. }
  2100. //---------------------------------------------------------------------------
  2101. int __fastcall TLoginDialog::LastSupportedFtpProxyMethod()
  2102. {
  2103. return pmHTTP;
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. bool __fastcall TLoginDialog::SupportedFtpProxyMethod(int Method)
  2107. {
  2108. return (Method >= 0) && (Method <= LastSupportedFtpProxyMethod());
  2109. }
  2110. //---------------------------------------------------------------------------
  2111. TProxyMethod __fastcall TLoginDialog::GetProxyMethod()
  2112. {
  2113. TProxyMethod Result;
  2114. if (IndexToFSProtocol(TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked) != fsFTP)
  2115. {
  2116. Result = (TProxyMethod)SshProxyMethodCombo->ItemIndex;
  2117. }
  2118. else
  2119. {
  2120. if (SupportedFtpProxyMethod(FtpProxyMethodCombo->ItemIndex))
  2121. {
  2122. Result = (TProxyMethod)FtpProxyMethodCombo->ItemIndex;
  2123. }
  2124. else
  2125. {
  2126. Result = pmNone;
  2127. }
  2128. }
  2129. return Result;
  2130. }
  2131. //---------------------------------------------------------------------------
  2132. int __fastcall TLoginDialog::GetFtpProxyLogonType()
  2133. {
  2134. int Result;
  2135. if (IndexToFSProtocol(TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked) != fsFTP)
  2136. {
  2137. Result = 0;
  2138. }
  2139. else
  2140. {
  2141. if (SupportedFtpProxyMethod(FtpProxyMethodCombo->ItemIndex))
  2142. {
  2143. Result = 0;
  2144. }
  2145. else
  2146. {
  2147. Result = FtpProxyMethodCombo->ItemIndex - LastSupportedFtpProxyMethod();
  2148. }
  2149. }
  2150. return Result;
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. int __fastcall TLoginDialog::DefaultPort()
  2154. {
  2155. TFSProtocol FSProtocol = IndexToFSProtocol(
  2156. TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked);
  2157. int Result;
  2158. switch (FSProtocol)
  2159. {
  2160. case fsSCPonly:
  2161. case fsSFTP:
  2162. case fsSFTPonly:
  2163. Result = 22;
  2164. break;
  2165. case fsFTP:
  2166. if (FtpsCombo->ItemIndex == ftpsImplicit)
  2167. {
  2168. Result = 990;
  2169. }
  2170. else
  2171. {
  2172. Result = 21;
  2173. }
  2174. break;
  2175. default:
  2176. assert(false);
  2177. Result = -1;
  2178. break;
  2179. }
  2180. return Result;
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. void __fastcall TLoginDialog::TransferProtocolComboChange(TObject * Sender)
  2184. {
  2185. int ADefaultPort = DefaultPort();
  2186. if (!NoUpdate)
  2187. {
  2188. if (PortNumberEdit->AsInteger == FDefaultPort)
  2189. {
  2190. PortNumberEdit->AsInteger = ADefaultPort;
  2191. }
  2192. }
  2193. FDefaultPort = ADefaultPort;
  2194. DataChange(Sender);
  2195. }
  2196. //---------------------------------------------------------------------------
  2197. void __fastcall TLoginDialog::NavigationTreeCollapsing(
  2198. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & AllowCollapse)
  2199. {
  2200. AllowCollapse = false;
  2201. }
  2202. //---------------------------------------------------------------------------
  2203. void __fastcall TLoginDialog::RecycleBinLinkLabelClick(TObject * /*Sender*/)
  2204. {
  2205. FRecycleBinSheetVisible = true;
  2206. UpdateControls();
  2207. ChangePage(RecycleBinSheet);
  2208. }
  2209. //---------------------------------------------------------------------------
  2210. void __fastcall TLoginDialog::ProxyLocalCommandBrowseButtonClick(
  2211. TObject * /*Sender*/)
  2212. {
  2213. BrowseForExecutable(ProxyLocalCommandEdit,
  2214. LoadStr(LOGIN_SELECT_LOCAL_PROXY),
  2215. LoadStr(EXECUTABLE_FILTER), false, true);
  2216. }
  2217. //---------------------------------------------------------------------------
  2218. void __fastcall TLoginDialog::SessionTreeExpandedCollapsed(TObject * /*Sender*/,
  2219. TTreeNode * Node)
  2220. {
  2221. UpdateFolderNode(Node);
  2222. }
  2223. //---------------------------------------------------------------------------
  2224. void __fastcall TLoginDialog::SessionTreeCompare(TObject * /*Sender*/,
  2225. TTreeNode * Node1, TTreeNode * Node2, int /*Data*/, int & Compare)
  2226. {
  2227. if ((Node1->Data == NULL) && (Node2->Data != NULL))
  2228. {
  2229. Compare = -1;
  2230. }
  2231. else if ((Node1->Data != NULL) && (Node2->Data == NULL))
  2232. {
  2233. Compare = 1;
  2234. }
  2235. else if (Node1->Data == NULL)
  2236. {
  2237. Compare = AnsiCompareText(Node1->Text, Node2->Text);
  2238. }
  2239. else
  2240. {
  2241. Compare = NamedObjectSortProc(Node1->Data, Node2->Data);
  2242. }
  2243. }
  2244. //---------------------------------------------------------------------------
  2245. void __fastcall TLoginDialog::NewSessionFolderInputDialogInitialize(
  2246. TObject * /*Sender*/, TInputDialogData * Data)
  2247. {
  2248. TEdit * Edit = Data->Edit;
  2249. int P = Edit->Text.LastDelimiter("/");
  2250. if (P > 0)
  2251. {
  2252. Edit->SetFocus();
  2253. Edit->SelStart = P;
  2254. Edit->SelLength = Edit->Text.Length() - P;
  2255. }
  2256. }
  2257. //---------------------------------------------------------------------------
  2258. TTreeNode * __fastcall TLoginDialog::SessionFolderNode(TTreeNode * Node)
  2259. {
  2260. TTreeNode * Parent;
  2261. if (Node == NULL)
  2262. {
  2263. Parent = NULL;
  2264. }
  2265. else if (Node->Data != NULL)
  2266. {
  2267. Parent = Node->Parent;
  2268. }
  2269. else
  2270. {
  2271. Parent = Node;
  2272. }
  2273. return Parent;
  2274. }
  2275. //---------------------------------------------------------------------------
  2276. TTreeNode * __fastcall TLoginDialog::CurrentSessionFolderNode()
  2277. {
  2278. return SessionFolderNode(SessionTree->Selected);
  2279. }
  2280. //---------------------------------------------------------------------------
  2281. void __fastcall TLoginDialog::NewSessionFolderActionExecute(
  2282. TObject * /*Sender*/)
  2283. {
  2284. AnsiString Name =
  2285. UnixIncludeTrailingBackslash(SessionNodePath(CurrentSessionFolderNode())) +
  2286. LoadStr(NEW_FOLDER);
  2287. if (InputDialog(LoadStr(LOGIN_NEW_SESSION_FOLDER_CAPTION),
  2288. LoadStr(LOGIN_NEW_SESSION_FOLDER_PROMPT), Name, HELP_NEW_SESSION_FOLDER,
  2289. NULL, true, NewSessionFolderInputDialogInitialize))
  2290. {
  2291. Name = UnixExcludeTrailingBackslash(Name);
  2292. if (!Name.IsEmpty())
  2293. {
  2294. TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Name));
  2295. CheckDuplicateFolder(Parent, UnixExtractFileName(Name), NULL);
  2296. TTreeNode * Node = AddSessionPath(Name);
  2297. SessionTree->Selected = Node;
  2298. Node->MakeVisible();
  2299. }
  2300. }
  2301. }
  2302. //---------------------------------------------------------------------------
  2303. bool __fastcall TLoginDialog::SessionAllowDrop(TTreeNode * DropTarget)
  2304. {
  2305. assert(SessionTree->Selected != NULL);
  2306. return (SessionTree->Selected->Parent != DropTarget);
  2307. }
  2308. //---------------------------------------------------------------------------
  2309. void __fastcall TLoginDialog::SessionTreeProc(TMessage & AMessage)
  2310. {
  2311. if (AMessage.Msg == CM_DRAG)
  2312. {
  2313. TCMDrag & Message = reinterpret_cast<TCMDrag &>(AMessage);
  2314. // reimplement dmDragMove to avoid TCustomTreeView.DoDragOver,
  2315. // which resets DropTarget to pointed-to node
  2316. // (note that this disables OnDragOver event handler)
  2317. if ((Message.DragMessage == dmDragMove) ||
  2318. (Message.DragMessage == dmDragEnter) ||
  2319. (Message.DragMessage == dmDragLeave))
  2320. {
  2321. if (Message.DragMessage != dmDragMove)
  2322. {
  2323. // must call it at least for dmDragLeave, because it does some cleanup,
  2324. // but we need to override result below, as it defaults to "not accepted"
  2325. FOldSessionTreeProc(AMessage);
  2326. }
  2327. TDragControlObject * DragObject = dynamic_cast<TDragControlObject *>(Message.DragRec->Source);
  2328. if ((DragObject != NULL) && (DragObject->Control == SessionTree))
  2329. {
  2330. TPoint P = SessionTree->ScreenToClient(Message.DragRec->Pos);
  2331. TTreeNode * DropTarget = SessionFolderNode(SessionTree->GetNodeAt(P.x, P.y));
  2332. if (!SessionAllowDrop(DropTarget))
  2333. {
  2334. DropTarget = NULL;
  2335. Message.Result = 0;
  2336. }
  2337. else
  2338. {
  2339. Message.Result = 1;
  2340. }
  2341. if (Message.DragMessage == dmDragMove)
  2342. {
  2343. SessionTree->DropTarget = DropTarget;
  2344. }
  2345. FScrollOnDragOver->DragOver(P);
  2346. }
  2347. else
  2348. {
  2349. Message.Result = 0;
  2350. }
  2351. }
  2352. else
  2353. {
  2354. FOldSessionTreeProc(AMessage);
  2355. }
  2356. }
  2357. else
  2358. {
  2359. FOldSessionTreeProc(AMessage);
  2360. }
  2361. }
  2362. //---------------------------------------------------------------------------
  2363. void __fastcall TLoginDialog::SessionTreeStartDrag(TObject * /*Sender*/,
  2364. TDragObject *& /*DragObject*/)
  2365. {
  2366. assert(SessionTree->Selected != NULL);
  2367. // neither session folders not special sessions can be dragged
  2368. if ((SessionTree->Selected == NULL) ||
  2369. (SessionTree->Selected->Data == NULL) ||
  2370. ((SessionTree->Selected->Data != NULL) &&
  2371. SelectedSession->Special))
  2372. {
  2373. Abort();
  2374. }
  2375. FScrollOnDragOver->StartDrag();
  2376. }
  2377. //---------------------------------------------------------------------------
  2378. void __fastcall TLoginDialog::SessionTreeDragDrop(TObject * Sender,
  2379. TObject * Source, int /*X*/, int /*Y*/)
  2380. {
  2381. if ((Sender == Source) &&
  2382. (SessionTree->Selected != NULL) &&
  2383. (SessionTree->Selected->Parent != SessionTree->DropTarget))
  2384. {
  2385. TTreeNode * DropTarget = SessionTree->DropTarget;
  2386. TSessionData * Session = SelectedSession;
  2387. AnsiString Path =
  2388. UnixIncludeTrailingBackslash(SessionNodePath(DropTarget)) +
  2389. UnixExtractFileName(Session->SessionName);
  2390. SessionNameValidate(Path, Session);
  2391. // remove from storage
  2392. Session->Remove();
  2393. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  2394. // modified, only explicit
  2395. StoredSessions->Save(false, true);
  2396. // this should aways be the case
  2397. if (Session != NewSession)
  2398. {
  2399. TTreeNode * Node = SessionTree->Selected;
  2400. // look for overwritten node (if any)
  2401. TTreeNode * ANode = SessionTree->Items->GetFirstNode();
  2402. while (ANode != NULL)
  2403. {
  2404. if (ANode->Data == NewSession)
  2405. {
  2406. ANode->Delete();
  2407. break;
  2408. }
  2409. ANode = ANode->GetNext();
  2410. }
  2411. Node->MoveTo(DropTarget, naAddChild);
  2412. Node->Data = NewSession;
  2413. // try to make both visible
  2414. if (DropTarget != NULL)
  2415. {
  2416. DropTarget->MakeVisible();
  2417. }
  2418. Node->MakeVisible();
  2419. DestroySession(Session);
  2420. }
  2421. else
  2422. {
  2423. assert(false);
  2424. }
  2425. SessionData = NewSession;
  2426. }
  2427. else
  2428. {
  2429. assert(false);
  2430. }
  2431. }
  2432. //---------------------------------------------------------------------------
  2433. void __fastcall TLoginDialog::SessionTreeMouseMove(TObject * /*Sender*/,
  2434. TShiftState /*Shift*/, int X, int Y)
  2435. {
  2436. TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
  2437. THitTests HitTest = SessionTree->GetHitTestInfoAt(X, Y);
  2438. if (Node != FHintNode)
  2439. {
  2440. Application->CancelHint();
  2441. AnsiString Hint;
  2442. if (HitTest.Contains(htOnItem) || HitTest.Contains(htOnIcon) ||
  2443. HitTest.Contains(htOnLabel) || HitTest.Contains(htOnStateIcon))
  2444. {
  2445. FHintNode = Node;
  2446. if (Node->Data != NULL)
  2447. {
  2448. Hint = static_cast<TSessionData *>(Node->Data)->InfoTip;
  2449. }
  2450. else
  2451. {
  2452. Hint = "";
  2453. }
  2454. }
  2455. else
  2456. {
  2457. FHintNode = NULL;
  2458. Hint = "";
  2459. }
  2460. SessionTree->Hint = Hint;
  2461. }
  2462. }
  2463. //---------------------------------------------------------------------------
  2464. void __fastcall TLoginDialog::SessionTreeEndDrag(TObject * /*Sender*/,
  2465. TObject * /*Target*/, int /*X*/, int /*Y*/)
  2466. {
  2467. FScrollOnDragOver->EndDrag();
  2468. }
  2469. //---------------------------------------------------------------------------