Login.cpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  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 <limits>
  13. #include "WinInterface.h"
  14. #include "Login.h"
  15. #include "GUITools.h"
  16. #include "Tools.h"
  17. #include "Setup.h"
  18. #include "WinConfiguration.h"
  19. #include "ProgParams.h"
  20. #include "WinApi.h"
  21. //---------------------------------------------------------------------
  22. #pragma link "ComboEdit"
  23. #pragma link "PasswordEdit"
  24. #pragma link "UpDownEdit"
  25. #ifndef NO_RESOURCES
  26. #pragma resource "*.dfm"
  27. #endif
  28. //---------------------------------------------------------------------------
  29. const int SiteImageIndex = 1;
  30. const int OpenFolderImageIndex = 2;
  31. const int ClosedFolderImageIndex = 3;
  32. const int WorkspaceImageIndex = 4;
  33. const int NewSiteImageIndex = 6;
  34. const int SiteColorMaskImageIndex = 8;
  35. //---------------------------------------------------------------------------
  36. bool __fastcall DoLoginDialog(TStoredSessionList *SessionList, TList * DataList, TForm * LinkedForm)
  37. {
  38. DebugAssert(DataList != NULL);
  39. TLoginDialog * LoginDialog = SafeFormCreate<TLoginDialog>();
  40. bool Result;
  41. try
  42. {
  43. LoginDialog->Init(SessionList, LinkedForm);
  44. Result = LoginDialog->Execute(DataList);
  45. }
  46. __finally
  47. {
  48. delete LoginDialog;
  49. }
  50. return Result;
  51. }
  52. //---------------------------------------------------------------------
  53. static const TFSProtocol FSOrder[] = { fsSFTPonly, fsSCPonly, fsFTP, fsWebDAV, fsS3 };
  54. //---------------------------------------------------------------------
  55. __fastcall TLoginDialog::TLoginDialog(TComponent* AOwner)
  56. : TForm(AOwner)
  57. {
  58. FNewSiteData = new TSessionData(L"");
  59. FInitialized = false;
  60. FHintNode = NULL;
  61. FScrollOnDragOver = new TTreeViewScrollOnDragOver(SessionTree, true);
  62. FDataList = NULL;
  63. FUpdatePortWithProtocol = true;
  64. FIncrementalSearching = 0;
  65. FSitesIncrementalSearchHaveNext = false;
  66. FEditing = false;
  67. FRenaming = false;
  68. FNewSiteKeepName = false;
  69. FForceNewSite = false;
  70. FLoading = false;
  71. FSortEnablePending = false;
  72. FSiteSearch = ssSiteName;
  73. FLinkedForm = NULL;
  74. FPrevPos = TPoint(std::numeric_limits<LONG>::min(), std::numeric_limits<LONG>::min());
  75. // we need to make sure that window procedure is set asap
  76. // (so that CM_SHOWINGCHANGED handling is applied)
  77. UseSystemSettingsPre(this);
  78. FBasicGroupBaseHeight = BasicGroup->Height - BasicSshPanel->Height - BasicFtpPanel->Height;
  79. FNoteGroupOffset = NoteGroup->Top - (BasicGroup->Top + BasicGroup->Height);
  80. FUserNameLabel = UserNameLabel->Caption;
  81. FPasswordLabel = PasswordLabel->Caption;
  82. FSiteButtonsPadding = SitesPanel->ClientHeight - ToolsMenuButton->Top - ToolsMenuButton->Height;
  83. HideComponentsPanel(this);
  84. }
  85. //---------------------------------------------------------------------
  86. __fastcall TLoginDialog::~TLoginDialog()
  87. {
  88. delete FScrollOnDragOver;
  89. delete FNewSiteData;
  90. InvalidateSessionData();
  91. }
  92. //---------------------------------------------------------------------
  93. void __fastcall TLoginDialog::InvalidateSessionData()
  94. {
  95. delete FSessionData;
  96. FSessionData = NULL;
  97. }
  98. //---------------------------------------------------------------------
  99. void __fastcall TLoginDialog::Init(TStoredSessionList *SessionList, TForm * LinkedForm)
  100. {
  101. FStoredSessions = SessionList;
  102. FLinkedForm = LinkedForm;
  103. LoadSessions();
  104. UnicodeString Dummy;
  105. RunPageantAction->Visible = FindTool(PageantTool, Dummy);
  106. RunPuttygenAction->Visible = FindTool(PuttygenTool, Dummy);
  107. UpdateControls();
  108. }
  109. //---------------------------------------------------------------------
  110. void __fastcall TLoginDialog::InitControls()
  111. {
  112. if (SessionTree->WindowProc != SessionTreeProc)
  113. {
  114. FOldSessionTreeProc = SessionTree->WindowProc;
  115. SessionTree->WindowProc = SessionTreeProc;
  116. }
  117. int FtpsNoneIndex = FtpsToIndex(ftpsNone);
  118. int FtpsImplicitIndex = FtpsToIndex(ftpsImplicit);
  119. FtpsCombo->Items->Strings[FtpsImplicitIndex] = LoadStr(FTPS_IMPLICIT);
  120. FtpsCombo->Items->Strings[FtpsToIndex(ftpsExplicitTls)] = LoadStr(FTPS_EXPLICIT);
  121. WebDavsCombo->Items->Strings[FtpsNoneIndex] = FtpsCombo->Items->Strings[FtpsNoneIndex];
  122. WebDavsCombo->Items->Strings[FtpsImplicitIndex] = FtpsCombo->Items->Strings[FtpsImplicitIndex];
  123. BasicSshPanel->Top = BasicFtpPanel->Top;
  124. SitesIncrementalSearchLabel->AutoSize = false;
  125. SitesIncrementalSearchLabel->Left = SessionTree->Left;
  126. SitesIncrementalSearchLabel->Width = SessionTree->Width;
  127. SitesIncrementalSearchLabel->Top = SessionTree->BoundsRect.Bottom - SitesIncrementalSearchLabel->Height;
  128. SitesIncrementalSearchLabel->Visible = false;
  129. ReadOnlyControl(TransferProtocolView);
  130. ReadOnlyControl(EncryptionView);
  131. ReadOnlyControl(NoteMemo);
  132. MenuButton(ToolsMenuButton);
  133. MenuButton(ManageButton);
  134. FixButtonImage(LoginButton);
  135. CenterButtonImage(LoginButton);
  136. SelectScaledImageList(SessionImageList);
  137. // have to recreate the site images
  138. UpdateNodeImages();
  139. SelectScaledImageList(ActionImageList);
  140. GenerateButtonImages();
  141. if (SessionTree->Items->Count > 0)
  142. {
  143. SetNewSiteNodeLabel();
  144. }
  145. }
  146. //---------------------------------------------------------------------
  147. void __fastcall TLoginDialog::GenerateButtonImages()
  148. {
  149. // Generate button images.
  150. // The button does not support alpha channel,
  151. // so we have to copy the PNG's to BMP's and use plain transparent color
  152. FButtonImageList.reset(new TImageList(this));
  153. FButtonImageList->SetSize(ActionImageList->Width, ActionImageList->Height);
  154. LoginButton->Images = FButtonImageList.get();
  155. LoginButton->ImageIndex = AddLoginButtonImage(true);
  156. LoginButton->DisabledImageIndex = AddLoginButtonImage(false);
  157. }
  158. //---------------------------------------------------------------------
  159. int __fastcall TLoginDialog::AddLoginButtonImage(bool Enabled)
  160. {
  161. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  162. Bitmap->SetSize(ActionImageList->Width, ActionImageList->Height);
  163. ActionImageList->Draw(Bitmap->Canvas, 0, 0, LoginAction->ImageIndex, Enabled);
  164. const TColor TransparentColor = clFuchsia;
  165. // 16x16 version does not have any background
  166. if (Bitmap->Canvas->Pixels[0][0] == clWhite)
  167. {
  168. // A background is white, but there's also white used on the image itself.
  169. // So we first replace the background white with a unique color,
  170. // setting it as a transparent later.
  171. // This is obviously a hack specific to this particular image.
  172. Bitmap->Canvas->Brush->Color = TransparentColor;
  173. Bitmap->Canvas->FloodFill(0, 0, Bitmap->Canvas->Pixels[0][0], fsSurface);
  174. }
  175. return FButtonImageList->AddMasked(Bitmap.get(), TransparentColor);
  176. }
  177. //---------------------------------------------------------------------
  178. void __fastcall TLoginDialog::Init()
  179. {
  180. FInitialized = true;
  181. UseSystemSettingsPost(this);
  182. Caption = FormatFormCaption(this, Caption);
  183. InitControls();
  184. ReadOnlyControl(ContentsNameEdit);
  185. ReadOnlyControl(ContentsMemo);
  186. if (DebugAlwaysFalse(SessionTree->Items->Count == 0) ||
  187. ((SessionTree->Items->Count == 1) &&
  188. DebugAlwaysTrue(IsNewSiteNode(SessionTree->Items->GetFirstNode()))) ||
  189. FForceNewSite)
  190. {
  191. ActiveControl = HostNameEdit;
  192. }
  193. else
  194. {
  195. ActiveControl = SessionTree;
  196. }
  197. UpdateControls();
  198. }
  199. //---------------------------------------------------------------------
  200. TTreeNode * __fastcall TLoginDialog::AddSessionPath(UnicodeString Path,
  201. bool CanCreate, bool IsWorkspace)
  202. {
  203. TTreeNode * Parent = NULL;
  204. while (!Path.IsEmpty())
  205. {
  206. UnicodeString Folder = CutToChar(Path, L'/', false);
  207. TTreeNode * Node =
  208. ((Parent == NULL) ? SessionTree->Items->GetFirstNode() : Parent->getFirstChild());
  209. // note that we allow folder with the same name as existing session
  210. // on the same level (see also SessionTreeEdited)
  211. while ((Node != NULL) && (IsSessionNode(Node) || !AnsiSameText(Node->Text, Folder)))
  212. {
  213. Node = Node->getNextSibling();
  214. }
  215. if (Node == NULL)
  216. {
  217. if (!CanCreate)
  218. {
  219. return NULL;
  220. }
  221. else
  222. {
  223. TTreeNode * AParent = Parent;
  224. Parent = SessionTree->Items->AddChild(Parent, Folder);
  225. // once workspace, forever workspace
  226. if (!IsWorkspaceNode(Parent))
  227. {
  228. if (IsWorkspace)
  229. {
  230. SetNodeImage(Parent, WorkspaceImageIndex);
  231. }
  232. else
  233. {
  234. UpdateFolderNode(Parent);
  235. }
  236. }
  237. // optimization
  238. if (!FLoading)
  239. {
  240. // folders seem not to be sorted automatically (not having set the data property)
  241. if (AParent == NULL)
  242. {
  243. SessionTree->Items->AlphaSort();
  244. }
  245. else
  246. {
  247. AParent->AlphaSort();
  248. }
  249. }
  250. }
  251. }
  252. else
  253. {
  254. Parent = Node;
  255. }
  256. }
  257. return Parent;
  258. }
  259. //---------------------------------------------------------------------
  260. bool __fastcall TLoginDialog::IsFolderNode(TTreeNode * Node)
  261. {
  262. return (Node != NULL) && (Node->Data == NULL) && (Node->ImageIndex != WorkspaceImageIndex);
  263. }
  264. //---------------------------------------------------------------------
  265. bool __fastcall TLoginDialog::IsWorkspaceNode(TTreeNode * Node)
  266. {
  267. return (Node != NULL) && (Node->Data == NULL) && (Node->ImageIndex == WorkspaceImageIndex);
  268. }
  269. //---------------------------------------------------------------------
  270. bool __fastcall TLoginDialog::IsFolderOrWorkspaceNode(TTreeNode * Node)
  271. {
  272. return (Node != NULL) && (Node->Data == NULL);
  273. }
  274. //---------------------------------------------------------------------
  275. bool __fastcall TLoginDialog::IsSiteNode(TTreeNode * Node)
  276. {
  277. return (Node != NULL) && (Node->Data != NULL) && (Node->Data != FNewSiteData);
  278. }
  279. //---------------------------------------------------------------------
  280. bool __fastcall TLoginDialog::IsNewSiteNode(TTreeNode * Node)
  281. {
  282. return (Node != NULL) && (Node->Data != NULL) && (Node->Data == FNewSiteData);
  283. }
  284. //---------------------------------------------------------------------
  285. bool __fastcall TLoginDialog::IsSessionNode(TTreeNode * Node)
  286. {
  287. return (Node != NULL) && (Node->Data != NULL);
  288. }
  289. //---------------------------------------------------------------------
  290. TSessionData * __fastcall TLoginDialog::GetNodeSession(TTreeNode * Node)
  291. {
  292. return DebugNotNull(static_cast<TSessionData *>(Node->Data));
  293. }
  294. //---------------------------------------------------------------------
  295. TTreeNode * __fastcall TLoginDialog::AddSession(TSessionData * Data)
  296. {
  297. TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Data->Name), true, Data->IsWorkspace);
  298. TTreeNode * Node = SessionTree->Items->AddChild(Parent, UnixExtractFileName(Data->Name));
  299. Node->Data = Data;
  300. UpdateNodeImage(Node);
  301. return Node;
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TLoginDialog::UpdateNodeImages()
  305. {
  306. SessionTree->Images->BeginUpdate();
  307. try
  308. {
  309. TTreeNode * Node = SessionTree->Items->GetFirstNode();
  310. while (Node != NULL)
  311. {
  312. if (IsSiteNode(Node))
  313. {
  314. UpdateNodeImage(Node);
  315. }
  316. Node = Node->GetNext();
  317. }
  318. }
  319. __finally
  320. {
  321. SessionTree->Images->EndUpdate();
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TLoginDialog::UpdateNodeImage(TTreeNode * Node)
  326. {
  327. SetNodeImage(Node, GetSessionImageIndex(GetNodeSession(Node)));
  328. }
  329. //---------------------------------------------------------------------
  330. int __fastcall TLoginDialog::GetSessionImageIndex(TSessionData * Data)
  331. {
  332. int Result;
  333. if (Data->Color != 0)
  334. {
  335. AddSessionColorImage(SessionTree->Images, static_cast<TColor>(Data->Color), SiteColorMaskImageIndex);
  336. Result = SessionTree->Images->Count - 1;
  337. }
  338. else
  339. {
  340. Result = SiteImageIndex;
  341. }
  342. return Result;
  343. }
  344. //---------------------------------------------------------------------
  345. void __fastcall TLoginDialog::SetNodeImage(TTreeNode * Node, int ImageIndex)
  346. {
  347. Node->ImageIndex = ImageIndex;
  348. Node->SelectedIndex = ImageIndex;
  349. }
  350. //---------------------------------------------------------------------
  351. void __fastcall TLoginDialog::DestroySession(TSessionData * Data)
  352. {
  353. StoredSessions->Remove(Data);
  354. }
  355. //---------------------------------------------------------------------
  356. TTreeNode * __fastcall TLoginDialog::GetNewSiteNode()
  357. {
  358. TTreeNode * Result = SessionTree->Items->GetFirstNode();
  359. DebugAssert(IsNewSiteNode(Result));
  360. return Result;
  361. }
  362. //---------------------------------------------------------------------
  363. void __fastcall TLoginDialog::SetNewSiteNodeLabel()
  364. {
  365. GetNewSiteNode()->Text = LoadStr(LOGIN_NEW_SITE_NODE);
  366. }
  367. //---------------------------------------------------------------------
  368. void __fastcall TLoginDialog::LoadSessions()
  369. {
  370. TAutoFlag LoadingFlag(FLoading);
  371. SessionTree->Items->BeginUpdate();
  372. SessionTree->Images->BeginUpdate();
  373. try
  374. {
  375. // optimization
  376. SessionTree->SortType = Comctrls::stNone;
  377. SessionTree->Items->Clear();
  378. TTreeNode * Node = SessionTree->Items->AddChild(NULL, L"");
  379. Node->Data = FNewSiteData;
  380. SetNewSiteNodeLabel();
  381. SetNodeImage(Node, NewSiteImageIndex);
  382. DebugAssert(StoredSessions != NULL);
  383. for (int Index = 0; Index < StoredSessions->Count; Index++)
  384. {
  385. AddSession(StoredSessions->Sessions[Index]);
  386. }
  387. }
  388. __finally
  389. {
  390. // Restore sorting. Moreover, folders would not be sorted automatically even when
  391. // SortType is set (not having set the data property), so we would have to
  392. // call AlphaSort here explicitly
  393. SessionTree->SortType = Comctrls::stBoth;
  394. SessionTree->Images->EndUpdate();
  395. SessionTree->Items->EndUpdate();
  396. }
  397. SessionTree->Selected = SessionTree->Items->GetFirstNode();
  398. UpdateControls();
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TLoginDialog::UpdateFolderNode(TTreeNode * Node)
  402. {
  403. DebugAssert((Node->ImageIndex == 0) ||
  404. (Node->ImageIndex == OpenFolderImageIndex) || (Node->ImageIndex == ClosedFolderImageIndex));
  405. SetNodeImage(Node, (Node->Expanded ? OpenFolderImageIndex : ClosedFolderImageIndex));
  406. }
  407. //---------------------------------------------------------------------------
  408. void __fastcall TLoginDialog::NewSite()
  409. {
  410. TTreeNode * NewSiteNode = GetNewSiteNode();
  411. if (DebugAlwaysTrue(IsNewSiteNode(NewSiteNode)))
  412. {
  413. SessionTree->Selected = NewSiteNode;
  414. }
  415. LoadContents();
  416. }
  417. //---------------------------------------------------------------------------
  418. void __fastcall TLoginDialog::ResetNewSiteData()
  419. {
  420. if (DebugAlwaysTrue(StoredSessions != NULL))
  421. {
  422. FNewSiteData->CopyData(StoredSessions->DefaultSettings);
  423. }
  424. }
  425. //---------------------------------------------------------------------------
  426. void __fastcall TLoginDialog::Default()
  427. {
  428. ResetNewSiteData();
  429. NewSite();
  430. }
  431. //---------------------------------------------------------------------
  432. void __fastcall TLoginDialog::LoadContents()
  433. {
  434. bool UseContentsPanel;
  435. TTreeNode * Node = SessionTree->Selected;
  436. if (IsSessionNode(Node))
  437. {
  438. LoadSession(SelectedSession);
  439. UseContentsPanel = false;
  440. }
  441. else if (DebugAlwaysTrue(IsFolderOrWorkspaceNode(Node)))
  442. {
  443. UnicodeString NodePath = SessionNodePath(Node);
  444. ContentsNameEdit->Text = NodePath;
  445. UnicodeString CommonRoot = IsFolderNode(Node) ? UnixIncludeTrailingBackslash(NodePath) : UnicodeString();
  446. ContentsMemo->Lines->Text =
  447. GetFolderOrWorkspaceContents(Node, L"", CommonRoot);
  448. UseContentsPanel = true;
  449. if (IsFolderNode(Node))
  450. {
  451. ContentsGroupBox->Caption = LoadStr(LOGIN_SITE_FOLDER_CAPTION);
  452. }
  453. else if (DebugAlwaysTrue(IsWorkspaceNode(Node)))
  454. {
  455. ContentsGroupBox->Caption = LoadStr(LOGIN_WORKSPACE_CAPTION);
  456. }
  457. }
  458. SitePanel->Visible = !UseContentsPanel;
  459. ContentsPanel->Visible = UseContentsPanel;
  460. }
  461. //---------------------------------------------------------------------
  462. void __fastcall TLoginDialog::LoadSession(TSessionData * SessionData)
  463. {
  464. WinConfiguration->BeginMasterPasswordSession();
  465. try
  466. {
  467. UserNameEdit->Text = SessionData->UserName;
  468. PortNumberEdit->AsInteger = SessionData->PortNumber;
  469. bool Editable = IsEditable();
  470. if (Editable)
  471. {
  472. PasswordEdit->Text = NormalizeString(SessionData->Password);
  473. }
  474. else
  475. {
  476. PasswordEdit->Text =
  477. SessionData->HasPassword() ?
  478. UnicodeString::StringOfChar(L'?', 16) : UnicodeString();
  479. }
  480. int FtpsIndex = FtpsToIndex(SessionData->Ftps);
  481. FtpsCombo->ItemIndex = FtpsIndex;
  482. WebDavsCombo->ItemIndex = FtpsIndex;
  483. EncryptionView->Text =
  484. DebugAlwaysTrue(FtpsCombo->ItemIndex >= WebDavsCombo->ItemIndex) ? FtpsCombo->Text : WebDavsCombo->Text;
  485. bool AllowScpFallback;
  486. TransferProtocolCombo->ItemIndex = FSProtocolToIndex(SessionData->FSProtocol, AllowScpFallback);
  487. TransferProtocolView->Text = TransferProtocolCombo->Text;
  488. // Only after loading TransferProtocolCombo, so that we do not overwrite it with default S3 hostname
  489. HostNameEdit->Text = SessionData->HostName;
  490. NoteGroup->Visible = !Trim(SessionData->Note).IsEmpty();
  491. NoteMemo->Lines->Text = SessionData->Note;
  492. // just in case TransferProtocolComboChange is not triggered
  493. FDefaultPort = DefaultPort();
  494. FUpdatePortWithProtocol = true;
  495. if (SessionData != FSessionData)
  496. {
  497. // advanced
  498. InvalidateSessionData();
  499. // clone advanced settings only when really needed,
  500. // see also note in SessionAdvancedActionExecute
  501. if (Editable)
  502. {
  503. FSessionData = new TSessionData(L"");
  504. FSessionData->Assign(SessionData);
  505. }
  506. }
  507. else
  508. {
  509. // we should get here only when called from SessionAdvancedActionExecute
  510. DebugAssert(Editable);
  511. }
  512. }
  513. __finally
  514. {
  515. WinConfiguration->EndMasterPasswordSession();
  516. }
  517. UpdateControls();
  518. }
  519. //---------------------------------------------------------------------
  520. void __fastcall TLoginDialog::SaveSession(TSessionData * SessionData)
  521. {
  522. // advanced
  523. if (DebugAlwaysTrue(FSessionData != NULL))
  524. {
  525. SessionData->Assign(FSessionData);
  526. }
  527. // Basic page
  528. SessionData->UserName = UserNameEdit->Text.Trim();
  529. SessionData->PortNumber = PortNumberEdit->AsInteger;
  530. // must be loaded after UserName, because HostName may be in format user@host
  531. SessionData->HostName = HostNameEdit->Text.Trim();
  532. SessionData->Password = PasswordEdit->Text;
  533. SessionData->Ftps = GetFtps();
  534. SessionData->FSProtocol =
  535. // requiring SCP fallback distinction
  536. GetFSProtocol(true);
  537. TSessionData * EditingSessionData = GetEditingSessionData();
  538. SessionData->Name =
  539. (EditingSessionData != NULL) ? EditingSessionData->Name :
  540. (FNewSiteKeepName ? SessionData->Name : SessionData->DefaultSessionName);
  541. }
  542. //---------------------------------------------------------------------
  543. bool __fastcall TLoginDialog::IsEditable()
  544. {
  545. return IsNewSiteNode(SessionTree->Selected) || FEditing;
  546. }
  547. //---------------------------------------------------------------------
  548. void __fastcall TLoginDialog::UpdateControls()
  549. {
  550. if (Visible && FInitialized)
  551. {
  552. bool Editable = IsEditable();
  553. TFSProtocol FSProtocol = GetFSProtocol(false);
  554. bool SshProtocol = IsSshProtocol(FSProtocol);
  555. bool FtpProtocol = (FSProtocol == fsFTP);
  556. bool WebDavProtocol = (FSProtocol == fsWebDAV);
  557. bool S3Protocol = (FSProtocol == fsS3);
  558. // session
  559. PortNumberEdit->Visible = !S3Protocol;
  560. Label2->Visible = PortNumberEdit->Visible;
  561. FtpsCombo->Visible = Editable && FtpProtocol;
  562. FtpsLabel->Visible = FtpProtocol;
  563. WebDavsCombo->Visible = Editable && WebDavProtocol;
  564. WebDavsLabel->Visible = WebDavProtocol;
  565. EncryptionView->Visible = !Editable && (FtpProtocol || WebDavProtocol);
  566. BasicSshPanel->Visible = SshProtocol;
  567. BasicFtpPanel->Visible = FtpProtocol && Editable;
  568. // we do not support both at the same time
  569. DebugAssert(!BasicSshPanel->Visible || !BasicFtpPanel->Visible);
  570. BasicGroup->Height =
  571. FBasicGroupBaseHeight +
  572. (BasicSshPanel->Visible ? BasicSshPanel->Height : 0) +
  573. (BasicFtpPanel->Visible ? BasicFtpPanel->Height : 0);
  574. int NoteGroupTop = (BasicGroup->Top + BasicGroup->Height) + FNoteGroupOffset;
  575. NoteGroup->SetBounds(
  576. NoteGroup->Left, (BasicGroup->Top + BasicGroup->Height) + FNoteGroupOffset,
  577. NoteGroup->Width, NoteGroup->Top + NoteGroup->Height - NoteGroupTop);
  578. AnonymousLoginCheck->Checked =
  579. SameText(UserNameEdit->Text, AnonymousUserName) &&
  580. SameText(PasswordEdit->Text, AnonymousPassword);
  581. TransferProtocolCombo->Visible = Editable;
  582. TransferProtocolView->Visible = !TransferProtocolCombo->Visible;
  583. ReadOnlyControl(HostNameEdit, !Editable);
  584. ReadOnlyControl(PortNumberEdit, !Editable);
  585. PortNumberEdit->ButtonsVisible = Editable;
  586. // FSessionData may be NULL temporary even when Editable while switching nodes
  587. bool NoAuth = Editable && SshProtocol && (FSessionData != NULL) && FSessionData->SshNoUserAuth;
  588. ReadOnlyAndEnabledControl(UserNameEdit, !Editable, !NoAuth);
  589. EnableControl(UserNameLabel, UserNameEdit->Enabled);
  590. ReadOnlyAndEnabledControl(PasswordEdit, !Editable, !NoAuth);
  591. EnableControl(PasswordLabel, PasswordEdit->Enabled);
  592. UserNameLabel->Caption = S3Protocol ? LoadStr(S3_ACCESS_KEY_ID_PROMPT) : FUserNameLabel;
  593. PasswordLabel->Caption = S3Protocol ? LoadStr(S3_SECRET_ACCESS_KEY_PROMPT) : FPasswordLabel;
  594. // sites
  595. if (SitesIncrementalSearchLabel->Visible != !FSitesIncrementalSearch.IsEmpty())
  596. {
  597. if (FSitesIncrementalSearch.IsEmpty())
  598. {
  599. SitesIncrementalSearchLabel->Visible = false;
  600. SessionTree->Height = SitesIncrementalSearchLabel->BoundsRect.Bottom - SessionTree->Top;
  601. }
  602. else
  603. {
  604. SitesIncrementalSearchLabel->Visible = true;
  605. SessionTree->Height = SitesIncrementalSearchLabel->BoundsRect.Top - SessionTree->Top;
  606. }
  607. }
  608. if (!FSitesIncrementalSearch.IsEmpty())
  609. {
  610. SitesIncrementalSearchLabel->Caption =
  611. L" " + FMTLOAD(LOGIN_SITES_INC_SEARCH, (FSitesIncrementalSearch)) +
  612. (FSitesIncrementalSearchHaveNext ? L" " + LoadStr(LOGIN_SITES_NEXT_SEARCH) : UnicodeString());
  613. }
  614. EnableControl(ManageButton, !FEditing);
  615. EnableControl(ToolsMenuButton, !FEditing);
  616. EnableControl(CloseButton, !FEditing);
  617. DefaultButton(LoginButton, !FEditing && !FRenaming && !IsCloneToNewSiteDefault());
  618. CloseButton->Cancel = !FEditing && !FRenaming;
  619. DefaultButton(SaveButton, FEditing);
  620. EditCancelButton->Cancel = FEditing;
  621. SiteClonetoNewSiteMenuItem->Default = IsCloneToNewSiteDefault();
  622. SiteLoginMenuItem->Default = LoginButton->Default;
  623. UpdateButtonVisibility(SaveButton);
  624. UpdateButtonVisibility(EditButton);
  625. UpdateButtonVisibility(EditCancelButton);
  626. SaveAsSessionMenuItem->Visible = FEditing;
  627. }
  628. }
  629. //---------------------------------------------------------------------------
  630. void __fastcall TLoginDialog::UpdateButtonVisibility(TButton * Button)
  631. {
  632. TAction * Action = DebugNotNull(dynamic_cast<TAction *>(Button->Action));
  633. // when all action targets are hidden, action does not get updated,
  634. // so we need to do it manually
  635. Action->Update();
  636. // button visibility cannot be bound to action visibility,
  637. // so we do not bother setting action visibility, instead we manually
  638. // bind visibility to enabled state
  639. Button->Visible = Action->Enabled;
  640. }
  641. //---------------------------------------------------------------------------
  642. void __fastcall TLoginDialog::DataChange(TObject * /*Sender*/)
  643. {
  644. UpdateControls();
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TLoginDialog::FormShow(TObject * /*Sender*/)
  648. {
  649. // this is called twice on startup, first with ControlState = [csRecreating]
  650. // we should probably filter this out, it would avoid need for explicit
  651. // LoadContents call below
  652. bool NeedInitialize = !FInitialized;
  653. if (NeedInitialize)
  654. {
  655. Init();
  656. }
  657. // WORKAROUND for a bug in the VCL layout code for bottom aligned controls
  658. int Offset = (SitesPanel->ClientHeight - FSiteButtonsPadding - ToolsMenuButton->Height) - ToolsMenuButton->Top;
  659. ToolsMenuButton->Top = ToolsMenuButton->Top + Offset;
  660. ManageButton->Top = ManageButton->Top + Offset;
  661. SessionTree->Height = SessionTree->Height + Offset;
  662. // among other this makes the expanded nodes look like expanded,
  663. // because the LoadState call in Execute would be too early,
  664. // and some stray call to collapsed event during showing process,
  665. // make the image be set to collapsed.
  666. // Also LoadState calls RestoreFormSize that has to be
  667. // called only after DoFormWindowProc(CM_SHOWINGCHANGED).
  668. // See also comment about MakeVisible in LoadState().
  669. LoadState();
  670. if (NeedInitialize)
  671. {
  672. // Need to load contents only after state (as that selects initial node).
  673. // Explicit call is needed, as we get here during csRecreating phase,
  674. // when SessionTreeChange is not triggered, see initial method comment
  675. LoadContents();
  676. }
  677. UpdateControls();
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TLoginDialog::SessionTreeChange(TObject * /*Sender*/,
  681. TTreeNode * /*Node*/)
  682. {
  683. if (FIncrementalSearching <= 0)
  684. {
  685. // Make sure UpdateControls is called here, no matter what,
  686. // now it is always called from ResetSitesIncrementalSearch.
  687. // For the "else" scenario, UpdateControls is called later from SitesIncrementalSearch.
  688. ResetSitesIncrementalSearch();
  689. }
  690. if (FInitialized)
  691. {
  692. LoadContents();
  693. }
  694. }
  695. //---------------------------------------------------------------------------
  696. TSessionData * __fastcall TLoginDialog::GetSessionData()
  697. {
  698. if (SelectedSession == FNewSiteData)
  699. {
  700. SaveSession(FNewSiteData);
  701. }
  702. return SelectedSession;
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TLoginDialog::SessionTreeDblClick(TObject * /*Sender*/)
  706. {
  707. TPoint P = SessionTree->ScreenToClient(Mouse->CursorPos);
  708. TTreeNode * Node = SessionTree->GetNodeAt(P.x, P.y);
  709. // This may be false, when collapsed folder was double-clicked,
  710. // it got expanded, view was shifted to accommodate folder contents,
  711. // so that cursor now points to a different node (site).
  712. // This has to be evaluated before EnsureNotEditing,
  713. // as that may pop-up modal box.
  714. if (Node == SessionTree->Selected)
  715. {
  716. // EnsureNotEditing must be before CanLogin, as CanLogin checks for FEditing
  717. if (EnsureNotEditing())
  718. {
  719. if (IsCloneToNewSiteDefault())
  720. {
  721. CloneToNewSite();
  722. }
  723. // this can hardle be false
  724. // (after editing and clone tests above)
  725. // (except for empty folders, but those do not pass a condition below)
  726. else if (CanLogin())
  727. {
  728. if (IsSessionNode(Node) || IsWorkspaceNode(Node))
  729. {
  730. Login();
  731. }
  732. }
  733. }
  734. }
  735. }
  736. //---------------------------------------------------------------------------
  737. TSessionData * __fastcall TLoginDialog::GetSelectedSession()
  738. {
  739. // Selected can be temporarily NULL, e.g. while deleting selected node
  740. TTreeNode * Node = SessionTree->Selected;
  741. if ((Node != NULL) &&
  742. (IsSiteNode(Node) || IsNewSiteNode(Node)))
  743. {
  744. return GetNodeSession(Node);
  745. }
  746. else
  747. {
  748. return NULL;
  749. }
  750. }
  751. //---------------------------------------------------------------------------
  752. void __fastcall TLoginDialog::SessionTreeKeyDown(TObject * /*Sender*/,
  753. WORD & Key, TShiftState /*Shift*/)
  754. {
  755. if (!SessionTree->IsEditing())
  756. {
  757. if (Key == VK_DELETE)
  758. {
  759. DeleteSessionAction->Execute();
  760. Key = 0;
  761. }
  762. else if (Key == VK_F2)
  763. {
  764. RenameSessionAction->Execute();
  765. Key = 0;
  766. }
  767. else if (Key == VK_BACK)
  768. {
  769. Key = 0;
  770. }
  771. }
  772. }
  773. //---------------------------------------------------------------------------
  774. void __fastcall TLoginDialog::SessionTreeKeyPress(TObject * /*Sender*/, System::WideChar & Key)
  775. {
  776. if (!SessionTree->IsEditing())
  777. {
  778. // filter control sequences
  779. if (Key >= VK_SPACE)
  780. {
  781. if (FSitesIncrementalSearch.IsEmpty())
  782. {
  783. Configuration->Usage->Inc(L"SiteIncrementalSearches");
  784. }
  785. if (!SitesIncrementalSearch(FSitesIncrementalSearch + Key, false, false))
  786. {
  787. MessageBeep(MB_ICONHAND);
  788. }
  789. Key = 0;
  790. }
  791. else if (Key == VK_BACK)
  792. {
  793. if (!FSitesIncrementalSearch.IsEmpty())
  794. {
  795. if (FSitesIncrementalSearch.Length() == 1)
  796. {
  797. ResetSitesIncrementalSearch();
  798. }
  799. else
  800. {
  801. UnicodeString NewText =
  802. FSitesIncrementalSearch.SubString(1, FSitesIncrementalSearch.Length() - 1);
  803. SitesIncrementalSearch(NewText, false, false);
  804. }
  805. Key = 0;
  806. }
  807. }
  808. else if ((Key == VK_RETURN) && IsCloneToNewSiteDefault())
  809. {
  810. CloneToNewSite();
  811. Key = 0;
  812. }
  813. }
  814. }
  815. //---------------------------------------------------------------------------
  816. void __fastcall TLoginDialog::EditSession()
  817. {
  818. HostNameEdit->SetFocus();
  819. }
  820. //---------------------------------------------------------------------------
  821. void __fastcall TLoginDialog::EditSessionActionExecute(TObject * /*Sender*/)
  822. {
  823. if (DebugAlwaysTrue(SelectedSession != NULL))
  824. {
  825. FEditing = true;
  826. EditSession();
  827. // reload session, to make sure we load decrypted password
  828. LoadContents();
  829. UpdateControls();
  830. }
  831. }
  832. //---------------------------------------------------------------------------
  833. TTreeNode * __fastcall TLoginDialog::FindSessionNode(TSessionData * SessionData, bool ByName)
  834. {
  835. TTreeNode * Node = SessionTree->Items->GetFirstNode();
  836. while (
  837. (Node != NULL) &&
  838. ((!ByName && (Node->Data != SessionData)) ||
  839. (ByName && (!IsSiteNode(Node) || (GetNodeSession(Node)->Name != SessionData->Name)))))
  840. {
  841. Node = Node->GetNext();
  842. }
  843. return Node;
  844. }
  845. //---------------------------------------------------------------------------
  846. TSessionData * __fastcall TLoginDialog::GetEditingSessionData()
  847. {
  848. return FEditing ? DebugNotNull(SelectedSession) : NULL;
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TLoginDialog::SaveAsSession(bool ForceDialog)
  852. {
  853. // Parse hostname before saving
  854. // (HostNameEditExit is not triggered when child dialog pops up when it is invoked by accelerator)
  855. // We should better handle this automaticaly when focus is moved to another dialog.
  856. ParseHostName();
  857. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  858. SaveSession(SessionData.get());
  859. TSessionData * EditingSessionData = GetEditingSessionData();
  860. // collect list of empty folders (these are not persistent and known to login dialog only)
  861. std::unique_ptr<TStrings> NewFolders(new TStringList());
  862. TTreeNode * Node = SessionTree->Items->GetFirstNode();
  863. while (Node != NULL)
  864. {
  865. if (IsFolderNode(Node) && !Node->HasChildren)
  866. {
  867. NewFolders->Add(SessionNodePath(Node));
  868. }
  869. Node = Node->GetNext();
  870. }
  871. TSessionData * NewSession =
  872. DoSaveSession(SessionData.get(), EditingSessionData, ForceDialog, NewFolders.get());
  873. if (NewSession != NULL)
  874. {
  875. TTreeNode * ParentNode = AddSessionPath(UnixExtractFilePath(NewSession->SessionName), false, false);
  876. CheckIsSessionFolder(ParentNode);
  877. TTreeNode * Node = FindSessionNode(NewSession, false);
  878. if (Node == NULL)
  879. {
  880. Node = AddSession(NewSession);
  881. }
  882. if ((SessionTree->Selected != Node) &&
  883. IsSiteNode(SessionTree->Selected))
  884. {
  885. CancelEditing();
  886. }
  887. else
  888. {
  889. FEditing = false;
  890. }
  891. SessionTree->Selected = Node;
  892. SessionTree->SetFocus();
  893. // this
  894. // - updates TransferProtocolView and EncryptionView
  895. // - clears the password box, if user has not opted to save password
  896. // - reloads fake password
  897. LoadContents();
  898. UpdateControls();
  899. ResetNewSiteData();
  900. }
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TLoginDialog::SaveSessionActionExecute(TObject * /*Sender*/)
  904. {
  905. bool NewSiteSelected = IsNewSiteNode(SessionTree->Selected);
  906. // for new site, the "save" command is actually "save as"
  907. SaveAsSession(NewSiteSelected);
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TLoginDialog::SaveAsSessionActionExecute(TObject * /*Sender*/)
  911. {
  912. SaveAsSession(true);
  913. }
  914. //---------------------------------------------------------------------------
  915. UnicodeString __fastcall TLoginDialog::SessionNodePath(TTreeNode * Node)
  916. {
  917. UnicodeString Path;
  918. if ((Node != NULL) && !IsNewSiteNode(Node))
  919. {
  920. Path = Node->Text;
  921. Node = Node->Parent;
  922. while (Node != NULL)
  923. {
  924. Path.Insert(UnixIncludeTrailingBackslash(Node->Text), 1);
  925. Node = Node->Parent;
  926. }
  927. }
  928. return Path;
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TLoginDialog::DeleteSessionActionExecute(TObject * /*Sender*/)
  932. {
  933. DebugAssert(SessionTree->Selected != NULL);
  934. TMessageParams Params;
  935. Params.ImageName = L"Delete file";
  936. TTreeNode * Node = SessionTree->Selected;
  937. if (IsSiteNode(Node))
  938. {
  939. TSessionData * Session = SelectedSession;
  940. UnicodeString Message = MainInstructions(FMTLOAD(CONFIRM_DELETE_SESSION, (Session->SessionName)));
  941. if (MessageDialog(Message,
  942. qtConfirmation, qaOK | qaCancel, HELP_DELETE_SESSION, &Params) == qaOK)
  943. {
  944. WinConfiguration->DeleteSessionFromJumpList(Session->SessionName);
  945. Session->Remove();
  946. DestroySession(Session);
  947. SessionTree->Selected->Delete();
  948. }
  949. }
  950. else if (IsFolderNode(Node) || IsWorkspaceNode(Node))
  951. {
  952. int Sessions = 0;
  953. TTreeNode * ANode = Node->GetNext();
  954. while ((ANode != NULL) && ANode->HasAsParent(Node))
  955. {
  956. if (IsSessionNode(ANode))
  957. {
  958. TSessionData * Session = GetNodeSession(ANode);
  959. if (Session->Special)
  960. {
  961. SessionTree->Selected = ANode;
  962. ANode->MakeVisible();
  963. throw Exception(FMTLOAD(LOGIN_DELETE_SPECIAL_SESSION, (Session->SessionName)));
  964. }
  965. Sessions++;
  966. }
  967. ANode = ANode->GetNext();
  968. }
  969. UnicodeString Path = SessionNodePath(Node);
  970. int Prompt;
  971. UnicodeString HelpKeyword;
  972. if (IsFolderNode(Node))
  973. {
  974. Prompt = LOGIN_DELETE_SESSION_FOLDER;
  975. HelpKeyword = HELP_DELETE_SESSION_FOLDER;
  976. }
  977. else
  978. {
  979. Prompt = LOGIN_DELETE_WORKSPACE;
  980. HelpKeyword = HELP_DELETE_WORKSPACE;
  981. }
  982. if ((Sessions == 0) ||
  983. (MessageDialog(MainInstructions(FMTLOAD(Prompt, (Path, Sessions))),
  984. qtConfirmation, qaOK | qaCancel, HelpKeyword, &Params) == qaOK))
  985. {
  986. if (IsWorkspaceNode(Node))
  987. {
  988. WinConfiguration->DeleteWorkspaceFromJumpList(Path);
  989. }
  990. Node = SessionTree->Selected;
  991. TTreeNode * ANode = Node->GetNext();
  992. while ((ANode != NULL) && ANode->HasAsParent(Node))
  993. {
  994. if (IsSessionNode(ANode))
  995. {
  996. TSessionData * Session = GetNodeSession(ANode);
  997. Session->Remove();
  998. DestroySession(Session);
  999. }
  1000. ANode = ANode->GetNext();
  1001. }
  1002. SessionTree->Selected->Delete();
  1003. }
  1004. }
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. void __fastcall TLoginDialog::ReloadSessions(const UnicodeString & SelectSite)
  1008. {
  1009. SaveState();
  1010. if (!SelectSite.IsEmpty())
  1011. {
  1012. WinConfiguration->LastStoredSession = SelectSite;
  1013. }
  1014. LoadSessions();
  1015. LoadState();
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TLoginDialog::ImportSessionsActionExecute(TObject * /*Sender*/)
  1019. {
  1020. std::unique_ptr<TList> Imported(new TList());
  1021. if (DoImportSessionsDialog(Imported.get()) &&
  1022. // Can be empty when imported known_hosts
  1023. (Imported->Count > 0))
  1024. {
  1025. // Focus the first imported session.
  1026. // We should also consider expanding all newly created folders
  1027. UnicodeString SelectSite = static_cast<TSessionData *>(Imported->Items[0])->Name;
  1028. ReloadSessions(SelectSite);
  1029. // Focus the tree with focused imported session(s).
  1030. SessionTree->SetFocus();
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TLoginDialog::CleanUpActionExecute(TObject * /*Sender*/)
  1035. {
  1036. if (DoCleanupDialog(StoredSessions, Configuration))
  1037. {
  1038. SaveState();
  1039. LoadSessions();
  1040. LoadState();
  1041. }
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. void __fastcall TLoginDialog::AboutActionExecute(TObject * /*Sender*/)
  1045. {
  1046. DoAboutDialog(Configuration);
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. void __fastcall TLoginDialog::ActionListUpdate(TBasicAction * BasicAction,
  1050. bool &Handled)
  1051. {
  1052. bool NewSiteSelected = IsNewSiteNode(SessionTree->Selected);
  1053. bool SiteSelected = IsSiteNode(SessionTree->Selected);
  1054. bool FolderOrWorkspaceSelected = IsFolderOrWorkspaceNode(SessionTree->Selected);
  1055. TAction * Action = DebugNotNull(dynamic_cast<TAction *>(BasicAction));
  1056. bool PrevEnabled = Action->Enabled;
  1057. bool Editable = IsEditable();
  1058. if ((Action == EditSessionAction) ||
  1059. (Action == CloneToNewSiteAction))
  1060. {
  1061. Action->Enabled = SiteSelected && !FEditing;
  1062. }
  1063. else if (Action == EditCancelAction)
  1064. {
  1065. EditCancelAction->Enabled = FEditing;
  1066. }
  1067. else if (Action == DeleteSessionAction)
  1068. {
  1069. DeleteSessionAction->Enabled =
  1070. ((SiteSelected && !SelectedSession->Special && !FEditing) ||
  1071. FolderOrWorkspaceSelected);
  1072. }
  1073. else if (Action == RenameSessionAction)
  1074. {
  1075. RenameSessionAction->Enabled =
  1076. ((SiteSelected && !SelectedSession->Special && !FEditing) ||
  1077. FolderOrWorkspaceSelected);
  1078. }
  1079. else if (Action == DesktopIconAction)
  1080. {
  1081. DesktopIconAction->Enabled =
  1082. (SiteSelected && !FEditing) ||
  1083. (FolderOrWorkspaceSelected && HasNodeAnySession(SessionTree->Selected));
  1084. }
  1085. else if (Action == SendToHookAction)
  1086. {
  1087. SendToHookAction->Enabled = SiteSelected && !FEditing;
  1088. }
  1089. else if (Action == LoginAction)
  1090. {
  1091. LoginAction->Enabled = CanLogin();
  1092. }
  1093. else if (Action == PuttyAction)
  1094. {
  1095. Action->Enabled = (NewSiteSelected || SiteSelected) && CanLogin();
  1096. }
  1097. else if (Action == SaveSessionAction)
  1098. {
  1099. SaveSessionAction->Enabled = Editable;
  1100. }
  1101. else if (Action == SessionAdvancedAction)
  1102. {
  1103. SessionAdvancedAction->Enabled = Editable;
  1104. }
  1105. else if (Action == SaveAsSessionAction)
  1106. {
  1107. // Save as is needed for new site only when !SupportsSplitButton()
  1108. SaveAsSessionAction->Enabled = Editable;
  1109. }
  1110. else if (Action == NewSessionFolderAction)
  1111. {
  1112. NewSessionFolderAction->Enabled = !FEditing;
  1113. }
  1114. else if (Action == PasteUrlAction)
  1115. {
  1116. UnicodeString ClipboardUrl;
  1117. Action->Enabled =
  1118. NonEmptyTextFromClipboard(ClipboardUrl) &&
  1119. StoredSessions->IsUrl(ClipboardUrl);
  1120. }
  1121. else if (Action == GenerateUrlAction2)
  1122. {
  1123. TSessionData * Data = GetSessionData();
  1124. // URL without hostname is pointless
  1125. Action->Enabled = (Data != NULL) && !Data->HostName.IsEmpty() && !FEditing;
  1126. }
  1127. else if (Action == CopyParamRuleAction)
  1128. {
  1129. TSessionData * Data = GetSessionData();
  1130. // without hostname it's pointless
  1131. Action->Enabled = (Data != NULL) && !Data->HostName.IsEmpty();
  1132. }
  1133. else if (Action == SearchSiteNameStartOnlyAction)
  1134. {
  1135. Action->Checked = (FSiteSearch == ssSiteNameStartOnly);
  1136. }
  1137. else if (Action == SearchSiteNameAction)
  1138. {
  1139. Action->Checked = (FSiteSearch == ssSiteName);
  1140. }
  1141. else if (Action == SearchSiteAction)
  1142. {
  1143. Action->Checked = (FSiteSearch == ssSite);
  1144. }
  1145. else if (Action == CheckForUpdatesAction)
  1146. {
  1147. Action->Visible = !IsUWP();
  1148. }
  1149. Handled = true;
  1150. // to update buttons visibility
  1151. if (PrevEnabled != Action->Enabled)
  1152. {
  1153. UpdateControls();
  1154. }
  1155. Idle();
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. bool __fastcall TLoginDialog::IsCloneToNewSiteDefault()
  1159. {
  1160. return !FEditing && !FRenaming && IsSiteNode(SessionTree->Selected) && !FStoredSessions->CanLogin(GetSessionData());
  1161. }
  1162. //---------------------------------------------------------------------------
  1163. bool __fastcall TLoginDialog::CanLogin()
  1164. {
  1165. TSessionData * Data = GetSessionData();
  1166. return
  1167. ((Data != NULL) && FStoredSessions->CanLogin(Data) && !FEditing) ||
  1168. (IsFolderOrWorkspaceNode(SessionTree->Selected) && HasNodeAnySession(SessionTree->Selected, true));
  1169. }
  1170. //---------------------------------------------------------------------------
  1171. void __fastcall TLoginDialog::Idle()
  1172. {
  1173. if (SessionTree->IsEditing() != FRenaming)
  1174. {
  1175. FRenaming = SessionTree->IsEditing();
  1176. UpdateControls();
  1177. }
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. bool __fastcall TLoginDialog::Execute(TList * DataList)
  1181. {
  1182. FDataList = DataList;
  1183. if (DataList->Count > 0)
  1184. {
  1185. TSessionData * SessionData = reinterpret_cast<TSessionData * >(DataList->Items[0]);
  1186. TTreeNode * Node = FindSessionNode(SessionData, true);
  1187. if (Node != NULL)
  1188. {
  1189. SessionTree->Selected = Node;
  1190. ActiveControl = SessionTree;
  1191. }
  1192. else
  1193. {
  1194. FNewSiteData->CopyData(SessionData);
  1195. FNewSiteData->Special = false;
  1196. // This is actualy bit pointless, as we focus the last selected site anyway
  1197. // in LoadState(). As of now, we hardly get any useful data
  1198. // in ad-hoc DataList anyway, so it is not a big deal
  1199. // (this was implemented for support taking session url from clipboard instead
  1200. // of command-line, but without autoconnect, but this functionality was cancelled)
  1201. if (!FNewSiteData->IsSame(StoredSessions->DefaultSettings, false))
  1202. {
  1203. // we want to start with new site page
  1204. FForceNewSite = true;
  1205. }
  1206. LoadContents();
  1207. }
  1208. }
  1209. else
  1210. {
  1211. Default();
  1212. }
  1213. // Optimization. List view is recreated while showing the form,
  1214. // causing nodes repopulation and in a consequence a huge number of
  1215. // nodes comparison
  1216. SessionTree->SortType = Comctrls::stNone;
  1217. FSortEnablePending = true;
  1218. // Not calling LoadState here.
  1219. // It's redundant and does not work anyway, see comment in the method.
  1220. int AResult = ShowModal();
  1221. // When CanLogin is false, the DefaultResult() will fail finding a default button.
  1222. bool Result = CanLogin() && (AResult == DefaultResult());
  1223. SaveState();
  1224. if (Result)
  1225. {
  1226. SaveConfiguration();
  1227. // DataList saved already from FormCloseQuery
  1228. }
  1229. return Result;
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. void __fastcall TLoginDialog::SaveDataList(TList * DataList)
  1233. {
  1234. // Normally we would call this from Execute,
  1235. // but at that point the windows is already hidden.
  1236. // Cloning session data may pop up master password dialog:
  1237. // - if it happens between closing and destroyiong login dialog
  1238. // the next window will appear in background for some reason
  1239. // - and its actually even nicer when master password dialog pops up over
  1240. // the login dialog
  1241. DataList->Clear();
  1242. TTreeNode * Node = SessionTree->Selected;
  1243. if (IsFolderOrWorkspaceNode(Node))
  1244. {
  1245. UnicodeString Name = SessionNodePath(Node);
  1246. if (IsWorkspaceNode(Node))
  1247. {
  1248. WinConfiguration->AddWorkspaceToJumpList(Name);
  1249. }
  1250. StoredSessions->GetFolderOrWorkspace(Name, DataList);
  1251. }
  1252. else
  1253. {
  1254. DataList->Add(CloneSelectedSession());
  1255. }
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. TSessionData * __fastcall TLoginDialog::CloneSelectedSession()
  1259. {
  1260. TTreeNode * Node = SessionTree->Selected;
  1261. std::unique_ptr<TSessionData> Data2(new TSessionData(L""));
  1262. if (IsSiteNode(Node))
  1263. {
  1264. Data2->Assign(GetNodeSession(Node));
  1265. }
  1266. else if (DebugAlwaysTrue(IsNewSiteNode(Node)))
  1267. {
  1268. TSessionData * Data = GetSessionData();
  1269. Data2->Assign(Data);
  1270. // we carry the name of the edited stored session around while on the dialog,
  1271. // but we do not want it to leave the dialog, so that we can distinguish
  1272. // stored and ad-hoc sessions
  1273. Data2->Name = L"";
  1274. }
  1275. return Data2.release();
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. void __fastcall TLoginDialog::SaveState()
  1279. {
  1280. DebugAssert(WinConfiguration != NULL);
  1281. WinConfiguration->BeginUpdate();
  1282. try
  1283. {
  1284. std::unique_ptr<TStringList> OpenedStoredSessionFolders(CreateSortedStringList());
  1285. for (int Index = 0; Index < SessionTree->Items->Count; Index++)
  1286. {
  1287. TTreeNode * Node = SessionTree->Items->Item[Index];
  1288. if (IsFolderNode(Node))
  1289. {
  1290. if (Node->Expanded)
  1291. {
  1292. OpenedStoredSessionFolders->Add(SessionNodePath(Node));
  1293. }
  1294. }
  1295. }
  1296. WinConfiguration->OpenedStoredSessionFolders = OpenedStoredSessionFolders->CommaText;
  1297. WinConfiguration->LastStoredSession = SessionNodePath(SessionTree->Selected);
  1298. TLoginDialogConfiguration DialogConfiguration = CustomWinConfiguration->LoginDialog;
  1299. DialogConfiguration.WindowSize = StoreFormSize(this);
  1300. DialogConfiguration.SiteSearch = FSiteSearch;
  1301. CustomWinConfiguration->LoginDialog = DialogConfiguration;
  1302. }
  1303. __finally
  1304. {
  1305. WinConfiguration->EndUpdate();
  1306. }
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TLoginDialog::LoadOpenedStoredSessionFolders(
  1310. TTreeNode * Node, TStrings * OpenedStoredSessionFolders)
  1311. {
  1312. if (IsFolderNode(Node))
  1313. {
  1314. UnicodeString Path = SessionNodePath(Node);
  1315. if (OpenedStoredSessionFolders->IndexOf(Path) >= 0)
  1316. {
  1317. Node->Expand(false);
  1318. UpdateFolderNode(Node);
  1319. }
  1320. }
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TLoginDialog::LoadState()
  1324. {
  1325. // it does not make any sense to call this before
  1326. // DoFormWindowProc(CM_SHOWINGCHANGED), we would end up on wrong monitor
  1327. if (DebugAlwaysTrue(Visible))
  1328. {
  1329. RestoreFormSize(CustomWinConfiguration->LoginDialog.WindowSize, this);
  1330. }
  1331. FSiteSearch = CustomWinConfiguration->LoginDialog.SiteSearch;
  1332. TStringList * OpenedStoredSessionFolders = CreateSortedStringList();
  1333. try
  1334. {
  1335. OpenedStoredSessionFolders->CommaText = WinConfiguration->OpenedStoredSessionFolders;
  1336. for (int Index = 0; Index < SessionTree->Items->Count; Index++)
  1337. {
  1338. LoadOpenedStoredSessionFolders(
  1339. SessionTree->Items->Item[Index], OpenedStoredSessionFolders);
  1340. }
  1341. // tree view tries to make expanded node children all visible, what
  1342. // may scroll the selected node (what should be the first one here),
  1343. // out of the view
  1344. if (SessionTree->Selected != NULL)
  1345. {
  1346. // see comment for LastStoredSession branch below
  1347. DebugAssert(Visible);
  1348. SessionTree->Selected->MakeVisible();
  1349. }
  1350. }
  1351. __finally
  1352. {
  1353. delete OpenedStoredSessionFolders;
  1354. }
  1355. // calling TTreeNode::MakeVisible() when tree view is not visible yet,
  1356. // sometimes scrolls view horizontally when not needed
  1357. // (seems like it happens for sites that are at the same level
  1358. // as site folders, e.g. for the very last root-level site, at long as
  1359. // there are any folders)
  1360. if (!FForceNewSite &&
  1361. !WinConfiguration->LastStoredSession.IsEmpty() && DebugAlwaysTrue(Visible))
  1362. {
  1363. UnicodeString Path = WinConfiguration->LastStoredSession;
  1364. UnicodeString ParentPath = UnixExtractFilePath(Path);
  1365. TTreeNode * Node;
  1366. if (ParentPath.IsEmpty())
  1367. {
  1368. Node = SessionTree->Items->GetFirstNode();
  1369. }
  1370. else
  1371. {
  1372. TTreeNode * Parent = AddSessionPath(ParentPath, false, false);
  1373. Node = (Parent != NULL) ? Parent->getFirstChild() : NULL;
  1374. }
  1375. if (Node != NULL)
  1376. {
  1377. UnicodeString Name = UnixExtractFileName(Path);
  1378. // actually we cannot distinguish folder and session here
  1379. // (note that we allow folder and session with the same name),
  1380. // this is pending for future improvements
  1381. while ((Node != NULL) && !AnsiSameText(Node->Text, Name))
  1382. {
  1383. Node = Node->getNextSibling();
  1384. }
  1385. if (Node != NULL)
  1386. {
  1387. SessionTree->Selected = Node;
  1388. SessionTree->Selected->MakeVisible();
  1389. }
  1390. }
  1391. }
  1392. }
  1393. //---------------------------------------------------------------------------
  1394. void __fastcall TLoginDialog::SaveConfiguration()
  1395. {
  1396. DebugAssert(CustomWinConfiguration);
  1397. TTreeNode * Node = SessionTree->Selected;
  1398. if (IsWorkspaceNode(Node))
  1399. {
  1400. WinConfiguration->LastWorkspace = SessionNodePath(Node);
  1401. }
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. void __fastcall TLoginDialog::PreferencesActionExecute(TObject * /*Sender*/)
  1405. {
  1406. ShowPreferencesDialog(::pmDefault);
  1407. }
  1408. //---------------------------------------------------------------------------
  1409. void __fastcall TLoginDialog::PreferencesLoggingActionExecute(TObject * /*Sender*/)
  1410. {
  1411. ShowPreferencesDialog(pmLogging);
  1412. }
  1413. //---------------------------------------------------------------------------
  1414. void __fastcall TLoginDialog::MasterPasswordRecrypt(TObject * /*Sender*/)
  1415. {
  1416. FNewSiteData->RecryptPasswords();
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void __fastcall TLoginDialog::ShowPreferencesDialog(TPreferencesMode PreferencesMode)
  1420. {
  1421. DebugAssert(CustomWinConfiguration->OnMasterPasswordRecrypt == NULL);
  1422. CustomWinConfiguration->OnMasterPasswordRecrypt = MasterPasswordRecrypt;
  1423. try
  1424. {
  1425. DoPreferencesDialog(PreferencesMode);
  1426. }
  1427. __finally
  1428. {
  1429. DebugAssert(CustomWinConfiguration->OnMasterPasswordRecrypt == MasterPasswordRecrypt);
  1430. CustomWinConfiguration->OnMasterPasswordRecrypt = NULL;
  1431. }
  1432. UpdateControls();
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TLoginDialog::ResetNewSessionActionExecute(TObject * /*Sender*/)
  1436. {
  1437. Default();
  1438. EditSession();
  1439. FNewSiteKeepName = false;
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. void __fastcall TLoginDialog::CMDialogKey(TWMKeyDown & Message)
  1443. {
  1444. if (Message.CharCode == VK_TAB)
  1445. {
  1446. if (!FSitesIncrementalSearch.IsEmpty())
  1447. {
  1448. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  1449. bool Reverse = Shift.Contains(ssShift);
  1450. if (!SitesIncrementalSearch(FSitesIncrementalSearch, true, Reverse))
  1451. {
  1452. MessageBeep(MB_ICONHAND);
  1453. }
  1454. Message.Result = 1;
  1455. return;
  1456. }
  1457. }
  1458. else if (Message.CharCode == VK_ESCAPE)
  1459. {
  1460. if (!FSitesIncrementalSearch.IsEmpty())
  1461. {
  1462. ResetSitesIncrementalSearch();
  1463. Message.Result = 1;
  1464. return;
  1465. }
  1466. }
  1467. TForm::Dispatch(&Message);
  1468. }
  1469. //---------------------------------------------------------------------------
  1470. void __fastcall TLoginDialog::WMMoving(TMessage & Message)
  1471. {
  1472. TForm::Dispatch(&Message);
  1473. if (FLinkedForm != NULL)
  1474. {
  1475. if (FPrevPos.X == std::numeric_limits<LONG>::min())
  1476. {
  1477. FPrevPos = BoundsRect.TopLeft();
  1478. }
  1479. TRect Rect = *reinterpret_cast<RECT*>(Message.LParam);
  1480. FLinkedForm->SetBounds(
  1481. FLinkedForm->Left + (Rect.Left - FPrevPos.X),
  1482. FLinkedForm->Top + (Rect.Top - FPrevPos.Y),
  1483. FLinkedForm->Width, FLinkedForm->Height);
  1484. FPrevPos = Rect.TopLeft();
  1485. }
  1486. }
  1487. //---------------------------------------------------------------------------
  1488. void __fastcall TLoginDialog::CMDpiChanged(TMessage & Message)
  1489. {
  1490. TForm::Dispatch(&Message);
  1491. GenerateButtonImages();
  1492. CenterButtonImage(LoginButton);
  1493. }
  1494. //---------------------------------------------------------------------------
  1495. void __fastcall TLoginDialog::Dispatch(void * Message)
  1496. {
  1497. TMessage * M = reinterpret_cast<TMessage*>(Message);
  1498. DebugAssert(M);
  1499. if (M->Msg == CM_DIALOGKEY)
  1500. {
  1501. CMDialogKey(*((TWMKeyDown *)Message));
  1502. }
  1503. else if (M->Msg == WM_MANAGES_CAPTION)
  1504. {
  1505. // caption managed in TLoginDialog::Init()
  1506. M->Result = 1;
  1507. }
  1508. else if (M->Msg == WM_WANTS_MOUSEWHEEL)
  1509. {
  1510. M->Result = 1;
  1511. }
  1512. else if (M->Msg == WM_CAN_DISPLAY_UPDATES)
  1513. {
  1514. M->Result = 1;
  1515. }
  1516. else if (M->Msg == CM_ACTIVATE)
  1517. {
  1518. // Called from TCustomForm.ShowModal
  1519. if (FSortEnablePending)
  1520. {
  1521. FSortEnablePending = false;
  1522. SessionTree->SortType = Comctrls::stBoth;
  1523. }
  1524. TForm::Dispatch(Message);
  1525. }
  1526. else if (M->Msg == WM_SYSCOMMAND)
  1527. {
  1528. if (!HandleMinimizeSysCommand(*M))
  1529. {
  1530. TForm::Dispatch(Message);
  1531. }
  1532. }
  1533. else if (M->Msg == WM_MOVING)
  1534. {
  1535. WMMoving(*M);
  1536. }
  1537. else if (M->Msg == CM_DPICHANGED)
  1538. {
  1539. CMDpiChanged(*M);
  1540. }
  1541. else
  1542. {
  1543. TForm::Dispatch(Message);
  1544. }
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. void __fastcall TLoginDialog::SetDefaultSessionActionExecute(
  1548. TObject * /*Sender*/)
  1549. {
  1550. UnicodeString Message = MainInstructions(LoadStr(SET_DEFAULT_SESSION_SETTINGS));
  1551. if (MessageDialog(Message, qtConfirmation,
  1552. qaOK | qaCancel, HELP_SESSION_SAVE_DEFAULT) == qaOK)
  1553. {
  1554. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  1555. SaveSession(SessionData.get());
  1556. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData.get());
  1557. StoredSessions->DefaultSettings = SessionData.get();
  1558. Default();
  1559. }
  1560. }
  1561. //---------------------------------------------------------------------------
  1562. void __fastcall TLoginDialog::ToolsMenuButtonClick(TObject * /*Sender*/)
  1563. {
  1564. MenuPopup(ToolsPopupMenu, ToolsMenuButton);
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. void __fastcall TLoginDialog::DesktopIconActionExecute(TObject * /*Sender*/)
  1568. {
  1569. TTreeNode * Node = SessionTree->Selected;
  1570. UnicodeString Message;
  1571. UnicodeString Name;
  1572. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  1573. int IconIndex = 0;
  1574. if (IsSiteNode(Node))
  1575. {
  1576. Name = GetNodeSession(Node)->Name;
  1577. Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT, (Name));
  1578. AddToList(AdditionalParams, TProgramParams::FormatSwitch(UPLOAD_IF_ANY_SWITCH), L" ");
  1579. IconIndex = SITE_ICON;
  1580. }
  1581. else if (IsFolderNode(Node))
  1582. {
  1583. Name = SessionNodePath(SessionTree->Selected);
  1584. Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT_FOLDER, (Name));
  1585. IconIndex = SITE_FOLDER_ICON;
  1586. }
  1587. else if (IsWorkspaceNode(Node))
  1588. {
  1589. Name = SessionNodePath(SessionTree->Selected);
  1590. Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT_WORKSPACE, (Name));
  1591. IconIndex = WORKSPACE_ICON;
  1592. }
  1593. else
  1594. {
  1595. DebugFail();
  1596. }
  1597. Message = MainInstructions(Message);
  1598. if (MessageDialog(Message, qtConfirmation, qaYes | qaNo, HELP_CREATE_SHORTCUT) == qaYes)
  1599. {
  1600. TInstantOperationVisualizer Visualizer;
  1601. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, IconIndex);
  1602. }
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. void __fastcall TLoginDialog::SendToHookActionExecute(TObject * /*Sender*/)
  1606. {
  1607. DebugAssert(IsSiteNode(SessionTree->Selected));
  1608. DebugAssert(SelectedSession != NULL);
  1609. UnicodeString Message = MainInstructions(FMTLOAD(CONFIRM_CREATE_SENDTO, (SelectedSession->Name)));
  1610. if (MessageDialog(Message,
  1611. qtConfirmation, qaYes | qaNo, HELP_CREATE_SENDTO) == qaYes)
  1612. {
  1613. TInstantOperationVisualizer Visualizer;
  1614. UnicodeString AdditionalParams =
  1615. TProgramParams::FormatSwitch(SEND_TO_HOOK_SWITCH) + L" " +
  1616. TProgramParams::FormatSwitch(UPLOAD_SWITCH);
  1617. CreateDesktopSessionShortCut(SelectedSession->Name,
  1618. FMTLOAD(SESSION_SENDTO_HOOK_NAME2, (SelectedSession->LocalName, AppName)),
  1619. AdditionalParams,
  1620. CSIDL_SENDTO, SITE_ICON);
  1621. }
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. bool __fastcall TLoginDialog::HasNodeAnySession(TTreeNode * Node, bool NeedCanLogin)
  1625. {
  1626. bool Result = false;
  1627. TTreeNode * ANode = Node->GetNext();
  1628. while (!Result && (ANode != NULL) && ANode->HasAsParent(Node))
  1629. {
  1630. Result =
  1631. IsSessionNode(ANode) &&
  1632. (!NeedCanLogin || FStoredSessions->CanLogin(GetNodeSession(ANode)));
  1633. ANode = ANode->GetNext();
  1634. }
  1635. return Result;
  1636. }
  1637. //---------------------------------------------------------------------------
  1638. void __fastcall TLoginDialog::SessionTreeCustomDrawItem(
  1639. TCustomTreeView * Sender, TTreeNode * Node, TCustomDrawState State,
  1640. bool & DefaultDraw)
  1641. {
  1642. TFontStyles Styles = Sender->Canvas->Font->Style;
  1643. if (IsSessionNode(Node) && GetNodeSession(Node)->Special)
  1644. {
  1645. Styles = Styles << fsBold << fsUnderline;
  1646. }
  1647. else
  1648. {
  1649. Styles = Styles >> fsBold >> fsUnderline;
  1650. }
  1651. if (State.Empty() && !Node->DropTarget)
  1652. {
  1653. if (IsFolderOrWorkspaceNode(Node))
  1654. {
  1655. if (!HasNodeAnySession(Node))
  1656. {
  1657. Sender->Canvas->Font->Color = clGrayText;
  1658. }
  1659. }
  1660. }
  1661. Sender->Canvas->Font->Style = Styles;
  1662. DefaultDraw = true;
  1663. }
  1664. //---------------------------------------------------------------------------
  1665. void __fastcall TLoginDialog::CheckForUpdatesActionExecute(TObject * /*Sender*/)
  1666. {
  1667. CheckForUpdates(false);
  1668. }
  1669. //---------------------------------------------------------------------------
  1670. void __fastcall TLoginDialog::HelpButtonClick(TObject * /*Sender*/)
  1671. {
  1672. FormHelp(this);
  1673. }
  1674. //---------------------------------------------------------------------------
  1675. TModalResult __fastcall TLoginDialog::DefaultResult()
  1676. {
  1677. return ::DefaultResult(this, LoginButton);
  1678. }
  1679. //---------------------------------------------------------------------------
  1680. void __fastcall TLoginDialog::FormCloseQuery(TObject * /*Sender*/,
  1681. bool & CanClose)
  1682. {
  1683. // CanClose test is now probably redundant,
  1684. // once we have a fallback to LoginButton in DefaultResult
  1685. CanClose = EnsureNotEditing();
  1686. // When CanLogin is false, the DefaultResult() will fail finding a default button
  1687. if (CanClose && CanLogin() && (ModalResult == DefaultResult()))
  1688. {
  1689. SaveDataList(FDataList);
  1690. }
  1691. }
  1692. //---------------------------------------------------------------------------
  1693. void __fastcall TLoginDialog::SessionTreeEditing(TObject * /*Sender*/,
  1694. TTreeNode * Node, bool & AllowEdit)
  1695. {
  1696. DebugAssert(!FRenaming);
  1697. AllowEdit =
  1698. IsFolderOrWorkspaceNode(Node) ||
  1699. (DebugAlwaysTrue(IsSiteNode(Node)) && !GetNodeSession(Node)->Special);
  1700. FRenaming = AllowEdit;
  1701. UpdateControls();
  1702. }
  1703. //---------------------------------------------------------------------------
  1704. void __fastcall TLoginDialog::RenameSessionActionExecute(TObject * /*Sender*/)
  1705. {
  1706. if (SessionTree->Selected != NULL)
  1707. {
  1708. // would be more appropriate in SessionTreeEditing, but it does not work there
  1709. ResetSitesIncrementalSearch();
  1710. SessionTree->Selected->EditText();
  1711. }
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. void __fastcall TLoginDialog::CheckDuplicateFolder(TTreeNode * Parent,
  1715. UnicodeString Text, TTreeNode * Node)
  1716. {
  1717. TTreeNode * ANode =
  1718. ((Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1719. Parent->getFirstChild());
  1720. // note that we allow folder with the same name as existing session
  1721. // on the same level (see also AddSession)
  1722. while ((ANode != NULL) &&
  1723. ((ANode == Node) || IsSessionNode(ANode) || !AnsiSameText(ANode->Text, Text)))
  1724. {
  1725. ANode = ANode->getNextSibling();
  1726. }
  1727. if (ANode != NULL)
  1728. {
  1729. throw Exception(MainInstructions(FMTLOAD(LOGIN_DUPLICATE_SESSION_FOLDER_WORKSPACE, (Text))));
  1730. }
  1731. }
  1732. //---------------------------------------------------------------------------
  1733. void __fastcall TLoginDialog::CheckIsSessionFolder(TTreeNode * Node)
  1734. {
  1735. if ((Node != NULL) && (Node->Parent != NULL))
  1736. {
  1737. CheckIsSessionFolder(Node->Parent);
  1738. }
  1739. if (IsWorkspaceNode(Node))
  1740. {
  1741. throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (SessionNodePath(Node))));
  1742. }
  1743. }
  1744. //---------------------------------------------------------------------------
  1745. void __fastcall TLoginDialog::SessionTreeEdited(TObject * /*Sender*/,
  1746. TTreeNode * Node, UnicodeString & S)
  1747. {
  1748. if ((Node->Text != S) && !S.IsEmpty())
  1749. {
  1750. TSessionData * Session = SelectedSession;
  1751. TSessionData::ValidateName(S);
  1752. if (Session != NULL)
  1753. {
  1754. UnicodeString Path = UnixExtractFilePath(Session->Name) + S;
  1755. SessionNameValidate(Path, Session->Name);
  1756. // remove from storage
  1757. Session->Remove();
  1758. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  1759. // modified only, explicit
  1760. StoredSessions->Save(false, true);
  1761. // the session may be the same, if only letter case has changed
  1762. if (Session != NewSession)
  1763. {
  1764. // if we overwrite existing session, remove the original item
  1765. // (we must not delete the node we are editing)
  1766. TTreeNode * ANode =
  1767. ((Node->Parent == NULL) ? SessionTree->Items->GetFirstNode() :
  1768. Node->Parent->getFirstChild());
  1769. while ((ANode != NULL) && (ANode->Data != NewSession))
  1770. {
  1771. ANode = ANode->getNextSibling();
  1772. }
  1773. if (ANode != NULL)
  1774. {
  1775. ANode->Delete();
  1776. }
  1777. Node->Data = NewSession;
  1778. DestroySession(Session);
  1779. }
  1780. }
  1781. else
  1782. {
  1783. CheckDuplicateFolder(Node->Parent, S, Node);
  1784. UnicodeString ParentPath = UnixIncludeTrailingBackslash(SessionNodePath(Node->Parent));
  1785. UnicodeString OldRoot = ParentPath + Node->Text;
  1786. UnicodeString NewRoot = ParentPath + S;
  1787. bool AnySession = false;
  1788. TSortType PrevSortType = SessionTree->SortType;
  1789. // temporarily disable automatic sorting, so that nodes are kept in order
  1790. // while we traverse them. otherwise it may happen that we omit some.
  1791. SessionTree->SortType = Comctrls::stNone;
  1792. try
  1793. {
  1794. TTreeNode * ANode = Node->GetNext();
  1795. while ((ANode != NULL) && ANode->HasAsParent(Node))
  1796. {
  1797. if (IsSessionNode(ANode))
  1798. {
  1799. AnySession = true;
  1800. TSessionData * Session = GetNodeSession(ANode);
  1801. // remove from storage
  1802. Session->Remove();
  1803. UnicodeString Path = Session->Name;
  1804. DebugAssert(Path.SubString(1, OldRoot.Length()) == OldRoot);
  1805. Path.Delete(1, OldRoot.Length());
  1806. Path.Insert(NewRoot, 1);
  1807. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  1808. // the session may be the same, if only letter case has changed
  1809. if (NewSession != Session)
  1810. {
  1811. ANode->Data = NewSession;
  1812. DestroySession(Session);
  1813. }
  1814. }
  1815. ANode = ANode->GetNext();
  1816. }
  1817. }
  1818. __finally
  1819. {
  1820. SessionTree->SortType = PrevSortType;
  1821. }
  1822. if (AnySession)
  1823. {
  1824. // modified only, explicit
  1825. StoredSessions->Save(false, true);
  1826. }
  1827. }
  1828. }
  1829. }
  1830. //---------------------------------------------------------------------------
  1831. int __fastcall TLoginDialog::FSProtocolToIndex(TFSProtocol FSProtocol,
  1832. bool & AllowScpFallback)
  1833. {
  1834. if (FSProtocol == fsSFTP)
  1835. {
  1836. AllowScpFallback = true;
  1837. bool Dummy;
  1838. return FSProtocolToIndex(fsSFTPonly, Dummy);
  1839. }
  1840. else
  1841. {
  1842. AllowScpFallback = false;
  1843. for (int Index = 0; Index < TransferProtocolCombo->Items->Count; Index++)
  1844. {
  1845. if (FSOrder[Index] == FSProtocol)
  1846. {
  1847. return Index;
  1848. }
  1849. }
  1850. // SFTP is always present
  1851. return FSProtocolToIndex(fsSFTP, AllowScpFallback);
  1852. }
  1853. }
  1854. //---------------------------------------------------------------------------
  1855. TFSProtocol __fastcall TLoginDialog::IndexToFSProtocol(int Index, bool AllowScpFallback)
  1856. {
  1857. bool InBounds = (Index >= 0) && (Index < static_cast<int>(LENOF(FSOrder)));
  1858. // can be temporary "unselected" while new language is being loaded
  1859. DebugAssert(InBounds || (Index == -1));
  1860. TFSProtocol Result = fsSFTP;
  1861. if (InBounds)
  1862. {
  1863. Result = FSOrder[Index];
  1864. if ((Result == fsSFTPonly) && AllowScpFallback)
  1865. {
  1866. Result = fsSFTP;
  1867. }
  1868. }
  1869. return Result;
  1870. }
  1871. //---------------------------------------------------------------------------
  1872. int __fastcall TLoginDialog::FtpsToIndex(TFtps Ftps)
  1873. {
  1874. switch (Ftps)
  1875. {
  1876. default:
  1877. DebugFail();
  1878. case ftpsNone:
  1879. return 0;
  1880. case ftpsImplicit:
  1881. return 1;
  1882. case ftpsExplicitTls:
  1883. case ftpsExplicitSsl:
  1884. return 2;
  1885. }
  1886. }
  1887. //---------------------------------------------------------------------------
  1888. TFtps __fastcall TLoginDialog::GetFtps()
  1889. {
  1890. int Index = ((GetFSProtocol(false) == fsWebDAV) ? WebDavsCombo->ItemIndex : FtpsCombo->ItemIndex);
  1891. TFtps Ftps;
  1892. switch (Index)
  1893. {
  1894. default:
  1895. DebugFail();
  1896. case 0:
  1897. Ftps = ftpsNone;
  1898. break;
  1899. case 1:
  1900. Ftps = ftpsImplicit;
  1901. break;
  1902. case 2:
  1903. Ftps = ftpsExplicitTls;
  1904. break;
  1905. }
  1906. return Ftps;
  1907. }
  1908. //---------------------------------------------------------------------------
  1909. TFSProtocol __fastcall TLoginDialog::GetFSProtocol(bool RequireScpFallbackDistinction)
  1910. {
  1911. bool AllowScpFallback = false;
  1912. if (RequireScpFallbackDistinction && DebugAlwaysTrue(FSessionData != NULL))
  1913. {
  1914. FSProtocolToIndex(FSessionData->FSProtocol, AllowScpFallback);
  1915. }
  1916. return IndexToFSProtocol(TransferProtocolCombo->ItemIndex, AllowScpFallback);
  1917. }
  1918. //---------------------------------------------------------------------------
  1919. int __fastcall TLoginDialog::DefaultPort()
  1920. {
  1921. return ::DefaultPort(GetFSProtocol(false), GetFtps());
  1922. }
  1923. //---------------------------------------------------------------------------
  1924. void __fastcall TLoginDialog::TransferProtocolComboChange(TObject * Sender)
  1925. {
  1926. if (!NoUpdate)
  1927. {
  1928. if (GetFSProtocol(false) == fsS3)
  1929. {
  1930. FtpsCombo->ItemIndex = FtpsToIndex(ftpsImplicit);
  1931. HostNameEdit->Text = S3HostName;
  1932. }
  1933. }
  1934. int ADefaultPort = DefaultPort();
  1935. if (!NoUpdate && FUpdatePortWithProtocol)
  1936. {
  1937. NoUpdate++;
  1938. try
  1939. {
  1940. if (PortNumberEdit->AsInteger == FDefaultPort)
  1941. {
  1942. PortNumberEdit->AsInteger = ADefaultPort;
  1943. }
  1944. }
  1945. __finally
  1946. {
  1947. NoUpdate--;
  1948. }
  1949. }
  1950. FDefaultPort = ADefaultPort;
  1951. DataChange(Sender);
  1952. }
  1953. //---------------------------------------------------------------------------
  1954. void __fastcall TLoginDialog::NavigationTreeCollapsing(
  1955. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & AllowCollapse)
  1956. {
  1957. AllowCollapse = false;
  1958. }
  1959. //---------------------------------------------------------------------------
  1960. void __fastcall TLoginDialog::SessionTreeExpandedCollapsed(TObject * /*Sender*/,
  1961. TTreeNode * Node)
  1962. {
  1963. UpdateFolderNode(Node);
  1964. }
  1965. //---------------------------------------------------------------------------
  1966. void __fastcall TLoginDialog::SessionTreeCompare(TObject * /*Sender*/,
  1967. TTreeNode * Node1, TTreeNode * Node2, int /*Data*/, int & Compare)
  1968. {
  1969. bool Node1IsNewSite = IsNewSiteNode(Node1);
  1970. bool Node2IsNewSite = IsNewSiteNode(Node2);
  1971. bool Node1IsWorkspace = IsWorkspaceNode(Node1);
  1972. bool Node2IsWorkspace = IsWorkspaceNode(Node2);
  1973. bool Node1IsFolder = IsFolderNode(Node1);
  1974. bool Node2IsFolder = IsFolderNode(Node2);
  1975. if (Node1IsNewSite && !Node2IsNewSite)
  1976. {
  1977. Compare = -1;
  1978. }
  1979. else if (!Node1IsNewSite && Node2IsNewSite)
  1980. {
  1981. Compare = 1;
  1982. }
  1983. else if (Node1IsWorkspace && !Node2IsWorkspace)
  1984. {
  1985. Compare = -1;
  1986. }
  1987. else if (!Node1IsWorkspace && Node2IsWorkspace)
  1988. {
  1989. Compare = 1;
  1990. }
  1991. else if (Node1IsFolder && !Node2IsFolder)
  1992. {
  1993. Compare = -1;
  1994. }
  1995. else if (!Node1IsFolder && Node2IsFolder)
  1996. {
  1997. Compare = 1;
  1998. }
  1999. else if (Node1IsWorkspace || Node1IsFolder)
  2000. {
  2001. Compare = CompareLogicalText(Node1->Text, Node2->Text, true);
  2002. }
  2003. else
  2004. {
  2005. Compare = NamedObjectSortProc(Node1->Data, Node2->Data);
  2006. }
  2007. }
  2008. //---------------------------------------------------------------------------
  2009. void __fastcall TLoginDialog::NewSessionFolderInputDialogInitialize(
  2010. TObject * /*Sender*/, TInputDialogData * Data)
  2011. {
  2012. TCustomEdit * Edit = Data->Edit;
  2013. int P = Edit->Text.LastDelimiter(L"/");
  2014. if (P > 0)
  2015. {
  2016. Edit->SetFocus();
  2017. Edit->SelStart = P;
  2018. Edit->SelLength = Edit->Text.Length() - P;
  2019. }
  2020. }
  2021. //---------------------------------------------------------------------------
  2022. TTreeNode * __fastcall TLoginDialog::SessionFolderNode(TTreeNode * Node)
  2023. {
  2024. TTreeNode * Parent;
  2025. if (IsSessionNode(Node))
  2026. {
  2027. Parent = Node->Parent;
  2028. }
  2029. else if (IsFolderNode(Node))
  2030. {
  2031. Parent = Node;
  2032. }
  2033. else if (IsWorkspaceNode(Node))
  2034. {
  2035. Parent = NULL;
  2036. }
  2037. else
  2038. {
  2039. DebugAssert(Node == NULL);
  2040. Parent = NULL;
  2041. }
  2042. return Parent;
  2043. }
  2044. //---------------------------------------------------------------------------
  2045. TTreeNode * __fastcall TLoginDialog::CurrentSessionFolderNode()
  2046. {
  2047. return SessionFolderNode(SessionTree->Selected);
  2048. }
  2049. //---------------------------------------------------------------------------
  2050. void __fastcall TLoginDialog::NewSessionFolderActionExecute(
  2051. TObject * /*Sender*/)
  2052. {
  2053. UnicodeString Name =
  2054. UnixIncludeTrailingBackslash(SessionNodePath(CurrentSessionFolderNode())) +
  2055. LoadStr(NEW_FOLDER);
  2056. if (InputDialog(LoadStr(LOGIN_NEW_SESSION_FOLDER_CAPTION),
  2057. LoadStr(LOGIN_NEW_SESSION_FOLDER_PROMPT), Name, HELP_NEW_SESSION_FOLDER,
  2058. NULL, true, NewSessionFolderInputDialogInitialize))
  2059. {
  2060. Name = UnixExcludeTrailingBackslash(Name);
  2061. if (!Name.IsEmpty())
  2062. {
  2063. TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Name), true, false);
  2064. // this does not prevent creation of subfolder under workspace,
  2065. // if user creates more levels at once (but it does not show up anyway)
  2066. CheckIsSessionFolder(Parent);
  2067. CheckDuplicateFolder(Parent, UnixExtractFileName(Name), NULL);
  2068. TTreeNode * Node = AddSessionPath(Name, true, false);
  2069. SessionTree->Selected = Node;
  2070. Node->MakeVisible();
  2071. }
  2072. }
  2073. }
  2074. //---------------------------------------------------------------------------
  2075. TTreeNode * __fastcall TLoginDialog::NormalizeDropTarget(TTreeNode * DropTarget)
  2076. {
  2077. return IsWorkspaceNode(DropTarget) ? DropTarget : SessionFolderNode(DropTarget);
  2078. }
  2079. //---------------------------------------------------------------------------
  2080. bool __fastcall TLoginDialog::SessionAllowDrop(TTreeNode * DropTarget)
  2081. {
  2082. DropTarget = NormalizeDropTarget(DropTarget);
  2083. return
  2084. (SessionTree->Selected != NULL) &&
  2085. (SessionTree->Selected->Parent != DropTarget) &&
  2086. ((DropTarget == NULL) || IsFolderNode(DropTarget));
  2087. }
  2088. //---------------------------------------------------------------------------
  2089. void __fastcall TLoginDialog::SessionTreeProc(TMessage & AMessage)
  2090. {
  2091. if (AMessage.Msg == CM_DRAG)
  2092. {
  2093. TCMDrag & Message = reinterpret_cast<TCMDrag &>(AMessage);
  2094. // reimplement dmDragMove to avoid TCustomTreeView.DoDragOver,
  2095. // which resets DropTarget to pointed-to node
  2096. // (note that this disables OnDragOver event handler)
  2097. if ((Message.DragMessage == dmDragMove) ||
  2098. (Message.DragMessage == dmDragEnter) ||
  2099. (Message.DragMessage == dmDragLeave))
  2100. {
  2101. if (Message.DragMessage != dmDragMove)
  2102. {
  2103. // must call it at least for dmDragLeave, because it does some cleanup,
  2104. // but we need to override result below, as it defaults to "not accepted"
  2105. FOldSessionTreeProc(AMessage);
  2106. }
  2107. TDragControlObject * DragObject = dynamic_cast<TDragControlObject *>(Message.DragRec->Source);
  2108. if ((DragObject != NULL) && (DragObject->Control == SessionTree))
  2109. {
  2110. TPoint P = SessionTree->ScreenToClient(Message.DragRec->Pos);
  2111. TTreeNode * Node = SessionTree->GetNodeAt(P.x, P.y);
  2112. if (!SessionAllowDrop(Node))
  2113. {
  2114. DropTarget = NULL;
  2115. Message.Result = 0;
  2116. }
  2117. else
  2118. {
  2119. Message.Result = 1;
  2120. }
  2121. if (Message.DragMessage == dmDragMove)
  2122. {
  2123. SessionTree->DropTarget = NormalizeDropTarget(Node);
  2124. }
  2125. FScrollOnDragOver->DragOver(P);
  2126. }
  2127. else
  2128. {
  2129. Message.Result = 0;
  2130. }
  2131. }
  2132. else
  2133. {
  2134. FOldSessionTreeProc(AMessage);
  2135. }
  2136. }
  2137. else
  2138. {
  2139. FOldSessionTreeProc(AMessage);
  2140. }
  2141. }
  2142. //---------------------------------------------------------------------------
  2143. void __fastcall TLoginDialog::SessionTreeStartDrag(TObject * /*Sender*/,
  2144. TDragObject *& /*DragObject*/)
  2145. {
  2146. DebugAssert(SessionTree->Selected != NULL);
  2147. // neither session folders/workspaces, nor special sessions can be dragged
  2148. if ((SessionTree->Selected == NULL) ||
  2149. IsFolderOrWorkspaceNode(SessionTree->Selected) ||
  2150. IsNewSiteNode(SessionTree->Selected) ||
  2151. (IsSiteNode(SessionTree->Selected) && SelectedSession->Special))
  2152. {
  2153. Abort();
  2154. }
  2155. FScrollOnDragOver->StartDrag();
  2156. }
  2157. //---------------------------------------------------------------------------
  2158. void __fastcall TLoginDialog::SessionTreeDragDrop(TObject * Sender,
  2159. TObject * Source, int /*X*/, int /*Y*/)
  2160. {
  2161. TTreeNode * DropTarget = SessionTree->DropTarget;
  2162. if (DebugAlwaysTrue((Sender == Source) && SessionAllowDrop(DropTarget)) &&
  2163. // calling EnsureNotEditing only on drop, not on drag start,
  2164. // to avoid getting popup during unintended micro-dragging
  2165. EnsureNotEditing())
  2166. {
  2167. TSessionData * Session = SelectedSession;
  2168. UnicodeString Path =
  2169. UnixIncludeTrailingBackslash(SessionNodePath(DropTarget)) +
  2170. UnixExtractFileName(Session->SessionName);
  2171. SessionNameValidate(Path, Session->SessionName);
  2172. // remove from storage
  2173. Session->Remove();
  2174. TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
  2175. // modified only, explicit
  2176. StoredSessions->Save(false, true);
  2177. // this should aways be the case
  2178. if (DebugAlwaysTrue(Session != NewSession))
  2179. {
  2180. TTreeNode * Node = SessionTree->Selected;
  2181. // look for overwritten node (if any)
  2182. TTreeNode * ANode = SessionTree->Items->GetFirstNode();
  2183. while (ANode != NULL)
  2184. {
  2185. if (ANode->Data == NewSession)
  2186. {
  2187. ANode->Delete();
  2188. break;
  2189. }
  2190. ANode = ANode->GetNext();
  2191. }
  2192. Node->MoveTo(DropTarget, naAddChild);
  2193. Node->Data = NewSession;
  2194. // try to make both visible
  2195. if (DropTarget != NULL)
  2196. {
  2197. DropTarget->MakeVisible();
  2198. }
  2199. Node->MakeVisible();
  2200. DestroySession(Session);
  2201. }
  2202. }
  2203. }
  2204. //---------------------------------------------------------------------------
  2205. UnicodeString __fastcall TLoginDialog::GetFolderOrWorkspaceContents(
  2206. TTreeNode * Node, const UnicodeString & Indent, const UnicodeString & CommonRoot)
  2207. {
  2208. UnicodeString Contents;
  2209. UnicodeString Path = SessionNodePath(Node);
  2210. std::unique_ptr<TStrings> Names(FStoredSessions->GetFolderOrWorkspaceList(Path));
  2211. for (int Index = 0; Index < Names->Count; Index++)
  2212. {
  2213. UnicodeString Name = Names->Strings[Index];
  2214. if (StartsStr(CommonRoot, Name))
  2215. {
  2216. Name.Delete(1, CommonRoot.Length());
  2217. }
  2218. Contents += Indent + Name + L"\n";
  2219. }
  2220. return Contents;
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TLoginDialog::SessionTreeMouseMove(TObject * /*Sender*/,
  2224. TShiftState /*Shift*/, int X, int Y)
  2225. {
  2226. TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
  2227. THitTests HitTest = SessionTree->GetHitTestInfoAt(X, Y);
  2228. if (Node != FHintNode)
  2229. {
  2230. Application->CancelHint();
  2231. UnicodeString Hint;
  2232. if (HitTest.Contains(htOnItem) || HitTest.Contains(htOnIcon) ||
  2233. HitTest.Contains(htOnLabel) || HitTest.Contains(htOnStateIcon))
  2234. {
  2235. FHintNode = Node;
  2236. if (IsSiteNode(Node))
  2237. {
  2238. Hint = GetNodeSession(Node)->InfoTip;
  2239. }
  2240. else if (IsWorkspaceNode(Node))
  2241. {
  2242. UnicodeString Path = SessionNodePath(Node);
  2243. Hint =
  2244. FMTLOAD(WORKSPACE_INFO_TIP, (Path)) + L"\n" +
  2245. // trim the trailing new line
  2246. TrimRight(GetFolderOrWorkspaceContents(Node, L" ", UnicodeString()));
  2247. }
  2248. else
  2249. {
  2250. Hint = L"";
  2251. }
  2252. }
  2253. else
  2254. {
  2255. FHintNode = NULL;
  2256. Hint = L"";
  2257. }
  2258. SessionTree->Hint = Hint;
  2259. }
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. void __fastcall TLoginDialog::SessionTreeEndDrag(TObject * /*Sender*/,
  2263. TObject * /*Target*/, int /*X*/, int /*Y*/)
  2264. {
  2265. FScrollOnDragOver->EndDrag();
  2266. }
  2267. //---------------------------------------------------------------------------
  2268. void __fastcall TLoginDialog::AnonymousLoginCheckClick(TObject * /*Sender*/)
  2269. {
  2270. if (AnonymousLoginCheck->Checked)
  2271. {
  2272. UserNameEdit->Text = AnonymousUserName;
  2273. PasswordEdit->Text = AnonymousPassword;
  2274. }
  2275. UpdateControls();
  2276. }
  2277. //---------------------------------------------------------------------------
  2278. void __fastcall TLoginDialog::SaveButtonDropDownClick(TObject * /*Sender*/)
  2279. {
  2280. MenuPopup(SaveDropDownMenu, SaveButton);
  2281. }
  2282. //---------------------------------------------------------------------------
  2283. void __fastcall TLoginDialog::SessionTreeExpanding(TObject * /*Sender*/,
  2284. TTreeNode * Node, bool & AllowExpansion)
  2285. {
  2286. // to prevent workspace expansion
  2287. AllowExpansion = IsFolderNode(Node);
  2288. }
  2289. //---------------------------------------------------------------------------
  2290. void __fastcall TLoginDialog::RunPageantActionExecute(TObject * /*Sender*/)
  2291. {
  2292. ExecuteTool(PageantTool);
  2293. }
  2294. //---------------------------------------------------------------------------
  2295. void __fastcall TLoginDialog::RunPuttygenActionExecute(TObject * /*Sender*/)
  2296. {
  2297. ExecuteTool(PuttygenTool);
  2298. }
  2299. //---------------------------------------------------------------------------
  2300. void __fastcall TLoginDialog::PortNumberEditChange(TObject * Sender)
  2301. {
  2302. if (!NoUpdate)
  2303. {
  2304. bool WellKnownPort = false;
  2305. TFSProtocol FSProtocol;
  2306. TFtps Ftps = ftpsNone;
  2307. int PortNumber = PortNumberEdit->AsInteger;
  2308. if (PortNumber == SshPortNumber)
  2309. {
  2310. FSProtocol = fsSFTP;
  2311. WellKnownPort = true;
  2312. }
  2313. else if (PortNumber == FtpPortNumber)
  2314. {
  2315. FSProtocol = fsFTP;
  2316. WellKnownPort = true;
  2317. }
  2318. else if (PortNumber == FtpsImplicitPortNumber)
  2319. {
  2320. FSProtocol = fsFTP;
  2321. Ftps = ftpsImplicit;
  2322. WellKnownPort = true;
  2323. }
  2324. else if (PortNumber == HTTPPortNumber)
  2325. {
  2326. FSProtocol = fsWebDAV;
  2327. WellKnownPort = true;
  2328. }
  2329. else if (PortNumber == HTTPSPortNumber)
  2330. {
  2331. FSProtocol = fsWebDAV;
  2332. Ftps = ftpsImplicit;
  2333. WellKnownPort = true;
  2334. }
  2335. if (WellKnownPort)
  2336. {
  2337. bool AllowScpFallback;
  2338. int ProtocolIndex = FSProtocolToIndex(FSProtocol, AllowScpFallback);
  2339. if ((TransferProtocolCombo->ItemIndex == ProtocolIndex) &&
  2340. (GetFtps() == Ftps))
  2341. {
  2342. FUpdatePortWithProtocol = true;
  2343. }
  2344. else
  2345. {
  2346. FUpdatePortWithProtocol = false;
  2347. NoUpdate++;
  2348. try
  2349. {
  2350. TransferProtocolCombo->ItemIndex = ProtocolIndex;
  2351. int FtpsIndex = FtpsToIndex(Ftps);
  2352. FtpsCombo->ItemIndex = FtpsIndex;
  2353. WebDavsCombo->ItemIndex = FtpsIndex;
  2354. }
  2355. __finally
  2356. {
  2357. NoUpdate--;
  2358. }
  2359. }
  2360. }
  2361. }
  2362. DataChange(Sender);
  2363. }
  2364. //---------------------------------------------------------------------------
  2365. void __fastcall TLoginDialog::ExportActionExecute(TObject * /*Sender*/)
  2366. {
  2367. UnicodeString FileName = Configuration->GetDefaultIniFileExportPath();
  2368. if (SaveDialog(LoadStr(EXPORT_CONF_TITLE), LoadStr(EXPORT_CONF_FILTER), L"ini", FileName))
  2369. {
  2370. Configuration->Export(FileName);
  2371. }
  2372. }
  2373. //---------------------------------------------------------------------------
  2374. void __fastcall TLoginDialog::ImportActionExecute(TObject * /*Sender*/)
  2375. {
  2376. if (MessageDialog(MainInstructions(LoadStr(IMPORT_CONFIGURATION)),
  2377. qtWarning, qaOK | qaCancel, HELP_IMPORT_CONFIGURATION) == qaOK)
  2378. {
  2379. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  2380. OpenDialog->Title = LoadStr(IMPORT_CONF_TITLE);
  2381. OpenDialog->Filter = LoadStr(EXPORT_CONF_FILTER);
  2382. OpenDialog->DefaultExt = L"ini";
  2383. OpenDialog->FileName = Configuration->GetDefaultIniFileExportPath();
  2384. if (OpenDialog->Execute())
  2385. {
  2386. // before the session list gets destroyed
  2387. SessionTree->Items->Clear();
  2388. Configuration->Import(OpenDialog->FileName);
  2389. ReloadSessions(L"");
  2390. if (SessionTree->Items->Count > 0)
  2391. {
  2392. SessionTree->Items->GetFirstNode()->MakeVisible();
  2393. }
  2394. }
  2395. }
  2396. }
  2397. //---------------------------------------------------------------------------
  2398. void __fastcall TLoginDialog::ResetSitesIncrementalSearch()
  2399. {
  2400. FSitesIncrementalSearch = L"";
  2401. // this is to prevent active tree node being set back to Sites tab
  2402. // (from UpdateNavigationTree) when we are called from SessionTreeExit,
  2403. // while tab is changing
  2404. if (NoUpdate == 0)
  2405. {
  2406. UpdateControls();
  2407. }
  2408. }
  2409. //---------------------------------------------------------------------------
  2410. bool __fastcall TLoginDialog::SitesIncrementalSearch(const UnicodeString & Text,
  2411. bool SkipCurrent, bool Reverse)
  2412. {
  2413. TTreeNode * Node = SearchSite(Text, false, SkipCurrent, Reverse);
  2414. if (Node == NULL)
  2415. {
  2416. Node = SearchSite(Text, true, SkipCurrent, Reverse);
  2417. if (Node != NULL)
  2418. {
  2419. TTreeNode * Parent = Node->Parent;
  2420. while (Parent != NULL)
  2421. {
  2422. Parent->Expand(false);
  2423. Parent = Parent->Parent;
  2424. }
  2425. }
  2426. }
  2427. bool Result = (Node != NULL);
  2428. if (Result)
  2429. {
  2430. {
  2431. TAutoNestingCounter Guard(FIncrementalSearching);
  2432. SessionTree->Selected = Node;
  2433. }
  2434. FSitesIncrementalSearch = Text;
  2435. // Tab always searches even in collapsed nodes
  2436. TTreeNode * NextNode = SearchSite(Text, true, true, Reverse);
  2437. FSitesIncrementalSearchHaveNext =
  2438. (NextNode != NULL) && (NextNode != Node);
  2439. UpdateControls();
  2440. // make visible only after search panel is shown, what may obscure the node
  2441. Node->MakeVisible();
  2442. }
  2443. return Result;
  2444. }
  2445. //---------------------------------------------------------------------------
  2446. TTreeNode * __fastcall TLoginDialog::GetNextNode(TTreeNode * Node, bool Reverse)
  2447. {
  2448. if (Reverse)
  2449. {
  2450. Node = Node->GetPrev();
  2451. if (Node == NULL)
  2452. {
  2453. // GetLastNode
  2454. // https://stackoverflow.com/q/6257348/850848
  2455. Node = SessionTree->Items->GetFirstNode();
  2456. TTreeNode * Node2 = Node;
  2457. if (Node2 != NULL)
  2458. {
  2459. do
  2460. {
  2461. Node2 = Node;
  2462. if (Node != NULL)
  2463. {
  2464. Node = Node2->getNextSibling();
  2465. }
  2466. if (Node == NULL)
  2467. {
  2468. Node = Node2->getFirstChild();
  2469. }
  2470. }
  2471. while (Node != NULL);
  2472. }
  2473. Node = Node2;
  2474. }
  2475. }
  2476. else
  2477. {
  2478. Node = Node->GetNext();
  2479. if (Node == NULL)
  2480. {
  2481. Node = SessionTree->Items->GetFirstNode();
  2482. }
  2483. }
  2484. return Node;
  2485. }
  2486. //---------------------------------------------------------------------------
  2487. static bool __fastcall ContainsTextSemiCaseSensitive(
  2488. const UnicodeString & Text, const UnicodeString & SubText)
  2489. {
  2490. bool Result;
  2491. if (AnsiLowerCase(SubText) == SubText)
  2492. {
  2493. Result = ContainsText(Text, SubText);
  2494. }
  2495. else
  2496. {
  2497. Result = ContainsStr(Text, SubText);
  2498. }
  2499. return Result;
  2500. }
  2501. //---------------------------------------------------------------------------
  2502. TTreeNode * __fastcall TLoginDialog::SearchSite(const UnicodeString & Text,
  2503. bool AllowExpanding, bool SkipCurrent, bool Reverse)
  2504. {
  2505. TTreeNode * CurrentNode =
  2506. (SessionTree->Selected != NULL) ? SessionTree->Selected : SessionTree->Items->GetFirstNode();
  2507. if (CurrentNode == NULL)
  2508. {
  2509. return NULL;
  2510. }
  2511. else
  2512. {
  2513. TTreeNode * Node = CurrentNode;
  2514. if (SkipCurrent)
  2515. {
  2516. Node = GetNextNode(Node, Reverse);
  2517. if (Node == NULL)
  2518. {
  2519. return NULL;
  2520. }
  2521. }
  2522. while (true)
  2523. {
  2524. bool Eligible = true;
  2525. TTreeNode * Parent = Node->Parent;
  2526. while (Eligible && (Parent != NULL))
  2527. {
  2528. Eligible =
  2529. IsFolderNode(Parent) &&
  2530. (Parent->Expanded || AllowExpanding);
  2531. Parent = Parent->Parent;
  2532. }
  2533. if (Eligible)
  2534. {
  2535. bool Matches = false;
  2536. switch (FSiteSearch)
  2537. {
  2538. case ssSiteNameStartOnly:
  2539. Matches = ContainsTextSemiCaseSensitive(Node->Text.SubString(1, Text.Length()), Text);
  2540. break;
  2541. case ssSiteName:
  2542. Matches = ContainsTextSemiCaseSensitive(Node->Text, Text);
  2543. break;
  2544. case ssSite:
  2545. Matches = ContainsTextSemiCaseSensitive(Node->Text, Text);
  2546. if (!Matches && IsSiteNode(Node))
  2547. {
  2548. TSessionData * Data = GetNodeSession(Node);
  2549. Matches =
  2550. ContainsTextSemiCaseSensitive(Data->HostName, Text) ||
  2551. ContainsTextSemiCaseSensitive(Data->UserName, Text) ||
  2552. ContainsTextSemiCaseSensitive(Data->Note, Text);
  2553. }
  2554. break;
  2555. }
  2556. if (Matches)
  2557. {
  2558. return Node;
  2559. }
  2560. }
  2561. Node = GetNextNode(Node, Reverse);
  2562. if (Node == CurrentNode)
  2563. {
  2564. return NULL;
  2565. }
  2566. }
  2567. }
  2568. }
  2569. //---------------------------------------------------------------------------
  2570. void __fastcall TLoginDialog::SessionTreeExit(TObject * /*Sender*/)
  2571. {
  2572. ResetSitesIncrementalSearch();
  2573. }
  2574. //---------------------------------------------------------------------------
  2575. bool __fastcall TLoginDialog::EnsureNotEditing()
  2576. {
  2577. bool Result = !FEditing;
  2578. if (!Result)
  2579. {
  2580. UnicodeString Message = MainInstructions(LoadStr(LOGIN_SAVE_EDITING));
  2581. unsigned int Answer = MessageDialog(Message, qtConfirmation, qaYes |qaNo | qaCancel);
  2582. switch (Answer)
  2583. {
  2584. case qaYes:
  2585. SaveAsSession(false);
  2586. Result = true;
  2587. break;
  2588. case qaNo:
  2589. CancelEditing();
  2590. // Make sure OK button gets enabled
  2591. UpdateControls();
  2592. Result = true;
  2593. break;
  2594. default:
  2595. // noop;
  2596. break;
  2597. }
  2598. }
  2599. return Result;
  2600. }
  2601. //---------------------------------------------------------------------------
  2602. void __fastcall TLoginDialog::SessionTreeChanging(TObject * /*Sender*/,
  2603. TTreeNode * /*Node*/, bool & AllowChange)
  2604. {
  2605. if (!EnsureNotEditing())
  2606. {
  2607. AllowChange = false;
  2608. }
  2609. else
  2610. {
  2611. PersistNewSiteIfNeeded();
  2612. }
  2613. }
  2614. //---------------------------------------------------------------------------
  2615. void __fastcall TLoginDialog::PersistNewSiteIfNeeded()
  2616. {
  2617. // Visible: Never try to save data if we did not load any yet,
  2618. // otherwise we get "HostNameEdit" and such
  2619. if (Visible && IsNewSiteNode(SessionTree->Selected))
  2620. {
  2621. SaveSession(FNewSiteData);
  2622. }
  2623. }
  2624. //---------------------------------------------------------------------------
  2625. void __fastcall TLoginDialog::SessionAdvancedActionExecute(TObject * /*Sender*/)
  2626. {
  2627. // If we ever allow showing advanced settings, while read-only,
  2628. // we must make sure that FSessionData actually holds the advanced settings,
  2629. // what it currently does not, in order to avoid master password prompt,
  2630. // while cloning the session data in LoadSession.
  2631. // To implement this, we may delegate the cloning to TWinConfiguration and
  2632. // make use of FDontDecryptPasswords
  2633. if (DebugAlwaysTrue(FSessionData != NULL))
  2634. {
  2635. // parse hostname (it may change protocol particularly) before opening advanced settings
  2636. // (HostNameEditExit is not triggered when child dialog pops up when it is invoked by accelerator)
  2637. // We should better handle this automaticaly when focus is moved to another dialog.
  2638. ParseHostName();
  2639. SaveSession(FSessionData);
  2640. DoSiteAdvancedDialog(FSessionData);
  2641. // Needed only for Note.
  2642. // The only other property visible on Login dialog that Advanced site dialog
  2643. // can change is protocol (between fsSFTP and fsSFTPonly),
  2644. // difference of the two not being visible on Login dialog anyway.
  2645. LoadSession(FSessionData);
  2646. if (DebugAlwaysTrue(SessionTree->Selected != NULL) &&
  2647. IsSiteNode(SessionTree->Selected))
  2648. {
  2649. SetNodeImage(SessionTree->Selected, GetSessionImageIndex(FSessionData));
  2650. }
  2651. }
  2652. }
  2653. //---------------------------------------------------------------------------
  2654. TPopupMenu * __fastcall TLoginDialog::GetSelectedNodePopupMenu()
  2655. {
  2656. TPopupMenu * PopupMenu = NULL;
  2657. TTreeNode * Selected = SessionTree->Selected;
  2658. if (IsNewSiteNode(Selected))
  2659. {
  2660. PopupMenu = ManageNewSitePopupMenu;
  2661. }
  2662. else if (IsSiteNode(Selected))
  2663. {
  2664. PopupMenu = ManageSitePopupMenu;
  2665. }
  2666. else if (IsFolderNode(Selected))
  2667. {
  2668. PopupMenu = ManageFolderPopupMenu;
  2669. }
  2670. else if (IsWorkspaceNode(Selected))
  2671. {
  2672. PopupMenu = ManageWorkspacePopupMenu;
  2673. }
  2674. return DebugNotNull(PopupMenu);
  2675. }
  2676. //---------------------------------------------------------------------------
  2677. void __fastcall TLoginDialog::ManageButtonClick(TObject * /*Sender*/)
  2678. {
  2679. MenuPopup(GetSelectedNodePopupMenu(), ManageButton);
  2680. }
  2681. //---------------------------------------------------------------------------
  2682. void __fastcall TLoginDialog::SessionTreeMouseDown(TObject * /*Sender*/,
  2683. TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  2684. {
  2685. TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
  2686. if ((Button == mbRight) && (Node != NULL))
  2687. {
  2688. SessionTree->Selected = Node;
  2689. }
  2690. }
  2691. //---------------------------------------------------------------------------
  2692. void __fastcall TLoginDialog::SessionTreeContextPopup(TObject * /*Sender*/,
  2693. TPoint & MousePos, bool & Handled)
  2694. {
  2695. ResetSitesIncrementalSearch();
  2696. TTreeNode * Node = SessionTree->GetNodeAt(MousePos.X, MousePos.Y);
  2697. // This is mostly to prevent context menu from poping up,
  2698. // while there is prompt to confirm cancelling session edit,
  2699. // when right mouse is clicked on non-selected node
  2700. if (Node != SessionTree->Selected)
  2701. {
  2702. Handled = true;
  2703. }
  2704. else
  2705. {
  2706. if (SessionTree->Selected != NULL)
  2707. {
  2708. SessionTree->PopupMenu = GetSelectedNodePopupMenu();
  2709. if (DebugNotNull(SessionTree->PopupMenu))
  2710. {
  2711. MenuPopup(SessionTree, MousePos, Handled);
  2712. }
  2713. }
  2714. }
  2715. }
  2716. //---------------------------------------------------------------------------
  2717. void __fastcall TLoginDialog::EditCancelActionExecute(TObject * /*Sender*/)
  2718. {
  2719. CancelEditing();
  2720. // reset back to saved settings
  2721. LoadContents();
  2722. UpdateControls();
  2723. // we do not want to see blinking cursor in read-only edit box
  2724. SessionTree->SetFocus();
  2725. }
  2726. //---------------------------------------------------------------------------
  2727. void __fastcall TLoginDialog::AdvancedButtonDropDownClick(TObject * /*Sender*/)
  2728. {
  2729. MenuPopup(SessionAdvancedPopupMenu, AdvancedButton);
  2730. }
  2731. //---------------------------------------------------------------------------
  2732. void __fastcall TLoginDialog::CancelEditing()
  2733. {
  2734. FEditing = false;
  2735. // reset back the color
  2736. UpdateNodeImage(SessionTree->Selected);
  2737. }
  2738. //---------------------------------------------------------------------------
  2739. void __fastcall TLoginDialog::CloneToNewSite()
  2740. {
  2741. FNewSiteData->CopyData(SelectedSession);
  2742. FNewSiteData->MakeUniqueIn(FStoredSessions);
  2743. FNewSiteKeepName = true;
  2744. NewSite();
  2745. EditSession();
  2746. }
  2747. //---------------------------------------------------------------------------
  2748. void __fastcall TLoginDialog::CloneToNewSiteActionExecute(TObject * /*Sender*/)
  2749. {
  2750. CloneToNewSite();
  2751. }
  2752. //---------------------------------------------------------------------------
  2753. void __fastcall TLoginDialog::Login()
  2754. {
  2755. if (OpenInNewWindow() && !IsNewSiteNode(SessionTree->Selected))
  2756. {
  2757. UnicodeString Path = SessionNodePath(SessionTree->Selected);
  2758. ExecuteNewInstance(EncodeUrlString(Path));
  2759. // prevent closing the window, see below
  2760. ModalResult = mrNone;
  2761. }
  2762. else
  2763. {
  2764. // this is not needed when used from LoginButton,
  2765. // but is needed when used from popup menus
  2766. ModalResult = LoginButton->ModalResult;
  2767. }
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. void __fastcall TLoginDialog::LoginActionExecute(TObject * /*Sender*/)
  2771. {
  2772. Login();
  2773. }
  2774. //---------------------------------------------------------------------------
  2775. void __fastcall TLoginDialog::PuttyActionExecute(TObject * /*Sender*/)
  2776. {
  2777. // following may take some time, so cache the shift key state,
  2778. // in case user manages to release it before following finishes
  2779. bool Close = !OpenInNewWindow();
  2780. std::unique_ptr<TSessionData> Data(CloneSelectedSession());
  2781. // putty does not support resolving environment variables in session settings
  2782. Data->ExpandEnvironmentVariables();
  2783. OpenSessionInPutty(GUIConfiguration->PuttyPath, Data.get());
  2784. if (Close)
  2785. {
  2786. ModalResult = CloseButton->ModalResult;
  2787. }
  2788. }
  2789. //---------------------------------------------------------------------------
  2790. void __fastcall TLoginDialog::LoginButtonDropDownClick(TObject * /*Sender*/)
  2791. {
  2792. MenuPopup(LoginDropDownMenu, LoginButton);
  2793. }
  2794. //---------------------------------------------------------------------------
  2795. void __fastcall TLoginDialog::ParseUrl(const UnicodeString & Url)
  2796. {
  2797. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  2798. SaveSession(SessionData.get());
  2799. // We do not want to pass in StoredSessions as we do not want the URL be
  2800. // parsed as pointing to a stored site.
  2801. // It also prevents resetting to defaults (do we want this?)
  2802. bool DefaultsOnly; // unused
  2803. SessionData->ParseUrl(Url, NULL, NULL, DefaultsOnly, NULL, NULL, NULL, 0);
  2804. LoadSession(SessionData.get());
  2805. }
  2806. //---------------------------------------------------------------------------
  2807. void __fastcall TLoginDialog::PasteUrlActionExecute(TObject * /*Sender*/)
  2808. {
  2809. UnicodeString ClipboardUrl;
  2810. if (NonEmptyTextFromClipboard(ClipboardUrl))
  2811. {
  2812. if (!IsEditable())
  2813. {
  2814. // select new site node, when other node is selected and not in editing mode
  2815. SessionTree->Selected = GetNewSiteNode();
  2816. }
  2817. // sanity check
  2818. if (DebugAlwaysTrue(IsEditable()))
  2819. {
  2820. ParseUrl(ClipboardUrl);
  2821. }
  2822. // visualize the pasting
  2823. HostNameEdit->SetFocus();
  2824. HostNameEdit->SelectAll();
  2825. }
  2826. }
  2827. //---------------------------------------------------------------------------
  2828. void __fastcall TLoginDialog::ParseHostName()
  2829. {
  2830. UnicodeString HostName = HostNameEdit->Text;
  2831. if (!HostName.IsEmpty() &&
  2832. (StoredSessions->IsUrl(HostName) || (HostName.Pos(L"@") > 0)))
  2833. {
  2834. ParseUrl(HostName);
  2835. }
  2836. }
  2837. //---------------------------------------------------------------------------
  2838. void __fastcall TLoginDialog::HostNameEditExit(TObject * /*Sender*/)
  2839. {
  2840. ParseHostName();
  2841. }
  2842. //---------------------------------------------------------------------------
  2843. void __fastcall TLoginDialog::GenerateUrlAction2Execute(TObject * /*Sender*/)
  2844. {
  2845. if (DebugAlwaysTrue(SelectedSession != NULL))
  2846. {
  2847. PersistNewSiteIfNeeded();
  2848. std::unique_ptr<TSessionData> Data(SelectedSession->Clone());
  2849. Data->LookupLastFingerprint();
  2850. DoGenerateUrlDialog(Data.get(), NULL);
  2851. }
  2852. }
  2853. //---------------------------------------------------------------------------
  2854. void __fastcall TLoginDialog::CopyParamRuleActionExecute(TObject * /*Sender*/)
  2855. {
  2856. TSessionData * Data = GetSessionData();
  2857. std::unique_ptr<TCopyParamList> CopyParamList(new TCopyParamList());
  2858. (*CopyParamList) = *GUIConfiguration->CopyParamList;
  2859. TCopyParamRuleData RuleData;
  2860. RuleData.HostName = Data->HostNameExpanded;
  2861. RuleData.UserName = Data->UserNameExpanded;
  2862. int CopyParamIndex = CopyParamList->Find(RuleData);
  2863. if (CopyParamIndex < 0)
  2864. {
  2865. TCopyParamRuleData RuleDataHostNameOnly;
  2866. RuleDataHostNameOnly.HostName = Data->HostNameExpanded;
  2867. CopyParamIndex = CopyParamList->Find(RuleDataHostNameOnly);
  2868. }
  2869. TCopyParamPresetMode Mode;
  2870. TCopyParamRuleData * CurrentRuleData = NULL;
  2871. if (CopyParamIndex < 0)
  2872. {
  2873. Mode = cpmAddCurrent;
  2874. CurrentRuleData = &RuleData;
  2875. }
  2876. else
  2877. {
  2878. Mode = cpmEdit;
  2879. }
  2880. TCopyParamType DefaultCopyParams;
  2881. if (DoCopyParamPresetDialog(
  2882. CopyParamList.get(), CopyParamIndex, Mode, CurrentRuleData, DefaultCopyParams))
  2883. {
  2884. GUIConfiguration->CopyParamList = CopyParamList.get();
  2885. }
  2886. }
  2887. //---------------------------------------------------------------------------
  2888. void __fastcall TLoginDialog::SearchSiteNameStartOnlyActionExecute(TObject * /*Sender*/)
  2889. {
  2890. FSiteSearch = ssSiteNameStartOnly;
  2891. }
  2892. //---------------------------------------------------------------------------
  2893. void __fastcall TLoginDialog::SearchSiteNameActionExecute(TObject * /*Sender*/)
  2894. {
  2895. FSiteSearch = ssSiteName;
  2896. }
  2897. //---------------------------------------------------------------------------
  2898. void __fastcall TLoginDialog::SearchSiteActionExecute(TObject * /*Sender*/)
  2899. {
  2900. FSiteSearch = ssSite;
  2901. }
  2902. //---------------------------------------------------------------------------
  2903. void __fastcall TLoginDialog::ChangeScale(int M, int D)
  2904. {
  2905. TForm::ChangeScale(M, D);
  2906. FSiteButtonsPadding = MulDiv(FSiteButtonsPadding, M, D);
  2907. FBasicGroupBaseHeight = MulDiv(FBasicGroupBaseHeight, M, D);
  2908. FNoteGroupOffset = MulDiv(FNoteGroupOffset, M, D);
  2909. }
  2910. //---------------------------------------------------------------------------