unit uDataModuleBase;

interface

uses
  Controls, PngImageList, JvComponent, JvNavigationPane, TBXSwitcher,
  TBXOffice2003Theme, Classes, ImgList, uROClient, uROBinMessage,
  uROServiceComponent, uRORemoteService, uROWinInetHttpChannel,
  DataAbstract_Intf, uDADataTable, uDABINAdapter, cxintl, cxIntlPrintSys3,
  JvAppStorage, JvAppRegistryStorage, SysUtils,
  JvComponentBase;

type
  TdmBase = class(TDataModule)
    ROChannel: TROWinInetHTTPChannel;
    SmallImages: TPngImageList;
    StyleManager: TJvNavPaneStyleManager;
    TBXSwitcher: TTBXSwitcher;
    cxIntl: TcxIntl;
    cxIntlPrintSys3: TcxIntlPrintSys3;
    ROMessage: TROBinMessage;
    DABINAdapter: TDABINAdapter;
    JvAppRegistryStorage: TJvAppRegistryStorage;
    RORemoteService: TRORemoteService;
    procedure DAClientDataModuleCreate(Sender: TObject);
    procedure ROChannelFailure(Sender: TROTransportChannel;
      anException: Exception; var Retry: Boolean);
    procedure ROChannelProgress(iSender: TObject; iType: TProgressType;
      iDirection: TProgressDirection; iTransferred, iTotal: Integer);
  private
    FOnThemeChange: TNotifyEvent;
    function GetAdapter: TDABINAdapter;
    function GetChannel: TROWinInetHTTPChannel;
    function GetCodigoEmpresa: Integer;
    function GetMessage: TROBinMessage;
    procedure InitStyleManager;
    procedure OnTBXThemeChange(Sender: TObject);
    procedure LeerConfiguracion;
    procedure SalvarConfiguracion;
  public
    function DarVersion : String;
    procedure ConfigurarConexion;
    function HayConexion(const ATargetURL : String = ''): Boolean;
    property Adapter: TDABINAdapter read GetAdapter;
    property Channel: TROWinInetHTTPChannel read GetChannel;
    property CodigoEmpresa: Integer read GetCodigoEmpresa;
    property Message: TROBinMessage read GetMessage;
    property OnThemeChange: TNotifyEvent read FOnThemeChange write
            FOnThemeChange;
  end;

var
  dmBase: TdmBase;

implementation

{$R *.DFM}

uses
  Dialogs, TBX, TBXThemes, uConfigurarConexion, WinInet, Forms, Windows,
  JclFileUtils, VARELA_Intf, cxControls;

const
  SERVER_URL = 'http://localhost:8099/bin';
  IE_OFFLINE_ERROR = 'Unexpected error in WinInet HTTP Channel (2)';

{
*********************************** TdmBase ************************************
}
procedure TdmBase.ConfigurarConexion;
begin
  with TfConfigurarConexion.Create(NIL) do
    try
      TargetURL := ROChannel.TargetURL;
      if ShowModal = mrOk then
      begin
        ROChannel.TargetURL := TargetURL;
        ROChannel.Connected := False;
        ROChannel.Connected := True;
        SalvarConfiguracion;
      end;
    finally
      Free;
    end;
end;

procedure TdmBase.DAClientDataModuleCreate(Sender: TObject);
begin
  TBXSwitcher.OnThemeChange := OnTBXThemeChange;
  TBXSetTheme('Office2003');

  LeerConfiguracion;
end;

function TdmBase.GetAdapter: TDABINAdapter;
begin
  Result := DABinAdapter;
end;

function TdmBase.GetChannel: TROWinInetHTTPChannel;
begin
  Result := ROChannel;
end;

function TdmBase.GetCodigoEmpresa: Integer;
begin
  Result := 1;
end;

function TdmBase.GetMessage: TROBinMessage;
begin
  Result := ROMessage;
end;

