CustomWinConfiguration.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. // 0 means no "custom-pos"
  99. FSynchronizeChecklist.WindowParams = L"0;" + FormatDefaultWindowParams(600, 450);
  100. 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";
  101. FFindFile.WindowParams = FormatDefaultWindowSize(646, 481);
  102. FFindFile.ListParams = L"3;1|125,1;181,1;80,1;122,1;@" + SaveDefaultPixelsPerInch() + L"|0;1;2;3";
  103. FConsoleWin.WindowSize = FormatDefaultWindowSize(570, 430);
  104. FLoginDialog.WindowSize = FormatDefaultWindowSize(640, 430);
  105. FLoginDialog.SiteSearch = ssSiteName;
  106. FConfirmExitOnCompletion = true;
  107. FOperationProgressOnTop = true;
  108. FSessionColors = L"";
  109. FCopyShortCutHintShown = false;
  110. DefaultHistory();
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TCustomWinConfiguration::Saved()
  114. {
  115. TGUIConfiguration::Saved();
  116. THistoryStrings * HistoryStrings;
  117. for (int Index = 0; Index < FHistory->Count; Index++)
  118. {
  119. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  120. DebugAssert(HistoryStrings != NULL);
  121. HistoryStrings->Modified = false;
  122. }
  123. }
  124. //---------------------------------------------------------------------------
  125. // duplicated from core\configuration.cpp
  126. #define LASTELEM(ELEM) \
  127. ELEM.SubString(ELEM.LastDelimiter(L".>")+1, ELEM.Length() - ELEM.LastDelimiter(L".>"))
  128. #define BLOCK(KEY, CANCREATE, BLOCK) \
  129. if (Storage->OpenSubKey(KEY, CANCREATE, true)) try { BLOCK } __finally { Storage->CloseSubKey(); }
  130. #define REGCONFIG(CANCREATE) \
  131. BLOCK(L"Interface", CANCREATE, \
  132. KEY(Integer, Interface); \
  133. KEY(Bool, ConfirmExitOnCompletion); \
  134. KEY(String, SessionColors); \
  135. KEY(Bool, CopyShortCutHintShown); \
  136. ) \
  137. BLOCK(L"Interface\\SynchronizeChecklist", CANCREATE, \
  138. KEY(String, SynchronizeChecklist.WindowParams); \
  139. KEY(String, SynchronizeChecklist.ListParams); \
  140. ); \
  141. BLOCK(L"Interface\\FindFile", CANCREATE, \
  142. KEY(String, FindFile.WindowParams); \
  143. KEY(String, FindFile.ListParams); \
  144. ); \
  145. BLOCK(L"Interface\\ConsoleWin", CANCREATE, \
  146. KEY(String, ConsoleWin.WindowSize); \
  147. ); \
  148. BLOCK(L"Interface\\LoginDialog", CANCREATE, \
  149. KEY(String, LoginDialog.WindowSize); \
  150. KEY(Integer, LoginDialog.SiteSearch); \
  151. ); \
  152. //---------------------------------------------------------------------------
  153. void __fastcall TCustomWinConfiguration::SaveData(
  154. THierarchicalStorage * Storage, bool All)
  155. {
  156. TGUIConfiguration::SaveData(Storage, All);
  157. // duplicated from core\configuration.cpp
  158. #define KEY(TYPE, VAR) Storage->Write ## TYPE(LASTELEM(UnicodeString(TEXT(#VAR))), VAR)
  159. REGCONFIG(true);
  160. #undef KEY
  161. if (FHistory->Count > 0)
  162. {
  163. if (Storage->OpenSubKey(L"History", true))
  164. {
  165. try
  166. {
  167. THistoryStrings * HistoryStrings;
  168. for (int Index = 0; Index < FHistory->Count; Index++)
  169. {
  170. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  171. DebugAssert(HistoryStrings != NULL);
  172. if (All || HistoryStrings->Modified)
  173. {
  174. if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  175. {
  176. try
  177. {
  178. Storage->WriteValues(HistoryStrings);
  179. }
  180. __finally
  181. {
  182. Storage->CloseSubKey();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. __finally
  189. {
  190. Storage->CloseSubKey();
  191. }
  192. }
  193. if (Storage->OpenSubKey(L"HistoryParams", true))
  194. {
  195. try
  196. {
  197. THistoryStrings * HistoryStrings;
  198. for (int Index = 0; Index < FHistory->Count; Index++)
  199. {
  200. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  201. DebugAssert(HistoryStrings != NULL);
  202. if (All || HistoryStrings->Modified)
  203. {
  204. bool HasData = false;
  205. for (int VIndex = 0; !HasData && (VIndex < HistoryStrings->Count); VIndex++)
  206. {
  207. HasData = (HistoryStrings->Objects[VIndex] != NULL);
  208. }
  209. if (!HasData)
  210. {
  211. Storage->RecursiveDeleteSubKey(FHistory->Strings[Index]);
  212. }
  213. else if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  214. {
  215. try
  216. {
  217. Storage->ClearValues();
  218. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  219. {
  220. void * Data = HistoryStrings->Objects[VIndex];
  221. Storage->WriteBinaryData(IntToStr(VIndex), &Data, sizeof(Data));
  222. }
  223. }
  224. __finally
  225. {
  226. Storage->CloseSubKey();
  227. }
  228. }
  229. }
  230. }
  231. }
  232. __finally
  233. {
  234. Storage->CloseSubKey();
  235. }
  236. }
  237. }
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall TCustomWinConfiguration::LoadData(
  241. THierarchicalStorage * Storage)
  242. {
  243. TGUIConfiguration::LoadData(Storage);
  244. FAppliedInterface = FInterface;
  245. // duplicated from core\configuration.cpp
  246. #define KEY(TYPE, VAR) VAR = Storage->Read ## TYPE(LASTELEM(UnicodeString(TEXT(#VAR))), VAR)
  247. #pragma warn -eas
  248. REGCONFIG(false);
  249. #pragma warn +eas
  250. #undef KEY
  251. DefaultHistory();
  252. if (Storage->OpenSubKey(L"History", false))
  253. {
  254. TStrings * Names = NULL;
  255. try
  256. {
  257. Names = new TStringList();
  258. Storage->GetSubKeyNames(Names);
  259. for (int Index = 0; Index < Names->Count; Index++)
  260. {
  261. if (Storage->OpenSubKey(Names->Strings[Index], false))
  262. {
  263. THistoryStrings * HistoryStrings = NULL;
  264. try
  265. {
  266. // remove defaults, if any
  267. int HIndex = FHistory->IndexOf(Names->Strings[Index]);
  268. if (HIndex >= 0)
  269. {
  270. THistoryStrings * DefaultStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[HIndex]);
  271. delete DefaultStrings;
  272. FHistory->Delete(HIndex);
  273. }
  274. HistoryStrings = new THistoryStrings();
  275. Storage->ReadValues(HistoryStrings);
  276. FHistory->AddObject(Names->Strings[Index], HistoryStrings);
  277. HistoryStrings = NULL;
  278. }
  279. __finally
  280. {
  281. Storage->CloseSubKey();
  282. delete HistoryStrings;
  283. }
  284. }
  285. }
  286. }
  287. __finally
  288. {
  289. Storage->CloseSubKey();
  290. delete Names;
  291. }
  292. }
  293. if (Storage->OpenSubKey(L"HistoryParams", false))
  294. {
  295. try
  296. {
  297. THistoryStrings * HistoryStrings;
  298. for (int Index = 0; Index < FHistory->Count; Index++)
  299. {
  300. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  301. if (Storage->OpenSubKey(FHistory->Strings[Index], false))
  302. {
  303. try
  304. {
  305. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  306. {
  307. void * Data;
  308. if (Storage->ReadBinaryData(IntToStr(VIndex), &Data, sizeof(Data)) ==
  309. sizeof(Data))
  310. {
  311. HistoryStrings->Objects[VIndex] = reinterpret_cast<TObject *>(Data);
  312. }
  313. }
  314. }
  315. __finally
  316. {
  317. Storage->CloseSubKey();
  318. }
  319. }
  320. }
  321. }
  322. __finally
  323. {
  324. Storage->CloseSubKey();
  325. }
  326. }
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TCustomWinConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  330. {
  331. TGUIConfiguration::LoadAdmin(Storage);
  332. FDefaultInterface = TInterface(Storage->ReadInteger(L"DefaultInterfaceInterface", FDefaultInterface));
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TCustomWinConfiguration::RecryptPasswords(TStrings * RecryptPasswordErrors)
  336. {
  337. TOperationVisualizer Visualizer;
  338. StoredSessions->RecryptPasswords(RecryptPasswordErrors);
  339. if (OnMasterPasswordRecrypt != NULL)
  340. {
  341. try
  342. {
  343. OnMasterPasswordRecrypt(NULL);
  344. }
  345. catch (Exception & E)
  346. {
  347. UnicodeString Message;
  348. if (ExceptionMessage(&E, Message))
  349. {
  350. // we do not expect this really to happen,
  351. // so we do not bother providing context
  352. RecryptPasswordErrors->Add(Message);
  353. }
  354. }
  355. }
  356. }
  357. //---------------------------------------------------------------------
  358. void __fastcall TCustomWinConfiguration::AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(
  359. TSessionData * SessionData)
  360. {
  361. if (!DisablePasswordStoring &&
  362. SessionData->HasAnyPassword() &&
  363. UseMasterPassword)
  364. {
  365. AskForMasterPasswordIfNotSet();
  366. }
  367. }
  368. //---------------------------------------------------------------------------
  369. void __fastcall TCustomWinConfiguration::SetInterface(TInterface value)
  370. {
  371. SET_CONFIG_PROPERTY(Interface);
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomWinConfiguration::SetHistory(const UnicodeString Index,
  375. TStrings * value)
  376. {
  377. int I = FHistory->IndexOf(Index);
  378. bool NonEmpty = (value != NULL) && (value->Count > 0);
  379. THistoryStrings * HistoryStrings = NULL;
  380. if (I >= 0)
  381. {
  382. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[I]);
  383. if (HistoryStrings->Equals(value))
  384. {
  385. HistoryStrings = NULL;
  386. }
  387. }
  388. else if (NonEmpty)
  389. {
  390. HistoryStrings = new THistoryStrings();
  391. FHistory->AddObject(Index, HistoryStrings);
  392. }
  393. if (HistoryStrings != NULL)
  394. {
  395. if (NonEmpty)
  396. {
  397. HistoryStrings->Assign(value);
  398. while (HistoryStrings->Count > MaxHistoryCount)
  399. {
  400. HistoryStrings->Delete(HistoryStrings->Count - 1);
  401. }
  402. }
  403. else
  404. {
  405. HistoryStrings->Clear();
  406. }
  407. HistoryStrings->Modified = true;
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. TStrings * __fastcall TCustomWinConfiguration::GetHistory(const UnicodeString Index)
  412. {
  413. int I = FHistory->IndexOf(Index);
  414. return I >= 0 ? dynamic_cast<TStrings *>(FHistory->Objects[I]) : FEmptyHistory;
  415. }
  416. //---------------------------------------------------------------------------
  417. UnicodeString __fastcall TCustomWinConfiguration::GetValidHistoryKey(UnicodeString Key)
  418. {
  419. for (int Index = 1; Index <= Key.Length(); Index++)
  420. {
  421. if (!IsLetter(Key[Index]) && !IsDigit(Key[Index]))
  422. {
  423. Key[Index] = L'_';
  424. }
  425. }
  426. while (!Key.IsEmpty() && (Key[1] == L'_'))
  427. {
  428. Key.Delete(1, 1);
  429. }
  430. while (!Key.IsEmpty() && (Key[Key.Length()] == L'_'))
  431. {
  432. Key.Delete(Key.Length(), 1);
  433. }
  434. int P;
  435. while ((P = Key.Pos(L"__")) > 0)
  436. {
  437. Key.Delete(P, 1);
  438. }
  439. return Key;
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TCustomWinConfiguration::SetSynchronizeChecklist(TSynchronizeChecklistConfiguration value)
  443. {
  444. SET_CONFIG_PROPERTY(SynchronizeChecklist);
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TCustomWinConfiguration::SetFindFile(TFindFileConfiguration value)
  448. {
  449. SET_CONFIG_PROPERTY(FindFile);
  450. }
  451. //---------------------------------------------------------------------------
  452. void __fastcall TCustomWinConfiguration::SetConsoleWin(TConsoleWinConfiguration value)
  453. {
  454. SET_CONFIG_PROPERTY(ConsoleWin);
  455. }
  456. //---------------------------------------------------------------------------
  457. void __fastcall TCustomWinConfiguration::SetLoginDialog(TLoginDialogConfiguration value)
  458. {
  459. SET_CONFIG_PROPERTY(LoginDialog);
  460. }
  461. //---------------------------------------------------------------------------
  462. void __fastcall TCustomWinConfiguration::SetConfirmExitOnCompletion(bool value)
  463. {
  464. SET_CONFIG_PROPERTY(ConfirmExitOnCompletion);
  465. }
  466. //---------------------------------------------------------------------------
  467. UnicodeString __fastcall TCustomWinConfiguration::GetDefaultFixedWidthFontName()
  468. {
  469. // These are defaults for respective version of Windows Notepad
  470. UnicodeString Result;
  471. if (IsWin8())
  472. {
  473. Result = L"Consolas";
  474. }
  475. else
  476. {
  477. Result = L"Lucida Console";
  478. }
  479. return Result;
  480. }
  481. //---------------------------------------------------------------------------
  482. int __fastcall TCustomWinConfiguration::GetDefaultFixedWidthFontSize()
  483. {
  484. // These are defaults for respective version of Windows Notepad
  485. int Result;
  486. if (IsWin8())
  487. {
  488. Result = 11;
  489. }
  490. else
  491. {
  492. Result = 10;
  493. }
  494. return Result;
  495. }