diff --git a/src/main/java/team/elrant/bubbles/gui/ChatViewController.java b/src/main/java/team/elrant/bubbles/gui/ChatViewController.java index 4b675df..07b9712 100644 --- a/src/main/java/team/elrant/bubbles/gui/ChatViewController.java +++ b/src/main/java/team/elrant/bubbles/gui/ChatViewController.java @@ -1,6 +1,5 @@ package team.elrant.bubbles.gui; -import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -75,6 +74,6 @@ protected void sendMessage() { * @param message The incoming message to display. */ private void updateChatDisplay(@NotNull String message) { - Platform.runLater(() -> chatTextArea.appendText(bareContactJid + ": " + message + "\n")); + chatTextArea.appendText(bareContactJid + ": " + message + "\n"); } } diff --git a/src/main/java/team/elrant/bubbles/gui/LoginController.java b/src/main/java/team/elrant/bubbles/gui/LoginController.java index 6b88276..35c575f 100644 --- a/src/main/java/team/elrant/bubbles/gui/LoginController.java +++ b/src/main/java/team/elrant/bubbles/gui/LoginController.java @@ -58,7 +58,7 @@ protected void onSubmitButtonClick() { @FXML public void initialize() { try { - User userFromFile = new User("user.dat", ""); + User userFromFile = new User("user.dat"); if (!userFromFile.getUsername().isEmpty()) { username_field.setText(userFromFile.getUsername()); } diff --git a/src/main/java/team/elrant/bubbles/gui/SideViewController.java b/src/main/java/team/elrant/bubbles/gui/SideViewController.java index 613e338..5438628 100644 --- a/src/main/java/team/elrant/bubbles/gui/SideViewController.java +++ b/src/main/java/team/elrant/bubbles/gui/SideViewController.java @@ -10,6 +10,5 @@ */ public class SideViewController { private static final Logger logger = LogManager.getLogger(SideViewController.class); - - + // Unimplemented class } \ No newline at end of file diff --git a/src/main/java/team/elrant/bubbles/xmpp/ChatListener.java b/src/main/java/team/elrant/bubbles/xmpp/ChatListener.java new file mode 100644 index 0000000..6b73825 --- /dev/null +++ b/src/main/java/team/elrant/bubbles/xmpp/ChatListener.java @@ -0,0 +1,39 @@ +package team.elrant.bubbles.xmpp; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jivesoftware.smack.chat2.Chat; +import org.jivesoftware.smack.chat2.IncomingChatMessageListener; +import org.jivesoftware.smack.packet.Message; +import org.jxmpp.jid.BareJid; +import org.jxmpp.jid.EntityBareJid; + +import java.util.function.Consumer; + +public class ChatListener implements IncomingChatMessageListener { + private static final Logger logger = LogManager.getLogger(ChatListener.class); + public BareJid contactJid; + Consumer updateChatDisplay; + + public ChatListener(BareJid contactJid, Consumer updateChatDisplay){ + this.contactJid = contactJid; + this.updateChatDisplay = updateChatDisplay; + } + + /** + * @param from The JID of the sender + * @param message The message contents + * @param chat The chat channel + */ + @Override + public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) { + try { + if (from != null && from.equals(contactJid) && message.getBody() != null) { + updateChatDisplay.accept(message.getBody()); + logger.info("Received message from {}: {}", from, message.getBody()); + } + } catch (Exception e) { + logger.error("Error updating chat display: {}", e.getMessage()); + } + } +} diff --git a/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java b/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java index 63a8453..218a352 100644 --- a/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java +++ b/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java @@ -40,7 +40,7 @@ public class ConnectedUser extends User { * @param password The password of the user. * @param serviceName The service name of the XMPP server. */ - public ConnectedUser(@NotNull String username, @NotNull String password, @NotNull String serviceName) throws IOException, ClassNotFoundException { + public ConnectedUser(@NotNull String username, @NotNull String password, @NotNull String serviceName) { super(username, serviceName); this.password = password; } @@ -78,7 +78,7 @@ public void initializeConnection() throws SmackException, InterruptedException, * @param contactJid The JID of the contact to add (user@service.name). * @param nickname The user-defined nickname of the contact, defaults to the contact's username. */ - private void addContact(@NotNull BareJid contactJid, @Nullable String nickname) { + public void addContact(@NotNull BareJid contactJid, @Nullable String nickname) { try { if (roster != null && !roster.contains(contactJid)) { roster.createItemAndRequestSubscription(contactJid, nickname, null); @@ -159,7 +159,7 @@ public void saveUserToFile(@NotNull String filename, boolean savePassword) { } logger.info("User information (excluding password) saved to {}", filename); - } catch (IOException | ClassNotFoundException e) { + } catch (IOException e) { logger.error("Error saving user information to file: {}", e.getMessage()); } } @@ -203,24 +203,10 @@ public void disconnect() { /** * Adds an incoming message listener to the chat manager. */ - public void addIncomingMessageListener() { - if (chatManager != null) { - chatManager.addIncomingListener((from, message, chat) -> - logger.info("Received message from {}: {}", from, message.getBody())); - } - } - public void addIncomingMessageListener(BareJid contactJid, Consumer updateChatDisplay) { if (chatManager != null) { - chatManager.addIncomingListener((from, message, chat) -> { - try { - if (from != null && from.equals(contactJid) && message.getBody() != null) { - updateChatDisplay.accept(message.getBody()); - } - } catch (Exception e) { - logger.error("Error updating chat display: {}", e.getMessage()); - } - }); + ChatListener chatListener = new ChatListener(contactJid, updateChatDisplay); + chatManager.addIncomingListener(chatListener); } } } diff --git a/src/main/java/team/elrant/bubbles/xmpp/User.java b/src/main/java/team/elrant/bubbles/xmpp/User.java index 5412851..89ee4a9 100644 --- a/src/main/java/team/elrant/bubbles/xmpp/User.java +++ b/src/main/java/team/elrant/bubbles/xmpp/User.java @@ -3,7 +3,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.FileInputStream; import java.io.IOException; @@ -19,7 +18,6 @@ public class User implements Serializable { private final @NotNull String username; private final @NotNull String serviceName; - private final @Nullable String password; /** * Constructs a User object with the specified username and service name. @@ -28,10 +26,9 @@ public class User implements Serializable { * @param serviceName The service name of the XMPP server. */ - public User(@NotNull String username, @NotNull String serviceName, @NotNull String password) { + public User(@NotNull String username, @NotNull String serviceName) { this.username = username; this.serviceName = serviceName; - this.password = password; } /** @@ -41,8 +38,7 @@ public User(@NotNull String username, @NotNull String serviceName, @NotNull Stri * @throws IOException If an I/O error occurs while reading the file. * @throws ClassNotFoundException If the class of a serialized object cannot be found. */ - public User(@NotNull String filename, @NotNull String password) throws IOException, ClassNotFoundException { - this.password = password; + public User(@NotNull String filename) throws IOException, ClassNotFoundException { try (FileInputStream fileIn = new FileInputStream(filename); ObjectInputStream objectIn = new ObjectInputStream(fileIn)) { User serializedUser = (User) objectIn.readObject();