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-28 15:32:24 +01:00
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
|
2020-03-27 21:14:49 +01:00
|
|
|
import envoy.client.data.*;
|
|
|
|
import envoy.client.net.Client;
|
|
|
|
import envoy.client.net.WriteProxy;
|
2020-03-28 15:32:24 +01:00
|
|
|
import envoy.client.ui.container.LoginDialog;
|
2020-03-27 21:14:49 +01:00
|
|
|
import envoy.data.Message;
|
2020-03-28 15:32:24 +01:00
|
|
|
import envoy.data.User.UserStatus;
|
|
|
|
import envoy.exception.EnvoyException;
|
2020-03-27 21:14:49 +01:00
|
|
|
import envoy.util.EnvoyLog;
|
2020-03-26 20:23:25 +01:00
|
|
|
import javafx.application.Application;
|
|
|
|
import javafx.fxml.FXMLLoader;
|
|
|
|
import javafx.scene.Scene;
|
2020-03-26 21:01:42 +01:00
|
|
|
import javafx.scene.image.Image;
|
2020-03-26 20:23:25 +01:00
|
|
|
import javafx.scene.layout.GridPane;
|
|
|
|
import javafx.stage.Stage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>Startup.java</strong><br>
|
|
|
|
* Created: <strong>26.03.2020</strong><br>
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
private static final ClientConfig config = ClientConfig.getInstance();
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(Startup.class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void init() throws Exception {
|
2020-03-28 15:32:24 +01:00
|
|
|
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) {
|
|
|
|
JOptionPane.showMessageDialog(null, "Error loading configuration values:\n" + e, "Configuration error", JOptionPane.ERROR_MESSAGE);
|
|
|
|
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();
|
|
|
|
JOptionPane.showMessageDialog(null,
|
|
|
|
"Ignoring local database.\nMessages will not be saved!",
|
|
|
|
"Local database warning",
|
|
|
|
JOptionPane.WARNING_MESSAGE);
|
|
|
|
} 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);
|
|
|
|
JOptionPane.showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE);
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
JOptionPane.showMessageDialog(null,
|
|
|
|
"Error while loading local database: " + e + "\nChats will not be stored locally.",
|
|
|
|
"Local DB error",
|
|
|
|
JOptionPane.WARNING_MESSAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 != localDB.getUser()).forEach(u -> u.setStatus(UserStatus.OFFLINE));
|
2020-03-27 21:14:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 20:23:25 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2020-03-27 21:14:49 +01:00
|
|
|
public void start(Stage stage) throws Exception {
|
|
|
|
|
|
|
|
// Prepare stage and load ChatScene
|
2020-03-26 20:23:25 +01:00
|
|
|
var loader = new FXMLLoader(getClass().getResource("ChatScene.fxml"));
|
|
|
|
var anchorPane = loader.<GridPane>load();
|
2020-03-27 21:14:49 +01:00
|
|
|
var chatScene = new Scene(anchorPane);
|
|
|
|
stage.setTitle("Envoy");
|
|
|
|
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png")));
|
|
|
|
stage.setScene(chatScene);
|
2020-03-28 15:32:24 +01:00
|
|
|
|
|
|
|
loader.<ChatSceneController>getController().initializeData(localDB, client, writeProxy);
|
2020-03-27 21:14:49 +01:00
|
|
|
stage.show();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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);
|
|
|
|
}
|
2020-03-26 20:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) { launch(args); }
|
|
|
|
}
|