unit uConexionBD;

interface

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

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;
    IBServerProperties: TIBServerProperties;
    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);
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;
      MessageDlg('Conexión válida con la base de datos.', mtInformation, [mbOK], 0);
      Connected := False;      
    except
      on E : Exception do
        MessageDlg('No se ha podido establecer la conexión con la base de datos.'
          + #13 + #10 + #13 + #10 + E.Message, mtError, [mbOK], 0);
    end;
  end;
end;

procedure TfrConexionBD.Init;
begin
  if Length(dmServer.FDBServer) > 0 then
    edtServer.Text := dmServer.FDBServer;

  if Length(dmServer.FDBPort) > 0 then
    edtPort.Text := dmServer.FDBPort;

  edtPath.Text := dmServer.FDBPath;

  if Length(dmServer.FDBUser) > 0 then
    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 := '3050';
end;

end.
