| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099 | 
							- //---------------------------------------------------------------------
 
- #include <vcl.h>
 
- #pragma hdrstop
 
- #include <StrUtils.hpp>
 
- #include <CoreMain.h>
 
- #include <Common.h>
 
- #include <PuttyTools.h>
 
- #include <TextsWin.h>
 
- #include <TextsCore.h>
 
- #include <HelpWin.h>
 
- #include <VCLCommon.h>
 
- #include <limits>
 
- #include "WinInterface.h"
 
- #include "Login.h"
 
- #include "GUITools.h"
 
- #include "Tools.h"
 
- #include "Setup.h"
 
- #include "WinConfiguration.h"
 
- #include "ProgParams.h"
 
- #include "WinApi.h"
 
- //---------------------------------------------------------------------
 
- #pragma link "ComboEdit"
 
- #pragma link "PasswordEdit"
 
- #pragma link "UpDownEdit"
 
- #pragma resource "*.dfm"
 
- //---------------------------------------------------------------------------
 
- const int SiteImageIndex = 1;
 
- const int OpenFolderImageIndex = 2;
 
- const int ClosedFolderImageIndex = 3;
 
- const int WorkspaceImageIndex = 4;
 
- const int NewSiteImageIndex = 6;
 
- const int SiteColorMaskImageIndex = 8;
 
- //---------------------------------------------------------------------------
 
- bool __fastcall DoLoginDialog(TList * DataList, TForm * LinkedForm)
 