procedure TdmBase.InitStyleManager;
begin
  if not USE_THEMES then
  begin
    TBXSwitcher.EnableXPStyles := False;
    StyleManager.Theme := nptStandard;
  end
  else begin
    TBXSwitcher.EnableXPStyles := True;
    with StyleManager do
    begin
      if CurrentTheme is TTBXOffice2003Theme then
      begin
        case GetOffice2003Scheme of
          osBlue      : Theme := nptXPBlue;
          osMetallic  : Theme := nptXPSilver;
          osGreen     : Theme := nptXPOlive;
          else
                        Theme := nptStandard;
        end;
      end;
    end;
  end;
end;


procedure TdmBase.LeerConfiguracion;
begin
  ROChannel.TargetURL := JvAppRegistryStorage.ReadString('ServerURL', SERVER_URL);
end;

procedure TdmBase.OnTBXThemeChange(Sender: TObject);
begin
  InitStyleManager;

  if Assigned(FOnThemeChange) then
    FOnThemeChange(Sender);
end;

procedure TdmBase.SalvarConfiguracion;
begin
  JvAppRegistryStorage.BeginUpdate;
  try
    JvAppRegistryStorage.WriteString('ServerURL', ROChannel.TargetURL);
  finally
    JvAppRegistryStorage.EndUpdate;
  end;
end;

procedure TdmBase.ROChannelFailure(Sender: TROTransportChannel;
  anException: Exception; var Retry: Boolean);
begin
  if (Pos(anException.Message, IE_OFFLINE_ERROR) > 0) then
  begin
    // Preguntar al usuario si se quiere conectar
    if InternetGoOnline(PAnsiChar(ROChannel.TargetURL), GetDesktopWindow(), 0) then
      Retry := True  // Si el usuario pulsa en 'Conectar' reintentar la operación
    else
      Abort; // Si el usuario pulsa en 'Seguir desconectado' parar todo
  end
  else
    Retry := True;
//    raise Exception.Create(anException.Message);
end;

procedure TdmBase.ROChannelProgress(iSender: TObject; iType: TProgressType;
  iDirection: TProgressDirection; iTransferred, iTotal: Integer);
begin
{  if iType = ptInProgress then
  begin
    if not Form1.Visible then
      Form1.Show;
  end
  else
    Form1.Hide;}
end;

function TdmBase.DarVersion: String;
var
  Obj : TJclFileVersionInfo;
begin
  Obj := TJclFileVersionInfo.Create(Application.ExeName);
  try
    Result := 'v' + Obj.FileVersion;
  finally
    FreeAndNil(Obj);
  end;
end;

function TdmBase.HayConexion(const ATargetURL: String): Boolean;
var
  AHTTPChannel: TROWinInetHTTPChannel;
  AROBinMessage: TROBinMessage;
  ACoService: TRORemoteService;
begin
  AHTTPChannel := TROWinInetHTTPChannel.Create(Self);
  AROBinMessage := TROBinMessage.Create(Self);
  ACoService := TRORemoteService.Create(Self);

  ShowHourglassCursor;
  try
    with AHTTPChannel do
    begin
      Name := 'HTTPChannel';
      UserAgent := 'AdminPV';
      if Length(ATargetURL) > 0 then
        TargetURL := ATargetURL
      else
        TargetURL := ROChannel.TargetURL;
    end;

    with ACoService do
    begin
      ServiceName := 'srvLogin';
      Message := AROBinMessage;
      Channel := AHTTPChannel;
    end;

    try
      (ACoService as IsrvLogin).Ping;
      Result := True;
    except
      Result := False;
    end;
  finally
    AHTTPChannel.Connected := False;
    FreeAndNil(AHTTPChannel);
    FreeAndNil(ACoService);
    FreeAndNil(AROBinMessage);
    HideHourglassCursor;
  end;
end;

initialization
  dmBase := TdmBase.Create(NIL);

finalization
  dmBase.Free;

end.
