BaseConfig.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. unit BaseConfig;
  2. interface
  3. uses
  4. mybean.core.objects, Classes, SysUtils, TypInfo, superobject,
  5. InterfaceConfig;
  6. type
  7. TConfigType = (ctPublic, ctUser);
  8. TBaseConfig = class(TMyBeanInterfacedObject, IBaseConfig)
  9. private
  10. FData: ISuperObject;
  11. FFileName: string;
  12. FBaseDir: string;
  13. FConfigType: TConfigType;
  14. FLoginName: string;
  15. procedure SetData(const Value: ISuperObject);
  16. procedure SetFileName(const Value: string);
  17. procedure SetConfigType(const Value: TConfigType);
  18. procedure SetLoginName(const Value: string);
  19. function GetBaseDir: string;
  20. function GetUserDir(ALoginName: string): string;
  21. function GetPublicDir: string;
  22. function JsnParseFromFile(AFileName: string): ISuperObject;
  23. protected
  24. function GetFile: string;
  25. procedure New; virtual;
  26. property Data: ISuperObject read FData write SetData;
  27. property FileName: string read FFileName write SetFileName;
  28. property ConfigType: TConfigType read FConfigType write SetConfigType;
  29. property LoginName: string read FLoginName write SetLoginName;
  30. public
  31. constructor Create(); override;
  32. function Load(ALoginName: string = ''): Boolean;
  33. procedure Save;
  34. procedure RestoreDefault;
  35. procedure UndoChange;
  36. function ToJsonStr: string;
  37. end;
  38. implementation
  39. uses
  40. RealICQUtility;
  41. { TBaseConfig }
  42. constructor TBaseConfig.Create;
  43. begin
  44. inherited;
  45. Load;
  46. end;
  47. function TBaseConfig.GetBaseDir: string;
  48. var
  49. strs: TStrings;
  50. ADir: string;
  51. i: Integer;
  52. begin
  53. Result := ExtractFilePath(ParamStr(0));
  54. // if FBaseDir <> '' then
  55. // Result := FBaseDir;
  56. //
  57. // ADir := GetSpecialFolderLocation(CSIDL_APPDATA);
  58. // if (ADir = '') or (not DirectoryExists(ADir)) then
  59. // begin
  60. // raise Exception.CreateFmt('%s', ['找不到系统的AppData目录,不能正常创建或读取程序的配置文件,程序初始化失败!']);
  61. // Exit;
  62. // end;
  63. // ADir := MakeDir(ADir);
  64. //
  65. // OnlineConfig.LoadFromFile('Online.ini');
  66. // strs := TRealICQUtility.SplitString(OnlineConfig.MainHomePath, '.');
  67. // try
  68. // for I := 0 to strs.Count - 1 do
  69. // begin
  70. // if SameText(strs[i], 'Produces') or (Trim(strs[i]) = '') then
  71. // Continue;
  72. // ADir := ADir + '\' + strs[i];
  73. // end;
  74. // finally
  75. // strs.Free;
  76. // end;
  77. //
  78. // if ForceDirectories(ADir) then
  79. // begin
  80. // Result := ADir;
  81. // FBaseDir := ADir;
  82. // end;
  83. end;
  84. function TBaseConfig.GetFile: string;
  85. var
  86. ADir: string;
  87. begin
  88. Result := '';
  89. case FConfigType of
  90. ctPublic: ADir := GetPublicDir;
  91. ctUser:
  92. begin
  93. if FLoginName = '' then
  94. Exit;
  95. ADir := GetUserDir(FLoginName);
  96. end;
  97. end;
  98. if ADir <> '' then
  99. Result := ADir + FFileName;
  100. end;
  101. function TBaseConfig.GetPublicDir: string;
  102. begin
  103. Result := GetBaseDir + 'Config\public\';
  104. end;
  105. function TBaseConfig.GetUserDir(ALoginName: string): string;
  106. begin
  107. Result := GetBaseDir + 'Config\user\' + ALoginName + '\';
  108. ForceDirectories(Result);
  109. end;
  110. function TBaseConfig.JsnParseFromFile(AFileName: string): ISuperObject;
  111. var
  112. lvStream: TMemoryStream;
  113. lvStr: AnsiString;
  114. begin
  115. Result := nil;
  116. if FileExists(AFileName) then
  117. begin
  118. lvStream := TMemoryStream.Create;
  119. try
  120. lvStream.LoadFromFile(AFileName);
  121. lvStream.Position := 0;
  122. SetLength(lvStr, lvStream.Size);
  123. lvStream.ReadBuffer(lvStr[1], lvStream.Size);
  124. Result := SO(UTF8ToAnsi(lvStr));
  125. finally
  126. lvStream.Free;
  127. end;
  128. end;
  129. if Result <> nil then
  130. if not (Result.DataType in [stArray, stObject]) then
  131. Result := nil;
  132. end;
  133. function TBaseConfig.Load(ALoginName: string = ''): Boolean;
  134. var
  135. AFile: string;
  136. begin
  137. if (ALoginName = '') and (FConfigType = ctUser) then
  138. Exit;
  139. // if ALoginName = '' then
  140. // FConfigType := ctPublic
  141. // else
  142. // FConfigType := ctUser;
  143. FData := TRealICQUtility.JsnParseFromFile(GetFile);
  144. if (FData = nil) then
  145. begin
  146. New;
  147. Save;
  148. end;
  149. end;
  150. procedure TBaseConfig.New;
  151. begin
  152. FData := SO();
  153. end;
  154. procedure TBaseConfig.RestoreDefault;
  155. begin
  156. New;
  157. Save;
  158. end;
  159. procedure TBaseConfig.Save;
  160. var
  161. AFile: string;
  162. begin
  163. AFile := GetFile;
  164. if AFile = '' then
  165. Exit;
  166. TRealICQUtility.JsnSaveToFile(FData, GetFile);
  167. end;
  168. procedure TBaseConfig.SetConfigType(const Value: TConfigType);
  169. begin
  170. FConfigType := Value;
  171. end;
  172. procedure TBaseConfig.SetData(const Value: ISuperObject);
  173. begin
  174. FData := Value;
  175. end;
  176. procedure TBaseConfig.SetFileName(const Value: string);
  177. begin
  178. FFileName := Value;
  179. end;
  180. procedure TBaseConfig.SetLoginName(const Value: string);
  181. begin
  182. FLoginName := Value;
  183. end;
  184. function TBaseConfig.ToJsonStr: string;
  185. begin
  186. if FData = nil then
  187. begin
  188. Result := '{}';
  189. Exit;
  190. end;
  191. Result := FData.AsString;
  192. end;
  193. procedure TBaseConfig.UndoChange;
  194. begin
  195. Load;
  196. end;
  197. end.