unit uConfiguracionController;

interface

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

type
  TEnumAlmacen = (teXML, teBD);

  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 AFuente: TEnumAlmacen; const ASettingName: String; const ADefaultValue : String = ''): String;
    function GetSettingAsInteger(const AFuente: TEnumAlmacen; const ASettingName: String; const ADefaultValue : Integer = 0): Integer;
    function GetSettingAsFloat(const AFuente: TEnumAlmacen; const ASettingName: String; const ADefaultValue : Float = 0): Float;
    function GetSettingAsBoolean(const AFuente: TEnumAlmacen; const ASettingName: String; const ADefaultValue : Boolean = False): Boolean;

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

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

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

    procedure SetSettingAsString(const AFuente: TEnumAlmacen; const ASettingName: String; AValue : String);
    procedure SetSettingAsInteger(const AFuente: TEnumAlmacen; const ASettingName: String; AValue : Integer);
    procedure SetSettingAsFloat(const AFuente: TEnumAlmacen; const ASettingName: String; AValue : Float);
    procedure SetSettingAsBoolean(const AFuente: TEnumAlmacen; 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;

  FAppDBStorage := dmConfiguracion;

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

destructor TConfiguracionController.Destroy;
begin
  FreeANDNIL(FAppXMLFileStorage);

  FAppDBStorage := NIL;

  inherited;
end;

function TConfiguracionController.GetSettingAsBoolean(const AFuente: TEnumAlmacen;
  const ASettingName: String; const ADefaultValue: Boolean): Boolean;
begin
  case AFuente of
    teXML: Result := FAppXMLFileStorage.ReadBoolean(ASettingName, ADefaultValue);
    teBD: Result := FAppDBStorage.DarValor(ASettingName);
  end;
end;

function TConfiguracionController.GetSettingAsFloat(const AFuente: TEnumAlmacen; const ASettingName: String;
  const ADefaultValue: Float): Float;
begin
  case AFuente of
    teXML: Result := FAppXMLFileStorage.ReadFloat(ASettingName, ADefaultValue);
    teBD: Result := FAppDBStorage.DarValor(ASettingName);
  end;
end;

function TConfiguracionController.GetSettingAsInteger(const AFuente: TEnumAlmacen;
  const ASettingName: String; const ADefaultValue: Integer): Integer;
begin
  case AFuente of
    teXML: Result := FAppXMLFileStorage.ReadInteger(ASettingName, ADefaultValue);
    teBD: Result := FAppDBStorage.DarValor(ASettingName);
  end;
end;

function TConfiguracionController.GetSettingAsString(const AFuente: TEnumAlmacen; const ASettingName,
  ADefaultValue: String): String;
begin
  case AFuente of
    teXML: Result := FAppXMLFileStorage.ReadString(ASettingName, ADefaultValue);
    teBD: Result := FAppDBStorage.DarValor(ASettingName);
  end;
end;

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

procedure TConfiguracionController.SetSettingAsBoolean(const AFuente: TEnumAlmacen;
  const ASettingName: String; AValue: Boolean);
begin
  case AFuente of
    teXML: FAppXMLFileStorage.WriteBoolean(ASettingName, AValue);
    teBD: FAppDBStorage.GuardarValor(ASettingName, AValue);
  end;
end;

procedure TConfiguracionController.SetSettingAsFloat(const AFuente: TEnumAlmacen; const ASettingName: String;
  AValue: Float);
begin
  case AFuente of
    teXML: FAppXMLFileStorage.WriteFloat(ASettingName, AValue);
    teBD: FAppDBStorage.GuardarValor(ASettingName, AValue);
  end;
end;

procedure TConfiguracionController.SetSettingAsInteger(const AFuente: TEnumAlmacen;
  const ASettingName: String; AValue: Integer);
begin
  case AFuente of
    teXML: FAppXMLFileStorage.WriteInteger(ASettingName, AValue);
    teBD: FAppDBStorage.GuardarValor(ASettingName, AValue);
  end;
end;

procedure TConfiguracionController.SetSettingAsString(const AFuente: TEnumAlmacen;
  const ASettingName: String; AValue: String);
begin
  case AFuente of
    teXML: FAppXMLFileStorage.WriteString(ASettingName, AValue);
    teBD: FAppDBStorage.GuardarValor(ASettingName, AValue);
  end;
end;


end.
