Login.cpp 96 KB

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