Login.cpp 86 KB

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