Login.cpp 82 KB

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