Login.cpp 85 KB

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