unit uDMBase;

interface

uses
  Controls, SyncObjs, PngImageList, JvComponent, JvNavigationPane, TBXSwitcher,
  TBXOffice2003Theme, Classes, ImgList,
  DataAbstract4_Intf, uDADataTable, JvAppStorage, JvAppRegistryStorage, 
  JvComponentBase, JvLogFile, dxPSGlbl, dxPSUtl, dxPrnPg,
  dxBkgnd, dxWrap, dxPrnDev, dxPgsDlg, dxPSCore, cxLookAndFeels;

type
  TdmBase = class(TDataModule)
    SmallImages: TPngImageList;
    StyleManager: TJvNavPaneStyleManager;
    TBXSwitcher: TTBXSwitcher;
    JvLogFile: TJvLogFile;
    dxPrintStyleManager1: TdxPrintStyleManager;
    dxPrintStyleManager1Style1: TdxPSPrintStyle;
    procedure DAClientDataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    FOnThemeChange: TNotifyEvent;
    FEscribirLog : TCriticalSection;
    procedure IniciarLog;
    procedure DetenerLog;
    procedure OnTBXThemeChange(Sender: TObject);
    procedure LeerConfiguracion;
  public
    procedure InitStyleManager (ThemeID : integer = 0);
    procedure EscribirLog(const AMensaje : String);
    procedure SalvarConfiguracion;
    property OnThemeChange: TNotifyEvent read FOnThemeChange write
            FOnThemeChange;
  end;

var
  dmBase: TdmBase = nil;

implementation

{$R *.DFM}

uses
  uDataModuleConexion, Dialogs, TBX, TBXThemes, Forms, Windows,
  JclFileUtils, cxControls, SysUtils, uDataModuleConfiguracion,
  uSistemaFunc, SHFolder;

{
*********************************** TdmBase ************************************
}
procedure TdmBase.DAClientDataModuleCreate(Sender: TObject);
begin
  IniciarLog;

  TBXSwitcher.OnThemeChange := OnTBXThemeChange;
  TBXSetTheme('Office2003');

  LeerConfiguracion;
end;

procedure TdmBase.IniciarLog;
var
  ALogFileName : String;
begin
  FEscribirLog := TCriticalSection.Create;

  ALogFileName := GetSpecialFolderPath(CSIDL_APPDATA); //[Current Users]\Application Data
  ALogFileName := ALogFileName + PathDelim + 'Rodax Software' + PathDelim + 'FactuGES' + PathDelim;
  if SysUtils.ForceDirectories(ALogFileName) then
  begin
//SE COMENTA PORQUE TUESTA LA PARTE CLIENTE SI SE DESEA ARREGLAR ACTUALIZAR LAS JV de tecsitel
      JvLogFile.Active := False;
      JvLogFile.FileName := ALogFileName + 'ClientLog.txt';
      JvLogFile.AutoSave := True;
      JvLogFile.Active := True;
      JvLogFile.Clear;
     
  end
  else
    raise Exception.Create('Error al crear directorio para Log: ' + IntToStr(GetLastError));
end;

procedure TdmBase.InitStyleManager(ThemeID : integer = 0);
begin
  if ThemeID < 2 then
  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
  else begin
    StyleManager.Theme := nptStandard;
  end;
end;


procedure TdmBase.LeerConfiguracion;
begin
  dmConfiguracion.LeerConfiguracion;
end;

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

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

procedure TdmBase.SalvarConfiguracion;
begin
  dmConfiguracion.SalvarConfiguracion;
end;

procedure TdmBase.DataModuleDestroy(Sender: TObject);
begin
  DetenerLog;
end;

procedure TdmBase.DetenerLog;
begin
  FreeAndNIL(FEscribirLog);
end;

procedure TdmBase.EscribirLog(const AMensaje: String);
begin

  FEscribirLog.Acquire;
  try
    JvLogFile.Add(AMensaje);
  finally
    FEscribirLog.Release;
  end;

end;

end.
