1
0

CustomWinConfiguration.cpp 17 KB

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