- {
 
-   DebugAssert(DataList != NULL);
 
-   TLoginDialog * LoginDialog = SafeFormCreate<TLoginDialog>();
 
-   bool Result;
 
-   try
 
-   {
 
-     LoginDialog->Init(LinkedForm);
 
-     Result = LoginDialog->Execute(DataList);
 
-   }
 
-   __finally
 
-   {
 
-     delete LoginDialog;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------
 
- static const TFSProtocol FSOrder[] = { fsSFTPonly, fsSCPonly, fsFTP, fsWebDAV, fsS3 };
 
- //---------------------------------------------------------------------
 
- __fastcall TLoginDialog::TLoginDialog(TComponent* AOwner)
 
-         : TForm(AOwner)
 
- {
 
-   FNewSiteData = new TSessionData(L"");
 
-   FInitialized = false;
 
-   FHintNode = NULL;
 
-   FScrollOnDragOver = new TTreeViewScrollOnDragOver(SessionTree, true);
 
-   FDataList = NULL;
 
-   FUpdatePortWithProtocol = true;
 
-   FIncrementalSearching = 0;
 
-   FSitesIncrementalSearchHaveNext = false;
 
-   FEditing = false;
 
-   FRenaming = false;
 
-   FNewSiteKeepName = false;
 
-   FForceNewSite = false;
 
-   FLoading = false;
 
-   FSortEnablePending = false;
 
-   FSiteSearch = isName;
 
-   FLinkedForm = NULL;
 
-   FPrevPos = TPoint(std::numeric_limits<LONG>::min(), std::numeric_limits<LONG>::min());
 
-   // we need to make sure that window procedure is set asap
 
-   // (so that CM_SHOWINGCHANGED handling is applied)
 
-   UseSystemSettingsPre(this);
 
-   FBasicGroupBaseHeight = BasicGroup->Height - BasicSshPanel->Height - BasicFtpPanel->Height;
 
-   FNoteGroupOffset = NoteGroup->Top - (BasicGroup->Top + BasicGroup->Height);
 
-   FUserNameLabel = UserNameLabel->Caption;
 
-   FPasswordLabel = PasswordLabel->Caption;
 
-   FSiteButtonsPadding = SitesPanel->ClientHeight - ToolsMenuButton->Top - ToolsMenuButton->Height;
 
-   HideComponentsPanel(this);
 
- }
 
- //---------------------------------------------------------------------
 
- __fastcall TLoginDialog::~TLoginDialog()
 
- {
 
-   delete FScrollOnDragOver;
 
-   delete FNewSiteData;
 
-   InvalidateSessionData();
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::InvalidateSessionData()
 
- {
 
-   delete FSessionData;
 
-   FSessionData = NULL;
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::Init(TForm * LinkedForm)
 
- {
 
-   FLinkedForm = LinkedForm;
 
-   LoadSessions();
 
-   UnicodeString Dummy;
 
-   RunPageantAction->Visible = FindTool(PageantTool, Dummy);
 
-   RunPuttygenAction->Visible = FindTool(PuttygenTool, Dummy);
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::InitControls()
 
- {
 
-   if (SessionTree->WindowProc != SessionTreeProc)
 
-   {
 
-     FOldSessionTreeProc = SessionTree->WindowProc;
 
-     SessionTree->WindowProc = SessionTreeProc;
 
-   }
 
-   int FtpsNoneIndex = FtpsToIndex(ftpsNone);
 
-   int FtpsImplicitIndex = FtpsToIndex(ftpsImplicit);
 
-   FtpsCombo->Items->Strings[FtpsImplicitIndex] = LoadStr(FTPS_IMPLICIT);
 
-   FtpsCombo->Items->Strings[FtpsToIndex(ftpsExplicitTls)] = LoadStr(FTPS_EXPLICIT);
 
-   WebDavsCombo->Items->Strings[FtpsNoneIndex] = FtpsCombo->Items->Strings[FtpsNoneIndex];
 
-   WebDavsCombo->Items->Strings[FtpsImplicitIndex] = FtpsCombo->Items->Strings[FtpsImplicitIndex];
 
-   BasicSshPanel->Top = BasicFtpPanel->Top;
 
-   SitesIncrementalSearchLabel->AutoSize = false;
 
-   SitesIncrementalSearchLabel->Left = SessionTree->Left;
 
-   SitesIncrementalSearchLabel->Width = SessionTree->Width;
 
-   SitesIncrementalSearchLabel->Top = SessionTree->BoundsRect.Bottom - SitesIncrementalSearchLabel->Height;
 
-   SitesIncrementalSearchLabel->Visible = false;
 
-   ReadOnlyControl(TransferProtocolView);
 
-   ReadOnlyControl(EncryptionView);
 
-   ReadOnlyControl(NoteMemo);
 
-   MenuButton(ToolsMenuButton);
 
-   MenuButton(ManageButton);
 
-   FixButtonImage(LoginButton);
 
-   CenterButtonImage(LoginButton);
 
-   SelectScaledImageList(SessionImageList);
 
-   // have to recreate the site images
 
-   UpdateNodeImages();
 
-   SelectScaledImageList(ActionImageList);
 
-   GenerateButtonImages();
 
-   if (SessionTree->Items->Count > 0)
 
-   {
 
-     SetNewSiteNodeLabel();
 
-   }
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::GenerateButtonImages()
 
- {
 
-   // Generate button images.
 
-   // The button does not support alpha channel,
 
-   // so we have to copy the PNG's to BMP's and use plain transparent color
 
-   FButtonImageList.reset(new TImageList(this));
 
-   FButtonImageList->SetSize(ActionImageList->Width, ActionImageList->Height);
 
-   LoginButton->Images = FButtonImageList.get();
 
-   LoginButton->ImageIndex = AddLoginButtonImage(true);
 
-   LoginButton->DisabledImageIndex = AddLoginButtonImage(false);
 
- }
 
- //---------------------------------------------------------------------
 
- int __fastcall TLoginDialog::AddLoginButtonImage(bool Enabled)
 
- {
 
-   std::unique_ptr<TBitmap> Bitmap(new TBitmap());
 
-   Bitmap->SetSize(ActionImageList->Width, ActionImageList->Height);
 
-   ActionImageList->Draw(Bitmap->Canvas, 0, 0, LoginAction->ImageIndex, Enabled);
 
-   const TColor TransparentColor = clFuchsia;
 
-   // 16x16 version does not have any background
 
-   if (Bitmap->Canvas->Pixels[0][0] == clWhite)
 
-   {
 
-     // A background is white, but there's also white used on the image itself.
 
-     // So we first replace the background white with a unique color,
 
-     // setting it as a transparent later.
 
-     // This is obviously a hack specific to this particular image.
 
-     Bitmap->Canvas->Brush->Color = TransparentColor;
 
-     Bitmap->Canvas->FloodFill(0, 0, Bitmap->Canvas->Pixels[0][0], fsSurface);
 
-   }
 
-   return FButtonImageList->AddMasked(Bitmap.get(), TransparentColor);
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::Init()
 
- {
 
-   FInitialized = true;
 
-   UseSystemSettingsPost(this);
 
-   Caption = FormatFormCaption(this, Caption);
 
-   InitControls();
 
-   ReadOnlyControl(ContentsNameEdit);
 
-   ReadOnlyControl(ContentsMemo);
 
-   if (DebugAlwaysFalse(SessionTree->Items->Count == 0) ||
 
-       ((SessionTree->Items->Count == 1) &&
 
-        DebugAlwaysTrue(IsNewSiteNode(SessionTree->Items->GetFirstNode()))) ||
 
-       FForceNewSite)
 
-   {
 
-     ActiveControl = HostNameEdit;
 
-   }
 
-   else
 
-   {
 
-     ActiveControl = SessionTree;
 
-   }
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::AddSessionPath(UnicodeString Path,
 
-   bool CanCreate, bool IsWorkspace)
 
- {
 
-   TTreeNode * Parent = NULL;
 
-   while (!Path.IsEmpty())
 
-   {
 
-     UnicodeString Folder = CutToChar(Path, L'/', false);
 
-     TTreeNode * Node =
 
-       ((Parent == NULL) ? SessionTree->Items->GetFirstNode() : Parent->getFirstChild());
 
-     // note that we allow folder with the same name as existing session
 
-     // on the same level (see also SessionTreeEdited)
 
-     while ((Node != NULL) && (IsSessionNode(Node) || !AnsiSameText(Node->Text, Folder)))
 
-     {
 
-       Node = Node->getNextSibling();
 
-     }
 
-     if (Node == NULL)
 
-     {
 
-       if (!CanCreate)
 
-       {
 
-         return NULL;
 
-       }
 
-       else
 
-       {
 
-         TTreeNode * AParent = Parent;
 
-         Parent = SessionTree->Items->AddChild(Parent, Folder);
 
-         // once workspace, forever workspace
 
-         if (!IsWorkspaceNode(Parent))
 
-         {
 
-           if (IsWorkspace)
 
-           {
 
-             SetNodeImage(Parent, WorkspaceImageIndex);
 
-           }
 
-           else
 
-           {
 
-             UpdateFolderNode(Parent);
 
-           }
 
-         }
 
-         // optimization
 
-         if (!FLoading)
 
-         {
 
-           // folders seem not to be sorted automatically (not having set the data property)
 
-           if (AParent == NULL)
 
-           {
 
-             SessionTree->Items->AlphaSort();
 
-           }
 
-           else
 
-           {
 
-             AParent->AlphaSort();
 
-           }
 
-         }
 
-       }
 
-     }
 
-     else
 
-     {
 
-       Parent = Node;
 
-     }
 
-   }
 
-   return Parent;
 
- }
 
- //---------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsFolderNode(TTreeNode * Node)
 
- {
 
-   return (Node != NULL) && (Node->Data == NULL) && (Node->ImageIndex != WorkspaceImageIndex);
 
- }
 
- //---------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsWorkspaceNode(TTreeNode * Node)
 
- {
 
-   return (Node != NULL) && (Node->Data == NULL) && (Node->ImageIndex == WorkspaceImageIndex);
 
- }
 
- //---------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsFolderOrWorkspaceNode(TTreeNode * Node)
 
- {
 
-   return (Node != NULL) && (Node->Data == NULL);
 
- }
 
- //---------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsSiteNode(TTreeNode * Node)
 
- {
 
-   return (Node != NULL) && (Node->Data != NULL) && (Node->Data != FNewSiteData);
 
- }
 
- //---------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsNewSiteNode(TTreeNode * Node)
 
- {
 
-   return (Node != NULL) && (Node->Data != NULL) && (Node->Data == FNewSiteData);
 
- }
 
- //---------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsSessionNode(TTreeNode * Node)
 
- {
 
-   return (Node != NULL) && (Node->Data != NULL);
 
- }
 
- //---------------------------------------------------------------------
 
- TSessionData * __fastcall TLoginDialog::GetNodeSession(TTreeNode * Node)
 
- {
 
-   return DebugNotNull(static_cast<TSessionData *>(Node->Data));
 
- }
 
- //---------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::AddSession(TSessionData * Data)
 
- {
 
-   TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Data->Name), true, Data->IsWorkspace);
 
-   TTreeNode * Node = SessionTree->Items->AddChild(Parent, UnixExtractFileName(Data->Name));
 
-   Node->Data = Data;
 
-   UpdateNodeImage(Node);
 
-   return Node;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::UpdateNodeImages()
 
- {
 
-   SessionTree->Images->BeginUpdate();
 
-   try
 
-   {
 
-     TTreeNode * Node = SessionTree->Items->GetFirstNode();
 
-     while (Node != NULL)
 
-     {
 
-       if (IsSiteNode(Node))
 
-       {
 
-         UpdateNodeImage(Node);
 
-       }
 
-       Node = Node->GetNext();
 
-     }
 
-   }
 
-   __finally
 
-   {
 
-     SessionTree->Images->EndUpdate();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::UpdateNodeImage(TTreeNode * Node)
 
- {
 
-   SetNodeImage(Node, GetSessionImageIndex(GetNodeSession(Node)));
 
- }
 
- //---------------------------------------------------------------------
 
- int __fastcall TLoginDialog::GetSessionImageIndex(TSessionData * Data)
 
- {
 
-   int Result;
 
-   if (Data->Color != 0)
 
-   {
 
-     Result = GetSessionColorImage(SessionTree->Images, static_cast<TColor>(Data->Color), SiteColorMaskImageIndex);
 
-   }
 
-   else
 
-   {
 
-     Result = SiteImageIndex;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SetNodeImage(TTreeNode * Node, int ImageIndex)
 
- {
 
-   Node->ImageIndex = ImageIndex;
 
-   Node->SelectedIndex = ImageIndex;
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::DestroySession(TSessionData * Data)
 
- {
 
-   StoredSessions->Remove(Data);
 
- }
 
- //---------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::GetNewSiteNode()
 
- {
 
-   TTreeNode * Result = SessionTree->Items->GetFirstNode();
 
-   DebugAssert(IsNewSiteNode(Result));
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SetNewSiteNodeLabel()
 
- {
 
-   GetNewSiteNode()->Text = LoadStr(LOGIN_NEW_SITE_NODE);
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::LoadSessions()
 
- {
 
-   {
 
-     // Otherwise, once the selected node is deleted, another code is selected and we get failure
 
-     // while trying to access its data somewhere in LoadContents
 
-     TAutoFlag LoadingFlag(FLoading);
 
-     SessionTree->Items->BeginUpdate();
 
-     SessionTree->Images->BeginUpdate();
 
-     try
 
-     {
 
-       // optimization
 
-       SessionTree->SortType = Comctrls::stNone;
 
-       SessionTree->Items->Clear();
 
-       TTreeNode * Node = SessionTree->Items->AddChild(NULL, L"");
 
-       Node->Data = FNewSiteData;
 
-       SetNewSiteNodeLabel();
 
-       SetNodeImage(Node, NewSiteImageIndex);
 
-       DebugAssert(StoredSessions != NULL);
 
-       for (int Index = 0; Index < StoredSessions->Count; Index++)
 
-       {
 
-         AddSession(StoredSessions->Sessions[Index]);
 
-       }
 
-     }
 
-     __finally
 
-     {
 
-       // Restore sorting. Moreover, folders would not be sorted automatically even when
 
-       // SortType is set (not having set the data property), so we would have to
 
-       // call AlphaSort here explicitly
 
-       SessionTree->SortType = Comctrls::stBoth;
 
-       SessionTree->Images->EndUpdate();
 
-       SessionTree->Items->EndUpdate();
 
-     }
 
-   }
 
-   SessionTree->Selected = SessionTree->Items->GetFirstNode();
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::UpdateFolderNode(TTreeNode * Node)
 
- {
 
-   DebugAssert((Node->ImageIndex == 0) ||
 
-     (Node->ImageIndex == OpenFolderImageIndex) || (Node->ImageIndex == ClosedFolderImageIndex));
 
-   SetNodeImage(Node, (Node->Expanded ? OpenFolderImageIndex : ClosedFolderImageIndex));
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::NewSite()
 
- {
 
-   TTreeNode * NewSiteNode = GetNewSiteNode();
 
-   if (DebugAlwaysTrue(IsNewSiteNode(NewSiteNode)))
 
-   {
 
-     SessionTree->Selected = NewSiteNode;
 
-   }
 
-   LoadContents();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ResetNewSiteData()
 
- {
 
-   if (DebugAlwaysTrue(StoredSessions != NULL))
 
-   {
 
-     FNewSiteData->CopyData(StoredSessions->DefaultSettings);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::Default()
 
- {
 
-   ResetNewSiteData();
 
-   NewSite();
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::LoadContents()
 
- {
 
-   bool UseContentsPanel;
 
-   TTreeNode * Node = SessionTree->Selected;
 
-   if (IsSessionNode(Node))
 
-   {
 
-     LoadSession(SelectedSession);
 
-     UseContentsPanel = false;
 
-   }
 
-   else if (DebugAlwaysTrue(IsFolderOrWorkspaceNode(Node)))
 
-   {
 
-     UnicodeString NodePath = SessionNodePath(Node);
 
-     ContentsNameEdit->Text = NodePath;
 
-     UnicodeString CommonRoot = IsFolderNode(Node) ? UnixIncludeTrailingBackslash(NodePath) : UnicodeString();
 
-     ContentsMemo->Lines->Text =
 
-       GetFolderOrWorkspaceContents(Node, L"", CommonRoot);
 
-     UseContentsPanel = true;
 
-     if (IsFolderNode(Node))
 
-     {
 
-       ContentsGroupBox->Caption = LoadStr(LOGIN_SITE_FOLDER_CAPTION);
 
-     }
 
-     else if (DebugAlwaysTrue(IsWorkspaceNode(Node)))
 
-     {
 
-       ContentsGroupBox->Caption = LoadStr(LOGIN_WORKSPACE_CAPTION);
 
-     }
 
-   }
 
-   SitePanel->Visible = !UseContentsPanel;
 
-   ContentsPanel->Visible = UseContentsPanel;
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::LoadSession(TSessionData * SessionData)
 
- {
 
-   WinConfiguration->BeginMasterPasswordSession();
 
-   try
 
-   {
 
-     UserNameEdit->Text = SessionData->UserName;
 
-     PortNumberEdit->AsInteger = SessionData->PortNumber;
 
-     bool Editable = IsEditable();
 
-     if (Editable)
 
-     {
 
-       PasswordEdit->Text = NormalizeString(SessionData->Password);
 
-     }
 
-     else
 
-     {
 
-       PasswordEdit->Text =
 
-         SessionData->HasPassword() ?
 
-           UnicodeString::StringOfChar(L'?', 16) : UnicodeString();
 
-     }
 
-     int FtpsIndex = FtpsToIndex(SessionData->Ftps);
 
-     FtpsCombo->ItemIndex = FtpsIndex;
 
-     WebDavsCombo->ItemIndex = FtpsIndex;
 
-     EncryptionView->Text =
 
-       DebugAlwaysTrue(FtpsCombo->ItemIndex >= WebDavsCombo->ItemIndex) ? FtpsCombo->Text : WebDavsCombo->Text;
 
-     bool AllowScpFallback;
 
-     TransferProtocolCombo->ItemIndex = FSProtocolToIndex(SessionData->FSProtocol, AllowScpFallback);
 
-     TransferProtocolView->Text = TransferProtocolCombo->Text;
 
-     // Only after loading TransferProtocolCombo, so that we do not overwrite it with default S3 hostname
 
-     HostNameEdit->Text = SessionData->HostName;
 
-     NoteGroup->Visible = !Trim(SessionData->Note).IsEmpty();
 
-     NoteMemo->Lines->Text = SessionData->Note;
 
-     // just in case TransferProtocolComboChange is not triggered
 
-     FDefaultPort = DefaultPort();
 
-     FUpdatePortWithProtocol = true;
 
-     if (SessionData != FSessionData)
 
-     {
 
-       // advanced
 
-       InvalidateSessionData();
 
-       // clone advanced settings only when really needed,
 
-       // see also note in SessionAdvancedActionExecute
 
-       if (Editable)
 
-       {
 
-         FSessionData = new TSessionData(L"");
 
-         FSessionData->Assign(SessionData);
 
-       }
 
-     }
 
-     else
 
-     {
 
-       // we should get here only when called from SessionAdvancedActionExecute
 
-       DebugAssert(Editable);
 
-     }
 
-   }
 
-   __finally
 
-   {
 
-     WinConfiguration->EndMasterPasswordSession();
 
-   }
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveSession(TSessionData * SessionData)
 
- {
 
-   // advanced
 
-   if (DebugAlwaysTrue(FSessionData != NULL))
 
-   {
 
-     SessionData->Assign(FSessionData);
 
-   }
 
-   // Basic page
 
-   SessionData->UserName = UserNameEdit->Text.Trim();
 
-   SessionData->PortNumber = PortNumberEdit->AsInteger;
 
-   // must be loaded after UserName, because HostName may be in format user@host
 
-   SessionData->HostName = HostNameEdit->Text.Trim();
 
-   SessionData->Password = PasswordEdit->Text;
 
-   SessionData->Ftps = GetFtps();
 
-   SessionData->FSProtocol =
 
-     // requiring SCP fallback distinction
 
-     GetFSProtocol(true);
 
-   TSessionData * EditingSessionData = GetEditingSessionData();
 
-   SessionData->Name =
 
-     (EditingSessionData != NULL) ? EditingSessionData->Name :
 
-         (FNewSiteKeepName ? SessionData->Name : SessionData->DefaultSessionName);
 
- }
 
- //---------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsEditable()
 
- {
 
-   return IsNewSiteNode(SessionTree->Selected) || FEditing;
 
- }
 
- //---------------------------------------------------------------------
 
- void __fastcall TLoginDialog::UpdateControls()
 
- {
 
-   if (Visible && FInitialized)
 
-   {
 
-     bool Editable = IsEditable();
 
-     TFSProtocol FSProtocol = GetFSProtocol(false);
 
-     bool SshProtocol = IsSshProtocol(FSProtocol);
 
-     bool FtpProtocol = (FSProtocol == fsFTP);
 
-     bool WebDavProtocol = (FSProtocol == fsWebDAV);
 
-     bool S3Protocol = (FSProtocol == fsS3);
 
-     // session
 
-     FtpsCombo->Visible = Editable && FtpProtocol;
 
-     FtpsLabel->Visible = FtpProtocol;
 
-     WebDavsCombo->Visible = Editable && WebDavProtocol;
 
-     WebDavsLabel->Visible = WebDavProtocol;
 
-     EncryptionView->Visible = !Editable && (FtpProtocol || WebDavProtocol);
 
-     BasicSshPanel->Visible = SshProtocol;
 
-     BasicFtpPanel->Visible = FtpProtocol && Editable;
 
-     // we do not support both at the same time
 
-     DebugAssert(!BasicSshPanel->Visible || !BasicFtpPanel->Visible);
 
-     BasicGroup->Height =
 
-       FBasicGroupBaseHeight +
 
-       (BasicSshPanel->Visible ? BasicSshPanel->Height : 0) +
 
-       (BasicFtpPanel->Visible ? BasicFtpPanel->Height : 0);
 
-     int NoteGroupTop = (BasicGroup->Top + BasicGroup->Height) + FNoteGroupOffset;
 
-     NoteGroup->SetBounds(
 
-       NoteGroup->Left, (BasicGroup->Top + BasicGroup->Height) + FNoteGroupOffset,
 
-       NoteGroup->Width, NoteGroup->Top + NoteGroup->Height - NoteGroupTop);
 
-     AnonymousLoginCheck->Checked =
 
-       SameText(UserNameEdit->Text, AnonymousUserName) &&
 
-       SameText(PasswordEdit->Text, AnonymousPassword);
 
-     TransferProtocolCombo->Visible = Editable;
 
-     TransferProtocolView->Visible = !TransferProtocolCombo->Visible;
 
-     ReadOnlyControl(HostNameEdit, !Editable);
 
-     ReadOnlyControl(PortNumberEdit, !Editable);
 
-     PortNumberEdit->ButtonsVisible = Editable;
 
-     // FSessionData may be NULL temporary even when Editable while switching nodes
 
-     bool NoAuth = Editable && SshProtocol && (FSessionData != NULL) && FSessionData->SshNoUserAuth;
 
-     ReadOnlyAndEnabledControl(UserNameEdit, !Editable, !NoAuth);
 
-     EnableControl(UserNameLabel, UserNameEdit->Enabled);
 
-     ReadOnlyAndEnabledControl(PasswordEdit, !Editable, !NoAuth);
 
-     EnableControl(PasswordLabel, PasswordEdit->Enabled);
 
-     UserNameLabel->Caption = S3Protocol ? LoadStr(S3_ACCESS_KEY_ID_PROMPT) : FUserNameLabel;
 
-     PasswordLabel->Caption = S3Protocol ? LoadStr(S3_SECRET_ACCESS_KEY_PROMPT) : FPasswordLabel;
 
-     // sites
 
-     if (SitesIncrementalSearchLabel->Visible != !FSitesIncrementalSearch.IsEmpty())
 
-     {
 
-       if (FSitesIncrementalSearch.IsEmpty())
 
-       {
 
-         SitesIncrementalSearchLabel->Visible = false;
 
-         SessionTree->Height = SitesIncrementalSearchLabel->BoundsRect.Bottom - SessionTree->Top;
 
-       }
 
-       else
 
-       {
 
-         SitesIncrementalSearchLabel->Visible = true;
 
-         SessionTree->Height = SitesIncrementalSearchLabel->BoundsRect.Top - SessionTree->Top;
 
-       }
 
-     }
 
-     if (!FSitesIncrementalSearch.IsEmpty())
 
-     {
 
-       SitesIncrementalSearchLabel->Caption = FormatIncrementalSearchStatus(FSitesIncrementalSearch, FSitesIncrementalSearchHaveNext);
 
-     }
 
-     EnableControl(ManageButton, !FEditing);
 
-     EnableControl(ToolsMenuButton, !FEditing);
 
-     EnableControl(CloseButton, !FEditing);
 
-     DefaultButton(LoginButton, !FEditing && !FRenaming && !IsCloneToNewSiteDefault());
 
-     CloseButton->Cancel = !FEditing && !FRenaming;
 
-     DefaultButton(SaveButton, FEditing);
 
-     EditCancelButton->Cancel = FEditing;
 
-     SiteClonetoNewSiteMenuItem->Default = IsCloneToNewSiteDefault();
 
-     SiteLoginMenuItem->Default = LoginButton->Default;
 
-     UpdateButtonVisibility(SaveButton);
 
-     UpdateButtonVisibility(EditButton);
 
-     UpdateButtonVisibility(EditCancelButton);
 
-     SaveAsSessionMenuItem->Visible = FEditing;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::UpdateButtonVisibility(TButton * Button)
 
- {
 
-   TAction * Action = DebugNotNull(dynamic_cast<TAction *>(Button->Action));
 
-   // when all action targets are hidden, action does not get updated,
 
-   // so we need to do it manually
 
-   Action->Update();
 
-   // button visibility cannot be bound to action visibility,
 
-   // so we do not bother setting action visibility, instead we manually
 
-   // bind visibility to enabled state
 
-   Button->Visible = Action->Enabled;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::DataChange(TObject * /*Sender*/)
 
- {
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::FormShow(TObject * /*Sender*/)
 
- {
 
-   // this is called twice on startup, first with ControlState = [csRecreating]
 
-   // we should probably filter this out, it would avoid need for explicit
 
-   // LoadContents call below
 
-   bool NeedInitialize = !FInitialized;
 
-   if (NeedInitialize)
 
-   {
 
-     Init();
 
-   }
 
-   // WORKAROUND for a bug in the VCL layout code for bottom aligned controls
 
-   int Offset = (SitesPanel->ClientHeight - FSiteButtonsPadding - ToolsMenuButton->Height) - ToolsMenuButton->Top;
 
-   ToolsMenuButton->Top = ToolsMenuButton->Top + Offset;
 
-   ManageButton->Top = ManageButton->Top + Offset;
 
-   SessionTree->Height = SessionTree->Height + Offset;
 
-   // Bit of a hack: Assume an auto open, when we are linked to the main form
 
-   ShowAgainPanel->Visible = (FLinkedForm != NULL);
 
-   // among other this makes the expanded nodes look like expanded,
 
-   // because the LoadState call in Execute would be too early,
 
-   // and some stray call to collapsed event during showing process,
 
-   // make the image be set to collapsed.
 
-   // Also LoadState calls RestoreFormSize that has to be
 
-   // called only after DoFormWindowProc(CM_SHOWINGCHANGED).
 
-   // See also comment about MakeVisible in LoadState().
 
-   LoadState();
 
-   if (NeedInitialize)
 
-   {
 
-     // Need to load contents only after state (as that selects initial node).
 
-     // Explicit call is needed, as we get here during csRecreating phase,
 
-     // when SessionTreeChange is not triggered, see initial method comment
 
-     LoadContents();
 
-   }
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeChange(TObject * /*Sender*/,
 
-   TTreeNode * /*Node*/)
 
- {
 
-   if (!FLoading)
 
-   {
 
-     if (FIncrementalSearching <= 0)
 
-     {
 
-       // Make sure UpdateControls is called here, no matter what,
 
-       // now it is always called from ResetSitesIncrementalSearch.
 
-       // For the "else" scenario, UpdateControls is called later from SitesIncrementalSearch.
 
-       ResetSitesIncrementalSearch();
 
-     }
 
-     if (FInitialized)
 
-     {
 
-       LoadContents();
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TSessionData * __fastcall TLoginDialog::GetSessionData()
 
- {
 
-   if (SelectedSession == FNewSiteData)
 
-   {
 
-     SaveSession(FNewSiteData);
 
-   }
 
-   return SelectedSession;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeDblClick(TObject * /*Sender*/)
 
- {
 
-   TPoint P = SessionTree->ScreenToClient(Mouse->CursorPos);
 
-   TTreeNode * Node = SessionTree->GetNodeAt(P.x, P.y);
 
-   // This may be false, when collapsed folder was double-clicked,
 
-   // it got expanded, view was shifted to accommodate folder contents,
 
-   // so that cursor now points to a different node (site).
 
-   // This has to be evaluated before EnsureNotEditing,
 
-   // as that may pop-up modal box.
 
-   if (Node == SessionTree->Selected)
 
-   {
 
-     // EnsureNotEditing must be before CanLogin, as CanLogin checks for FEditing
 
-     if (EnsureNotEditing())
 
-     {
 
-       if (IsCloneToNewSiteDefault())
 
-       {
 
-         CloneToNewSite();
 
-       }
 
-       // this can hardle be false
 
-       // (after editing and clone tests above)
 
-       // (except for empty folders, but those do not pass a condition below)
 
-       else if (CanLogin())
 
-       {
 
-         if (IsSessionNode(Node) || IsWorkspaceNode(Node))
 
-         {
 
-           Login();
 
-         }
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TSessionData * __fastcall TLoginDialog::GetSelectedSession()
 
- {
 
-   // Selected can be temporarily NULL, e.g. while deleting selected node
 
-   TTreeNode * Node = SessionTree->Selected;
 
-   if ((Node != NULL) &&
 
-       (IsSiteNode(Node) || IsNewSiteNode(Node)))
 
-   {
 
-     return GetNodeSession(Node);
 
-   }
 
-   else
 
-   {
 
-     return NULL;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeKeyDown(TObject * /*Sender*/,
 
-   WORD & Key, TShiftState /*Shift*/)
 
- {
 
-   if (!SessionTree->IsEditing())
 
-   {
 
-     if (Key == VK_DELETE)
 
-     {
 
-       DeleteSessionAction->Execute();
 
-       Key = 0;
 
-     }
 
-     else if (Key == VK_F2)
 
-     {
 
-       RenameSessionAction->Execute();
 
-       Key = 0;
 
-     }
 
-     else if (Key == VK_BACK)
 
-     {
 
-       Key = 0;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeKeyPress(TObject * /*Sender*/, System::WideChar & Key)
 
- {
 
-   if (!SessionTree->IsEditing())
 
-   {
 
-     // filter control sequences
 
-     if (Key >= VK_SPACE)
 
-     {
 
-       if (FSitesIncrementalSearch.IsEmpty())
 
-       {
 
-         Configuration->Usage->Inc(L"SiteIncrementalSearches");
 
-       }
 
-       if (!SitesIncrementalSearch(FSitesIncrementalSearch + Key, false, false))
 
-       {
 
-         MessageBeep(MB_ICONHAND);
 
-       }
 
-       Key = 0;
 
-     }
 
-     else if (Key == VK_BACK)
 
-     {
 
-       if (!FSitesIncrementalSearch.IsEmpty())
 
-       {
 
-         if (FSitesIncrementalSearch.Length() == 1)
 
-         {
 
-           ResetSitesIncrementalSearch();
 
-         }
 
-         else
 
-         {
 
-           UnicodeString NewText =
 
-             FSitesIncrementalSearch.SubString(1, FSitesIncrementalSearch.Length() - 1);
 
-           SitesIncrementalSearch(NewText, false, false);
 
-         }
 
-         Key = 0;
 
-       }
 
-     }
 
-     else if ((Key == VK_RETURN) && IsCloneToNewSiteDefault())
 
-     {
 
-       CloneToNewSite();
 
-       Key = 0;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::EditSession()
 
- {
 
-   HostNameEdit->SetFocus();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::EditSessionActionExecute(TObject * /*Sender*/)
 
- {
 
-   if (DebugAlwaysTrue(SelectedSession != NULL))
 
-   {
 
-     FEditing = true;
 
-     EditSession();
 
-     // reload session, to make sure we load decrypted password
 
-     LoadContents();
 
-     UpdateControls();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::FindSessionNode(TSessionData * SessionData, bool ByName)
 
- {
 
-   TTreeNode * Node = SessionTree->Items->GetFirstNode();
 
-   while (
 
-     (Node != NULL) &&
 
-     ((!ByName && (Node->Data != SessionData)) ||
 
-      (ByName && (!IsSiteNode(Node) || (GetNodeSession(Node)->Name != SessionData->Name)))))
 
-   {
 
-     Node = Node->GetNext();
 
-   }
 
-   return Node;
 
- }
 
- //---------------------------------------------------------------------------
 
- TSessionData * __fastcall TLoginDialog::GetEditingSessionData()
 
- {
 
-   return FEditing ? DebugNotNull(SelectedSession) : NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveAsSession(bool ForceDialog)
 
- {
 
-   // Parse hostname before saving
 
-   // (HostNameEditExit is not triggered when child dialog pops up when it is invoked by accelerator)
 
-   // We should better handle this automaticaly when focus is moved to another dialog.
 
-   ParseHostName();
 
-   std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
 
-   SaveSession(SessionData.get());
 
-   TSessionData * EditingSessionData = GetEditingSessionData();
 
-   // collect list of empty folders (these are not persistent and known to login dialog only)
 
-   std::unique_ptr<TStrings> NewFolders(new TStringList());
 
-   TTreeNode * Node = SessionTree->Items->GetFirstNode();
 
-   while (Node != NULL)
 
-   {
 
-     if (IsFolderNode(Node) && !Node->HasChildren)
 
-     {
 
-       NewFolders->Add(SessionNodePath(Node));
 
-     }
 
-     Node = Node->GetNext();
 
-   }
 
-   TSessionData * NewSession =
 
-     DoSaveSession(SessionData.get(), EditingSessionData, ForceDialog, NewFolders.get());
 
-   if (NewSession != NULL)
 
-   {
 
-     TTreeNode * ParentNode = AddSessionPath(UnixExtractFilePath(NewSession->SessionName), false, false);
 
-     CheckIsSessionFolder(ParentNode);
 
-     TTreeNode * Node = FindSessionNode(NewSession, false);
 
-     if (Node == NULL)
 
-     {
 
-       Node = AddSession(NewSession);
 
-     }
 
-     if ((SessionTree->Selected != Node) &&
 
-         IsSiteNode(SessionTree->Selected))
 
-     {
 
-       CancelEditing();
 
-     }
 
-     else
 
-     {
 
-       FEditing = false;
 
-     }
 
-     SessionTree->Selected = Node;
 
-     SessionTree->SetFocus();
 
-     // this
 
-     // - updates TransferProtocolView and EncryptionView
 
-     // - clears the password box, if user has not opted to save password
 
-     // - reloads fake password
 
-     LoadContents();
 
-     UpdateControls();
 
-     ResetNewSiteData();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveSessionActionExecute(TObject * /*Sender*/)
 
- {
 
-   bool NewSiteSelected = IsNewSiteNode(SessionTree->Selected);
 
-   // for new site, the "save" command is actually "save as"
 
-   SaveAsSession(NewSiteSelected);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveAsSessionActionExecute(TObject * /*Sender*/)
 
- {
 
-   SaveAsSession(true);
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TLoginDialog::SessionNodePath(TTreeNode * Node)
 
- {
 
-   UnicodeString Path;
 
-   if ((Node != NULL) && !IsNewSiteNode(Node))
 
-   {
 
-     Path = Node->Text;
 
-     Node = Node->Parent;
 
-     while (Node != NULL)
 
-     {
 
-       Path.Insert(UnixIncludeTrailingBackslash(Node->Text), 1);
 
-       Node = Node->Parent;
 
-     }
 
-   }
 
-   return Path;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::DeleteSessionActionExecute(TObject * /*Sender*/)
 
- {
 
-   DebugAssert(SessionTree->Selected != NULL);
 
-   TMessageParams Params;
 
-   Params.ImageName = L"Delete file";
 
-   TTreeNode * Node = SessionTree->Selected;
 
-   if (IsSiteNode(Node))
 
-   {
 
-     TSessionData * Session = SelectedSession;
 
-     UnicodeString Message = MainInstructions(FMTLOAD(CONFIRM_DELETE_SESSION, (Session->SessionName)));
 
-     if (MessageDialog(Message,
 
-           qtConfirmation, qaOK | qaCancel, HELP_DELETE_SESSION, &Params) == qaOK)
 
-     {
 
-       WinConfiguration->DeleteSessionFromJumpList(Session->SessionName);
 
-       Session->Remove();
 
-       DestroySession(Session);
 
-       SessionTree->Selected->Delete();
 
-     }
 
-   }
 
-   else if (IsFolderNode(Node) || IsWorkspaceNode(Node))
 
-   {
 
-     int Sessions = 0;
 
-     TTreeNode * ANode = Node->GetNext();
 
-     while ((ANode != NULL) && ANode->HasAsParent(Node))
 
-     {
 
-       if (IsSessionNode(ANode))
 
-       {
 
-         TSessionData * Session = GetNodeSession(ANode);
 
-         if (Session->Special)
 
-         {
 
-           SessionTree->Selected = ANode;
 
-           ANode->MakeVisible();
 
-           throw Exception(FMTLOAD(LOGIN_DELETE_SPECIAL_SESSION, (Session->SessionName)));
 
-         }
 
-         Sessions++;
 
-       }
 
-       ANode = ANode->GetNext();
 
-     }
 
-     UnicodeString Path = SessionNodePath(Node);
 
-     int Prompt;
 
-     UnicodeString HelpKeyword;
 
-     if (IsFolderNode(Node))
 
-     {
 
-       Prompt = LOGIN_DELETE_SESSION_FOLDER;
 
-       HelpKeyword = HELP_DELETE_SESSION_FOLDER;
 
-     }
 
-     else
 
-     {
 
-       Prompt = LOGIN_DELETE_WORKSPACE;
 
-       HelpKeyword = HELP_DELETE_WORKSPACE;
 
-     }
 
-     if ((Sessions == 0) ||
 
-         (MessageDialog(MainInstructions(FMTLOAD(Prompt, (Path, Sessions))),
 
-           qtConfirmation, qaOK | qaCancel, HelpKeyword, &Params) == qaOK))
 
-     {
 
-       if (IsWorkspaceNode(Node))
 
-       {
 
-         WinConfiguration->DeleteWorkspaceFromJumpList(Path);
 
-       }
 
-       Node = SessionTree->Selected;
 
-       TTreeNode * ANode = Node->GetNext();
 
-       while ((ANode != NULL) && ANode->HasAsParent(Node))
 
-       {
 
-         if (IsSessionNode(ANode))
 
-         {
 
-           TSessionData * Session = GetNodeSession(ANode);
 
-           Session->Remove();
 
-           DestroySession(Session);
 
-         }
 
-         ANode = ANode->GetNext();
 
-       }
 
-       SessionTree->Selected->Delete();
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ReloadSessions(const UnicodeString & SelectSite)
 
- {
 
-   SaveState();
 
-   if (!SelectSite.IsEmpty())
 
-   {
 
-     WinConfiguration->LastStoredSession = SelectSite;
 
-   }
 
-   LoadSessions();
 
-   LoadState();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ImportSessionsActionExecute(TObject * /*Sender*/)
 
- {
 
-   std::unique_ptr<TList> Imported(new TList());
 
-   if (DoImportSessionsDialog(Imported.get()) &&
 
-       // Can be empty when imported known_hosts
 
-       (Imported->Count > 0))
 
-   {
 
-     // Focus the first imported session.
 
-     // We should also consider expanding all newly created folders
 
-     UnicodeString SelectSite = static_cast<TSessionData *>(Imported->Items[0])->Name;
 
-     ReloadSessions(SelectSite);
 
-     // Focus the tree with focused imported session(s).
 
-     SessionTree->SetFocus();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CleanUpActionExecute(TObject * /*Sender*/)
 
- {
 
-   if (DoCleanupDialog())
 
-   {
 
-     SaveState();
 
-     LoadSessions();
 
-     LoadState();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::AboutActionExecute(TObject * /*Sender*/)
 
- {
 
-   DoAboutDialog(Configuration);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ActionListUpdate(TBasicAction * BasicAction,
 
-       bool &Handled)
 
- {
 
-   bool NewSiteSelected = IsNewSiteNode(SessionTree->Selected);
 
-   bool SiteSelected = IsSiteNode(SessionTree->Selected);
 
-   bool FolderOrWorkspaceSelected = IsFolderOrWorkspaceNode(SessionTree->Selected);
 
-   TAction * Action = DebugNotNull(dynamic_cast<TAction *>(BasicAction));
 
-   bool PrevEnabled = Action->Enabled;
 
-   bool Editable = IsEditable();
 
-   if ((Action == EditSessionAction) ||
 
-       (Action == CloneToNewSiteAction))
 
-   {
 
-     Action->Enabled = SiteSelected && !FEditing;
 
-   }
 
-   else if (Action == EditCancelAction)
 
-   {
 
-     EditCancelAction->Enabled = FEditing;
 
-   }
 
-   else if (Action == DeleteSessionAction)
 
-   {
 
-     DeleteSessionAction->Enabled =
 
-       ((SiteSelected && !SelectedSession->Special && !FEditing) ||
 
-        FolderOrWorkspaceSelected);
 
-   }
 
-   else if (Action == RenameSessionAction)
 
-   {
 
-     RenameSessionAction->Enabled =
 
-       ((SiteSelected && !SelectedSession->Special && !FEditing) ||
 
-        FolderOrWorkspaceSelected);
 
-   }
 
-   else if (Action == DesktopIconAction)
 
-   {
 
-     DesktopIconAction->Enabled =
 
-       (SiteSelected && !FEditing) ||
 
-       (FolderOrWorkspaceSelected && HasNodeAnySession(SessionTree->Selected));
 
-   }
 
-   else if (Action == SendToHookAction)
 
-   {
 
-     SendToHookAction->Enabled = SiteSelected && !FEditing;
 
-   }
 
-   else if (Action == LoginAction)
 
-   {
 
-     LoginAction->Enabled = CanLogin();
 
-   }
 
-   else if (Action == PuttyAction)
 
-   {
 
-     Action->Enabled = (NewSiteSelected || SiteSelected) && CanLogin();
 
-   }
 
-   else if (Action == SaveSessionAction)
 
-   {
 
-     SaveSessionAction->Enabled = Editable;
 
-   }
 
-   else if (Action == SessionAdvancedAction)
 
-   {
 
-     SessionAdvancedAction->Enabled = Editable;
 
-   }
 
-   else if (Action == SessionRawAction)
 
-   {
 
-     SessionRawAction->Enabled = Editable;
 
-   }
 
-   else if (Action == SaveAsSessionAction)
 
-   {
 
-     // Save as is needed for new site only when !SupportsSplitButton()
 
-     SaveAsSessionAction->Enabled = Editable;
 
-   }
 
-   else if (Action == NewSessionFolderAction)
 
-   {
 
-     NewSessionFolderAction->Enabled = !FEditing;
 
-   }
 
-   else if (Action == PasteUrlAction)
 
-   {
 
-     UnicodeString ClipboardUrl;
 
-     Action->Enabled =
 
-       NonEmptyTextFromClipboard(ClipboardUrl) &&
 
-       StoredSessions->IsUrl(ClipboardUrl);
 
-   }
 
-   else if (Action == GenerateUrlAction2)
 
-   {
 
-     TSessionData * Data = GetSessionData();
 
-     // URL without hostname is pointless
 
-     Action->Enabled = (Data != NULL) && !Data->HostName.IsEmpty() && !FEditing;
 
-   }
 
-   else if (Action == CopyParamRuleAction)
 
-   {
 
-     TSessionData * Data = GetSessionData();
 
-     // without hostname it's pointless
 
-     Action->Enabled = (Data != NULL) && !Data->HostName.IsEmpty();
 
-   }
 
-   else if (Action == SearchSiteNameStartOnlyAction)
 
-   {
 
-     Action->Checked = (FSiteSearch == isNameStartOnly);
 
-   }
 
-   else if (Action == SearchSiteNameAction)
 
-   {
 
-     Action->Checked = (FSiteSearch == isName);
 
-   }
 
-   else if (Action == SearchSiteAction)
 
-   {
 
-     Action->Checked = (FSiteSearch == isAll);
 
-   }
 
-   else if (Action == CheckForUpdatesAction)
 
-   {
 
-     Action->Visible = !IsUWP();
 
-   }
 
-   Handled = true;
 
-   // to update buttons visibility
 
-   if (PrevEnabled != Action->Enabled)
 
-   {
 
-     UpdateControls();
 
-   }
 
-   Idle();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::IsCloneToNewSiteDefault()
 
- {
 
-   return !FEditing && !FRenaming && IsSiteNode(SessionTree->Selected) && !StoredSessions->CanLogin(GetSessionData());
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::CanLogin()
 
- {
 
-   TSessionData * Data = GetSessionData();
 
-   return
 
-     ((Data != NULL) && StoredSessions->CanLogin(Data) && !FEditing) ||
 
-     (IsFolderOrWorkspaceNode(SessionTree->Selected) && HasNodeAnySession(SessionTree->Selected, true));
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::Idle()
 
- {
 
-   if (SessionTree->IsEditing() != FRenaming)
 
-   {
 
-     FRenaming = SessionTree->IsEditing();
 
-     UpdateControls();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::Execute(TList * DataList)
 
- {
 
-   FDataList = DataList;
 
-   if (DataList->Count > 0)
 
-   {
 
-     TSessionData * SessionData = reinterpret_cast<TSessionData * >(DataList->Items[0]);
 
-     TTreeNode * Node = FindSessionNode(SessionData, true);
 
-     if (Node != NULL)
 
-     {
 
-       SessionTree->Selected = Node;
 
-       ActiveControl = SessionTree;
 
-     }
 
-     else
 
-     {
 
-       FNewSiteData->CopyData(SessionData);
 
-       FNewSiteData->Special = false;
 
-       // This is actualy bit pointless.
 
-       // As of now, we hardly ever get any useful data in ad-hoc DataList.
 
-       // (this was implemented for support taking session url from clipboard instead
 
-       // of command-line, but without autoconnect, but this functionality was cancelled)
 
-       if (!FNewSiteData->IsSameDecrypted(StoredSessions->DefaultSettings))
 
-       {
 
-         // we want to start with new site page
 
-         FForceNewSite = true;
 
-       }
 
-       LoadContents();
 
-     }
 
-   }
 
-   else
 
-   {
 
-     Default();
 
-   }
 
-   // Optimization. List view is recreated while showing the form,
 
-   // causing nodes repopulation and in a consequence a huge number of
 
-   // nodes comparison
 
-   SessionTree->SortType = Comctrls::stNone;
 
-   FSortEnablePending = true;
 
-   // Not calling LoadState here.
 
-   // It's redundant and does not work anyway, see comment in the method.
 
-   int AResult = ShowModal();
 
-   // When CanLogin is false, the DefaultResult() will fail finding a default button.
 
-   bool Result = CanLogin() && (AResult == DefaultResult());
 
-   SaveState();
 
-   if (Result)
 
-   {
 
-     SaveConfiguration();
 
-     // DataList saved already from FormCloseQuery
 
-   }
 
-   if (!ShowAgainCheck->Checked)
 
-   {
 
-     WinConfiguration->ShowLoginWhenNoSession = false;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveDataList(TList * DataList)
 
- {
 
-   // Normally we would call this from Execute,
 
-   // but at that point the windows is already hidden.
 
-   // Cloning session data may pop up master password dialog:
 
-   // - if it happens between closing and destroyiong login dialog
 
-   //   the next window will appear in background for some reason
 
-   // - and its actually even nicer when master password dialog pops up over
 
-   //   the login dialog
 
-   DataList->Clear();
 
-   TTreeNode * Node = SessionTree->Selected;
 
-   if (IsFolderOrWorkspaceNode(Node))
 
-   {
 
-     UnicodeString Name = SessionNodePath(Node);
 
-     if (IsWorkspaceNode(Node))
 
-     {
 
-       WinConfiguration->AddWorkspaceToJumpList(Name);
 
-     }
 
-     StoredSessions->GetFolderOrWorkspace(Name, DataList);
 
-   }
 
-   else
 
-   {
 
-     DataList->Add(CloneSelectedSession());
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TSessionData * __fastcall TLoginDialog::CloneSelectedSession()
 
- {
 
-   TTreeNode * Node = SessionTree->Selected;
 
-   std::unique_ptr<TSessionData> Data2(new TSessionData(L""));
 
-   if (IsSiteNode(Node))
 
-   {
 
-     Data2->Assign(GetNodeSession(Node));
 
-   }
 
-   else if (DebugAlwaysTrue(IsNewSiteNode(Node)))
 
-   {
 
-     TSessionData * Data = GetSessionData();
 
-     Data2->Assign(Data);
 
-     // we carry the name of the edited stored session around while on the dialog,
 
-     // but we do not want it to leave the dialog, so that we can distinguish
 
-     // stored and ad-hoc sessions
 
-     Data2->Name = L"";
 
-   }
 
-   return Data2.release();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveState()
 
- {
 
-   DebugAssert(WinConfiguration != NULL);
 
-   WinConfiguration->BeginUpdate();
 
-   try
 
-   {
 
-     std::unique_ptr<TStringList> OpenedStoredSessionFolders(CreateSortedStringList());
 
-     for (int Index = 0; Index < SessionTree->Items->Count; Index++)
 
-     {
 
-       TTreeNode * Node = SessionTree->Items->Item[Index];
 
-       if (IsFolderNode(Node))
 
-       {
 
-         if (Node->Expanded)
 
-         {
 
-           OpenedStoredSessionFolders->Add(SessionNodePath(Node));
 
-         }
 
-       }
 
-     }
 
-     WinConfiguration->OpenedStoredSessionFolders = OpenedStoredSessionFolders->CommaText;
 
-     WinConfiguration->LastStoredSession = SessionNodePath(SessionTree->Selected);
 
-     TLoginDialogConfiguration DialogConfiguration = CustomWinConfiguration->LoginDialog;
 
-     DialogConfiguration.WindowSize = StoreFormSize(this);
 
-     DialogConfiguration.SiteSearch = FSiteSearch;
 
-     CustomWinConfiguration->LoginDialog = DialogConfiguration;
 
-   }
 
-   __finally
 
-   {
 
-     WinConfiguration->EndUpdate();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::LoadOpenedStoredSessionFolders(
 
-   TTreeNode * Node, TStrings * OpenedStoredSessionFolders)
 
- {
 
-   if (IsFolderNode(Node))
 
-   {
 
-     UnicodeString Path = SessionNodePath(Node);
 
-     if (OpenedStoredSessionFolders->IndexOf(Path) >= 0)
 
-     {
 
-       Node->Expand(false);
 
-       UpdateFolderNode(Node);
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::LoadState()
 
- {
 
-   // it does not make any sense to call this before
 
-   // DoFormWindowProc(CM_SHOWINGCHANGED), we would end up on wrong monitor
 
-   if (DebugAlwaysTrue(Visible))
 
-   {
 
-     RestoreFormSize(CustomWinConfiguration->LoginDialog.WindowSize, this);
 
-   }
 
-   FSiteSearch = CustomWinConfiguration->LoginDialog.SiteSearch;
 
-   TStringList * OpenedStoredSessionFolders = CreateSortedStringList();
 
-   try
 
-   {
 
-     OpenedStoredSessionFolders->CommaText = WinConfiguration->OpenedStoredSessionFolders;
 
-     for (int Index = 0; Index < SessionTree->Items->Count; Index++)
 
-     {
 
-       LoadOpenedStoredSessionFolders(
 
-         SessionTree->Items->Item[Index], OpenedStoredSessionFolders);
 
-     }
 
-     // tree view tries to make expanded node children all visible, what
 
-     // may scroll the selected node (what should be the first one here),
 
-     // out of the view
 
-     if (SessionTree->Selected != NULL)
 
-     {
 
-       // see comment for LastStoredSession branch below
 
-       DebugAssert(Visible);
 
-       SessionTree->Selected->MakeVisible();
 
-     }
 
-   }
 
-   __finally
 
-   {
 
-     delete OpenedStoredSessionFolders;
 
-   }
 
-   // calling TTreeNode::MakeVisible() when tree view is not visible yet,
 
-   // sometimes scrolls view horizontally when not needed
 
-   // (seems like it happens for sites that are at the same level
 
-   // as site folders, e.g. for the very last root-level site, as long as
 
-   // there are any folders)
 
-   if (!FForceNewSite &&
 
-       !WinConfiguration->LastStoredSession.IsEmpty() && DebugAlwaysTrue(Visible))
 
-   {
 
-     UnicodeString Path = WinConfiguration->LastStoredSession;
 
-     UnicodeString ParentPath = UnixExtractFilePath(Path);
 
-     TTreeNode * Node;
 
-     if (ParentPath.IsEmpty())
 
-     {
 
-       Node = SessionTree->Items->GetFirstNode();
 
-     }
 
-     else
 
-     {
 
-       TTreeNode * Parent = AddSessionPath(ParentPath, false, false);
 
-       Node = (Parent != NULL) ? Parent->getFirstChild() : NULL;
 
-     }
 
-     if (Node != NULL)
 
-     {
 
-       UnicodeString Name = UnixExtractFileName(Path);
 
-       // actually we cannot distinguish folder and session here
 
-       // (note that we allow folder and session with the same name),
 
-       // this is pending for future improvements
 
-       while ((Node != NULL) && !AnsiSameText(Node->Text, Name))
 
-       {
 
-         Node = Node->getNextSibling();
 
-       }
 
-       if (Node != NULL)
 
-       {
 
-         SessionTree->Selected = Node;
 
-         SessionTree->Selected->MakeVisible();
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveConfiguration()
 
- {
 
-   DebugAssert(CustomWinConfiguration);
 
-   TTreeNode * Node = SessionTree->Selected;
 
-   if (IsWorkspaceNode(Node))
 
-   {
 
-     WinConfiguration->LastWorkspace = SessionNodePath(Node);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::PreferencesActionExecute(TObject * /*Sender*/)
 
- {
 
-   ShowPreferencesDialog(::pmDefault);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::PreferencesLoggingActionExecute(TObject * /*Sender*/)
 
- {
 
-   ShowPreferencesDialog(pmLogging);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::MasterPasswordRecrypt(TObject * /*Sender*/)
 
- {
 
-   FNewSiteData->RecryptPasswords();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ShowPreferencesDialog(TPreferencesMode PreferencesMode)
 
- {
 
-   DebugAssert(CustomWinConfiguration->OnMasterPasswordRecrypt == NULL);
 
-   CustomWinConfiguration->OnMasterPasswordRecrypt = MasterPasswordRecrypt;
 
-   try
 
-   {
 
-     DoPreferencesDialog(PreferencesMode);
 
-   }
 
-   __finally
 
-   {
 
-     DebugAssert(CustomWinConfiguration->OnMasterPasswordRecrypt == MasterPasswordRecrypt);
 
-     CustomWinConfiguration->OnMasterPasswordRecrypt = NULL;
 
-   }
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ResetNewSessionActionExecute(TObject * /*Sender*/)
 
- {
 
-   Default();
 
-   EditSession();
 
-   FNewSiteKeepName = false;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CMDialogKey(TWMKeyDown & Message)
 
- {
 
-   if (Message.CharCode == VK_TAB)
 
-   {
 
-     if (!FSitesIncrementalSearch.IsEmpty())
 
-     {
 
-       TShiftState Shift = KeyDataToShiftState(Message.KeyData);
 
-       bool Reverse = Shift.Contains(ssShift);
 
-       if (!SitesIncrementalSearch(FSitesIncrementalSearch, true, Reverse))
 
-       {
 
-         MessageBeep(MB_ICONHAND);
 
-       }
 
-       Message.Result = 1;
 
-       return;
 
-     }
 
-   }
 
-   else if (Message.CharCode == VK_ESCAPE)
 
-   {
 
-     if (!FSitesIncrementalSearch.IsEmpty())
 
-     {
 
-       ResetSitesIncrementalSearch();
 
-       Message.Result = 1;
 
-       return;
 
-     }
 
-   }
 
-   TForm::Dispatch(&Message);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::WMMoving(TMessage & Message)
 
- {
 
-   TForm::Dispatch(&Message);
 
-   if (FLinkedForm != NULL)
 
-   {
 
-     if (FPrevPos.X == std::numeric_limits<LONG>::min())
 
-     {
 
-       FPrevPos = BoundsRect.TopLeft();
 
-     }
 
-     TRect Rect = *reinterpret_cast<RECT*>(Message.LParam);
 
-     FLinkedForm->SetBounds(
 
-       FLinkedForm->Left + (Rect.Left - FPrevPos.X),
 
-       FLinkedForm->Top + (Rect.Top - FPrevPos.Y),
 
-       FLinkedForm->Width, FLinkedForm->Height);
 
-     FPrevPos = Rect.TopLeft();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CMDpiChanged(TMessage & Message)
 
- {
 
-   TForm::Dispatch(&Message);
 
-   GenerateButtonImages();
 
-   CenterButtonImage(LoginButton);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::Dispatch(void * Message)
 
- {
 
-   TMessage * M = reinterpret_cast<TMessage*>(Message);
 
-   DebugAssert(M);
 
-   if (M->Msg == CM_DIALOGKEY)
 
-   {
 
-     CMDialogKey(*((TWMKeyDown *)Message));
 
-   }
 
-   else if (M->Msg == WM_MANAGES_CAPTION)
 
-   {
 
-     // caption managed in TLoginDialog::Init()
 
-     M->Result = 1;
 
-   }
 
-   else if (M->Msg == WM_WANTS_MOUSEWHEEL)
 
-   {
 
-     M->Result = 1;
 
-   }
 
-   else if (M->Msg == WM_CAN_DISPLAY_UPDATES)
 
-   {
 
-     M->Result = 1;
 
-   }
 
-   else if (M->Msg == CM_ACTIVATE)
 
-   {
 
-     // Called from TCustomForm.ShowModal
 
-     if (FSortEnablePending)
 
-     {
 
-       FSortEnablePending = false;
 
-       SessionTree->SortType = Comctrls::stBoth;
 
-     }
 
-     TForm::Dispatch(Message);
 
-   }
 
-   else if (M->Msg == WM_SYSCOMMAND)
 
-   {
 
-     if (!HandleMinimizeSysCommand(*M))
 
-     {
 
-       TForm::Dispatch(Message);
 
-     }
 
-   }
 
-   else if (M->Msg == WM_MOVING)
 
-   {
 
-     WMMoving(*M);
 
-   }
 
-   else if (M->Msg == CM_DPICHANGED)
 
-   {
 
-     CMDpiChanged(*M);
 
-   }
 
-   else
 
-   {
 
-     TForm::Dispatch(Message);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SetDefaultSessionActionExecute(
 
-       TObject * /*Sender*/)
 
- {
 
-   UnicodeString Message = MainInstructions(LoadStr(SET_DEFAULT_SESSION_SETTINGS));
 
-   if (MessageDialog(Message, qtConfirmation,
 
-         qaOK | qaCancel, HELP_SESSION_SAVE_DEFAULT) == qaOK)
 
-   {
 
-     std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
 
-     SaveSession(SessionData.get());
 
-     // See the comment to the other use of the method in DoSaveSession.
 
-     CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData.get());
 
-     StoredSessions->DefaultSettings = SessionData.get();
 
-     Default();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ToolsMenuButtonClick(TObject * /*Sender*/)
 
- {
 
-   MenuPopup(ToolsPopupMenu, ToolsMenuButton);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::DesktopIconActionExecute(TObject * /*Sender*/)
 
- {
 
-   TTreeNode * Node = SessionTree->Selected;
 
-   UnicodeString Message;
 
-   UnicodeString Name;
 
-   UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
 
-   int IconIndex = 0;
 
-   if (IsSiteNode(Node))
 
-   {
 
-     Name = GetNodeSession(Node)->Name;
 
-     Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT, (Name));
 
-     AddToList(AdditionalParams, TProgramParams::FormatSwitch(UPLOAD_IF_ANY_SWITCH), L" ");
 
-     IconIndex = SITE_ICON;
 
-   }
 
-   else if (IsFolderNode(Node))
 
-   {
 
-     Name = SessionNodePath(SessionTree->Selected);
 
-     Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT_FOLDER, (Name));
 
-     IconIndex = SITE_FOLDER_ICON;
 
-   }
 
-   else if (IsWorkspaceNode(Node))
 
-   {
 
-     Name = SessionNodePath(SessionTree->Selected);
 
-     Message = FMTLOAD(CONFIRM_CREATE_SHORTCUT_WORKSPACE, (Name));
 
-     IconIndex = WORKSPACE_ICON;
 
-   }
 
-   else
 
-   {
 
-     DebugFail();
 
-   }
 
-   Message = MainInstructions(Message);
 
-   if (MessageDialog(Message, qtConfirmation, qaYes | qaNo, HELP_CREATE_SHORTCUT) == qaYes)
 
-   {
 
-     TInstantOperationVisualizer Visualizer;
 
-     CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, IconIndex);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SendToHookActionExecute(TObject * /*Sender*/)
 
- {
 
-   DebugAssert(IsSiteNode(SessionTree->Selected));
 
-   DebugAssert(SelectedSession != NULL);
 
-   UnicodeString Message = MainInstructions(FMTLOAD(CONFIRM_CREATE_SENDTO, (SelectedSession->Name)));
 
-   if (MessageDialog(Message,
 
-         qtConfirmation, qaYes | qaNo, HELP_CREATE_SENDTO) == qaYes)
 
-   {
 
-     TInstantOperationVisualizer Visualizer;
 
-     UnicodeString AdditionalParams =
 
-       TProgramParams::FormatSwitch(SEND_TO_HOOK_SWITCH) + L" " +
 
-       TProgramParams::FormatSwitch(UPLOAD_SWITCH);
 
-     CreateDesktopSessionShortCut(SelectedSession->Name,
 
-       FMTLOAD(SESSION_SENDTO_HOOK_NAME2, (SelectedSession->LocalName, AppName)),
 
-       AdditionalParams,
 
-       CSIDL_SENDTO, SITE_ICON);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::HasNodeAnySession(TTreeNode * Node, bool NeedCanLogin)
 
- {
 
-   bool Result = false;
 
-   TTreeNode * ANode = Node->GetNext();
 
-   while (!Result && (ANode != NULL) && ANode->HasAsParent(Node))
 
-   {
 
-     Result =
 
-       IsSessionNode(ANode) &&
 
-       (!NeedCanLogin || StoredSessions->CanLogin(GetNodeSession(ANode)));
 
-     ANode = ANode->GetNext();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeCustomDrawItem(
 
-   TCustomTreeView * Sender, TTreeNode * Node, TCustomDrawState State,
 
-   bool & DefaultDraw)
 
- {
 
-   TFontStyles Styles = Sender->Canvas->Font->Style;
 
-   if (IsSessionNode(Node) && GetNodeSession(Node)->Special)
 
-   {
 
-     Styles = Styles << fsBold << fsUnderline;
 
-   }
 
-   else
 
-   {
 
-     Styles = Styles >> fsBold >> fsUnderline;
 
-   }
 
-   if (State.Empty() && !Node->DropTarget)
 
-   {
 
-     if (IsFolderOrWorkspaceNode(Node))
 
-     {
 
-       if (!HasNodeAnySession(Node))
 
-       {
 
-         Sender->Canvas->Font->Color = clGrayText;
 
-       }
 
-     }
 
-   }
 
-   Sender->Canvas->Font->Style = Styles;
 
-   DefaultDraw = true;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CheckForUpdatesActionExecute(TObject * /*Sender*/)
 
- {
 
-   CheckForUpdates(false);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::HelpButtonClick(TObject * /*Sender*/)
 
- {
 
-   FormHelp(this);
 
- }
 
- //---------------------------------------------------------------------------
 
- TModalResult __fastcall TLoginDialog::DefaultResult()
 
- {
 
-   return ::DefaultResult(this, LoginButton);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::FormCloseQuery(TObject * /*Sender*/,
 
-   bool & CanClose)
 
- {
 
-   // CanClose test is now probably redundant,
 
-   // once we have a fallback to LoginButton in DefaultResult
 
-   CanClose = EnsureNotEditing();
 
-   // When CanLogin is false, the DefaultResult() will fail finding a default button
 
-   if (CanClose && CanLogin() && (ModalResult == DefaultResult()))
 
-   {
 
-     SaveDataList(FDataList);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeEditing(TObject * /*Sender*/,
 
-   TTreeNode * Node, bool & AllowEdit)
 
- {
 
-   DebugAssert(!FRenaming);
 
-   AllowEdit =
 
-     IsFolderOrWorkspaceNode(Node) ||
 
-     (DebugAlwaysTrue(IsSiteNode(Node)) && !GetNodeSession(Node)->Special);
 
-   FRenaming = AllowEdit;
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::RenameSessionActionExecute(TObject * /*Sender*/)
 
- {
 
-   if (SessionTree->Selected != NULL)
 
-   {
 
-     // would be more appropriate in SessionTreeEditing, but it does not work there
 
-     ResetSitesIncrementalSearch();
 
-     SessionTree->SetFocus();
 
-     SessionTree->Selected->EditText();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CheckDuplicateFolder(TTreeNode * Parent,
 
-   UnicodeString Text, TTreeNode * Node)
 
- {
 
-   TTreeNode * ANode =
 
-     ((Parent == NULL) ? SessionTree->Items->GetFirstNode() :
 
-      Parent->getFirstChild());
 
-   // note that we allow folder with the same name as existing session
 
-   // on the same level (see also AddSession)
 
-   while ((ANode != NULL) &&
 
-     ((ANode == Node) || IsSessionNode(ANode) || !AnsiSameText(ANode->Text, Text)))
 
-   {
 
-     ANode = ANode->getNextSibling();
 
-   }
 
-   if (ANode != NULL)
 
-   {
 
-     throw Exception(MainInstructions(FMTLOAD(LOGIN_DUPLICATE_SESSION_FOLDER_WORKSPACE, (Text))));
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CheckIsSessionFolder(TTreeNode * Node)
 
- {
 
-   if ((Node != NULL) && (Node->Parent != NULL))
 
-   {
 
-     CheckIsSessionFolder(Node->Parent);
 
-   }
 
-   if (IsWorkspaceNode(Node))
 
-   {
 
-     throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (SessionNodePath(Node))));
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeEdited(TObject * /*Sender*/,
 
-   TTreeNode * Node, UnicodeString & S)
 
- {
 
-   if ((Node->Text != S) && !S.IsEmpty())
 
-   {
 
-     TSessionData * Session = SelectedSession;
 
-     TSessionData::ValidateName(S);
 
-     if (Session != NULL)
 
-     {
 
-       UnicodeString Path = UnixExtractFilePath(Session->Name) + S;
 
-       SessionNameValidate(Path, Session->Name);
 
-       // remove from storage
 
-       Session->Remove();
 
-       TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
 
-       // modified only, explicit
 
-       StoredSessions->Save(false, true);
 
-       // the session may be the same, if only letter case has changed
 
-       if (Session != NewSession)
 
-       {
 
-         // if we overwrite existing session, remove the original item
 
-         // (we must not delete the node we are editing)
 
-         TTreeNode * ANode =
 
-           ((Node->Parent == NULL) ? SessionTree->Items->GetFirstNode() :
 
-            Node->Parent->getFirstChild());
 
-         while ((ANode != NULL) && (ANode->Data != NewSession))
 
-         {
 
-           ANode = ANode->getNextSibling();
 
-         }
 
-         if (ANode != NULL)
 
-         {
 
-           ANode->Delete();
 
-         }
 
-         Node->Data = NewSession;
 
-         DestroySession(Session);
 
-       }
 
-     }
 
-     else
 
-     {
 
-       CheckDuplicateFolder(Node->Parent, S, Node);
 
-       UnicodeString ParentPath = UnixIncludeTrailingBackslash(SessionNodePath(Node->Parent));
 
-       UnicodeString OldRoot = ParentPath + Node->Text;
 
-       UnicodeString NewRoot = ParentPath + S;
 
-       bool AnySession = false;
 
-       TSortType PrevSortType = SessionTree->SortType;
 
-       // temporarily disable automatic sorting, so that nodes are kept in order
 
-       // while we traverse them. otherwise it may happen that we omit some.
 
-       SessionTree->SortType = Comctrls::stNone;
 
-       try
 
-       {
 
-         TTreeNode * ANode = Node->GetNext();
 
-         while ((ANode != NULL) && ANode->HasAsParent(Node))
 
-         {
 
-           if (IsSessionNode(ANode))
 
-           {
 
-             AnySession = true;
 
-             TSessionData * Session = GetNodeSession(ANode);
 
-             // remove from storage
 
-             Session->Remove();
 
-             UnicodeString Path = Session->Name;
 
-             DebugAssert(Path.SubString(1, OldRoot.Length()) == OldRoot);
 
-             Path.Delete(1, OldRoot.Length());
 
-             Path.Insert(NewRoot, 1);
 
-             TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
 
-             // the session may be the same, if only letter case has changed
 
-             if (NewSession != Session)
 
-             {
 
-               ANode->Data = NewSession;
 
-               DestroySession(Session);
 
-             }
 
-           }
 
-           ANode = ANode->GetNext();
 
-         }
 
-       }
 
-       __finally
 
-       {
 
-         SessionTree->SortType = PrevSortType;
 
-       }
 
-       if (AnySession)
 
-       {
 
-         // modified only, explicit
 
-         StoredSessions->Save(false, true);
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TLoginDialog::FSProtocolToIndex(TFSProtocol FSProtocol,
 
-   bool & AllowScpFallback)
 
- {
 
-   if (FSProtocol == fsSFTP)
 
-   {
 
-     AllowScpFallback = true;
 
-     bool Dummy;
 
-     return FSProtocolToIndex(fsSFTPonly, Dummy);
 
-   }
 
-   else
 
-   {
 
-     AllowScpFallback = false;
 
-     for (int Index = 0; Index < TransferProtocolCombo->Items->Count; Index++)
 
-     {
 
-       if (FSOrder[Index] == FSProtocol)
 
-       {
 
-         return Index;
 
-       }
 
-     }
 
-     // SFTP is always present
 
-     return FSProtocolToIndex(fsSFTP, AllowScpFallback);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TFSProtocol __fastcall TLoginDialog::IndexToFSProtocol(int Index, bool AllowScpFallback)
 
- {
 
-   bool InBounds = (Index >= 0) && (Index < static_cast<int>(LENOF(FSOrder)));
 
-   // can be temporary "unselected" while new language is being loaded
 
-   DebugAssert(InBounds || (Index == -1));
 
-   TFSProtocol Result = fsSFTP;
 
-   if (InBounds)
 
-   {
 
-     Result = FSOrder[Index];
 
-     if ((Result == fsSFTPonly) && AllowScpFallback)
 
-     {
 
-       Result = fsSFTP;
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TLoginDialog::FtpsToIndex(TFtps Ftps)
 
- {
 
-   switch (Ftps)
 
-   {
 
-     default:
 
-       DebugFail();
 
-     case ftpsNone:
 
-       return 0;
 
-     case ftpsImplicit:
 
-       return 1;
 
-     case ftpsExplicitTls:
 
-     case ftpsExplicitSsl:
 
-       return 2;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TFtps __fastcall TLoginDialog::GetFtps()
 
- {
 
-   int Index = ((GetFSProtocol(false) == fsWebDAV) ? WebDavsCombo->ItemIndex : FtpsCombo->ItemIndex);
 
-   TFtps Ftps;
 
-   switch (Index)
 
-   {
 
-     default:
 
-       DebugFail();
 
-     case 0:
 
-       Ftps = ftpsNone;
 
-       break;
 
-     case 1:
 
-       Ftps = ftpsImplicit;
 
-       break;
 
-     case 2:
 
-       Ftps = ftpsExplicitTls;
 
-       break;
 
-   }
 
-   return Ftps;
 
- }
 
- //---------------------------------------------------------------------------
 
- TFSProtocol __fastcall TLoginDialog::GetFSProtocol(bool RequireScpFallbackDistinction)
 
- {
 
-   bool AllowScpFallback = false;
 
-   if (RequireScpFallbackDistinction && DebugAlwaysTrue(FSessionData != NULL))
 
-   {
 
-     FSProtocolToIndex(FSessionData->FSProtocol, AllowScpFallback);
 
-   }
 
-   return IndexToFSProtocol(TransferProtocolCombo->ItemIndex, AllowScpFallback);
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TLoginDialog::DefaultPort()
 
- {
 
-   return ::DefaultPort(GetFSProtocol(false), GetFtps());
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::TransferProtocolComboChange(TObject * Sender)
 
- {
 
-   if (!NoUpdate)
 
-   {
 
-     if (GetFSProtocol(false) == fsS3)
 
-     {
 
-       FtpsCombo->ItemIndex = FtpsToIndex(ftpsImplicit);
 
-       HostNameEdit->Text = S3HostName;
 
-     }
 
-   }
 
-   int ADefaultPort = DefaultPort();
 
-   if (!NoUpdate && FUpdatePortWithProtocol)
 
-   {
 
-     NoUpdate++;
 
-     try
 
-     {
 
-       if (PortNumberEdit->AsInteger == FDefaultPort)
 
-       {
 
-         PortNumberEdit->AsInteger = ADefaultPort;
 
-       }
 
-     }
 
-     __finally
 
-     {
 
-       NoUpdate--;
 
-     }
 
-   }
 
-   FDefaultPort = ADefaultPort;
 
-   DataChange(Sender);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::NavigationTreeCollapsing(
 
-   TObject * /*Sender*/, TTreeNode * /*Node*/, bool & AllowCollapse)
 
- {
 
-   AllowCollapse = false;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeExpandedCollapsed(TObject * /*Sender*/,
 
-   TTreeNode * Node)
 
- {
 
-   UpdateFolderNode(Node);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeCompare(TObject * /*Sender*/,
 
-   TTreeNode * Node1, TTreeNode * Node2, int /*Data*/, int & Compare)
 
- {
 
-   bool Node1IsNewSite = IsNewSiteNode(Node1);
 
-   bool Node2IsNewSite = IsNewSiteNode(Node2);
 
-   bool Node1IsWorkspace = IsWorkspaceNode(Node1);
 
-   bool Node2IsWorkspace = IsWorkspaceNode(Node2);
 
-   bool Node1IsFolder = IsFolderNode(Node1);
 
-   bool Node2IsFolder = IsFolderNode(Node2);
 
-   if (Node1IsNewSite && !Node2IsNewSite)
 
-   {
 
-     Compare = -1;
 
-   }
 
-   else if (!Node1IsNewSite && Node2IsNewSite)
 
-   {
 
-     Compare = 1;
 
-   }
 
-   else if (Node1IsWorkspace && !Node2IsWorkspace)
 
-   {
 
-     Compare = -1;
 
-   }
 
-   else if (!Node1IsWorkspace && Node2IsWorkspace)
 
-   {
 
-     Compare = 1;
 
-   }
 
-   else if (Node1IsFolder && !Node2IsFolder)
 
-   {
 
-     Compare = -1;
 
-   }
 
-   else if (!Node1IsFolder && Node2IsFolder)
 
-   {
 
-     Compare = 1;
 
-   }
 
-   else if (Node1IsWorkspace || Node1IsFolder)
 
-   {
 
-     Compare = CompareLogicalText(Node1->Text, Node2->Text, true);
 
-   }
 
-   else
 
-   {
 
-     Compare = NamedObjectSortProc(Node1->Data, Node2->Data);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::NewSessionFolderInputDialogInitialize(
 
-   TObject * /*Sender*/, TInputDialogData * Data)
 
- {
 
-   TCustomEdit * Edit = Data->Edit;
 
-   int P = Edit->Text.LastDelimiter(L"/");
 
-   if (P > 0)
 
-   {
 
-     Edit->SetFocus();
 
-     Edit->SelStart = P;
 
-     Edit->SelLength = Edit->Text.Length() - P;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::SessionFolderNode(TTreeNode * Node)
 
- {
 
-   TTreeNode * Parent;
 
-   if (IsSessionNode(Node))
 
-   {
 
-     Parent = Node->Parent;
 
-   }
 
-   else if (IsFolderNode(Node))
 
-   {
 
-     Parent = Node;
 
-   }
 
-   else if (IsWorkspaceNode(Node))
 
-   {
 
-     Parent = NULL;
 
-   }
 
-   else
 
-   {
 
-     DebugAssert(Node == NULL);
 
-     Parent = NULL;
 
-   }
 
-   return Parent;
 
- }
 
- //---------------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::CurrentSessionFolderNode()
 
- {
 
-   return SessionFolderNode(SessionTree->Selected);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::NewSessionFolderActionExecute(
 
-   TObject * /*Sender*/)
 
- {
 
-   UnicodeString Name =
 
-     UnixIncludeTrailingBackslash(SessionNodePath(CurrentSessionFolderNode())) +
 
-     LoadStr(NEW_FOLDER);
 
-   if (InputDialog(LoadStr(LOGIN_NEW_SESSION_FOLDER_CAPTION),
 
-         LoadStr(LOGIN_NEW_SESSION_FOLDER_PROMPT), Name, HELP_NEW_SESSION_FOLDER,
 
-         NULL, true, NewSessionFolderInputDialogInitialize))
 
-   {
 
-     Name = UnixExcludeTrailingBackslash(Name);
 
-     if (!Name.IsEmpty())
 
-     {
 
-       TTreeNode * Parent = AddSessionPath(UnixExtractFilePath(Name), true, false);
 
-       // this does not prevent creation of subfolder under workspace,
 
-       // if user creates more levels at once (but it does not show up anyway)
 
-       CheckIsSessionFolder(Parent);
 
-       CheckDuplicateFolder(Parent, UnixExtractFileName(Name), NULL);
 
-       TTreeNode * Node = AddSessionPath(Name, true, false);
 
-       SessionTree->Selected = Node;
 
-       Node->MakeVisible();
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::NormalizeDropTarget(TTreeNode * DropTarget)
 
- {
 
-   return IsWorkspaceNode(DropTarget) ? DropTarget : SessionFolderNode(DropTarget);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::SessionAllowDrop(TTreeNode * DropTarget)
 
- {
 
-   DropTarget = NormalizeDropTarget(DropTarget);
 
-   return
 
-     (SessionTree->Selected != NULL) &&
 
-     (SessionTree->Selected->Parent != DropTarget) &&
 
-     ((DropTarget == NULL) || IsFolderNode(DropTarget));
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeProc(TMessage & AMessage)
 
- {
 
-   if (AMessage.Msg == CM_DRAG)
 
-   {
 
-     TCMDrag & Message = reinterpret_cast<TCMDrag &>(AMessage);
 
-     // reimplement dmDragMove to avoid TCustomTreeView.DoDragOver,
 
-     // which resets DropTarget to pointed-to node
 
-     // (note that this disables OnDragOver event handler)
 
-     if ((Message.DragMessage == dmDragMove) ||
 
-         (Message.DragMessage == dmDragEnter) ||
 
-         (Message.DragMessage == dmDragLeave))
 
-     {
 
-       if (Message.DragMessage != dmDragMove)
 
-       {
 
-         // must call it at least for dmDragLeave, because it does some cleanup,
 
-         // but we need to override result below, as it defaults to "not accepted"
 
-         FOldSessionTreeProc(AMessage);
 
-       }
 
-       TDragControlObject * DragObject = dynamic_cast<TDragControlObject *>(Message.DragRec->Source);
 
-       if ((DragObject != NULL) && (DragObject->Control == SessionTree))
 
-       {
 
-         TPoint P = SessionTree->ScreenToClient(Message.DragRec->Pos);
 
-         TTreeNode * Node = SessionTree->GetNodeAt(P.x, P.y);
 
-         if (!SessionAllowDrop(Node))
 
-         {
 
-           DropTarget = NULL;
 
-           Message.Result = 0;
 
-         }
 
-         else
 
-         {
 
-           Message.Result = 1;
 
-         }
 
-         if (Message.DragMessage == dmDragMove)
 
-         {
 
-           SessionTree->DropTarget = NormalizeDropTarget(Node);
 
-         }
 
-         FScrollOnDragOver->DragOver(P);
 
-       }
 
-       else
 
-       {
 
-         Message.Result = 0;
 
-       }
 
-     }
 
-     else
 
-     {
 
-       FOldSessionTreeProc(AMessage);
 
-     }
 
-   }
 
-   else
 
-   {
 
-     FOldSessionTreeProc(AMessage);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeStartDrag(TObject * /*Sender*/,
 
-   TDragObject *& /*DragObject*/)
 
- {
 
-   DebugAssert(SessionTree->Selected != NULL);
 
-   // neither session folders/workspaces, nor special sessions can be dragged
 
-   if ((SessionTree->Selected == NULL) ||
 
-       IsFolderOrWorkspaceNode(SessionTree->Selected) ||
 
-       IsNewSiteNode(SessionTree->Selected) ||
 
-       (IsSiteNode(SessionTree->Selected) && SelectedSession->Special))
 
-   {
 
-     Abort();
 
-   }
 
-   FScrollOnDragOver->StartDrag();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeDragDrop(TObject * Sender,
 
-   TObject * Source, int /*X*/, int /*Y*/)
 
- {
 
-   TTreeNode * DropTarget = SessionTree->DropTarget;
 
-   if (DebugAlwaysTrue((Sender == Source) && SessionAllowDrop(DropTarget)) &&
 
-       // calling EnsureNotEditing only on drop, not on drag start,
 
-       // to avoid getting popup during unintended micro-dragging
 
-       EnsureNotEditing())
 
-   {
 
-     TSessionData * Session = SelectedSession;
 
-     UnicodeString Path =
 
-       UnixIncludeTrailingBackslash(SessionNodePath(DropTarget)) +
 
-       UnixExtractFileName(Session->SessionName);
 
-     SessionNameValidate(Path, Session->SessionName);
 
-     // remove from storage
 
-     Session->Remove();
 
-     TSessionData * NewSession = StoredSessions->NewSession(Path, Session);
 
-     // modified only, explicit
 
-     StoredSessions->Save(false, true);
 
-     // this should aways be the case
 
-     if (DebugAlwaysTrue(Session != NewSession))
 
-     {
 
-       TTreeNode * Node = SessionTree->Selected;
 
-       // look for overwritten node (if any)
 
-       TTreeNode * ANode = SessionTree->Items->GetFirstNode();
 
-       while (ANode != NULL)
 
-       {
 
-         if (ANode->Data == NewSession)
 
-         {
 
-           ANode->Delete();
 
-           break;
 
-         }
 
-         ANode = ANode->GetNext();
 
-       }
 
-       Node->MoveTo(DropTarget, naAddChild);
 
-       Node->Data = NewSession;
 
-       // try to make both visible
 
-       if (DropTarget != NULL)
 
-       {
 
-         DropTarget->MakeVisible();
 
-       }
 
-       Node->MakeVisible();
 
-       DestroySession(Session);
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TLoginDialog::GetFolderOrWorkspaceContents(
 
-   TTreeNode * Node, const UnicodeString & Indent, const UnicodeString & CommonRoot)
 
- {
 
-   UnicodeString Contents;
 
-   UnicodeString Path = SessionNodePath(Node);
 
-   std::unique_ptr<TStrings> Names(StoredSessions->GetFolderOrWorkspaceList(Path));
 
-   for (int Index = 0; Index < Names->Count; Index++)
 
-   {
 
-     UnicodeString Name = Names->Strings[Index];
 
-     if (StartsStr(CommonRoot, Name))
 
-     {
 
-       Name.Delete(1, CommonRoot.Length());
 
-     }
 
-     Contents += Indent + Name + L"\n";
 
-   }
 
-   return Contents;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeMouseMove(TObject * /*Sender*/,
 
-   TShiftState /*Shift*/, int X, int Y)
 
- {
 
-   TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
 
-   THitTests HitTest = SessionTree->GetHitTestInfoAt(X, Y);
 
-   if (Node != FHintNode)
 
-   {
 
-     Application->CancelHint();
 
-     UnicodeString Hint;
 
-     if (HitTest.Contains(htOnItem) || HitTest.Contains(htOnIcon) ||
 
-         HitTest.Contains(htOnLabel) || HitTest.Contains(htOnStateIcon))
 
-     {
 
-       FHintNode = Node;
 
-       if (IsSiteNode(Node))
 
-       {
 
-         Hint = GetNodeSession(Node)->InfoTip;
 
-       }
 
-       else if (IsWorkspaceNode(Node))
 
-       {
 
-         UnicodeString Path = SessionNodePath(Node);
 
-         Hint =
 
-           FMTLOAD(WORKSPACE_INFO_TIP, (Path)) + L"\n" +
 
-           // trim the trailing new line
 
-           TrimRight(GetFolderOrWorkspaceContents(Node, L"  ", UnicodeString()));
 
-       }
 
-       else
 
-       {
 
-         Hint = L"";
 
-       }
 
-     }
 
-     else
 
-     {
 
-       FHintNode = NULL;
 
-       Hint = L"";
 
-     }
 
-     SessionTree->Hint = Hint;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeEndDrag(TObject * /*Sender*/,
 
-   TObject * /*Target*/, int /*X*/, int /*Y*/)
 
- {
 
-   FScrollOnDragOver->EndDrag();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::AnonymousLoginCheckClick(TObject * /*Sender*/)
 
- {
 
-   if (AnonymousLoginCheck->Checked)
 
-   {
 
-     UserNameEdit->Text = AnonymousUserName;
 
-     PasswordEdit->Text = AnonymousPassword;
 
-   }
 
-   UpdateControls();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SaveButtonDropDownClick(TObject * /*Sender*/)
 
- {
 
-   MenuPopup(SaveDropDownMenu, SaveButton);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeExpanding(TObject * /*Sender*/,
 
-   TTreeNode * Node, bool & AllowExpansion)
 
- {
 
-   // to prevent workspace expansion
 
-   AllowExpansion = IsFolderNode(Node);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::RunPageantActionExecute(TObject * /*Sender*/)
 
- {
 
-   ExecuteTool(PageantTool);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::RunPuttygenActionExecute(TObject * /*Sender*/)
 
- {
 
-   ExecuteTool(PuttygenTool);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::PortNumberEditChange(TObject * Sender)
 
- {
 
-   if (!NoUpdate)
 
-   {
 
-     bool WellKnownPort = false;
 
-     TFSProtocol FSProtocol;
 
-     TFtps Ftps = ftpsNone;
 
-     int PortNumber = PortNumberEdit->AsInteger;
 
-     if (PortNumber == SshPortNumber)
 
-     {
 
-       FSProtocol = fsSFTP;
 
-       WellKnownPort = true;
 
-     }
 
-     else if (PortNumber == FtpPortNumber)
 
-     {
 
-       FSProtocol = fsFTP;
 
-       WellKnownPort = true;
 
-     }
 
-     else if (PortNumber == FtpsImplicitPortNumber)
 
-     {
 
-       FSProtocol = fsFTP;
 
-       Ftps = ftpsImplicit;
 
-       WellKnownPort = true;
 
-     }
 
-     else if (PortNumber == HTTPPortNumber)
 
-     {
 
-       FSProtocol = fsWebDAV;
 
-       WellKnownPort = true;
 
-     }
 
-     else if (PortNumber == HTTPSPortNumber)
 
-     {
 
-       FSProtocol = fsWebDAV;
 
-       Ftps = ftpsImplicit;
 
-       WellKnownPort = true;
 
-     }
 
-     if (WellKnownPort)
 
-     {
 
-       bool AllowScpFallback;
 
-       int ProtocolIndex = FSProtocolToIndex(FSProtocol, AllowScpFallback);
 
-       if ((TransferProtocolCombo->ItemIndex == ProtocolIndex) &&
 
-           (GetFtps() == Ftps))
 
-       {
 
-         FUpdatePortWithProtocol = true;
 
-       }
 
-       else
 
-       {
 
-         FUpdatePortWithProtocol = false;
 
-         NoUpdate++;
 
-         try
 
-         {
 
-           TransferProtocolCombo->ItemIndex = ProtocolIndex;
 
-           int FtpsIndex = FtpsToIndex(Ftps);
 
-           FtpsCombo->ItemIndex = FtpsIndex;
 
-           WebDavsCombo->ItemIndex = FtpsIndex;
 
-         }
 
-         __finally
 
-         {
 
-           NoUpdate--;
 
-         }
 
-       }
 
-     }
 
-   }
 
-   DataChange(Sender);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ExportActionExecute(TObject * /*Sender*/)
 
- {
 
-   UnicodeString FileName = Configuration->GetDefaultIniFileExportPath();
 
-   if (SaveDialog(LoadStr(EXPORT_CONF_TITLE), LoadStr(EXPORT_CONF_FILTER), L"ini", FileName))
 
-   {
 
-     Configuration->Export(FileName);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ImportActionExecute(TObject * /*Sender*/)
 
- {
 
-   if (MessageDialog(MainInstructions(LoadStr(IMPORT_CONFIGURATION)),
 
-         qtWarning, qaOK | qaCancel, HELP_IMPORT_CONFIGURATION) == qaOK)
 
-   {
 
-     std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
 
-     OpenDialog->Title = LoadStr(IMPORT_CONF_TITLE);
 
-     OpenDialog->Filter = LoadStr(EXPORT_CONF_FILTER);
 
-     OpenDialog->DefaultExt = L"ini";
 
-     OpenDialog->FileName = Configuration->GetDefaultIniFileExportPath();
 
-     if (OpenDialog->Execute())
 
-     {
 
-       // before the session list gets destroyed
 
-       SessionTree->Items->Clear();
 
-       Configuration->Import(OpenDialog->FileName);
 
-       ReloadSessions(L"");
 
-       if (SessionTree->Items->Count > 0)
 
-       {
 
-         SessionTree->Items->GetFirstNode()->MakeVisible();
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ResetSitesIncrementalSearch()
 
- {
 
-   FSitesIncrementalSearch = L"";
 
-   // this is to prevent active tree node being set back to Sites tab
 
-   // (from UpdateNavigationTree) when we are called from SessionTreeExit,
 
-   // while tab is changing
 
-   if (NoUpdate == 0)
 
-   {
 
-     UpdateControls();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::SitesIncrementalSearch(const UnicodeString & Text,
 
-   bool SkipCurrent, bool Reverse)
 
- {
 
-   TTreeNode * Node = SearchSite(Text, false, SkipCurrent, Reverse);
 
-   if (Node == NULL)
 
-   {
 
-     Node = SearchSite(Text, true, SkipCurrent, Reverse);
 
-     if (Node != NULL)
 
-     {
 
-       TTreeNode * Parent = Node->Parent;
 
-       while (Parent != NULL)
 
-       {
 
-         Parent->Expand(false);
 
-         Parent = Parent->Parent;
 
-       }
 
-     }
 
-   }
 
-   bool Result = (Node != NULL);
 
-   if (Result)
 
-   {
 
-     {
 
-       TAutoNestingCounter Guard(FIncrementalSearching);
 
-       SessionTree->Selected = Node;
 
-     }
 
-     FSitesIncrementalSearch = Text;
 
-     // Tab always searches even in collapsed nodes
 
-     TTreeNode * NextNode = SearchSite(Text, true, true, Reverse);
 
-     FSitesIncrementalSearchHaveNext =
 
-       (NextNode != NULL) && (NextNode != Node);
 
-     UpdateControls();
 
-     // make visible only after search panel is shown, what may obscure the node
 
-     Node->MakeVisible();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::GetNextNode(TTreeNode * Node, bool Reverse)
 
- {
 
-   if (Reverse)
 
-   {
 
-     Node = Node->GetPrev();
 
-     if (Node == NULL)
 
-     {
 
-       // GetLastNode
 
-       // https://stackoverflow.com/q/6257348/850848
 
-       Node = SessionTree->Items->GetFirstNode();
 
-       TTreeNode * Node2 = Node;
 
-       if (Node2 != NULL)
 
-       {
 
-         do
 
-         {
 
-           Node2 = Node;
 
-           if (Node != NULL)
 
-           {
 
-             Node = Node2->getNextSibling();
 
-           }
 
-           if (Node == NULL)
 
-           {
 
-             Node = Node2->getFirstChild();
 
-           }
 
-         }
 
-         while (Node != NULL);
 
-       }
 
-       Node = Node2;
 
-     }
 
-   }
 
-   else
 
-   {
 
-     Node = Node->GetNext();
 
-     if (Node == NULL)
 
-     {
 
-       Node = SessionTree->Items->GetFirstNode();
 
-     }
 
-   }
 
-   return Node;
 
- }
 
- //---------------------------------------------------------------------------
 
- TTreeNode * __fastcall TLoginDialog::SearchSite(const UnicodeString & Text,
 
-   bool AllowExpanding, bool SkipCurrent, bool Reverse)
 
- {
 
-   TTreeNode * CurrentNode =
 
-     (SessionTree->Selected != NULL) ? SessionTree->Selected : SessionTree->Items->GetFirstNode();
 
-   if (CurrentNode == NULL)
 
-   {
 
-     return NULL;
 
-   }
 
-   else
 
-   {
 
-     TTreeNode * Node = CurrentNode;
 
-     if (SkipCurrent)
 
-     {
 
-       Node = GetNextNode(Node, Reverse);
 
-       if (Node == NULL)
 
-       {
 
-         return NULL;
 
-       }
 
-     }
 
-     while (true)
 
-     {
 
-       bool Eligible = true;
 
-       TTreeNode * Parent = Node->Parent;
 
-       while (Eligible && (Parent != NULL))
 
-       {
 
-         Eligible =
 
-           IsFolderNode(Parent) &&
 
-           (Parent->Expanded || AllowExpanding);
 
-         Parent = Parent->Parent;
 
-       }
 
-       if (Eligible)
 
-       {
 
-         bool Matches = false;
 
-         switch (FSiteSearch)
 
-         {
 
-           case isNameStartOnly:
 
-             Matches = ContainsTextSemiCaseSensitive(Node->Text.SubString(1, Text.Length()), Text);
 
-             break;
 
-           case isName:
 
-             Matches = ContainsTextSemiCaseSensitive(Node->Text, Text);
 
-             break;
 
-           case isAll:
 
-             Matches = ContainsTextSemiCaseSensitive(Node->Text, Text);
 
-             if (!Matches && IsSiteNode(Node))
 
-             {
 
-               TSessionData * Data = GetNodeSession(Node);
 
-               Matches =
 
-                 ContainsTextSemiCaseSensitive(Data->HostName, Text) ||
 
-                 ContainsTextSemiCaseSensitive(Data->UserName, Text) ||
 
-                 ContainsTextSemiCaseSensitive(Data->Note, Text);
 
-             }
 
-             break;
 
-         }
 
-         if (Matches)
 
-         {
 
-           return Node;
 
-         }
 
-       }
 
-       Node = GetNextNode(Node, Reverse);
 
-       if (Node == CurrentNode)
 
-       {
 
-         return NULL;
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeExit(TObject * /*Sender*/)
 
- {
 
-   ResetSitesIncrementalSearch();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLoginDialog::EnsureNotEditing()
 
- {
 
-   bool Result = !FEditing;
 
-   if (!Result)
 
-   {
 
-     UnicodeString Message = MainInstructions(LoadStr(LOGIN_SAVE_EDITING));
 
-     unsigned int Answer = MessageDialog(Message, qtConfirmation, qaYes |qaNo | qaCancel);
 
-     switch (Answer)
 
-     {
 
-       case qaYes:
 
-         SaveAsSession(false);
 
-         Result = true;
 
-         break;
 
-       case qaNo:
 
-         CancelEditing();
 
-         // Make sure OK button gets enabled
 
-         UpdateControls();
 
-         Result = true;
 
-         break;
 
-       default:
 
-         // noop;
 
-         break;
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeChanging(TObject * /*Sender*/,
 
-   TTreeNode * /*Node*/, bool & AllowChange)
 
- {
 
-   if (!EnsureNotEditing())
 
-   {
 
-     AllowChange = false;
 
-   }
 
-   else
 
-   {
 
-     PersistNewSiteIfNeeded();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::PersistNewSiteIfNeeded()
 
- {
 
-   // Visible: Never try to save data if we did not load any yet,
 
-   // otherwise we get "HostNameEdit" and such
 
-   if (Visible && IsNewSiteNode(SessionTree->Selected))
 
-   {
 
-     SaveSession(FNewSiteData);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionAdvancedActionExecute(TObject * Sender)
 
- {
 
-   // If we ever allow showing advanced settings, while read-only,
 
-   // we must make sure that FSessionData actually holds the advanced settings,
 
-   // what it currently does not, in order to avoid master password prompt,
 
-   // while cloning the session data in LoadSession.
 
-   // To implement this, we may delegate the cloning to TWinConfiguration and
 
-   // make use of FDontDecryptPasswords
 
-   if (DebugAlwaysTrue(FSessionData != NULL))
 
-   {
 
-     // parse hostname (it may change protocol particularly) before opening advanced settings
 
-     // (HostNameEditExit is not triggered when child dialog pops up when it is invoked by accelerator)
 
-     // We should better handle this automaticaly when focus is moved to another dialog.
 
-     ParseHostName();
 
-     SaveSession(FSessionData);
 
-     if (Sender == SessionAdvancedAction)
 
-     {
 
-       DoSiteAdvancedDialog(FSessionData);
 
-     }
 
-     else
 
-     {
 
-       DoSiteRawDialog(FSessionData);
 
-     }
 
-     // Needed only for Note.
 
-     // The only other property visible on Login dialog that Advanced site dialog
 
-     // can change is protocol (between fsSFTP and fsSFTPonly),
 
-     // difference of the two not being visible on Login dialog anyway.
 
-     LoadSession(FSessionData);
 
-     if (DebugAlwaysTrue(SessionTree->Selected != NULL) &&
 
-         IsSiteNode(SessionTree->Selected))
 
-     {
 
-       SetNodeImage(SessionTree->Selected, GetSessionImageIndex(FSessionData));
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TPopupMenu * __fastcall TLoginDialog::GetSelectedNodePopupMenu()
 
- {
 
-   TPopupMenu * PopupMenu = NULL;
 
-   TTreeNode * Selected = SessionTree->Selected;
 
-   if (IsNewSiteNode(Selected))
 
-   {
 
-     PopupMenu = ManageNewSitePopupMenu;
 
-   }
 
-   else if (IsSiteNode(Selected))
 
-   {
 
-     PopupMenu = ManageSitePopupMenu;
 
-   }
 
-   else if (IsFolderNode(Selected))
 
-   {
 
-     PopupMenu = ManageFolderPopupMenu;
 
-   }
 
-   else if (IsWorkspaceNode(Selected))
 
-   {
 
-     PopupMenu = ManageWorkspacePopupMenu;
 
-   }
 
-   return DebugNotNull(PopupMenu);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ManageButtonClick(TObject * /*Sender*/)
 
- {
 
-   MenuPopup(GetSelectedNodePopupMenu(), ManageButton);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeMouseDown(TObject * /*Sender*/,
 
-   TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
 
- {
 
-   TTreeNode * Node = SessionTree->GetNodeAt(X, Y);
 
-   if ((Button == mbRight) && (Node != NULL))
 
-   {
 
-     SessionTree->Selected = Node;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SessionTreeContextPopup(TObject * /*Sender*/,
 
-   TPoint & MousePos, bool & Handled)
 
- {
 
-   ResetSitesIncrementalSearch();
 
-   TTreeNode * Node = SessionTree->GetNodeAt(MousePos.X, MousePos.Y);
 
-   // This is mostly to prevent context menu from poping up,
 
-   // while there is prompt to confirm cancelling session edit,
 
-   // when right mouse is clicked on non-selected node
 
-   if (Node != SessionTree->Selected)
 
-   {
 
-     Handled = true;
 
-   }
 
-   else
 
-   {
 
-     if (SessionTree->Selected != NULL)
 
-     {
 
-       SessionTree->PopupMenu = GetSelectedNodePopupMenu();
 
-       if (DebugNotNull(SessionTree->PopupMenu))
 
-       {
 
-         MenuPopup(SessionTree, MousePos, Handled);
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::EditCancelActionExecute(TObject * /*Sender*/)
 
- {
 
-   CancelEditing();
 
-   // reset back to saved settings
 
-   LoadContents();
 
-   UpdateControls();
 
-   // we do not want to see blinking cursor in read-only edit box
 
-   SessionTree->SetFocus();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::AdvancedButtonDropDownClick(TObject * /*Sender*/)
 
- {
 
-   MenuPopup(SessionAdvancedPopupMenu, AdvancedButton);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CancelEditing()
 
- {
 
-   FEditing = false;
 
-   // reset back the color
 
-   UpdateNodeImage(SessionTree->Selected);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CloneToNewSite()
 
- {
 
-   FNewSiteData->CopyData(SelectedSession);
 
-   FNewSiteData->MakeUniqueIn(StoredSessions);
 
-   FNewSiteKeepName = true;
 
-   NewSite();
 
-   EditSession();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CloneToNewSiteActionExecute(TObject * /*Sender*/)
 
- {
 
-   CloneToNewSite();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::Login()
 
- {
 
-   if (OpenInNewWindow() && !IsNewSiteNode(SessionTree->Selected))
 
-   {
 
-     UnicodeString Path = SessionNodePath(SessionTree->Selected);
 
-     ExecuteNewInstance(EncodeUrlString(Path));
 
-     // prevent closing the window, see below
 
-     ModalResult = mrNone;
 
-   }
 
-   else
 
-   {
 
-     // this is not needed when used from LoginButton,
 
-     // but is needed when used from popup menus
 
-     ModalResult = LoginButton->ModalResult;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::LoginActionExecute(TObject * /*Sender*/)
 
- {
 
-   Login();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::PuttyActionExecute(TObject * /*Sender*/)
 
- {
 
-   // following may take some time, so cache the shift key state,
 
-   // in case user manages to release it before following finishes
 
-   bool Close = !OpenInNewWindow();
 
-   std::unique_ptr<TSessionData> Data(CloneSelectedSession());
 
-   // putty does not support resolving environment variables in session settings
 
-   Data->ExpandEnvironmentVariables();
 
-   OpenSessionInPutty(GUIConfiguration->PuttyPath, Data.get());
 
-   if (Close)
 
-   {
 
-     ModalResult = CloseButton->ModalResult;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::LoginButtonDropDownClick(TObject * /*Sender*/)
 
- {
 
-   MenuPopup(LoginDropDownMenu, LoginButton);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ParseUrl(const UnicodeString & Url)
 
- {
 
-   std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
 
-   SaveSession(SessionData.get());
 
-   // We do not want to pass in StoredSessions as we do not want the URL be
 
-   // parsed as pointing to a stored site.
 
-   // It also prevents resetting to defaults (do we want this?)
 
-   bool DefaultsOnly; // unused
 
-   SessionData->ParseUrl(Url, NULL, NULL, DefaultsOnly, NULL, NULL, NULL, 0);
 
-   LoadSession(SessionData.get());
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::PasteUrlActionExecute(TObject * /*Sender*/)
 
- {
 
-   UnicodeString ClipboardUrl;
 
-   if (NonEmptyTextFromClipboard(ClipboardUrl))
 
-   {
 
-     if (!IsEditable())
 
-     {
 
-       // select new site node, when other node is selected and not in editing mode
 
-       SessionTree->Selected = GetNewSiteNode();
 
-     }
 
-     // sanity check
 
-     if (DebugAlwaysTrue(IsEditable()))
 
-     {
 
-       ParseUrl(ClipboardUrl);
 
-     }
 
-     // visualize the pasting
 
-     HostNameEdit->SetFocus();
 
-     HostNameEdit->SelectAll();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ParseHostName()
 
- {
 
-   UnicodeString HostName = HostNameEdit->Text;
 
-   if (!HostName.IsEmpty() &&
 
-       (StoredSessions->IsUrl(HostName) || (HostName.Pos(L"@") > 0)))
 
-   {
 
-     ParseUrl(HostName);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::HostNameEditExit(TObject * /*Sender*/)
 
- {
 
-   ParseHostName();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::GenerateUrlAction2Execute(TObject * /*Sender*/)
 
- {
 
-   if (DebugAlwaysTrue(SelectedSession != NULL))
 
-   {
 
-     PersistNewSiteIfNeeded();
 
-     std::unique_ptr<TSessionData> Data(SelectedSession->Clone());
 
-     Data->LookupLastFingerprint();
 
-     DoGenerateUrlDialog(Data.get(), NULL);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::CopyParamRuleActionExecute(TObject * /*Sender*/)
 
- {
 
-   TSessionData * Data = GetSessionData();
 
-   std::unique_ptr<TCopyParamList> CopyParamList(new TCopyParamList());
 
-   (*CopyParamList) = *GUIConfiguration->CopyParamList;
 
-   TCopyParamRuleData RuleData;
 
-   RuleData.HostName = Data->HostNameExpanded;
 
-   RuleData.UserName = Data->UserNameExpanded;
 
-   int CopyParamIndex = CopyParamList->Find(RuleData);
 
-   if (CopyParamIndex < 0)
 
-   {
 
-     TCopyParamRuleData RuleDataHostNameOnly;
 
-     RuleDataHostNameOnly.HostName = Data->HostNameExpanded;
 
-     CopyParamIndex = CopyParamList->Find(RuleDataHostNameOnly);
 
-   }
 
-   TCopyParamPresetMode Mode;
 
-   TCopyParamRuleData * CurrentRuleData = NULL;
 
-   if (CopyParamIndex < 0)
 
-   {
 
-     Mode = cpmAddCurrent;
 
-     CurrentRuleData = &RuleData;
 
-   }
 
-   else
 
-   {
 
-     Mode = cpmEdit;
 
-   }
 
-   TCopyParamType DefaultCopyParams;
 
-   if (DoCopyParamPresetDialog(
 
-         CopyParamList.get(), CopyParamIndex, Mode, CurrentRuleData, DefaultCopyParams))
 
-   {
 
-     GUIConfiguration->CopyParamList = CopyParamList.get();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SearchSiteNameStartOnlyActionExecute(TObject * /*Sender*/)
 
- {
 
-   FSiteSearch = isNameStartOnly;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SearchSiteNameActionExecute(TObject * /*Sender*/)
 
- {
 
-   FSiteSearch = isName;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::SearchSiteActionExecute(TObject * /*Sender*/)
 
- {
 
-   FSiteSearch = isAll;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::ChangeScale(int M, int D)
 
- {
 
-   TForm::ChangeScale(M, D);
 
-   FSiteButtonsPadding = MulDiv(FSiteButtonsPadding, M, D);
 
-   FBasicGroupBaseHeight = MulDiv(FBasicGroupBaseHeight, M, D);
 
-   FNoteGroupOffset = MulDiv(FNoteGroupOffset, M, D);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLoginDialog::PanelMouseDown(TObject *, TMouseButton, TShiftState, int, int)
 
- {
 
-   CountClicksForWindowPrint(this);
 
- }
 
- //---------------------------------------------------------------------------
 
 
  |