CustomWinConfiguration.cpp 17 KB

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