unit uConfig;
{$INCLUDE 'Filters.inc'}

interface

uses
  SysUtils, Classes,
  {$INCLUDE 'DelphiFarUnits.inc'} ,
  uMessages;

type
  TConfig = class(TFarConfig)
  private
    fFilterKeys: TStringList;
    function GetFilterKey(Index: integer): string;
    function GetFilterKeyCount: integer;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Reload;
    procedure DeleteFilter(const Key: string);
    procedure DeleteAllFilters;
    function GetFilterKeys: TStringList;
    property FilterKeyCount: integer read GetFilterKeyCount;
    property FilterKeys[Index: integer]: string read GetFilterKey;
  end;

  TFilterConfig = class(TFarConfig)
  private
    fKey: string;
    fMustExist: boolean;
    function GetDescription: string;
    function GetFileMaskStr: string;
    function GetID: string;
    procedure SetDescription(const Value: string);
    procedure SetFileMaskStr(const Value: string);
    procedure SetID(const Value: string);
    procedure SetKey(const Value: string);
    procedure SetMustExist(const Value: boolean);
    function GetEditBeforeUse: boolean;
    procedure SetEditBeforeUse(Value: boolean);
  protected
    procedure CheckActive;
  public
    constructor Create;
    property Key: string read fKey write SetKey;
    property MustExist: boolean read fMustExist write SetMustExist;
    property ID: string read GetID write SetID;
    property Description: string read GetDescription write SetDescription;
    property FileMaskStr: string read GetFileMaskStr write SetFileMaskStr;
    property EditBeforeUse: boolean read GetEditBeforeUse write SetEditBeforeUse;
  end;

implementation

const
  CONFIG_ROOT = 'Filters';
  FILTERS_ROOT = '\Filters';

{ TConfig }

constructor TConfig.Create;
begin
  inherited Create(CONFIG_ROOT, True);
  fFilterKeys := TStringList.Create;
  Reload;
end;

procedure TConfig.DeleteAllFilters;
begin
  Close;
  try
    if Open('') then
      DeleteKey(FILTERS_ROOT);
  finally
    Reload;
  end;
end;

procedure TConfig.DeleteFilter(const Key: string);
begin
  Close;
  try
    if Open(FILTERS_ROOT) then
      DeleteKey(Key);
  finally
    Reload;
  end;
end;

destructor TConfig.Destroy;
begin
  FreeAndNil(fFilterKeys);
  inherited;
end;

function TConfig.GetFilterKey(Index: integer): string;
begin
  Result := fFilterKeys[Index];
end;

function TConfig.GetFilterKeyCount: integer;
begin
  Result := fFilterKeys.Count;
end;

function TConfig.GetFilterKeys: TStringList;
begin
  Result := TStringList.Create;
  Result.Assign(fFilterKeys);
end;

procedure TConfig.Reload;
begin
  Close;
  fFilterKeys.Clear;
  try
    if Open(FILTERS_ROOT) then
    begin
      ReadKeyNames(fFilterKeys);
      fFilterKeys.Sort;
    end;
  finally
    Close;
  end;
end;

{ TFilterConfig }

procedure TFilterConfig.CheckActive;
begin
  if not IsOpen then
    raise EFarError.Create(GetMsg(MConfigNotActive));
end;

constructor TFilterConfig.Create;
begin
  inherited Create(CONFIG_ROOT, False);
  fKey := '';
  fMustExist := True;
end;

function TFilterConfig.GetDescription: string;
begin
  CheckActive;
  Result := ReadString('Description');
end;

function TFilterConfig.GetEditBeforeUse: boolean;
begin
  CheckActive;
  Result := ReadBoolean('EditBeforeUse');
end;

function TFilterConfig.GetFileMaskStr: string;
begin
  CheckActive;
  Result := ReadString('FileMasks');
end;

function TFilterConfig.GetID: string;
begin
  CheckActive;
  Result := ReadString('FilterID');
end;

procedure TFilterConfig.SetDescription(const Value: string);
begin
  CheckActive;
  WriteString('Description', Value);
end;

procedure TFilterConfig.SetEditBeforeUse(Value: boolean);
begin
  CheckActive;
  WriteBoolean('EditBeforeUse', Value);
end;

procedure TFilterConfig.SetFileMaskStr(const Value: string);
begin
  CheckActive;
  WriteString('FileMasks', Value);
end;

procedure TFilterConfig.SetID(const Value: string);
begin
  CheckActive;
  WriteString('FilterID', Value);
end;

procedure TFilterConfig.SetKey(const Value: string);
begin
  Close;
  if Value <> '' then
  begin
    if not Open(FILTERS_ROOT) then
      raise EFarError.CreateFmt(GetMsg(MConfigNotFound), [Value]);
    if MustExist and (not KeyExists(Value)) then
      raise EFarError.CreateFmt(GetMsg(MConfigNotFound), [Value]);
    if not Open(FILTERS_ROOT + '\' + Value) then
      raise EFarError.CreateFmt(GetMsg(MConfigNotFound), [Value]);
  end;
  fKey := Value;
end;

procedure TFilterConfig.SetMustExist(const Value: boolean);
begin
  fMustExist := Value;
  Key := Key;
end;

end.
