Login.cpp 84 KB

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