CustomWinConfiguration.cpp 10 KB

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