Login.cpp 87 KB

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