CustomWinConfiguration.cpp 16 KB

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