unit uOptions;
{$INCLUDE 'FarCtags.inc'}

interface

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

type
  TOptions = class(TFarConfig)
  private
    fChanged: boolean;
    fCtagsFileName: string;
    fFunctionSearchDelayEnabled: boolean;
    fFunctionSearchDelay: integer;
    procedure SetCtagsFileName(const Value: string);
    procedure SetFunctionSearchDelay(const Value: integer);
    procedure SetFunctionSearchDelayEnabled(const Value: boolean);
  protected
    property Changed: boolean read fChanged;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Init;
    procedure Load;
    procedure Save(Force: boolean = False);
    procedure Assign(Options: TOptions);
    property CtagsFileName: string read fCtagsFileName write SetCtagsFileName;
    property FunctionSearchDelayEnabled: boolean read fFunctionSearchDelayEnabled write SetFunctionSearchDelayEnabled;
    property FunctionSearchDelay: integer read fFunctionSearchDelay write SetFunctionSearchDelay;
  end;

implementation

uses
  uMessages;

{ TOptions }

procedure TOptions.Assign(Options: TOptions);
begin
  CtagsFileName := Options.CtagsFileName;
  FunctionSearchDelayEnabled := Options.FunctionSearchDelayEnabled;
  FunctionSearchDelay := Options.FunctionSearchDelay;
end;

constructor TOptions.Create;
begin
  inherited Create('FarCtags', False);
  Init;
end;

destructor TOptions.Destroy;
begin
  inherited;
end;

procedure TOptions.Init;
begin
  CtagsFileName := '';
  FunctionSearchDelayEnabled := True;
  FunctionSearchDelay := 300;
end;

procedure TOptions.Load;
begin
  if Open then
    try
      CtagsFileName := ReadString('CtagsFileName', CtagsFileName);
      FunctionSearchDelayEnabled := ReadBoolean('FunctionSearchDelayEnabled', FunctionSearchDelayEnabled);
      FunctionSearchDelay := ReadInteger('FunctionSearchDelay', FunctionSearchDelay);
      fChanged := False;
    finally
      Close;
    end;
end;

procedure TOptions.Save(Force: boolean);
begin
  if not (Force or Changed) then
    Exit;
  if Open then
    try
      WriteString('CtagsFileName', CtagsFileName);
      WriteBoolean('FunctionSearchDelayEnabled', FunctionSearchDelayEnabled);
      WriteInteger('FunctionSearchDelay', FunctionSearchDelay);
      fChanged := False;
    finally
      Close;
    end;
end;

procedure TOptions.SetCtagsFileName(const Value: string);
begin
  if Value <> fCtagsFileName then
  begin
    fCtagsFileName := Value;
    fChanged := True;
  end;
end;

procedure TOptions.SetFunctionSearchDelay(const Value: integer);
begin
  if Value <> fFunctionSearchDelay then
  begin
    fFunctionSearchDelay := Value;
    fChanged := True;
  end;
end;

procedure TOptions.SetFunctionSearchDelayEnabled(const Value: boolean);
begin
  if Value <> fFunctionSearchDelayEnabled then
  begin
    fFunctionSearchDelayEnabled := Value;
    fChanged := True;
  end;
end;

end.
