Login.cpp 98 KB

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