package envoy.client.ui; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.image.Image; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; import envoy.client.data.*; import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.data.Message; import envoy.data.User; import envoy.data.User.UserStatus; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; /** * Project: envoy-client
* File: Startup.java
* Created: 26.03.2020
* * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ public final class Startup extends Application { private LocalDB localDB; private Client client; private WriteProxy writeProxy; private Cache cache; private FXMLLoader loader = new FXMLLoader(); private Stage stage; private Scene previousScene; private final String[] CSSPaths = { "file://./src/main/resources/fxml/themes/base.css", "file://./src/main/resources/fxml/themes/" + (settings.isUsingDefaultTheme() ? settings.getCurrentThemeName() : "custom") + ".css" }; private static final Settings settings = Settings.getInstance(); private static final ClientConfig config = ClientConfig.getInstance(); private static final Logger logger = EnvoyLog.getLogger(Startup.class); /** * {@inheritDoc} */ @Override public void start(Stage stage) throws Exception { this.stage = stage; try { // Load the configuration from client.properties first Properties properties = new Properties(); properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties")); config.load(properties); // Override configuration values with command line arguments String[] args = getParameters().getRaw().toArray(new String[0]); 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"); } catch (Exception e) { new Alert(AlertType.ERROR, "Error loading configuration values:\n" + e); 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(); new Alert(AlertType.WARNING, "Ignoring local database.\nMessages will not be saved!").showAndWait(); } else try { localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); } catch (IOException e3) { logger.log(Level.SEVERE, "Could not initialize local database", e3); new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait(); System.exit(1); return; } // Initialize client and unread message cache client = new Client(); cache = new Cache<>(); // Try to connect to the server new LoginDialog(client, localDB, cache).showAndWait(); // Set client user in local database localDB.setUser(client.getSender()); // Initialize chats in local database try { localDB.initializeUserStorage(); localDB.loadUserData(); } catch (FileNotFoundException e) { // The local database file has not yet been created, probably first login } catch (Exception e) { e.printStackTrace(); new Alert(AlertType.ERROR, "Error while loading local database: " + e + "\nChats will not be stored locally.").showAndWait(); } // 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 localDB.getUsers() .values() .stream() .filter(u -> u instanceof User && u != localDB.getUser()) .map(User.class::cast) .forEach(u -> u.setStatus(UserStatus.OFFLINE)); // Prepare stage and load ChatScene changeScene("/fxml/ChatScene.fxml", new GridPane(), false); Platform.runLater(() -> { ((ChatSceneController) loader.getController()).initializeData(this, localDB, client, writeProxy); }); stage.setTitle("Envoy"); stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png"))); stage.show(); // Relay unread messages from cache if (cache != null && client.isOnline()) cache.relay(); } /** * Changes the scene of the stage. * * @param 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 * instance of Startup, else the variable storing it will * be * set to null * @since Envoy Client v0.1-beta */ public 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(); var rootNode = loader.load(getClass().getResourceAsStream(fxmlLocation)); var scene = new Scene(rootNode); previousScene = savePrevious ? stage.getScene() : null; // Setting the visual appearance scene.getStylesheets().addAll(CSSPaths); System.out.println(Paths.get(".").toAbsolutePath().normalize().toString()); stage.setScene(scene); stage.show(); // return loader.getController(); } catch (IOException e) { new Alert(AlertType.ERROR, "The screen could not be updated due to reasons. (...bad programming...)"); System.err.println("input: FXMLLocation: " + fxmlLocation + ", CSS paths: " + CSSPaths); 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 * can be saved, but must not be. * * @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 var temp = storeCurrent ? stage.getScene() : null; stage.setScene(previousScene); previousScene = temp; stage.show(); } }); } /** * {@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(); } catch (Exception e) { logger.log(Level.SEVERE, "Unable to save local files", e); } } @SuppressWarnings("javadoc") public static void main(String[] args) { launch(args); } /** * @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(); } /** * @return the CSSPaths * @since Envoy Client v0.1-beta */ public String[] getCSSPaths() { return CSSPaths; } /** * Changes the currently displayed theme * * @since Envoy Client v0.1-beta */ public void changeTheme() { // the base.css file should never be changed during runtime CSSPaths[1] = "file://.fxml/themes/" + (settings.isUsingDefaultTheme() ? settings.getCurrentThemeName() : "custom") + ".css"; var styleSheets = stage.getScene().getStylesheets(); styleSheets.remove(styleSheets.size() - 1); styleSheets.add(CSSPaths[1]); } }