src/main/java/de/unixwork/im/App.java

Thu, 26 Dec 2024 12:53:02 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 26 Dec 2024 12:53:02 +0100
changeset 2
94c6a715fa44
parent 0
f3095cda599e
child 3
25a32e2dfde5
permissions
-rw-r--r--

handle presence messages

0
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 package de.unixwork.im;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 import javax.swing.*;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 import java.util.HashMap;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 import java.util.List;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 import java.util.Map;
2
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
7 import org.jivesoftware.smack.packet.Presence;
0
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 import org.jivesoftware.smack.roster.RosterEntry;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 import org.jxmpp.jid.Jid;
2
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
10 import org.jxmpp.jid.parts.Resourcepart;
0
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 public class App {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 private static App instance;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 private final ContactListFrame contactListFrame;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 private final Map<String, ConversationFrame> conversations;
2
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
18 private final Map<String, PresenceInfo> presence = new HashMap<>(32);
0
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 private final Xmpp xmpp;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 public App(Xmpp xmpp) throws Exception {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 if(instance != null) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 throw new Exception("App already initilized");
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 App.instance = this;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 conversations = new HashMap<>();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 this.xmpp = xmpp;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 // Create the contact list window
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 contactListFrame = new ContactListFrame();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 contactListFrame.setContactClickListener(contact -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 openConversation(contact.getJid().asUnescapedString());
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 contactListFrame.setVisible(true);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 public static App getInstance() {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 return instance;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 public Xmpp getXmpp() {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 return xmpp;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 // Method to open a conversation window
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 public void openConversation(String xid) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 SwingUtilities.invokeLater(() -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 if (!conversations.containsKey(xid)) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 ConversationFrame conversationFrame = new ConversationFrame(xid);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 conversations.put(xid, conversationFrame);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 conversationFrame.setVisible(true);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 } else {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 conversations.get(xid).toFront();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 public void dispatchMessage(Jid from, String msg, boolean secure) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 SwingUtilities.invokeLater(() -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 // add message to the correct conversation
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 // if no conversation exists yet, create a new window
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 String xid = from.asBareJid().toString();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 //String resource = from.getResourceOrNull();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 ConversationFrame conversation = conversations.get(xid);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 if(conversation == null) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 conversation = new ConversationFrame(xid);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 conversations.put(xid, conversation);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 conversation.addToLog(msg, true, false);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 conversation.setVisible(true);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 public void setContacts(List<RosterEntry> contacts) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 SwingUtilities.invokeLater(() -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 contactListFrame.setContacts(contacts);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 // Method to perform actions in the GUI thread from other threads
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 public void runOnUiThread(Runnable action) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 SwingUtilities.invokeLater(action);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
2
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
87
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
88 public String getStatusForXID(String xid) {
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
89 PresenceInfo ps = presence.get(xid);
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
90 if(ps == null) {
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
91 return "<offline> ";
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
92 }
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
93 return ps.getOnlineStatus();
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
94 }
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
95
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
96 void handlePresence(Jid from, Presence.Type type) {
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
97 System.out.println("presence from: " + from.toString() + " type: " + type.toString());
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
98
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
99 String xid = from.asBareJid().toString();
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
100 PresenceInfo ps = presence.get(xid);
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
101 if(ps == null) {
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
102 ps = new PresenceInfo();
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
103 presence.put(xid, ps);
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
104 }
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
105
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
106 // update presence
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
107 Resourcepart resource = from.getResourceOrNull();
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
108 if(resource != null) {
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
109 ps.setStatus(resource.toString(), type);
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
110 } else {
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
111 // TODO
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
112 }
94c6a715fa44 handle presence messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
113 }
0
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 }

mercurial