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

type
  TfrmJoinRoom = class(TAnyQBaseForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    txtRoom: TEdit;
    txtNick: TEdit;
    frameButtons1: TframeButtons;
    txtServer: TComboBox;
    txtPassword: TEdit;
    lblPassword: TLabel;
    procedure frameButtons1btnOKClick(Sender: TObject);
    procedure frameButtons1btnCancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    _rjid: string;
    _iq: TJabberIQ;
    procedure FindCallback(event: string; tag: TXMLTag);
    procedure JoinRoom;
  public
    { Public declarations }
    procedure DoFind(const jid: string);
  end;

var
  frmJoinRoom: TfrmJoinRoom;

procedure StartJoinRoom; overload;
procedure StartJoinRoom(room_jid: TJabberID; nick, password: WideString); overload;

resourcestring
  sInvalidNick = '请务必输入昵称';

implementation
uses
  GnuGetText, Jabber1, Agents, Session, Room, JabberConst, RiserWindow;
{$R *.DFM}

procedure StartJoinRoom;
var
  f: TfrmJoinRoom;
  i: integer;
  agents: TAgents;
  a: TAgentItem;
begin
  f := TfrmJoinRoom.Create(Application);
  with f do begin
    txtRoom.Text := MainSession.Prefs.getString('tc_lastroom');
    txtServer.Text := MainSession.Prefs.getString('tc_lastserver');
    txtNick.Text := MainSession.Prefs.getString('tc_lastnick');
    if (txtNick.Text = '') then
      txtNick.Text := MainSession.Username;

    with txtServer do begin
      Items.Clear;
      agents := MainSession.MyAgents;
      for i := 0 to agents.Count - 1 do begin
        a := agents.getAgent(i);
        if (a.groupchat) then
          Items.Add(a.jid);
      end;
    end;
    Show;
  end;
end;

procedure StartJoinRoom(room_jid: TJabberID; nick, password: WideString); overload;
var
  f: TfrmJoinRoom;
  i: integer;
  agents: TAgents;
  a: TAgentItem;
begin
  f := TfrmJoinRoom.Create(Application);
  with f do begin
    txtRoom.Text := room_jid.user;
    txtServer.Text := room_jid.domain;
    txtNick.Text := nick;

    if (txtNick.Text = '') then
      txtNick.Text := MainSession.Username;

    with txtServer do begin
      Items.Clear;
      agents := MainSession.MyAgents;
      for i := 0 to agents.Count - 1 do begin
        a := agents.getAgent(i);
        if (a.groupchat) then
          Items.Add(a.jid);
      end;
    end;
    Show;
  end;
end;

procedure TfrmJoinRoom.frameButtons1btnOKClick(Sender: TObject);
begin
    // join this room
  _rjid := txtRoom.Text + '@' + txtServer.Text;
  if (not isValidJid(_rjid)) then begin
    ShowRiserWindow(nil, sInvalidRoomJID, ico_info);
        //MessageBox(Handle,Pchar(sInvalidRoomJID), '错误', MB_OK OR MB_ICONERROR);
    exit;
  end;

  if (txtNick.Text = '') then begin
    ShowRiserWindow(nil, sInvalidNick, ico_info);
        //MessageBox(Handle,Pchar(sInvalidNick), '错误', MB_OK OR MB_ICONERROR);
    exit;
  end;

  DoFind(txtServer.Text);
//    Self.Close;
end;

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

procedure TfrmJoinRoom.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if (_iq <> nil) then
    _iq.Free();
  Action := caFree;
end;

procedure TfrmJoinRoom.FormCreate(Sender: TObject);
begin
  TranslateProperties(Self);
  _iq := nil;
end;

procedure TfrmJoinRoom.DoFind(const jid: string);
var
  id: string;
  i: integer;
begin
  id := MainSession.generateID();
  _iq := TJabberIQ.Create(MainSession, id, FindCallback, 30);
  with _iq do begin
    Namespace := XMLNS_BROWSE;
    iqType := 'get';
    toJid := jid;
    Send();
  end;
end;

procedure TfrmJoinRoom.FindCallback(event: string; tag: TXMLTag);
var
  i: integer;
  dup, ptag: TXMLTag;
  clist: TXMLTagList;
  jid: string;
begin
    // IQ Callback for current query
  if ((event = 'xml') or (event = 'cache')) then begin
    if (tag.GetAttribute('type') = 'error') then begin
      ShowRiserWindow(nil, '查找会议时出错！', ico_info);
            //MessageBox(handle, '查找会议时出错！', '错误',MB_OK or MB_ICONERROR);
      _iq := nil;
      exit;
    end;

        // we have some kind of result
    clist := tag.ChildTags();
    if (clist.Count <= 0) then begin
      ShowRiserWindow(nil, '查找会议时出错！', ico_info);
            //MessageBox(handle, '查找会议时出错！', '错误',MB_OK or MB_ICONERROR);
      _iq := nil;
      exit;
    end;

    ptag := clist[0];
    clist.Free();

        // show the main parent details
    clist := ptag.ChildTags();
    for i := 0 to clist.Count - 1 do begin
      if (clist[i].Name <> 'ns') then
        if clist[i].getAttribute('jid') = _rjid then
        begin
          _iq := nil;
          JoinRoom;
          exit;
        end;
    end;
    ShowRiserWindow(nil, '没有找到会议！', ico_info);
        //MessageBox(handle, '没有找到会议！', '错误',MB_OK or MB_ICONERROR);
    _iq := nil;
    exit;
  end
  else begin
        // probably a timeout
  end;
  _iq := nil;
end;

procedure TfrmJoinRoom.JoinRoom;
var
  pass: WideString;
begin
  pass := Trim(txtPassword.Text);
  StartRoom(_rjid, txtNick.Text, pass);

  with MainSession.Prefs do begin
    setString('tc_lastroom', txtRoom.Text);
    setString('tc_lastserver', txtServer.Text);
    setString('tc_lastnick', txtNick.Text);
  end;
  Close;
end;

end.
