Added Client#sendMessage(Message), closing socket on exit

This commit is contained in:
2019-12-31 11:57:11 +02:00
parent 3cebdc8997
commit 46d9cd49f4
5 changed files with 81 additions and 44 deletions

View File

@ -2,6 +2,7 @@ package envoy.client.ui;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -76,7 +77,7 @@ public class Startup {
logger.severe("User name is not set or empty. Exiting...");
System.exit(1);
}
// TODO: create dialog
String pass = JOptionPane.showInputDialog("Enter password");
@ -125,17 +126,17 @@ public class Startup {
// Initialize chats in local database
try {
localDB.initializeDBFile();
// TODO: localDB.loadChats();
localDB.loadChats();
} 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.toString() + "\nChats will not be stored locally.",
"Error while loading local database: " + e.toString() + "\nChats might 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());
@ -147,8 +148,13 @@ public class Startup {
try {
new StatusTrayIcon(chatWindow).show();
// If the tray icon is supported and corresponding settings is set, hide the chat window on close
Settings.getInstance().getItems().get("onCloseMode").setChangeHandler((onCloseMode) -> chatWindow.setDefaultCloseOperation((boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE));
// If the tray icon is supported and corresponding settings is set, hide the
// chat window on close
Settings.getInstance()
.getItems()
.get("onCloseMode")
.setChangeHandler((onCloseMode) -> chatWindow
.setDefaultCloseOperation((boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE));
} catch (EnvoyException e) {
logger.warning("The StatusTrayIcon is not supported on this platform!");
}
@ -160,13 +166,14 @@ public class Startup {
// Save Settings and LocalDB on shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
logger.info("Saving local database...");
logger.info("Closing connection...");
client.close();
logger.info("Saving local database and settings...");
localDB.save();
logger.info("Saving settings...");
Settings.getInstance().save();
} catch (IOException e1) {
e1.printStackTrace();
logger.log(Level.WARNING, "Unable to save the messages", e1);
} catch (IOException e) {
logger.log(Level.SEVERE, "Unable to save local files", e);
}
}));
}