CustomWinConfiguration.cpp 16 KB

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