2020-06-08 11:58:57 +02:00
|
|
|
package envoy.client.ui.controller;
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
import java.io.FileNotFoundException;
|
2020-04-10 11:01:03 +02:00
|
|
|
import java.io.IOException;
|
2020-04-10 21:57:05 +02:00
|
|
|
import java.util.concurrent.TimeoutException;
|
2020-04-10 16:18:01 +02:00
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2020-04-10 21:05:08 +02:00
|
|
|
import javafx.application.Platform;
|
|
|
|
import javafx.fxml.FXML;
|
|
|
|
import javafx.scene.control.*;
|
|
|
|
import javafx.scene.control.Alert.AlertType;
|
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
import envoy.client.data.Cache;
|
|
|
|
import envoy.client.data.ClientConfig;
|
|
|
|
import envoy.client.data.LocalDB;
|
|
|
|
import envoy.client.net.Client;
|
2020-06-08 11:58:57 +02:00
|
|
|
import envoy.client.ui.SceneContext;
|
2020-04-10 16:18:01 +02:00
|
|
|
import envoy.data.LoginCredentials;
|
|
|
|
import envoy.data.Message;
|
|
|
|
import envoy.data.User;
|
2020-06-08 11:58:57 +02:00
|
|
|
import envoy.data.User.UserStatus;
|
2020-04-10 16:18:01 +02:00
|
|
|
import envoy.event.EventBus;
|
|
|
|
import envoy.event.HandshakeRejectionEvent;
|
|
|
|
import envoy.exception.EnvoyException;
|
|
|
|
import envoy.util.EnvoyLog;
|
2020-04-10 11:01:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>LoginDialog.java</strong><br>
|
|
|
|
* Created: <strong>03.04.2020</strong><br>
|
2020-06-09 22:24:09 +02:00
|
|
|
*
|
2020-04-10 11:01:03 +02:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-08 11:58:57 +02:00
|
|
|
public final class LoginScene {
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
@FXML
|
|
|
|
private TextField userTextField;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private PasswordField passwordField;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private PasswordField repeatPasswordField;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private Label repeatPasswordLabel;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private CheckBox registerCheckBox;
|
|
|
|
|
2020-06-07 17:03:02 +02:00
|
|
|
@FXML
|
|
|
|
private Label connectionLabel;
|
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
private Client client;
|
|
|
|
private LocalDB localDB;
|
|
|
|
private Cache<Message> receivedMessageCache;
|
|
|
|
private SceneContext sceneContext;
|
2020-04-10 16:18:01 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
|
2020-04-10 16:18:01 +02:00
|
|
|
private static final EventBus eventBus = EventBus.getInstance();
|
|
|
|
private static final ClientConfig config = ClientConfig.getInstance();
|
|
|
|
|
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
|
|
|
|
eventBus.register(HandshakeRejectionEvent.class,
|
|
|
|
e -> Platform.runLater(() -> { clearPasswordFields(); new Alert(AlertType.ERROR, e.get()).showAndWait(); }));
|
|
|
|
}
|
|
|
|
|
2020-04-10 11:01:03 +02:00
|
|
|
/**
|
|
|
|
* Loads the login dialog using the FXML file {@code LoginDialog.fxml}.
|
2020-06-09 22:24:09 +02:00
|
|
|
*
|
2020-04-10 16:18:01 +02:00
|
|
|
* @param client the client used to perform the handshake
|
|
|
|
* @param localDB the local database used for offline login
|
2020-06-06 10:19:33 +02:00
|
|
|
* @param receivedMessageCache the cache storing messages received during
|
|
|
|
* the handshake
|
2020-06-08 11:58:57 +02:00
|
|
|
* @param sceneContext the scene context used to initialize the chat
|
|
|
|
* scene
|
2020-04-10 11:01:03 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-08 11:58:57 +02:00
|
|
|
public void initializeData(Client client, LocalDB localDB, Cache<Message> receivedMessageCache, SceneContext sceneContext) {
|
2020-04-10 16:18:01 +02:00
|
|
|
this.client = client;
|
|
|
|
this.localDB = localDB;
|
|
|
|
this.receivedMessageCache = receivedMessageCache;
|
2020-06-08 11:58:57 +02:00
|
|
|
this.sceneContext = sceneContext;
|
2020-04-10 16:18:01 +02:00
|
|
|
|
2020-06-07 16:26:54 +02:00
|
|
|
// Prepare handshake
|
|
|
|
localDB.loadIDGenerator();
|
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
// Set initial cursor
|
|
|
|
userTextField.requestFocus();
|
2020-04-10 17:07:27 +02:00
|
|
|
|
|
|
|
// Perform automatic login if configured
|
2020-06-08 11:58:57 +02:00
|
|
|
if (config.hasLoginCredentials()) performHandshake(config.getLoginCredentials());
|
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() {
|
|
|
|
|
|
|
|
// Prevent registration with unequal passwords
|
|
|
|
if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) {
|
|
|
|
clearPasswordFields();
|
|
|
|
new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait();
|
2020-06-09 22:24:09 +02:00
|
|
|
} else performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected()));
|
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
|
|
|
|
private void offlineModeButtonPressed() {
|
|
|
|
attemptOfflineMode(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), false));
|
2020-04-10 16:18:01 +02:00
|
|
|
}
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
@FXML
|
|
|
|
private void registerCheckboxChanged() {
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
// Make repeat password field and label visible / invisible
|
|
|
|
repeatPasswordField.setVisible(registerCheckBox.isSelected());
|
|
|
|
repeatPasswordLabel.setVisible(registerCheckBox.isSelected());
|
|
|
|
clearPasswordFields();
|
|
|
|
}
|
2020-04-10 11:01:03 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
@FXML
|
|
|
|
private void abortLogin() {
|
|
|
|
logger.info("The login process has been cancelled. Exiting...");
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
private void performHandshake(LoginCredentials credentials) {
|
|
|
|
try {
|
|
|
|
client.performHandshake(credentials, receivedMessageCache);
|
|
|
|
if (client.isOnline()) {
|
|
|
|
client.initReceiver(localDB, receivedMessageCache);
|
2020-06-08 11:58:57 +02:00
|
|
|
loadChatScene();
|
2020-04-10 16:18:01 +02:00
|
|
|
}
|
2020-04-10 21:57:05 +02:00
|
|
|
} catch (IOException | InterruptedException | TimeoutException e) {
|
2020-06-07 16:26:54 +02:00
|
|
|
logger.warning("Could not connect to server: " + e);
|
|
|
|
logger.finer("Attempting offline mode...");
|
2020-06-07 17:03:02 +02:00
|
|
|
attemptOfflineMode(credentials);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void attemptOfflineMode(LoginCredentials credentials) {
|
|
|
|
try {
|
|
|
|
// Try entering offline mode
|
|
|
|
localDB.loadUsers();
|
2020-06-09 22:24:09 +02:00
|
|
|
final User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier());
|
2020-06-07 17:03:02 +02:00
|
|
|
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();
|
2020-06-08 11:58:57 +02:00
|
|
|
loadChatScene();
|
2020-06-09 22:24:09 +02:00
|
|
|
} catch (final Exception e) {
|
2020-06-07 17:03:02 +02:00
|
|
|
new Alert(AlertType.ERROR, "Client error: " + e).showAndWait();
|
|
|
|
System.exit(1);
|
2020-04-10 11:01:03 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 16:18:01 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
private void loadChatScene() {
|
|
|
|
|
|
|
|
// Set client user in local database
|
|
|
|
localDB.setUser(client.getSender());
|
|
|
|
|
|
|
|
// Initialize chats in local database
|
|
|
|
try {
|
|
|
|
localDB.initializeUserStorage();
|
|
|
|
localDB.loadUserData();
|
|
|
|
} catch (final FileNotFoundException e) {
|
|
|
|
// The local database file has not yet been created, probably first login
|
|
|
|
} catch (final Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
new Alert(AlertType.ERROR, "Error while loading local database: " + e + "\nChats will not be stored locally.").showAndWait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize write proxy
|
|
|
|
final var writeProxy = client.createWriteProxy(localDB);
|
|
|
|
|
|
|
|
if (client.isOnline()) {
|
|
|
|
|
|
|
|
// Save all users to the local database and flush cache
|
|
|
|
localDB.setUsers(client.getUsers());
|
|
|
|
writeProxy.flushCache();
|
|
|
|
} else
|
|
|
|
// Set all contacts to offline mode
|
|
|
|
localDB.getUsers().values().stream().filter(User.class::isInstance).map(User.class::cast).forEach(u -> u.setStatus(UserStatus.OFFLINE));
|
|
|
|
|
|
|
|
// Load ChatScene
|
|
|
|
sceneContext.pop();
|
|
|
|
sceneContext.getStage().setMinHeight(400);
|
|
|
|
sceneContext.getStage().setMinWidth(350);
|
|
|
|
sceneContext.load(SceneContext.SceneInfo.CHAT_SCENE);
|
|
|
|
sceneContext.<ChatScene>getController().initializeData(sceneContext, localDB, client, writeProxy);
|
|
|
|
|
|
|
|
// Relay unread messages from cache
|
|
|
|
if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay();
|
|
|
|
}
|
|
|
|
|
2020-04-10 16:18:01 +02:00
|
|
|
private void clearPasswordFields() {
|
|
|
|
passwordField.clear();
|
|
|
|
repeatPasswordField.clear();
|
|
|
|
}
|
2020-04-10 11:01:03 +02:00
|
|
|
}
|