unit uDataModuleConfiguracion;

interface

uses
  SysUtils, Classes, uRORemoteService, uDADataTable,
  uDABINAdapter, uROClient, IniFiles,
  uDADataStreamer;

const
  SERVER_URL = 'http://localhost:8099/bin';

type
  TdmConfiguracion = class(TDataModule)
    RORemoteService: TRORemoteService;
  private
    FIniFile : TIniFile;
  public
    function DarValor(const CODIGO: String; const ValorPorDefecto: String = ''): Variant;
    procedure LeerConfiguracion;
    procedure SalvarConfiguracion;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  dmConfiguracion: TdmConfiguracion;

implementation

{$R *.dfm}

uses
  Forms, FactuGES_Intf, Variants, uDataModuleConexion,
  uSistemaFunc, SHFolder, uAppInfoUtils;

const
  CONFIG_INI_FILE = 'config.ini';

{ TdmConfiguracion }

constructor TdmConfiguracion.Create(AOwner: TComponent);
var
  AIniFile : String;
begin
  inherited;
  AIniFile := GetSpecialFolderPath(CSIDL_COMMON_APPDATA); //[All Users]\Application Data
  AIniFile := AIniFile + PathDelim + GetAppName + PathDelim;

  if not DirectoryExists(AIniFile) then
    CreateDir(AIniFile);
    
  FIniFile := TIniFile.Create(AIniFile + CONFIG_INI_FILE);
end;

function TdmConfiguracion.DarValor(const CODIGO: String; const ValorPorDefecto: String = ''): Variant;
begin
  RORemoteService.Channel := dmConexion.ROChannel;
  RORemoteService.Message := dmConexion.ROMessage;
  Result := (RORemoteService as IsrvConfiguracion).DarValor(CODIGO, ValorPorDefecto);
end;

destructor TdmConfiguracion.Destroy;
begin
  FreeAndNIL(FIniFile);
  inherited;
end;

procedure TdmConfiguracion.LeerConfiguracion;
begin
  with FIniFile do
    dmConexion.TargetURL := ReadString('Server', 'URL', SERVER_URL);
end;

procedure TdmConfiguracion.SalvarConfiguracion;
begin
  with FIniFile do
    WriteString('Server', 'URL', dmConexion.TargetURL);
end;

end.
