unit uLogin;
// (C) by Marco Schulze <marco@nightlabs.com>
// [YMD] 2001-03-07
// published under GPL (GNU Public Licence)

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TFormLogin = class(TForm)
    EditUsrName: TEdit;
    EditPwd: TEdit;
    BtnOK: TBitBtn;
    EditHost: TEdit;
    EditPort: TEdit;
    BtnCancel: TBitBtn;
    CheckBox1: TCheckBox;
    function Execute(AutoLogin:Boolean):Boolean;
    procedure BtnOKClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormLogin: TFormLogin;

implementation

uses MServClients, Defs;

{$R *.DFM}

function TFormLogin.Execute(AutoLogin:Boolean):Boolean;
var
  LoginDone : Boolean;
begin
  Result:=False;

  EditHost.Text:=IniFile.ReadString('Server','HostName','');
  EditPort.Text:=IniFile.ReadString('Server','Port','');

  EditUsrName.Text:=IniFile.ReadString('User','UserName','');
  EditPwd.Text:=IniFile.ReadString('User','Password','');

  if (EditHost.Text='') or
     (EditPort.Text='') or
     (EditUsrName.Text='') or
     (EditPwd.Text='') then AutoLogin:=False;


  if not AutoLogin then begin
    LoginDone:=ShowModal=mrOK;
  end
  else begin
    BtnOK.OnClick(BtnOK);
    if ModalResult=mrOK then begin
      CheckBox1.Checked:=True;
      LoginDone:=True
    end
    else begin
      LoginDone:=ShowModal=mrOK;
    end;
  end;


  if LoginDone then begin
    IniFile.WriteString('Server','HostName',EditHost.Text);
    IniFile.WriteString('Server','Port',EditPort.Text);

    IniFile.WriteString('User','UserName',EditUsrName.Text);
    if CheckBox1.Checked then
      IniFile.WriteString('User','Password',EditPwd.Text)
    else
      IniFile.WriteString('User','Password','');

    Result:=True;
  end;
end;

procedure TFormLogin.BtnOKClick(Sender: TObject);
var
  ResI   : Integer;
  ResS   : String;
begin
  ModalResult:=mrNone;

  MServClient.Connect(EditHost.Text,StrToInt(EditPort.Text));

  if not MServClient.Login(EditUsrName.Text,EditPwd.Text, ResI, ResS) then begin
    MessageDlg('Login failed: ['+IntToStr(ResI)+'] '+ResS,mtError,[mbOK],0);
    Exit;
  end;

  ModalResult:=mrOK;
end;

procedure TFormLogin.FormShow(Sender: TObject);
begin
  if EditPwd.Text='' then EditPwd.SetFocus;
  if EditUsrName.Text='' then EditUsrName.SetFocus;
  if EditPort.Text='' then EditPort.SetFocus;
  if EditHost.Text='' then EditHost.SetFocus;
end;

end.
