Login.cpp 85 KB

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