Login.cpp 97 KB

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