CustomWinConfiguration.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. //---------------------------------------------------------------------------
  2. #include <WinPCH.h>
  3. #pragma hdrstop
  4. #include <SessionData.h>
  5. //---------------------------------------------------------------------------
  6. TCustomWinConfiguration * CustomWinConfiguration = NULL;
  7. //---------------------------------------------------------------------------
  8. class THistoryStrings : public TStringList
  9. {
  10. public:
  11. __fastcall THistoryStrings() : TStringList()
  12. {
  13. FModified = false;
  14. }
  15. __property bool Modified = { read = FModified, write = FModified };
  16. private:
  17. bool FModified;
  18. };
  19. //---------------------------------------------------------------------------
  20. __fastcall TCustomWinConfiguration::TCustomWinConfiguration():
  21. TGUIConfiguration()
  22. {
  23. FHistory = new TStringList();
  24. FEmptyHistory = new TStringList();
  25. FDefaultInterface = ifCommander;
  26. FCanApplyInterfaceImmediately = true;
  27. }
  28. //---------------------------------------------------------------------------
  29. __fastcall TCustomWinConfiguration::~TCustomWinConfiguration()
  30. {
  31. ClearHistory();
  32. delete FHistory;
  33. delete FEmptyHistory;
  34. }
  35. //---------------------------------------------------------------------------
  36. void __fastcall TCustomWinConfiguration::ClearHistory()
  37. {
  38. DebugAssert(FHistory != NULL);
  39. THistoryStrings * HistoryStrings;
  40. for (int Index = 0; Index < FHistory->Count; Index++)
  41. {
  42. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  43. FHistory->Objects[Index] = NULL;
  44. delete HistoryStrings;
  45. }
  46. FHistory->Clear();
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TCustomWinConfiguration::DefaultHistory()
  50. {
  51. ClearHistory();
  52. std::unique_ptr<THistoryStrings> Strings;
  53. // Defaults for speed limits.
  54. Strings.reset(new THistoryStrings());
  55. // This is language-specifics, what has to be dealt with when changing language.
  56. // There's ad-hoc workaround in CopySpeedLimits.
  57. // If we need to solve this for another history, we should introduce
  58. // a generic solution, like language-specific history ("SpeedLimitEN")
  59. Strings->Add(LoadStr(SPEED_UNLIMITED));
  60. unsigned long Speed = MaxSpeed;
  61. while (Speed >= MinSpeed)
  62. {
  63. Strings->Add(SetSpeedLimit(Speed));
  64. Speed = Speed / 2;
  65. }
  66. FHistory->AddObject(L"SpeedLimit", Strings.release());
  67. Strings.reset(new THistoryStrings());
  68. Strings->Add(FormatCommand(DefaultPuttyPath, L""));
  69. Strings->Add(FormatCommand(DefaultPuttyPath, L"-t -m \"%TEMP%\\putty.txt\" !`cmd.exe /c echo cd '!/' ; /bin/bash -login > \"%TEMP%\\putty.txt\"`"));
  70. Strings->Add(KittyExecutable);
  71. Strings->Add(FORMAT(L"%s -cmd \"cd '!/'\" !U@!@ -P !# -title \"!N\"", (KittyExecutable)));
  72. Strings->Add(L"%SystemRoot%\\Sysnative\\OpenSSH\\ssh.exe !U@!@ -p !#");
  73. Strings->Add(L"%SystemRoot%\\Sysnative\\OpenSSH\\ssh.exe !U@!@ -p !# -t \"cd !/ ; /bin/bash\"");
  74. FHistory->AddObject(L"PuttyPath", Strings.release());
  75. }
  76. //---------------------------------------------------------------------------
  77. UnicodeString __fastcall TCustomWinConfiguration::FormatDefaultWindowSize(int Width, int Height)
  78. {
  79. return FORMAT(L"%d,%d,%s", (Width, Height, SaveDefaultPixelsPerInch()));
  80. }
  81. //---------------------------------------------------------------------------
  82. UnicodeString __fastcall TCustomWinConfiguration::FormatDefaultWindowParams(int Width, int Height)
  83. {
  84. return FORMAT(L"-1;-1;%d;%d;%d;%s", (Width, Height, int(wsNormal), SaveDefaultPixelsPerInch()));
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TCustomWinConfiguration::Default()
  88. {
  89. TGUIConfiguration::Default();
  90. FInterface = FDefaultInterface;
  91. int WorkAreaWidthScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaWidth);
  92. int WorkAreaHeightScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaHeight);
  93. // Same as commander interface (really used only with /synchronize)
  94. int ChecklistWidth = Min(WorkAreaWidthScaled - 40, 1090);
  95. int ChecklistHeight = Min(WorkAreaHeightScaled - 30, 700);
  96. // 0 means no "custom-pos"
  97. FSynchronizeChecklist.WindowParams = L"0;" + FormatDefaultWindowParams(ChecklistWidth, ChecklistHeight);
  98. 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";
  99. FFindFile.WindowParams = FormatDefaultWindowSize(646, 481);
  100. FFindFile.ListParams = L"1;1|125,1;181,1;80,1;122,1;@" + SaveDefaultPixelsPerInch() + L"|0;1;2;3|/1";
  101. FConsoleWin.WindowSize = FormatDefaultWindowSize(570, 430);
  102. FLoginDialog.WindowSize = FormatDefaultWindowSize(640, 430);
  103. FLoginDialog.SiteSearch = isName;
  104. FConfirmExitOnCompletion = true;
  105. FSynchronizeSummary = true;
  106. FSessionColors = L"";
  107. FFontColors = L"";
  108. FCopyShortCutHintShown = false;
  109. FHttpForWebDAV = false;
  110. FDefaultFixedWidthFontName = L"";
  111. FDefaultFixedWidthFontSize = 0;
  112. DefaultHistory();
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TCustomWinConfiguration::Saved()
  116. {
  117. TGUIConfiguration::Saved();
  118. THistoryStrings * HistoryStrings;
  119. for (int Index = 0; Index < FHistory->Count; Index++)
  120. {
  121. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  122. DebugAssert(HistoryStrings != NULL);
  123. HistoryStrings->Modified = false;
  124. }
  125. }
  126. //---------------------------------------------------------------------------
  127. // duplicated from core\configuration.cpp
  128. #define BLOCK(KEY, CANCREATE, BLOCK) \
  129. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  130. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  131. #define REGCONFIG(CANCREATE) \
  132. BLOCK(L"Interface", CANCREATE, \
  133. KEY(Enum, Interface); \
  134. KEY(Bool, ConfirmExitOnCompletion); \
  135. KEY(Bool, SynchronizeSummary); \
  136. KEY(String, SessionColors); \
  137. KEY(String, FontColors); \
  138. KEY(Bool, CopyShortCutHintShown); \
  139. KEY(Bool, HttpForWebDAV); \
  140. KEY(String, FDefaultFixedWidthFontName); \
  141. KEY(Integer, FDefaultFixedWidthFontSize); \
  142. ) \
  143. BLOCK(L"Interface\\SynchronizeChecklist", CANCREATE, \
  144. KEY(String, SynchronizeChecklist.WindowParams); \
  145. KEY(String, SynchronizeChecklist.ListParams); \
  146. ); \
  147. BLOCK(L"Interface\\FindFile", CANCREATE, \
  148. KEY(String, FindFile.WindowParams); \
  149. KEY(String, FindFile.ListParams); \
  150. ); \
  151. BLOCK(L"Interface\\ConsoleWin", CANCREATE, \
  152. KEY(String, ConsoleWin.WindowSize); \
  153. ); \
  154. BLOCK(L"Interface\\LoginDialog", CANCREATE, \
  155. KEY(String, LoginDialog.WindowSize); \
  156. KEY(Enum, LoginDialog.SiteSearch); \
  157. ); \
  158. //---------------------------------------------------------------------------
  159. void __fastcall TCustomWinConfiguration::SaveData(
  160. THierarchicalStorage * Storage, bool All)
  161. {
  162. TGUIConfiguration::SaveData(Storage, All);
  163. // duplicated from core\configuration.cpp
  164. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  165. #pragma warn -eas
  166. REGCONFIG(true);
  167. #pragma warn +eas
  168. #undef KEYEX
  169. if (FHistory->Count > 0)
  170. {
  171. if (Storage->OpenSubKey(L"History", true))
  172. {
  173. try
  174. {
  175. THistoryStrings * HistoryStrings;
  176. for (int Index = 0; Index < FHistory->Count; Index++)
  177. {
  178. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  179. DebugAssert(HistoryStrings != NULL);
  180. if (All || HistoryStrings->Modified)
  181. {
  182. if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  183. {
  184. try
  185. {
  186. Storage->WriteValues(HistoryStrings);
  187. }
  188. __finally
  189. {
  190. Storage->CloseSubKey();
  191. }
  192. }
  193. }
  194. }
  195. }
  196. __finally
  197. {
  198. Storage->CloseSubKey();
  199. }
  200. }
  201. if (Storage->OpenSubKey(L"HistoryParams", true))
  202. {
  203. try
  204. {
  205. THistoryStrings * HistoryStrings;
  206. for (int Index = 0; Index < FHistory->Count; Index++)
  207. {
  208. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  209. DebugAssert(HistoryStrings != NULL);
  210. if (All || HistoryStrings->Modified)
  211. {
  212. bool HasData = false;
  213. for (int VIndex = 0; !HasData && (VIndex < HistoryStrings->Count); VIndex++)
  214. {
  215. HasData = (HistoryStrings->Objects[VIndex] != NULL);
  216. }
  217. if (!HasData)
  218. {
  219. Storage->RecursiveDeleteSubKey(FHistory->Strings[Index]);
  220. }
  221. else if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  222. {
  223. try
  224. {
  225. Storage->ClearValues();
  226. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  227. {
  228. void * Data = HistoryStrings->Objects[VIndex];
  229. Storage->WriteBinaryData(IntToStr(VIndex), &Data, sizeof(Data));
  230. }
  231. }
  232. __finally
  233. {
  234. Storage->CloseSubKey();
  235. }
  236. }
  237. }
  238. }
  239. }
  240. __finally
  241. {
  242. Storage->CloseSubKey();
  243. }
  244. }
  245. }
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TCustomWinConfiguration::LoadData(
  249. THierarchicalStorage * Storage)
  250. {
  251. TGUIConfiguration::LoadData(Storage);
  252. FAppliedInterface = FInterface;
  253. // duplicated from core\configuration.cpp
  254. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(NAME, VAR)
  255. #pragma warn -eas
  256. REGCONFIG(false);
  257. #pragma warn +eas
  258. #undef KEYEX
  259. DefaultHistory();
  260. if (Storage->OpenSubKey(L"History", false))
  261. {
  262. TStrings * Names = NULL;
  263. try
  264. {
  265. Names = new TStringList();
  266. Storage->GetSubKeyNames(Names);
  267. for (int Index = 0; Index < Names->Count; Index++)
  268. {
  269. if (Storage->OpenSubKey(Names->Strings[Index], false))
  270. {
  271. THistoryStrings * HistoryStrings = NULL;
  272. try
  273. {
  274. // remove defaults, if any
  275. int HIndex = FHistory->IndexOf(Names->Strings[Index]);
  276. if (HIndex >= 0)
  277. {
  278. THistoryStrings * DefaultStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[HIndex]);
  279. delete DefaultStrings;
  280. FHistory->Delete(HIndex);
  281. }
  282. HistoryStrings = new THistoryStrings();
  283. Storage->ReadValues(HistoryStrings);
  284. FHistory->AddObject(Names->Strings[Index], HistoryStrings);
  285. HistoryStrings = NULL;
  286. }
  287. __finally
  288. {
  289. Storage->CloseSubKey();
  290. delete HistoryStrings;
  291. }
  292. }
  293. }
  294. }
  295. __finally
  296. {
  297. Storage->CloseSubKey();
  298. delete Names;
  299. }
  300. }
  301. if (Storage->OpenSubKey(L"HistoryParams", false))
  302. {
  303. try
  304. {
  305. THistoryStrings * HistoryStrings;
  306. for (int Index = 0; Index < FHistory->Count; Index++)
  307. {
  308. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  309. if (Storage->OpenSubKey(FHistory->Strings[Index], false))
  310. {
  311. try
  312. {
  313. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  314. {
  315. void * Data;
  316. if (Storage->ReadBinaryData(IntToStr(VIndex), &Data, sizeof(Data)) ==
  317. sizeof(Data))
  318. {
  319. HistoryStrings->Objects[VIndex] = reinterpret_cast<TObject *>(Data);
  320. }
  321. }
  322. }
  323. __finally
  324. {
  325. Storage->CloseSubKey();
  326. }
  327. }
  328. }
  329. }
  330. __finally
  331. {
  332. Storage->CloseSubKey();
  333. }
  334. }
  335. }
  336. //---------------------------------------------------------------------------
  337. void __fastcall TCustomWinConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  338. {
  339. TGUIConfiguration::LoadAdmin(Storage);
  340. FDefaultInterface = TInterface(Storage->ReadInteger(L"DefaultInterfaceInterface", FDefaultInterface));
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TCustomWinConfiguration::RecryptPasswords(TStrings * RecryptPasswordErrors)
  344. {
  345. TOperationVisualizer Visualizer;
  346. StoredSessions->RecryptPasswords(RecryptPasswordErrors);
  347. if (OnMasterPasswordRecrypt != NULL)
  348. {
  349. try
  350. {
  351. OnMasterPasswordRecrypt(NULL);
  352. }
  353. catch (Exception & E)
  354. {
  355. UnicodeString Message;
  356. if (ExceptionMessage(&E, Message))
  357. {
  358. // we do not expect this really to happen,
  359. // so we do not bother providing context
  360. RecryptPasswordErrors->Add(Message);
  361. }
  362. }
  363. }
  364. }
  365. //---------------------------------------------------------------------
  366. void __fastcall TCustomWinConfiguration::AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(
  367. TSessionData * SessionData)
  368. {
  369. if (!DisablePasswordStoring &&
  370. SessionData->HasAnyPassword() &&
  371. UseMasterPassword)
  372. {
  373. AskForMasterPasswordIfNotSet();
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TCustomWinConfiguration::SetInterface(TInterface value)
  378. {
  379. SET_CONFIG_PROPERTY(Interface);
  380. }
  381. //---------------------------------------------------------------------------
  382. void __fastcall TCustomWinConfiguration::SetHistory(const UnicodeString Index,
  383. TStrings * value)
  384. {
  385. int I = FHistory->IndexOf(Index);
  386. bool NonEmpty = (value != NULL) && (value->Count > 0);
  387. THistoryStrings * HistoryStrings = NULL;
  388. if (I >= 0)
  389. {
  390. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[I]);
  391. if (HistoryStrings->Equals(value))
  392. {
  393. HistoryStrings = NULL;
  394. }
  395. }
  396. else if (NonEmpty)
  397. {
  398. HistoryStrings = new THistoryStrings();
  399. FHistory->AddObject(Index, HistoryStrings);
  400. }
  401. if (HistoryStrings != NULL)
  402. {
  403. if (NonEmpty)
  404. {
  405. HistoryStrings->Assign(value);
  406. while (HistoryStrings->Count > MaxHistoryCount)
  407. {
  408. HistoryStrings->Delete(HistoryStrings->Count - 1);
  409. }
  410. }
  411. else
  412. {
  413. HistoryStrings->Clear();
  414. }
  415. HistoryStrings->Modified = true;
  416. }
  417. }
  418. //---------------------------------------------------------------------------
  419. TStrings * __fastcall TCustomWinConfiguration::GetHistory(const UnicodeString Index)
  420. {
  421. int I = FHistory->IndexOf(Index);
  422. return I >= 0 ? dynamic_cast<TStrings *>(FHistory->Objects[I]) : FEmptyHistory;
  423. }
  424. //---------------------------------------------------------------------------
  425. UnicodeString __fastcall TCustomWinConfiguration::GetValidHistoryKey(UnicodeString Key)
  426. {
  427. for (int Index = 1; Index <= Key.Length(); Index++)
  428. {
  429. if (!IsLetter(Key[Index]) && !IsDigit(Key[Index]))
  430. {
  431. Key[Index] = L'_';
  432. }
  433. }
  434. while (!Key.IsEmpty() && (Key[1] == L'_'))
  435. {
  436. Key.Delete(1, 1);
  437. }
  438. while (!Key.IsEmpty() && (Key[Key.Length()] == L'_'))
  439. {
  440. Key.Delete(Key.Length(), 1);
  441. }
  442. int P;
  443. while ((P = Key.Pos(L"__")) > 0)
  444. {
  445. Key.Delete(P, 1);
  446. }
  447. return Key;
  448. }
  449. //---------------------------------------------------------------------------
  450. void __fastcall TCustomWinConfiguration::SetSynchronizeChecklist(TSynchronizeChecklistConfiguration value)
  451. {
  452. SET_CONFIG_PROPERTY(SynchronizeChecklist);
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TCustomWinConfiguration::SetFindFile(TFindFileConfiguration value)
  456. {
  457. SET_CONFIG_PROPERTY(FindFile);
  458. }
  459. //---------------------------------------------------------------------------
  460. void __fastcall TCustomWinConfiguration::SetConsoleWin(TConsoleWinConfiguration value)
  461. {
  462. SET_CONFIG_PROPERTY(ConsoleWin);
  463. }
  464. //---------------------------------------------------------------------------
  465. void __fastcall TCustomWinConfiguration::SetLoginDialog(TLoginDialogConfiguration value)
  466. {
  467. SET_CONFIG_PROPERTY(LoginDialog);
  468. }
  469. //---------------------------------------------------------------------------
  470. void __fastcall TCustomWinConfiguration::SetConfirmExitOnCompletion(bool value)
  471. {
  472. SET_CONFIG_PROPERTY(ConfirmExitOnCompletion);
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomWinConfiguration::SetSynchronizeSummary(bool value)
  476. {
  477. SET_CONFIG_PROPERTY(SynchronizeSummary);
  478. }
  479. //---------------------------------------------------------------------------
  480. UnicodeString __fastcall TCustomWinConfiguration::GetDefaultFixedWidthFontName()
  481. {
  482. // These are defaults for respective version of Windows Notepad
  483. UnicodeString Result;
  484. if (!FDefaultFixedWidthFontName.IsEmpty())
  485. {
  486. Result = FDefaultFixedWidthFontName;
  487. }
  488. else 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 (FDefaultFixedWidthFontSize != 0)
  504. {
  505. Result = FDefaultFixedWidthFontSize;
  506. }
  507. else if (IsWin8())
  508. {
  509. Result = 11;
  510. }
  511. else
  512. {
  513. Result = 10;
  514. }
  515. return Result;
  516. }