Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Settings;
- interface
- type
- ISettings = interface
- ['{DF0CDD58-0CDB-4828-B1A0-5A3C4D8E02B9}']
- // private
- function GetSingleInstance: Boolean;
- procedure SetSingleInstance(const AValue: Boolean);
- // public
- function AppDir: String;
- property SingleInstance: Boolean read GetSingleInstance write SetSingleInstance;
- end;
- function GlobalSettings: ISettings;
- implementation
- uses
- Windows, SysUtils, Registry;
- type
- TSettings = class(TInterfacedObject, ISettings)
- strict private
- FAppDir: String;
- FSingleInstance: Boolean;
- private
- class var FSettings: ISettings;
- strict protected
- FModified: Boolean;
- procedure LoadSettings; virtual;
- procedure SaveSettings; virtual;
- function RegistryPath: String; virtual;
- { ISettings }
- function AppDir: String;
- function GetSingleInstance: Boolean;
- procedure SetSingleInstance(const AValue: Boolean);
- public
- constructor Create; virtual;
- destructor Destroy; override;
- end;
- { TSettings }
- constructor TSettings.Create;
- begin
- inherited Create;
- LoadSettings;
- end;
- destructor TSettings.Destroy;
- begin
- SaveSettings;
- inherited Destroy;
- end;
- // ___
- function TSettings.RegistryPath: String;
- begin
- Result := '\Software\MyCompany\MyProduct';
- end;
- procedure TSettings.LoadSettings;
- var
- Reg: TRegIniFile;
- begin
- FAppDir := ExtractFilePath(ParamStr(0));
- Reg := TRegIniFile.Create(KEY_READ);
- try
- Reg.RootKey := HKEY_CURRENT_USER;
- FSingleInstance := Reg.ReadBool(RegistryPath, 'SingleInstance', False);
- finally
- FreeAndNil(Reg);
- end;
- FModified := False;
- end;
- procedure TSettings.SaveSettings;
- var
- Reg: TRegIniFile;
- begin
- if not FModified then
- Exit;
- FModified := False;
- Reg := TRegIniFile.Create;
- try
- Reg.RootKey := HKEY_CURRENT_USER;
- Reg.WriteBool(RegistryPath, 'SingleInstance', FSingleInstance);
- finally
- FreeAndNil(Reg);
- end;
- end;
- // ___
- function TSettings.AppDir: String;
- begin
- Result := FAppDir;
- end;
- function TSettings.GetSingleInstance: Boolean;
- begin
- Result := FSingleInstance;
- end;
- procedure TSettings.SetSingleInstance(const AValue: Boolean);
- begin
- FSingleInstance := AValue;
- FModified := True;
- end;
- // ___________________________________
- function GlobalSettings: ISettings;
- begin
- if TSettings.FSettings = nil then
- TSettings.FSettings := TSettings.Create;
- Result := TSettings.FSettings;
- end;
- initialization
- finalization
- TSettings.FSettings := nil;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement