Login.cpp 97 KB

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