diff --git a/client/src/main/java/envoy/client/data/ClientConfig.java b/client/src/main/java/envoy/client/data/ClientConfig.java index c6d4158..a7763e0 100644 --- a/client/src/main/java/envoy/client/data/ClientConfig.java +++ b/client/src/main/java/envoy/client/data/ClientConfig.java @@ -35,7 +35,6 @@ public final class ClientConfig extends Config { put("server", "s", identity()); put("port", "p", Integer::parseInt); put("localDB", "db", File::new); - put("ignoreLocalDB", "nodb", Boolean::parseBoolean); put("user", "u", identity()); put("password", "pw", identity()); } @@ -58,12 +57,6 @@ public final class ClientConfig extends Config { */ public File getLocalDB() { return (File) items.get("localDB").get(); } - /** - * @return {@code true} if the local database is to be ignored - * @since Envoy Client v0.3-alpha - */ - public Boolean isIgnoreLocalDB() { return (Boolean) items.get("ignoreLocalDB").get(); } - /** * @return the user name * @since Envoy Client v0.3-alpha diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index d344d98..d44caa0 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -1,17 +1,19 @@ package envoy.client.data; +import java.io.*; import java.time.Instant; import java.util.*; import envoy.data.*; -import envoy.event.GroupResize; -import envoy.event.MessageStatusChange; -import envoy.event.NameChange; +import envoy.event.*; +import envoy.util.SerializationUtils; /** * Stores information about the current {@link User} and their {@link Chat}s. * For message ID generation a {@link IDGenerator} is stored as well. *
+ * The managed objects are stored inside a folder in the local file system. + *
* Project: envoy-client
- * Project: envoy-client
- * Project: envoy-client
* File: LocalDB.java
* Created: 3 Feb 2020
@@ -19,60 +21,105 @@ import envoy.event.NameChange;
* @author Kai S. K. Engelbart
* @since Envoy Client v0.3-alpha
*/
-public abstract class LocalDB {
+public final class LocalDB {
- protected User user;
- protected Map
- * File: PersistentLocalDB.java
- * Created: 27.10.2019
- *
- * @author Kai S. K. Engelbart
- * @author Maximilian Käfer
- * @since Envoy Client v0.1-alpha
- */
-public final class PersistentLocalDB extends LocalDB {
-
- private File dbDir, userFile, idGeneratorFile, usersFile;
-
- /**
- * Constructs an empty local database. To serialize any user-specific data to
- * the file system, call {@link PersistentLocalDB#initializeUserStorage()} first
- * and then {@link PersistentLocalDB#save(boolean)}.
- *
- * @param dbDir the directory in which to persist data
- * @throws IOException if {@code dbDir} is a file (and not a directory)
- * @since Envoy Client v0.1-alpha
- */
- public PersistentLocalDB(File dbDir) throws IOException {
- this.dbDir = dbDir;
-
- // Test if the database directory is actually a directory
- if (dbDir.exists() && !dbDir.isDirectory())
- throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath()));
-
- // Initialize global files
- idGeneratorFile = new File(dbDir, "id_gen.db");
- usersFile = new File(dbDir, "users.db");
- }
-
- /**
- * Creates a database file for a user-specific list of chats.
- *
- * @throws IllegalStateException if the client user is not specified
- * @since Envoy Client v0.1-alpha
- */
- @Override
- public void initializeUserStorage() {
- if (user == null) throw new IllegalStateException("Client user is null, cannot initialize user storage");
- userFile = new File(dbDir, user.getID() + ".db");
- }
-
- @Override
- public void save(boolean isOnline) throws IOException {
- // Save users
- SerializationUtils.write(usersFile, users);
-
- // Save user data and last sync time stamp
- if (user != null) SerializationUtils.write(userFile, chats, cacheMap, isOnline ? Instant.now() : lastSync);
-
- // Save id generator
- if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator);
- }
-
- @Override
- public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); }
-
- @Override
- public void loadUserData() throws ClassNotFoundException, IOException {
- try (var in = new ObjectInputStream(new FileInputStream(userFile))) {
- chats = (ArrayList
- * File: TransientLocalDB.java
- * Created: 3 Feb 2020
- *
- * @author Kai S. K. Engelbart
- * @since Envoy Client v0.3-alpha
- */
-public final class TransientLocalDB extends LocalDB {
-}
diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java
index 8e7132a..30757c5 100644
--- a/client/src/main/java/envoy/client/ui/Startup.java
+++ b/client/src/main/java/envoy/client/ui/Startup.java
@@ -1,12 +1,9 @@
package envoy.client.ui;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
+import java.io.*;
import java.time.Instant;
import java.util.concurrent.TimeoutException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import java.util.logging.*;
import javafx.application.Application;
import javafx.scene.control.Alert;
@@ -19,8 +16,7 @@ import envoy.client.ui.SceneContext.SceneInfo;
import envoy.client.ui.controller.LoginScene;
import envoy.data.*;
import envoy.data.User.UserStatus;
-import envoy.event.GroupMessageStatusChange;
-import envoy.event.MessageStatusChange;
+import envoy.event.*;
import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog;
@@ -70,11 +66,8 @@ public final class Startup extends Application {
logger.log(Level.INFO, "Envoy starting...");
// 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()));
+ try {
+ localDB = new LocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath()));
} catch (final IOException e3) {
logger.log(Level.SEVERE, "Could not initialize local database: ", e3);
new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait();