Login.cpp 95 KB

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