1
0

Login.cpp 97 KB

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