Login.cpp 97 KB

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