2020-06-08 11:58:57 +02:00
|
|
|
package envoy.client.ui.controller;
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-09-18 11:29:05 +02:00
|
|
|
import java.time.Instant;
|
2020-09-08 20:41:01 +02:00
|
|
|
import java.util.logging.*;
|
2020-04-10 16:18:01 +02:00
|
|
|
|
2020-09-20 09:55:07 +02:00
|
|
|
import javafx.application.Platform;
|
2020-04-10 21:05:08 +02:00
|
|
|
import javafx.fxml.FXML;
|
2020-07-15 18:45:55 +02:00
|
|
|
import javafx.geometry.Insets;
|
2020-04-10 21:05:08 +02:00
|
|
|
import javafx.scene.control.*;
|
|
|
|
import javafx.scene.control.Alert.AlertType;
|
2020-07-16 16:02:03 +02:00
|
|
|
import javafx.scene.image.ImageView;
|
2020-04-10 21:05:08 +02:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import dev.kske.eventbus.*;
|
|
|
|
|
2020-07-09 09:12:41 +02:00
|
|
|
import envoy.data.LoginCredentials;
|
|
|
|
import envoy.event.HandshakeRejection;
|
2020-09-08 20:41:01 +02:00
|
|
|
import envoy.util.*;
|
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.client.data.ClientConfig;
|
|
|
|
import envoy.client.ui.Startup;
|
|
|
|
import envoy.client.util.IconUtil;
|
2020-04-10 11:01:03 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-25 14:29:23 +02:00
|
|
|
* Controller for the login scene.
|
2020-06-09 22:24:09 +02:00
|
|
|
*
|
2020-04-10 11:01:03 +02:00
|
|
|
* @author Kai S. K. Engelbart
|
2020-06-10 22:50:09 +02:00
|
|
|
* @author Maximilian Käfer
|
2020-04-10 11:01:03 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-09-08 20:41:01 +02:00
|
|
|
public final class LoginScene implements EventListener {
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
@FXML
|
2020-07-15 14:05:47 +02:00
|
|
|
private TextField userTextField;
|
2020-04-10 16:18:01 +02:00
|
|
|
|
|
|
|
@FXML
|
|
|
|
private PasswordField passwordField;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private PasswordField repeatPasswordField;
|
|
|
|
|
|
|
|
@FXML
|
2020-07-15 18:45:55 +02:00
|
|
|
private Button registerSwitch;
|
2020-04-10 16:18:01 +02:00
|
|
|
|
|
|
|
@FXML
|
2020-07-15 18:45:55 +02:00
|
|
|
private Label connectionLabel;
|
2020-04-10 16:18:01 +02:00
|
|
|
|
2020-06-07 17:03:02 +02:00
|
|
|
@FXML
|
2020-07-15 18:45:55 +02:00
|
|
|
private Button loginButton;
|
|
|
|
|
2020-09-19 09:13:04 +02:00
|
|
|
@FXML
|
|
|
|
private CheckBox cbStaySignedIn;
|
|
|
|
|
2020-07-15 18:45:55 +02:00
|
|
|
@FXML
|
|
|
|
private Button offlineModeButton;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private Label registerTextLabel;
|
2020-06-07 17:03:02 +02:00
|
|
|
|
2020-07-16 16:02:03 +02:00
|
|
|
@FXML
|
|
|
|
private ImageView logo;
|
|
|
|
|
2020-09-19 09:13:04 +02:00
|
|
|
private boolean registration;
|
2020-07-15 18:45:55 +02:00
|
|
|
|
2020-09-18 11:29:05 +02:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
|
|
|
|
private static final ClientConfig config = ClientConfig.getInstance();
|
2020-04-10 16:18:01 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
@FXML
|
|
|
|
private void initialize() {
|
|
|
|
connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort());
|
|
|
|
|
|
|
|
// Show an alert after an unsuccessful handshake
|
2020-09-08 20:41:01 +02:00
|
|
|
EventBus.getInstance().registerListener(this);
|
2020-07-16 16:02:03 +02:00
|
|
|
|
|
|
|
logo.setImage(IconUtil.loadIcon("envoy_logo"));
|
2020-06-07 16:26:54 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
// Set initial cursor
|
|
|
|
userTextField.requestFocus();
|
2020-04-10 11:01:03 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
@FXML
|
2020-06-08 11:58:57 +02:00
|
|
|
private void loginButtonPressed() {
|
2020-10-19 18:17:51 +02:00
|
|
|
final String user = userTextField.getText(), pass = passwordField.getText(),
|
|
|
|
repeatPass = repeatPasswordField.getText();
|
2020-09-19 09:13:04 +02:00
|
|
|
final boolean requestToken = cbStaySignedIn.isSelected();
|
2020-06-08 11:58:57 +02:00
|
|
|
|
|
|
|
// Prevent registration with unequal passwords
|
2020-09-18 11:29:05 +02:00
|
|
|
if (registration && !pass.equals(repeatPass)) {
|
2020-10-19 18:17:51 +02:00
|
|
|
new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one")
|
|
|
|
.showAndWait();
|
2020-06-25 10:21:27 +02:00
|
|
|
repeatPasswordField.clear();
|
2020-09-18 11:29:05 +02:00
|
|
|
} else if (!Bounds.isValidContactName(user)) {
|
2020-10-19 18:17:51 +02:00
|
|
|
new Alert(AlertType.ERROR,
|
|
|
|
"The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")")
|
|
|
|
.showAndWait();
|
2020-07-15 14:05:47 +02:00
|
|
|
userTextField.clear();
|
2020-09-18 11:29:05 +02:00
|
|
|
} else {
|
|
|
|
Instant lastSync = Startup.loadLastSync(userTextField.getText());
|
2020-10-19 18:17:51 +02:00
|
|
|
Startup.performHandshake(registration
|
|
|
|
? LoginCredentials.registration(user, pass, requestToken, Startup.VERSION, lastSync)
|
|
|
|
: LoginCredentials.login(user, pass, requestToken, Startup.VERSION, lastSync));
|
2020-09-18 11:29:05 +02:00
|
|
|
}
|
2020-06-08 11:58:57 +02:00
|
|
|
}
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
@FXML
|
2020-10-19 18:17:51 +02:00
|
|
|
private void offlineModeButtonPressed() {
|
|
|
|
Startup.attemptOfflineMode(userTextField.getText());
|
|
|
|
}
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
@FXML
|
2020-07-15 18:45:55 +02:00
|
|
|
private void registerSwitchPressed() {
|
2020-09-25 14:29:23 +02:00
|
|
|
|
|
|
|
// Update button text and register switch
|
2020-07-29 21:59:55 +02:00
|
|
|
if (!registration) {
|
2020-07-15 18:45:55 +02:00
|
|
|
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");
|
|
|
|
}
|
2020-07-29 21:59:55 +02:00
|
|
|
registration = !registration;
|
2020-09-25 14:29:23 +02:00
|
|
|
|
2020-07-29 21:59:55 +02:00
|
|
|
// Make repeat password field and label visible / invisible
|
|
|
|
repeatPasswordField.setVisible(registration);
|
|
|
|
offlineModeButton.setDisable(registration);
|
2020-04-10 16:18:01 +02:00
|
|
|
}
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
@FXML
|
|
|
|
private void abortLogin() {
|
2020-06-13 22:36:52 +02:00
|
|
|
logger.log(Level.INFO, "The login process has been cancelled. Exiting...");
|
2020-06-08 11:58:57 +02:00
|
|
|
System.exit(0);
|
|
|
|
}
|
2020-09-08 20:41:01 +02:00
|
|
|
|
|
|
|
@Event
|
2020-10-19 18:17:51 +02:00
|
|
|
private void onHandshakeRejection(HandshakeRejection evt) {
|
|
|
|
Platform.runLater(() -> new Alert(AlertType.ERROR, evt.get()).showAndWait());
|
|
|
|
}
|
2020-04-10 11:01:03 +02:00
|
|
|
}
|