Login.cpp 97 KB

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