1
0

Login.cpp 108 KB

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