CustomWinConfiguration.cpp 17 KB

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