Login.cpp 98 KB

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