public void XmppCon_OnMessage(object sender, CSS.IM.XMPP.protocol.client.Message msg) { 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 Invoke(new OnMessageDelegate(XmppCon_OnMessage), new object[] { sender, msg }); return; } // Dont handle GroupChat Messages here, they have their own callbacks in the // GroupChat Form if (msg.Type == MessageType.groupchat) { //Debug.WriteLine("创建聊天室发送了消息:"+msg.Body); return; } if (msg.Type == MessageType.error) { //Handle errors here // we dont handle them in this example return; } //if (msg.Type == MessageType.normal) //{ // filename = msg.Body; // Debug.WriteLine(msg.ToString()); // return; //} // check for xData Message if (msg.HasTag(typeof(Data)))//如果是文件 { Element e = msg.SelectSingleElement(typeof(Data)); Data xdata = e as Data; if (xdata.Type == XDataFormType.form) { //frmXData fXData = new frmXData(xdata); //fXData.Text = "xData Form from " + msg.From.ToString(); //fXData.Show(); } } else if (msg.HasTag(typeof(CSS.IM.XMPP.protocol.extensions.ibb.Data))) { // ignore IBB messages return; } else { if (msg.Body != null) { if (msg.GetTag("subject") == "notify")//xu { filename = msg.Body; return; } if (msg.From.ToString() == msg.From.Server) { if (CSS.IM.UI.Util.Path.ReveiveSystemNotification)//是否接收服务器消息 { if (CSS.IM.UI.Util.Path.SystemSwitch) SoundPlayEx.MsgPlay(CSS.IM.UI.Util.Path.SystemPath); MessageBoxForm sBox = new MessageBoxForm("系统通知", msg); try { sBox.TopMost = true; sBox.Show(); } catch (Exception) { } string sqlstr = "insert into MessageLog (Belong,MessageType,MessageLog,[DateNow]) values ({0},{1},{2},{3})"; sqlstr = String.Format(sqlstr, "'" + XmppCon.MyJID.Bare.ToString() + "'", "'0'", "'" + msg.ToString() + "'", "'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'"); CSS.IM.Library.Data.OleDb.ExSQL(sqlstr); } } else { if (!Util.ChatForms.ContainsKey(msg.From.Bare))//查看聊天窗口是否已经打开了 { //RosterNode rn = rosterControl.GetRosterItem(msg.From); //string nick = msg.From.Bare; //if (rn != null) // nick = rn.Text; try { //string sqlstr = "insert into ChatMessageLog (Jid,[MessageLog],[DateNow])values ({0},{1},{2})"; //sqlstr = String.Format(sqlstr, // "'" + msg.From.Bare.ToString() + "'", // "'" + msg.ToString() + "'", // "'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'"); //CSS.IM.Library.Data.OleDb.ExSQL(sqlstr); if (msg.GetTagInt("m_type") == 0)//如果为0就是正常消息,就播放声音 { if (CSS.IM.UI.Util.Path.MsgSwitch) SoundPlayEx.MsgPlay(CSS.IM.UI.Util.Path.MsgPath); } else//代码进入到这里,说明,聊天的窗口已经关闭,接收到属于聊天窗口里面的业务不做处理 { //6发图片 //1接收视频 if (!(msg.GetTagInt("m_type")==6||msg.GetTagInt("m_type")==1)) { return; } } Friend flfriend = friendListView.Rosters[msg.From.User]; if (CSS.IM.UI.Util.Path.ChatOpen) { //friendListView.flickerFriend(new Jid(msg.From.Bare)); //ChatMessageBox.GetInstance(this).AddFriendMessage(msg.From, XmppCon, msg.From.Bare); //timer_notifyIco.Enabled = true; ChatForm chatForm = new ChatForm(msg.From, XmppCon, msg.From.Bare); try { chatForm.UpdateFriendOnline(flfriend.IsOnline);//设置好友在线状态 chatForm.Show(); chatForm.Activate(); chatForm.IncomingMessage(msg); } catch (Exception) { } } else { if (msgBox.ContainsKey(msg.From.Bare.ToString())) { List<CSS.IM.XMPP.protocol.client.Message> msgs = msgBox[msg.From.Bare.ToString()]; msgs.Add(msg); msgBox.Remove(msg.From.Bare.ToString()); msgBox.Add(msg.From.Bare.ToString(), msgs); friendListView.flickerFriend(new Jid(msg.From.Bare)); } else { if (friendListView.Rosters.ContainsKey(msg.From.User)) { List<CSS.IM.XMPP.protocol.client.Message> msgs = new List<XMPP.protocol.client.Message>(); msgs.Add(msg); msgBox.Add(msg.From.Bare.ToString(), msgs); friendListView.flickerFriend(new Jid(msg.From.Bare)); ChatMessageBox.GetInstance(this).AddFriendMessage(msg.From, XmppCon, msg.From.Bare); timer_notifyIco.Enabled = true; } else { ChatForm chatForm = new ChatForm(msg.From, XmppCon, msg.From.Bare); try { chatForm.UpdateFriendOnline(flfriend.IsOnline);//设置好友在线状态 chatForm.Show(); chatForm.Activate(); chatForm.IncomingMessage(msg); } catch (Exception) { } } //Friend item=friendListView.Rosters[msg.From.Bare.ToString()]; } } //ChatForm chatForm = new ChatForm(msg.From, XmppCon, msg.From.Bare); //chatForm.Show(); //chatForm.IncomingMessage(msg); } catch (Exception) { //MessageBox.Show(ex.Message); } } } } } }
/// <summary> /// 聊天窗口打开事件 /// </summary> /// <param name="sender"></param> public void friendListView_OpenChatEvent(Friend sender) { 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 QQListViewEx.delegate_openChat(friendListView_OpenChatEvent), new object[] { sender }); return; } if (!Util.ChatForms.ContainsKey(sender.Ritem.Jid.ToString())) { try { ChatForm chat = new ChatForm(sender.Ritem.Jid, XmppCon, sender.NikeName); chat.UpdateFriendOnline(sender.IsOnline);//设置好友在线状态 ChatMessageBox.GetInstance(this).RemoveFriend(sender.Ritem.Jid); if (msgBox.ContainsKey(sender.Ritem.Jid.ToString())) { chat.FristMessage(msgBox[sender.Ritem.Jid.ToString()]); msgBox.Remove(sender.Ritem.Jid.ToString()); } chat.Show(); if (ChatMessageBox.GetInstance(this).FrienMessageCount() > 0) Debug.WriteLine("?"); else { notifyIcon1.Icon = CSS.IM.UI.Properties.Resources.Iico; timer_notifyIco.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { try { ChatForm chatform = Util.ChatForms[sender.Ritem.Jid.ToString()] as ChatForm; chatform.WindowState = FormWindowState.Normal; chatform.Activate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/// <summary> /// 通过消息盒子打开消息窗体 /// </summary> /// <param name="friend"></param> /// <param name="sender"></param> /// <param name="CName"></param> void QQMainForm_OpenChatEvent(Friend friend, Jid sender, string CName) { RosterItem ritem = new RosterItem(sender); friend.Ritem = ritem; //throw new NotImplementedException(); //friendListView_OpenChatEvent(friend); if (!Util.ChatForms.ContainsKey(friend.Ritem.Jid.Bare.ToString())) { friendListView.UpdateFriendFlicker(friend.NikeName); ChatForm chat = new ChatForm(friend.Ritem.Jid, XmppCon, friend.NikeName); Friend flfriend = friendListView.Rosters[friend.NikeName]; if (flfriend!=null) { chat.UpdateFriendOnline(flfriend.IsOnline); } if (msgBox.ContainsKey(friend.Ritem.Jid.Bare.ToString())) { chat.FristMessage(msgBox[friend.Ritem.Jid.Bare.ToString()]); msgBox.Remove(friend.Ritem.Jid.Bare.ToString()); } chat.Show(); } else { try { ChatForm chatform = Util.ChatForms[friend.Ritem.Jid.ToString()] as ChatForm; chatform.WindowState = FormWindowState.Normal; chatform.Activate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } if (ChatMessageBox.GetInstance(this).FrienMessageCount() > 0) Debug.WriteLine("?"); else { notifyIcon1.Icon = CSS.IM.UI.Properties.Resources.Iico; timer_notifyIco.Enabled = false; } }
private void tsmi发送消息_Click(object sender, EventArgs e) { if (treeView1.SelectedNode == null) return; if (treeView1.SelectedNode.Tag == null) return; Jid j = new Jid(treeView1.SelectedNode.Tag.ToString(), XmppCon.Server, null); if (!Util.ChatForms.ContainsKey(j.ToString())) { friendListView.UpdateFriendFlicker(j.User); ChatForm chat = new ChatForm(j, XmppCon, j.User); Friend friend = friendListView.Rosters[j.User]; if (friend!=null) { chat.UpdateFriendOnline(friendListView.Rosters[j.User].IsOnline); } if (msgBox.ContainsKey(j.Bare.ToString())) { chat.FristMessage(msgBox[j.Bare.ToString()]); msgBox.Remove(j.Bare.ToString()); } try { chat.Show(); } catch (Exception) { } } }
/// <summary> /// 查看联系人详细信息事件、删除、备注 /// </summary> /// <param name="sender"></param> /// <param name="item"></param> public void friendListView_friend_qcm_MouseClickEvent(object sender, Friend item, String type) { CSS.IM.XMPP.protocol.iq.roster.RosterItem ritem = friendListView.SelectedFriend.Ritem; switch (type) { case "vcar": VcardForm vcardForm = new VcardForm(ritem.Jid, XmppCon); try { vcardForm.Show(); } catch (Exception) { } break; case "chat": if (!Util.ChatForms.ContainsKey(item.Ritem.Jid.ToString())) { try { ChatForm chat = new ChatForm(item.Ritem.Jid, XmppCon, item.NikeName); chat.UpdateFriendOnline(item.IsOnline);//设置好友在线状态 chat.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { try { ChatForm chatform = Util.ChatForms[item.Ritem.Jid.ToString()] as ChatForm; chatform.WindowState = FormWindowState.Normal; chatform.Activate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } break; case "dele": Jid jid = item.Ritem.Jid; Friend friend = new Friend(); friend.NikeName = item.Ritem.Jid.User; friend.Description = "";//心情 friend.HeadIMG = "big1"; friend.IsSysHead = true; friend.State = 1; if (item.Ritem.GetGroups().Count > 0) { CSS.IM.XMPP.protocol.Base.Group g = (CSS.IM.XMPP.protocol.Base.Group)item.Ritem.GetGroups().Item(0); int groupID = 0; foreach (var groups in friendListView.Groups) { if (groups.Value.Title == g.Name) { groupID = groups.Value.Id; } } //if (groupID == 0) //{ // friendListView.AddGroup(g.Name); // groupID = friendListView.Groups[g.Name].Id; // friendListView.UpdateLayout(3, groupID); //} friend.GroupID = groupID; friend.GroupName = g.Name; } else { //离线联系人 Group group = friendListView.Groups["我的联系人"]; friend.GroupID = group.Id; friend.GroupName = group.Title; } friend.Ritem = item.Ritem; friend.IsOnline = false; DialogResult msgResult=MsgBox.Show(this, "CSS&IM", "确认要删除联系人么?", MessageBoxButtons.YesNo); if (msgResult==DialogResult.Yes) { friendListView.RemoveFriend(friend); try { RosterIq riq = new RosterIq(); riq.Type = IqType.set; XmppCon.RosterManager.RemoveRosterItem(jid); } catch (Exception ex) { throw ex; } } break; case "move": MoveFriendGroup _MoveFriendGroup = new MoveFriendGroup(); Dictionary<string, Group> group_args = friendListView.Groups; string[] strvalue = new string[group_args.Count]; int index = 0; foreach (String keystr in group_args.Keys) { Group groupargs = group_args[keystr]; strvalue[index] = groupargs.Title; index++; } _MoveFriendGroup.basicComboBox1.Items = strvalue; _MoveFriendGroup.basicComboBox1.SelectIndex = 0; DialogResult reslut = _MoveFriendGroup.ShowDialog(); String groupupdate = _MoveFriendGroup.basicComboBox1.SelectItem.ToString(); String name_move = friendListView.SelectedFriend.Ritem.Jid.User; if (reslut == DialogResult.Yes) { foreach (String user_key in friendListView.Rosters.Keys) { if (friendListView.Rosters[user_key].Ritem.Jid.User == name_move) { Friend friend_old = friendListView.Rosters[user_key]; friendListView.RemoveFriend(friendListView.Rosters[user_key].Ritem.Jid.User); Group fgroup = friendListView.Groups[groupupdate]; friend_old.GroupID = fgroup.Id; friend_old.GroupName = fgroup.Title; friendListView.AddFriend(friend_old); friendListView.UpdateLayout(3, 0); break; } } CSS.IM.XMPP.protocol.Base.Group group_move = new CSS.IM.XMPP.protocol.Base.Group(groupupdate); CSS.IM.XMPP.protocol.Base.Item item_move = new CSS.IM.XMPP.protocol.Base.Item(); item_move.Namespace = null; item_move.AddChild(group_move); item_move.SetAttribute("jid", friendListView.SelectedFriend.Ritem.Jid); item_move.SetAttribute("subscripton", "from"); CSS.IM.XMPP.protocol.Base.Query query_move = new CSS.IM.XMPP.protocol.Base.Query(); query_move.Namespace = CSS.IM.XMPP.Uri.IQ_ROSTER; query_move.AddChild(item_move); IQ iq = new IQ(IqType.set); iq.GenerateId(); iq.Namespace = null; iq.AddChild(query_move); XmppCon.IqGrabber.SendIq(iq); } break; case "g_dele": break; case "remark": //RemarkFriendGroup _RemarkFriendGroup = new RemarkFriendGroup(); //Dictionary<string, Group> group_args = friendListView.Groups; //string[] strvalue = new string[group_args.Count]; //int index = 0; //foreach (String keystr in group_args.Keys) //{ // Group groupargs = group_args[keystr]; // strvalue[index] = groupargs.Title; // index++; //} //_MoveFriendGroup.basicComboBox1.Items = strvalue; //_MoveFriendGroup.basicComboBox1.SelectIndex = 0; //DialogResult reslut = _MoveFriendGroup.ShowDialog(); //String groupupdate = _MoveFriendGroup.basicComboBox1.SelectItem.ToString(); //String name_move = friendListView.SelectedFriend.Ritem.Jid.User; //if (reslut == DialogResult.Yes) //{ // foreach (String user_key in friendListView.Rosters.Keys) // { // if (friendListView.Rosters[user_key].Ritem.Jid.User == name_move) // { // Friend friend_old = friendListView.Rosters[user_key]; // friendListView.RemoveFriend(friendListView.Rosters[user_key].Ritem.Jid.User); // Group fgroup = friendListView.Groups[groupupdate]; // friend_old.GroupID = fgroup.Id; // friend_old.GroupName = fgroup.Title; // friendListView.AddFriend(friend_old); // friendListView.UpdateLayout(3, 0); // break; // } // } // CSS.IM.XMPP.protocol.Base.Group group_move = new CSS.IM.XMPP.protocol.Base.Group(groupupdate); // CSS.IM.XMPP.protocol.Base.Item item_move = new CSS.IM.XMPP.protocol.Base.Item(); // item_move.Namespace = null; // item_move.AddChild(group_move); // item_move.SetAttribute("jid", friendListView.SelectedFriend.Ritem.Jid); // item_move.SetAttribute("subscripton", "from"); // CSS.IM.XMPP.protocol.Base.Query query_move = new CSS.IM.XMPP.protocol.Base.Query(); // query_move.Namespace = CSS.IM.XMPP.Uri.IQ_ROSTER; // query_move.AddChild(item_move); // IQ iq = new IQ(IqType.set); // iq.GenerateId(); // iq.Namespace = null; // iq.AddChild(query_move); // XmppCon.IqGrabber.SendIq(iq); break; default: break; } }