using agsXMPP;
using agsXMPP.protocol;
using agsXMPP.protocol.client;
using agsXMPP.protocol.extensions.chatstates;
using agsXMPP.util;
namespace MiniClient
{
/// <summary>
///
/// </summary>
public class frmChat
: System.
Windows.
Forms.
Form
{
private IContainer components;
private XmppClientConnection _connection;
private Jid m_Jid;
private System.
Windows.
Forms.
RichTextBox rtfSend
;
private System.
Windows.
Forms.
Splitter splitter1
;
private System.
Windows.
Forms.
RichTextBox rtfChat
;
private string _nickname;
private DateTime dt = DateTime.Now;
private PictureBox pictureBox1;
private StatusBar statusBar1;
private Label chatstatemsg
;
internal
Timer timerChat
;
private bool _isMinimize;
#region Public Property
private String _SendLastMessageDate
;
public String SendLastMessageDate
{
get { return _SendLastMessageDate; }
set { _SendLastMessageDate = value; }
}
private String _SendLastMessageTime
;
public String SendLastMessageTime
{
get { return _SendLastMessageTime; }
set { _SendLastMessageTime = value; }
}
private String _StatusMessage
;
{
get { return _StatusMessage; }
set { _StatusMessage = value; }
}
private String _ReceiveLastMessageDate
;
public String ReceiveLastMessageDate
{
get { return _ReceiveLastMessageDate; }
set { _ReceiveLastMessageDate = value; }
}
private String _ReceiveLastMessageTime
;
public String ReceiveLastMessageTime
{
get { return _ReceiveLastMessageTime; }
set { _ReceiveLastMessageTime = value; }
}
private String _messageThreadID
;
{
get { return _messageThreadID; }
set { _messageThreadID = value; }
}
private bool _isChatStateActive;
public bool IsChatStateActive
{
get { return _isChatStateActive; }
set { _isChatStateActive = value; }
}
private bool _HasReceiveChatStateActivation;
public bool HasReceiveChatStateActivation
{
get { return _HasReceiveChatStateActivation; }
set { _HasReceiveChatStateActivation = value; }
}
public Jid Jid
{
get { return m_Jid; }
set { m_Jid = value; }
}
#endregion
public frmChat(Jid jid, XmppClientConnection con, string nickname)
{
m_Jid = jid;
_connection = con;
_nickname = nickname;
InitializeComponent();
this.Text = "Chat with " + nickname;
Util.
ChatForms.
Add(m_Jid.
Bare.
ToLower(),
this);
IsChatStateActive = false;
HasReceiveChatStateActivation = false;
MyLastChatState
= String.
Empty;
this.
chatstatemsg.
Text = String.
Empty;
ReceiveLastMessageDate = dt.ToString("D", DateTimeFormatInfo.InvariantInfo);
ReceiveLastMessageTime = dt.ToString("T", DateTimeFormatInfo.InvariantInfo);
SendLastMessageDate = dt.ToString("D", DateTimeFormatInfo.InvariantInfo);
SendLastMessageTime = dt.ToString("T", DateTimeFormatInfo.InvariantInfo);
// Setup new Message Callback
con.MesagageGrabber.Add(jid, new BareJidComparer(), new MessageCB(MessageCallback), null);
}
private void OutgoingMessage(agsXMPP.protocol.client.Message msg)
{
rtfChat.
SelectionColor = Color.
Blue;
rtfChat.AppendText("Me said: ");
rtfChat.
SelectionColor = Color.
Black;
rtfChat.AppendText(msg.Body);
// Scroll down
rtfChat.SelectionStart = this.rtfChat.Text.Length - 1;
rtfChat.ScrollToCaret();
rtfChat.AppendText("\r\n");
}
public void IncomingMessage(agsXMPP.protocol.client.Message msg)
{
#region JEP-0085 Contact's Client Sends Content Message Reply With <active/> Notification
/*
<message
from='juliet@capulet.com/balcony'
to='romeo@shakespeare.lit/orchard'
type='chat'>
<thread>act2scene2chat1</thread>
<body>
What man art thou that thus bescreen'd in night
So stumblest on my counsel?
</body>
<active xmlns='http://jabber.org/protocol/chatstates'/>
</message>
*/
#endregion
// Once receive message from Contact Client we set ChatState Active
if (!HasReceiveChatStateActivation)
{
IsChatStateActive = true;
HasReceiveChatStateActivation = true;
Debug.WriteLine("HasReceiveChatStateActivation");
}
rtfChat.
SelectionColor = Color.
Red;
rtfChat.AppendText(_nickname + " said: ");
rtfChat.
SelectionColor = Color.
Black;
rtfChat.AppendText(msg.Body);
// Scroll down
rtfChat.SelectionStart = this.rtfChat.Text.Length - 1;
rtfChat.ScrollToCaret();
rtfChat.AppendText("\r\n");
this.chatstatemsg.Text = StatusMessage;
}
private void cmdSend_Click
(object sender,
System.
EventArgs e
)
{
agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
if (!IsChatStateActive)
{
// ******
// Do you think this is the right way to generate the MsgThreadID?
// ******
_messageThreadID = Hash.Sha1Hash(Id.GetNextId() + _connection.MyJID.ToString() + m_Jid.ToString());
IsChatStateActive = true;
Debug.WriteLine("IsChatStateActive is FALSE. Create new MSGThreadID: " + MsgThreadID.ToString());
}
msg.Type = MessageType.chat;
msg.To = m_Jid;
msg.From = _connection.MyJID;
msg.Body = rtfSend.Text;
msg.Chatstate = Chatstate.active;
_connection.Send(msg);
OutgoingMessage(msg);
rtfSend.Text = "";
if (IsComposing)
{
IsComposing = false;
timerChat.Stop();
Debug.WriteLine("cmdSend_Click Send Paused");
}
}
private void MessageCallback(object sender, agsXMPP.protocol.client.Message msg, object data)
{
if (InvokeRequired)
{
// Windows Forms are not Thread Safe, we need to invoke this :(
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new MessageCB(MessageCallback), new object[] { sender, msg, data });
return;
}
if (msg.Body != null)
{
DateTime dt = DateTime.Now;
ReceiveLastMessageDate = dt.ToString("D", DateTimeFormatInfo.InvariantInfo);
ReceiveLastMessageTime = dt.ToString("T", DateTimeFormatInfo.InvariantInfo);
this.chatstatemsg.Text = "Last message received at " + ReceiveLastMessageTime + " on " + ReceiveLastMessageDate;
IncomingMessage(msg);
}
else if (msg.Body == null && msg.Chatstate == Chatstate.composing)
{
this.chatstatemsg.Text = _nickname + " is writing a message";
}
else if (msg.Body == null && msg.Chatstate == Chatstate.gone)
{
IsChatStateActive = false;
HasReceiveChatStateActivation = false;
Debug.WriteLine("------ Receive: Chatstate.gone ------\n");
}
else if (msg.Body == null && msg.Chatstate == Chatstate.paused)
{
this.chatstatemsg.Text = "Last message received at " + ReceiveLastMessageTime + " on " + ReceiveLastMessageDate;
}
else if (msg.Body == null && msg.Chatstate == Chatstate.inactive)
{
HasReceiveChatStateActivation = false;
Debug.WriteLine("*** Receive: Chatstate.inactive ***\n");
}
else if (msg.Body == null && msg.Chatstate == Chatstate.active)
{
HasReceiveChatStateActivation = true;
Debug.WriteLine("### Receive: Chatstate.active ###\n");
}
}