CustomWinConfiguration.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. ClearHistory();
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TCustomWinConfiguration::ModifyAll()
  58. {
  59. TGUIConfiguration::ModifyAll();
  60. THistoryStrings * HistoryStrings;
  61. for (int Index = 0; Index < FHistory->Count; Index++)
  62. {
  63. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  64. assert(HistoryStrings != NULL);
  65. HistoryStrings->Modified = false;
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. // duplicated from core\configuration.cpp
  70. #define LASTELEM(ELEM) \
  71. ELEM.SubString(ELEM.LastDelimiter(".>")+1, ELEM.Length() - ELEM.LastDelimiter(".>"))
  72. #define BLOCK(KEY, CANCREATE, BLOCK) \
  73. if (Storage->OpenSubKey(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKey(); }
  74. #define REGCONFIG(CANCREATE) \
  75. BLOCK("Interface", CANCREATE, \
  76. KEY(Integer, Interface); \
  77. KEY(Bool, ShowAdvancedLoginOptions); \
  78. ) \
  79. BLOCK("Logging", CANCREATE, \
  80. KEY(Integer, LogView); \
  81. );
  82. //---------------------------------------------------------------------------
  83. void __fastcall TCustomWinConfiguration::SaveSpecial(
  84. THierarchicalStorage * Storage)
  85. {
  86. TGUIConfiguration::SaveSpecial(Storage);
  87. // duplicated from core\configuration.cpp
  88. #define KEY(TYPE, VAR) Storage->Write ## TYPE(LASTELEM(AnsiString(#VAR)), VAR)
  89. REGCONFIG(true);
  90. #undef KEY
  91. if ((FHistory->Count > 0) && Storage->OpenSubKey("History", true))
  92. {
  93. try
  94. {
  95. THistoryStrings * HistoryStrings;
  96. for (int Index = 0; Index < FHistory->Count; Index++)
  97. {
  98. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  99. assert(HistoryStrings != NULL);
  100. if (HistoryStrings->Modified)
  101. {
  102. if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  103. {
  104. try
  105. {
  106. Storage->WriteValues(HistoryStrings);
  107. HistoryStrings->Modified = false;
  108. }
  109. __finally
  110. {
  111. Storage->CloseSubKey();
  112. }
  113. }
  114. }
  115. }
  116. }
  117. __finally
  118. {
  119. Storage->CloseSubKey();
  120. }
  121. }
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TCustomWinConfiguration::LoadSpecial(
  125. THierarchicalStorage * Storage)
  126. {
  127. TGUIConfiguration::LoadSpecial(Storage);
  128. // duplicated from core\configuration.cpp
  129. #define KEY(TYPE, VAR) VAR = Storage->Read ## TYPE(LASTELEM(AnsiString(#VAR)), VAR)
  130. #pragma warn -eas
  131. REGCONFIG(false);
  132. #pragma warn +eas
  133. #undef KEY
  134. ClearHistory();
  135. TStrings * Names = NULL;
  136. if (Storage->OpenSubKey("History", false))
  137. {
  138. try
  139. {
  140. Names = new TStringList();
  141. Storage->GetSubKeyNames(Names);
  142. THistoryStrings * HistoryStrings;
  143. for (int Index = 0; Index < Names->Count; Index++)
  144. {
  145. HistoryStrings = NULL;
  146. if (Storage->OpenSubKey(Names->Strings[Index], false))
  147. {
  148. try
  149. {
  150. HistoryStrings = new THistoryStrings();
  151. Storage->ReadValues(HistoryStrings);
  152. FHistory->AddObject(Names->Strings[Index], HistoryStrings);
  153. HistoryStrings = NULL;
  154. }
  155. __finally
  156. {
  157. Storage->CloseSubKey();
  158. delete HistoryStrings;
  159. }
  160. }
  161. }
  162. }
  163. __finally
  164. {
  165. Storage->CloseSubKey();
  166. delete Names;
  167. }
  168. }
  169. }
  170. //---------------------------------------------------------------------------
  171. void __fastcall TCustomWinConfiguration::SetShowAdvancedLoginOptions(bool value)
  172. {
  173. SET_CONFIG_PROPERTY(ShowAdvancedLoginOptions);
  174. }
  175. //---------------------------------------------------------------------
  176. void __fastcall TCustomWinConfiguration::SetLogView(TLogView value)
  177. {
  178. SET_CONFIG_PROPERTY(LogView);
  179. }
  180. //---------------------------------------------------------------------------
  181. void __fastcall TCustomWinConfiguration::SetInterface(TInterface value)
  182. {
  183. SET_CONFIG_PROPERTY(Interface);
  184. }
  185. //---------------------------------------------------------------------------
  186. void __fastcall TCustomWinConfiguration::SetHistory(const AnsiString Index,
  187. TStrings * value)
  188. {
  189. int I = FHistory->IndexOf(Index);
  190. bool NonEmpty = (value != NULL) && (value->Count > 0);
  191. THistoryStrings * HistoryStrings = NULL;
  192. if (I >= 0)
  193. {
  194. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[I]);
  195. if (HistoryStrings->Equals(value))
  196. {
  197. HistoryStrings = NULL;
  198. }
  199. }
  200. else if (NonEmpty)
  201. {
  202. HistoryStrings = new THistoryStrings();
  203. FHistory->AddObject(Index, HistoryStrings);
  204. }
  205. if (HistoryStrings != NULL)
  206. {
  207. if (NonEmpty)
  208. {
  209. HistoryStrings->Assign(value);
  210. while (HistoryStrings->Count > MaxHistoryCount)
  211. {
  212. HistoryStrings->Delete(HistoryStrings->Count - 1);
  213. }
  214. }
  215. else
  216. {
  217. HistoryStrings->Clear();
  218. }
  219. HistoryStrings->Modified = true;
  220. }
  221. }
  222. //---------------------------------------------------------------------------
  223. TStrings * __fastcall TCustomWinConfiguration::GetHistory(const AnsiString Index)
  224. {
  225. int I = FHistory->IndexOf(Index);
  226. return I >= 0 ? dynamic_cast<TStrings *>(FHistory->Objects[I]) : FEmptyHistory;
  227. }