Skip to content

Commit

Permalink
Merge pull request #2 from elrant/login-dev
Browse files Browse the repository at this point in the history
Merge login changes
  • Loading branch information
code-irisnk authored Apr 21, 2024
2 parents 755211c + c390959 commit 118b2c8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 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
45 changes: 37 additions & 8 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 @@ public 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();

Check warning on line 178 in src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Result of method call ignored

Result of `File.delete()` is ignored

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 @@ -179,7 +208,7 @@ public void saveUserToFile(@NotNull String filename, boolean savePassword) {
}

/**
* Checks if the user is currently logged in.
* Checks if the user is currently logged in.>>>>>>> main
*
* @return true if the user is logged in, otherwise false.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/team/elrant/bubbles/xmpp/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,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;
public @NotNull String username;
public @NotNull String serviceName;

/**
* Constructs a User object with the specified username and service name.
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 118b2c8

Please sign in to comment.