This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/client/src/main/java/envoy/client/ui/controller/LoginScene.java

142 lines
3.6 KiB
Java

package envoy.client.ui.controller;
import java.time.Instant;
import java.util.logging.*;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.ImageView;
import dev.kske.eventbus.core.*;
import envoy.data.LoginCredentials;
import envoy.event.HandshakeRejection;
import envoy.util.*;
import envoy.client.data.ClientConfig;
import envoy.client.ui.Startup;
import envoy.client.util.IconUtil;
/**
* Controller for the login scene.
*
* @author Kai S. K. Engelbart
* @author Maximilian Käfer
* @since Envoy Client v0.1-beta
*/
public final class LoginScene {
@FXML
private TextField userTextField;
@FXML
private PasswordField passwordField;
@FXML
private PasswordField repeatPasswordField;
@FXML
private Button registerSwitch;
@FXML
private Label connectionLabel;
@FXML
private Button loginButton;
@FXML
private CheckBox cbStaySignedIn;
@FXML
private Button offlineModeButton;
@FXML
private Label registerTextLabel;
@FXML
private ImageView logo;
private boolean registration;
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
private static final ClientConfig config = ClientConfig.getInstance();
@FXML
private void initialize() {
connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort());
// Show an alert after an unsuccessful handshake
EventBus.getInstance().registerListener(this);
logo.setImage(IconUtil.loadIcon("envoy_logo"));
// Set initial cursor
userTextField.requestFocus();
}
@FXML
private void loginButtonPressed() {
final String user = userTextField.getText(), pass = passwordField.getText(),
repeatPass = repeatPasswordField.getText();
final boolean requestToken = cbStaySignedIn.isSelected();
// Prevent registration with unequal passwords
if (registration && !pass.equals(repeatPass)) {
new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one")
.showAndWait();
repeatPasswordField.clear();
} else if (!Bounds.isValidContactName(user)) {
new Alert(AlertType.ERROR,
"The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")")
.showAndWait();
userTextField.clear();
} else {
Instant lastSync = Startup.loadLastSync(userTextField.getText());
Startup.performHandshake(registration
? LoginCredentials.registration(user, pass, requestToken, Startup.VERSION, lastSync)
: LoginCredentials.login(user, pass, requestToken, Startup.VERSION, lastSync));
}
}
@FXML
private void offlineModeButtonPressed() {
Startup.attemptOfflineMode(userTextField.getText());
}
@FXML
private void registerSwitchPressed() {
// Update button text and register switch
if (!registration) {
loginButton.setText("Register");
loginButton.setPadding(new Insets(2, 116, 2, 116));
registerTextLabel.setText("Already an account?");
registerSwitch.setText("Login");
} else {
loginButton.setText("Login");
loginButton.setPadding(new Insets(2, 125, 2, 125));
registerTextLabel.setText("No account yet?");
registerSwitch.setText("Register");
}
registration = !registration;
// Make repeat password field and label visible / invisible
repeatPasswordField.setVisible(registration);
offlineModeButton.setDisable(registration);
}
@FXML
private void abortLogin() {
logger.log(Level.INFO, "The login process has been cancelled. Exiting...");
System.exit(0);
}
@Event
private void onHandshakeRejection(HandshakeRejection evt) {
Platform.runLater(() -> new Alert(AlertType.ERROR, evt.get()).showAndWait());
}
}