Login.cpp 85 KB

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