CustomWinConfiguration.cpp 11 KB

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