Thu, 26 Dec 2024 17:19:15 +0100
update status in contact list and conversations
package de.unixwork.im; import javax.swing.*; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.roster.RosterEntry; import org.jxmpp.jid.Jid; import org.jxmpp.jid.parts.Resourcepart; public class App { private static App instance; private final ContactListFrame contactListFrame; private final Map<String, ConversationFrame> conversations; private final Map<String, PresenceInfo> presence = new HashMap<>(32); private final Xmpp xmpp; public App(Xmpp xmpp) throws Exception { if(instance != null) { throw new Exception("App already initilized"); } App.instance = this; conversations = new HashMap<>(); this.xmpp = xmpp; // Create the contact list window contactListFrame = new ContactListFrame(); contactListFrame.setContactClickListener(contact -> { openConversation(contact.getJid().asUnescapedString()); }); contactListFrame.setVisible(true); } public static App getInstance() { return instance; } public Xmpp getXmpp() { return xmpp; } // Method to open a conversation window public void openConversation(String xid) { SwingUtilities.invokeLater(() -> { ConversationFrame conversationFrame = conversations.get(xid); if (conversationFrame == null) { conversationFrame = new ConversationFrame(xid); conversations.put(xid, conversationFrame); conversationFrame.setVisible(true); } else { conversationFrame.setVisible(true); conversationFrame.toFront(); } }); } public void dispatchMessage(Jid from, String msg, boolean secure) { SwingUtilities.invokeLater(() -> { // add message to the correct conversation // if no conversation exists yet, create a new window String xid = from.asBareJid().toString(); //String resource = from.getResourceOrNull(); ConversationFrame conversation = conversations.get(xid); if(conversation == null) { conversation = new ConversationFrame(xid); conversations.put(xid, conversation); } conversation.addToLog(msg, true, false); conversation.setVisible(true); }); } public void setContacts(List<RosterEntry> contacts) { SwingUtilities.invokeLater(() -> { contactListFrame.setContacts(contacts); }); } public PresenceInfo getPresenceForXID(String xid) { return presence.get(xid); } public String getStatusForXID(String xid) { PresenceInfo ps = getPresenceForXID(xid); if(ps == null) { return "<offline> "; } return ps.getOnlineStatus(); } void handlePresence(Jid from, Presence.Type type) { System.out.println("presence from: " + from.toString() + " type: " + type.toString()); SwingUtilities.invokeLater(() -> { String xid = from.asBareJid().toString(); PresenceInfo ps = presence.get(xid); if(ps == null) { ps = new PresenceInfo(); presence.put(xid, ps); } // update presence Resourcepart resource = from.getResourceOrNull(); if(resource != null) { ps.setStatus(resource.toString(), type); } else { // TODO } contactListFrame.reload(); ConversationFrame conversation = conversations.get(xid); if(conversation != null) { conversation.updatePresence(ps); } }); } }