Merge branch 'develop' into f/system_commands

This commit is contained in:
delvh
2020-07-18 11:27:59 +02:00
40 changed files with 323 additions and 328 deletions

View File

@ -5,10 +5,8 @@ import static java.util.function.Function.identity;
import java.io.File;
import java.util.logging.Level;
import envoy.client.ui.Startup;
import envoy.data.Config;
import envoy.data.ConfigItem;
import envoy.data.LoginCredentials;
/**
* Implements a configuration specific to the Envoy Client with default values
@ -40,8 +38,8 @@ public class ClientConfig extends Config {
items.put("localDB", new ConfigItem<>("localDB", "db", File::new, new File("localDB"), true));
items.put("ignoreLocalDB", new ConfigItem<>("ignoreLocalDB", "nodb", Boolean::parseBoolean, false, false));
items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy"), true));
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.CONFIG, true));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true));
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.OFF, true));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.OFF, true));
items.put("user", new ConfigItem<>("user", "u", identity()));
items.put("password", new ConfigItem<>("password", "pw", identity()));
}
@ -105,11 +103,4 @@ public class ClientConfig extends Config {
* @since Envoy Client v0.3-alpha
*/
public boolean hasLoginCredentials() { return getUser() != null && getPassword() != null; }
/**
* @return login credentials for the specified user name and password, without
* the registration option
* @since Envoy Client v0.3-alpha
*/
public LoginCredentials getLoginCredentials() { return new LoginCredentials(getUser(), getPassword(), false, Startup.VERSION); }
}

View File

@ -1,7 +1,7 @@
package envoy.client.data;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.Instant;
import envoy.client.net.WriteProxy;
import envoy.data.Contact;
@ -46,7 +46,7 @@ public class GroupChat extends Chat {
else {
gmsg.getMemberStatuses().replace(sender.getID(), MessageStatus.READ);
writeProxy
.writeMessageStatusChange(new GroupMessageStatusChange(gmsg.getID(), MessageStatus.READ, LocalDateTime.now(), sender.getID()));
.writeMessageStatusChange(new GroupMessageStatusChange(gmsg.getID(), MessageStatus.READ, Instant.now(), sender.getID()));
}
}
}

View File

@ -1,5 +1,6 @@
package envoy.client.data;
import java.time.Instant;
import java.util.*;
import envoy.data.*;
@ -20,11 +21,12 @@ import envoy.event.NameChange;
*/
public abstract class LocalDB {
protected User user;
protected Map<String, Contact> users = new HashMap<>();
protected List<Chat> chats = new ArrayList<>();
protected IDGenerator idGenerator;
protected CacheMap cacheMap = new CacheMap();
protected User user;
protected Map<String, User> users = new HashMap<>();
protected List<Chat> chats = new ArrayList<>();
protected IDGenerator idGenerator;
protected CacheMap cacheMap = new CacheMap();
protected Instant lastSync = Instant.EPOCH;
{
cacheMap.put(Message.class, new Cache<>());
@ -42,10 +44,11 @@ public abstract class LocalDB {
* 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 Exception if the saving process failed
* @since Envoy Client v0.3-alpha
*/
public void save() throws Exception {}
public void save(boolean isOnline) throws Exception {}
/**
* Loads all user data.
@ -77,7 +80,7 @@ public abstract class LocalDB {
* @since Envoy Client v0.1-beta
*/
public void synchronize() {
user.getContacts().stream().filter(u -> u instanceof User && !users.containsKey(u.getName())).forEach(u -> users.put(u.getName(), u));
user.getContacts().stream().filter(u -> u instanceof User && !users.containsKey(u.getName())).forEach(u -> users.put(u.getName(), (User) u));
users.put(user.getName(), user);
// Synchronize user status data
@ -98,7 +101,7 @@ public abstract class LocalDB {
* user names as keys
* @since Envoy Client v0.2-alpha
*/
public Map<String, Contact> getUsers() { return users; }
public Map<String, User> getUsers() { return users; }
/**
* @return all saved {@link Chat} objects that list the client user as the
@ -148,6 +151,12 @@ public abstract class LocalDB {
*/
public CacheMap getCacheMap() { return cacheMap; }
/**
* @return the time stamp when the database was last saved
* @since Envoy Client v0.2-beta
*/
public Instant getLastSync() { return lastSync; }
/**
* Searches for a message by ID.
*

View File

@ -1,6 +1,7 @@
package envoy.client.data;
import java.io.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
@ -26,7 +27,7 @@ public final class PersistentLocalDB extends LocalDB {
/**
* 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()}.
* 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)
@ -57,12 +58,12 @@ public final class PersistentLocalDB extends LocalDB {
}
@Override
public void save() throws IOException {
public void save(boolean isOnline) throws IOException {
// Save users
SerializationUtils.write(usersFile, users);
// Save user data
if (user != null) SerializationUtils.write(userFile, chats, cacheMap);
// 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);
@ -76,6 +77,7 @@ public final class PersistentLocalDB extends LocalDB {
try (var in = new ObjectInputStream(new FileInputStream(userFile))) {
chats = (ArrayList<Chat>) in.readObject();
cacheMap = (CacheMap) in.readObject();
lastSync = (Instant) in.readObject();
}
}