unit Login;
{
    Copyright 2001, Peter Millard

    This file is part of Exodus.

    Exodus is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Exodus is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Exodus; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
}

interface

uses
  Session, AnyQBase,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, buttonFrame, ComCtrls, Menus, TntStdCtrls, ExtCtrls;

type
  TfrmLogin = class(TAnyQBaseForm)
    Label5: TLabel;
    cboProfiles: TTntComboBox;
    chkInvisible: TCheckBox;
    popProfiles: TPopupMenu;
    CreateNew1: TMenuItem;
    Delete1: TMenuItem;
    lblJID: TTntLabel;
    Label3: TLabel;
    txtUsername: TTntEdit;
    Label11: TLabel;
    cboServer: TTntComboBox;
    Label12: TLabel;
    cboResource: TTntComboBox;
    Label10: TLabel;
    txtPassword: TTntEdit;
    chkSavePasswd: TCheckBox;
    Panel1: TPanel;
    frameButtons1: TframeButtons;
    Panel2: TPanel;
    btnDetails: TButton;
    Bevel1: TBevel;
    Image1: TImage;
    Label1: TLabel;
    btnOK: TButton;

    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cboProfilesChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnDetailsClick(Sender: TObject);
    procedure CreateNew1Click(Sender: TObject);
    procedure Delete1Click(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure frameButtons1btnOKClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure CreateNewProfile(sProfileNew: string);
  end;

var
  frmLogin: TfrmLogin;

procedure ShowLogin;

resourcestring
  sProfileDefaultResource = 'AnyQ';
  sProfileRemove = 'Remove this profile?';
  sProfileDefault = ''; //'Default Profile';
//    sProfileNew = 'Untitled Profile';
  sProfileCreate = 'New Profile';
  sProfileNamePrompt = 'Enter Profile Name';

{---------------------------------------}
{---------------------------------------}
{---------------------------------------}
implementation

{$R *.DFM}

uses
  ExUtils, GnuGetText, Jabber1, JabberID, Unicode, InputPassword,
  ConnDetails, PrefController, RiserWindow, regUser;

{---------------------------------------}

procedure ShowLogin;
var
  l: TfrmLogin;
  i: integer;
  p: TJabberProfile;
begin
  l := TfrmLogin.Create(Application);
  l.cboProfiles.Items.Assign(MainSession.Prefs.Profiles);
  i := MainSession.Prefs.getInt('profile_active');
  if (i < 0) then i := 0;
  if (i >= l.cboProfiles.Items.Count) then i := l.cboProfiles.Items.Count - 1;
  l.cboProfiles.ItemIndex := i;
  l.cboProfilesChange(nil);

  frmAnyQ.PreModal(l);

  if l.ShowModal = mrOK then begin
        // Save the info on the profile and login
    i := MainSession.Prefs.Profiles.IndexOf(l.cboProfiles.text);
    if i < 0 then
    begin
      l.CreateNewProfile(l.cboProfiles.text);
      i := MainSession.Prefs.Profiles.Count - 1;
    end;

    p := TJabberProfile(MainSession.Prefs.Profiles.Objects[i]);
    p.SavePasswd := l.chkSavePasswd.Checked;
    p.password := l.txtPassword.Text;
    p.Server := l.cboServer.Text;

    if (Trim(p.Resource) = '') then
      p.Resource := sProfileDefaultResource;

    MainSession.Prefs.setInt('profile_active', i);
    MainSession.Prefs.SaveProfiles();

        // Active this profile, and fire it UP!
    MainSession.ActivateProfile(i);
    MainSession.Invisible := l.chkInvisible.Checked;
    l.Close();
    frmAnyQ.DoConnect();
  end
  else
    l.Close();

  frmAnyQ.PostModal();
end;

{---------------------------------------}

procedure TfrmLogin.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if MainSession <> nil then
    MainSession.Prefs.SavePosition(Self);
  Action := caFree;
end;

{---------------------------------------}

procedure TfrmLogin.cboProfilesChange(Sender: TObject);
var
  Index: Integer;
begin
  chkInvisible.Checked := false;
  chkSavePasswd.Checked := false;
  txtPassword.Text := '';

  cboResource.Text := sProfileDefaultResource;

  Index := cboProfiles.ItemIndex;
  if Index >= 0 then
    with TJabberProfile(MainSession.Prefs.Profiles.Objects[Index]) do
    begin
      lblJID.Caption := Username + '@' + Server + '/' + Resource;
      chkSavePasswd.Checked := SavePasswd;
      cboServer.Text := Server;
      cboResource.Text := Resource;
      if SavePasswd then
        txtPassword.Text := Password
    end;
end;

{---------------------------------------}

procedure TfrmLogin.FormCreate(Sender: TObject);
begin
  cboServer.Items.Add(DefaultServerName);
  cboServer.Text := DefaultServerName;
  TranslateProperties(Self);
  MainSession.Prefs.RestorePosition(Self);
end;

{---------------------------------------}

procedure TfrmLogin.btnDetailsClick(Sender: TObject);
var
  i: integer;
  p: TJabberProfile;
begin
  i := cboProfiles.ItemIndex;
  if i < 0 then
  begin
    CreateNewProfile(cboProfiles.text);
    i := MainSession.Prefs.Profiles.Count - 1;
  end;
  p := TJabberProfile(MainSession.Prefs.Profiles.Objects[i]);

  ShowConnDetails(p);
  lblJID.Caption := p.Username + '@' + p.Server + '/' + p.Resource;
end;

{---------------------------------------}

procedure TfrmLogin.CreateNew1Click(Sender: TObject);
var
  pname: WideString;
  i: integer;
  p: TJabberProfile;
begin
    // Create a new profile
{    pname := sProfileNew;
    if InputQueryW(sProfileCreate, sProfileNamePrompt, pname) then begin
        p := MainSession.Prefs.CreateProfile(pname);
        cboProfiles.Items.Assign(MainSession.Prefs.Profiles);
        i := cboProfiles.Items.Indexof(pname);
        cboProfiles.ItemIndex := i;
        cboProfilesChange(Self);
        p.Resource := sProfileDefaultResource;
        ShowConnDetails(p);
    end;{}
end;

{---------------------------------------}

procedure TfrmLogin.Delete1Click(Sender: TObject);
var
  i: integer;
  p: TJabberProfile;
begin
    // Delete this profile
  if (MessageBox(Handle, Pchar(sProfileRemove), '确认', MB_YESNO or MB_ICONQUESTION) = mrNo) then exit;

  i := cboProfiles.ItemIndex;
  p := TJabberProfile(MainSession.Prefs.Profiles.Objects[i]);
  MainSession.Prefs.RemoveProfile(p);
  MainSession.Prefs.setInt('profile_active', 0);

    // make sure we have at least a default profile
  if (MainSession.Prefs.Profiles.Count) <= 0 then begin
    MainSession.Prefs.CreateProfile(sProfileDefault)
  end;

  cboProfiles.Items.Assign(MainSession.Prefs.Profiles);
  cboProfiles.ItemIndex := MainSession.Prefs.getInt('profile_active');
  cboProfilesChange(nil);

end;

{---------------------------------------}

procedure TfrmLogin.Label5Click(Sender: TObject);
var
  cp: TPoint;
begin
    // show the popup
  GetCursorPos(cp);
  popProfiles.Popup(cp.x, cp.y);
end;

procedure TfrmLogin.CreateNewProfile(sProfileNew: string);
var
  pname: WideString;
  i: integer;
  p: TJabberProfile;
begin
    // Create a new profile
  pname := sProfileNew;
  p := MainSession.Prefs.CreateProfile(pname);
  p.Username := p.Name;
  p.Server := cboServer.Text;
  p.Resource := cboResource.Text;
  p.SavePasswd := chkSavePasswd.Checked;
  p.password := txtPassword.Text;
  cboProfiles.Items.Assign(MainSession.Prefs.Profiles);
  i := cboProfiles.Items.Indexof(pname);
  cboProfiles.ItemIndex := i;
  if Visible then
    cboProfilesChange(Self);
//        ShowConnDetails(p);
end;

procedure TfrmLogin.frameButtons1btnOKClick(Sender: TObject);
begin
  if trim(cboProfiles.Text) = '' then
  begin
    ShowRiserWindow(nil, '请输入用户名！', ico_info);
    abort;
    //raise Exception.Create('请输入用户名！');
  end;
  if trim(cboServer.Text) = '' then
  begin
    ShowRiserWindow(nil, '请输入企业名！', ico_info);
    abort;
    //raise Exception.Create('请输入企业名！');
  end;
  if trim(txtPassword.Text) = '' then
  begin
    ShowRiserWindow(nil, '请输入密码！', ico_info);
    abort;
    //raise Exception.Create('请输入密码！');
  end;
  if pos('@', cboProfiles.Text) > 0 then
  begin
    ShowRiserWindow(nil, '用户名中不能包含“@”！', ico_info);
    abort;
    //raise Exception.Create('请输入用户名！');
  end;

  ModalResult := mrok;
end;

procedure TfrmLogin.btnOKClick(Sender: TObject);
var
  fmreg: TFrmRegUser;
begin
  fmreg := TFrmRegUser.Create(Application);
  try
    if fmreg.ShowModal=mrOK then
    begin
      cboProfiles.Text := fmreg.txtUser.Text;
      cboServer.Text := fmreg.cboServer.Text;
      txtPassword.Text := fmreg.txtPassword.Text;
      frmAnyQ.newAccount := True;
      MainSession.CreateAccount;
      ModalResult := mrOk;
    end;
  finally
    fmreg.Free;
  end;
end;

end.
