Login.cpp 107 KB

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