CustomWinConfiguration.cpp 16 KB

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