unit RosterAdd;
{
    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
  Agents, AnyQBase,
  XMLTag, iq,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  buttonFrame, StdCtrls;

type
  TfrmAdd = class(TAnyQBaseForm)
    Label1: TLabel;
    txtJID: TEdit;
    Label2: TLabel;
    txtNickname: TEdit;
    Label3: TLabel;
    cboGroup: TComboBox;
    frameButtons1: TframeButtons;
    lblAddGrp: TLabel;
    cboType: TComboBox;
    Label4: TLabel;
    lblGateway: TLabel;
    txtGateway: TEdit;
    procedure frameButtons1btnOKClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure frameButtons1btnCancelClick(Sender: TObject);
    procedure lblAddGrpClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure txtJIDExit(Sender: TObject);
    procedure cboTypeChange(Sender: TObject);
  private
    { Private declarations }
    cb: integer;
    agents: TAgents;
    svc, gw, sjid, snick, sgrp: string;
    iq: TJabberIQ;
    procedure doAdd;
  published
    procedure agentsCallback(event: string; tag: TXMLTag);
  public
    { Public declarations }
    procedure vcard(event: string; tag: TXMLTag);
  end;

var
  frmAdd: TfrmAdd;

resourcestring
  sNoDomain = '您输入的SID没有按照标准的"用户@企业"的格式输入，要继续吗?';
  sResourceSpec = 'SID通常不需要包含resource。 您确定要添加这个联系人SID?';
  sNoGatewayFound = '网关服务器不支持该联系类型';
  sNotAddMyself = '您不能够将您自己加入联系人列表中';

function ShowAddContact: TfrmAdd;

{---------------------------------------}
{---------------------------------------}
{---------------------------------------}
implementation
uses
  InputPassword, ExSession, ExUtils, math,
  GnuGetText, Jabber1, JabberID, Presence, Session, RiserWindow,
  RosterWindow, vcard, XMLVCard;
{$R *.DFM}

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

function ShowAddContact: TfrmAdd;
begin
  Result := TfrmAdd.Create(Application);
  Result.Show;
end;

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

procedure TfrmAdd.frameButtons1btnOKClick(Sender: TObject);
var
  tmp_jid: TJabberID;
begin
    // Add the new roster item..
  sjid := txtJID.Text;
  snick := txtNickname.Text;
  sgrp := cboGroup.Text;
  svc := lowercase(cboType.Text);

    // check to see if we need an agents list
  if (cboType.ItemIndex > 0) then begin
        // Adding a gateway'd user
    gw := txtGateway.Text;
    agents := MainSession.GetAgentsList(gw);
    if (agents = nil) then begin
      cb := MainSession.RegisterCallback(agentsCallback, '/session/agents');
      agents := MainSession.NewAgentsList(gw);
      agents.Fetch(gw);
      Self.Hide;
    end
    else
      doAdd();
  end
  else begin
        // Adding a normal Jabber user
        // check to see if we have an "@" sign
    if (Lowercase(sjid) = Lowercase(MainSession.Username + '@' + MainSession.Server)) then begin
      ShowRiserWindow(nil, sNotAddMyself, ico_info);
            //MessageBox(Handle, Pchar(sNotAddMyself), '错误', MB_OK OR MB_ICONERROR);
      exit;
    end;

    tmp_jid := TJabberID.Create(sjid);
    if (tmp_jid.user = '') then
      if MessageBox(Handle, Pchar(sNoDomain), '确认', MB_YESNO or MB_ICONQUESTION) = mrNo then exit;
    if (tmp_jid.resource <> '') then
      if MessageBox(Handle, Pchar(sResourceSpec), '确认', MB_YESNO or MB_ICONQUESTION) = mrNo then exit;
    doAdd();
    tmp_jid.Free();
  end;
end;

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

procedure TfrmAdd.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

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

procedure TfrmAdd.frameButtons1btnCancelClick(Sender: TObject);
begin
  Self.Close;
end;

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

procedure TfrmAdd.lblAddGrpClick(Sender: TObject);
var
  ngrp: WideString;
begin
  frmAnyQ.NewGroup2.Click;
  cboGroup.Items.Assign(MainSession.Roster.GrpList);
  removeSpecialGroups(cboGroup.Items);
  cboGroup.ItemIndex := max(0, cboGroup.Items.IndexOf(MainSession.Prefs.getString('roster_default')));
{
    // Add a new group to the list...
  ngrp := sDefaultGroup;
  if InputQueryW(sNewGroup, sNewGroupPrompt, ngrp) then begin
    MainSession.Roster.GrpList.Add(ngrp);
    cboGroup.Items.Assign(MainSession.Roster.GrpList);
    removeSpecialGroups(cboGroup.Items);
  end;{}
end;

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

procedure TfrmAdd.FormCreate(Sender: TObject);
begin
  cboType.Items.Insert(0, ProductName);
  TranslateProperties(Self);
  cboGroup.Items.Assign(MainSession.Roster.GrpList);
  removeSpecialGroups(cboGroup.Items);
  cboGroup.ItemIndex := max(0, cboGroup.Items.IndexOf(MainSession.Prefs.getString('roster_default')));
  cboType.ItemIndex := 0;
  txtGateway.Text := MainSession.Server;
end;

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

procedure TfrmAdd.txtJIDExit(Sender: TObject);
var
  tmp_id: TJabberID;
begin
    // add the nickname if it's not there.
  if (txtNickname.Text = '') then begin
    tmp_id := TJabberID.Create(txtJID.Text);
    txtNickname.Text := tmp_id.user;
    tmp_id.Free();
  end;
end;

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

procedure TfrmAdd.cboTypeChange(Sender: TObject);
var
  en: boolean;
begin
    //
  en := (cboType.ItemIndex > 0);
  lblGateway.Enabled := en;
  txtGateway.Enabled := en;
end;

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

procedure TfrmAdd.doAdd;
var
  a: TAgentItem;
  j: string;
  i: integer;
  tmps: string;
begin
    // check to see if there is an agent for this type
    // of contact type
{
  if (svc = 'anyq') then begin
    MainSession.Roster.AddItem(sjid, snick, sgrp, true);
    self.Enabled := false;
//        Self.Close;
  end
  else begin
    a := agents.findService(Lowercase(svc));
    if (a <> nil) then begin
            // we have this type of svc..
      j := '';
      for i := 1 to length(sjid) do begin
        if (sjid[i] = '@') then
          j := j + '%'
        else
          j := j + sjid[i];
      end;
      sjid := j + '@' + a.jid;
      ExRegController.MonitorJid(sjid, false);
      MainSession.Roster.AddItem(sjid, snick, sgrp, true);
      self.Enabled := false;
//            Self.Close;
    end
    else begin
            // we don't have this svc..
      ShowRiserWindow(nil, sNoGatewayFound, ico_info);
            //MessageBox(Handle, Pchar(sNoGatewayFound), '错误', MB_OK OR MB_ICONERROR);
      Self.Show();
    end;
  end;
{}
  tmps := MainSession.generateID();
  iq := TJabberIQ.Create(MainSession, tmps, vcard);
  iq.Namespace := 'vcard-temp';
  iq.qTag.Name := 'vCard';
  iq.iqType := 'get';
  iq.toJid := sjid;
  iq.Send;
end;

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

procedure TfrmAdd.agentsCallback(event: string; tag: TXMLTag);
begin
    // we are getting some kind of agents list
  if (tag.GetAttribute('from') = gw) then begin
    MainSession.UnRegisterCallback(cb);
    doAdd();
  end;
end;


procedure TfrmAdd.vcard(event: string; tag: TXMLTag);
var
  a: TAgentItem;
  j: string;
  i: integer;
  tmps: string;

  vcard: TXMLVCard;
  sNick: widestring;
  smobile: string;
begin
    // callback for vcard info
  iq := nil;
//    aniProfile.Visible := false;
//    aniProfile.Active := false;

  if (event <> 'xml') then exit;
  vcard := TXMLVCard.Create;
  vcard.parse(tag);

  with vcard do begin
    sNick := nick;
    if sNick = '' then
      sNick := FullName;
    if email <> '' then
      ForceAddrBook(sjid, nick, email);
    smobile := WorkCell.number;
  end;

  vcard.Free();

    // check to see if there is an agent for this type
    // of contact type

  if (svc = 'anyq') then begin
    MainSession.Roster.AddItem(sjid, snick, sgrp, true, smobile);
    self.Enabled := false;
//        Self.Close;
  end
  else begin
    a := agents.findService(Lowercase(svc));
    if (a <> nil) then begin
            // we have this type of svc..
      j := '';
      for i := 1 to length(sjid) do begin
        if (sjid[i] = '@') then
          j := j + '%'
        else
          j := j + sjid[i];
      end;
      sjid := j + '@' + a.jid;
      ExRegController.MonitorJid(sjid, false);
      MainSession.Roster.AddItem(sjid, snick, sgrp, true, smobile);
      self.Enabled := false;
//            Self.Close;
    end
    else begin
            // we don't have this svc..
      ShowRiserWindow(nil, sNoGatewayFound, ico_info);
            //MessageBox(Handle, Pchar(sNoGatewayFound), '错误', MB_OK OR MB_ICONERROR);
      Self.Show();
      exit;
    end;
  end;
{}

  Close;
end;

end.
