From 647e4ec739751abc0a29e5acb499f10e69fa8bc9 Mon Sep 17 00:00:00 2001 From: Sciaopersone <98805033+Sciaopersone@users.noreply.github.com> Date: Fri, 19 Apr 2024 10:58:18 +0200 Subject: [PATCH 1/2] Init --- .idea/misc.xml | 1 - .../elrant/bubbles/gui/LoginController.java | 32 +++++++++++-- .../bubbles/gui/SideViewApplication.java | 10 ++-- .../elrant/bubbles/xmpp/ConnectedUser.java | 47 ++++++++++++++++--- .../java/team/elrant/bubbles/xmpp/User.java | 21 ++------- .../elrant/bubbles/gui/views/LoginView.fxml | 9 +++- 6 files changed, 83 insertions(+), 37 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index dddff60..a83b373 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -9,7 +9,6 @@ - diff --git a/src/main/java/team/elrant/bubbles/gui/LoginController.java b/src/main/java/team/elrant/bubbles/gui/LoginController.java index 35c575f..85d45cb 100644 --- a/src/main/java/team/elrant/bubbles/gui/LoginController.java +++ b/src/main/java/team/elrant/bubbles/gui/LoginController.java @@ -1,6 +1,7 @@ package team.elrant.bubbles.gui; import javafx.fxml.FXML; +import javafx.scene.AccessibleRole; import javafx.scene.control.*; import javafx.stage.Stage; import org.apache.logging.log4j.LogManager; @@ -19,7 +20,9 @@ public class LoginController { @FXML private TextField username_field; @FXML - private PasswordField password_field; + private PasswordField password_field_hidden; + @FXML + private TextField password_field_visible; @FXML private Button submitButton; @FXML @@ -28,6 +31,8 @@ public class LoginController { private Label successfulLoginLabel; @FXML private CheckBox rememberPassword; + @FXML + private CheckBox seePasswordCheckbox; private @Nullable ConnectedUser connectedUser; @@ -38,7 +43,10 @@ public class LoginController { @FXML protected void onSubmitButtonClick() { try { - connectedUser = new ConnectedUser(username_field.getText(), password_field.getText(), "bubbles.elrant.team"); + if (seePasswordCheckbox.isSelected()) + connectedUser = new ConnectedUser(username_field.getText(), password_field_visible.getText(), "bubbles.elrant.team"); + else + connectedUser = new ConnectedUser(username_field.getText(), password_field_hidden.getText(), "bubbles.elrant.team"); connectedUser.initializeConnection(); connectedUser.saveUserToFile("user.dat", rememberPassword.isSelected()); } catch (Exception e) { @@ -51,6 +59,19 @@ protected void onSubmitButtonClick() { } } + @FXML + protected void onSeePasswordCheckBoxClick (){ + if (seePasswordCheckbox.isSelected()) { + password_field_hidden.setVisible(false); + password_field_visible.setText(password_field_hidden.getText()); + password_field_visible.setVisible(true); + } else { + password_field_visible.setVisible(false); + password_field_hidden.setText(password_field_visible.getText()); + password_field_hidden.setVisible(true); + } + } + /** * Initializes the login form. * It loads the username from a file and populates the username field, if available. @@ -58,10 +79,13 @@ protected void onSubmitButtonClick() { @FXML public void initialize() { try { - User userFromFile = new User("user.dat"); - if (!userFromFile.getUsername().isEmpty()) { + ConnectedUser userFromFile = new ConnectedUser("user.dat"); + if (!userFromFile.getUsername().equals("uninit")) { username_field.setText(userFromFile.getUsername()); } + if (!userFromFile.getPassword().equals("uninit")){ + password_field_hidden.setText(userFromFile.getPassword()); + } } catch (Exception ignored) { logger.warn("Failed to load user information from file."); } diff --git a/src/main/java/team/elrant/bubbles/gui/SideViewApplication.java b/src/main/java/team/elrant/bubbles/gui/SideViewApplication.java index 57ca47b..5097dd4 100644 --- a/src/main/java/team/elrant/bubbles/gui/SideViewApplication.java +++ b/src/main/java/team/elrant/bubbles/gui/SideViewApplication.java @@ -33,11 +33,11 @@ public void start(@NotNull Stage stage) throws Exception { AnchorPane root = fxmlLoader.load(); @NotNull Scene scene = new Scene(root, 320, 720); scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("styling/fluent-light.css")).toExternalForm()); - primaryStage.setTitle("Chat"); - primaryStage.setScene(scene); - primaryStage.centerOnScreen(); - primaryStage.setResizable(false); - primaryStage.show(); + stage.setTitle("Chat"); + stage.setScene(scene); + stage.centerOnScreen(); + stage.setResizable(false); + stage.show(); } catch (Exception e) { logger.error("Error starting SideViewApplication: {}", e.getMessage()); throw e; diff --git a/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java b/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java index b5407c8..c319595 100644 --- a/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java +++ b/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java @@ -17,9 +17,7 @@ import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.impl.JidCreate; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; +import java.io.*; import java.util.function.Consumer; /** @@ -28,7 +26,7 @@ */ public class ConnectedUser extends User { private static final Logger logger = LogManager.getLogger(ConnectedUser.class); - private final @NotNull String password; + private final @Nullable String password; private @Nullable Roster roster; private @Nullable XMPPTCPConnection connection; private @Nullable ChatManager chatManager; @@ -45,6 +43,33 @@ public ConnectedUser(@NotNull String username, @NotNull String password, @NotNul this.password = password; } + + /** + * Load a new Connected user from a file. + * + * @param filename the filename + * + * @throws IOException the io exception + * @throws ClassNotFoundException the class not found exception + */ + public ConnectedUser(@NotNull String filename) throws IOException, ClassNotFoundException { + super("uninit", "uninit"); //initialize after read file + try (FileInputStream fileIn = new FileInputStream(filename); + ObjectInputStream objectIn = new ObjectInputStream(fileIn)) { + ConnectedUser serializedUser = (ConnectedUser) objectIn.readObject(); + logger.info("User information loaded from {}", filename); + super.username = serializedUser.getUsername(); + super.serviceName = serializedUser.getServiceName(); + if (serializedUser.getPassword().equals("uninit")) + this.password = ""; + else + this.password = serializedUser.getPassword(); + } catch (IOException | ClassNotFoundException e) { + logger.error("Error loading user information from file: {}", e.getMessage()); + throw e; + } + } + /** * Initializes the XMPP connection, logs in, sets up chat manager, and populates the roster. * After connecting, it sends a message to the test user. @@ -88,6 +113,7 @@ private void addContact(@NotNull BareJid contactJid, @Nullable String nickname) } } + /** * Removes a contact from the user's roster. * @@ -148,14 +174,17 @@ public void acceptSubscription(@NotNull BareJid contactJid, @Nullable String nic * @param filename The name of the file to save the user information to. */ public void saveUserToFile(@NotNull String filename, boolean savePassword) { + File file = new File("user.dat"); + file.delete(); + try (@NotNull FileOutputStream fileOut = new FileOutputStream(filename); @NotNull ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) { if (savePassword) - objectOut.writeObject(this); + objectOut.writeObject(new ConnectedUser(this.getUsername(), this.getPassword(), this.getServiceName())); else { - @NotNull User userToFile = new User(super.getUsername(), super.getServiceName()); - objectOut.writeObject(userToFile); + ConnectedUser user = new ConnectedUser(this.getUsername(), "uninit", this.getServiceName()); + objectOut.writeObject(user); } logger.info("User information (excluding password) saved to {}", filename); @@ -178,6 +207,10 @@ public void saveUserToFile(@NotNull String filename, boolean savePassword) { } } + public String getPassword(){ + return password; + } + /** * Checks if the user is currently logged in. * diff --git a/src/main/java/team/elrant/bubbles/xmpp/User.java b/src/main/java/team/elrant/bubbles/xmpp/User.java index 5412851..d6479b2 100644 --- a/src/main/java/team/elrant/bubbles/xmpp/User.java +++ b/src/main/java/team/elrant/bubbles/xmpp/User.java @@ -17,9 +17,8 @@ public class User implements Serializable { private static final Logger logger = LogManager.getLogger(User.class); - private final @NotNull String username; - private final @NotNull String serviceName; - private final @Nullable String password; + public @NotNull String username; + public @NotNull String serviceName; /** * Constructs a User object with the specified username and service name. @@ -28,10 +27,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,19 +39,6 @@ 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; - try (FileInputStream fileIn = new FileInputStream(filename); - ObjectInputStream objectIn = new ObjectInputStream(fileIn)) { - User serializedUser = (User) objectIn.readObject(); - logger.info("User information loaded from {}", filename); - this.username = serializedUser.getUsername(); - this.serviceName = serializedUser.getServiceName(); - } catch (IOException | ClassNotFoundException e) { - logger.error("Error loading user information from file: {}", e.getMessage()); - throw e; - } - } /** * Retrieves the username of the user. diff --git a/src/main/resources/team/elrant/bubbles/gui/views/LoginView.fxml b/src/main/resources/team/elrant/bubbles/gui/views/LoginView.fxml index 5433558..0cbfac5 100644 --- a/src/main/resources/team/elrant/bubbles/gui/views/LoginView.fxml +++ b/src/main/resources/team/elrant/bubbles/gui/views/LoginView.fxml @@ -13,7 +13,7 @@ - + @@ -23,16 +23,21 @@ - + + + + + +