2020-03-26 20:23:25 +01:00
|
|
|
package envoy.client.ui;
|
|
|
|
|
2020-03-28 15:32:24 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Properties;
|
2020-03-27 21:14:49 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2020-03-26 20:23:25 +01:00
|
|
|
import javafx.application.Application;
|
2020-04-18 19:46:04 +02:00
|
|
|
import javafx.application.Platform;
|
2020-03-26 20:23:25 +01:00
|
|
|
import javafx.fxml.FXMLLoader;
|
|
|
|
import javafx.scene.Scene;
|
2020-04-10 16:18:01 +02:00
|
|
|
import javafx.scene.control.Alert;
|
|
|
|
import javafx.scene.control.Alert.AlertType;
|
2020-03-26 20:23:25 +01:00
|
|
|
import javafx.scene.layout.GridPane;
|
2020-04-18 19:46:04 +02:00
|
|
|
import javafx.scene.layout.Pane;
|
2020-03-26 20:23:25 +01:00
|
|
|
import javafx.stage.Stage;
|
|
|
|
|
2020-04-10 21:05:08 +02:00
|
|
|
import envoy.client.data.*;
|
2020-05-30 15:28:11 +02:00
|
|
|
import envoy.client.event.ThemeChangeEvent;
|
2020-04-10 21:05:08 +02:00
|
|
|
import envoy.client.net.Client;
|
|
|
|
import envoy.client.net.WriteProxy;
|
|
|
|
import envoy.data.Message;
|
|
|
|
import envoy.data.User;
|
|
|
|
import envoy.data.User.UserStatus;
|
2020-05-30 15:28:11 +02:00
|
|
|
import envoy.event.EventBus;
|
2020-04-10 21:05:08 +02:00
|
|
|
import envoy.exception.EnvoyException;
|
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
2020-03-26 20:23:25 +01:00
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>Startup.java</strong><br>
|
|
|
|
* Created: <strong>26.03.2020</strong><br>
|
2020-04-02 22:03:43 +02:00
|
|
|
*
|
2020-03-26 20:23:25 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public final class Startup extends Application {
|
|
|
|
|
2020-03-28 15:32:24 +01:00
|
|
|
private LocalDB localDB;
|
|
|
|
private Client client;
|
|
|
|
private WriteProxy writeProxy;
|
2020-03-28 10:39:15 +01:00
|
|
|
private Cache<Message> cache;
|
2020-03-27 21:14:49 +01:00
|
|
|
|
2020-04-18 19:46:04 +02:00
|
|
|
private FXMLLoader loader = new FXMLLoader();
|
|
|
|
private Stage stage;
|
2020-05-09 10:57:30 +02:00
|
|
|
private Scene previousScene;
|
2020-04-18 19:46:04 +02:00
|
|
|
|
2020-05-09 10:57:30 +02:00
|
|
|
private static final Settings settings = Settings.getInstance();
|
|
|
|
private static final ClientConfig config = ClientConfig.getInstance();
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(Startup.class);
|
2020-03-27 21:14:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2020-04-10 16:18:01 +02:00
|
|
|
public void start(Stage stage) throws Exception {
|
2020-04-18 19:46:04 +02:00
|
|
|
this.stage = stage;
|
2020-03-28 15:32:24 +01:00
|
|
|
try {
|
|
|
|
// Load the configuration from client.properties first
|
2020-05-30 15:28:11 +02:00
|
|
|
final Properties properties = new Properties();
|
2020-03-28 15:32:24 +01:00
|
|
|
properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties"));
|
|
|
|
config.load(properties);
|
|
|
|
|
|
|
|
// Override configuration values with command line arguments
|
2020-05-30 15:28:11 +02:00
|
|
|
final String[] args = getParameters().getRaw().toArray(new String[0]);
|
2020-03-28 15:32:24 +01:00
|
|
|
if (args.length > 0) config.load(args);
|
|
|
|
|
|
|
|
// Check if all mandatory configuration values have been initialized
|
|
|
|
if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized");
|
2020-05-30 15:28:11 +02:00
|
|
|
} catch (final Exception e) {
|
2020-04-10 16:18:01 +02:00
|
|
|
new Alert(AlertType.ERROR, "Error loading configuration values:\n" + e);
|
2020-03-28 15:32:24 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup logger for the envoy package
|
|
|
|
EnvoyLog.initialize(config);
|
|
|
|
EnvoyLog.attach("envoy");
|
|
|
|
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
|
|
|
|
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
|
|
|
|
|
|
|
|
// Initialize the local database
|
|
|
|
if (config.isIgnoreLocalDB()) {
|
|
|
|
localDB = new TransientLocalDB();
|
2020-04-10 16:18:01 +02:00
|
|
|
new Alert(AlertType.WARNING, "Ignoring local database.\nMessages will not be saved!").showAndWait();
|
2020-03-28 15:32:24 +01:00
|
|
|
} else try {
|
|
|
|
localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath()));
|
2020-05-30 15:28:11 +02:00
|
|
|
} catch (final IOException e3) {
|
2020-03-28 15:32:24 +01:00
|
|
|
logger.log(Level.SEVERE, "Could not initialize local database", e3);
|
2020-04-10 16:18:01 +02:00
|
|
|
new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait();
|
2020-03-28 15:32:24 +01:00
|
|
|
System.exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize client and unread message cache
|
|
|
|
client = new Client();
|
|
|
|
cache = new Cache<>();
|
|
|
|
|
|
|
|
// Try to connect to the server
|
2020-04-10 16:18:01 +02:00
|
|
|
new LoginDialog(client, localDB, cache).showAndWait();
|
2020-03-28 15:32:24 +01:00
|
|
|
|
|
|
|
// Set client user in local database
|
|
|
|
localDB.setUser(client.getSender());
|
|
|
|
|
|
|
|
// Initialize chats in local database
|
|
|
|
try {
|
|
|
|
localDB.initializeUserStorage();
|
|
|
|
localDB.loadUserData();
|
2020-05-30 15:28:11 +02:00
|
|
|
} catch (final FileNotFoundException e) {
|
2020-03-28 15:32:24 +01:00
|
|
|
// The local database file has not yet been created, probably first login
|
2020-05-30 15:28:11 +02:00
|
|
|
} catch (final Exception e) {
|
2020-03-28 15:32:24 +01:00
|
|
|
e.printStackTrace();
|
2020-04-10 16:18:01 +02:00
|
|
|
new Alert(AlertType.ERROR, "Error while loading local database: " + e + "\nChats will not be stored locally.").showAndWait();
|
2020-03-28 15:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize write proxy
|
|
|
|
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
|
2020-04-02 22:03:43 +02:00
|
|
|
localDB.getUsers()
|
|
|
|
.values()
|
|
|
|
.stream()
|
|
|
|
.filter(u -> u instanceof User && u != localDB.getUser())
|
|
|
|
.map(User.class::cast)
|
|
|
|
.forEach(u -> u.setStatus(UserStatus.OFFLINE));
|
2020-03-27 21:14:49 +01:00
|
|
|
|
|
|
|
// Prepare stage and load ChatScene
|
2020-04-18 19:46:04 +02:00
|
|
|
changeScene("/fxml/ChatScene.fxml", new GridPane(), false);
|
|
|
|
Platform.runLater(() -> { ((ChatSceneController) loader.getController()).initializeData(this, localDB, client, writeProxy); });
|
2020-03-27 21:14:49 +01:00
|
|
|
stage.setTitle("Envoy");
|
2020-06-06 10:50:23 +02:00
|
|
|
stage.getIcons().add(IconUtil.load("/icons/envoy_logo.png"));
|
2020-03-27 21:14:49 +01:00
|
|
|
stage.show();
|
2020-05-30 15:28:11 +02:00
|
|
|
// TODO: Add capability to change custom CSS. In case of switching to a default
|
|
|
|
// theme, no further action is required
|
|
|
|
EventBus.getInstance().register(ThemeChangeEvent.class, theme -> applyCSS());
|
2020-03-28 10:39:15 +01:00
|
|
|
|
|
|
|
// Relay unread messages from cache
|
|
|
|
if (cache != null && client.isOnline()) cache.relay();
|
2020-03-27 21:14:49 +01:00
|
|
|
}
|
|
|
|
|
2020-04-18 19:46:04 +02:00
|
|
|
/**
|
|
|
|
* Changes the scene of the stage.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the layout to use
|
|
|
|
* @param fxmlLocation the location of the fxml file
|
|
|
|
* @param layout the layout to use
|
|
|
|
* @param savePrevious if true, the previous stage will be stored in this
|
2020-05-30 15:28:11 +02:00
|
|
|
* instance of Startup, else the variable storing it will be
|
2020-04-18 19:46:04 +02:00
|
|
|
* set to null
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public <T extends Pane> void changeScene(String fxmlLocation, T layout, boolean savePrevious) {
|
|
|
|
Platform.runLater(() -> {
|
|
|
|
try {
|
|
|
|
// Clearing the loader so that a new Scene can be initialised
|
|
|
|
loader = new FXMLLoader();
|
2020-05-30 15:28:11 +02:00
|
|
|
final var rootNode = loader.<T>load(getClass().getResourceAsStream(fxmlLocation));
|
|
|
|
final var scene = new Scene(rootNode);
|
2020-05-09 10:57:30 +02:00
|
|
|
previousScene = savePrevious ? stage.getScene() : null;
|
|
|
|
// Setting the visual appearance
|
|
|
|
stage.setScene(scene);
|
2020-05-30 15:28:11 +02:00
|
|
|
applyCSS();
|
2020-04-18 19:46:04 +02:00
|
|
|
stage.show();
|
2020-05-30 15:28:11 +02:00
|
|
|
} catch (final IOException e) {
|
2020-04-18 19:46:04 +02:00
|
|
|
new Alert(AlertType.ERROR, "The screen could not be updated due to reasons. (...bad programming...)");
|
2020-05-30 15:28:11 +02:00
|
|
|
System.err.println("input: FXMLLocation: " + fxmlLocation);
|
2020-04-18 19:46:04 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
logger.severe("Something happened (while loading the new scene from " + fxmlLocation + ")");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the visual scene back to the saved value. The currently active scene
|
2020-05-09 10:57:30 +02:00
|
|
|
* can be saved, but must not be.
|
2020-04-18 19:46:04 +02:00
|
|
|
*
|
|
|
|
* @param storeCurrent the old scene to store, if wanted. Can be null
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public void restoreScene(boolean storeCurrent) {
|
|
|
|
Platform.runLater(() -> {
|
|
|
|
if (previousScene == null) throw new IllegalStateException("Someone tried restoring a null scene. (Something happened)");
|
|
|
|
else {
|
|
|
|
// switching previous and current
|
2020-05-30 15:28:11 +02:00
|
|
|
final var temp = storeCurrent ? stage.getScene() : null;
|
2020-04-18 19:46:04 +02:00
|
|
|
stage.setScene(previousScene);
|
2020-05-30 15:28:11 +02:00
|
|
|
applyCSS();
|
2020-04-18 19:46:04 +02:00
|
|
|
previousScene = temp;
|
|
|
|
stage.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:14:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void stop() throws Exception {
|
|
|
|
try {
|
|
|
|
// Save Settings and PersistentLocalDB on shutdown
|
|
|
|
logger.info("Closing connection...");
|
|
|
|
client.close();
|
|
|
|
|
|
|
|
logger.info("Saving local database and settings...");
|
|
|
|
localDB.save();
|
|
|
|
Settings.getInstance().save();
|
2020-05-30 15:28:11 +02:00
|
|
|
} catch (final Exception e) {
|
2020-03-27 21:14:49 +01:00
|
|
|
logger.log(Level.SEVERE, "Unable to save local files", e);
|
|
|
|
}
|
2020-03-26 20:23:25 +01:00
|
|
|
}
|
|
|
|
|
2020-04-18 19:46:04 +02:00
|
|
|
/**
|
|
|
|
* @return the controller of the current scene or a {@link NullPointerException}
|
|
|
|
* if there is none
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public Object getCurrentController() {
|
|
|
|
if (loader.getController() == null) throw new NullPointerException("Cannot deliver current controller as its undefined (duh!)");
|
|
|
|
else return loader.getController();
|
|
|
|
}
|
2020-05-09 10:57:30 +02:00
|
|
|
|
|
|
|
/**
|
2020-05-30 15:28:11 +02:00
|
|
|
* Sets the CSS files used for each scene. Should be called when the theme
|
|
|
|
* changes.
|
2020-05-09 10:57:30 +02:00
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-05-30 15:28:11 +02:00
|
|
|
public void applyCSS() {
|
|
|
|
final var styleSheets = stage.getScene().getStylesheets();
|
|
|
|
styleSheets.clear();
|
|
|
|
styleSheets.add(getClass().getResource("/css/base.css").toExternalForm());
|
|
|
|
styleSheets.add(getClass().getResource("/css/" + (settings.isUsingDefaultTheme() ? settings.getCurrentThemeName() : "custom") + ".css")
|
|
|
|
.toExternalForm());
|
2020-05-09 10:57:30 +02:00
|
|
|
}
|
2020-05-30 15:28:11 +02:00
|
|
|
|
|
|
|
@SuppressWarnings("javadoc")
|
|
|
|
public static void main(String[] args) { launch(args); }
|
2020-03-26 20:23:25 +01:00
|
|
|
}
|