Login.cpp 108 KB

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