Login.cpp 97 KB

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