Login.cpp 102 KB

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