protected void _openConversation(object jid, bool stealFocus, bool onTop) { if (!this.ChatWindows.ContainsKey(jid.ToString())) { ChatWindow c = new ChatWindow(jid.ToString(), stealFocus, onTop); c.TopMost = false; this.ChatWindows.Add(jid.ToString(), c);//create } else if (this.ChatWindows[jid.ToString()].IsDisposed) { ChatWindow c = new ChatWindow(jid.ToString(), stealFocus, onTop); c.TopMost = false; this.ChatWindows[jid.ToString()] = c;//renew } else { this.ChatWindows[jid.ToString()].DoActivate(); return; } try { Application.Run(this.ChatWindows[jid.ToString()]); } catch (Exception e) { throw e; } }
protected void ProcessMessages(ProtocolTreeNode[] nodes) { foreach (ProtocolTreeNode node in nodes) { if (node.tag.Equals("message")) { ProtocolTreeNode body = node.GetChild("body"); ProtocolTreeNode media = node.GetChild("media"); ProtocolTreeNode paused = node.GetChild("paused"); ProtocolTreeNode composing = node.GetChild("composing"); ProtocolTreeNode notification = node.GetChild("notification"); string jid = node.GetAttribute("from"); if (body != null || media != null) { //extract and save nickname if (node.GetChild("notify") != null && node.GetChild("notify").GetAttribute("name") != null) { string nick = node.GetChild("notify").GetAttribute("name"); Contact c = ContactStore.GetContactByJid(jid); if (c != null) { c.nickname = nick; ContactStore.UpdateNickname(c); } } try { this.getChat(jid, true, false).AddMessage(node); } catch (Exception) { } //refresh list this._loadConversations(); } if (paused != null) { try { if (this.getChat(jid, false, false) != null) { this.getChat(jid, false, false).SetOnline(); } } catch (Exception e) { //throw e; } } if (composing != null) { try { if (this.getChat(jid, false, false) != null) { this.getChat(jid, false, false).SetTyping(); } } catch (Exception e) { //throw e; } } if (notification != null) { if (notification.GetAttribute("type") == "picture") { ChatWindow.GetImageAsync(notification.GetChild("set").GetAttribute("jid"), false); } } } else if (node.tag.Equals("presence")) { string jid = node.GetAttribute("from"); if (node.GetAttribute("type") != null && node.GetAttribute("type").Equals("available")) { try { if (this.getChat(jid, false, false) != null) { this.getChat(jid, false, false).SetOnline(); } } catch (Exception e) { //throw e; } } if (node.GetAttribute("type") != null && node.GetAttribute("type").Equals("unavailable")) { try { if (this.getChat(jid, false, false) != null) { this.getChat(jid, false, false).SetUnavailable(); } } catch (Exception e) { //throw e; } } } else if (node.tag.Equals("iq")) { string jid = node.GetAttribute("from"); if (node.children.First().tag.Equals("query")) { DateTime lastseen = DateTime.Now; int seconds = Int32.Parse(node.GetChild("query").GetAttribute("seconds")); lastseen = lastseen.Subtract(new TimeSpan(0, 0, seconds)); try { if (this.getChat(jid, false, false) != null) { getChat(jid, false, false).SetLastSeen(lastseen); } } catch (Exception e) { //throw e; } } else if (node.children.First().tag.Equals("group")) { string subject = node.children.First().GetAttribute("subject"); Contact cont = ContactStore.GetContactByJid(jid); if (cont != null) { cont.nickname = subject; ContactStore.UpdateNickname(cont); } else { } } else if (node.children.First().tag.Equals("picture")) { string pjid = node.GetAttribute("from"); string id = node.GetAttribute("id"); byte[] rawpicture = node.GetChild("picture").GetData(); Contact c = ContactStore.GetContactByJid(pjid); if (c != null) { Image img = null; using (var ms = new MemoryStream(rawpicture)) { try { img = Image.FromStream(ms); string targetdir = Directory.GetCurrentDirectory() + "\\data\\profilecache"; if (!Directory.Exists(targetdir)) { Directory.CreateDirectory(targetdir); } img.Save(targetdir + "\\" + c.jid + ".jpg"); try { if (this.getChat(pjid, false, false) != null) { this.getChat(pjid, false, false).SetPicture(img); } } catch (Exception e) { throw e; } } catch (Exception e) { throw e; } } } if (picturesToSync.Remove(pjid)) { this.requestProfilePicture(); } } else if (node.children.First().tag.Equals("error") && node.children.First().GetAttribute("code") == "404") { string pjid = node.GetAttribute("from"); picturesToSync.Remove(pjid); this.requestProfilePicture(); } } } }