CustomWinConfiguration.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. //---------------------------------------------------------------------------
  13. class THistoryStrings : public TStringList
  14. {
  15. public:
  16. __fastcall THistoryStrings() : TStringList()
  17. {
  18. FModified = false;
  19. };
  20. __property bool Modified = { read = FModified, write = FModified };
  21. private:
  22. bool FModified;
  23. };
  24. //---------------------------------------------------------------------------
  25. __fastcall TCustomWinConfiguration::TCustomWinConfiguration():
  26. TGUIConfiguration()
  27. {
  28. FHistory = new TStringList();
  29. FEmptyHistory = new TStringList();
  30. FDefaultInterface = ifCommander;
  31. FDefaultShowAdvancedLoginOptions = false;
  32. }
  33. //---------------------------------------------------------------------------
  34. __fastcall TCustomWinConfiguration::~TCustomWinConfiguration()
  35. {
  36. ClearHistory();
  37. delete FHistory;
  38. delete FEmptyHistory;
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TCustomWinConfiguration::ClearHistory()
  42. {
  43. assert(FHistory != NULL);
  44. THistoryStrings * HistoryStrings;
  45. for (int Index = 0; Index < FHistory->Count; Index++)
  46. {
  47. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  48. FHistory->Objects[Index] = NULL;
  49. delete HistoryStrings;
  50. }
  51. FHistory->Clear();
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TCustomWinConfiguration::DefaultHistory()
  55. {
  56. ClearHistory();
  57. // defaults for speed limits
  58. THistoryStrings * Strings = new THistoryStrings();
  59. Strings->Add(LoadStr(SPEED_UNLIMITED));
  60. unsigned long Speed = 1024;
  61. while (Speed >= 8)
  62. {
  63. Strings->Add(IntToStr(Speed));
  64. Speed = Speed / 2;
  65. }
  66. FHistory->AddObject("SpeedLimit", Strings);
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TCustomWinConfiguration::Default()
  70. {
  71. TGUIConfiguration::Default();
  72. FShowAdvancedLoginOptions = FDefaultShowAdvancedLoginOptions;
  73. FInterface = FDefaultInterface;
  74. FLogView = lvNone;
  75. FSynchronizeChecklist.WindowParams = "0;-1;-1;600;450;0";
  76. FSynchronizeChecklist.ListParams = "1;1|150,1;100,1;80,1;130,1;25,1;100,1;80,1;130,1|0;1;2;3;4;5;6;7";
  77. FFindFile.WindowParams = "646,481";
  78. FFindFile.ListParams = "3;1|125,1;181,1;80,1;122,1|0;1;2;3";
  79. FConsoleWin.WindowSize = "570,430";
  80. FConfirmExitOnCompletion = true;
  81. DefaultHistory();
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TCustomWinConfiguration::Saved()
  85. {
  86. TGUIConfiguration::Saved();
  87. THistoryStrings * HistoryStrings;
  88. for (int Index = 0; Index < FHistory->Count; Index++)
  89. {
  90. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  91. assert(HistoryStrings != NULL);
  92. HistoryStrings->Modified = false;
  93. }
  94. }
  95. //---------------------------------------------------------------------------
  96. // duplicated from core\configuration.cpp
  97. #define LASTELEM(ELEM) \
  98. ELEM.SubString(ELEM.LastDelimiter(".>")+1, ELEM.Length() - ELEM.LastDelimiter(".>"))
  99. #define BLOCK(KEY, CANCREATE, BLOCK) \
  100. if (Storage->OpenSubKey(KEY, CANCREATE, true)) try { BLOCK } __finally { Storage->CloseSubKey(); }
  101. #define REGCONFIG(CANCREATE) \
  102. BLOCK("Interface", CANCREATE, \
  103. KEY(Integer, Interface); \
  104. KEY(Bool, ShowAdvancedLoginOptions); \
  105. KEY(Bool, ConfirmExitOnCompletion); \
  106. ) \
  107. BLOCK("Logging", CANCREATE, \
  108. KEY(Integer, LogView); \
  109. ) \
  110. BLOCK("Interface\\SynchronizeChecklist", CANCREATE, \
  111. KEY(String, SynchronizeChecklist.WindowParams); \
  112. KEY(String, SynchronizeChecklist.ListParams); \
  113. ); \
  114. BLOCK("Interface\\FindFile", CANCREATE, \
  115. KEY(String, FindFile.WindowParams); \
  116. KEY(String, FindFile.ListParams); \
  117. ); \
  118. BLOCK("Interface\\ConsoleWin", CANCREATE, \
  119. KEY(String, ConsoleWin.WindowSize); \
  120. ); \
  121. //---------------------------------------------------------------------------
  122. void __fastcall TCustomWinConfiguration::SaveData(
  123. THierarchicalStorage * Storage, bool All)
  124. {
  125. TGUIConfiguration::SaveData(Storage, All);
  126. // duplicated from core\configuration.cpp
  127. #define KEY(TYPE, VAR) Storage->Write ## TYPE(LASTELEM(AnsiString(#VAR)), VAR)
  128. REGCONFIG(true);
  129. #undef KEY
  130. if (FHistory->Count > 0)
  131. {
  132. if (Storage->OpenSubKey("History", true))
  133. {
  134. try
  135. {
  136. THistoryStrings * HistoryStrings;
  137. for (int Index = 0; Index < FHistory->Count; Index++)
  138. {
  139. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  140. assert(HistoryStrings != NULL);
  141. if (All || HistoryStrings->Modified)
  142. {
  143. if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  144. {
  145. try
  146. {
  147. Storage->WriteValues(HistoryStrings);
  148. }
  149. __finally
  150. {
  151. Storage->CloseSubKey();
  152. }
  153. }
  154. }
  155. }
  156. }
  157. __finally
  158. {
  159. Storage->CloseSubKey();
  160. }
  161. }
  162. if (Storage->OpenSubKey("HistoryParams", true))
  163. {
  164. try
  165. {
  166. THistoryStrings * HistoryStrings;
  167. for (int Index = 0; Index < FHistory->Count; Index++)
  168. {
  169. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  170. assert(HistoryStrings != NULL);
  171. if (All || HistoryStrings->Modified)
  172. {
  173. bool HasData = false;
  174. for (int VIndex = 0; !HasData && (VIndex < HistoryStrings->Count); VIndex++)
  175. {
  176. HasData = (HistoryStrings->Objects[VIndex] != NULL);
  177. }
  178. if (!HasData)
  179. {
  180. Storage->RecursiveDeleteSubKey(FHistory->Strings[Index]);
  181. }
  182. else if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  183. {
  184. try
  185. {
  186. Storage->ClearValues();
  187. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  188. {
  189. void * Data = HistoryStrings->Objects[VIndex];
  190. Storage->WriteBinaryData(IntToStr(VIndex), &Data, sizeof(Data));
  191. }
  192. }
  193. __finally
  194. {
  195. Storage->CloseSubKey();
  196. }
  197. }
  198. }
  199. }
  200. }
  201. __finally
  202. {
  203. Storage->CloseSubKey();
  204. }
  205. }
  206. }
  207. }
  208. //---------------------------------------------------------------------------
  209. void __fastcall TCustomWinConfiguration::LoadData(
  210. THierarchicalStorage * Storage)
  211. {
  212. TGUIConfiguration::LoadData(Storage);
  213. // duplicated from core\configuration.cpp
  214. #define KEY(TYPE, VAR) VAR = Storage->Read ## TYPE(LASTELEM(AnsiString(#VAR)), VAR)
  215. #pragma warn -eas
  216. REGCONFIG(false);
  217. #pragma warn +eas
  218. #undef KEY
  219. DefaultHistory();
  220. if (Storage->OpenSubKey("History", false))
  221. {
  222. TStrings * Names = NULL;
  223. try
  224. {
  225. Names = new TStringList();
  226. Storage->GetSubKeyNames(Names);
  227. for (int Index = 0; Index < Names->Count; Index++)
  228. {
  229. if (Storage->OpenSubKey(Names->Strings[Index], false))
  230. {
  231. THistoryStrings * HistoryStrings = NULL;
  232. try
  233. {
  234. // remove defaults, if any
  235. int HIndex = FHistory->IndexOf(Names->Strings[Index]);
  236. if (HIndex >= 0)
  237. {
  238. THistoryStrings * DefaultStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[HIndex]);
  239. delete DefaultStrings;
  240. FHistory->Delete(HIndex);
  241. }
  242. HistoryStrings = new THistoryStrings();
  243. Storage->ReadValues(HistoryStrings);
  244. FHistory->AddObject(Names->Strings[Index], HistoryStrings);
  245. HistoryStrings = NULL;
  246. }
  247. __finally
  248. {
  249. Storage->CloseSubKey();
  250. delete HistoryStrings;
  251. }
  252. }
  253. }
  254. }
  255. __finally
  256. {
  257. Storage->CloseSubKey();
  258. delete Names;
  259. }
  260. }
  261. if (Storage->OpenSubKey("HistoryParams", false))
  262. {
  263. try
  264. {
  265. THistoryStrings * HistoryStrings;
  266. for (int Index = 0; Index < FHistory->Count; Index++)
  267. {
  268. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  269. if (Storage->OpenSubKey(FHistory->Strings[Index], false))
  270. {
  271. try
  272. {
  273. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  274. {
  275. void * Data;
  276. if (Storage->ReadBinaryData(IntToStr(VIndex), &Data, sizeof(Data)) ==
  277. sizeof(Data))
  278. {
  279. HistoryStrings->Objects[VIndex] = reinterpret_cast<TObject *>(Data);
  280. }
  281. }
  282. }
  283. __finally
  284. {
  285. Storage->CloseSubKey();
  286. }
  287. }
  288. }
  289. }
  290. __finally
  291. {
  292. Storage->CloseSubKey();
  293. }
  294. }
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TCustomWinConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  298. {
  299. TGUIConfiguration::LoadAdmin(Storage);
  300. FDefaultInterface = TInterface(Storage->ReadInteger("DefaultInterfaceInterface", FDefaultInterface));
  301. FDefaultShowAdvancedLoginOptions = Storage->ReadBool("DefaultInterfaceShowAdvancedLoginOptions", FDefaultShowAdvancedLoginOptions);
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TCustomWinConfiguration::RecryptPasswords()
  305. {
  306. Busy(true);
  307. try
  308. {
  309. StoredSessions->RecryptPasswords();
  310. if (OnMasterPasswordRecrypt != NULL)
  311. {
  312. OnMasterPasswordRecrypt(NULL);
  313. }
  314. }
  315. __finally
  316. {
  317. Busy(false);
  318. }
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TCustomWinConfiguration::SetShowAdvancedLoginOptions(bool value)
  322. {
  323. SET_CONFIG_PROPERTY(ShowAdvancedLoginOptions);
  324. }
  325. //---------------------------------------------------------------------
  326. void __fastcall TCustomWinConfiguration::SetLogView(TLogView value)
  327. {
  328. SET_CONFIG_PROPERTY(LogView);
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TCustomWinConfiguration::SetInterface(TInterface value)
  332. {
  333. SET_CONFIG_PROPERTY(Interface);
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TCustomWinConfiguration::SetHistory(const AnsiString Index,
  337. TStrings * value)
  338. {
  339. int I = FHistory->IndexOf(Index);
  340. bool NonEmpty = (value != NULL) && (value->Count > 0);
  341. THistoryStrings * HistoryStrings = NULL;
  342. if (I >= 0)
  343. {
  344. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[I]);
  345. if (HistoryStrings->Equals(value))
  346. {
  347. HistoryStrings = NULL;
  348. }
  349. }
  350. else if (NonEmpty)
  351. {
  352. HistoryStrings = new THistoryStrings();
  353. FHistory->AddObject(Index, HistoryStrings);
  354. }
  355. if (HistoryStrings != NULL)
  356. {
  357. if (NonEmpty)
  358. {
  359. HistoryStrings->Assign(value);
  360. while (HistoryStrings->Count > MaxHistoryCount)
  361. {
  362. HistoryStrings->Delete(HistoryStrings->Count - 1);
  363. }
  364. }
  365. else
  366. {
  367. HistoryStrings->Clear();
  368. }
  369. HistoryStrings->Modified = true;
  370. }
  371. }
  372. //---------------------------------------------------------------------------
  373. TStrings * __fastcall TCustomWinConfiguration::GetHistory(const AnsiString Index)
  374. {
  375. int I = FHistory->IndexOf(Index);
  376. return I >= 0 ? dynamic_cast<TStrings *>(FHistory->Objects[I]) : FEmptyHistory;
  377. }
  378. //---------------------------------------------------------------------------
  379. void __fastcall TCustomWinConfiguration::SetSynchronizeChecklist(TSynchronizeChecklistConfiguration value)
  380. {
  381. SET_CONFIG_PROPERTY(SynchronizeChecklist);
  382. }
  383. //---------------------------------------------------------------------------
  384. void __fastcall TCustomWinConfiguration::SetFindFile(TFindFileConfiguration value)
  385. {
  386. SET_CONFIG_PROPERTY(FindFile);
  387. }
  388. //---------------------------------------------------------------------------
  389. void __fastcall TCustomWinConfiguration::SetConsoleWin(TConsoleWinConfiguration value)
  390. {
  391. SET_CONFIG_PROPERTY(ConsoleWin);
  392. }
  393. //---------------------------------------------------------------------------
  394. void __fastcall TCustomWinConfiguration::SetConfirmExitOnCompletion(bool value)
  395. {
  396. SET_CONFIG_PROPERTY(ConfirmExitOnCompletion);
  397. }