Fixed Bug Not Saving Values When Exiting via “Control”+”Q” (#40)

Fixed bug not saving values when exiting via "Control"+"Q"
Reviewed-on: https://git.kske.dev/zdm/envoy/pulls/40
Reviewed-by: kske <kai@kske.dev>
This commit is contained in:
2020-09-22 14:42:51 +02:00
parent 8ed6faca96
commit 1b60ab3f0d
7 changed files with 87 additions and 64 deletions

View File

@ -5,11 +5,13 @@ import java.nio.channels.*;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
import java.util.*;
import java.util.logging.Level;
import envoy.client.event.EnvoyCloseEvent;
import envoy.data.*;
import envoy.event.*;
import envoy.exception.EnvoyException;
import envoy.util.SerializationUtils;
import envoy.util.*;
import dev.kske.eventbus.Event;
import dev.kske.eventbus.EventBus;
@ -42,12 +44,13 @@ public final class LocalDB implements EventListener {
private Instant lastSync = Instant.EPOCH;
// Persistence
private File dbDir, userFile, idGeneratorFile, lastLoginFile, usersFile;
private File userFile;
private FileLock instanceLock;
private final File dbDir, idGeneratorFile, lastLoginFile, usersFile;
/**
* Constructs an empty local database. To serialize any user-specific data to
* the file system, call {@link LocalDB#save(boolean)}.
* Constructs an empty local database.
*
* @param dbDir the directory in which to persist data
* @throws IOException if {@code dbDir} is a file (and not a directory)
@ -59,9 +62,8 @@ public final class LocalDB implements EventListener {
EventBus.getInstance().registerListener(this);
// Ensure that the database directory exists
if (!dbDir.exists()) {
dbDir.mkdirs();
} else if (!dbDir.isDirectory()) throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath()));
if (!dbDir.exists()) dbDir.mkdirs();
else if (!dbDir.isDirectory()) throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath()));
// Lock the directory
lock();
@ -88,12 +90,12 @@ public final class LocalDB implements EventListener {
* @since Envoy Client v0.2-beta
*/
private synchronized void lock() throws EnvoyException {
File file = new File(dbDir, "instance.lock");
final var file = new File(dbDir, "instance.lock");
try {
FileChannel fc = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
final var fc = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
instanceLock = fc.tryLock();
if (instanceLock == null) throw new EnvoyException("Another Envoy instance is using this local database!");
} catch (IOException e) {
} catch (final IOException e) {
throw new EnvoyException("Could not create lock file!", e);
}
}
@ -146,9 +148,8 @@ public final class LocalDB implements EventListener {
users.put(user.getName(), user);
// Synchronize user status data
for (Contact contact : users.values())
if (contact instanceof User)
getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(((User) contact).getStatus()); });
for (final var contact : users.values())
if (contact instanceof User) getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(contact.getStatus()); });
// Create missing chats
user.getContacts()
@ -162,26 +163,31 @@ public final class LocalDB implements EventListener {
* Stores all users. If the client user is specified, their chats will be stored
* as well. The message id generator will also be saved if present.
*
* @param isOnline determines which {@code lastSync} time stamp is saved
* @throws IOException if the saving process failed
* @since Envoy Client v0.3-alpha
*/
public synchronized void save(boolean isOnline) throws IOException {
@Event(eventType = EnvoyCloseEvent.class, priority = 1000)
private synchronized void save() {
EnvoyLog.getLogger(LocalDB.class).log(Level.INFO, "Saving local database...");
// Save users
SerializationUtils.write(usersFile, users);
try {
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 user data and last sync time stamp
if (user != null)
SerializationUtils.write(userFile, chats, cacheMap, Context.getInstance().getClient().isOnline() ? Instant.now() : lastSync);
// Save last login information
if (authToken != null) SerializationUtils.write(lastLoginFile, user, authToken);
// Save last login information
if (authToken != null) SerializationUtils.write(lastLoginFile, user, authToken);
// Save id generator
if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator);
// Save ID generator
if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator);
} catch (final IOException e) {
EnvoyLog.getLogger(LocalDB.class).log(Level.SEVERE, "Unable to save local database: ", e);
}
}
@Event
private void onNewAuthToken(NewAuthToken evt) { authToken = evt.get(); }

View File

@ -2,9 +2,14 @@ package envoy.client.data;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.prefs.Preferences;
import envoy.util.SerializationUtils;
import envoy.client.event.EnvoyCloseEvent;
import envoy.util.*;
import dev.kske.eventbus.*;
import dev.kske.eventbus.EventListener;
/**
* Manages all application settings, which are different objects that can be
@ -20,7 +25,7 @@ import envoy.util.SerializationUtils;
* @author Kai S. K. Engelbart
* @since Envoy Client v0.2-alpha
*/
public final class Settings {
public final class Settings implements EventListener {
// Actual settings accessible by the rest of the application
private Map<String, SettingsItem<?>> items;
@ -42,6 +47,8 @@ public final class Settings {
* @since Envoy Client v0.2-alpha
*/
private Settings() {
EventBus.getInstance().registerListener(this);
// Load settings from settings file
try {
items = SerializationUtils.read(settingsFile, HashMap.class);
@ -65,10 +72,16 @@ public final class Settings {
* @throws IOException if an error occurs while saving the themes
* @since Envoy Client v0.2-alpha
*/
public void save() throws IOException {
@Event(eventType = EnvoyCloseEvent.class, priority = 900)
private void save() {
EnvoyLog.getLogger(Settings.class).log(Level.INFO, "Saving settings...");
// Save settings to settings file
SerializationUtils.write(settingsFile, items);
try {
SerializationUtils.write(settingsFile, items);
} catch (final IOException e) {
EnvoyLog.getLogger(Settings.class).log(Level.SEVERE, "Unable to save settings file: ", e);
}
}
private void supplementDefaults() {