unit uConfiguracionController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIViewConfiguracionBase,
  uViewConfiguracionBase, uClassRegistryUtils, JvAppXMLStorage;

type
  IPluginConfigurable = interface
    ['{30063608-BFD2-47B9-80FA-F10A5067AF82}']
    function GetVistaConfiguracion : TClass;
    function GetEtiquetaConfiguracion : String;
  end;

  IConfiguracionController = interface(IControllerBase)
    ['{F29215BA-0D34-4AE9-B9A9-C80ABF49A81D}']
    procedure ConfigurarAplicacion;
    procedure AplicarConfiguracion;    
    procedure SalvarConfiguracion;
    procedure CargarConfiguracion;

    function GetSettingAsString(const ASettingName: String; const ADefaultValue : String = ''): String;
    function GetSettingAsInteger(const ASettingName: String; const ADefaultValue : Integer = 0): Integer;
    function GetSettingAsFloat(const ASettingName: String; const ADefaultValue : Float = 0): Float;
    function GetSettingAsBoolean(const ASettingName: String; const ADefaultValue : Boolean = False): Boolean;

    procedure SetSettingAsString(const ASettingName: String; AValue : String);
    procedure SetSettingAsInteger(const ASettingName: String; AValue : Integer);
    procedure SetSettingAsFloat(const ASettingName: String; AValue : Float);
    procedure SetSettingAsBoolean(const ASettingName: String; AValue : Boolean);
  end;

  TConfiguracionController = class(TControllerBase, IConfiguracionController)
  private
    FAppXMLFileStorage : TJvAppXMLFileStorage;
  public
    procedure SalvarConfiguracion;
    procedure AplicarConfiguracion;
    procedure CargarConfiguracion;
    procedure ConfigurarAplicacion;

    function GetSettingAsString(const ASettingName: String; const ADefaultValue : String = ''): String;
    function GetSettingAsInteger(const ASettingName: String; const ADefaultValue : Integer = 0): Integer;
    function GetSettingAsFloat(const ASettingName: String; const ADefaultValue : Float = 0): Float;
    function GetSettingAsBoolean(const ASettingName: String; const ADefaultValue : Boolean = False): Boolean;

    procedure SetSettingAsString(const ASettingName: String; AValue : String);
    procedure SetSettingAsInteger(const ASettingName: String; AValue : Integer);
    procedure SetSettingAsFloat(const ASettingName: String; AValue : Float);
    procedure SetSettingAsBoolean(const ASettingName: String; AValue : Boolean);

    constructor Create; override;
    destructor Destroy; override;
  end;

implementation

uses
  Forms, uEditorConfiguracion, uViewConfiguracionGeneral, uDMBase, 
  JvAppStorage, uAppInfoUtils, Controls, uViewConfiguracionRegistryUtils;

const
  CONFIG_INI_FILE = 'config.xml';

{ TGestorConfiguracion }

procedure TConfiguracionController.AplicarConfiguracion;
begin
	//
end;

procedure TConfiguracionController.CargarConfiguracion;
begin
  FAppXMLFileStorage.Reload;
end;

procedure TConfiguracionController.ConfigurarAplicacion;
var
  i: Integer;
  AViewList : TStringList;
  AEditor : TfConfiguracionEditor;
  AView : TfrViewConfiguracionBase;
  AEtiqueta : String;
begin
  AViewList := TStringList.Create;
  try
    ViewConfiguracionRegistry.GetRegisteredClasses(AViewList);
    AEditor := TfConfiguracionEditor.Create(NIL);
    try
      for i := 0 to AViewList.Count - 1 do
      begin
        AEtiqueta := AViewList[i];
        AView := TfrViewConfiguracionBase(ViewConfiguracionRegistry.CreateView(AEtiqueta, AEditor));
        AEditor.AnadirVista(AView, AEtiqueta);
      end;

      AEditor.CargarValores;
      if (AEditor.ShowModal = mrOK) then
        AEditor.GuardarValores;

    finally
      FreeANDNIL(AEditor)
    end;
  finally
    FreeANDNIL(AViewList)
  end;
end;

constructor TConfiguracionController.Create;
begin
  inherited;
  FAppXMLFileStorage := TJvAppXMLFileStorage.Create(NIL);
  with FAppXMLFileStorage do
  begin
    StorageOptions.BooleanStringTrueValues := 'TRUE, YES, Y';
    StorageOptions.BooleanStringFalseValues := 'FALSE, NO, N';
    StorageOptions.InvalidCharReplacement := '_';
    Location := flUserFolder;
    RootNodeName := 'Configuration';
    FileName := 'Rodax Software' + PathDelim + GetAppName + PathDelim + CONFIG_INI_FILE;
  end;

  ViewConfiguracionRegistry.RegisterClass(TfrViewConfiguracionGeneral, 'General');
end;

destructor TConfiguracionController.Destroy;
begin
  FreeANDNIL(FAppXMLFileStorage);
  inherited;
end;

function TConfiguracionController.GetSettingAsBoolean(
  const ASettingName: String; const ADefaultValue: Boolean): Boolean;
begin
  Result := FAppXMLFileStorage.ReadBoolean(ASettingName, ADefaultValue)
end;

function TConfiguracionController.GetSettingAsFloat(const ASettingName: String;
  const ADefaultValue: Float): Float;
begin
  Result := FAppXMLFileStorage.ReadFloat(ASettingName, ADefaultValue);
end;

function TConfiguracionController.GetSettingAsInteger(
  const ASettingName: String; const ADefaultValue: Integer): Integer;
begin
  Result := FAppXMLFileStorage.ReadInteger(ASettingName, ADefaultValue);
end;

function TConfiguracionController.GetSettingAsString(const ASettingName,
  ADefaultValue: String): String;
begin
  Result := FAppXMLFileStorage.ReadString(ASettingName, ADefaultValue);
end;

procedure TConfiguracionController.SalvarConfiguracion;
begin
  FAppXMLFileStorage.Flush;
end;

procedure TConfiguracionController.SetSettingAsBoolean(
  const ASettingName: String; AValue: Boolean);
begin
  FAppXMLFileStorage.WriteBoolean(ASettingName, AValue);
end;

procedure TConfiguracionController.SetSettingAsFloat(const ASettingName: String;
  AValue: Float);
begin
  FAppXMLFileStorage.WriteFloat(ASettingName, AValue);
end;

procedure TConfiguracionController.SetSettingAsInteger(
  const ASettingName: String; AValue: Integer);
begin
  FAppXMLFileStorage.WriteInteger(ASettingName, AValue);
end;

procedure TConfiguracionController.SetSettingAsString(
  const ASettingName: String; AValue: String);
begin
  FAppXMLFileStorage.WriteString(ASettingName, AValue);
end;


end.
