Login.cpp 95 KB

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