CustomWinConfiguration.cpp 17 KB

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