Login.cpp 96 KB

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