Login.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  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. TSessionData * Data = SessionData;
  1029. bool CanLogin = (Data != NULL) && Data->CanLogin;
  1030. bool SessionSelected =
  1031. (SessionTree->Selected != NULL) && (SessionTree->Selected->Data != NULL);
  1032. LoginButton->Default = CanLogin || !SessionSelected;
  1033. LoadButton->Default = !LoginButton->Default;
  1034. AboutButton->Visible = (Options & loAbout);
  1035. LanguagesButton->Visible = (Options & loLanguage);
  1036. ShellIconsButton->Visible = (Options & loTools);
  1037. ToolsMenuButton->Visible = (Options & loTools);
  1038. LoggingFrame->EnableLogWindow = (Options & loLogWindow);
  1039. ColorButton->Visible = (Options & loColor);
  1040. UpdateNavigationTree();
  1041. }
  1042. __finally
  1043. {
  1044. NoUpdate--;
  1045. }
  1046. }
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. void __fastcall TLoginDialog::DataChange(TObject * /*Sender*/)
  1050. {
  1051. if (!NoUpdate) UpdateControls();
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. void __fastcall TLoginDialog::FormShow(TObject * /*Sender*/)
  1055. {
  1056. if (!FInitialized)
  1057. {
  1058. Init();
  1059. TSessionData * Data = GetSessionData();
  1060. if (Data == FSessionData)
  1061. {
  1062. LoadSession(Data);
  1063. }
  1064. else
  1065. {
  1066. Default();
  1067. }
  1068. }
  1069. if (FLocaleChanging)
  1070. {
  1071. Init();
  1072. LoadSession(FSessionData);
  1073. ChangePage(FSavedTab);
  1074. if (FSavedSession >= 0)
  1075. {
  1076. SessionTree->Selected = SessionTree->Items->Item[FSavedSession];
  1077. SessionTree->Selected->MakeVisible();
  1078. }
  1079. LoadConfiguration();
  1080. }
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. void __fastcall TLoginDialog::SessionTreeChange(TObject * /*Sender*/,
  1084. TTreeNode * /*Node*/)
  1085. {
  1086. UpdateControls();
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __fastcall TLoginDialog::SetSessionData(TSessionData * value)
  1090. {
  1091. FSessionData->Assign(value);
  1092. FSessionData->Special = false;
  1093. FEditingSessionData = value;
  1094. LoadSession(FSessionData);
  1095. }
  1096. //---------------------------------------------------------------------------
  1097. TSessionData * __fastcall TLoginDialog::GetSessionData()
  1098. {
  1099. if (PageControl->ActivePage == SessionListSheet)
  1100. {
  1101. return SelectedSession;
  1102. }
  1103. else
  1104. {
  1105. SaveSession(FSessionData);
  1106. return FSessionData;
  1107. }
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. void __fastcall TLoginDialog::SetStoredSessions(TStoredSessionList * value)
  1111. {
  1112. if (FStoredSessions != value)
  1113. {
  1114. FStoredSessions = value;
  1115. LoadSessions();
  1116. }
  1117. }
  1118. //---------------------------------------------------------------------------
  1119. void __fastcall TLoginDialog::SessionTreeDblClick(TObject * /*Sender*/)
  1120. {
  1121. TPoint P = SessionTree->ScreenToClient(Mouse->CursorPos);
  1122. if (SelectedSession && (SessionTree->GetNodeAt(P.x, P.y) == SessionTree->Selected))
  1123. {
  1124. if (SelectedSession->CanLogin)
  1125. {
  1126. ModalResult = mrOk;
  1127. }
  1128. else
  1129. {
  1130. SessionData = SelectedSession;
  1131. EditSession();
  1132. }
  1133. }
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. TSessionData * __fastcall TLoginDialog::GetSelectedSession()
  1137. {
  1138. if (SessionTree->Selected != NULL)
  1139. {
  1140. return (TSessionData *)SessionTree->Selected->Data;
  1141. }
  1142. else
  1143. {
  1144. return NULL;
  1145. }
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. void __fastcall TLoginDialog::SessionTreeKeyDown(TObject * /*Sender*/,
  1149. WORD & Key, TShiftState /*Shift*/)
  1150. {
  1151. if (!SessionTree->IsEditing())
  1152. {
  1153. if (Key == VK_DELETE)
  1154. {
  1155. DeleteSessionAction->Execute();
  1156. Key = 0;
  1157. }
  1158. else if (Key == VK_F2)
  1159. {
  1160. RenameSessionAction->Execute();
  1161. Key = 0;
  1162. }
  1163. }
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TLoginDialog::EditSession()
  1167. {
  1168. ChangePage(BasicSheet);
  1169. HostNameEdit->SetFocus();
  1170. }
  1171. //---------------------------------------------------------------------------
  1172. void __fastcall TLoginDialog::EditSessionActionExecute(TObject * /*Sender*/)
  1173. {
  1174. if (SelectedSession)
  1175. {
  1176. SessionData = SelectedSession;
  1177. EditSession();
  1178. }
  1179. }
  1180. //---------------------------------------------------------------------------
  1181. void __fastcall TLoginDialog::SaveSessionActionExecute(TObject * /*Sender*/)
  1182. {
  1183. SaveSession(FSessionData);
  1184. bool SavePassword;
  1185. bool * PSavePassword;
  1186. if (Configuration->DisablePasswordStoring ||
  1187. !FSessionData->HasAnyPassword())
  1188. {
  1189. PSavePassword = NULL;
  1190. }
  1191. else
  1192. {
  1193. PSavePassword = &SavePassword;
  1194. SavePassword =
  1195. ((FEditingSessionData != NULL) &&
  1196. (FEditingSessionData->Password == FSessionData->Password)) ||
  1197. CustomWinConfiguration->UseMasterPassword;
  1198. }
  1199. AnsiString SessionName = FSessionData->SessionName;
  1200. // when saving new session, save by default to current session folder
  1201. if (FCurrentSessionName.IsEmpty())
  1202. {
  1203. SessionName.Insert(
  1204. UnixIncludeTrailingBackslash(SessionNodePath(CurrentSessionFolderNode())), 1);
  1205. }
  1206. if (DoSaveSessionDialog(SessionName, PSavePassword, FEditingSessionData))
  1207. {
  1208. if ((PSavePassword != NULL) && !*PSavePassword)
  1209. {
  1210. FSessionData->Password = "";
  1211. }
  1212. TSessionData * NewSession =
  1213. StoredSessions->NewSession(SessionName, FSessionData);
  1214. // modified only, explicit
  1215. StoredSessions->Save(false, true);
  1216. SaveConfiguration();
  1217. TTreeNode * Node = SessionTree->Items->GetFirstNode();
  1218. while ((Node != NULL) && (Node->Data != NewSession))
  1219. {
  1220. Node = Node->GetNext();
  1221. }
  1222. if (Node == NULL)
  1223. {
  1224. Node = AddSession(NewSession);
  1225. }
  1226. SessionTree->Selected = Node;
  1227. SessionData = NewSession;
  1228. ChangePage(SessionListSheet);
  1229. SessionTree->SetFocus();
  1230. }
  1231. }
  1232. //---------------------------------------------------------------------------
  1233. AnsiString __fastcall TLoginDialog::SessionNodePath(TTreeNode * Node)
  1234. {
  1235. AnsiString Path;
  1236. if (Node != NULL)
  1237. {
  1238. Path = Node->Text;
  1239. Node = Node->Parent;
  1240. while (Node != NULL)
  1241. {
  1242. Path.Insert(UnixIncludeTrailingBackslash(Node->Text), 1);
  1243. Node = Node->Parent;
  1244. }
  1245. }
  1246. return Path;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TLoginDialog::DeleteSessionActionExecute(TObject * /*Sender*/)
  1250. {
  1251. assert(SessionTree->Selected != NULL);
  1252. TSessionData * Session = SelectedSession;
  1253. if (Session != NULL)
  1254. {
  1255. if (MessageDialog(FMTLOAD(CONFIRM_DELETE_SESSION, (Session->SessionName)),
  1256. qtConfirmation, qaOK | qaCancel, HELP_DELETE_SESSION) == qaOK)
  1257. {
  1258. Session->Remove();
  1259. DestroySession(Session);
  1260. SessionTree->Selected->Delete();
  1261. }
  1262. }
  1263. else
  1264. {
  1265. TTreeNode * Node = SessionTree->Selected;
  1266. int Sessions = 0;
  1267. TTreeNode * ANode = Node->GetNext();
  1268. while ((ANode != NULL) && ANode->HasAsParent(Node))
  1269. {
  1270. if (ANode->Data != NULL)
  1271. {
  1272. TSessionData * Session = static_cast<TSessionData *>(ANode->Data);
  1273. if (Session->Special)
  1274. {
  1275. SessionTree->Selected = ANode;
  1276. ANode->MakeVisible();
  1277. throw Exception(FMTLOAD(LOGIN_DELETE_SPECIAL_SESSION, (Session->SessionName)));
  1278. }
  1279. Sessions++;
  1280. }
  1281. ANode = ANode->GetNext();
  1282. }
  1283. AnsiString Path = SessionNodePath(Node);
  1284. if ((Sessions == 0) ||
  1285. (MessageDialog(FMTLOAD(LOGIN_DELETE_SESSION_FOLDER, (Path, Sessions)),
  1286. qtConfirmation, qaOK | qaCancel, HELP_DELETE_SESSION_FOLDER) == qaOK))
  1287. {
  1288. Node = SessionTree->Selected;
  1289. TTreeNode * ANode = Node->GetNext();
  1290. while ((ANode != NULL) && ANode->HasAsParent(Node))
  1291. {
  1292. if (ANode->Data != NULL)
  1293. {
  1294. TSessionData * Session = static_cast<TSessionData *>(ANode->Data);
  1295. Session->Remove();
  1296. DestroySession(Session);
  1297. }
  1298. ANode = ANode->GetNext();
  1299. }
  1300. SessionTree->Selected->Delete();
  1301. }
  1302. }
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TLoginDialog::ImportSessionsActionExecute(TObject * /*Sender*/)
  1306. {
  1307. if (DoImportSessionsDialog(StoredSessions))
  1308. {
  1309. LoadSessions();
  1310. if (SessionTree->Items->Count > 0)
  1311. {
  1312. SessionTree->Items->GetFirstNode()->MakeVisible();
  1313. }
  1314. }
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. void __fastcall TLoginDialog::CleanUpActionExecute(TObject * /*Sender*/)
  1318. {
  1319. if (DoCleanupDialog(StoredSessions, Configuration))
  1320. LoadSessions();
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TLoginDialog::AboutActionExecute(TObject * /*Sender*/)
  1324. {
  1325. DoAboutDialog(Configuration);
  1326. }
  1327. //---------------------------------------------------------------------------
  1328. void __fastcall TLoginDialog::ActionListUpdate(TBasicAction *Action,
  1329. bool &Handled)
  1330. {
  1331. bool SessionSelected =
  1332. (SessionTree->Selected != NULL) && (SessionTree->Selected->Data != NULL);
  1333. if (Action == EditSessionAction)
  1334. {
  1335. EditSessionAction->Enabled = SessionSelected;
  1336. }
  1337. else if (Action == DeleteSessionAction)
  1338. {
  1339. TSessionData * Data = SessionData;
  1340. DeleteSessionAction->Enabled =
  1341. (SessionSelected && !Data->Special) ||
  1342. ((SessionTree->Selected != NULL) && (SessionTree->Selected->Data == NULL));
  1343. }
  1344. else if (Action == RenameSessionAction)
  1345. {
  1346. TSessionData * Data = SessionData;
  1347. RenameSessionAction->Enabled =
  1348. (SessionSelected && !Data->Special) ||
  1349. ((SessionTree->Selected != NULL) && (SessionTree->Selected->Data == NULL));
  1350. }
  1351. else if (Action == DesktopIconAction)
  1352. {
  1353. DesktopIconAction->Enabled = SessionSelected;
  1354. }
  1355. else if (Action == SendToHookAction)
  1356. {
  1357. SendToHookAction->Enabled = SessionSelected;
  1358. }
  1359. else if (Action == LoginAction)
  1360. {
  1361. TSessionData * Data = SessionData;
  1362. LoginAction->Enabled = Data && Data->CanLogin;
  1363. }
  1364. else if (Action == SaveSessionAction)
  1365. {
  1366. SaveSessionAction->Enabled = (PageControl->ActivePage != SessionListSheet);
  1367. }
  1368. else if (Action == ShellIconSessionAction)
  1369. {
  1370. ShellIconSessionAction->Enabled = SessionSelected;
  1371. }
  1372. else if (Action == NewSessionFolderAction)
  1373. {
  1374. NewSessionFolderAction->Enabled = true;
  1375. }
  1376. Handled = true;
  1377. if (!SessionTree->IsEditing())
  1378. {
  1379. // default property setter does not have guard for "the same value"
  1380. if (!LoginButton->Default)
  1381. {
  1382. LoginButton->Default = true;
  1383. }
  1384. CloseButton->Cancel = true;
  1385. }
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. bool __fastcall TLoginDialog::Execute()
  1389. {
  1390. LoadConfiguration();
  1391. bool Result = (ShowModal() == mrOk);
  1392. if (Result)
  1393. {
  1394. SaveConfiguration();
  1395. }
  1396. return Result;
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. void __fastcall TLoginDialog::SaveConfiguration()
  1400. {
  1401. assert(CustomWinConfiguration);
  1402. CustomWinConfiguration->BeginUpdate();
  1403. try
  1404. {
  1405. LoggingFrame->SaveConfiguration();
  1406. GeneralSettingsFrame->SaveConfiguration();
  1407. CustomWinConfiguration->ShowAdvancedLoginOptions = ShowAdvancedLoginOptionsCheck->Checked;
  1408. }
  1409. __finally
  1410. {
  1411. CustomWinConfiguration->EndUpdate();
  1412. }
  1413. }
  1414. //---------------------------------------------------------------------------
  1415. void __fastcall TLoginDialog::LoadConfiguration()
  1416. {
  1417. assert(CustomWinConfiguration);
  1418. LoggingFrame->LoadConfiguration();
  1419. GeneralSettingsFrame->LoadConfiguration();
  1420. ShowAdvancedLoginOptionsCheck->Checked = CustomWinConfiguration->ShowAdvancedLoginOptions;
  1421. UpdateControls();
  1422. }
  1423. //---------------------------------------------------------------------------
  1424. void __fastcall TLoginDialog::PreferencesButtonClick(TObject * /*Sender*/)
  1425. {
  1426. ShowPreferencesDialog();
  1427. UpdateControls();
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. void __fastcall TLoginDialog::MasterPasswordRecrypt(TObject * /*Sender*/)
  1431. {
  1432. FSessionData->RecryptPasswords();
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TLoginDialog::ShowPreferencesDialog()
  1436. {
  1437. assert(CustomWinConfiguration->OnMasterPasswordRecrypt == NULL);
  1438. CustomWinConfiguration->OnMasterPasswordRecrypt = MasterPasswordRecrypt;
  1439. try
  1440. {
  1441. DoPreferencesDialog(pmLogin);
  1442. }
  1443. __finally
  1444. {
  1445. assert(CustomWinConfiguration->OnMasterPasswordRecrypt == MasterPasswordRecrypt);
  1446. CustomWinConfiguration->OnMasterPasswordRecrypt = NULL;
  1447. }
  1448. }
  1449. //---------------------------------------------------------------------------
  1450. void __fastcall TLoginDialog::NewSessionActionExecute(TObject * /*Sender*/)
  1451. {
  1452. Default();
  1453. EditSession();
  1454. }
  1455. //---------------------------------------------------------------------------
  1456. void __fastcall TLoginDialog::NavigationTreeChange(TObject * /*Sender*/,
  1457. TTreeNode *Node)
  1458. {
  1459. if (NoUpdate == 0)
  1460. {
  1461. TTabSheet * Tab = reinterpret_cast<TTabSheet *>(Node->SelectedIndex);
  1462. // should happen only while loading language
  1463. // (UpdateNavigationTree may not be called yet)
  1464. if (Tab != NULL)
  1465. {
  1466. PageControl->ActivePage = Tab;
  1467. // reshow the accelerators, etc
  1468. ResetSystemSettings(this);
  1469. }
  1470. }
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. void __fastcall TLoginDialog::ChangePage(TTabSheet * Tab)
  1474. {
  1475. PageControl->ActivePage = Tab;
  1476. PageControlChange(PageControl);
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. void __fastcall TLoginDialog::PageControlChange(TObject *Sender)
  1480. {
  1481. bool Found = false;
  1482. if (PageControl->ActivePage)
  1483. {
  1484. for (int Index = 0; Index < NavigationTree->Items->Count; Index++)
  1485. {
  1486. if (NavigationTree->Items->Item[Index]->SelectedIndex ==
  1487. reinterpret_cast<int>(PageControl->ActivePage))
  1488. {
  1489. NavigationTree->Items->Item[Index]->Selected = true;
  1490. Found = true;
  1491. }
  1492. }
  1493. }
  1494. if (!Found)
  1495. {
  1496. assert(false);
  1497. ChangePage(BasicSheet);
  1498. }
  1499. else
  1500. {
  1501. DataChange(Sender);
  1502. }
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. void __fastcall TLoginDialog::CMDialogKey(TWMKeyDown & Message)
  1506. {
  1507. if (Message.CharCode == VK_TAB)
  1508. {
  1509. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  1510. if (Shift.Contains(ssCtrl))
  1511. {
  1512. TTreeNode * Node = NavigationTree->Selected;
  1513. if (!Shift.Contains(ssShift))
  1514. {
  1515. Node = Node->GetNext();
  1516. if (!Node) Node = NavigationTree->Items->GetFirstNode();
  1517. }
  1518. else
  1519. {
  1520. if (Node->GetPrev()) Node = Node->GetPrev();
  1521. else
  1522. while (Node->GetNext()) Node = Node->GetNext();
  1523. }
  1524. Node->Selected = True;
  1525. Message.Result = 1;
  1526. return;
  1527. }
  1528. }
  1529. TForm::Dispatch(&Message);
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TLoginDialog::WMHelp(TWMHelp & Message)
  1533. {
  1534. assert(Message.HelpInfo != NULL);
  1535. if (Message.HelpInfo->iContextType == HELPINFO_WINDOW)
  1536. {
  1537. // invoke help for active page (not for whole form), regardless of focus
  1538. // (e.g. even if focus is on control outside pagecontrol)
  1539. Message.HelpInfo->hItemHandle = PageControl->ActivePage->Handle;
  1540. }
  1541. TForm::Dispatch(&Message);
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TLoginDialog::Dispatch(void * Message)
  1545. {
  1546. TMessage * M = reinterpret_cast<TMessage*>(Message);
  1547. assert(M);
  1548. if (M->Msg == CM_DIALOGKEY)
  1549. {
  1550. CMDialogKey(*((TWMKeyDown *)Message));
  1551. }
  1552. else if (M->Msg == WM_LOCALE_CHANGE)
  1553. {
  1554. if (M->WParam == 0)
  1555. {
  1556. SaveConfiguration();
  1557. SaveSession(FSessionData);
  1558. FSavedTab = PageControl->ActivePage;
  1559. FSavedSession = ((SessionTree->Selected != NULL) ?
  1560. SessionTree->Selected->AbsoluteIndex : -1);
  1561. assert(FSystemSettings);
  1562. RevokeSystemSettings(this, FSystemSettings);
  1563. FSystemSettings = NULL;
  1564. Hide();
  1565. }
  1566. else
  1567. {
  1568. FLocaleChanging = true;
  1569. try
  1570. {
  1571. Show();
  1572. }
  1573. __finally
  1574. {
  1575. FLocaleChanging = false;
  1576. }
  1577. }
  1578. }
  1579. else if (M->Msg == WM_HELP)
  1580. {
  1581. WMHelp(*((TWMHelp *)Message));
  1582. }
  1583. else
  1584. {
  1585. TForm::Dispatch(Message);
  1586. }
  1587. }
  1588. //---------------------------------------------------------------------------
  1589. void __fastcall TLoginDialog::AlgListBoxStartDrag(TObject * Sender,
  1590. TDragObject *& /*DragObject*/)
  1591. {
  1592. FAlgDragSource = dynamic_cast<TListBox*>(Sender)->ItemIndex;
  1593. FAlgDragDest = -1;
  1594. }
  1595. //---------------------------------------------------------------------------
  1596. void __fastcall TLoginDialog::AlgListBoxDragOver(TObject * Sender,
  1597. TObject *Source, int X, int Y, TDragState /*State*/, bool &Accept)
  1598. {
  1599. if (Source == Sender)
  1600. {
  1601. Accept = AllowAlgDrag(dynamic_cast<TListBox*>(Sender), X, Y);
  1602. }
  1603. else
  1604. {
  1605. Accept = false;
  1606. }
  1607. }
  1608. //---------------------------------------------------------------------------
  1609. void __fastcall TLoginDialog::AlgListBoxDragDrop(TObject * Sender,
  1610. TObject * Source, int X, int Y)
  1611. {
  1612. if (Source == Sender)
  1613. {
  1614. TListBox * AlgListBox = dynamic_cast<TListBox*>(Sender);
  1615. if (AllowAlgDrag(AlgListBox, X, Y))
  1616. {
  1617. AlgMove(AlgListBox, FAlgDragSource, FAlgDragDest);
  1618. }
  1619. }
  1620. }
  1621. //---------------------------------------------------------------------------
  1622. void __fastcall TLoginDialog::CipherButtonClick(TObject *Sender)
  1623. {
  1624. AlgMove(CipherListBox, CipherListBox->ItemIndex,
  1625. CipherListBox->ItemIndex + (Sender == CipherUpButton ? -1 : 1));
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. void __fastcall TLoginDialog::KexButtonClick(TObject *Sender)
  1629. {
  1630. AlgMove(KexListBox, KexListBox->ItemIndex,
  1631. KexListBox->ItemIndex + (Sender == KexUpButton ? -1 : 1));
  1632. }
  1633. //---------------------------------------------------------------------------
  1634. bool __fastcall TLoginDialog::AllowAlgDrag(TListBox * AlgListBox, int X, int Y)
  1635. {
  1636. FAlgDragDest = AlgListBox->ItemAtPos(TPoint(X, Y), true);
  1637. return (FAlgDragDest >= 0) && (FAlgDragDest != FAlgDragSource);
  1638. }
  1639. //---------------------------------------------------------------------------
  1640. void __fastcall TLoginDialog::AlgMove(TListBox * AlgListBox, int Source, int Dest)
  1641. {
  1642. if (Source >= 0 && Source < AlgListBox->Items->Count &&
  1643. Dest >= 0 && Dest < AlgListBox->Items->Count)
  1644. {
  1645. AlgListBox->Items->Move(Source, Dest);
  1646. AlgListBox->ItemIndex = Dest;
  1647. AlgListBox->SetFocus();
  1648. }
  1649. UpdateControls();
  1650. }
  1651. //---------------------------------------------------------------------------
  1652. void __fastcall TLoginDialog::SetDefaultSessionActionExecute(
  1653. TObject * /*Sender*/)
  1654. {
  1655. if (MessageDialog(LoadStr(SET_DEFAULT_SESSION_SETTINGS), qtConfirmation,
  1656. qaOK | qaCancel, HELP_SESSION_SAVE_DEFAULT) == qaOK)
  1657. {
  1658. SaveSession(FSessionData);
  1659. StoredSessions->DefaultSettings = FSessionData;
  1660. }
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. void __fastcall TLoginDialog::ToolsMenuButtonClick(TObject * /*Sender*/)
  1664. {
  1665. MenuPopup(ToolsPopupMenu, ToolsMenuButton);
  1666. }
  1667. //---------------------------------------------------------------------------
  1668. void __fastcall TLoginDialog::ShellIconSessionActionExecute(
  1669. TObject * /*Sender*/)
  1670. {
  1671. MenuPopup(IconsPopupMenu, ShellIconsButton);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. void __fastcall TLoginDialog::DesktopIconActionExecute(TObject * /*Sender*/)
  1675. {
  1676. if (MessageDialog(FMTLOAD(CONFIRM_CREATE_SHORTCUT, (SelectedSession->Name)),
  1677. qtConfirmation, qaYes | qaNo, HELP_CREATE_SHORTCUT) == qaYes)
  1678. {
  1679. assert(SelectedSession);
  1680. CreateDesktopShortCut(SelectedSession->Name, Application->ExeName,
  1681. FORMAT("\"%s\" /UploadIfAny", (SelectedSession->Name)),
  1682. FMTLOAD(SHORTCUT_INFO_TIP, (SelectedSession->Name, SelectedSession->InfoTip)));
  1683. }
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. void __fastcall TLoginDialog::SendToHookActionExecute(TObject * /*Sender*/)
  1687. {
  1688. if (MessageDialog(FMTLOAD(CONFIRM_CREATE_SENDTO, (SelectedSession->Name)),
  1689. qtConfirmation, qaYes | qaNo, HELP_CREATE_SENDTO) == qaYes)
  1690. {
  1691. assert(SelectedSession);
  1692. CreateDesktopShortCut(FMTLOAD(SESSION_SENDTO_HOOK_NAME, (SelectedSession->Name)),
  1693. Application->ExeName,
  1694. FORMAT("\"%s\" /Upload", (SelectedSession->Name)), "",
  1695. CSIDL_SENDTO);
  1696. }
  1697. }
  1698. //---------------------------------------------------------------------------
  1699. void __fastcall TLoginDialog::SessionTreeCustomDrawItem(
  1700. TCustomTreeView * Sender, TTreeNode * Node, TCustomDrawState State,
  1701. bool & DefaultDraw)
  1702. {
  1703. TSessionData * Data = (TSessionData *)Node->Data;
  1704. TFontStyles Styles = Sender->Canvas->Font->Style;
  1705. if ((Data != NULL) && Data->Special)
  1706. {
  1707. Styles = Styles << fsBold << fsUnderline;
  1708. }
  1709. else
  1710. {
  1711. if ((Data == NULL) && State.Empty() && !Node->DropTarget)
  1712. {
  1713. bool AnySession = false;
  1714. TTreeNode * ANode = Node->GetNext();
  1715. while (!AnySession && (ANode != NULL) && ANode->HasAsParent(Node))
  1716. {
  1717. AnySession = (ANode->Data != NULL);
  1718. ANode = ANode->GetNext();
  1719. }
  1720. if (!AnySession)
  1721. {
  1722. Sender->Canvas->Font->Color = clGrayText;
  1723. }
  1724. }
  1725. Styles = Styles >> fsBold >> fsUnderline;
  1726. }
  1727. Sender->Canvas->Font->Style = Styles;
  1728. DefaultDraw = true;
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. void __fastcall TLoginDialog::CheckForUpdatesActionExecute(TObject * /*Sender*/)
  1732. {
  1733. CheckForUpdates(false);
  1734. }
  1735. //---------------------------------------------------------------------------
  1736. void __fastcall TLoginDialog::SetOptions(int value)
  1737. {
  1738. if (Options != value)
  1739. {
  1740. FOptions = value;
  1741. UpdateControls();
  1742. }
  1743. }
  1744. //---------------------------------------------------------------------------
  1745. void __fastcall TLoginDialog::LanguagesButtonClick(TObject * /*Sender*/)
  1746. {
  1747. delete FLanguagesPopupMenu;
  1748. FLanguagesPopupMenu = new TPopupMenu(this);
  1749. TMenuItem * Item;
  1750. TStrings * Locales = GUIConfiguration->Locales;
  1751. for (int Index = 0; Index < Locales->Count; Index++)
  1752. {
  1753. Item = new TMenuItem(FLanguagesPopupMenu);
  1754. FLanguagesPopupMenu->Items->Add(Item);
  1755. Item->Caption = Locales->Strings[Index];
  1756. Item->Tag = reinterpret_cast<int>(Locales->Objects[Index]);
  1757. Item->OnClick = LocaleClick;
  1758. Item->Checked = (reinterpret_cast<LCID>(Locales->Objects[Index]) ==
  1759. GUIConfiguration->Locale);
  1760. }
  1761. Item = new TMenuItem(FLanguagesPopupMenu);
  1762. FLanguagesPopupMenu->Items->Add(Item);
  1763. Item->Caption = "-";
  1764. Item = new TMenuItem(FLanguagesPopupMenu);
  1765. FLanguagesPopupMenu->Items->Add(Item);
  1766. Item->Caption = LoadStr(LOGIN_GET_LOCALES);
  1767. Item->OnClick = LocaleGetClick;
  1768. MenuPopup(FLanguagesPopupMenu, LanguagesButton);
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. void __fastcall TLoginDialog::LocaleClick(TObject * Sender)
  1772. {
  1773. assert(Sender);
  1774. GUIConfiguration->Locale =
  1775. static_cast<LCID>(dynamic_cast<TComponent*>(Sender)->Tag);
  1776. LanguagesButton->SetFocus();
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. void __fastcall TLoginDialog::LocaleGetClick(TObject * /*Sender*/)
  1780. {
  1781. OpenBrowser(LoadStr(LOCALES_URL));
  1782. }
  1783. //---------------------------------------------------------------------------
  1784. void __fastcall TLoginDialog::AuthGSSAPICheck2Click(TObject * /*Sender*/)
  1785. {
  1786. if (!NoUpdate)
  1787. {
  1788. UpdateControls();
  1789. if (AuthGSSAPICheck2->Checked && !Configuration->GSSAPIInstalled)
  1790. {
  1791. throw Exception(LoadStr(GSSAPI_NOT_INSTALLED2));
  1792. }
  1793. }
  1794. }
  1795. //---------------------------------------------------------------------------
  1796. void __fastcall TLoginDialog::HelpButtonClick(TObject * /*Sender*/)
  1797. {
  1798. FormHelp(this);
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. void __fastcall TLoginDialog::VerifyKey(AnsiString FileName, bool TypeOnly)
  1802. {
  1803. if (!FileName.Trim().IsEmpty())
  1804. {
  1805. FileName = ExpandEnvironmentVariables(FileName);
  1806. TKeyType Type = KeyType(FileName);
  1807. AnsiString Message;
  1808. switch (Type)
  1809. {
  1810. case ktOpenSSH:
  1811. Message = FMTLOAD(KEY_TYPE_UNSUPPORTED, (FileName, "OpenSSH SSH-2"));
  1812. break;
  1813. case ktSSHCom:
  1814. Message = FMTLOAD(KEY_TYPE_UNSUPPORTED, (FileName, "ssh.com SSH-2"));
  1815. break;
  1816. case ktSSH1:
  1817. case ktSSH2:
  1818. // on file select do not check for SSH version as user may
  1819. // intend to change it only after he/she selects key file
  1820. if (!TypeOnly)
  1821. {
  1822. TSessionData * Data = SessionData;
  1823. if ((Type == ktSSH1) !=
  1824. ((Data->SshProt == ssh1only) || (Data->SshProt == ssh1)))
  1825. {
  1826. Message = FMTLOAD(KEY_TYPE_DIFFERENT_SSH,
  1827. (FileName, (Type == ktSSH1 ? "SSH-1" : "PuTTY SSH-2")));
  1828. }
  1829. }
  1830. break;
  1831. default:
  1832. assert(false);
  1833. // fallthru
  1834. case ktUnopenable:
  1835. case ktUnknown:
  1836. Message = FMTLOAD(KEY_TYPE_UNKNOWN, (FileName));
  1837. break;
  1838. }
  1839. if (!Message.IsEmpty())
  1840. {
  1841. if (MessageDialog(Message, qtWarning, qaIgnore | qaAbort,
  1842. HELP_LOGIN_KEY_TYPE) == qaAbort)
  1843. {
  1844. Abort();
  1845. }
  1846. }
  1847. }
  1848. }
  1849. //---------------------------------------------------------------------------
  1850. void __fastcall TLoginDialog::PrivateKeyEditAfterDialog(TObject * Sender,
  1851. AnsiString & Name, bool & /*Action*/)
  1852. {
  1853. if (CompareFileName(Name, ExpandEnvironmentVariables(FBeforeDialogPath)))
  1854. {
  1855. Name = FBeforeDialogPath;
  1856. }
  1857. TFilenameEdit * Edit = dynamic_cast<TFilenameEdit *>(Sender);
  1858. if (Name != Edit->Text)
  1859. {
  1860. VerifyKey(Name, true);
  1861. }
  1862. }
  1863. //---------------------------------------------------------------------------
  1864. void __fastcall TLoginDialog::FormCloseQuery(TObject * /*Sender*/,
  1865. bool & /*CanClose*/)
  1866. {
  1867. if (ModalResult != mrCancel)
  1868. {
  1869. VerifyKey(SessionData->PublicKeyFile, false);
  1870. // for tunnel key do not check SSH version as it is not configurable
  1871. VerifyKey(SessionData->TunnelPublicKeyFile, true);
  1872. }
  1873. }
  1874. //---------------------------------------------------------------------------
  1875. void __fastcall TLoginDialog::ColorButtonClick(TObject * /*Sender*/)
  1876. {
  1877. ColorDefaultItem->Checked = (FColor == 0);
  1878. PickColorItem->Checked = (FColor != 0);
  1879. PickColorItem->ImageIndex = (FColor != 0 ? 0 : -1);
  1880. ColorImageList->BkColor = FColor;
  1881. MenuPopup(ColorPopupMenu, ColorButton);
  1882. }
  1883. //---------------------------------------------------------------------------
  1884. void __fastcall TLoginDialog::ColorDefaultItemClick(TObject * /*Sender*/)
  1885. {
  1886. FColor = (TColor)0;
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. void __fastcall TLoginDialog::PickColorItemClick(TObject * /*Sender*/)
  1890. {
  1891. TColorDialog * Dialog = new TColorDialog(this);
  1892. try
  1893. {
  1894. Dialog->Options = Dialog->Options << cdFullOpen;
  1895. Dialog->Color = (FColor != 0 ? FColor : clSkyBlue);
  1896. if (Dialog->Execute())
  1897. {
  1898. FColor = Dialog->Color;
  1899. }
  1900. }
  1901. __finally
  1902. {
  1903. delete Dialog;
  1904. }
  1905. }
  1906. //---------------------------------------------------------------------------
  1907. void __fastcall TLoginDialog::SessionTreeEditing(TObject * /*Sender*/,
  1908. TTreeNode * Node, bool & AllowEdit)
  1909. {
  1910. TSessionData * Data = static_cast<TSessionData *>(Node->Data);
  1911. AllowEdit = (Data == NULL) || !Data->Special;
  1912. if (AllowEdit)
  1913. {
  1914. LoginButton->Default = false;
  1915. CloseButton->Cancel = false;
  1916. }
  1917. }
  1918. //---------------------------------------------------------------------------
  1919. void __fastcall TLoginDialog::RenameSessionActionExecute(TObject * /*Sender*/)
  1920. {
  1921. if (SessionTree->Selected != NULL)
  1922. {
  1923. SessionTree->Selected->EditText();
  1924. }
  1925. }
  1926. //---------------------------------------------------------------------------
  1927. void __fastcall TLoginDialog::CheckDuplicateFolder(TTreeNode * Parent,
  1928. AnsiString Text, TTreeNode * Node)
  1929. {
  1930. TTreeNode * ANode =
  1931. ((Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1932. Parent->getFirstChild());
  1933. // note that we allow folder with the same name as existing session
  1934. // on the same level (see also AddSession)
  1935. while ((ANode != NULL) &&
  1936. ((ANode == Node) || (ANode->Data != NULL) || !AnsiSameText(ANode->Text, Text)))
  1937. {
  1938. ANode = ANode->getNextSibling();
  1939. }
  1940. if (ANode != NULL)
  1941. {
  1942. throw Exception(FMTLOAD(LOGIN_DUPLICATE_SESSION_FOLDER, (Text)));
  1943. }
  1944. }
  1945. //---------------------------------------------------------------------------
  1946. void __fastcall TLoginDialog::SessionTreeEdited(TObject * /*Sender*/,
  1947. TTreeNode * Node, AnsiString & S)
  1948. {
  1949. if (Node->Text != S)
  1950. {
  1951. TSessionData * Session = SelectedSession;
  1952. TSessionData::ValidateName(S);
  1953. if (Session != NULL)
  1954. {
  1955. AnsiString Path = UnixExtractFilePath(Session->Name) + S;
  1956. SessionNameValidate(Path, Session);
  1957. // remove from storage
  1958. Session->Remove();
  1959. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  1960. // modified, only explicit
  1961. StoredSessions->Save(false, true);
  1962. // the session may be the same, if only letter case has changed
  1963. if (Session != NewSession)
  1964. {
  1965. // if we overwrite existing session, remove the original item
  1966. // (we must not delete the node we are editing)
  1967. TTreeNode * ANode =
  1968. ((Node->Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1969. Node->Parent->getFirstChild());
  1970. while ((ANode != NULL) && (ANode->Data != NewSession))
  1971. {
  1972. ANode = ANode->getNextSibling();
  1973. }
  1974. if (ANode != NULL)
  1975. {
  1976. ANode->Delete();
  1977. }
  1978. Node->Data = NewSession;
  1979. DestroySession(Session);
  1980. }
  1981. SessionData = NewSession;
  1982. }
  1983. else
  1984. {
  1985. CheckDuplicateFolder(Node->Parent, S, Node);
  1986. AnsiString ParentPath = UnixIncludeTrailingBackslash(SessionNodePath(Node->Parent));
  1987. AnsiString OldRoot = ParentPath + Node->Text;
  1988. AnsiString NewRoot = ParentPath + S;
  1989. bool AnySession = false;
  1990. TSortType PrevSortType = SessionTree->SortType;
  1991. // temporarily disable automatic sorting, so that nodes are kept in order
  1992. // while we traverse them. otherwise it may happen that we omit some.
  1993. SessionTree->SortType = Comctrls::stNone;
  1994. try
  1995. {
  1996. TTreeNode * ANode = Node->GetNext();
  1997. while ((ANode != NULL) && ANode->HasAsParent(Node))
  1998. {
  1999. if (ANode->Data != NULL)
  2000. {
  2001. AnySession = true;
  2002. TSessionData * Session = static_cast<TSessionData *>(ANode->Data);
  2003. // remove from storage
  2004. Session->Remove();
  2005. AnsiString Path = Session->Name;
  2006. assert(Path.SubString(1, OldRoot.Length()) == OldRoot);
  2007. Path.Delete(1, OldRoot.Length());
  2008. Path.Insert(NewRoot, 1);
  2009. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  2010. // the session may be the same, if only letter case has changed
  2011. if (NewSession != Session)
  2012. {
  2013. ANode->Data = NewSession;
  2014. DestroySession(Session);
  2015. }
  2016. }
  2017. ANode = ANode->GetNext();
  2018. }
  2019. }
  2020. __finally
  2021. {
  2022. SessionTree->SortType = PrevSortType;
  2023. }
  2024. if (AnySession)
  2025. {
  2026. // modified, only explicit
  2027. StoredSessions->Save(false, true);
  2028. }
  2029. }
  2030. }
  2031. }
  2032. //---------------------------------------------------------------------------
  2033. void __fastcall TLoginDialog::UnixEnvironmentButtonClick(TObject * /*Sender*/)
  2034. {
  2035. EOLTypeCombo->ItemIndex = 0;
  2036. DSTModeUnixCheck->Checked = true;
  2037. }
  2038. //---------------------------------------------------------------------------
  2039. void __fastcall TLoginDialog::WindowsEnvironmentButtonClick(
  2040. TObject * /*Sender*/)
  2041. {
  2042. EOLTypeCombo->ItemIndex = 1;
  2043. DSTModeWinCheck->Checked = true;
  2044. }
  2045. //---------------------------------------------------------------------------
  2046. void __fastcall TLoginDialog::PathEditBeforeDialog(TObject * /*Sender*/,
  2047. AnsiString & Name, bool & /*Action*/)
  2048. {
  2049. FBeforeDialogPath = Name;
  2050. Name = ExpandEnvironmentVariables(Name);
  2051. }
  2052. //---------------------------------------------------------------------------
  2053. int __fastcall TLoginDialog::FSProtocolToIndex(TFSProtocol FSProtocol,
  2054. bool & AllowScpFallback)
  2055. {
  2056. if (FSProtocol == fsSFTP)
  2057. {
  2058. AllowScpFallback = true;
  2059. bool Dummy;
  2060. return FSProtocolToIndex(fsSFTPonly, Dummy);
  2061. }
  2062. else
  2063. {
  2064. AllowScpFallback = false;
  2065. for (int Index = 0; Index < TransferProtocolCombo->Items->Count; Index++)
  2066. {
  2067. if (FSOrder[Index] == FSProtocol)
  2068. {
  2069. return Index;
  2070. }
  2071. }
  2072. // SFTP is always present
  2073. return FSProtocolToIndex(fsSFTP, AllowScpFallback);
  2074. }
  2075. }
  2076. //---------------------------------------------------------------------------
  2077. TFSProtocol __fastcall TLoginDialog::IndexToFSProtocol(int Index, bool AllowScpFallback)
  2078. {
  2079. bool InBounds = (Index >= 0) && (Index < LENOF(FSOrder));
  2080. // can be temporary "unselected" while new language is being loaded
  2081. assert(InBounds || (Index == -1));
  2082. TFSProtocol Result = fsSFTP;
  2083. if (InBounds)
  2084. {
  2085. Result = FSOrder[Index];
  2086. if ((Result == fsSFTPonly) && AllowScpFallback)
  2087. {
  2088. Result = fsSFTP;
  2089. }
  2090. }
  2091. return Result;
  2092. }
  2093. //---------------------------------------------------------------------------
  2094. int __fastcall TLoginDialog::LastSupportedFtpProxyMethod()
  2095. {
  2096. return pmHTTP;
  2097. }
  2098. //---------------------------------------------------------------------------
  2099. bool __fastcall TLoginDialog::SupportedFtpProxyMethod(int Method)
  2100. {
  2101. return (Method >= 0) && (Method <= LastSupportedFtpProxyMethod());
  2102. }
  2103. //---------------------------------------------------------------------------
  2104. TProxyMethod __fastcall TLoginDialog::GetProxyMethod()
  2105. {
  2106. TProxyMethod Result;
  2107. if (IndexToFSProtocol(TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked) != fsFTP)
  2108. {
  2109. Result = (TProxyMethod)SshProxyMethodCombo->ItemIndex;
  2110. }
  2111. else
  2112. {
  2113. if (SupportedFtpProxyMethod(FtpProxyMethodCombo->ItemIndex))
  2114. {
  2115. Result = (TProxyMethod)FtpProxyMethodCombo->ItemIndex;
  2116. }
  2117. else
  2118. {
  2119. Result = pmNone;
  2120. }
  2121. }
  2122. return Result;
  2123. }
  2124. //---------------------------------------------------------------------------
  2125. int __fastcall TLoginDialog::GetFtpProxyLogonType()
  2126. {
  2127. int Result;
  2128. if (IndexToFSProtocol(TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked) != fsFTP)
  2129. {
  2130. Result = 0;
  2131. }
  2132. else
  2133. {
  2134. if (SupportedFtpProxyMethod(FtpProxyMethodCombo->ItemIndex))
  2135. {
  2136. Result = 0;
  2137. }
  2138. else
  2139. {
  2140. Result = FtpProxyMethodCombo->ItemIndex - LastSupportedFtpProxyMethod();
  2141. }
  2142. }
  2143. return Result;
  2144. }
  2145. //---------------------------------------------------------------------------
  2146. int __fastcall TLoginDialog::DefaultPort()
  2147. {
  2148. TFSProtocol FSProtocol = IndexToFSProtocol(
  2149. TransferProtocolCombo->ItemIndex, AllowScpFallbackCheck->Checked);
  2150. int Result;
  2151. switch (FSProtocol)
  2152. {
  2153. case fsSCPonly:
  2154. case fsSFTP:
  2155. case fsSFTPonly:
  2156. Result = 22;
  2157. break;
  2158. case fsFTP:
  2159. if (FtpsCombo->ItemIndex == ftpsImplicit)
  2160. {
  2161. Result = 990;
  2162. }
  2163. else
  2164. {
  2165. Result = 21;
  2166. }
  2167. break;
  2168. default:
  2169. assert(false);
  2170. Result = -1;
  2171. break;
  2172. }
  2173. return Result;
  2174. }
  2175. //---------------------------------------------------------------------------
  2176. void __fastcall TLoginDialog::TransferProtocolComboChange(TObject * Sender)
  2177. {
  2178. int ADefaultPort = DefaultPort();
  2179. if (!NoUpdate)
  2180. {
  2181. if (PortNumberEdit->AsInteger == FDefaultPort)
  2182. {
  2183. PortNumberEdit->AsInteger = ADefaultPort;
  2184. }
  2185. }
  2186. FDefaultPort = ADefaultPort;
  2187. DataChange(Sender);
  2188. }
  2189. //---------------------------------------------------------------------------
  2190. void __fastcall TLoginDialog::NavigationTreeCollapsing(
  2191. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & AllowCollapse)
  2192. {
  2193. AllowCollapse = false;
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. void __fastcall TLoginDialog::RecycleBinLinkLabelClick(TObject * /*Sender*/)
  2197. {
  2198. FRecycleBinSheetVisible = true;
  2199. UpdateControls();
  2200. ChangePage(RecycleBinSheet);
  2201. }
  2202. //---------------------------------------------------------------------------
  2203. void __fastcall TLoginDialog::ProxyLocalCommandBrowseButtonClick(
  2204. TObject * /*Sender*/)
  2205. {
  2206. BrowseForExecutable(ProxyLocalCommandEdit,
  2207. LoadStr(LOGIN_SELECT_LOCAL_PROXY),
  2208. LoadStr(EXECUTABLE_FILTER), false, true);
  2209. }
  2210. //---------------------------------------------------------------------------
  2211. void __fastcall TLoginDialog::SessionTreeExpandedCollapsed(TObject * /*Sender*/,
  2212. TTreeNode * Node)
  2213. {
  2214. UpdateFolderNode(Node);
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TLoginDialog::SessionTreeCompare(TObject * /*Sender*/,
  2218. TTreeNode * Node1, TTreeNode * Node2, int /*Data*/, int & Compare)
  2219. {
  2220. if ((Node1->Data == NULL) && (Node2->Data != NULL))
  2221. {
  2222. Compare = -1;
  2223. }
  2224. else if ((Node1->Data != NULL) && (Node2->Data == NULL))
  2225. {
  2226. Compare = 1;
  2227. }
  2228. else if (Node1->Data == NULL)
  2229. {
  2230. Compare = AnsiCompareText(Node1->Text, Node2->Text);
  2231. }
  2232. else
  2233. {
  2234. Compare = NamedObjectSortProc(Node1->Data, Node2->Data);
  2235. }
  2236. }
  2237. //---------------------------------------------------------------------------
  2238. void __fastcall TLoginDialog::NewSessionFolderInputDialogInitialize(
  2239. TObject * /*Sender*/, TInputDialogData * Data)
  2240. {
  2241. TEdit * Edit = Data->Edit;
  2242. int P = Edit->Text.LastDelimiter("/");
  2243. if (P > 0)
  2244. {
  2245. Edit->SetFocus();
  2246. Edit->SelStart = P;
  2247. Edit->SelLength = Edit->Text.Length() - P;
  2248. }
  2249. }
  2250. //---------------------------------------------------------------------------
  2251. TTreeNode * __fastcall TLoginDialog::SessionFolderNode(TTreeNode * Node)
  2252. {
  2253. TTreeNode * Parent;
  2254. if (Node == NULL)
  2255. {
  2256. Parent = NULL;
  2257. }
  2258. else if (Node->Data != NULL)
  2259. {
  2260. Parent = Node->Parent;
  2261. }
  2262. else
  2263. {
  2264. Parent = Node;
  2265. }
  2266. return Parent;
  2267. }
  2268. //---------------------------------------------------------------------------
  2269. TTreeNode * __fastcall TLoginDialog::CurrentSessionFolderNode()
  2270. {
  2271. return SessionFolderNode(SessionTree->Selected);
  2272. }
  2273. //---------------------------------------------------------------------------
  2274. void __fastcall TLoginDialog::NewSessionFolderActionExecute(
  2275. TObject * /*Sender*/)
  2276. {
  2277. AnsiString Name =
  2278. UnixIncludeTrailingBackslash(SessionNodePath(CurrentSessionFolderNode())) +
  2279. LoadStr(NEW_FOLDER);
  2280. if (InputDialog(LoadStr(LOGIN_NEW_SESSION_FOLDER_CAPTION),
  2281. LoadStr(LOGIN_NEW_SESSION_FOLDER_PROMPT), Name, HELP_NEW_SESSION_FOLDER,
  2282. NULL, true, NewSessionFolderInputDialogInitialize))
  2283. {
  2284. Name = UnixExcludeTrailingBackslash(Name);
  2285. if (!Name.IsEmpty())
  2286. {
  2287. TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Name));
  2288. CheckDuplicateFolder(Parent, UnixExtractFileName(Name), NULL);
  2289. TTreeNode * Node = AddSessionPath(Name);
  2290. SessionTree->Selected = Node;
  2291. Node->MakeVisible();
  2292. }
  2293. }
  2294. }
  2295. //---------------------------------------------------------------------------
  2296. bool __fastcall TLoginDialog::SessionAllowDrop(TTreeNode * DropTarget)
  2297. {
  2298. assert(SessionTree->Selected != NULL);
  2299. return (SessionTree->Selected->Parent != DropTarget);
  2300. }
  2301. //---------------------------------------------------------------------------
  2302. void __fastcall TLoginDialog::SessionTreeProc(TMessage & AMessage)
  2303. {
  2304. if (AMessage.Msg == CM_DRAG)
  2305. {
  2306. TCMDrag & Message = reinterpret_cast<TCMDrag &>(AMessage);
  2307. // reimplement dmDragMove to avoid TCustomTreeView.DoDragOver,
  2308. // which resets DropTarget to pointed-to node
  2309. // (note that this disables OnDragOver event handler)
  2310. if ((Message.DragMessage == dmDragMove) ||
  2311. (Message.DragMessage == dmDragEnter) ||
  2312. (Message.DragMessage == dmDragLeave))
  2313. {
  2314. if (Message.DragMessage != dmDragMove)
  2315. {
  2316. // must call it at least for dmDragLeave, because it does some cleanup,
  2317. // but we need to override result below, as it defaults to "not accepted"
  2318. FOldSessionTreeProc(AMessage);
  2319. }
  2320. TDragControlObject * DragObject = dynamic_cast<TDragControlObject *>(Message.DragRec->Source);
  2321. if ((DragObject != NULL) && (DragObject->Control == SessionTree))
  2322. {
  2323. TPoint P = SessionTree->ScreenToClient(Message.DragRec->Pos);
  2324. TTreeNode * DropTarget = SessionFolderNode(SessionTree->GetNodeAt(P.x, P.y));
  2325. if (!SessionAllowDrop(DropTarget))
  2326. {
  2327. DropTarget = NULL;
  2328. Message.Result = 0;
  2329. }
  2330. else
  2331. {
  2332. Message.Result = 1;
  2333. }
  2334. if (Message.DragMessage == dmDragMove)
  2335. {
  2336. SessionTree->DropTarget = DropTarget;
  2337. }
  2338. FScrollOnDragOver->DragOver(P);
  2339. }
  2340. else
  2341. {
  2342. Message.Result = 0;
  2343. }
  2344. }
  2345. else
  2346. {
  2347. FOldSessionTreeProc(AMessage);
  2348. }
  2349. }
  2350. else
  2351. {
  2352. FOldSessionTreeProc(AMessage);
  2353. }
  2354. }
  2355. //---------------------------------------------------------------------------
  2356. void __fastcall TLoginDialog::SessionTreeStartDrag(TObject * /*Sender*/,
  2357. TDragObject *& /*DragObject*/)
  2358. {
  2359. assert(SessionTree->Selected != NULL);
  2360. // neither session folders not special sessions can be dragged
  2361. if ((SessionTree->Selected == NULL) ||
  2362. (SessionTree->Selected->Data == NULL) ||
  2363. ((SessionTree->Selected->Data != NULL) &&
  2364. SelectedSession->Special))
  2365. {
  2366. Abort();
  2367. }
  2368. FScrollOnDragOver->StartDrag();
  2369. }
  2370. //---------------------------------------------------------------------------
  2371. void __fastcall TLoginDialog::SessionTreeDragDrop(TObject * Sender,
  2372. TObject * Source, int /*X*/, int /*Y*/)
  2373. {
  2374. if ((Sender == Source) &&
  2375. (SessionTree->Selected != NULL) &&
  2376. (SessionTree->Selected->Parent != SessionTree->DropTarget))
  2377. {
  2378. TTreeNode * DropTarget = SessionTree->DropTarget;
  2379. TSessionData * Session = SelectedSession;
  2380. AnsiString Path =
  2381. UnixIncludeTrailingBackslash(SessionNodePath(DropTarget)) +
  2382. UnixExtractFileName(Session->SessionName);
  2383. SessionNameValidate(Path, Session);
  2384. // remove from storage
  2385. Session->Remove();
  2386. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  2387. // modified, only explicit
  2388. StoredSessions->Save(false, true);
  2389. // this should aways be the case
  2390. if (Session != NewSession)
  2391. {
  2392. TTreeNode * Node = SessionTree->Selected;
  2393. // look for overwritten node (if any)
  2394. TTreeNode * ANode = SessionTree->Items->GetFirstNode();
  2395. while (ANode != NULL)
  2396. {
  2397. if (ANode->Data == NewSession)
  2398. {
  2399. ANode->Delete();
  2400. break;
  2401. }
  2402. ANode = ANode->GetNext();
  2403. }
  2404. Node->MoveTo(DropTarget, naAddChild);
  2405. Node->Data = NewSession;
  2406. // try to make both visible
  2407. if (DropTarget != NULL)
  2408. {
  2409. DropTarget->MakeVisible();
  2410. }
  2411. Node->MakeVisible();
  2412. DestroySession(Session);
  2413. }
  2414. else
  2415. {
  2416. assert(false);
  2417. }
  2418. SessionData = NewSession;
  2419. }
  2420. else
  2421. {
  2422. assert(false);
  2423. }
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TLoginDialog::SessionTreeMouseMove(TObject * /*Sender*/,
  2427. TShiftState /*Shift*/, int X, int Y)
  2428. {
  2429. TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
  2430. THitTests HitTest = SessionTree->GetHitTestInfoAt(X, Y);
  2431. if (Node != FHintNode)
  2432. {
  2433. Application->CancelHint();
  2434. AnsiString Hint;
  2435. if (HitTest.Contains(htOnItem) || HitTest.Contains(htOnIcon) ||
  2436. HitTest.Contains(htOnLabel) || HitTest.Contains(htOnStateIcon))
  2437. {
  2438. FHintNode = Node;
  2439. if (Node->Data != NULL)
  2440. {
  2441. Hint = static_cast<TSessionData *>(Node->Data)->InfoTip;
  2442. }
  2443. else
  2444. {
  2445. Hint = "";
  2446. }
  2447. }
  2448. else
  2449. {
  2450. FHintNode = NULL;
  2451. Hint = "";
  2452. }
  2453. SessionTree->Hint = Hint;
  2454. }
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. void __fastcall TLoginDialog::SessionTreeEndDrag(TObject * /*Sender*/,
  2458. TObject * /*Target*/, int /*X*/, int /*Y*/)
  2459. {
  2460. FScrollOnDragOver->EndDrag();
  2461. }
  2462. //---------------------------------------------------------------------------