Add offline mode check box and connection label to login dialog

This commit is contained in:
2020-06-07 17:03:02 +02:00
parent 2409c5b2f7
commit 33abd461a5
2 changed files with 37 additions and 20 deletions

View File

@ -49,6 +49,12 @@ public final class LoginDialog extends Dialog<Void> {
@FXML
private CheckBox registerCheckBox;
@FXML
private CheckBox offlineCheckBox;
@FXML
private Label connectionLabel;
private final Client client;
private final LocalDB localDB;
private final Cache<Message> receivedMessageCache;
@ -97,8 +103,12 @@ public final class LoginDialog extends Dialog<Void> {
if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) {
clearPasswordFields();
new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait();
} else
performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected()));
} else {
final var credentials = new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(),
registerCheckBox.isSelected());
if (!offlineCheckBox.isSelected()) performHandshake(credentials);
else attemptOfflineMode(credentials);
}
});
// Perform automatic login if configured
@ -112,6 +122,7 @@ public final class LoginDialog extends Dialog<Void> {
@FXML
private void initialize() {
connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort());
// Show an alert after an unsuccessful handshake
eventBus.register(HandshakeRejectionEvent.class,
@ -128,11 +139,6 @@ public final class LoginDialog extends Dialog<Void> {
clearPasswordFields();
}
private void abortLogin() {
logger.info("The login process has been cancelled. Exiting...");
System.exit(0);
}
private void performHandshake(LoginCredentials credentials) {
try {
client.performHandshake(credentials, receivedMessageCache);
@ -143,18 +149,22 @@ public final class LoginDialog extends Dialog<Void> {
} catch (IOException | InterruptedException | TimeoutException e) {
logger.warning("Could not connect to server: " + e);
logger.finer("Attempting offline mode...");
try {
// Try entering offline mode
localDB.loadUsers();
User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser);
new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" + e).showAndWait();
hide();
} catch (Exception e1) {
new Alert(AlertType.ERROR, "Client error: " + e).showAndWait();
System.exit(1);
}
attemptOfflineMode(credentials);
}
}
private void attemptOfflineMode(LoginCredentials credentials) {
try {
// Try entering offline mode
localDB.loadUsers();
User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser);
new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.").showAndWait();
hide();
} catch (Exception e) {
new Alert(AlertType.ERROR, "Client error: " + e).showAndWait();
System.exit(1);
}
}
@ -162,4 +172,9 @@ public final class LoginDialog extends Dialog<Void> {
passwordField.clear();
repeatPasswordField.clear();
}
private void abortLogin() {
logger.info("The login process has been cancelled. Exiting...");
System.exit(0);
}
}