Quick.Config.Base.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. { ***************************************************************************
  2. Copyright (c) 2015-2019 Kike Pérez
  3. Unit : Quick.Config.Base
  4. Description : Quick Config Base classes
  5. Author : Kike Pérez
  6. Version : 1.5
  7. Created : 26/01/2017
  8. Modified : 12/02/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Config.Base;
  22. interface
  23. {$i QuickLib.inc}
  24. uses
  25. Classes,
  26. SysUtils,
  27. Rtti,
  28. {$IFDEF FPC}
  29. fpjson,
  30. jsonparser,
  31. fpjsonrtti,
  32. {$ELSE}
  33. DBXJSON,
  34. System.JSON,
  35. Rest.Json.Types,
  36. Rest.Json,
  37. {$ENDIF}
  38. Quick.Json.Serializer;
  39. type
  40. TAppConfig = class;
  41. IAppConfigProvider = interface
  42. ['{D55B1EBF-47F6-478B-8F70-9444575CB825}']
  43. procedure Load(cConfig : TAppConfig);
  44. procedure Save(cConfig : TAppConfig);
  45. end;
  46. TSerializeProperty = (spPublic, spPublished);
  47. TAppConfigProvider = class(TInterfacedObject,IAppConfigProvider)
  48. private
  49. fCreateIfNotExists : Boolean;
  50. fSerializeLevel : TSerializeProperty;
  51. fUseEnumNames : Boolean;
  52. protected
  53. procedure Load(cConfig : TAppConfig); virtual; abstract;
  54. procedure Save(cConfig : TAppConfig); virtual; abstract;
  55. public
  56. constructor Create; virtual;
  57. property CreateIfNotExists : Boolean read fCreateIfNotExists write fCreateIfNotExists;
  58. property SerializeLevel : TSerializeProperty read fSerializeLevel write fSerializeLevel;
  59. property UseEnumNames : Boolean read fUseEnumNames write fUseEnumNames;
  60. end;
  61. TApplyConfigEvent = procedure of object;
  62. {$IFDEF DELPHIXE2_UP}[JsonSerialize(TJsonMemberSerialization.&Public)]{$ENDIF}
  63. TAppConfig = class
  64. private
  65. {$IFDEF FPC}
  66. fOnApplyConfig : TApplyConfigEvent;
  67. fJsonIndent: Boolean;
  68. fLastSaved : TDateTime;
  69. {$ELSE}
  70. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  71. fOnApplyConfig : TApplyConfigEvent;
  72. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  73. fJsonIndent: Boolean;
  74. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  75. fLastSaved : TDateTime;
  76. {$ENDIF}
  77. protected
  78. fProvider : TAppConfigProvider;
  79. public
  80. constructor Create(aConfigProvider : TAppConfigProvider); virtual;
  81. destructor Destroy; override;
  82. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  83. property OnApplyConfig : TApplyConfigEvent read fOnApplyConfig write fOnApplyConfig;
  84. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  85. property JsonIndent : Boolean read fJsonIndent write fJsonIndent;
  86. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  87. property LastSaved : TDateTime read fLastSaved write fLastSaved;
  88. procedure Apply;
  89. //override this method to provide your class initialization
  90. procedure Init; virtual;
  91. procedure DefaultValues; virtual;
  92. procedure Load; virtual;
  93. procedure Save; virtual;
  94. function ToJSON : string;
  95. procedure FromJSON(const json : string);
  96. end;
  97. EAppConfig = class(Exception);
  98. implementation
  99. { TAppConfigProviderBase }
  100. constructor TAppConfigProvider.Create;
  101. begin
  102. fCreateIfNotExists := True;
  103. fSerializeLevel := spPublished;
  104. fUseEnumNames := True;
  105. end;
  106. { TAppConfig }
  107. constructor TAppConfig.Create(aConfigProvider : TAppConfigProvider);
  108. begin
  109. fProvider := aConfigProvider;
  110. fJsonIndent := True;
  111. fLastSaved := 0;
  112. Init;
  113. end;
  114. procedure TAppConfig.Apply;
  115. begin
  116. if Assigned(fOnApplyConfig) then fOnApplyConfig;
  117. end;
  118. procedure TAppConfig.DefaultValues;
  119. begin
  120. //inherit to set default values if no config exists before
  121. end;
  122. destructor TAppConfig.Destroy;
  123. begin
  124. if Assigned(fProvider) then fProvider.Free;
  125. inherited;
  126. end;
  127. function TAppConfig.ToJSON : string;
  128. var
  129. Serializer : TJsonSerializer;
  130. begin
  131. Result := '';
  132. try
  133. serializer := TJsonSerializer.Create(slPublishedProperty,fProvider.UseEnumNames);
  134. try
  135. Result := serializer.ObjectToJSON(Self,fJsonIndent);
  136. finally
  137. serializer.Free;
  138. end;
  139. except
  140. on e : Exception do raise Exception.Create(e.Message);
  141. end;
  142. end;
  143. procedure TAppConfig.FromJSON(const json : string);
  144. var
  145. Serializer : TJsonSerializer;
  146. begin
  147. try
  148. serializer := TJsonSerializer.Create(slPublishedProperty,fProvider.UseEnumNames);
  149. try
  150. serializer.JsonToObject(Self,json);
  151. finally
  152. serializer.Free;
  153. end;
  154. except
  155. on e : Exception do raise Exception.Create(e.Message);
  156. end;
  157. end;
  158. procedure TAppConfig.Init;
  159. begin
  160. //override to create private classes
  161. end;
  162. procedure TAppConfig.Load;
  163. begin
  164. if not Assigned(fProvider) then raise EAppConfig.Create('No provider assigned!');
  165. fProvider.Load(Self);
  166. end;
  167. procedure TAppConfig.Save;
  168. begin
  169. if not Assigned(fProvider) then raise EAppConfig.Create('No provider assigned!');
  170. fProvider.Save(Self);
  171. fLastSaved := Now();
  172. end;
  173. end.