Login.cpp 88 KB

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