(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)

unit AHMTDBPasswordDlg;

interface
uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,DBCtrls,DB,DBTables,
  Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls,AHMDialogClass,AHMTPasswordDlg;

type
  TAHMDBPasswordDlg = class;
  TAHMDataSourceLink = class(TDataLink)
  private
    FDBPasswordDlg: TAHMDBPasswordDlg;
  protected
  end;
  TAHMDBPasswordDlg = class(TAHMAdvDialogComponent)
  private
    FDataLink: TAHMDatasourceLink;
    FUserName: string;
    FPassWord: string;
    FListFields: TList;
    FFont : Tfont;
    FPasswordChar : char;
    FNoOfRetries : byte;
    FGlyphCancel,FGlyphOk : TBitmap;
    FOnRightPassword,FOnWrongPassword,FOnOk,FOnCancel : TNotifyEvent;
    FError : String;
    FAfterRetries : TNotifyevent;
    function GetDataSource: TDataSource;
    procedure SetFont(Value : TFont);
    procedure SetDataSource(Value: TDataSource);
    procedure SetUserName(const Value: string);
    procedure SetPassword(const Value: string);
    Procedure setbm1(value : Tbitmap);
    Procedure Setbm2(value : Tbitmap);
    procedure DoBitBtn2Click(Sender: TObject);
    procedure DoBitBtn1Click(Sender: TObject);
    procedure DoFormShow(Sender: TObject);
    Function VerifyLogin : Boolean;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    Function Execute : Boolean; override;
  published
    property DataUserName: string read fUserName write SetUserName;
    property DataPassword: string read fPassword write SetPassword;
    property DataSource: TDataSource read GetDataSource write SetDataSource;
    Property Fonts : TFont read FFont write SetFont;
    Property Passwordchar : Char read FPasswordChar write FPasswordChar;
    Property GlyphCancel : TBitmap read FGlyphCancel write Setbm2;
    Property GlyphOk : TBitmap read FGlyphOk write setbm1;
    Property ErrorMessage : String read FError write FError;
    Property OnRightPassword : TNotifyevent read FOnRightPassword write FOnRightPassword;
    Property OnWrongPassword : TNotifyevent read FOnWrongPassword write FOnWrongPassword;
    Property OnOk : TNotifyevent read FOnOk write FOnOk;
    Property OnCancel : TNotifyevent read FOnCancel write FOnCancel;
    Property NoOfRetries : Byte read FNoOfRetries write FNoOfRetries default 3;
    Property AfterRetries : TNotifyevent read FAfterRetries write FAfterRetries;
  end;

implementation
uses AHMDialogConst;
var DBPassForm : TPasswordForm;

constructor TAHMDBPasswordDlg.Create(AOwner: TComponent);
begin
  FGlyphOk := TBitmap.Create;
  FGlyphCancel := TBitmap.Create;
  FFont:=TFont.create;
  FError:=ahmrcdlginvalidusernameorpassword;
  FNoOfRetries := 3;
  inherited Create(AOwner);
  FormCaption := ahmrcdlgLogin;
  FDataLink := TAHMDataSourceLink.Create;
  FDataLink.FDBpasswordDlg := Self;
  FListFields := TList.Create;
end;

destructor TAHMDBPasswordDlg.Destroy;
begin
  FListFields.Free;
  FDataLink.FDBpassworddlg := nil;
  FDataLink.Free;
  FGlyphOk.free;
  FGlyphCancel.free;
  FFont.free;
  inherited Destroy;
end;


procedure TAHMDBPasswordDlg.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = DataSource) then DataSource := nil;
end;

function TAHMDBPasswordDlg.GetDataSource: TDataSource;
begin
  Result := FDataLink.DataSource;
end;

procedure TAHMDBPasswordDlg.SetDataSource(Value: TDataSource);
begin
  FDataLink.DataSource := Value;
  if Value <> nil then Value.FreeNotification(Self);
end;

procedure TAHMDBPasswordDlg.SetFont(Value: TFont);
begin
  FFont.assign(Value);
end;

procedure TAHMDBPasswordDlg.SetUserName(const Value: string);
begin
  if FUserName <> Value then FUserName := Value;
end;

procedure TAHMDBPasswordDlg.Setpassword(const Value: string);
begin
  if FPassword <> Value then FPassword := Value;
end;

Function TAHMDBPasswordDlg.Execute : Boolean;
begin
  if (FDataLink<>nil) and assigned(Datasource) then
  begin
    DBPassForm:=TPasswordForm.CreateNew(Application);
    with DBPassForm do
    try
      SetFormProperties(DBPassForm);
      SetupForm;
      Font:=FFont;
      Image1.Picture.Icon := Application.Icon;
      BitBtn2.OnClick:=DoBitBtn2Click;
      BitBtn1.OnClick:=DoBitBtn1Click;
      OnShow:=DoFormShow;
      PassEdit.passwordchar:=FPasswordChar;
      Bitbtn1.glyph:=FGlyphOk;
      Bitbtn2.glyph:=FGlyphCancel;
      showmodal;
    finally
      free;
    end;
    result:=true;
  end else result:=false;
end;

function TAHMDBPasswordDlg.VerifyLogin: Boolean;
begin
  if DataSource=nil then Result := false else
  if DataSource.Dataset=nil then Result := false else
  begin
    Result := False;
    try
      TTable(DataSource.Dataset).IndexFieldNames := DataUserName;
      TTable(DataSource.Dataset).Open;
      if TTable(DataSource.DataSet).FindKey([DBPassform.UserEdit.Text]) then
      begin
        if TTable(DataSource.DataSet).fieldbyname(DataPassword).asstring=DBPassform.PassEdit.Text then
        begin
          Result := true;
          if assigned(FOnRightPassword) then FOnRightPassword(self);
        end else if assigned(FOnWrongPassword) then FOnWrongPassword(self);
        if not(Result) then raise EDatabaseError.Create(FError);
      end else raise EDatabaseError.Create(FError);
    except
      Application.HandleException(Self);
    end;
  end;
end;

procedure TAHMDBPasswordDlg.Setbm1(value : Tbitmap);
begin
  FGlyphOk.assign(value);
end;

procedure TAHMDBPasswordDlg.Setbm2(value : Tbitmap);
begin
  FGlyphCancel.assign(value);
end;

procedure TAHMDBPasswordDlg.DoBitBtn2Click(Sender: TObject);
begin
  if assigned(FOnCancel) then FOnCancel(self);
  DBPassForm.close;
end;

procedure TAHMDBPasswordDlg.DoBitBtn1Click(Sender: TObject);
begin
  if assigned(FOnOk) then FOnOk(self);
  if VerifyLogin then DBPassForm.CLOSE
  else if DBPassForm.xsret>=FNoOfRetries then
  begin
    DBPassForm.close;
    if assigned(FAfterRetries) then FAfterRetries(self);
  end;
  inc(DBPassForm.xsret);
end;

procedure TAHMDBPasswordDlg.DoFormShow(Sender: TObject);
begin
  DBPassForm.xsret:=1;
  DBPassForm.passEdit.text:='';
  DBPassForm.userEdit.text:='';
  DBPassForm.userEdit.Setfocus;
  DBPassForm.bitbtn1.enabled:=false;
end;

end.
