Login.cpp 103 KB

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