Login.cpp 104 KB

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