Login.cpp 94 KB

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