unit U_BaseConnectParams;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzButton, ExtCtrls, RzPanel, Mask, RzEdit, RzLabel,
  RzRadChk,
  FIB, FIBDatabase, pFIBDatabase,

  U_BaseCommon, U_ProtectionCommon, U_ProtectionBase, Unit_DM_SCS, siComp, siLngLnk;

type
  TF_BaseConnectParams = class(TForm)
    RzPanel1: TRzPanel;
    RzPanel2: TRzPanel;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    edUser: TRzEdit;
    edPass: TRzEdit;
    lbSetDefault: TRzLabel;
    cbRememberInPC: TRzCheckBox;
    lng_Forms: TsiLangLinked;
    procedure lbSetDefaultClick(Sender: TObject);

  private
    GForm: TForm;
  public
    ResUserName: String;
    ResPass: String;

    Constructor Create(AOwner: TComponent; AForm: TForm);
    Destructor Destroy; override;

    function Execute(var ABaseConnectParams: TBaseConnectParams; ABaseMode: TDBKind; ARemember: Boolean): Boolean;
  end;

  function ShowBaseConnectParams(var ABaseConnectParams: TBaseConnectParams; ABaseMode: TDBKind; ARemember: Boolean): Boolean;

//var
//  Form1: TForm1;

implementation

uses pFIBProps;

{$R *.dfm}

constructor TF_BaseConnectParams.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_BaseConnectParams.Destroy;
begin
  inherited;
end;

function TF_BaseConnectParams.Execute(var ABaseConnectParams: TBaseConnectParams; ABaseMode: TDBKind; ARemember: Boolean): Boolean;
begin
  Result := false;
  try
    edUser.Text := ABaseConnectParams.UserName;
    edPass.Text := ABaseConnectParams.Pass;


    cbRememberInPC.Visible := ARemember;

    if ShowModal = mrOk then
    begin
      Result := true;

      ABaseConnectParams.UserName := edUser.Text;
      ABaseConnectParams.Pass := edPass.Text;

      if ARemember then
        if cbRememberInPC.Checked then
        begin
          if ABaseMode = bkNormBase then
          begin
            SaveStrToRegistry(pnNBUser, ABaseConnectParams.UserName);
            SaveStrToRegistry(pnNBPass, XorStr(ABaseConnectParams.Pass, $AA, true));
          end
          else
          if ABaseMode = bkProjectManager then
          begin
            SaveStrToRegistry(pnPMUser, ABaseConnectParams.UserName);
            SaveStrToRegistry(pnPMPass, XorStr(ABaseConnectParams.Pass, $AA, true));
          end;
        end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_BaseConnectParams.Execute', E.Message);
  end;
end;

procedure TF_BaseConnectParams.lbSetDefaultClick(Sender: TObject);
begin
  edUser.Text := unSYSDBA;
  edPass.Text := upMasterKey;
end;


function ShowBaseConnectParams(var ABaseConnectParams: TBaseConnectParams; ABaseMode: TDBKind; ARemember: Boolean): Boolean;
var
  FBaseConnectParams: TF_BaseConnectParams;
begin
  FBaseConnectParams := TF_BaseConnectParams.Create(Application, nil);
  try
    Result := FBaseConnectParams.Execute(ABaseConnectParams, ABaseMode, ARemember);
  finally
    FreeAndNil(FBaseConnectParams);
  end;
end;

end.