unit uConexionBD;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, ExtCtrls, IBServices,
  DB, IBDatabase, uFrameConfiguracion, cxControls, cxContainer, cxEdit,
  cxTextEdit, cxMaskEdit, cxButtonEdit;

type
  TfrConexionBD = class(TFrameConfiguracion)
    GroupBox1: TGroupBox;
    edtServer: TEdit;
    edtPort: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    bProbar: TButton;
    Label4: TLabel;
    edtUser: TEdit;
    Label5: TLabel;
    edtPassword: TEdit;
    Bevel1: TBevel;
    OpenDialog: TOpenDialog;
    edtPath: TcxButtonEdit;
    IBDatabase: TIBDatabase;
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure bProbarClick(Sender: TObject);
    procedure edtPortExit(Sender: TObject);
  protected
    procedure Finalize; override;
  public
    procedure Init; override;
  end;

implementation

uses uDataModuleServer;

{$R *.dfm}

procedure TfrConexionBD.cxButtonEdit1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  if OpenDialog.Execute then
    edtPath.Text := OpenDialog.FileName;
end;

procedure TfrConexionBD.bProbarClick(Sender: TObject);
var
  Aux : string;
begin
  with IBDatabase do
  begin
    Connected := False;
    DatabaseName := edtServer.Text + '/' + edtPort.Text + ':' + edtPath.Text;
    LoginPrompt := False;

    Params.Clear;
    Params.Add('lc_ctype=ISO8859_1');
    Params.Add('user_name=' + edtUser.Text);
    Params.Add('password=' + edtPassword.Text);

    try
      Connected := True;
      Application.MessageBox('Conexión válida con la base de datos.', 'Etiquetas', MB_OK);
      Connected := False;
    except
      on E : Exception do
      begin
        Aux := 'No se ha podido establecer la conexión con la base de datos.'
          + #13 + #10 + #13 + #10 + PChar(E.Message);
        Application.MessageBox(PAnsiChar(Aux), 'Error', MB_OK);
      end;
    end;
  end;
end;

procedure TfrConexionBD.Init;
begin
  edtServer.Text := dmServer.FDBServer;
  edtPort.Text := dmServer.FDBPort;
  edtPath.Text := dmServer.FDBPath;
  edtUser.Text := dmServer.FDBUser;
  edtPassword.Text := Decrypt(dmServer.FDBPass);
end;

procedure TfrConexionBD.Finalize;
begin
  dmServer.FDBServer := edtServer.Text;
  dmServer.FDBPort := edtPort.Text;
  dmServer.FDBPath := edtPath.Text;
  dmServer.FDBUser := edtUser.Text;
  dmServer.FDBPass := Encrypt(edtPassword.Text);
end;

procedure TfrConexionBD.edtPortExit(Sender: TObject);
begin
  inherited;
  if Length(edtPort.Text) = 0 then
    edtPort.Text := DBSERVER_PORT;
end;

end.
