2004-06-08 17:02:57 +02:00
|
|
|
{
|
|
|
|
:: X2UtSettings provides a generic access mechanism for application settings.
|
|
|
|
:: Include one of the extensions (X2UtSettingsINI, X2UtSettingsRegistry) for
|
|
|
|
:: an implementation.
|
|
|
|
::
|
2005-08-26 12:25:39 +02:00
|
|
|
:: X2UtSettings is deprecated, and replaced by X2UtConfig.
|
|
|
|
::
|
2004-06-08 17:02:57 +02:00
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtSettings;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
2004-07-22 16:52:09 +02:00
|
|
|
Classes,
|
|
|
|
SysUtils,
|
2004-08-20 15:03:07 +02:00
|
|
|
Variants,
|
|
|
|
X2UtHashes;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
2004-06-08 17:02:57 +02:00
|
|
|
type
|
2004-07-22 16:52:09 +02:00
|
|
|
//:$ Raised when an unregistered setting is requested without providing a
|
|
|
|
//:$ default value.
|
|
|
|
EX2SettingsUndefined = class(Exception);
|
|
|
|
|
|
|
|
//:$ Raised when trying to read a registered setting as a different type than
|
|
|
|
//:$ it was registered with.
|
|
|
|
EX2SettingsType = class(Exception);
|
|
|
|
|
|
|
|
//:$ Raised when the specified range is invalid.
|
|
|
|
EX2SettingsRange = class(Exception);
|
|
|
|
|
2004-08-20 16:43:13 +02:00
|
|
|
//:$ Raised when the specified define already exists
|
|
|
|
EX2SettingsExists = class(Exception);
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
//:$ Callback method for defines
|
2004-08-20 21:04:22 +02:00
|
|
|
TX2SettingsAction = (saRead, saWrite);
|
|
|
|
TX2SettingsCallback = procedure(const AAction: TX2SettingsAction;
|
|
|
|
const ASection, AName: String;
|
2004-08-20 15:03:07 +02:00
|
|
|
var AValue: Variant) of object;
|
|
|
|
|
|
|
|
{
|
2004-08-20 16:43:13 +02:00
|
|
|
:$ Internal representation of a persistent setting
|
2004-08-20 15:03:07 +02:00
|
|
|
}
|
|
|
|
TX2SettingsDefine = class(TObject)
|
|
|
|
private
|
2005-03-12 17:54:34 +01:00
|
|
|
FCached: Boolean;
|
|
|
|
FCallback: TX2SettingsCallback;
|
|
|
|
FDefaultValue: Variant;
|
|
|
|
FValue: Variant;
|
2004-08-20 16:43:13 +02:00
|
|
|
public
|
|
|
|
constructor Create(const AValue: Variant;
|
|
|
|
const ACallback: TX2SettingsCallback);
|
2004-08-20 15:03:07 +02:00
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure Action(const AAction: TX2SettingsAction;
|
|
|
|
const ASection, AName: String;
|
2005-03-12 17:54:34 +01:00
|
|
|
var AValue: Variant);
|
2004-08-20 21:04:22 +02:00
|
|
|
|
2005-03-12 17:54:34 +01:00
|
|
|
property Cached: Boolean read FCached write FCached;
|
|
|
|
property Callback: TX2SettingsCallback read FCallback;
|
|
|
|
property DefaultValue: Variant read FDefaultValue;
|
|
|
|
property Value: Variant read FValue write FValue;
|
2004-08-20 15:03:07 +02:00
|
|
|
end;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
// Forward declaration
|
|
|
|
TX2SettingsFactory = class;
|
|
|
|
|
2004-06-08 17:02:57 +02:00
|
|
|
{
|
2004-07-22 16:52:09 +02:00
|
|
|
:$ Abstract settings object.
|
2004-06-08 17:02:57 +02:00
|
|
|
|
|
|
|
:: Provides access to the settings regardless of the storage backend.
|
|
|
|
}
|
2004-07-22 16:52:09 +02:00
|
|
|
TX2Settings = class(TObject)
|
|
|
|
private
|
|
|
|
FFactory: TX2SettingsFactory;
|
|
|
|
FSection: String;
|
2005-03-12 17:54:34 +01:00
|
|
|
|
|
|
|
function ReadCache(const AName: String; out AValue: Variant): Boolean;
|
|
|
|
procedure WriteCache(const AName: String; const AValue: Variant); overload;
|
|
|
|
procedure WriteCache(const ADefine: TX2SettingsDefine; const AValue: Variant); overload;
|
2004-07-22 16:52:09 +02:00
|
|
|
protected
|
2005-03-12 17:54:34 +01:00
|
|
|
function InternalReadBool(const AName: String; out AValue: Boolean): Boolean; virtual;
|
|
|
|
function InternalReadFloat(const AName: String; out AValue: Double): Boolean; virtual;
|
|
|
|
function InternalReadInteger(const AName: String; out AValue: Integer): Boolean; virtual;
|
|
|
|
function InternalReadString(const AName: String; out AValue: String): Boolean; virtual;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
2005-03-12 17:54:34 +01:00
|
|
|
procedure InternalWriteBool(const AName: String; AValue: Boolean); virtual;
|
|
|
|
procedure InternalWriteFloat(const AName: String; AValue: Double); virtual;
|
|
|
|
procedure InternalWriteInteger(const AName: String; AValue: Integer); virtual;
|
|
|
|
procedure InternalWriteString(const AName, AValue: String); virtual;
|
2004-08-20 21:04:22 +02:00
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
property Factory: TX2SettingsFactory read FFactory;
|
|
|
|
property Section: String read FSection;
|
2004-06-08 17:02:57 +02:00
|
|
|
public
|
2004-07-22 16:52:09 +02:00
|
|
|
constructor Create(const AFactory: TX2SettingsFactory;
|
|
|
|
const ASection: String); virtual;
|
|
|
|
|
|
|
|
//:$ Reads a boolean value from the settings.
|
|
|
|
//:: If the ADefault parameter is used, that value will be returned if the
|
|
|
|
//:: setting could not be found. Otherwise the setting must have been
|
|
|
|
//:: registered with the factory's Register() method.
|
|
|
|
function ReadBool(const AName: String): Boolean; overload; virtual;
|
|
|
|
function ReadBool(const AName: String; const ADefault: Boolean): Boolean; overload; virtual;
|
|
|
|
|
|
|
|
//:$ Reads a floating point value from the settings.
|
|
|
|
//:: If the ADefault parameter is used, that value will be returned if the
|
|
|
|
//:: setting could not be found. Otherwise the setting must have been
|
|
|
|
//:: registered with the factory's Register() method.
|
|
|
|
function ReadFloat(const AName: String): Double; overload; virtual;
|
|
|
|
function ReadFloat(const AName: String; const ADefault: Double): Double; overload; virtual;
|
2004-06-08 17:02:57 +02:00
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
//:$ Reads an integer value from the settings.
|
|
|
|
//:: If the ADefault parameter is used, that value will be returned if the
|
|
|
|
//:: setting could not be found. Otherwise the setting must have been
|
|
|
|
//:: registered with the factory's Register() method.
|
|
|
|
function ReadInteger(const AName: String): Integer; overload; virtual;
|
|
|
|
function ReadInteger(const AName: String; const ADefault: Integer): Integer; overload; virtual;
|
|
|
|
|
|
|
|
//:$ Reads a string value from the settings.
|
|
|
|
//:: If the ADefault parameter is used, that value will be returned if the
|
|
|
|
//:: setting could not be found. Otherwise the setting must have been
|
|
|
|
//:: registered with the factory's Register() method.
|
|
|
|
function ReadString(const AName: String): String; overload; virtual;
|
|
|
|
function ReadString(const AName, ADefault: String): String; overload; virtual;
|
|
|
|
|
|
|
|
//:$ Writes a boolean value to the settings.
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure WriteBool(const AName: String; AValue: Boolean); virtual;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
//:$ Writes a floating point value to the settings.
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure WriteFloat(const AName: String; AValue: Double); virtual;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
//:$ Writes an integer value to the settings.
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure WriteInteger(const AName: String; AValue: Integer); virtual;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
//:$ Writes a string value to the settings.
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure WriteString(const AName, AValue: String); virtual;
|
2004-06-08 17:02:57 +02:00
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
//:$ Checks if the specified setting exists.
|
2004-06-08 20:45:47 +02:00
|
|
|
function ValueExists(const AName: String): Boolean; virtual; abstract;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
//:$ Retrieves the list of sub-sections for this section.
|
2004-06-08 17:02:57 +02:00
|
|
|
procedure GetSectionNames(const ADest: TStrings); virtual; abstract;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
//:$ Retrieves the list of values for this section.
|
2004-06-08 17:02:57 +02:00
|
|
|
procedure GetValueNames(const ADest: TStrings); virtual; abstract;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
//:$ Deletes this section.
|
2004-06-08 17:02:57 +02:00
|
|
|
procedure DeleteSection(); virtual; abstract;
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
//:$ Deletes the specified value.
|
2005-07-01 10:50:41 +02:00
|
|
|
procedure DeleteValue(const AName: String); virtual;
|
2004-09-26 15:59:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
//:$ Validates the specified value using the defined callback method
|
|
|
|
//:$ if present.
|
|
|
|
function ValidateValue(const AName: String; const AValue: Variant): Variant;
|
2005-12-28 20:15:17 +01:00
|
|
|
end;
|
2004-06-08 17:02:57 +02:00
|
|
|
|
|
|
|
{
|
2004-07-22 16:52:09 +02:00
|
|
|
:$ Settings factory.
|
2004-06-08 17:02:57 +02:00
|
|
|
|
|
|
|
:: Extensions must implement a factory descendant which an application can
|
|
|
|
:: create to provide application-wide access to the same settings.
|
|
|
|
}
|
2004-07-22 16:52:09 +02:00
|
|
|
TX2SettingsFactory = class(TObject)
|
2004-08-20 16:43:13 +02:00
|
|
|
private
|
2005-07-01 10:50:41 +02:00
|
|
|
FDefines: TX2SOHash;
|
2004-06-08 17:02:57 +02:00
|
|
|
protected
|
|
|
|
function GetSection(const ASection: String): TX2Settings; virtual; abstract;
|
2004-08-20 21:04:22 +02:00
|
|
|
function GetDefine(const ASection, AName: String): TX2SettingsDefine; virtual;
|
2004-06-08 17:02:57 +02:00
|
|
|
public
|
2004-08-20 16:43:13 +02:00
|
|
|
constructor Create();
|
|
|
|
destructor Destroy(); override;
|
|
|
|
|
2005-12-28 20:15:17 +01:00
|
|
|
procedure ResetCache();
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
//:$ Loads a section from the settings.
|
2004-06-08 17:02:57 +02:00
|
|
|
//:: Sub-sections are indicated by seperating the sections with a dot ('.')
|
|
|
|
//:: characters, ex: Sub.Section. The underlying extension will translate
|
|
|
|
//:: this into a compatible section.
|
|
|
|
//:! The application is responsible for freeing the returned class.
|
|
|
|
property Sections[const ASection: String]: TX2Settings read GetSection; default;
|
2004-08-20 15:03:07 +02:00
|
|
|
|
|
|
|
//:$ Defines a persistent setting
|
|
|
|
//:: Persistent settings are a way for the application to register it's
|
2004-08-20 21:04:22 +02:00
|
|
|
//:: configuration settings on startup with a default value and a possible
|
|
|
|
//:: callback method to perform centralized checks.
|
2004-08-20 15:03:07 +02:00
|
|
|
procedure Define(const ASection, AName: String; const AValue: Variant;
|
|
|
|
const ACallback: TX2SettingsCallback = nil);
|
2005-12-28 20:15:17 +01:00
|
|
|
end;
|
2004-06-08 17:02:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
implementation
|
2004-07-22 16:52:09 +02:00
|
|
|
resourcestring
|
2004-08-20 16:43:13 +02:00
|
|
|
RSInvalidRange = 'Invalid range!';
|
|
|
|
RSInvalidType = 'Invalid type!';
|
|
|
|
RSUndefined = 'Undefined setting: %s!';
|
2004-10-30 14:28:15 +02:00
|
|
|
RSDefineExists = 'Define "%s" already exists!';
|
2004-07-22 16:52:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
{============================ TX2Settings
|
|
|
|
Initialization
|
|
|
|
========================================}
|
|
|
|
constructor TX2Settings.Create;
|
|
|
|
begin
|
|
|
|
inherited Create();
|
|
|
|
|
|
|
|
FFactory := AFactory;
|
|
|
|
FSection := ASection;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{============================ TX2Settings
|
2004-08-20 21:04:22 +02:00
|
|
|
Reading
|
2004-07-22 16:52:09 +02:00
|
|
|
========================================}
|
2005-03-12 17:54:34 +01:00
|
|
|
function TX2Settings.InternalReadBool(const AName: String;
|
|
|
|
out AValue: Boolean): Boolean;
|
|
|
|
var
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := ReadCache(AName, vValue);
|
|
|
|
if Result then
|
|
|
|
AValue := vValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.InternalReadFloat(const AName: String;
|
|
|
|
out AValue: Double): Boolean;
|
|
|
|
var
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := ReadCache(AName, vValue);
|
|
|
|
if Result then
|
|
|
|
AValue := vValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.InternalReadInteger(const AName: String;
|
|
|
|
out AValue: Integer): Boolean;
|
|
|
|
var
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := ReadCache(AName, vValue);
|
|
|
|
if Result then
|
|
|
|
AValue := vValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.InternalReadString(const AName: String;
|
|
|
|
out AValue: String): Boolean;
|
|
|
|
var
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := ReadCache(AName, vValue);
|
|
|
|
if Result then
|
|
|
|
AValue := vValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
function TX2Settings.ReadBool(const AName: String): Boolean;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 21:04:22 +02:00
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadBool(AName, Result) then
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := pDefine.DefaultValue
|
2004-08-20 21:04:22 +02:00
|
|
|
else
|
2004-09-05 16:09:08 +02:00
|
|
|
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
|
2004-08-20 21:04:22 +02:00
|
|
|
|
2004-09-05 16:04:41 +02:00
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.ReadBool(const AName: String;
|
|
|
|
const ADefault: Boolean): Boolean;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadBool(AName, Result) then
|
2004-07-22 16:52:09 +02:00
|
|
|
Result := ADefault;
|
2004-08-20 21:04:22 +02:00
|
|
|
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.ReadFloat(const AName: String): Double;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 21:04:22 +02:00
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadFloat(AName, Result) then
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := pDefine.DefaultValue
|
2004-08-20 21:04:22 +02:00
|
|
|
else
|
|
|
|
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
|
|
|
|
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.ReadFloat(const AName: String;
|
|
|
|
const ADefault: Double): Double;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadFloat(AName, Result) then
|
2004-07-22 16:52:09 +02:00
|
|
|
Result := ADefault;
|
2004-08-20 21:04:22 +02:00
|
|
|
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.ReadInteger(const AName: String): Integer;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 21:04:22 +02:00
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadInteger(AName, Result) then
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := pDefine.DefaultValue
|
2004-08-20 21:04:22 +02:00
|
|
|
else
|
|
|
|
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
|
|
|
|
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.ReadInteger(const AName: String;
|
|
|
|
const ADefault: Integer): Integer;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadInteger(AName, Result) then
|
2004-07-22 16:52:09 +02:00
|
|
|
Result := ADefault;
|
2004-08-20 21:04:22 +02:00
|
|
|
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.ReadString(const AName: String): String;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 21:04:22 +02:00
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadString(AName, Result) then
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
2005-03-12 17:54:34 +01:00
|
|
|
Result := pDefine.DefaultValue
|
2004-08-20 21:04:22 +02:00
|
|
|
else
|
|
|
|
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
|
|
|
|
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2Settings.ReadString(const AName, ADefault: String): String;
|
2004-08-20 21:04:22 +02:00
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
2004-07-22 16:52:09 +02:00
|
|
|
begin
|
2004-08-20 15:03:07 +02:00
|
|
|
if not InternalReadString(AName, Result) then
|
2004-07-22 16:52:09 +02:00
|
|
|
Result := ADefault;
|
2004-08-20 21:04:22 +02:00
|
|
|
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
vValue := Result;
|
|
|
|
pDefine.Action(saRead, FSection, AName, vValue);
|
|
|
|
Result := vValue;
|
2005-03-12 17:54:34 +01:00
|
|
|
WriteCache(pDefine, Result);
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{============================ TX2Settings
|
|
|
|
Writing
|
|
|
|
========================================}
|
2005-03-12 17:54:34 +01:00
|
|
|
procedure TX2Settings.InternalWriteBool(const AName: String; AValue: Boolean);
|
|
|
|
begin
|
|
|
|
WriteCache(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.InternalWriteFloat(const AName: String; AValue: Double);
|
|
|
|
begin
|
|
|
|
WriteCache(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.InternalWriteInteger(const AName: String;
|
|
|
|
AValue: Integer);
|
|
|
|
begin
|
|
|
|
WriteCache(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.InternalWriteString(const AName, AValue: String);
|
|
|
|
begin
|
|
|
|
WriteCache(AName, AValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
procedure TX2Settings.WriteBool;
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
2004-09-05 16:50:31 +02:00
|
|
|
vValue := AValue;
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
|
|
|
pDefine.Action(saWrite, FSection, AName, vValue);
|
|
|
|
|
|
|
|
InternalWriteBool(AName, vValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.WriteFloat;
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
2004-09-05 16:50:31 +02:00
|
|
|
vValue := AValue;
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
|
|
|
pDefine.Action(saWrite, FSection, AName, vValue);
|
|
|
|
|
|
|
|
InternalWriteFloat(AName, vValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.WriteInteger;
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
2004-09-05 16:50:31 +02:00
|
|
|
vValue := AValue;
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
|
|
|
pDefine.Action(saWrite, FSection, AName, vValue);
|
|
|
|
|
|
|
|
InternalWriteInteger(AName, vValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.WriteString;
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
vValue: Variant;
|
|
|
|
|
|
|
|
begin
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
2004-09-05 16:50:31 +02:00
|
|
|
vValue := AValue;
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
if Assigned(pDefine) then
|
|
|
|
pDefine.Action(saWrite, FSection, AName, vValue);
|
|
|
|
|
2004-09-25 15:29:06 +02:00
|
|
|
InternalWriteString(AName, vValue);
|
2004-07-22 16:52:09 +02:00
|
|
|
end;
|
2004-06-08 17:02:57 +02:00
|
|
|
|
2005-07-01 10:50:41 +02:00
|
|
|
procedure TX2Settings.DeleteValue(const AName: String);
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
|
|
|
|
begin
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
pDefine.Cached := False;
|
|
|
|
pDefine.Value := Unassigned;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
|
2004-09-26 15:59:43 +02:00
|
|
|
function TX2Settings.ValidateValue;
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := AValue;
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
pDefine.Action(saRead, FSection, AName, Result);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2005-03-12 17:54:34 +01:00
|
|
|
|
|
|
|
function TX2Settings.ReadCache(const AName: String;
|
|
|
|
out AValue: Variant): Boolean;
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
|
|
|
|
begin
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
Result := Assigned(pDefine) and pDefine.Cached;
|
|
|
|
if Result then
|
|
|
|
AValue := pDefine.Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.WriteCache(const AName: String;
|
|
|
|
const AValue: Variant);
|
|
|
|
var
|
|
|
|
pDefine: TX2SettingsDefine;
|
|
|
|
|
|
|
|
begin
|
|
|
|
pDefine := FFactory.GetDefine(FSection, AName);
|
|
|
|
if Assigned(pDefine) then
|
|
|
|
begin
|
|
|
|
pDefine.Cached := True;
|
|
|
|
pDefine.Value := AValue;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2Settings.WriteCache(const ADefine: TX2SettingsDefine;
|
|
|
|
const AValue: Variant);
|
|
|
|
begin
|
|
|
|
ADefine.Cached := True;
|
|
|
|
ADefine.Value := AValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
{===================== TX2SettingsFactory
|
|
|
|
Defines
|
|
|
|
========================================}
|
2004-08-20 16:43:13 +02:00
|
|
|
constructor TX2SettingsFactory.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
2005-07-01 10:50:41 +02:00
|
|
|
FDefines := TX2SOHash.Create(True);
|
2004-08-20 16:43:13 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TX2SettingsFactory.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FDefines);
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
procedure TX2SettingsFactory.Define;
|
2004-08-20 16:43:13 +02:00
|
|
|
function CheckVarType(const AValue: Variant): TVarType;
|
|
|
|
begin
|
|
|
|
case VarType(AValue) of
|
2004-08-20 21:04:22 +02:00
|
|
|
varBoolean: Result := varBoolean;
|
2004-08-20 16:43:13 +02:00
|
|
|
varByte,
|
|
|
|
varSmallint,
|
|
|
|
varInteger,
|
|
|
|
varWord,
|
|
|
|
varLongWord: Result := varInteger;
|
|
|
|
varSingle,
|
|
|
|
varDouble,
|
|
|
|
varDate: Result := varDouble;
|
|
|
|
varOleStr,
|
|
|
|
varStrArg,
|
|
|
|
varString: Result := varString;
|
|
|
|
else
|
|
|
|
raise EX2SettingsType.Create(RSInvalidType);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
sHash: String;
|
|
|
|
vtValue: TVarType;
|
|
|
|
|
2004-08-20 15:03:07 +02:00
|
|
|
begin
|
2004-08-20 16:43:13 +02:00
|
|
|
sHash := ASection + #0 + AName;
|
|
|
|
if FDefines.Exists(sHash) then
|
2004-10-30 14:28:15 +02:00
|
|
|
raise EX2SettingsExists.CreateFmt(RSDefineExists, [ASection + '.' + AName]);
|
2004-08-20 16:43:13 +02:00
|
|
|
|
|
|
|
// Validate type
|
|
|
|
vtValue := CheckVarType(AValue);
|
2004-08-20 21:04:22 +02:00
|
|
|
FDefines[sHash] := TX2SettingsDefine.Create(VarAsType(AValue, vtValue),
|
|
|
|
ACallback);
|
|
|
|
end;
|
2004-08-20 16:43:13 +02:00
|
|
|
|
|
|
|
|
2004-08-20 21:04:22 +02:00
|
|
|
function TX2SettingsFactory.GetDefine;
|
|
|
|
begin
|
|
|
|
Result := TX2SettingsDefine(FDefines[ASection + #0 + AName]);
|
2004-08-20 16:43:13 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{====================== TX2SettingsDefine
|
|
|
|
Initialization
|
|
|
|
========================================}
|
2005-03-12 17:54:34 +01:00
|
|
|
constructor TX2SettingsDefine.Create(const AValue: Variant;
|
|
|
|
const ACallback: TX2SettingsCallback);
|
2004-08-20 16:43:13 +02:00
|
|
|
begin
|
2005-03-12 17:54:34 +01:00
|
|
|
FCached := False;
|
|
|
|
FCallback := ACallback;
|
|
|
|
FDefaultValue := AValue;
|
2004-08-20 21:04:22 +02:00
|
|
|
end;
|
2004-08-20 16:43:13 +02:00
|
|
|
|
2005-03-12 17:54:34 +01:00
|
|
|
procedure TX2SettingsDefine.Action(const AAction: TX2SettingsAction;
|
|
|
|
const ASection, AName: String;
|
|
|
|
var AValue: Variant);
|
2004-08-20 21:04:22 +02:00
|
|
|
begin
|
|
|
|
if Assigned(FCallback) then
|
|
|
|
FCallback(AAction, ASection, AName, AValue);
|
2004-08-20 15:03:07 +02:00
|
|
|
end;
|
|
|
|
|
2005-12-28 20:15:17 +01:00
|
|
|
procedure TX2SettingsFactory.ResetCache();
|
|
|
|
begin
|
|
|
|
FDefines.First();
|
|
|
|
while FDefines.Next() do
|
|
|
|
TX2SettingsDefine(FDefines.CurrentValue).Cached := False;
|
|
|
|
end;
|
|
|
|
|
2004-06-08 17:02:57 +02:00
|
|
|
end.
|