unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdException, ComCtrls, IdGlobal;

type
  TForm2 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    ed_HostIP: TEdit;
    ed_Port: TEdit;
    btn_ConnectDisConnect: TButton;
    IdTCPClient1: TIdTCPClient;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Memo_Histroy: TMemo;
    Panel2: TPanel;
    ed_ISay: TEdit;
    Label3: TLabel;
    btn_Send: TButton;
    Panel3: TPanel;
    Panel4: TPanel;
    GroupBox2: TGroupBox;
    LV_Users: TListView;
    procedure btn_ConnectDisConnectClick(Sender: TObject);
    procedure btn_SendClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure EnableAuthoring;
    procedure DisableAuthoring;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.btn_ConnectDisConnectClick(Sender: TObject);
var
   newName : string;
   userlist : string;
   LI : TListItem;
begin
   self.IdTCPClient1.Host := self.ed_HostIP.Text;
   self.IdTCPClient1.Port := StrToInt(self.ed_Port.Text);

   if self.IdTCPClient1.Connected then begin
      self.IdTCPClient1.Disconnect;
      self.btn_ConnectDisConnect.Caption := 'Connect';

      self.DisableAuthoring;
   end else begin
       self.IdTCPClient1.Connect;
      self.btn_ConnectDisConnect.Caption := 'Disonnect';

      newName := InputBox('Please select a name to be used as your name', 'name please:', newName);
      self.EnableAuthoring;
      self.IdTCPClient1.IOHandler.ReadLn();
      self.IdTCPClient1.IOHandler.Writeln('login ' + newName);
      self.IdTCPClient1.IOHandler.WriteLn('list');
      userlist := self.IdTCPClient1.IOHandler.ReadLn();

      repeat
         newName := Fetch(userlist,  #9);
         if newName <> '' then begin
            LI := Self.LV_Users.Items.Add;
            LI.Caption := newName;
         end;
      until (userlist = '');
   end;
end;

procedure TForm2.btn_SendClick(Sender: TObject);
var
   tmpStr : string;
begin
  if self.IdTCPClient1.Connected then begin
     //self.Memo_Histroy.Lines.Add('broadcast ' + self.ed_ISay.Text);
     self.IdTCPClient1.IOHandler.WriteLn('broadcast ' +self.ed_ISay.Text);
     //self.IdTCPClient1.IOHandler.ReadLnTimedout := true;
     tmpStr := self.IdTCPClient1.IOHandler.ReadLn();

     if tmpStr <> '' then begin
        //Fetch(tmpStr, ' ');
        self.Memo_Histroy.Lines.Add(tmpStr);
     end;
  end;
end;

procedure TForm2.DisableAuthoring;
begin
   self.GroupBox1.Enabled := False;
   self.Memo_Histroy.Enabled := False;
   self.Label3.Enabled := False;
   self.ed_ISay.Enabled := False;
   self.btn_Send.Enabled := False;
end;

procedure TForm2.EnableAuthoring;
begin
   self.GroupBox1.Enabled := True;
   self.Memo_Histroy.Enabled := True;
   self.Label3.Enabled := True;
   self.ed_ISay.Enabled := True;
   self.btn_Send.Enabled := True;
end;

end.
