Login.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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 "WinConfiguration.h"
  18. //---------------------------------------------------------------------
  19. #pragma link "ComboEdit"
  20. #pragma link "PasswordEdit"
  21. #pragma link "UpDownEdit"
  22. #ifndef NO_RESOURCES
  23. #pragma resource "*.dfm"
  24. #endif
  25. //---------------------------------------------------------------------------
  26. const int OpenFolderStateIndex = 2;
  27. const int ClosedFolderStateIndex = 3;
  28. const int WorkspaceStateIndex = 4;
  29. const int NewSiteStateIndex = 6;
  30. //---------------------------------------------------------------------------
  31. bool __fastcall DoLoginDialog(TStoredSessionList *SessionList,
  32. TList * DataList, int Options)
  33. {
  34. assert(DataList != NULL);
  35. TLoginDialog * LoginDialog = SafeFormCreate<TLoginDialog>();
  36. bool Result;
  37. try
  38. {
  39. LoginDialog->Init(SessionList, Options);
  40. Result = LoginDialog->Execute(DataList);
  41. }
  42. __finally
  43. {
  44. delete LoginDialog;
  45. }
  46. return Result;
  47. }
  48. //---------------------------------------------------------------------
  49. static const TFSProtocol FSOrder[] = { fsSFTPonly, fsSCPonly, fsFTP, fsWebDAV };
  50. //---------------------------------------------------------------------
  51. __fastcall TLoginDialog::TLoginDialog(TComponent* AOwner)
  52. : TForm(AOwner)
  53. {
  54. FNewSiteData = new TSessionData(L"");
  55. FInitialized = false;
  56. FOptions = loStartup;
  57. FLocaleChanging = false;
  58. FHintNode = NULL;
  59. FScrollOnDragOver = new TTreeViewScrollOnDragOver(SessionTree, true);
  60. FDataList = NULL;
  61. FUpdatePortWithProtocol = true;
  62. FIncrementalSearching = 0;
  63. FSitesIncrementalSearchHaveNext = false;
  64. FEditing = false;
  65. FRenaming = false;
  66. // we need to make sure that window procedure is set asap
  67. // (so that CM_SHOWINGCHANGED handling is applied)
  68. UseSystemSettingsPre(this, &FSystemSettings);
  69. FOriginalSize = BoundsRect.GetSize();
  70. FBasicGroupBaseHeight = BasicGroup->Height - BasicSshPanel->Height - BasicFtpPanel->Height;
  71. InitControls();
  72. }
  73. //---------------------------------------------------------------------
  74. __fastcall TLoginDialog::~TLoginDialog()
  75. {
  76. delete FScrollOnDragOver;
  77. assert(FSystemSettings);
  78. DeleteSystemSettings(this, FSystemSettings);
  79. FSystemSettings = NULL;
  80. delete FNewSiteData;
  81. InvalidateSessionData();
  82. }
  83. //---------------------------------------------------------------------
  84. void __fastcall TLoginDialog::InvalidateSessionData()
  85. {
  86. delete FSessionData;
  87. FSessionData = NULL;
  88. }
  89. //---------------------------------------------------------------------
  90. void __fastcall TLoginDialog::Init(TStoredSessionList *SessionList,
  91. int Options)
  92. {
  93. FStoredSessions = SessionList;
  94. LoadSessions();
  95. FOptions = Options;
  96. UnicodeString Dummy;
  97. RunPageantAction->Visible = FindTool(PageantTool, Dummy);
  98. RunPuttygenAction->Visible = FindTool(PuttygenTool, Dummy);
  99. UpdateControls();
  100. }
  101. //---------------------------------------------------------------------
  102. void __fastcall TLoginDialog::InitControls()
  103. {
  104. if (SessionTree->WindowProc != SessionTreeProc)
  105. {
  106. FOldSessionTreeProc = SessionTree->WindowProc;
  107. SessionTree->WindowProc = SessionTreeProc;
  108. }
  109. int FtpsNoneIndex = FtpsToIndex(ftpsNone);
  110. int FtpsImplicitIndex = FtpsToIndex(ftpsImplicit);
  111. FtpsCombo->Items->Strings[FtpsImplicitIndex] = LoadStr(FTPS_IMPLICIT);
  112. FtpsCombo->Items->Strings[FtpsToIndex(ftpsExplicitTls)] = LoadStr(FTPS_EXPLICIT_TLS);
  113. FtpsCombo->Items->Strings[FtpsToIndex(ftpsExplicitSsl)] = LoadStr(FTPS_EXPLICIT_SSL);
  114. WebDavsCombo->Items->Strings[FtpsNoneIndex] = FtpsCombo->Items->Strings[FtpsNoneIndex];
  115. WebDavsCombo->Items->Strings[FtpsImplicitIndex] = FtpsCombo->Items->Strings[FtpsImplicitIndex];
  116. BasicSshPanel->Top = BasicFtpPanel->Top;
  117. SitesIncrementalSearchLabel->AutoSize = false;
  118. SitesIncrementalSearchLabel->Left = SessionTree->Left;
  119. SitesIncrementalSearchLabel->Width = SessionTree->Width;
  120. SitesIncrementalSearchLabel->Top = SessionTree->BoundsRect.Bottom - SitesIncrementalSearchLabel->Height;
  121. SitesIncrementalSearchLabel->Visible = false;
  122. ReadOnlyControl(TransferProtocolView);
  123. ReadOnlyControl(EncryptionView);
  124. MenuButton(ToolsMenuButton);
  125. MenuButton(ManageButton);
  126. }
  127. //---------------------------------------------------------------------
  128. void __fastcall TLoginDialog::Init()
  129. {
  130. if (!FInitialized)
  131. {
  132. UseSystemSettingsPost(this, FSystemSettings);
  133. }
  134. else
  135. {
  136. UseSystemSettings(this, &FSystemSettings);
  137. }
  138. FInitialized = true;
  139. Caption = FORMAT(L"%s %s", (AppName, Caption));
  140. InitControls();
  141. #ifdef NO_FILEZILLA
  142. assert(TransferProtocolCombo->Items->Count == FSPROTOCOL_COUNT - 2 - 1);
  143. TransferProtocolCombo->Items->Delete(TransferProtocolCombo->Items->Count - 1);
  144. #endif
  145. ReadOnlyControl(ContentsNameEdit);
  146. ReadOnlyControl(ContentsMemo);
  147. if (ALWAYS_FALSE(SessionTree->Items->Count == 0) ||
  148. ((SessionTree->Items->Count == 1) &&
  149. ALWAYS_TRUE(IsNewSiteNode(SessionTree->Items->GetFirstNode()))))
  150. {
  151. ActiveControl = HostNameEdit;
  152. }
  153. else
  154. {
  155. ActiveControl = SessionTree;
  156. }
  157. UpdateControls();
  158. }
  159. //---------------------------------------------------------------------
  160. TTreeNode * __fastcall TLoginDialog::AddSessionPath(UnicodeString Path,
  161. bool CanCreate, bool IsWorkspace)
  162. {
  163. TTreeNode * Parent = NULL;
  164. while (!Path.IsEmpty())
  165. {
  166. UnicodeString Folder = ::CutToChar(Path, L'/', false);
  167. TTreeNode * Node =
  168. ((Parent == NULL) ? SessionTree->Items->GetFirstNode() : Parent->getFirstChild());
  169. // note that we allow folder with the same name as existing session
  170. // on the same level (see also SessionTreeEdited)
  171. while ((Node != NULL) && (IsSessionNode(Node) || !AnsiSameText(Node->Text, Folder)))
  172. {
  173. Node = Node->getNextSibling();
  174. }
  175. if (Node == NULL)
  176. {
  177. if (!CanCreate)
  178. {
  179. return NULL;
  180. }
  181. else
  182. {
  183. TTreeNode * AParent = Parent;
  184. Parent = SessionTree->Items->AddChild(Parent, Folder);
  185. // once workspace, forever workspace
  186. if (!IsWorkspaceNode(Parent))
  187. {
  188. if (IsWorkspace)
  189. {
  190. Parent->StateIndex = WorkspaceStateIndex;
  191. }
  192. else
  193. {
  194. UpdateFolderNode(Parent);
  195. }
  196. }
  197. // folders seem not to be sorted automatically (not having set the data property)
  198. if (AParent == NULL)
  199. {
  200. SessionTree->Items->AlphaSort();
  201. }
  202. else
  203. {
  204. AParent->AlphaSort();
  205. }
  206. }
  207. }
  208. else
  209. {
  210. Parent = Node;
  211. }
  212. }
  213. return Parent;
  214. }
  215. //---------------------------------------------------------------------
  216. bool __fastcall TLoginDialog::IsFolderNode(TTreeNode * Node)
  217. {
  218. return (Node != NULL) && (Node->Data == NULL) && (Node->StateIndex != WorkspaceStateIndex);
  219. }
  220. //---------------------------------------------------------------------
  221. bool __fastcall TLoginDialog::IsWorkspaceNode(TTreeNode * Node)
  222. {
  223. return (Node != NULL) && (Node->Data == NULL) && (Node->StateIndex == WorkspaceStateIndex);
  224. }
  225. //---------------------------------------------------------------------
  226. bool __fastcall TLoginDialog::IsFolderOrWorkspaceNode(TTreeNode * Node)
  227. {
  228. return (Node != NULL) && (Node->Data == NULL);
  229. }
  230. //---------------------------------------------------------------------
  231. bool __fastcall TLoginDialog::IsSiteNode(TTreeNode * Node)
  232. {
  233. return (Node != NULL) && (Node->Data != NULL) && (Node->Data != FNewSiteData);
  234. }
  235. //---------------------------------------------------------------------
  236. bool __fastcall TLoginDialog::IsNewSiteNode(TTreeNode * Node)
  237. {
  238. return (Node != NULL) && (Node->Data != NULL) && (Node->Data == FNewSiteData);
  239. }
  240. //---------------------------------------------------------------------
  241. bool __fastcall TLoginDialog::IsSessionNode(TTreeNode * Node)
  242. {
  243. return (Node != NULL) && (Node->Data != NULL);
  244. }
  245. //---------------------------------------------------------------------
  246. TSessionData * __fastcall TLoginDialog::GetNodeSession(TTreeNode * Node)
  247. {
  248. return NOT_NULL(static_cast<TSessionData *>(Node->Data));
  249. }
  250. //---------------------------------------------------------------------
  251. TTreeNode * __fastcall TLoginDialog::AddSession(TSessionData * Data)
  252. {
  253. TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Data->Name), true, Data->IsWorkspace);
  254. TTreeNode * Node = SessionTree->Items->AddChild(Parent, UnixExtractFileName(Data->Name));
  255. Node->Data = Data;
  256. return Node;
  257. }
  258. //---------------------------------------------------------------------
  259. void __fastcall TLoginDialog::DestroySession(TSessionData * Data)
  260. {
  261. StoredSessions->Remove(Data);
  262. }
  263. //---------------------------------------------------------------------
  264. void __fastcall TLoginDialog::LoadSessions()
  265. {
  266. SessionTree->Items->BeginUpdate();
  267. try
  268. {
  269. SessionTree->Items->Clear();
  270. TTreeNode * Node = SessionTree->Items->AddChild(NULL, LoadStr(LOGIN_NEW_SITE_NODE));
  271. Node->Data = FNewSiteData;
  272. Node->StateIndex = NewSiteStateIndex;
  273. assert(StoredSessions != NULL);
  274. for (int Index = 0; Index < StoredSessions->Count; Index++)
  275. {
  276. AddSession(StoredSessions->Sessions[Index]);
  277. }
  278. }
  279. __finally
  280. {
  281. // folders seem not to be sorted automatically (not having set the data property)
  282. SessionTree->AlphaSort();
  283. SessionTree->Items->EndUpdate();
  284. }
  285. SessionTree->Selected = SessionTree->Items->GetFirstNode();
  286. UpdateControls();
  287. }
  288. //---------------------------------------------------------------------------
  289. void __fastcall TLoginDialog::UpdateFolderNode(TTreeNode * Node)
  290. {
  291. assert((Node->StateIndex == -1) ||
  292. (Node->StateIndex == OpenFolderStateIndex) || (Node->StateIndex == ClosedFolderStateIndex));
  293. Node->StateIndex = (Node->Expanded ? OpenFolderStateIndex : ClosedFolderStateIndex);
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TLoginDialog::Default()
  297. {
  298. if (ALWAYS_TRUE(StoredSessions != NULL))
  299. {
  300. FNewSiteData->Assign(StoredSessions->DefaultSettings);
  301. }
  302. TTreeNode * NewSiteNode = SessionTree->Items->GetFirstNode();
  303. if (ALWAYS_TRUE(IsNewSiteNode(NewSiteNode)))
  304. {
  305. SessionTree->Selected = NewSiteNode;
  306. }
  307. LoadContents();
  308. }
  309. //---------------------------------------------------------------------
  310. void __fastcall TLoginDialog::LoadContents()
  311. {
  312. bool UseContentsPanel;
  313. TTreeNode * Node = SessionTree->Selected;
  314. if (IsSessionNode(Node))
  315. {
  316. LoadSession(SelectedSession);
  317. UseContentsPanel = false;
  318. }
  319. else if (ALWAYS_TRUE(IsFolderOrWorkspaceNode(Node)))
  320. {
  321. UnicodeString NodePath = SessionNodePath(Node);
  322. ContentsNameEdit->Text = NodePath;
  323. UnicodeString CommonRoot = IsFolderNode(Node) ? UnixIncludeTrailingBackslash(NodePath) : UnicodeString();
  324. ContentsMemo->Lines->Text =
  325. GetFolderOrWorkspaceContents(Node, L"", CommonRoot);
  326. UseContentsPanel = true;
  327. if (IsFolderNode(Node))
  328. {
  329. ContentsGroupBox->Caption = LoadStr(LOGIN_SITE_FOLDER_CAPTION);
  330. }
  331. else if (ALWAYS_TRUE(IsWorkspaceNode(Node)))
  332. {
  333. ContentsGroupBox->Caption = LoadStr(LOGIN_WORKSPACE_CAPTION);
  334. }
  335. }
  336. SitePanel->Visible = !UseContentsPanel;
  337. ContentsPanel->Visible = UseContentsPanel;
  338. }
  339. //---------------------------------------------------------------------
  340. void __fastcall TLoginDialog::LoadSession(TSessionData * SessionData)
  341. {
  342. WinConfiguration->BeginMasterPasswordSession();
  343. try
  344. {
  345. UserNameEdit->Text = SessionData->UserName;
  346. PortNumberEdit->AsInteger = SessionData->PortNumber;
  347. HostNameEdit->Text = SessionData->HostName;
  348. bool Editable = FEditing || (SessionData == FNewSiteData);
  349. if (Editable)
  350. {
  351. PasswordEdit->Text = SessionData->Password;
  352. }
  353. else
  354. {
  355. PasswordEdit->Text =
  356. SessionData->HasPassword() ?
  357. UnicodeString::StringOfChar(L'?', 16) : UnicodeString();
  358. }
  359. int FtpsIndex = FtpsToIndex(SessionData->Ftps);
  360. FtpsCombo->ItemIndex = FtpsIndex;
  361. WebDavsCombo->ItemIndex = FtpsIndex;
  362. EncryptionView->Text =
  363. ALWAYS_TRUE(FtpsCombo->ItemIndex >= WebDavsCombo->ItemIndex) ? FtpsCombo->Text : WebDavsCombo->Text;
  364. bool AllowScpFallback;
  365. TransferProtocolCombo->ItemIndex = FSProtocolToIndex(SessionData->FSProtocol, AllowScpFallback);
  366. TransferProtocolView->Text = TransferProtocolCombo->Text;
  367. // just in case TransferProtocolComboChange is not triggered
  368. FDefaultPort = DefaultPort();
  369. FUpdatePortWithProtocol = true;
  370. // advanced
  371. InvalidateSessionData();
  372. // close advanced settings only when really needed,
  373. // see also note in SessionAdvancedActionExecute
  374. if (Editable)
  375. {
  376. FSessionData = new TSessionData(L"");
  377. FSessionData->Assign(SessionData);
  378. }
  379. }
  380. __finally
  381. {
  382. WinConfiguration->EndMasterPasswordSession();
  383. }
  384. UpdateControls();
  385. }
  386. //---------------------------------------------------------------------
  387. void __fastcall TLoginDialog::SaveSession(TSessionData * SessionData)
  388. {
  389. // advanced
  390. if (ALWAYS_TRUE(FSessionData != NULL))
  391. {
  392. SessionData->Assign(FSessionData);
  393. }
  394. // Basic page
  395. SessionData->UserName = UserNameEdit->Text.Trim();
  396. SessionData->PortNumber = PortNumberEdit->AsInteger;
  397. // must be loaded after UserName, because HostName may be in format user@host
  398. SessionData->HostName = HostNameEdit->Text.Trim();
  399. SessionData->Password = PasswordEdit->Text;
  400. SessionData->Ftps = GetFtps();
  401. SessionData->FSProtocol =
  402. // requiring SCP fallback distinction
  403. GetFSProtocol(true);
  404. TSessionData * EditingSessionData = GetEditingSessionData();
  405. SessionData->Name =
  406. (EditingSessionData != NULL) ? EditingSessionData->Name : SessionData->DefaultSessionName;
  407. }
  408. //---------------------------------------------------------------------
  409. void __fastcall TLoginDialog::UpdateControls()
  410. {
  411. // without FLocaleChanging test, some button controls get lost, when changing language
  412. if (Visible && FInitialized && !FLocaleChanging)
  413. {
  414. bool NewSiteSelected = IsNewSiteNode(SessionTree->Selected);
  415. bool Editable = NewSiteSelected || FEditing;
  416. TFSProtocol FSProtocol = GetFSProtocol(false);
  417. bool SshProtocol = IsSshProtocol(FSProtocol);
  418. bool FtpProtocol = (FSProtocol == fsFTP);
  419. bool WebDavProtocol = (FSProtocol == fsWebDAV);
  420. // session
  421. FtpsCombo->Visible = Editable && FtpProtocol;
  422. FtpsLabel->Visible = FtpProtocol;
  423. WebDavsCombo->Visible = Editable && WebDavProtocol;
  424. WebDavsLabel->Visible = WebDavProtocol;
  425. EncryptionView->Visible = !Editable && (FtpProtocol || WebDavProtocol);
  426. BasicSshPanel->Visible = SshProtocol;
  427. BasicFtpPanel->Visible = FtpProtocol && Editable;
  428. // we do not support both at the same time
  429. assert(!BasicSshPanel->Visible || !BasicFtpPanel->Visible);
  430. BasicGroup->Height =
  431. FBasicGroupBaseHeight +
  432. (BasicSshPanel->Visible ? BasicSshPanel->Height : 0) +
  433. (BasicFtpPanel->Visible ? BasicFtpPanel->Height : 0);
  434. AnonymousLoginCheck->Checked =
  435. SameText(UserNameEdit->Text, AnonymousUserName) &&
  436. SameText(PasswordEdit->Text, AnonymousPassword);
  437. TransferProtocolCombo->Visible = Editable;
  438. TransferProtocolView->Visible = !TransferProtocolCombo->Visible;
  439. ReadOnlyControl(HostNameEdit, !Editable);
  440. ReadOnlyControl(PortNumberEdit, !Editable);
  441. PortNumberEdit->ButtonsVisible = Editable;
  442. ReadOnlyControl(UserNameEdit, !Editable);
  443. ReadOnlyControl(PasswordEdit, !Editable);
  444. // sites
  445. if (SitesIncrementalSearchLabel->Visible != !FSitesIncrementalSearch.IsEmpty())
  446. {
  447. if (FSitesIncrementalSearch.IsEmpty())
  448. {
  449. SitesIncrementalSearchLabel->Visible = false;
  450. SessionTree->Height = SitesIncrementalSearchLabel->BoundsRect.Bottom - SessionTree->Top;
  451. }
  452. else
  453. {
  454. SitesIncrementalSearchLabel->Visible = true;
  455. SessionTree->Height = SitesIncrementalSearchLabel->BoundsRect.Top - SessionTree->Top;
  456. }
  457. }
  458. if (!FSitesIncrementalSearch.IsEmpty())
  459. {
  460. SitesIncrementalSearchLabel->Caption =
  461. L" " + FMTLOAD(LOGIN_SITES_INC_SEARCH, (FSitesIncrementalSearch)) +
  462. (FSitesIncrementalSearchHaveNext ? L" " + LoadStr(LOGIN_SITES_NEXT_SEARCH) : UnicodeString());
  463. }
  464. EnableControl(ManageButton, !FEditing);
  465. EnableControl(ToolsMenuButton, !FEditing);
  466. EnableControl(CloseButton, !FEditing);
  467. DefaultButton(LoginButton, !FEditing && !FRenaming);
  468. CloseButton->Cancel = !FEditing && !FRenaming;
  469. DefaultButton(SaveButton, FEditing);
  470. EditCancelButton->Cancel = FEditing;
  471. UpdateButtonVisibility(SaveButton);
  472. UpdateButtonVisibility(EditButton);
  473. UpdateButtonVisibility(EditCancelButton);
  474. bool CanSaveSssion = FEditing;
  475. TAction * SaveButtonAction =
  476. CanSaveSssion && SupportsSplitButton() ? SaveSessionAction : SaveAsSessionAction;
  477. if (SaveButton->Action != SaveButtonAction)
  478. {
  479. SaveButton->Action = SaveButtonAction;
  480. }
  481. SaveSessionMenuItem->Visible = CanSaveSssion;
  482. SaveSessionMenuItem->Default = CanSaveSssion;
  483. SaveAsSessionMenuItem->Default = !CanSaveSssion;
  484. }
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TLoginDialog::DefaultButton(TButton * Button, bool Default)
  488. {
  489. // default property setter does not have guard for "the same value"
  490. if (Button->Default != Default)
  491. {
  492. Button->Default = Default;
  493. }
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TLoginDialog::UpdateButtonVisibility(TButton * Button)
  497. {
  498. TAction * Action = NOT_NULL(dynamic_cast<TAction *>(Button->Action));
  499. // when all action targets are hidden, action does not get updated,
  500. // so we need to do it manually
  501. Action->Update();
  502. // button visibility cannot be bound to action visibility,
  503. // so we do not bother setting action visibility, instead we manually
  504. // bind visibility to enabled state
  505. Button->Visible = Action->Enabled;
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall TLoginDialog::DataChange(TObject * /*Sender*/)
  509. {
  510. UpdateControls();
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TLoginDialog::FormShow(TObject * /*Sender*/)
  514. {
  515. if (!FInitialized || FLocaleChanging)
  516. {
  517. Init();
  518. LoadContents();
  519. }
  520. // among other this makes the expanded nodes look like expanded,
  521. // because the LoadState call in Execute is too early,
  522. // and some stray call to collapsed event during showing process,
  523. // make the image be set to collapsed
  524. LoadState();
  525. UpdateControls();
  526. }
  527. //---------------------------------------------------------------------------
  528. void __fastcall TLoginDialog::SessionTreeChange(TObject * /*Sender*/,
  529. TTreeNode * /*Node*/)
  530. {
  531. if (FIncrementalSearching <= 0)
  532. {
  533. // Make sure UpdateControls is called here, no matter what,
  534. // now it is always called from ResetSitesIncrementalSearch.
  535. // For the "else" scenario, UpdateControls is called later from SitesIncrementalSearch.
  536. ResetSitesIncrementalSearch();
  537. }
  538. if (FInitialized)
  539. {
  540. LoadContents();
  541. }
  542. }
  543. //---------------------------------------------------------------------------
  544. TSessionData * __fastcall TLoginDialog::GetSessionData()
  545. {
  546. if (SelectedSession == FNewSiteData)
  547. {
  548. SaveSession(FNewSiteData);
  549. }
  550. return SelectedSession;
  551. }
  552. //---------------------------------------------------------------------------
  553. void __fastcall TLoginDialog::SessionTreeDblClick(TObject * /*Sender*/)
  554. {
  555. TPoint P = SessionTree->ScreenToClient(Mouse->CursorPos);
  556. TTreeNode * Node = SessionTree->GetNodeAt(P.x, P.y);
  557. if (IsSessionNode(Node))
  558. {
  559. TSessionData * Data = GetNodeSession(Node);
  560. // this may be false, when collapsed folder was double-clicked,
  561. // it got expanded, view was shifted to accommodate folder contents,
  562. // so that cursor now points to a different node (site)
  563. if (Data == SelectedSession)
  564. {
  565. if (FStoredSessions->CanLogin(Data))
  566. {
  567. ModalResult = mrOk;
  568. }
  569. }
  570. }
  571. else if (IsWorkspaceNode(Node))
  572. {
  573. if (HasNodeAnySession(Node, true))
  574. {
  575. ModalResult = mrOk;
  576. }
  577. }
  578. }
  579. //---------------------------------------------------------------------------
  580. TSessionData * __fastcall TLoginDialog::GetSelectedSession()
  581. {
  582. // Selected can be temporarily NULL, e.g. while deleting selected node
  583. TTreeNode * Node = SessionTree->Selected;
  584. if ((Node != NULL) &&
  585. (IsSiteNode(Node) || IsNewSiteNode(Node)))
  586. {
  587. return GetNodeSession(Node);
  588. }
  589. else
  590. {
  591. return NULL;
  592. }
  593. }
  594. //---------------------------------------------------------------------------
  595. void __fastcall TLoginDialog::SessionTreeKeyDown(TObject * /*Sender*/,
  596. WORD & Key, TShiftState /*Shift*/)
  597. {
  598. if (!SessionTree->IsEditing())
  599. {
  600. if (Key == VK_DELETE)
  601. {
  602. DeleteSessionAction->Execute();
  603. Key = 0;
  604. }
  605. else if (Key == VK_F2)
  606. {
  607. RenameSessionAction->Execute();
  608. Key = 0;
  609. }
  610. else if (Key == VK_BACK)
  611. {
  612. Key = 0;
  613. }
  614. }
  615. }
  616. //---------------------------------------------------------------------------
  617. void __fastcall TLoginDialog::SessionTreeKeyPress(TObject * /*Sender*/, System::WideChar & Key)
  618. {
  619. if (!SessionTree->IsEditing())
  620. {
  621. // filter control sequences
  622. if (Key >= VK_SPACE)
  623. {
  624. if (!SitesIncrementalSearch(FSitesIncrementalSearch + Key, false, false))
  625. {
  626. MessageBeep(0);
  627. }
  628. Key = 0;
  629. }
  630. else if (Key == VK_BACK)
  631. {
  632. if (!FSitesIncrementalSearch.IsEmpty())
  633. {
  634. if (FSitesIncrementalSearch.Length() == 1)
  635. {
  636. ResetSitesIncrementalSearch();
  637. }
  638. else
  639. {
  640. UnicodeString NewText =
  641. FSitesIncrementalSearch.SubString(1, FSitesIncrementalSearch.Length() - 1);
  642. SitesIncrementalSearch(NewText, false, false);
  643. }
  644. Key = 0;
  645. }
  646. }
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TLoginDialog::EditSession()
  651. {
  652. HostNameEdit->SetFocus();
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TLoginDialog::EditSessionActionExecute(TObject * /*Sender*/)
  656. {
  657. if (ALWAYS_TRUE(SelectedSession != NULL))
  658. {
  659. FEditing = true;
  660. EditSession();
  661. // reload session, to make sure we load decrypted password
  662. LoadContents();
  663. UpdateControls();
  664. }
  665. }
  666. //---------------------------------------------------------------------------
  667. TTreeNode * __fastcall TLoginDialog::FindSessionNode(TSessionData * SessionData, bool ByName)
  668. {
  669. TTreeNode * Node = SessionTree->Items->GetFirstNode();
  670. while (
  671. (Node != NULL) &&
  672. ((!ByName && (Node->Data != SessionData)) ||
  673. (ByName && (!IsSiteNode(Node) || (GetNodeSession(Node)->Name != SessionData->Name)))))
  674. {
  675. Node = Node->GetNext();
  676. }
  677. return Node;
  678. }
  679. //---------------------------------------------------------------------------
  680. TSessionData * __fastcall TLoginDialog::GetEditingSessionData()
  681. {
  682. return FEditing ? NOT_NULL(SelectedSession) : NULL;
  683. }
  684. //---------------------------------------------------------------------------
  685. void __fastcall TLoginDialog::SaveAsSession(bool ForceDialog)
  686. {
  687. std::auto_ptr<TSessionData> SessionData(new TSessionData(L""));
  688. SaveSession(SessionData.get());
  689. TSessionData * EditingSessionData = GetEditingSessionData();
  690. TSessionData * NewSession = DoSaveSession(SessionData.get(), EditingSessionData, ForceDialog);
  691. if (NewSession != NULL)
  692. {
  693. TTreeNode * ParentNode = AddSessionPath(UnixExtractFilePath(NewSession->SessionName), false, false);
  694. CheckIsSessionFolder(ParentNode);
  695. FEditing = false;
  696. TTreeNode * Node = FindSessionNode(NewSession, false);
  697. if (Node == NULL)
  698. {
  699. Node = AddSession(NewSession);
  700. }
  701. SessionTree->Selected = Node;
  702. SessionTree->SetFocus();
  703. // this
  704. // - updates TransferProtocolView and EncryptionView
  705. // - clears the password box, if user has not opted to save password
  706. // - reloads fake password
  707. LoadContents();
  708. UpdateControls();
  709. }
  710. }
  711. //---------------------------------------------------------------------------
  712. void __fastcall TLoginDialog::SaveSessionActionExecute(TObject * /*Sender*/)
  713. {
  714. SaveAsSession(false);
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TLoginDialog::SaveAsSessionActionExecute(TObject * /*Sender*/)
  718. {
  719. SaveAsSession(true);
  720. }
  721. //---------------------------------------------------------------------------
  722. UnicodeString __fastcall TLoginDialog::SessionNodePath(TTreeNode * Node)
  723. {
  724. UnicodeString Path;
  725. if ((Node != NULL) && !IsNewSiteNode(Node))
  726. {
  727. Path = Node->Text;
  728. Node = Node->Parent;
  729. while (Node != NULL)
  730. {
  731. Path.Insert(UnixIncludeTrailingBackslash(Node->Text), 1);
  732. Node = Node->Parent;
  733. }
  734. }
  735. return Path;
  736. }
  737. //---------------------------------------------------------------------------
  738. void __fastcall TLoginDialog::DeleteSessionActionExecute(TObject * /*Sender*/)
  739. {
  740. assert(SessionTree->Selected != NULL);
  741. TTreeNode * Node = SessionTree->Selected;
  742. if (IsSiteNode(Node))
  743. {
  744. TSessionData * Session = SelectedSession;
  745. if (MessageDialog(FMTLOAD(CONFIRM_DELETE_SESSION, (Session->SessionName)),
  746. qtConfirmation, qaOK | qaCancel, HELP_DELETE_SESSION) == qaOK)
  747. {
  748. WinConfiguration->DeleteSessionFromJumpList(Session->SessionName);
  749. Session->Remove();
  750. DestroySession(Session);
  751. SessionTree->Selected->Delete();
  752. }
  753. }
  754. else if (IsFolderNode(Node) || IsWorkspaceNode(Node))
  755. {
  756. int Sessions = 0;
  757. TTreeNode * ANode = Node->GetNext();
  758. while ((ANode != NULL) && ANode->HasAsParent(Node))
  759. {
  760. if (IsSessionNode(ANode))
  761. {
  762. TSessionData * Session = GetNodeSession(ANode);
  763. if (Session->Special)
  764. {
  765. SessionTree->Selected = ANode;
  766. ANode->MakeVisible();
  767. throw Exception(FMTLOAD(LOGIN_DELETE_SPECIAL_SESSION, (Session->SessionName)));
  768. }
  769. Sessions++;
  770. }
  771. ANode = ANode->GetNext();
  772. }
  773. UnicodeString Path = SessionNodePath(Node);
  774. int Prompt;
  775. UnicodeString HelpKeyword;
  776. if (IsFolderNode(Node))
  777. {
  778. Prompt = LOGIN_DELETE_SESSION_FOLDER;
  779. HelpKeyword = HELP_DELETE_SESSION_FOLDER;
  780. }
  781. else
  782. {
  783. Prompt = LOGIN_DELETE_WORKSPACE;
  784. HelpKeyword = HELP_DELETE_WORKSPACE;
  785. }
  786. if ((Sessions == 0) ||
  787. (MessageDialog(FMTLOAD(Prompt, (Path, Sessions)),
  788. qtConfirmation, qaOK | qaCancel, HelpKeyword) == qaOK))
  789. {
  790. if (IsWorkspaceNode(Node))
  791. {
  792. WinConfiguration->DeleteWorkspaceFromJumpList(Path);
  793. }
  794. Node = SessionTree->Selected;
  795. TTreeNode * ANode = Node->GetNext();
  796. while ((ANode != NULL) && ANode->HasAsParent(Node))
  797. {
  798. if (IsSessionNode(ANode))
  799. {
  800. TSessionData * Session = GetNodeSession(ANode);
  801. Session->Remove();
  802. DestroySession(Session);
  803. }
  804. ANode = ANode->GetNext();
  805. }
  806. SessionTree->Selected->Delete();
  807. }
  808. }
  809. }
  810. //---------------------------------------------------------------------------
  811. void __fastcall TLoginDialog::ReloadSessions()
  812. {
  813. SaveState();
  814. LoadSessions();
  815. LoadState();
  816. if (SessionTree->Items->Count > 0)
  817. {
  818. SessionTree->Items->GetFirstNode()->MakeVisible();
  819. }
  820. }
  821. //---------------------------------------------------------------------------
  822. void __fastcall TLoginDialog::ImportSessionsActionExecute(TObject * /*Sender*/)
  823. {
  824. if (DoImportSessionsDialog())
  825. {
  826. ReloadSessions();
  827. }
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TLoginDialog::CleanUpActionExecute(TObject * /*Sender*/)
  831. {
  832. if (DoCleanupDialog(StoredSessions, Configuration))
  833. {
  834. SaveState();
  835. LoadSessions();
  836. LoadState();
  837. }
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TLoginDialog::AboutActionExecute(TObject * /*Sender*/)
  841. {
  842. DoAboutDialog(Configuration);
  843. }
  844. //---------------------------------------------------------------------------
  845. void __fastcall TLoginDialog::ActionListUpdate(TBasicAction * BasicAction,
  846. bool &Handled)
  847. {
  848. bool NewSiteSelected = IsNewSiteNode(SessionTree->Selected);
  849. bool SiteSelected = IsSiteNode(SessionTree->Selected);
  850. bool FolderOrWorkspaceSelected = IsFolderOrWorkspaceNode(SessionTree->Selected);
  851. TAction * Action = NOT_NULL(dynamic_cast<TAction *>(BasicAction));
  852. bool PrevEnabled = Action->Enabled;
  853. if (Action == EditSessionAction)
  854. {
  855. EditSessionAction->Enabled = SiteSelected && !FEditing;
  856. }
  857. else if (Action == EditCancelAction)
  858. {
  859. EditCancelAction->Enabled = FEditing;
  860. }
  861. else if (Action == DeleteSessionAction)
  862. {
  863. DeleteSessionAction->Enabled =
  864. ((SiteSelected && !SelectedSession->Special && !FEditing) ||
  865. FolderOrWorkspaceSelected);
  866. }
  867. else if (Action == RenameSessionAction)
  868. {
  869. RenameSessionAction->Enabled =
  870. ((SiteSelected && !SelectedSession->Special && !FEditing) ||
  871. FolderOrWorkspaceSelected);
  872. }
  873. else if (Action == DesktopIconAction)
  874. {
  875. DesktopIconAction->Enabled =
  876. (SiteSelected && !FEditing) ||
  877. (FolderOrWorkspaceSelected && HasNodeAnySession(SessionTree->Selected));
  878. }
  879. else if (Action == SendToHookAction)
  880. {
  881. SendToHookAction->Enabled = SiteSelected && !FEditing;
  882. }
  883. else if (Action == LoginAction)
  884. {
  885. TSessionData * Data = GetSessionData();
  886. LoginAction->Enabled =
  887. ((Data != NULL) && FStoredSessions->CanLogin(Data) && !FEditing) ||
  888. (FolderOrWorkspaceSelected && HasNodeAnySession(SessionTree->Selected, true));
  889. }
  890. else if (Action == SaveSessionAction)
  891. {
  892. SaveSessionAction->Enabled = FEditing;
  893. }
  894. else if (Action == SessionAdvancedAction)
  895. {
  896. SessionAdvancedAction->Enabled = NewSiteSelected || FEditing;
  897. }
  898. else if (Action == SaveAsSessionAction)
  899. {
  900. SaveAsSessionAction->Enabled = NewSiteSelected || FEditing;
  901. }
  902. else if (Action == NewSessionFolderAction)
  903. {
  904. NewSessionFolderAction->Enabled = !FEditing;
  905. }
  906. else if (Action == ImportSessionsAction)
  907. {
  908. ImportSessionsAction->Enabled =
  909. GUIConfiguration->AnyPuttySessionForImport(StoredSessions) ||
  910. GUIConfiguration->AnyFilezillaSessionForImport(StoredSessions);
  911. }
  912. else if ((Action == ImportAction) ||
  913. (Action == AboutAction) || (Action == CleanUpAction) ||
  914. (Action == CheckForUpdatesAction) || (Action == PreferencesAction))
  915. {
  916. Action->Visible = FLAGSET(FOptions, loExternalTools);
  917. }
  918. Handled = true;
  919. // to update buttons visibility
  920. if (PrevEnabled != Action->Enabled)
  921. {
  922. UpdateControls();
  923. }
  924. Idle();
  925. }
  926. //---------------------------------------------------------------------------
  927. void __fastcall TLoginDialog::Idle()
  928. {
  929. if (SessionTree->IsEditing() != FRenaming)
  930. {
  931. FRenaming = SessionTree->IsEditing();
  932. UpdateControls();
  933. }
  934. }
  935. //---------------------------------------------------------------------------
  936. bool __fastcall TLoginDialog::Execute(TList * DataList)
  937. {
  938. FDataList = DataList;
  939. if (DataList->Count > 0)
  940. {
  941. TSessionData * SessionData = reinterpret_cast<TSessionData * >(DataList->Items[0]);
  942. TTreeNode * Node = FindSessionNode(SessionData, true);
  943. if (Node != NULL)
  944. {
  945. SessionTree->Selected = Node;
  946. ActiveControl = SessionTree;
  947. }
  948. else
  949. {
  950. // This is actualy bit pointless, as we focus the last selected site anyway
  951. // in LoadState(). As of now, we hardly get any useful data
  952. // in ad-hoc DataList anyway, so it is not a big deal
  953. FNewSiteData->Assign(SessionData);
  954. FNewSiteData->Special = false;
  955. LoadContents();
  956. }
  957. }
  958. else
  959. {
  960. Default();
  961. }
  962. LoadState();
  963. bool Result = (ShowModal() == mrOk);
  964. SaveState();
  965. if (Result)
  966. {
  967. SaveConfiguration();
  968. // DataList saved already from FormCloseQuery
  969. }
  970. return Result;
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TLoginDialog::SaveDataList(TList * DataList)
  974. {
  975. // Normally we would call this from Execute,
  976. // but at that point the windows is already hidden.
  977. // Cloning session data may pop up master password dialog:
  978. // - if it happens between closing and destroyiong login dialog
  979. // the next window will appear in background for some reason
  980. // - and its actually even nicer when master password dialog pops up over
  981. // the login dialog
  982. DataList->Clear();
  983. if (!IsNewSiteNode(SessionTree->Selected))
  984. {
  985. TTreeNode * Node = SessionTree->Selected;
  986. if (IsFolderOrWorkspaceNode(Node))
  987. {
  988. UnicodeString Name = SessionNodePath(Node);
  989. if (IsWorkspaceNode(Node))
  990. {
  991. WinConfiguration->AddWorkspaceToJumpList(Name);
  992. }
  993. StoredSessions->GetFolderOrWorkspace(Name, DataList);
  994. }
  995. else if (ALWAYS_TRUE(IsSiteNode(Node)))
  996. {
  997. TSessionData * Data2 = new TSessionData(L"");
  998. Data2->Assign(GetNodeSession(Node));
  999. DataList->Add(Data2);
  1000. }
  1001. }
  1002. else
  1003. {
  1004. TSessionData * Data = GetSessionData();
  1005. TSessionData * Data2 = new TSessionData(L"");
  1006. Data2->Assign(Data);
  1007. // we carry the name of the edited stored session around while on the dialog,
  1008. // but we do not want it to leave the dialog, so that we can distinguish
  1009. // stored and ad-hoc sessions
  1010. Data2->Name = L"";
  1011. DataList->Add(Data2);
  1012. }
  1013. }
  1014. //---------------------------------------------------------------------------
  1015. void __fastcall TLoginDialog::SaveOpenedStoredSessionFolders(
  1016. TTreeNode * Node, TStrings * OpenedStoredSessionFolders)
  1017. {
  1018. if (IsFolderNode(Node))
  1019. {
  1020. if (Node->Expanded)
  1021. {
  1022. OpenedStoredSessionFolders->Add(SessionNodePath(Node));
  1023. }
  1024. for (int Index = 0; Index < Node->Count; Index++)
  1025. {
  1026. SaveOpenedStoredSessionFolders(Node->Item[Index], OpenedStoredSessionFolders);
  1027. }
  1028. }
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. void __fastcall TLoginDialog::SaveState()
  1032. {
  1033. assert(WinConfiguration != NULL);
  1034. TStringList * OpenedStoredSessionFolders = new TStringList();
  1035. WinConfiguration->BeginUpdate();
  1036. try
  1037. {
  1038. OpenedStoredSessionFolders->CaseSensitive = false;
  1039. OpenedStoredSessionFolders->Sorted = true;
  1040. for (int Index = 0; Index < SessionTree->Items->Count; Index++)
  1041. {
  1042. SaveOpenedStoredSessionFolders(
  1043. SessionTree->Items->Item[Index], OpenedStoredSessionFolders);
  1044. }
  1045. WinConfiguration->OpenedStoredSessionFolders = OpenedStoredSessionFolders->CommaText;
  1046. }
  1047. __finally
  1048. {
  1049. WinConfiguration->EndUpdate();
  1050. delete OpenedStoredSessionFolders;
  1051. }
  1052. WinConfiguration->LastStoredSession = SessionNodePath(SessionTree->Selected);
  1053. // used only when changing locale
  1054. FSavedBounds = BoundsRect;
  1055. TLoginDialogConfiguration DialogConfiguration = CustomWinConfiguration->LoginDialog;
  1056. DialogConfiguration.WindowSize = StoreFormSize(this);
  1057. CustomWinConfiguration->LoginDialog = DialogConfiguration;
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. void __fastcall TLoginDialog::LoadOpenedStoredSessionFolders(
  1061. TTreeNode * Node, TStrings * OpenedStoredSessionFolders)
  1062. {
  1063. if (IsFolderNode(Node))
  1064. {
  1065. UnicodeString Path = SessionNodePath(Node);
  1066. if (OpenedStoredSessionFolders->IndexOf(Path) >= 0)
  1067. {
  1068. Node->Expand(false);
  1069. UpdateFolderNode(Node);
  1070. }
  1071. }
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TLoginDialog::LoadState()
  1075. {
  1076. TStringList * OpenedStoredSessionFolders = new TStringList();
  1077. try
  1078. {
  1079. OpenedStoredSessionFolders->CaseSensitive = false;
  1080. OpenedStoredSessionFolders->Sorted = true;
  1081. OpenedStoredSessionFolders->CommaText = WinConfiguration->OpenedStoredSessionFolders;
  1082. for (int Index = 0; Index < SessionTree->Items->Count; Index++)
  1083. {
  1084. LoadOpenedStoredSessionFolders(
  1085. SessionTree->Items->Item[Index], OpenedStoredSessionFolders);
  1086. }
  1087. // tree view tried to make expanded node children all visible, what
  1088. // may scroll the selected node (what should be the first one here),
  1089. // out of the view
  1090. if (SessionTree->Selected != NULL)
  1091. {
  1092. SessionTree->Selected->MakeVisible();
  1093. }
  1094. }
  1095. __finally
  1096. {
  1097. delete OpenedStoredSessionFolders;
  1098. }
  1099. if (!WinConfiguration->LastStoredSession.IsEmpty())
  1100. {
  1101. UnicodeString Path = WinConfiguration->LastStoredSession;
  1102. UnicodeString ParentPath = UnixExtractFilePath(Path);
  1103. TTreeNode * Node;
  1104. if (ParentPath.IsEmpty())
  1105. {
  1106. Node = SessionTree->Items->GetFirstNode();
  1107. }
  1108. else
  1109. {
  1110. TTreeNode * Parent = AddSessionPath(ParentPath, false, false);
  1111. Node = (Parent != NULL) ? Parent->getFirstChild() : NULL;
  1112. }
  1113. if (Node != NULL)
  1114. {
  1115. UnicodeString Name = UnixExtractFileName(Path);
  1116. // actually we cannot distinguish folder and session here
  1117. // (note that we allow folder and session with the same name),
  1118. // this is pending for future improvements
  1119. while ((Node != NULL) && !AnsiSameText(Node->Text, Name))
  1120. {
  1121. Node = Node->getNextSibling();
  1122. }
  1123. if (Node != NULL)
  1124. {
  1125. SessionTree->Selected = Node;
  1126. SessionTree->Selected->MakeVisible();
  1127. }
  1128. }
  1129. }
  1130. if (FLocaleChanging)
  1131. {
  1132. BoundsRect = FSavedBounds;
  1133. }
  1134. else
  1135. {
  1136. RestoreFormSize(CustomWinConfiguration->LoginDialog.WindowSize, this);
  1137. }
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TLoginDialog::SaveConfiguration()
  1141. {
  1142. assert(CustomWinConfiguration);
  1143. TTreeNode * Node = SessionTree->Selected;
  1144. if (IsWorkspaceNode(Node))
  1145. {
  1146. WinConfiguration->LastWorkspace = SessionNodePath(Node);
  1147. }
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. void __fastcall TLoginDialog::PreferencesActionExecute(TObject * /*Sender*/)
  1151. {
  1152. ShowPreferencesDialog(::pmDefault);
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TLoginDialog::PreferencesLoggingActionExecute(TObject * /*Sender*/)
  1156. {
  1157. ShowPreferencesDialog(pmLogging);
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TLoginDialog::MasterPasswordRecrypt(TObject * /*Sender*/)
  1161. {
  1162. FNewSiteData->RecryptPasswords();
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. void __fastcall TLoginDialog::ShowPreferencesDialog(TPreferencesMode PreferencesMode)
  1166. {
  1167. assert(CustomWinConfiguration->OnMasterPasswordRecrypt == NULL);
  1168. CustomWinConfiguration->OnMasterPasswordRecrypt = MasterPasswordRecrypt;
  1169. try
  1170. {
  1171. DoPreferencesDialog(PreferencesMode);
  1172. }
  1173. __finally
  1174. {
  1175. assert(CustomWinConfiguration->OnMasterPasswordRecrypt == MasterPasswordRecrypt);
  1176. CustomWinConfiguration->OnMasterPasswordRecrypt = NULL;
  1177. }
  1178. UpdateControls();
  1179. }
  1180. //---------------------------------------------------------------------------
  1181. void __fastcall TLoginDialog::ResetNewSessionActionExecute(TObject * /*Sender*/)
  1182. {
  1183. Default();
  1184. EditSession();
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void __fastcall TLoginDialog::CMDialogKey(TWMKeyDown & Message)
  1188. {
  1189. if (Message.CharCode == VK_TAB)
  1190. {
  1191. if (!FSitesIncrementalSearch.IsEmpty())
  1192. {
  1193. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  1194. bool Reverse = Shift.Contains(ssShift);
  1195. if (!SitesIncrementalSearch(FSitesIncrementalSearch, true, Reverse))
  1196. {
  1197. MessageBeep(0);
  1198. }
  1199. Message.Result = 1;
  1200. return;
  1201. }
  1202. }
  1203. else if (Message.CharCode == VK_ESCAPE)
  1204. {
  1205. if (!FSitesIncrementalSearch.IsEmpty())
  1206. {
  1207. ResetSitesIncrementalSearch();
  1208. Message.Result = 1;
  1209. return;
  1210. }
  1211. }
  1212. TForm::Dispatch(&Message);
  1213. }
  1214. //---------------------------------------------------------------------------
  1215. void __fastcall TLoginDialog::Dispatch(void * Message)
  1216. {
  1217. TMessage * M = reinterpret_cast<TMessage*>(Message);
  1218. assert(M);
  1219. if (M->Msg == CM_DIALOGKEY)
  1220. {
  1221. CMDialogKey(*((TWMKeyDown *)Message));
  1222. }
  1223. else if (M->Msg == WM_LOCALE_CHANGE)
  1224. {
  1225. if (M->WParam == 0)
  1226. {
  1227. SaveConfiguration();
  1228. SaveState();
  1229. PersistNewSiteIfNeeded();
  1230. // restore sizes to design-time state,
  1231. // otherwise layout is lost while reloading the form
  1232. BasicGroup->Height = FBasicGroupBaseHeight + BasicSshPanel->Height + BasicFtpPanel->Height;
  1233. SetBounds(Left, Top, FOriginalSize.cx, FOriginalSize.cy);
  1234. assert(FSystemSettings);
  1235. RevokeSystemSettings(this, FSystemSettings);
  1236. FSystemSettings = NULL;
  1237. Hide();
  1238. }
  1239. else
  1240. {
  1241. FLocaleChanging = true;
  1242. try
  1243. {
  1244. Show();
  1245. }
  1246. __finally
  1247. {
  1248. FLocaleChanging = false;
  1249. }
  1250. }
  1251. }
  1252. else
  1253. {
  1254. TForm::Dispatch(Message);
  1255. }
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. void __fastcall TLoginDialog::SetDefaultSessionActionExecute(
  1259. TObject * /*Sender*/)
  1260. {
  1261. if (MessageDialog(LoadStr(SET_DEFAULT_SESSION_SETTINGS), qtConfirmation,
  1262. qaOK | qaCancel, HELP_SESSION_SAVE_DEFAULT) == qaOK)
  1263. {
  1264. std::auto_ptr<TSessionData> SessionData(new TSessionData(L""));
  1265. SaveSession(SessionData.get());
  1266. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData.get());
  1267. StoredSessions->DefaultSettings = SessionData.get();
  1268. Default();
  1269. }
  1270. }
  1271. //---------------------------------------------------------------------------
  1272. void __fastcall TLoginDialog::ToolsMenuButtonClick(TObject * /*Sender*/)
  1273. {
  1274. MenuPopup(ToolsPopupMenu, ToolsMenuButton);
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. void __fastcall TLoginDialog::DesktopIconActionExecute(TObject * /*Sender*/)
  1278. {
  1279. TTreeNode * Node = SessionTree->Selected;
  1280. UnicodeString Message;
  1281. UnicodeString Name;
  1282. UnicodeString AdditionalParams;
  1283. int IconIndex = 0;
  1284. if (IsSiteNode(Node))
  1285. {
  1286. Name = GetNodeSession(Node)->Name;
  1287. Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT, (Name));
  1288. AdditionalParams = L"/UploadIfAny";
  1289. IconIndex = SITE_ICON;
  1290. }
  1291. else if (IsFolderNode(Node))
  1292. {
  1293. Name = SessionNodePath(SessionTree->Selected);
  1294. Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT_FOLDER, (Name));
  1295. IconIndex = SITE_FOLDER_ICON;
  1296. }
  1297. else if (IsWorkspaceNode(Node))
  1298. {
  1299. Name = SessionNodePath(SessionTree->Selected);
  1300. Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT_WORKSPACE, (Name));
  1301. IconIndex = WORKSPACE_ICON;
  1302. }
  1303. else
  1304. {
  1305. FAIL;
  1306. }
  1307. if (MessageDialog(Message, qtConfirmation, qaYes | qaNo, HELP_CREATE_SHORTCUT) == qaYes)
  1308. {
  1309. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, IconIndex);
  1310. }
  1311. }
  1312. //---------------------------------------------------------------------------
  1313. void __fastcall TLoginDialog::SendToHookActionExecute(TObject * /*Sender*/)
  1314. {
  1315. assert(IsSiteNode(SessionTree->Selected));
  1316. assert(SelectedSession != NULL);
  1317. if (MessageDialog(FMTLOAD(CONFIRM_CREATE_SENDTO, (SelectedSession->Name)),
  1318. qtConfirmation, qaYes | qaNo, HELP_CREATE_SENDTO) == qaYes)
  1319. {
  1320. CreateDesktopSessionShortCut(SelectedSession->Name,
  1321. FMTLOAD(SESSION_SENDTO_HOOK_NAME, (SelectedSession->LocalName)),
  1322. L"/Upload", CSIDL_SENDTO, SITE_ICON);
  1323. }
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. bool __fastcall TLoginDialog::HasNodeAnySession(TTreeNode * Node, bool NeedCanLogin)
  1327. {
  1328. bool Result = false;
  1329. TTreeNode * ANode = Node->GetNext();
  1330. while (!Result && (ANode != NULL) && ANode->HasAsParent(Node))
  1331. {
  1332. Result =
  1333. IsSessionNode(ANode) &&
  1334. (!NeedCanLogin || FStoredSessions->CanLogin(GetNodeSession(ANode)));
  1335. ANode = ANode->GetNext();
  1336. }
  1337. return Result;
  1338. }
  1339. //---------------------------------------------------------------------------
  1340. void __fastcall TLoginDialog::SessionTreeCustomDrawItem(
  1341. TCustomTreeView * Sender, TTreeNode * Node, TCustomDrawState State,
  1342. bool & DefaultDraw)
  1343. {
  1344. TFontStyles Styles = Sender->Canvas->Font->Style;
  1345. if (IsSessionNode(Node) && GetNodeSession(Node)->Special)
  1346. {
  1347. Styles = Styles << fsBold << fsUnderline;
  1348. }
  1349. else
  1350. {
  1351. Styles = Styles >> fsBold >> fsUnderline;
  1352. }
  1353. if (State.Empty() && !Node->DropTarget)
  1354. {
  1355. if (IsFolderOrWorkspaceNode(Node))
  1356. {
  1357. if (!HasNodeAnySession(Node))
  1358. {
  1359. Sender->Canvas->Font->Color = clGrayText;
  1360. }
  1361. }
  1362. else if (ALWAYS_TRUE(IsSessionNode(Node)))
  1363. {
  1364. TSessionData * Data = GetNodeSession(Node);
  1365. if (Data->Color != 0)
  1366. {
  1367. Sender->Canvas->Brush->Color = (TColor)Data->Color;
  1368. }
  1369. }
  1370. }
  1371. Sender->Canvas->Font->Style = Styles;
  1372. DefaultDraw = true;
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. void __fastcall TLoginDialog::CheckForUpdatesActionExecute(TObject * /*Sender*/)
  1376. {
  1377. CheckForUpdates(false);
  1378. }
  1379. //---------------------------------------------------------------------------
  1380. void __fastcall TLoginDialog::HelpButtonClick(TObject * /*Sender*/)
  1381. {
  1382. FormHelp(this);
  1383. }
  1384. //---------------------------------------------------------------------------
  1385. void __fastcall TLoginDialog::FormCloseQuery(TObject * /*Sender*/,
  1386. bool & /*CanClose*/)
  1387. {
  1388. if (ModalResult != mrCancel)
  1389. {
  1390. SaveDataList(FDataList);
  1391. }
  1392. }
  1393. //---------------------------------------------------------------------------
  1394. void __fastcall TLoginDialog::SessionTreeEditing(TObject * /*Sender*/,
  1395. TTreeNode * Node, bool & AllowEdit)
  1396. {
  1397. assert(!FRenaming);
  1398. AllowEdit =
  1399. IsFolderOrWorkspaceNode(Node) ||
  1400. (ALWAYS_TRUE(IsSiteNode(Node)) && !GetNodeSession(Node)->Special);
  1401. FRenaming = AllowEdit;
  1402. UpdateControls();
  1403. }
  1404. //---------------------------------------------------------------------------
  1405. void __fastcall TLoginDialog::RenameSessionActionExecute(TObject * /*Sender*/)
  1406. {
  1407. if (SessionTree->Selected != NULL)
  1408. {
  1409. // would be more appropriate in SessionTreeEditing, but it does not work there
  1410. ResetSitesIncrementalSearch();
  1411. SessionTree->Selected->EditText();
  1412. }
  1413. }
  1414. //---------------------------------------------------------------------------
  1415. void __fastcall TLoginDialog::CheckDuplicateFolder(TTreeNode * Parent,
  1416. UnicodeString Text, TTreeNode * Node)
  1417. {
  1418. TTreeNode * ANode =
  1419. ((Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1420. Parent->getFirstChild());
  1421. // note that we allow folder with the same name as existing session
  1422. // on the same level (see also AddSession)
  1423. while ((ANode != NULL) &&
  1424. ((ANode == Node) || IsSessionNode(ANode) || !AnsiSameText(ANode->Text, Text)))
  1425. {
  1426. ANode = ANode->getNextSibling();
  1427. }
  1428. if (ANode != NULL)
  1429. {
  1430. throw Exception(FMTLOAD(LOGIN_DUPLICATE_SESSION_FOLDER_WORKSPACE, (Text)));
  1431. }
  1432. }
  1433. //---------------------------------------------------------------------------
  1434. void __fastcall TLoginDialog::CheckIsSessionFolder(TTreeNode * Node)
  1435. {
  1436. if ((Node != NULL) && (Node->Parent != NULL))
  1437. {
  1438. CheckIsSessionFolder(Node->Parent);
  1439. }
  1440. if (IsWorkspaceNode(Node))
  1441. {
  1442. throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (SessionNodePath(Node))));
  1443. }
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall TLoginDialog::SessionTreeEdited(TObject * /*Sender*/,
  1447. TTreeNode * Node, UnicodeString & S)
  1448. {
  1449. if (Node->Text != S)
  1450. {
  1451. TSessionData * Session = SelectedSession;
  1452. TSessionData::ValidateName(S);
  1453. if (Session != NULL)
  1454. {
  1455. UnicodeString Path = UnixExtractFilePath(Session->Name) + S;
  1456. SessionNameValidate(Path, Session->Name);
  1457. // remove from storage
  1458. Session->Remove();
  1459. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  1460. // modified, only explicit
  1461. StoredSessions->Save(false, true);
  1462. // the session may be the same, if only letter case has changed
  1463. if (Session != NewSession)
  1464. {
  1465. // if we overwrite existing session, remove the original item
  1466. // (we must not delete the node we are editing)
  1467. TTreeNode * ANode =
  1468. ((Node->Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1469. Node->Parent->getFirstChild());
  1470. while ((ANode != NULL) && (ANode->Data != NewSession))
  1471. {
  1472. ANode = ANode->getNextSibling();
  1473. }
  1474. if (ANode != NULL)
  1475. {
  1476. ANode->Delete();
  1477. }
  1478. Node->Data = NewSession;
  1479. DestroySession(Session);
  1480. }
  1481. }
  1482. else
  1483. {
  1484. CheckDuplicateFolder(Node->Parent, S, Node);
  1485. UnicodeString ParentPath = UnixIncludeTrailingBackslash(SessionNodePath(Node->Parent));
  1486. UnicodeString OldRoot = ParentPath + Node->Text;
  1487. UnicodeString NewRoot = ParentPath + S;
  1488. bool AnySession = false;
  1489. TSortType PrevSortType = SessionTree->SortType;
  1490. // temporarily disable automatic sorting, so that nodes are kept in order
  1491. // while we traverse them. otherwise it may happen that we omit some.
  1492. SessionTree->SortType = Comctrls::stNone;
  1493. try
  1494. {
  1495. TTreeNode * ANode = Node->GetNext();
  1496. while ((ANode != NULL) && ANode->HasAsParent(Node))
  1497. {
  1498. if (IsSessionNode(ANode))
  1499. {
  1500. AnySession = true;
  1501. TSessionData * Session = GetNodeSession(ANode);
  1502. // remove from storage
  1503. Session->Remove();
  1504. UnicodeString Path = Session->Name;
  1505. assert(Path.SubString(1, OldRoot.Length()) == OldRoot);
  1506. Path.Delete(1, OldRoot.Length());
  1507. Path.Insert(NewRoot, 1);
  1508. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  1509. // the session may be the same, if only letter case has changed
  1510. if (NewSession != Session)
  1511. {
  1512. ANode->Data = NewSession;
  1513. DestroySession(Session);
  1514. }
  1515. }
  1516. ANode = ANode->GetNext();
  1517. }
  1518. }
  1519. __finally
  1520. {
  1521. SessionTree->SortType = PrevSortType;
  1522. }
  1523. if (AnySession)
  1524. {
  1525. // modified, only explicit
  1526. StoredSessions->Save(false, true);
  1527. }
  1528. }
  1529. }
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. int __fastcall TLoginDialog::FSProtocolToIndex(TFSProtocol FSProtocol,
  1533. bool & AllowScpFallback)
  1534. {
  1535. if (FSProtocol == fsSFTP)
  1536. {
  1537. AllowScpFallback = true;
  1538. bool Dummy;
  1539. return FSProtocolToIndex(fsSFTPonly, Dummy);
  1540. }
  1541. else
  1542. {
  1543. AllowScpFallback = false;
  1544. for (int Index = 0; Index < TransferProtocolCombo->Items->Count; Index++)
  1545. {
  1546. if (FSOrder[Index] == FSProtocol)
  1547. {
  1548. return Index;
  1549. }
  1550. }
  1551. // SFTP is always present
  1552. return FSProtocolToIndex(fsSFTP, AllowScpFallback);
  1553. }
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. TFSProtocol __fastcall TLoginDialog::IndexToFSProtocol(int Index, bool AllowScpFallback)
  1557. {
  1558. bool InBounds = (Index >= 0) && (Index < static_cast<int>(LENOF(FSOrder)));
  1559. // can be temporary "unselected" while new language is being loaded
  1560. assert(InBounds || (Index == -1));
  1561. TFSProtocol Result = fsSFTP;
  1562. if (InBounds)
  1563. {
  1564. Result = FSOrder[Index];
  1565. if ((Result == fsSFTPonly) && AllowScpFallback)
  1566. {
  1567. Result = fsSFTP;
  1568. }
  1569. }
  1570. return Result;
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. int __fastcall TLoginDialog::FtpsToIndex(TFtps Ftps)
  1574. {
  1575. switch (Ftps)
  1576. {
  1577. default:
  1578. FAIL;
  1579. case ftpsNone:
  1580. return 0;
  1581. case ftpsImplicit:
  1582. return 1;
  1583. case ftpsExplicitSsl:
  1584. return 3;
  1585. case ftpsExplicitTls:
  1586. return 2;
  1587. }
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. TFtps __fastcall TLoginDialog::GetFtps()
  1591. {
  1592. int Index = ((GetFSProtocol(false) == fsWebDAV) ? WebDavsCombo->ItemIndex : FtpsCombo->ItemIndex);
  1593. TFtps Ftps;
  1594. switch (Index)
  1595. {
  1596. default:
  1597. FAIL;
  1598. case 0:
  1599. Ftps = ftpsNone;
  1600. break;
  1601. case 1:
  1602. Ftps = ftpsImplicit;
  1603. break;
  1604. case 2:
  1605. Ftps = ftpsExplicitTls;
  1606. break;
  1607. case 3:
  1608. Ftps = ftpsExplicitSsl;
  1609. break;
  1610. }
  1611. return Ftps;
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. TFSProtocol __fastcall TLoginDialog::GetFSProtocol(bool RequireScpFallbackDistinction)
  1615. {
  1616. bool AllowScpFallback = false;
  1617. if (RequireScpFallbackDistinction && ALWAYS_TRUE(FSessionData != NULL))
  1618. {
  1619. FSProtocolToIndex(FSessionData->FSProtocol, AllowScpFallback);
  1620. }
  1621. return IndexToFSProtocol(TransferProtocolCombo->ItemIndex, AllowScpFallback);
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. int __fastcall TLoginDialog::DefaultPort()
  1625. {
  1626. TFSProtocol FSProtocol = GetFSProtocol(false);
  1627. TFtps Ftps = GetFtps();
  1628. int Result;
  1629. switch (FSProtocol)
  1630. {
  1631. case fsFTP:
  1632. if (Ftps == ftpsImplicit)
  1633. {
  1634. Result = FtpsImplicitPortNumber;
  1635. }
  1636. else
  1637. {
  1638. Result = FtpPortNumber;
  1639. }
  1640. break;
  1641. case fsWebDAV:
  1642. if (Ftps == ftpsNone)
  1643. {
  1644. Result = HTTPPortNumber;
  1645. }
  1646. else
  1647. {
  1648. Result = HTTPSPortNumber;
  1649. }
  1650. break;
  1651. default:
  1652. if (IsSshProtocol(FSProtocol))
  1653. {
  1654. Result = SshPortNumber;
  1655. }
  1656. else
  1657. {
  1658. FAIL;
  1659. Result = -1;
  1660. }
  1661. break;
  1662. }
  1663. return Result;
  1664. }
  1665. //---------------------------------------------------------------------------
  1666. void __fastcall TLoginDialog::TransferProtocolComboChange(TObject * Sender)
  1667. {
  1668. int ADefaultPort = DefaultPort();
  1669. if (!NoUpdate && FUpdatePortWithProtocol)
  1670. {
  1671. NoUpdate++;
  1672. try
  1673. {
  1674. if (PortNumberEdit->AsInteger == FDefaultPort)
  1675. {
  1676. PortNumberEdit->AsInteger = ADefaultPort;
  1677. }
  1678. }
  1679. __finally
  1680. {
  1681. NoUpdate--;
  1682. }
  1683. }
  1684. FDefaultPort = ADefaultPort;
  1685. DataChange(Sender);
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. void __fastcall TLoginDialog::NavigationTreeCollapsing(
  1689. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & AllowCollapse)
  1690. {
  1691. AllowCollapse = false;
  1692. }
  1693. //---------------------------------------------------------------------------
  1694. void __fastcall TLoginDialog::SessionTreeExpandedCollapsed(TObject * /*Sender*/,
  1695. TTreeNode * Node)
  1696. {
  1697. UpdateFolderNode(Node);
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. void __fastcall TLoginDialog::SessionTreeCompare(TObject * /*Sender*/,
  1701. TTreeNode * Node1, TTreeNode * Node2, int /*Data*/, int & Compare)
  1702. {
  1703. bool Node1IsNewSite = IsNewSiteNode(Node1);
  1704. bool Node2IsNewSite = IsNewSiteNode(Node2);
  1705. bool Node1IsWorkspace = IsWorkspaceNode(Node1);
  1706. bool Node2IsWorkspace = IsWorkspaceNode(Node2);
  1707. bool Node1IsFolder = IsFolderNode(Node1);
  1708. bool Node2IsFolder = IsFolderNode(Node2);
  1709. if (Node1IsNewSite && !Node2IsNewSite)
  1710. {
  1711. Compare = -1;
  1712. }
  1713. else if (!Node1IsNewSite && Node2IsNewSite)
  1714. {
  1715. Compare = 1;
  1716. }
  1717. else if (Node1IsWorkspace && !Node2IsWorkspace)
  1718. {
  1719. Compare = -1;
  1720. }
  1721. else if (!Node1IsWorkspace && Node2IsWorkspace)
  1722. {
  1723. Compare = 1;
  1724. }
  1725. else if (Node1IsFolder && !Node2IsFolder)
  1726. {
  1727. Compare = -1;
  1728. }
  1729. else if (!Node1IsFolder && Node2IsFolder)
  1730. {
  1731. Compare = 1;
  1732. }
  1733. else if (Node1IsWorkspace || Node1IsFolder)
  1734. {
  1735. Compare = AnsiCompareText(Node1->Text, Node2->Text);
  1736. }
  1737. else
  1738. {
  1739. Compare = NamedObjectSortProc(Node1->Data, Node2->Data);
  1740. }
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. void __fastcall TLoginDialog::NewSessionFolderInputDialogInitialize(
  1744. TObject * /*Sender*/, TInputDialogData * Data)
  1745. {
  1746. TEdit * Edit = Data->Edit;
  1747. int P = Edit->Text.LastDelimiter(L"/");
  1748. if (P > 0)
  1749. {
  1750. Edit->SetFocus();
  1751. Edit->SelStart = P;
  1752. Edit->SelLength = Edit->Text.Length() - P;
  1753. }
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. TTreeNode * __fastcall TLoginDialog::SessionFolderNode(TTreeNode * Node)
  1757. {
  1758. TTreeNode * Parent;
  1759. if (IsSessionNode(Node))
  1760. {
  1761. Parent = Node->Parent;
  1762. }
  1763. else if (IsFolderNode(Node))
  1764. {
  1765. Parent = Node;
  1766. }
  1767. else if (IsWorkspaceNode(Node))
  1768. {
  1769. Parent = NULL;
  1770. }
  1771. else
  1772. {
  1773. assert(Node == NULL);
  1774. Parent = NULL;
  1775. }
  1776. return Parent;
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. TTreeNode * __fastcall TLoginDialog::CurrentSessionFolderNode()
  1780. {
  1781. return SessionFolderNode(SessionTree->Selected);
  1782. }
  1783. //---------------------------------------------------------------------------
  1784. void __fastcall TLoginDialog::NewSessionFolderActionExecute(
  1785. TObject * /*Sender*/)
  1786. {
  1787. UnicodeString Name =
  1788. UnixIncludeTrailingBackslash(SessionNodePath(CurrentSessionFolderNode())) +
  1789. LoadStr(NEW_FOLDER);
  1790. if (InputDialog(LoadStr(LOGIN_NEW_SESSION_FOLDER_CAPTION),
  1791. LoadStr(LOGIN_NEW_SESSION_FOLDER_PROMPT), Name, HELP_NEW_SESSION_FOLDER,
  1792. NULL, true, NewSessionFolderInputDialogInitialize))
  1793. {
  1794. Name = UnixExcludeTrailingBackslash(Name);
  1795. if (!Name.IsEmpty())
  1796. {
  1797. TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Name), true, false);
  1798. // this does not prevent creation of subfolder under workspace,
  1799. // if user creates more levels at once (but it does not show up anyway)
  1800. CheckIsSessionFolder(Parent);
  1801. CheckDuplicateFolder(Parent, UnixExtractFileName(Name), NULL);
  1802. TTreeNode * Node = AddSessionPath(Name, true, false);
  1803. SessionTree->Selected = Node;
  1804. Node->MakeVisible();
  1805. }
  1806. }
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. bool __fastcall TLoginDialog::SessionAllowDrop(TTreeNode * DropTarget)
  1810. {
  1811. return
  1812. (SessionTree->Selected != NULL) &&
  1813. (SessionTree->Selected->Parent != DropTarget) &&
  1814. IsFolderNode(DropTarget) || IsSiteNode(DropTarget);
  1815. }
  1816. //---------------------------------------------------------------------------
  1817. void __fastcall TLoginDialog::SessionTreeProc(TMessage & AMessage)
  1818. {
  1819. if (AMessage.Msg == CM_DRAG)
  1820. {
  1821. TCMDrag & Message = reinterpret_cast<TCMDrag &>(AMessage);
  1822. // reimplement dmDragMove to avoid TCustomTreeView.DoDragOver,
  1823. // which resets DropTarget to pointed-to node
  1824. // (note that this disables OnDragOver event handler)
  1825. if ((Message.DragMessage == dmDragMove) ||
  1826. (Message.DragMessage == dmDragEnter) ||
  1827. (Message.DragMessage == dmDragLeave))
  1828. {
  1829. if (Message.DragMessage != dmDragMove)
  1830. {
  1831. // must call it at least for dmDragLeave, because it does some cleanup,
  1832. // but we need to override result below, as it defaults to "not accepted"
  1833. FOldSessionTreeProc(AMessage);
  1834. }
  1835. TDragControlObject * DragObject = dynamic_cast<TDragControlObject *>(Message.DragRec->Source);
  1836. if ((DragObject != NULL) && (DragObject->Control == SessionTree))
  1837. {
  1838. TPoint P = SessionTree->ScreenToClient(Message.DragRec->Pos);
  1839. TTreeNode * Node = SessionTree->GetNodeAt(P.x, P.y);
  1840. TTreeNode * DropTarget =
  1841. IsWorkspaceNode(Node) ? Node : SessionFolderNode(Node);
  1842. if (!SessionAllowDrop(DropTarget))
  1843. {
  1844. DropTarget = NULL;
  1845. Message.Result = 0;
  1846. }
  1847. else
  1848. {
  1849. Message.Result = 1;
  1850. }
  1851. if (Message.DragMessage == dmDragMove)
  1852. {
  1853. SessionTree->DropTarget = DropTarget;
  1854. }
  1855. FScrollOnDragOver->DragOver(P);
  1856. }
  1857. else
  1858. {
  1859. Message.Result = 0;
  1860. }
  1861. }
  1862. else
  1863. {
  1864. FOldSessionTreeProc(AMessage);
  1865. }
  1866. }
  1867. else
  1868. {
  1869. FOldSessionTreeProc(AMessage);
  1870. }
  1871. }
  1872. //---------------------------------------------------------------------------
  1873. void __fastcall TLoginDialog::SessionTreeStartDrag(TObject * /*Sender*/,
  1874. TDragObject *& /*DragObject*/)
  1875. {
  1876. assert(SessionTree->Selected != NULL);
  1877. // neither session folders/workspaces, nor special sessions can be dragged
  1878. if ((SessionTree->Selected == NULL) ||
  1879. IsFolderOrWorkspaceNode(SessionTree->Selected) ||
  1880. IsNewSiteNode(SessionTree->Selected) ||
  1881. (IsSiteNode(SessionTree->Selected) && SelectedSession->Special))
  1882. {
  1883. Abort();
  1884. }
  1885. FScrollOnDragOver->StartDrag();
  1886. }
  1887. //---------------------------------------------------------------------------
  1888. void __fastcall TLoginDialog::SessionTreeDragDrop(TObject * Sender,
  1889. TObject * Source, int /*X*/, int /*Y*/)
  1890. {
  1891. TTreeNode * DropTarget = SessionTree->DropTarget;
  1892. if (ALWAYS_TRUE((Sender == Source) && SessionAllowDrop(DropTarget)))
  1893. {
  1894. TSessionData * Session = SelectedSession;
  1895. UnicodeString Path =
  1896. UnixIncludeTrailingBackslash(SessionNodePath(DropTarget)) +
  1897. UnixExtractFileName(Session->SessionName);
  1898. SessionNameValidate(Path, Session->SessionName);
  1899. // remove from storage
  1900. Session->Remove();
  1901. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  1902. // modified, only explicit
  1903. StoredSessions->Save(false, true);
  1904. // this should aways be the case
  1905. if (ALWAYS_TRUE(Session != NewSession))
  1906. {
  1907. TTreeNode * Node = SessionTree->Selected;
  1908. // look for overwritten node (if any)
  1909. TTreeNode * ANode = SessionTree->Items->GetFirstNode();
  1910. while (ANode != NULL)
  1911. {
  1912. if (ANode->Data == NewSession)
  1913. {
  1914. ANode->Delete();
  1915. break;
  1916. }
  1917. ANode = ANode->GetNext();
  1918. }
  1919. Node->MoveTo(DropTarget, naAddChild);
  1920. Node->Data = NewSession;
  1921. // try to make both visible
  1922. if (DropTarget != NULL)
  1923. {
  1924. DropTarget->MakeVisible();
  1925. }
  1926. Node->MakeVisible();
  1927. DestroySession(Session);
  1928. }
  1929. }
  1930. }
  1931. //---------------------------------------------------------------------------
  1932. UnicodeString __fastcall TLoginDialog::GetFolderOrWorkspaceContents(
  1933. TTreeNode * Node, const UnicodeString & Indent, const UnicodeString & CommonRoot)
  1934. {
  1935. UnicodeString Contents;
  1936. UnicodeString Path = SessionNodePath(Node);
  1937. std::auto_ptr<TStrings> Names(FStoredSessions->GetFolderOrWorkspaceList(Path));
  1938. for (int Index = 0; Index < Names->Count; Index++)
  1939. {
  1940. UnicodeString Name = Names->Strings[Index];
  1941. if (StartsStr(CommonRoot, Name))
  1942. {
  1943. Name.Delete(1, CommonRoot.Length());
  1944. }
  1945. Contents += Indent + Name + L"\n";
  1946. }
  1947. return Contents;
  1948. }
  1949. //---------------------------------------------------------------------------
  1950. void __fastcall TLoginDialog::SessionTreeMouseMove(TObject * /*Sender*/,
  1951. TShiftState /*Shift*/, int X, int Y)
  1952. {
  1953. TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
  1954. THitTests HitTest = SessionTree->GetHitTestInfoAt(X, Y);
  1955. if (Node != FHintNode)
  1956. {
  1957. Application->CancelHint();
  1958. UnicodeString Hint;
  1959. if (HitTest.Contains(htOnItem) || HitTest.Contains(htOnIcon) ||
  1960. HitTest.Contains(htOnLabel) || HitTest.Contains(htOnStateIcon))
  1961. {
  1962. FHintNode = Node;
  1963. if (IsSiteNode(Node))
  1964. {
  1965. Hint = GetNodeSession(Node)->InfoTip;
  1966. }
  1967. else if (IsWorkspaceNode(Node))
  1968. {
  1969. UnicodeString Path = SessionNodePath(Node);
  1970. Hint =
  1971. FMTLOAD(WORKSPACE_INFO_TIP, (Path)) + L"\n" +
  1972. // trim the trailing new line
  1973. TrimRight(GetFolderOrWorkspaceContents(Node, L" ", UnicodeString()));
  1974. }
  1975. else
  1976. {
  1977. Hint = L"";
  1978. }
  1979. }
  1980. else
  1981. {
  1982. FHintNode = NULL;
  1983. Hint = L"";
  1984. }
  1985. SessionTree->Hint = Hint;
  1986. }
  1987. }
  1988. //---------------------------------------------------------------------------
  1989. void __fastcall TLoginDialog::SessionTreeEndDrag(TObject * /*Sender*/,
  1990. TObject * /*Target*/, int /*X*/, int /*Y*/)
  1991. {
  1992. FScrollOnDragOver->EndDrag();
  1993. }
  1994. //---------------------------------------------------------------------------
  1995. void __fastcall TLoginDialog::AnonymousLoginCheckClick(TObject * /*Sender*/)
  1996. {
  1997. if (AnonymousLoginCheck->Checked)
  1998. {
  1999. UserNameEdit->Text = AnonymousUserName;
  2000. PasswordEdit->Text = AnonymousPassword;
  2001. }
  2002. UpdateControls();
  2003. }
  2004. //---------------------------------------------------------------------------
  2005. void __fastcall TLoginDialog::SaveButtonDropDownClick(TObject * /*Sender*/)
  2006. {
  2007. MenuPopup(SaveDropDownMenu, SaveButton);
  2008. }
  2009. //---------------------------------------------------------------------------
  2010. void __fastcall TLoginDialog::SessionTreeExpanding(TObject * /*Sender*/,
  2011. TTreeNode * Node, bool & AllowExpansion)
  2012. {
  2013. // to prevent workspace expansion
  2014. AllowExpansion = IsFolderNode(Node);
  2015. }
  2016. //---------------------------------------------------------------------------
  2017. void __fastcall TLoginDialog::ExecuteTool(const UnicodeString & Name)
  2018. {
  2019. UnicodeString Path;
  2020. if (!FindTool(Name, Path) ||
  2021. !ExecuteShell(Path, L""))
  2022. {
  2023. throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Name)));
  2024. }
  2025. }
  2026. //---------------------------------------------------------------------------
  2027. void __fastcall TLoginDialog::RunPageantActionExecute(TObject * /*Sender*/)
  2028. {
  2029. ExecuteTool(PageantTool);
  2030. }
  2031. //---------------------------------------------------------------------------
  2032. void __fastcall TLoginDialog::RunPuttygenActionExecute(TObject * /*Sender*/)
  2033. {
  2034. ExecuteTool(PuttygenTool);
  2035. }
  2036. //---------------------------------------------------------------------------
  2037. void __fastcall TLoginDialog::PortNumberEditChange(TObject * Sender)
  2038. {
  2039. if (!NoUpdate)
  2040. {
  2041. bool WellKnownPort = false;
  2042. TFSProtocol FSProtocol;
  2043. TFtps Ftps = ftpsNone;
  2044. int PortNumber = PortNumberEdit->AsInteger;
  2045. if (PortNumber == SshPortNumber)
  2046. {
  2047. FSProtocol = fsSFTP;
  2048. WellKnownPort = true;
  2049. }
  2050. else if (PortNumber == FtpPortNumber)
  2051. {
  2052. FSProtocol = fsFTP;
  2053. WellKnownPort = true;
  2054. }
  2055. else if (PortNumber == FtpsImplicitPortNumber)
  2056. {
  2057. FSProtocol = fsFTP;
  2058. Ftps = ftpsImplicit;
  2059. WellKnownPort = true;
  2060. }
  2061. else if (PortNumber == HTTPPortNumber)
  2062. {
  2063. FSProtocol = fsWebDAV;
  2064. WellKnownPort = true;
  2065. }
  2066. else if (PortNumber == HTTPSPortNumber)
  2067. {
  2068. FSProtocol = fsWebDAV;
  2069. Ftps = ftpsImplicit;
  2070. WellKnownPort = true;
  2071. }
  2072. if (WellKnownPort)
  2073. {
  2074. bool AllowScpFallback;
  2075. int ProtocolIndex = FSProtocolToIndex(FSProtocol, AllowScpFallback);
  2076. if ((TransferProtocolCombo->ItemIndex == ProtocolIndex) &&
  2077. (GetFtps() == Ftps))
  2078. {
  2079. FUpdatePortWithProtocol = true;
  2080. }
  2081. else
  2082. {
  2083. FUpdatePortWithProtocol = false;
  2084. NoUpdate++;
  2085. try
  2086. {
  2087. TransferProtocolCombo->ItemIndex = ProtocolIndex;
  2088. int FtpsIndex = FtpsToIndex(Ftps);
  2089. FtpsCombo->ItemIndex = FtpsIndex;
  2090. WebDavsCombo->ItemIndex = FtpsIndex;
  2091. }
  2092. __finally
  2093. {
  2094. NoUpdate--;
  2095. }
  2096. }
  2097. }
  2098. }
  2099. DataChange(Sender);
  2100. }
  2101. //---------------------------------------------------------------------------
  2102. UnicodeString __fastcall TLoginDialog::ImportExportIniFilePath()
  2103. {
  2104. UnicodeString PersonalDirectory;
  2105. ::SpecialFolderLocation(CSIDL_PERSONAL, PersonalDirectory);
  2106. UnicodeString FileName = IncludeTrailingBackslash(PersonalDirectory) +
  2107. ExtractFileName(ExpandEnvironmentVariables(Configuration->IniFileStorageName));
  2108. return FileName;
  2109. }
  2110. //---------------------------------------------------------------------------
  2111. void __fastcall TLoginDialog::ExportActionExecute(TObject * /*Sender*/)
  2112. {
  2113. UnicodeString FileName = ImportExportIniFilePath();
  2114. if (SaveDialog(LoadStr(EXPORT_CONF_TITLE), LoadStr(EXPORT_CONF_FILTER), L"ini", FileName))
  2115. {
  2116. Configuration->Export(FileName);
  2117. }
  2118. }
  2119. //---------------------------------------------------------------------------
  2120. void __fastcall TLoginDialog::ImportActionExecute(TObject * /*Sender*/)
  2121. {
  2122. if (MessageDialog(LoadStr(IMPORT_CONFIGURATION),
  2123. qtWarning, qaOK | qaCancel, HELP_IMPORT_CONFIGURATION) == qaOK)
  2124. {
  2125. std::auto_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  2126. OpenDialog->Title = LoadStr(IMPORT_CONF_TITLE);
  2127. OpenDialog->Filter = LoadStr(EXPORT_CONF_FILTER);
  2128. OpenDialog->DefaultExt = L"ini";
  2129. OpenDialog->FileName = ImportExportIniFilePath();
  2130. if (OpenDialog->Execute())
  2131. {
  2132. // before the session list gets destroyed
  2133. SessionTree->Items->Clear();
  2134. Configuration->Import(OpenDialog->FileName);
  2135. ReloadSessions();
  2136. }
  2137. }
  2138. }
  2139. //---------------------------------------------------------------------------
  2140. void __fastcall TLoginDialog::ResetSitesIncrementalSearch()
  2141. {
  2142. FSitesIncrementalSearch = L"";
  2143. // this is to prevent active tree node being set back to Sites tab
  2144. // (from UpdateNavigationTree) when we are called from SessionTreeExit,
  2145. // while tab is changing
  2146. if (NoUpdate == 0)
  2147. {
  2148. UpdateControls();
  2149. }
  2150. }
  2151. //---------------------------------------------------------------------------
  2152. bool __fastcall TLoginDialog::SitesIncrementalSearch(const UnicodeString & Text,
  2153. bool SkipCurrent, bool Reverse)
  2154. {
  2155. TTreeNode * Node = SearchSite(Text, false, SkipCurrent, Reverse);
  2156. if (Node == NULL)
  2157. {
  2158. Node = SearchSite(Text, true, SkipCurrent, Reverse);
  2159. if (Node != NULL)
  2160. {
  2161. TTreeNode * Parent = Node->Parent;
  2162. while (Parent != NULL)
  2163. {
  2164. Parent->Expand(false);
  2165. Parent = Parent->Parent;
  2166. }
  2167. }
  2168. }
  2169. bool Result = (Node != NULL);
  2170. if (Result)
  2171. {
  2172. {
  2173. TAutoNestingCounter Guard(FIncrementalSearching);
  2174. SessionTree->Selected = Node;
  2175. }
  2176. FSitesIncrementalSearch = Text;
  2177. // Tab always searches even in collapsed nodes
  2178. TTreeNode * NextNode = SearchSite(Text, true, true, Reverse);
  2179. FSitesIncrementalSearchHaveNext =
  2180. (NextNode != NULL) && (NextNode != Node);
  2181. UpdateControls();
  2182. // make visible only after search panel is shown, what may obscure the node
  2183. Node->MakeVisible();
  2184. }
  2185. return Result;
  2186. }
  2187. //---------------------------------------------------------------------------
  2188. TTreeNode * __fastcall TLoginDialog::GetNextNode(TTreeNode * Node, bool Reverse)
  2189. {
  2190. if (Reverse)
  2191. {
  2192. Node = Node->GetPrev();
  2193. if (Node == NULL)
  2194. {
  2195. // GetLastNode
  2196. // http://stackoverflow.com/questions/6257348/how-should-i-implement-getlastnode-for-ttreenodes
  2197. Node = SessionTree->Items->GetFirstNode();
  2198. TTreeNode * Node2 = Node;
  2199. if (Node2 != NULL)
  2200. {
  2201. do
  2202. {
  2203. Node2 = Node;
  2204. if (Node != NULL)
  2205. {
  2206. Node = Node2->getNextSibling();
  2207. }
  2208. if (Node == NULL)
  2209. {
  2210. Node = Node2->getFirstChild();
  2211. }
  2212. }
  2213. while (Node != NULL);
  2214. }
  2215. Node = Node2;
  2216. }
  2217. }
  2218. else
  2219. {
  2220. Node = Node->GetNext();
  2221. if (Node == NULL)
  2222. {
  2223. Node = SessionTree->Items->GetFirstNode();
  2224. }
  2225. }
  2226. return Node;
  2227. }
  2228. //---------------------------------------------------------------------------
  2229. TTreeNode * __fastcall TLoginDialog::SearchSite(const UnicodeString & Text,
  2230. bool AllowExpanding, bool SkipCurrent, bool Reverse)
  2231. {
  2232. TTreeNode * CurrentNode =
  2233. (SessionTree->Selected != NULL) ? SessionTree->Selected : SessionTree->Items->GetFirstNode();
  2234. if (CurrentNode == NULL)
  2235. {
  2236. return NULL;
  2237. }
  2238. else
  2239. {
  2240. TTreeNode * Node = CurrentNode;
  2241. if (SkipCurrent)
  2242. {
  2243. Node = GetNextNode(Node, Reverse);
  2244. if (Node == NULL)
  2245. {
  2246. return NULL;
  2247. }
  2248. }
  2249. while (true)
  2250. {
  2251. bool Eligible = true;
  2252. TTreeNode * Parent = Node->Parent;
  2253. while (Eligible && (Parent != NULL))
  2254. {
  2255. Eligible =
  2256. IsFolderNode(Parent) &&
  2257. (Parent->Expanded || AllowExpanding);
  2258. Parent = Parent->Parent;
  2259. }
  2260. if (Eligible && ContainsText(Node->Text, Text))
  2261. {
  2262. return Node;
  2263. }
  2264. Node = GetNextNode(Node, Reverse);
  2265. if (Node == CurrentNode)
  2266. {
  2267. return NULL;
  2268. }
  2269. }
  2270. }
  2271. }
  2272. //---------------------------------------------------------------------------
  2273. void __fastcall TLoginDialog::SessionTreeExit(TObject * /*Sender*/)
  2274. {
  2275. ResetSitesIncrementalSearch();
  2276. }
  2277. //---------------------------------------------------------------------------
  2278. void __fastcall TLoginDialog::SessionTreeChanging(TObject * /*Sender*/,
  2279. TTreeNode * /*Node*/, bool & AllowChange)
  2280. {
  2281. if (FEditing &&
  2282. (MessageDialog(LoadStr(LOGIN_CANCEL_EDITING), qtConfirmation, qaOK | qaCancel) == qaCancel))
  2283. {
  2284. AllowChange = false;
  2285. }
  2286. else
  2287. {
  2288. PersistNewSiteIfNeeded();
  2289. FEditing = false;
  2290. UpdateControls();
  2291. }
  2292. }
  2293. //---------------------------------------------------------------------------
  2294. void __fastcall TLoginDialog::PersistNewSiteIfNeeded()
  2295. {
  2296. // Visible: Never try to save data if we did not load any yet,
  2297. // otherwise we get "HostNameEdit" and such
  2298. if (Visible && IsNewSiteNode(SessionTree->Selected))
  2299. {
  2300. SaveSession(FNewSiteData);
  2301. }
  2302. }
  2303. //---------------------------------------------------------------------------
  2304. void __fastcall TLoginDialog::SessionAdvancedActionExecute(TObject * /*Sender*/)
  2305. {
  2306. // If we ever allow showing advanced settings, while read-only,
  2307. // we must make sure that FSessionData actually holds the advanced settings,
  2308. // what is currently does not, in order to avoid master password prompt,
  2309. // while cloning the session data in LoadSession.
  2310. // To implement this, we may delegate the cloning to TWinConfiguration and
  2311. // make use of FDontDecryptPasswords
  2312. if (ALWAYS_TRUE(FSessionData != NULL))
  2313. {
  2314. SaveSession(FSessionData);
  2315. DoSiteAdvancedDialog(FSessionData, FOptions);
  2316. }
  2317. }
  2318. //---------------------------------------------------------------------------
  2319. TPopupMenu * __fastcall TLoginDialog::GetSelectedNodePopupMenu()
  2320. {
  2321. TPopupMenu * PopupMenu = NULL;
  2322. TTreeNode * Selected = SessionTree->Selected;
  2323. if (IsNewSiteNode(Selected))
  2324. {
  2325. PopupMenu = ManageNewSitePopupMenu;
  2326. }
  2327. else if (IsSiteNode(Selected))
  2328. {
  2329. PopupMenu = ManageSitePopupMenu;
  2330. }
  2331. else if (IsFolderNode(Selected))
  2332. {
  2333. PopupMenu = ManageFolderPopupMenu;
  2334. }
  2335. else if (IsWorkspaceNode(Selected))
  2336. {
  2337. PopupMenu = ManageWorkspacePopupMenu;
  2338. }
  2339. return NOT_NULL(PopupMenu);
  2340. }
  2341. //---------------------------------------------------------------------------
  2342. void __fastcall TLoginDialog::ManageButtonClick(TObject * /*Sender*/)
  2343. {
  2344. MenuPopup(GetSelectedNodePopupMenu(), ManageButton);
  2345. }
  2346. //---------------------------------------------------------------------------
  2347. void __fastcall TLoginDialog::SessionTreeMouseDown(TObject * /*Sender*/,
  2348. TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  2349. {
  2350. TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
  2351. if ((Button == mbRight) && (Node != NULL))
  2352. {
  2353. SessionTree->Selected = Node;
  2354. }
  2355. }
  2356. //---------------------------------------------------------------------------
  2357. void __fastcall TLoginDialog::SessionTreeContextPopup(TObject * /*Sender*/,
  2358. TPoint & MousePos, bool & Handled)
  2359. {
  2360. TTreeNode * Node = SessionTree->GetNodeAt(MousePos.X, MousePos.Y);
  2361. // This is mostly to prevent context menu from poping up,
  2362. // while there is prompt to confirm cancelling session edit,
  2363. // when right mouse is clicked on non-selected node
  2364. if (Node != SessionTree->Selected)
  2365. {
  2366. Handled = true;
  2367. }
  2368. else
  2369. {
  2370. if (SessionTree->Selected != NULL)
  2371. {
  2372. SessionTree->PopupMenu = GetSelectedNodePopupMenu();
  2373. if (NOT_NULL(SessionTree->PopupMenu))
  2374. {
  2375. MenuPopup(SessionTree, MousePos, Handled);
  2376. }
  2377. }
  2378. }
  2379. }
  2380. //---------------------------------------------------------------------------
  2381. void __fastcall TLoginDialog::EditCancelActionExecute(TObject * /*Sender*/)
  2382. {
  2383. FEditing = false;
  2384. // reset back to saved settings
  2385. LoadContents();
  2386. UpdateControls();
  2387. // we do not want to see blinking cursor in read-only edit box
  2388. SessionTree->SetFocus();
  2389. }
  2390. //---------------------------------------------------------------------------
  2391. void __fastcall TLoginDialog::AdvancedButtonDropDownClick(TObject * /*Sender*/)
  2392. {
  2393. MenuPopup(SessionAdvancedPopupMenu, AdvancedButton);
  2394. }
  2395. //---------------------------------------------------------------------------