CustomWinConfiguration.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <TextsCore.h>
  6. #include <SessionData.h>
  7. #include <CoreMain.h>
  8. #include <Interface.h>
  9. #include "CustomWinConfiguration.h"
  10. #include <Exceptions.h>
  11. #include <PasTools.hpp>
  12. #include <Math.hpp>
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. //---------------------------------------------------------------------------
  16. TCustomWinConfiguration * CustomWinConfiguration = NULL;
  17. //---------------------------------------------------------------------------
  18. class THistoryStrings : public TStringList
  19. {
  20. public:
  21. __fastcall THistoryStrings() : TStringList()
  22. {
  23. FModified = false;
  24. }
  25. __property bool Modified = { read = FModified, write = FModified };
  26. private:
  27. bool FModified;
  28. };
  29. //---------------------------------------------------------------------------
  30. __fastcall TCustomWinConfiguration::TCustomWinConfiguration():
  31. TGUIConfiguration()
  32. {
  33. FHistory = new TStringList();
  34. FEmptyHistory = new TStringList();
  35. FDefaultInterface = ifCommander;
  36. FCanApplyInterfaceImmediately = true;
  37. }
  38. //---------------------------------------------------------------------------
  39. __fastcall TCustomWinConfiguration::~TCustomWinConfiguration()
  40. {
  41. ClearHistory();
  42. delete FHistory;
  43. delete FEmptyHistory;
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TCustomWinConfiguration::ClearHistory()
  47. {
  48. DebugAssert(FHistory != NULL);
  49. THistoryStrings * HistoryStrings;
  50. for (int Index = 0; Index < FHistory->Count; Index++)
  51. {
  52. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  53. FHistory->Objects[Index] = NULL;
  54. delete HistoryStrings;
  55. }
  56. FHistory->Clear();
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TCustomWinConfiguration::DefaultHistory()
  60. {
  61. ClearHistory();
  62. std::unique_ptr<THistoryStrings> Strings;
  63. // Defaults for speed limits.
  64. Strings.reset(new THistoryStrings());
  65. // This is language-specifics, what has to be dealt with when changing language.
  66. // There's ad-hoc workaround in CopySpeedLimits.
  67. // If we need to solve this for another history, we should introduce
  68. // a generic solution, like language-specific history ("SpeedLimitEN")
  69. Strings->Add(LoadStr(SPEED_UNLIMITED));
  70. unsigned long Speed = MaxSpeed;
  71. while (Speed >= MinSpeed)
  72. {
  73. Strings->Add(SetSpeedLimit(Speed));
  74. Speed = Speed / 2;
  75. }
  76. FHistory->AddObject(L"SpeedLimit", Strings.release());
  77. Strings.reset(new THistoryStrings());
  78. Strings->Add(FormatCommand(DefaultPuttyPath, L""));
  79. Strings->Add(FormatCommand(DefaultPuttyPath, L"-t -m \"%TEMP%\\putty.txt\" !`cmd.exe /c echo cd '!/' ; /bin/bash -login > \"%TEMP%\\putty.txt\"`"));
  80. Strings->Add(KittyExecutable);
  81. Strings->Add(FORMAT(L"%s -cmd \"cd '!/'\" !U@!@ -P !# -title \"!N\"", (KittyExecutable)));
  82. Strings->Add(L"%SystemRoot%\\Sysnative\\OpenSSH\\ssh.exe !U@!@ -p !#");
  83. Strings->Add(L"%SystemRoot%\\Sysnative\\OpenSSH\\ssh.exe !U@!@ -p !# -t \"cd !/ ; /bin/bash\"");
  84. FHistory->AddObject(L"PuttyPath", Strings.release());
  85. }
  86. //---------------------------------------------------------------------------
  87. UnicodeString __fastcall TCustomWinConfiguration::FormatDefaultWindowSize(int Width, int Height)
  88. {
  89. return FORMAT(L"%d,%d,%s", (Width, Height, SaveDefaultPixelsPerInch()));
  90. }
  91. //---------------------------------------------------------------------------
  92. UnicodeString __fastcall TCustomWinConfiguration::FormatDefaultWindowParams(int Width, int Height)
  93. {
  94. return FORMAT(L"-1;-1;%d;%d;%d;%s", (Width, Height, int(wsNormal), SaveDefaultPixelsPerInch()));
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TCustomWinConfiguration::Default()
  98. {
  99. TGUIConfiguration::Default();
  100. FInterface = FDefaultInterface;
  101. int WorkAreaWidthScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaWidth);
  102. int WorkAreaHeightScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaHeight);
  103. // Same as commander interface (really used only with /synchronize)
  104. int ChecklistWidth = Min(WorkAreaWidthScaled - 40, 1090);
  105. int ChecklistHeight = Min(WorkAreaHeightScaled - 30, 700);
  106. // 0 means no "custom-pos"
  107. FSynchronizeChecklist.WindowParams = L"0;" + FormatDefaultWindowParams(ChecklistWidth, ChecklistHeight);
  108. FSynchronizeChecklist.ListParams = L"1;1|150,1;100,1;80,1;130,1;25,1;100,1;80,1;130,1;@" + SaveDefaultPixelsPerInch() + L"|0;1;2;3;4;5;6;7";
  109. FFindFile.WindowParams = FormatDefaultWindowSize(646, 481);
  110. FFindFile.ListParams = L"1;1|125,1;181,1;80,1;122,1;@" + SaveDefaultPixelsPerInch() + L"|0;1;2;3|/1";
  111. FConsoleWin.WindowSize = FormatDefaultWindowSize(570, 430);
  112. FLoginDialog.WindowSize = FormatDefaultWindowSize(640, 430);
  113. FLoginDialog.SiteSearch = isName;
  114. FConfirmExitOnCompletion = true;
  115. FSynchronizeSummary = true;
  116. FSessionColors = L"";
  117. FFontColors = L"";
  118. FCopyShortCutHintShown = false;
  119. FHttpForWebDAV = false;
  120. FDefaultFixedWidthFontName = L"";
  121. FDefaultFixedWidthFontSize = 0;
  122. DefaultHistory();
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TCustomWinConfiguration::Saved()
  126. {
  127. TGUIConfiguration::Saved();
  128. THistoryStrings * HistoryStrings;
  129. for (int Index = 0; Index < FHistory->Count; Index++)
  130. {
  131. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  132. DebugAssert(HistoryStrings != NULL);
  133. HistoryStrings->Modified = false;
  134. }
  135. }
  136. //---------------------------------------------------------------------------
  137. // duplicated from core\configuration.cpp
  138. #define BLOCK(KEY, CANCREATE, BLOCK) \
  139. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  140. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  141. #define REGCONFIG(CANCREATE) \
  142. BLOCK(L"Interface", CANCREATE, \
  143. KEY(Enum, Interface); \
  144. KEY(Bool, ConfirmExitOnCompletion); \
  145. KEY(Bool, SynchronizeSummary); \
  146. KEY(String, SessionColors); \
  147. KEY(String, FontColors); \
  148. KEY(Bool, CopyShortCutHintShown); \
  149. KEY(Bool, HttpForWebDAV); \
  150. KEY(String, FDefaultFixedWidthFontName); \
  151. KEY(Integer, FDefaultFixedWidthFontSize); \
  152. ) \
  153. BLOCK(L"Interface\\SynchronizeChecklist", CANCREATE, \
  154. KEY(String, SynchronizeChecklist.WindowParams); \
  155. KEY(String, SynchronizeChecklist.ListParams); \
  156. ); \
  157. BLOCK(L"Interface\\FindFile", CANCREATE, \
  158. KEY(String, FindFile.WindowParams); \
  159. KEY(String, FindFile.ListParams); \
  160. ); \
  161. BLOCK(L"Interface\\ConsoleWin", CANCREATE, \
  162. KEY(String, ConsoleWin.WindowSize); \
  163. ); \
  164. BLOCK(L"Interface\\LoginDialog", CANCREATE, \
  165. KEY(String, LoginDialog.WindowSize); \
  166. KEY(Enum, LoginDialog.SiteSearch); \
  167. ); \
  168. //---------------------------------------------------------------------------
  169. void __fastcall TCustomWinConfiguration::SaveData(
  170. THierarchicalStorage * Storage, bool All)
  171. {
  172. TGUIConfiguration::SaveData(Storage, All);
  173. // duplicated from core\configuration.cpp
  174. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  175. #pragma warn -eas
  176. REGCONFIG(true);
  177. #pragma warn +eas
  178. #undef KEYEX
  179. if (FHistory->Count > 0)
  180. {
  181. if (Storage->OpenSubKey(L"History", true))
  182. {
  183. try
  184. {
  185. THistoryStrings * HistoryStrings;
  186. for (int Index = 0; Index < FHistory->Count; Index++)
  187. {
  188. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  189. DebugAssert(HistoryStrings != NULL);
  190. if (All || HistoryStrings->Modified)
  191. {
  192. if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  193. {
  194. try
  195. {
  196. Storage->WriteValues(HistoryStrings);
  197. }
  198. __finally
  199. {
  200. Storage->CloseSubKey();
  201. }
  202. }
  203. }
  204. }
  205. }
  206. __finally
  207. {
  208. Storage->CloseSubKey();
  209. }
  210. }
  211. if (Storage->OpenSubKey(L"HistoryParams", true))
  212. {
  213. try
  214. {
  215. THistoryStrings * HistoryStrings;
  216. for (int Index = 0; Index < FHistory->Count; Index++)
  217. {
  218. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  219. DebugAssert(HistoryStrings != NULL);
  220. if (All || HistoryStrings->Modified)
  221. {
  222. bool HasData = false;
  223. for (int VIndex = 0; !HasData && (VIndex < HistoryStrings->Count); VIndex++)
  224. {
  225. HasData = (HistoryStrings->Objects[VIndex] != NULL);
  226. }
  227. if (!HasData)
  228. {
  229. Storage->RecursiveDeleteSubKey(FHistory->Strings[Index]);
  230. }
  231. else if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  232. {
  233. try
  234. {
  235. Storage->ClearValues();
  236. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  237. {
  238. void * Data = HistoryStrings->Objects[VIndex];
  239. Storage->WriteBinaryData(IntToStr(VIndex), &Data, sizeof(Data));
  240. }
  241. }
  242. __finally
  243. {
  244. Storage->CloseSubKey();
  245. }
  246. }
  247. }
  248. }
  249. }
  250. __finally
  251. {
  252. Storage->CloseSubKey();
  253. }
  254. }
  255. }
  256. }
  257. //---------------------------------------------------------------------------
  258. void __fastcall TCustomWinConfiguration::LoadData(
  259. THierarchicalStorage * Storage)
  260. {
  261. TGUIConfiguration::LoadData(Storage);
  262. FAppliedInterface = FInterface;
  263. // duplicated from core\configuration.cpp
  264. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(NAME, VAR)
  265. #pragma warn -eas
  266. REGCONFIG(false);
  267. #pragma warn +eas
  268. #undef KEYEX
  269. DefaultHistory();
  270. if (Storage->OpenSubKey(L"History", false))
  271. {
  272. TStrings * Names = NULL;
  273. try
  274. {
  275. Names = new TStringList();
  276. Storage->GetSubKeyNames(Names);
  277. for (int Index = 0; Index < Names->Count; Index++)
  278. {
  279. if (Storage->OpenSubKey(Names->Strings[Index], false))
  280. {
  281. THistoryStrings * HistoryStrings = NULL;
  282. try
  283. {
  284. // remove defaults, if any
  285. int HIndex = FHistory->IndexOf(Names->Strings[Index]);
  286. if (HIndex >= 0)
  287. {
  288. THistoryStrings * DefaultStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[HIndex]);
  289. delete DefaultStrings;
  290. FHistory->Delete(HIndex);
  291. }
  292. HistoryStrings = new THistoryStrings();
  293. Storage->ReadValues(HistoryStrings);
  294. FHistory->AddObject(Names->Strings[Index], HistoryStrings);
  295. HistoryStrings = NULL;
  296. }
  297. __finally
  298. {
  299. Storage->CloseSubKey();
  300. delete HistoryStrings;
  301. }
  302. }
  303. }
  304. }
  305. __finally
  306. {
  307. Storage->CloseSubKey();
  308. delete Names;
  309. }
  310. }
  311. if (Storage->OpenSubKey(L"HistoryParams", false))
  312. {
  313. try
  314. {
  315. THistoryStrings * HistoryStrings;
  316. for (int Index = 0; Index < FHistory->Count; Index++)
  317. {
  318. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  319. if (Storage->OpenSubKey(FHistory->Strings[Index], false))
  320. {
  321. try
  322. {
  323. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  324. {
  325. void * Data;
  326. if (Storage->ReadBinaryData(IntToStr(VIndex), &Data, sizeof(Data)) ==
  327. sizeof(Data))
  328. {
  329. HistoryStrings->Objects[VIndex] = reinterpret_cast<TObject *>(Data);
  330. }
  331. }
  332. }
  333. __finally
  334. {
  335. Storage->CloseSubKey();
  336. }
  337. }
  338. }
  339. }
  340. __finally
  341. {
  342. Storage->CloseSubKey();
  343. }
  344. }
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TCustomWinConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  348. {
  349. TGUIConfiguration::LoadAdmin(Storage);
  350. FDefaultInterface = TInterface(Storage->ReadInteger(L"DefaultInterfaceInterface", FDefaultInterface));
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TCustomWinConfiguration::RecryptPasswords(TStrings * RecryptPasswordErrors)
  354. {
  355. TOperationVisualizer Visualizer;
  356. StoredSessions->RecryptPasswords(RecryptPasswordErrors);
  357. if (OnMasterPasswordRecrypt != NULL)
  358. {
  359. try
  360. {
  361. OnMasterPasswordRecrypt(NULL);
  362. }
  363. catch (Exception & E)
  364. {
  365. UnicodeString Message;
  366. if (ExceptionMessage(&E, Message))
  367. {
  368. // we do not expect this really to happen,
  369. // so we do not bother providing context
  370. RecryptPasswordErrors->Add(Message);
  371. }
  372. }
  373. }
  374. }
  375. //---------------------------------------------------------------------
  376. void __fastcall TCustomWinConfiguration::AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(
  377. TSessionData * SessionData)
  378. {
  379. if (!DisablePasswordStoring &&
  380. SessionData->HasAnyPassword() &&
  381. UseMasterPassword)
  382. {
  383. AskForMasterPasswordIfNotSet();
  384. }
  385. }
  386. //---------------------------------------------------------------------------
  387. void __fastcall TCustomWinConfiguration::SetInterface(TInterface value)
  388. {
  389. SET_CONFIG_PROPERTY(Interface);
  390. }
  391. //---------------------------------------------------------------------------
  392. void __fastcall TCustomWinConfiguration::SetHistory(const UnicodeString Index,
  393. TStrings * value)
  394. {
  395. int I = FHistory->IndexOf(Index);
  396. bool NonEmpty = (value != NULL) && (value->Count > 0);
  397. THistoryStrings * HistoryStrings = NULL;
  398. if (I >= 0)
  399. {
  400. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[I]);
  401. if (HistoryStrings->Equals(value))
  402. {
  403. HistoryStrings = NULL;
  404. }
  405. }
  406. else if (NonEmpty)
  407. {
  408. HistoryStrings = new THistoryStrings();
  409. FHistory->AddObject(Index, HistoryStrings);
  410. }
  411. if (HistoryStrings != NULL)
  412. {
  413. if (NonEmpty)
  414. {
  415. HistoryStrings->Assign(value);
  416. while (HistoryStrings->Count > MaxHistoryCount)
  417. {
  418. HistoryStrings->Delete(HistoryStrings->Count - 1);
  419. }
  420. }
  421. else
  422. {
  423. HistoryStrings->Clear();
  424. }
  425. HistoryStrings->Modified = true;
  426. }
  427. }
  428. //---------------------------------------------------------------------------
  429. TStrings * __fastcall TCustomWinConfiguration::GetHistory(const UnicodeString Index)
  430. {
  431. int I = FHistory->IndexOf(Index);
  432. return I >= 0 ? dynamic_cast<TStrings *>(FHistory->Objects[I]) : FEmptyHistory;
  433. }
  434. //---------------------------------------------------------------------------
  435. UnicodeString __fastcall TCustomWinConfiguration::GetValidHistoryKey(UnicodeString Key)
  436. {
  437. for (int Index = 1; Index <= Key.Length(); Index++)
  438. {
  439. if (!IsLetter(Key[Index]) && !IsDigit(Key[Index]))
  440. {
  441. Key[Index] = L'_';
  442. }
  443. }
  444. while (!Key.IsEmpty() && (Key[1] == L'_'))
  445. {
  446. Key.Delete(1, 1);
  447. }
  448. while (!Key.IsEmpty() && (Key[Key.Length()] == L'_'))
  449. {
  450. Key.Delete(Key.Length(), 1);
  451. }
  452. int P;
  453. while ((P = Key.Pos(L"__")) > 0)
  454. {
  455. Key.Delete(P, 1);
  456. }
  457. return Key;
  458. }
  459. //---------------------------------------------------------------------------
  460. void __fastcall TCustomWinConfiguration::SetSynchronizeChecklist(TSynchronizeChecklistConfiguration value)
  461. {
  462. SET_CONFIG_PROPERTY(SynchronizeChecklist);
  463. }
  464. //---------------------------------------------------------------------------
  465. void __fastcall TCustomWinConfiguration::SetFindFile(TFindFileConfiguration value)
  466. {
  467. SET_CONFIG_PROPERTY(FindFile);
  468. }
  469. //---------------------------------------------------------------------------
  470. void __fastcall TCustomWinConfiguration::SetConsoleWin(TConsoleWinConfiguration value)
  471. {
  472. SET_CONFIG_PROPERTY(ConsoleWin);
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomWinConfiguration::SetLoginDialog(TLoginDialogConfiguration value)
  476. {
  477. SET_CONFIG_PROPERTY(LoginDialog);
  478. }
  479. //---------------------------------------------------------------------------
  480. void __fastcall TCustomWinConfiguration::SetConfirmExitOnCompletion(bool value)
  481. {
  482. SET_CONFIG_PROPERTY(ConfirmExitOnCompletion);
  483. }
  484. //---------------------------------------------------------------------------
  485. void __fastcall TCustomWinConfiguration::SetSynchronizeSummary(bool value)
  486. {
  487. SET_CONFIG_PROPERTY(SynchronizeSummary);
  488. }
  489. //---------------------------------------------------------------------------
  490. UnicodeString __fastcall TCustomWinConfiguration::GetDefaultFixedWidthFontName()
  491. {
  492. // These are defaults for respective version of Windows Notepad
  493. UnicodeString Result;
  494. if (!FDefaultFixedWidthFontName.IsEmpty())
  495. {
  496. Result = FDefaultFixedWidthFontName;
  497. }
  498. else if (IsWin8())
  499. {
  500. Result = L"Consolas";
  501. }
  502. else
  503. {
  504. Result = L"Lucida Console";
  505. }
  506. return Result;
  507. }
  508. //---------------------------------------------------------------------------
  509. int __fastcall TCustomWinConfiguration::GetDefaultFixedWidthFontSize()
  510. {
  511. // These are defaults for respective version of Windows Notepad
  512. int Result;
  513. if (FDefaultFixedWidthFontSize != 0)
  514. {
  515. Result = FDefaultFixedWidthFontSize;
  516. }
  517. else if (IsWin8())
  518. {
  519. Result = 11;
  520. }
  521. else
  522. {
  523. Result = 10;
  524. }
  525. return Result;
  526. }