Login.cpp 104 KB

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