Quick.Config.Base.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 : 21/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. Quick.Json.Serializer;
  33. {$ELSE}
  34. Quick.Json.Serializer,
  35. DBXJSON,
  36. System.JSON,
  37. Rest.Json.Types,
  38. Rest.Json;
  39. {$ENDIF}
  40. type
  41. TAppConfig = class;
  42. IAppConfigProvider = interface
  43. ['{D55B1EBF-47F6-478B-8F70-9444575CB825}']
  44. procedure Load(cConfig : TAppConfig);
  45. procedure Save(cConfig : TAppConfig);
  46. end;
  47. TSerializeProperty = (spPublic, spPublished);
  48. TAppConfigProvider = class(TInterfacedObject,IAppConfigProvider)
  49. private
  50. fCreateIfNotExists : Boolean;
  51. fSerializeLevel : TSerializeProperty;
  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. end;
  60. TApplyConfigEvent = procedure of object;
  61. {$IFDEF DELPHIXE2_UP}[JsonSerialize(TJsonMemberSerialization.&Public)]{$ENDIF}
  62. TAppConfig = class
  63. private
  64. {$IFDEF FPC}
  65. fOnApplyConfig : TApplyConfigEvent;
  66. fJsonIndent: Boolean;
  67. fLastSaved : TDateTime;
  68. {$ELSE}
  69. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  70. fOnApplyConfig : TApplyConfigEvent;
  71. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  72. fJsonIndent: Boolean;
  73. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  74. fLastSaved : TDateTime;
  75. {$ENDIF}
  76. protected
  77. fProvider : TAppConfigProvider;
  78. public
  79. constructor Create(aConfigProvider : TAppConfigProvider); virtual;
  80. destructor Destroy; override;
  81. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  82. property OnApplyConfig : TApplyConfigEvent read fOnApplyConfig write fOnApplyConfig;
  83. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  84. property JsonIndent : Boolean read fJsonIndent write fJsonIndent;
  85. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  86. property LastSaved : TDateTime read fLastSaved write fLastSaved;
  87. procedure Apply;
  88. procedure DefaultValues; virtual;
  89. procedure Load; virtual;
  90. procedure Save; virtual;
  91. function ToJSON : string;
  92. procedure FromJSON(const json : string);
  93. end;
  94. implementation
  95. { TAppConfigProviderBase }
  96. constructor TAppConfigProvider.Create;
  97. begin
  98. fCreateIfNotExists := True;
  99. fSerializeLevel := spPublished;
  100. end;
  101. { TAppConfig }
  102. constructor TAppConfig.Create(aConfigProvider : TAppConfigProvider);
  103. begin
  104. fProvider := aConfigProvider;
  105. fJsonIndent := True;
  106. fLastSaved := 0;
  107. end;
  108. procedure TAppConfig.Apply;
  109. begin
  110. if Assigned(fOnApplyConfig) then fOnApplyConfig;
  111. end;
  112. procedure TAppConfig.DefaultValues;
  113. begin
  114. //inherit to set default values if no config exists before
  115. end;
  116. destructor TAppConfig.Destroy;
  117. begin
  118. if Assigned(fProvider) then fProvider.Free;
  119. inherited;
  120. end;
  121. function TAppConfig.ToJSON : string;
  122. var
  123. Serializer : TJsonSerializer;
  124. begin
  125. Result := '';
  126. try
  127. serializer := TJsonSerializer.Create(slPublishedProperty);
  128. try
  129. Result := serializer.ObjectToJSON(Self,True);
  130. finally
  131. serializer.Free;
  132. end;
  133. except
  134. on e : Exception do raise Exception.Create(e.Message);
  135. end;
  136. end;
  137. procedure TAppConfig.FromJSON(const json : string);
  138. var
  139. Serializer : TJsonSerializer;
  140. begin
  141. try
  142. serializer := TJsonSerializer.Create(slPublishedProperty);
  143. try
  144. {$IF NOT DEFINED(FPC) AND DEFINED(NEXTGEN)}
  145. serializer.JsonToObject(Self,json);
  146. {$ELSE}
  147. Self := TAppConfig(serializer.JsonToObject(Self,json));
  148. {$ENDIF}
  149. finally
  150. serializer.Free;
  151. end;
  152. except
  153. on e : Exception do raise Exception.Create(e.Message);
  154. end;
  155. end;
  156. procedure TAppConfig.Load;
  157. begin
  158. fProvider.Load(Self);
  159. end;
  160. procedure TAppConfig.Save;
  161. begin
  162. fProvider.Save(Self);
  163. end;
  164. end.