Login.cpp 98 KB

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