Login.cpp 95 KB

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