Login.cpp 97 KB

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