CustomWinConfiguration.cpp 15 KB

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