Login.cpp 101 KB

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