CustomWinConfiguration.cpp 12 KB

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