Login.cpp 97 KB

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