144 lines
4.3 KiB
Java
144 lines
4.3 KiB
Java
package envoy.client.ui;
|
|
|
|
import java.awt.EventQueue;
|
|
import java.io.IOException;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JOptionPane;
|
|
|
|
import envoy.client.Client;
|
|
import envoy.client.Config;
|
|
import envoy.client.LocalDB;
|
|
import envoy.client.Settings;
|
|
import envoy.client.util.EnvoyLog;
|
|
import envoy.exception.EnvoyException;
|
|
import envoy.schema.User;
|
|
|
|
/**
|
|
* Starts the Envoy client and prompts the user to enter their name.
|
|
* <br>
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>Startup.java</strong><br>
|
|
* Created: <strong>12 Oct 2019</strong><br>
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @author Maximilian Käfer
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy v0.1-alpha
|
|
*/
|
|
public class Startup {
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(Startup.class.getSimpleName());
|
|
|
|
public static void main(String[] args) {
|
|
Config config = Config.getInstance();
|
|
|
|
try {
|
|
// Load the configuration from client.properties first
|
|
config.load();
|
|
|
|
// Override configuration values with command line arguments
|
|
if (args.length > 0) config.load(args);
|
|
|
|
// Check if all configuration values have been initialized
|
|
if (!config.isInitialized()) throw new EnvoyException("Server or port are not defined");
|
|
} catch (Exception e) {
|
|
JOptionPane
|
|
.showMessageDialog(null, "Error loading configuration values: \n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE);
|
|
System.exit(1);
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// Ask the user for their user name
|
|
String userName = JOptionPane.showInputDialog("Please enter your username");
|
|
if (userName == null || userName.isEmpty()) {
|
|
logger.severe("User name is not set or empty. Exiting...");
|
|
System.exit(1);
|
|
}
|
|
|
|
// Initialize the local database
|
|
LocalDB localDB;
|
|
try {
|
|
localDB = new LocalDB(config.getLocalDB());
|
|
} catch (IOException e3) {
|
|
logger.log(Level.SEVERE, "Could not initialize local database", e3);
|
|
JOptionPane.showMessageDialog(null, "Could not initialize local database!\n" + e3.toString());
|
|
System.exit(1);
|
|
return;
|
|
}
|
|
|
|
// Acquire the client user (with ID) either from the server or from the local
|
|
// database, which triggers offline mode
|
|
Client client = new Client(config);
|
|
try {
|
|
// Try entering online mode first
|
|
client.onlineInit(userName);
|
|
} catch (Exception e1) {
|
|
logger.warning("Could not connect to server. Trying offline mode...");
|
|
try {
|
|
// Try entering offline mode
|
|
localDB.loadUsers();
|
|
User clientUser = localDB.getUsers().get(userName);
|
|
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
|
|
client.setSender(clientUser);
|
|
} catch (Exception e2) {
|
|
JOptionPane.showMessageDialog(null, e2.toString(), "Client error", JOptionPane.ERROR_MESSAGE);
|
|
System.exit(1);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Set client user in local database
|
|
localDB.setUser(client.getSender());
|
|
|
|
// Initialize chats in local database
|
|
try {
|
|
localDB.initializeDBFile();
|
|
localDB.loadChats();
|
|
} catch (EnvoyException e) {
|
|
e.printStackTrace();
|
|
JOptionPane.showMessageDialog(null,
|
|
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.",
|
|
"Local DB error",
|
|
JOptionPane.WARNING_MESSAGE);
|
|
}
|
|
|
|
logger.info("Client user ID: " + client.getSender().getID());
|
|
|
|
// Save all users to the local database
|
|
if (client.isOnline()) localDB.setUsers(client.getUsers());
|
|
|
|
EventQueue.invokeLater(() -> {
|
|
try {
|
|
ChatWindow chatWindow = new ChatWindow(client, localDB);
|
|
chatWindow.setVisible(true);
|
|
|
|
try {
|
|
new StatusTrayIcon(chatWindow).show();
|
|
|
|
// If the tray icon is supported, hide the chat window on close
|
|
chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
|
} catch (EnvoyException e) {
|
|
logger.warning("The StatusTrayIcon is not supported on this platform!");
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
|
|
// Save Settings and LocalDB on shutdown
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
try {
|
|
logger.info("Saving local database...");
|
|
localDB.save();
|
|
logger.info("Saving settings...");
|
|
Settings.getInstance().save();
|
|
} catch (IOException e1) {
|
|
e1.printStackTrace();
|
|
logger.log(Level.WARNING, "Unable to save the messages", e1);
|
|
}
|
|
}));
|
|
}
|
|
} |