Login.cpp 93 KB

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