Skip to content

Commit

Permalink
Woah, our own listener
Browse files Browse the repository at this point in the history
  • Loading branch information
code-irisnk committed Apr 18, 2024
1 parent 352afeb commit 00dbf32
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
}
}
2 changes: 1 addition & 1 deletion src/main/java/team/elrant/bubbles/gui/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
*/
public class SideViewController {
private static final Logger logger = LogManager.getLogger(SideViewController.class);


// Unimplemented class
}
39 changes: 39 additions & 0 deletions src/main/java/team/elrant/bubbles/xmpp/ChatListener.java
Original file line number Diff line number Diff line change
@@ -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<String> updateChatDisplay;

public ChatListener(BareJid contactJid, Consumer<String> 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());
}
}
}
24 changes: 5 additions & 19 deletions src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public void initializeConnection() throws SmackException, InterruptedException,
* @param contactJid The JID of the contact to add ([email protected]).
* @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);
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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<String> 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);
}
}
}
8 changes: 2 additions & 6 deletions src/main/java/team/elrant/bubbles/xmpp/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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();
Expand Down

0 comments on commit 00dbf32

Please sign in to comment.