unit newRoom;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, fRosterTree, StdCtrls, TntStdCtrls, Dockable, ExtCtrls;

type
  TfmNewRoom = class(TfrmDockable)
    GroupBox1: TGroupBox;
    Panel1: TPanel;
    Panel2: TPanel;
    frameTreeRoster1: TframeTreeRoster;
    lbledtRoom: TLabeledEdit;
    lbledtSubject: TLabeledEdit;
    Label1: TLabel;
    txtInviteMsg: TTntMemo;
    Button1: TButton;
    Button2: TButton;
    lbledtPass: TLabeledEdit;
    chkInviteOnly: TCheckBox;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure SendInvite;
  public
    { Public declarations }
  end;

var
  fmNewRoom: TfmNewRoom;

procedure ShowNewRoom;

implementation

uses gnugettext, JabberID, Session, JabberConst, Room, XMLTag, Roster,
  ExUtils, RiserWindow, Jabber1;

{$R *.dfm}

procedure ShowNewRoom;
var
  f: TfmNewRoom;
begin
  f := TfmNewRoom.create(Application);
  f.Show;
end;

procedure TfmNewRoom.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TfmNewRoom.FormCreate(Sender: TObject);
begin
  TranslateProperties(Self);
  frameTreeRoster1.Initialize();
  frameTreeRoster1.DrawRoster(false);
end;

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

procedure TfmNewRoom.Button1Click(Sender: TObject);
var
  pass: Widestring;
  rjid: string;
begin
    // join this room
  if trim(lblEdtRoom.Text) = '' then
  begin
    ShowRiserWindow(nil, '请务必输入会议名及会议主题。', ico_info);
    exit;
  end;
  if trim(lbledtSubject.Text) = '' then
  begin
    ShowRiserWindow(nil, '请务必输入会议主题。', ico_info);
    exit;
  end;
  rjid := lblEdtRoom.Text + '@' + 'conference.' + MainSession.Server;
  if (not isValidJid(rjid)) then begin
    ShowRiserWindow(nil, sInvalidRoomJID, ico_info);
        //MessageBox(handle,pchar(sInvalidRoomJID),'错误', MB_OK or MB_ICONERROR);
    exit;
  end;

  pass := Trim(lbledtPass.Text);
  StartRoomNew(rjid, lbledtSubject.Text, MainSession.Username, chkInviteOnly.Checked, pass);

  with MainSession.Prefs do begin
    setString('tc_lastroom', lbledtRoom.Text);
  end;

  SendInvite;
  Self.Close;
end;

procedure TfmNewRoom.SendInvite;
var
  frm: TfrmRoom;
  l: Tlist;
  i: integer;
  x, msg: TXMLTag;
  b, room: WideString;
  room_idx: integer;
begin
    // Make sure we are actually in this room...
  room := lblEdtRoom.Text + '@' + 'conference.' + MainSession.Server;

  if ((room = '') or (not isValidJID(room))) then begin
    ShowRiserWindow(nil, sInvalidRoomJID, ico_info);
        //MessageBox(handle,pchar(sInvalidRoomJID),'错误', MB_OK or MB_ICONERROR);
    exit;
  end;

  room_idx := room_list.IndexOf(room);
  if (room_idx < 0) then
    StartRoom(room, MainSession.Username);
  frm := FindRoom(room);

  l := frameTreeRoster1.GetCheckedList;
  for i := 0 to l.Count - 1 do begin
    msg := TXMLTag.Create('message');
    if ((frm <> nil) and (frm.isMUCRoom)) then begin
            // this is MUC.. use muc#user
      msg.setAttribute('to', room);
      x := msg.AddTag('x');
      with x do begin
        setAttribute('xmlns', xmlns_mucuser);
        with AddTag('invite') do begin
          setAttribute('to', TJabberRosterItem(L.Items[i]).jid.full);
          AddBasicTag('reason', txtInviteMsg.Lines.Text);
        end;
      end;
    end
    else begin
            // this is GC 1.0, or we aren't in the room yet..
            // Use jabber;x:conference
      msg.setAttribute('to', TJabberRosterItem(L.Items[i]).jid.full);
      x := msg.AddTag('x');
      with x do begin
        setAttribute('xmlns', 'jabber:x:conference');
        setAttribute('jid', room);
      end;
      b := txtInviteMsg.Lines.Text;
    end;
    jabberSendMsg(msg.getAttribute('to'), msg, x, b, '');
  end;
  l.Free;
  Self.Close;
end;

end.
