Quick.Config.Base.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 : 25/01/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. protected
  52. procedure Load(cConfig : TAppConfig); virtual; abstract;
  53. procedure Save(cConfig : TAppConfig); virtual; abstract;
  54. public
  55. constructor Create; virtual;
  56. property CreateIfNotExists : Boolean read fCreateIfNotExists write fCreateIfNotExists;
  57. property SerializeLevel : TSerializeProperty read fSerializeLevel write fSerializeLevel;
  58. end;
  59. TApplyConfigEvent = procedure of object;
  60. {$IFDEF DELPHIXE2_UP}[JsonSerialize(TJsonMemberSerialization.&Public)]{$ENDIF}
  61. TAppConfig = class
  62. private
  63. {$IFDEF FPC}
  64. fOnApplyConfig : TApplyConfigEvent;
  65. fJsonIndent: Boolean;
  66. fLastSaved : TDateTime;
  67. {$ELSE}
  68. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  69. fOnApplyConfig : TApplyConfigEvent;
  70. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  71. fJsonIndent: Boolean;
  72. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  73. fLastSaved : TDateTime;
  74. {$ENDIF}
  75. protected
  76. fProvider : TAppConfigProvider;
  77. public
  78. constructor Create(aConfigProvider : TAppConfigProvider); virtual;
  79. destructor Destroy; override;
  80. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  81. property OnApplyConfig : TApplyConfigEvent read fOnApplyConfig write fOnApplyConfig;
  82. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  83. property JsonIndent : Boolean read fJsonIndent write fJsonIndent;
  84. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  85. property LastSaved : TDateTime read fLastSaved write fLastSaved;
  86. procedure Apply;
  87. //override this method to provide your class initialization
  88. procedure Init; virtual;
  89. procedure DefaultValues; virtual;
  90. procedure Load; virtual;
  91. procedure Save; virtual;
  92. function ToJSON : string;
  93. procedure FromJSON(const json : string);
  94. end;
  95. EAppConfig = class(Exception);
  96. implementation
  97. { TAppConfigProviderBase }
  98. constructor TAppConfigProvider.Create;
  99. begin
  100. fCreateIfNotExists := True;
  101. fSerializeLevel := spPublished;
  102. end;
  103. { TAppConfig }
  104. constructor TAppConfig.Create(aConfigProvider : TAppConfigProvider);
  105. begin
  106. fProvider := aConfigProvider;
  107. fJsonIndent := True;
  108. fLastSaved := 0;
  109. Init;
  110. end;
  111. procedure TAppConfig.Apply;
  112. begin
  113. if Assigned(fOnApplyConfig) then fOnApplyConfig;
  114. end;
  115. procedure TAppConfig.DefaultValues;
  116. begin
  117. //inherit to set default values if no config exists before
  118. end;
  119. destructor TAppConfig.Destroy;
  120. begin
  121. if Assigned(fProvider) then fProvider.Free;
  122. inherited;
  123. end;
  124. function TAppConfig.ToJSON : string;
  125. var
  126. Serializer : TJsonSerializer;
  127. begin
  128. Result := '';
  129. try
  130. serializer := TJsonSerializer.Create(slPublishedProperty);
  131. try
  132. Result := serializer.ObjectToJSON(Self,fJsonIndent);
  133. finally
  134. serializer.Free;
  135. end;
  136. except
  137. on e : Exception do raise Exception.Create(e.Message);
  138. end;
  139. end;
  140. procedure TAppConfig.FromJSON(const json : string);
  141. var
  142. Serializer : TJsonSerializer;
  143. begin
  144. try
  145. serializer := TJsonSerializer.Create(slPublishedProperty);
  146. try
  147. serializer.JsonToObject(Self,json);
  148. finally
  149. serializer.Free;
  150. end;
  151. except
  152. on e : Exception do raise Exception.Create(e.Message);
  153. end;
  154. end;
  155. procedure TAppConfig.Init;
  156. begin
  157. //override to create private classes
  158. end;
  159. procedure TAppConfig.Load;
  160. begin
  161. if not Assigned(fProvider) then raise EAppConfig.Create('No provider assigned!');
  162. fProvider.Load(Self);
  163. end;
  164. procedure TAppConfig.Save;
  165. begin
  166. if not Assigned(fProvider) then raise EAppConfig.Create('No provider assigned!');
  167. fProvider.Save(Self);
  168. fLastSaved := Now();
  169. end;
  170. end.