Thu, 26 Dec 2024 17:19:15 +0100
update status in contact list and conversations
--- a/src/main/java/de/unixwork/im/App.java Thu Dec 26 12:53:02 2024 +0100 +++ b/src/main/java/de/unixwork/im/App.java Thu Dec 26 17:19:15 2024 +0100 @@ -46,12 +46,14 @@ // Method to open a conversation window public void openConversation(String xid) { SwingUtilities.invokeLater(() -> { - if (!conversations.containsKey(xid)) { - ConversationFrame conversationFrame = new ConversationFrame(xid); + ConversationFrame conversationFrame = conversations.get(xid); + if (conversationFrame == null) { + conversationFrame = new ConversationFrame(xid); conversations.put(xid, conversationFrame); conversationFrame.setVisible(true); } else { - conversations.get(xid).toFront(); + conversationFrame.setVisible(true); + conversationFrame.toFront(); } }); } @@ -80,13 +82,12 @@ }); } - // Method to perform actions in the GUI thread from other threads - public void runOnUiThread(Runnable action) { - SwingUtilities.invokeLater(action); + public PresenceInfo getPresenceForXID(String xid) { + return presence.get(xid); } - + public String getStatusForXID(String xid) { - PresenceInfo ps = presence.get(xid); + PresenceInfo ps = getPresenceForXID(xid); if(ps == null) { return "<offline> "; } @@ -96,19 +97,27 @@ void handlePresence(Jid from, Presence.Type type) { System.out.println("presence from: " + from.toString() + " type: " + type.toString()); - 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 - } + 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); + } + }); } }
--- a/src/main/java/de/unixwork/im/ContactListFrame.java Thu Dec 26 12:53:02 2024 +0100 +++ b/src/main/java/de/unixwork/im/ContactListFrame.java Thu Dec 26 17:19:15 2024 +0100 @@ -63,7 +63,11 @@ contactListModel.addElement(contact); } } - + + public void reload() { + contactList.updateUI(); + } + // Interface for click callback public interface ContactClickListener { void onContactClicked(RosterEntry contact);
--- a/src/main/java/de/unixwork/im/ConversationFrame.java Thu Dec 26 12:53:02 2024 +0100 +++ b/src/main/java/de/unixwork/im/ConversationFrame.java Thu Dec 26 17:19:15 2024 +0100 @@ -25,7 +25,7 @@ setTitle(xid); setSize(500, 400); - setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLayout(new BorderLayout(5, 5)); // Top panel with top-right button @@ -91,6 +91,12 @@ // message handler setMessageSendListener(this); + + // init presence + PresenceInfo ps = App.getInstance().getPresenceForXID(xid); + if(ps != null) { + updatePresence(ps); + } } public void addToLog(String message, boolean incoming, boolean secure) { @@ -133,4 +139,8 @@ } } + void updatePresence(PresenceInfo ps) { + setTitle(ps.getOnlineStatus() + xid); + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/unixwork/im/PresenceInfo.java Thu Dec 26 17:19:15 2024 +0100 @@ -0,0 +1,31 @@ +package de.unixwork.im; + +import java.util.HashMap; +import java.util.Map; +import org.jivesoftware.smack.packet.Presence; + + +public class PresenceInfo { + private final Map<String, Presence.Type> presence = new HashMap<>(4); + + public PresenceInfo() { + + } + + public void setStatus(String resource, Presence.Type type) { + presence.put(resource, type); + } + + public String getOnlineStatus() { + String status = "<offline> "; + for (Map.Entry<String, Presence.Type> entry : presence.entrySet()) { + Presence.Type type = entry.getValue(); + if(type == Presence.Type.available) { + status = "<online> "; + break; + } + } + + return status; + } +}