Made local database persistence optional
* Split LocalDB into abstract class LocalDb and PersistentLocalDb and TransientLocalDb * Moved LocalDb to database package * Added ignoreLocalDb option to Config
This commit is contained in:
@ -11,7 +11,12 @@ import javax.swing.JFrame;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import envoy.client.*;
|
||||
import envoy.client.Client;
|
||||
import envoy.client.Config;
|
||||
import envoy.client.Settings;
|
||||
import envoy.client.database.LocalDb;
|
||||
import envoy.client.database.PersistentLocalDb;
|
||||
import envoy.client.database.TransientLocalDb;
|
||||
import envoy.client.util.EnvoyLog;
|
||||
import envoy.data.LoginCredentials;
|
||||
import envoy.data.User;
|
||||
@ -63,8 +68,8 @@ public class Startup {
|
||||
} catch (Exception e) {
|
||||
JOptionPane
|
||||
.showMessageDialog(null, "Error loading configuration values:\n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE);
|
||||
System.exit(1);
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
// Set new logger levels loaded from config
|
||||
@ -80,12 +85,19 @@ public class Startup {
|
||||
}
|
||||
|
||||
// Initialize the local database
|
||||
LocalDB localDB;
|
||||
try {
|
||||
localDB = new LocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath()));
|
||||
LocalDb localDb;
|
||||
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.toString());
|
||||
JOptionPane
|
||||
.showMessageDialog(null, "Could not initialize local database!\n" + e3.toString(), "Local database error", JOptionPane.ERROR_MESSAGE);
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
@ -97,15 +109,15 @@ public class Startup {
|
||||
Client client = new Client();
|
||||
try {
|
||||
// Try entering online mode first
|
||||
localDB.loadIdGenerator();
|
||||
client.onlineInit(credentials, localDB);
|
||||
localDb.loadIdGenerator();
|
||||
client.onlineInit(credentials, localDb);
|
||||
} catch (Exception e1) {
|
||||
logger.warning("Could not connect to server. Trying offline mode...");
|
||||
e1.printStackTrace();
|
||||
try {
|
||||
// Try entering offline mode
|
||||
localDB.loadUsers();
|
||||
User clientUser = localDB.getUsers().get(credentials.getName());
|
||||
localDb.loadUsers();
|
||||
User clientUser = localDb.getUsers().get(credentials.getName());
|
||||
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
|
||||
client.setSender(clientUser);
|
||||
JOptionPane.showMessageDialog(null,
|
||||
@ -120,12 +132,12 @@ public class Startup {
|
||||
}
|
||||
|
||||
// Set client user in local database
|
||||
localDB.setUser(client.getSender());
|
||||
localDb.setUser(client.getSender());
|
||||
|
||||
// Initialize chats in local database
|
||||
try {
|
||||
localDB.initializeDBFile();
|
||||
localDB.loadChats();
|
||||
localDb.initializeUserStorage();
|
||||
localDb.loadChats();
|
||||
} catch (FileNotFoundException e) {
|
||||
// The local database file has not yet been created, probably first login
|
||||
} catch (Exception e) {
|
||||
@ -137,12 +149,12 @@ public class Startup {
|
||||
}
|
||||
|
||||
// Save all users to the local database
|
||||
if (client.isOnline()) localDB.setUsers(client.getUsers());
|
||||
if (client.isOnline()) localDb.setUsers(client.getUsers());
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
chatWindow.setClient(client);
|
||||
chatWindow.setLocalDB(localDB);
|
||||
chatWindow.setLocalDB(localDb);
|
||||
|
||||
try {
|
||||
new StatusTrayIcon(chatWindow).show();
|
||||
@ -162,16 +174,16 @@ public class Startup {
|
||||
}
|
||||
});
|
||||
|
||||
// Save Settings and LocalDB on shutdown
|
||||
// Save Settings and PersistentLocalDb on shutdown
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
logger.info("Closing connection...");
|
||||
client.close();
|
||||
|
||||
logger.info("Saving local database and settings...");
|
||||
localDB.save();
|
||||
localDb.save();
|
||||
Settings.getInstance().save();
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Unable to save local files", e);
|
||||
}
|
||||
}));
|
||||
|
Reference in New Issue
Block a user