CustomWinConfiguration.cpp 17 KB

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