CustomWinConfiguration.cpp 17 KB

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