Login.cpp 97 KB

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