Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Sciaopersone committed Apr 19, 2024
1 parent a1b779a commit 647e4ec
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 37 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions src/main/java/team/elrant/bubbles/gui/LoginController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -28,6 +31,8 @@ public class LoginController {
private Label successfulLoginLabel;
@FXML
private CheckBox rememberPassword;
@FXML
private CheckBox seePasswordCheckbox;
private @Nullable ConnectedUser connectedUser;


Expand All @@ -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) {
Expand All @@ -51,17 +59,33 @@ 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.
*/
@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.");
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/team/elrant/bubbles/gui/SideViewApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 40 additions & 7 deletions src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -88,6 +113,7 @@ private void addContact(@NotNull BareJid contactJid, @Nullable String nickname)
}
}


/**
* Removes a contact from the user's roster.
*
Expand Down Expand Up @@ -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);
Expand All @@ -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.
*
Expand Down
21 changes: 3 additions & 18 deletions src/main/java/team/elrant/bubbles/xmpp/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Font name="Segoe UI Semibold" size="12.0" />
</font>
</Label>
<TextField fx:id="username_field" layoutX="78.0" layoutY="1.0" prefHeight="22.0" prefWidth="162.0" promptText="Username">
<TextField fx:id="username_field" layoutX="78.0" layoutY="1.0" prefHeight="22.0" prefWidth="162.0" promptText="MarioRossi">
<font>
<Font name="Segoe UI" size="12.0" />
</font>
Expand All @@ -23,16 +23,21 @@
<Font name="Segoe UI Semibold" size="12.0" />
</font>
</Label>
<PasswordField fx:id="password_field" layoutX="78.0" layoutY="35.0" prefHeight="22.0" prefWidth="162.0" promptText="********">
<PasswordField fx:id="password_field_hidden" layoutX="78.0" layoutY="35.0" prefHeight="22.0" prefWidth="138.0" promptText="********">
<font>
<Font name="Segoe UI" size="12.0" />
</font>
</PasswordField>
<TextField fx:id="password_field_visible" layoutX="78.0" layoutY="35.0" prefHeight="22.0" prefWidth="138.0" promptText="********" visible="false">
<font>
<Font name="Segoe UI" size="12.0" />
</font></TextField>
<CheckBox fx:id="rememberPassword" layoutX="95.0" layoutY="64.0" mnemonicParsing="false" text="Remember password">
<font>
<Font name="Segoe UI Light" size="12.0" />
</font>
</CheckBox>
<CheckBox fx:id="seePasswordCheckbox" layoutX="223.0" layoutY="38.0" mnemonicParsing="false" onAction="#onSeePasswordCheckBoxClick" prefHeight="17.0" prefWidth="8.0" />
</AnchorPane>
<Label fx:id="loginLabel" alignment="BASELINE_CENTER" contentDisplay="CENTER" layoutX="-21.0" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="39.0" prefWidth="304.0" text="Login" textAlignment="CENTER">
<font>
Expand Down

0 comments on commit 647e4ec

Please sign in to comment.