2004-06-08 17:02:57 +02:00
|
|
|
{
|
|
|
|
:: X2UtSettingsRegistry extends X2UtSettings with registry reading/writing.
|
|
|
|
::
|
|
|
|
:: Subversion repository available at:
|
|
|
|
:: $URL$
|
|
|
|
::
|
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtSettingsRegistry;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
Classes,
|
|
|
|
Registry,
|
|
|
|
Windows,
|
|
|
|
X2UtSettings;
|
|
|
|
|
|
|
|
type
|
|
|
|
{
|
|
|
|
:$ Registry-based settings implementation
|
|
|
|
|
|
|
|
:: It is highly recommended to create instances using
|
|
|
|
:: TX2RegistrySettingsFactory instead of directly.
|
|
|
|
}
|
|
|
|
TX2RegistrySettings = class(TX2Settings)
|
|
|
|
private
|
|
|
|
FData: TRegistry;
|
|
|
|
FKey: String;
|
|
|
|
FOpen: Boolean;
|
|
|
|
FReadOnly: Boolean;
|
|
|
|
|
|
|
|
function OpenRead(): Boolean;
|
|
|
|
function OpenWrite(): Boolean;
|
2004-07-22 16:52:09 +02:00
|
|
|
protected
|
|
|
|
function InternalReadBool(const AName: String; out AValue: Boolean): Boolean; override;
|
|
|
|
function InternalReadFloat(const AName: String; out AValue: Double): Boolean; override;
|
|
|
|
function InternalReadInteger(const AName: String; out AValue: Integer): Boolean; override;
|
|
|
|
function InternalReadString(const AName: String; out AValue: String): Boolean; override;
|
2004-06-08 17:02:57 +02:00
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure InternalWriteBool(const AName: String; AValue: Boolean); override;
|
|
|
|
procedure InternalWriteFloat(const AName: String; AValue: Double); override;
|
|
|
|
procedure InternalWriteInteger(const AName: String; AValue: Integer); override;
|
|
|
|
procedure InternalWriteString(const AName, AValue: String); override;
|
|
|
|
public
|
2004-06-08 20:45:47 +02:00
|
|
|
function ValueExists(const AName: String): Boolean; override;
|
|
|
|
|
2004-06-08 17:02:57 +02:00
|
|
|
procedure GetSectionNames(const ADest: TStrings); override;
|
|
|
|
procedure GetValueNames(const ADest: TStrings); override;
|
|
|
|
|
|
|
|
procedure DeleteSection(); override;
|
|
|
|
procedure DeleteValue(const AName: String); override;
|
2007-08-10 14:48:00 +02:00
|
|
|
// procedure RenameSection(const ANewName: String)
|
2004-06-08 17:02:57 +02:00
|
|
|
public
|
2004-07-22 16:52:09 +02:00
|
|
|
constructor CreateInit(const AFactory: TX2SettingsFactory;
|
2004-09-05 16:04:05 +02:00
|
|
|
const AKey, ASection: String;
|
2004-07-22 16:52:09 +02:00
|
|
|
const ARoot: Cardinal);
|
2004-06-08 17:02:57 +02:00
|
|
|
destructor Destroy(); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{
|
|
|
|
:$ Factory for Registry-based settings
|
|
|
|
|
|
|
|
:: Before use, assign Root and Key to valid values.
|
|
|
|
}
|
|
|
|
TX2RegistrySettingsFactory = class(TX2SettingsFactory)
|
|
|
|
private
|
|
|
|
FKey: String;
|
|
|
|
FRoot: HKEY;
|
|
|
|
protected
|
|
|
|
function GetSection(const ASection: String): TX2Settings; override;
|
|
|
|
public
|
|
|
|
//:$ Specifies the base registry key
|
|
|
|
property Key: String read FKey write FKey;
|
|
|
|
|
|
|
|
//:$ Specifies the root key
|
|
|
|
property Root: HKEY read FRoot write FRoot;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
SysUtils;
|
|
|
|
|
|
|
|
{============= TX2RegistrySettingsFactory
|
|
|
|
Section
|
|
|
|
========================================}
|
|
|
|
function TX2RegistrySettingsFactory.GetSection;
|
|
|
|
begin
|
2004-09-05 16:04:05 +02:00
|
|
|
Result := TX2RegistrySettings.CreateInit(Self, FKey, ASection, FRoot);
|
2004-06-08 17:02:57 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{==================== TX2RegistrySettings
|
|
|
|
Initialization
|
|
|
|
========================================}
|
2004-07-22 16:52:09 +02:00
|
|
|
constructor TX2RegistrySettings.CreateInit;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2004-07-22 16:52:09 +02:00
|
|
|
inherited Create(AFactory, ASection);
|
2004-06-08 17:02:57 +02:00
|
|
|
|
|
|
|
FData := TRegistry.Create();
|
|
|
|
FData.RootKey := ARoot;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
2004-09-05 16:04:05 +02:00
|
|
|
FKey := IncludeTrailingPathDelimiter(AKey) +
|
2004-07-22 16:52:09 +02:00
|
|
|
StringReplace(ASection, '.', '\', [rfReplaceAll]);
|
2004-06-08 17:02:57 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TX2RegistrySettings.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FData);
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2RegistrySettings.OpenRead;
|
|
|
|
begin
|
|
|
|
if not FOpen then begin
|
|
|
|
FReadOnly := True;
|
|
|
|
FOpen := FData.OpenKey(FKey, False);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := FOpen;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2RegistrySettings.OpenWrite;
|
|
|
|
begin
|
|
|
|
if (FOpen) and (FReadOnly) then begin
|
|
|
|
FData.CloseKey();
|
|
|
|
FOpen := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if not FOpen then begin
|
|
|
|
FReadOnly := False;
|
|
|
|
FOpen := FData.OpenKey(FKey, True);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := FOpen;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{==================== TX2RegistrySettings
|
|
|
|
Read
|
|
|
|
========================================}
|
2004-07-22 16:52:09 +02:00
|
|
|
function TX2RegistrySettings.InternalReadBool;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := inherited InternalReadBool(AName, AValue);
|
|
|
|
if Result then
|
|
|
|
exit;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
if OpenRead() then
|
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
Result := ValueExists(AName);
|
|
|
|
|
|
|
|
if Result then
|
|
|
|
AValue := FData.ReadBool(AName);
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
2004-06-08 17:02:57 +02:00
|
|
|
end;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
function TX2RegistrySettings.InternalReadFloat;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := inherited InternalReadFloat(AName, AValue);
|
|
|
|
if Result then
|
|
|
|
exit;
|
2004-06-08 20:45:47 +02:00
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
if OpenRead() then
|
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
Result := ValueExists(AName);
|
|
|
|
|
|
|
|
if Result then
|
|
|
|
AValue := FData.ReadFloat(AName);
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
2004-06-08 17:02:57 +02:00
|
|
|
end;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
function TX2RegistrySettings.InternalReadInteger;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := inherited InternalReadInteger(AName, AValue);
|
|
|
|
if Result then
|
|
|
|
exit;
|
2004-06-08 20:45:47 +02:00
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
if OpenRead() then
|
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
Result := ValueExists(AName);
|
|
|
|
|
|
|
|
if Result then
|
|
|
|
AValue := FData.ReadInteger(AName);
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
2004-06-08 17:02:57 +02:00
|
|
|
end;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
function TX2RegistrySettings.InternalReadString;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := inherited InternalReadString(AName, AValue);
|
|
|
|
if Result then
|
|
|
|
exit;
|
2004-06-08 20:45:47 +02:00
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
if OpenRead() then
|
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
Result := ValueExists(AName);
|
|
|
|
|
|
|
|
if Result then
|
|
|
|
AValue := FData.ReadString(AName);
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
2004-06-08 17:02:57 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{==================== TX2RegistrySettings
|
|
|
|
Write
|
|
|
|
========================================}
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure TX2RegistrySettings.InternalWriteBool;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
inherited;
|
2004-06-08 17:02:57 +02:00
|
|
|
if OpenWrite() then
|
|
|
|
FData.WriteBool(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure TX2RegistrySettings.InternalWriteFloat;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
inherited;
|
2004-06-08 17:02:57 +02:00
|
|
|
if OpenWrite() then
|
|
|
|
FData.WriteFloat(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure TX2RegistrySettings.InternalWriteInteger;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
inherited;
|
2004-06-08 17:02:57 +02:00
|
|
|
if OpenWrite() then
|
|
|
|
FData.WriteInteger(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure TX2RegistrySettings.InternalWriteString;
|
2004-06-08 17:02:57 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
inherited;
|
2004-06-08 17:02:57 +02:00
|
|
|
if OpenWrite() then
|
|
|
|
FData.WriteString(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{==================== TX2RegistrySettings
|
|
|
|
Enumeration
|
|
|
|
========================================}
|
|
|
|
procedure TX2RegistrySettings.GetSectionNames;
|
|
|
|
begin
|
|
|
|
if OpenRead() then
|
|
|
|
FData.GetKeyNames(ADest);
|
|
|
|
end;
|
|
|
|
|
2004-06-08 20:45:47 +02:00
|
|
|
|
2004-06-08 17:02:57 +02:00
|
|
|
procedure TX2RegistrySettings.GetValueNames;
|
|
|
|
begin
|
|
|
|
if OpenRead() then
|
|
|
|
FData.GetValueNames(ADest);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{==================== TX2RegistrySettings
|
|
|
|
Delete
|
|
|
|
========================================}
|
|
|
|
procedure TX2RegistrySettings.DeleteSection;
|
|
|
|
begin
|
|
|
|
// On Delphi 6 at least DeleteKey recursively calls itself for subkeys,
|
|
|
|
// eliminating issues with WinNT based systems. Might need to verify
|
|
|
|
// for Delphi 5 or lower if it's ever used.
|
|
|
|
FData.CloseKey();
|
|
|
|
FData.DeleteKey(FKey);
|
|
|
|
FOpen := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2RegistrySettings.DeleteValue;
|
|
|
|
begin
|
2005-07-01 10:50:41 +02:00
|
|
|
inherited;
|
2004-06-08 17:02:57 +02:00
|
|
|
if OpenRead() then
|
|
|
|
if FData.ValueExists(AName) then
|
|
|
|
FData.DeleteValue(AName);
|
|
|
|
end;
|
|
|
|
|
2004-06-08 20:45:47 +02:00
|
|
|
function TX2RegistrySettings.ValueExists;
|
|
|
|
begin
|
2004-06-08 22:34:21 +02:00
|
|
|
Result := False;
|
|
|
|
|
2004-06-08 20:45:47 +02:00
|
|
|
if OpenRead() then
|
|
|
|
Result := FData.ValueExists(AName);
|
|
|
|
end;
|
|
|
|
|
2004-06-08 17:02:57 +02:00
|
|
|
end.
|