unit FindRoom;

interface

uses
  IQ, JabberConst, JabberID, Dockable,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, Menus, XMLTAG;

type
  TfmFindRoom = class(TfrmDockable)
    lvResult: TListView;
    Panel1: TPanel;
    PopupMenu1: TPopupMenu;
    Bevel1: TBevel;
    lbledtRoomName: TLabeledEdit;
    lbledtSubject: TLabeledEdit;
    lbledtServer: TLabeledEdit;
    btnFind: TButton;
    btnJoin: TButton;
    btnCancel: TButton;
    Animate1: TAnimate;
    Label1: TLabel;
    procedure btnCancelClick(Sender: TObject);
    procedure btnFindClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnJoinClick(Sender: TObject);
  private
    { Private declarations }
    _RoomName: string;
    _Subject: string;
    _Server: string;
    _iq: TJabberIQ;

    procedure FindCallback(event: string; tag: TXMLTag);
    procedure ClearItems;
    procedure ShowFound(tag: TXMLTag);
  public
    { Public declarations }
    procedure DoFind(jid: string);
  end;

var
  fmFindRoom: TfmFindRoom;

procedure ShowFindRoom;
procedure ShowJoinRoom;

implementation

uses Session, JoinRoom, Room, Agents, RiserWindow, Jabber1;

{$R *.dfm}

procedure ShowFindRoom;
var
  f: TfmFindRoom;
  i: Integer;
  agents: TAgents;
  a: TAgentItem;
begin
  f := TfmFindRoom.Create(Application);
  f.lbledtServer.Text := 'conference.' + MainSession.Server;
  {
  agents := MainSession.MyAgents;
  for i := 0 to agents.Count -1 do begin
    a := agents.getAgent(i);
    if (a.groupchat) then
    begin
      f.lbledtServer.Text := a.jid;
      break;
    end;
  end;
  {}
  f.ShowModal;
end;

procedure ShowJoinRoom;
var
  f: TfmFindRoom;
  i: Integer;
  agents: TAgents;
  a: TAgentItem;
begin
  f := TfmFindRoom.Create(Application);
  f.lbledtServer.Text := 'conference.' + MainSession.Server;
  f.Caption := '加入会议';
  f.Label1.Caption := '请从下面会议列表中选择加入您要参加的会议，也可以输入查询条件缩小您的选择范围。';
  f.btnFind.Click;
  {
  agents := MainSession.MyAgents;
  for i := 0 to agents.Count -1 do begin
    a := agents.getAgent(i);
    if (a.groupchat) then
    begin
      f.lbledtServer.Text := a.jid;
      break;
    end;
  end;
  {}
  f.ShowModal;
end;

procedure TfmFindRoom.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfmFindRoom.btnFindClick(Sender: TObject);
begin
  ClearItems;
  _RoomName := lbledtRoomName.Text;
  _Subject := lbledtSubject.Text;
  _Server := lbledtServer.Text;
  btnFind.Enabled := false;
  Animate1.Active := true;
  DoFind(_Server);
end;

procedure TfmFindRoom.DoFind(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 TfmFindRoom.FindCallback(event: string; tag: TXMLTag);
var
  i: integer;
  dup, ptag: TXMLTag;
  clist: TXMLTagList;
  jid: string;
begin
  Animate1.Active := false;

    // 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
        ShowFound(clist[i]);
    end;

  end
  else begin
        // probably a timeout
  end;

  _iq := nil;
  btnFind.Enabled := true;
  btnJoin.Enabled := lvResult.Items.Count > 0;
end;

procedure TfmFindRoom.ClearItems;
begin
  lvResult.Clear;
end;

procedure TfmFindRoom.FormCreate(Sender: TObject);
begin
  _iq := nil;
end;

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

  Action := caFree;
end;

procedure TfmFindRoom.ShowFound(tag: TXMLTag);
var
  stype, jid, name: string;
begin
  jid := tag.getAttribute('jid');
  name := tag.getAttribute('name');
  stype := tag.GetAttribute('type');
  if (lbledtRoomName.Text <> '') and (pos(lbledtRoomName.Text, name) = 0) then
    exit;
  with lvResult.Items.Add do
  begin
    Caption := name;
    SubItems.Add(Jid);
    SubItems.Add(sType);
  end;
end;

procedure TfmFindRoom.btnJoinClick(Sender: TObject);
var
  RID: TJabberID;
begin
  if lvResult.Selected = nil then
  begin
    ShowRiserWindow(nil, '请选择要加入的会议！', ico_info);
    //MessageBox(handle, '请选择要加入的会议！', '错误',MB_OK or MB_ICONERROR);
    exit;
  end;
  RID := TJabberID.Create(lvResult.Selected.SubItems[0]);
  StartJoinRoom(RID, '', '');
//  StartRoom(RID.jid, MainSession.Username, '');
end;

end.
