Login.cpp 102 KB

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