Login.cpp 98 KB

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