From: AsamK Date: Thu, 27 Oct 2016 14:03:20 +0000 (+0200) Subject: Split load function X-Git-Tag: v0.5.1~11 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/6c9f26f49b46d39421e822cf0be6b299f161c5bf?ds=sidebyside Split load function --- diff --git a/src/main/java/org/asamk/signal/Main.java b/src/main/java/org/asamk/signal/Main.java index fbdc0ce1..5dd471f9 100644 --- a/src/main/java/org/asamk/signal/Main.java +++ b/src/main/java/org/asamk/signal/Main.java @@ -110,7 +110,7 @@ public class Main { ts = m; if (m.userExists()) { try { - m.load(); + m.init(); } catch (Exception e) { System.err.println("Error loading state file \"" + m.getFileName() + "\": " + e.getMessage()); return 2; diff --git a/src/main/java/org/asamk/signal/Manager.java b/src/main/java/org/asamk/signal/Manager.java index e02258f1..34bb5068 100644 --- a/src/main/java/org/asamk/signal/Manager.java +++ b/src/main/java/org/asamk/signal/Manager.java @@ -211,7 +211,23 @@ class Manager implements Signal { } } - public void load() throws IOException, InvalidKeyException { + public void init() throws IOException { + load(); + + migrateLegacyConfigs(); + + accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, deviceId, USER_AGENT); + try { + if (registered && accountManager.getPreKeysCount() < PREKEY_MINIMUM_COUNT) { + refreshPreKeys(); + save(); + } + } catch (AuthorizationFailedException e) { + System.err.println("Authorization failed, was the number registered elsewhere?"); + } + } + + private void load() throws IOException { openFileChannel(); JsonNode rootNode = jsonProcessot.readTree(Channels.newInputStream(fileChannel)); @@ -243,10 +259,21 @@ class Manager implements Signal { if (groupStore == null) { groupStore = new JsonGroupStore(); } + + JsonNode contactStoreNode = rootNode.get("contactStore"); + if (contactStoreNode != null) { + contactStore = jsonProcessot.convertValue(contactStoreNode, JsonContactsStore.class); + } + if (contactStore == null) { + contactStore = new JsonContactsStore(); + } + } + + private void migrateLegacyConfigs() { // Copy group avatars that were previously stored in the attachments folder // to the new avatar folder - if (groupStore.groupsWithLegacyAvatarId.size() > 0) { - for (GroupInfo g : groupStore.groupsWithLegacyAvatarId) { + if (JsonGroupStore.groupsWithLegacyAvatarId.size() > 0) { + for (GroupInfo g : JsonGroupStore.groupsWithLegacyAvatarId) { File avatarFile = getGroupAvatarFile(g.groupId); File attachmentFile = getAttachmentFile(g.getAvatarId()); if (!avatarFile.exists() && attachmentFile.exists()) { @@ -258,27 +285,9 @@ class Manager implements Signal { } } } - groupStore.groupsWithLegacyAvatarId.clear(); + JsonGroupStore.groupsWithLegacyAvatarId.clear(); save(); } - - JsonNode contactStoreNode = rootNode.get("contactStore"); - if (contactStoreNode != null) { - contactStore = jsonProcessot.convertValue(contactStoreNode, JsonContactsStore.class); - } - if (contactStore == null) { - contactStore = new JsonContactsStore(); - } - - accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, deviceId, USER_AGENT); - try { - if (registered && accountManager.getPreKeysCount() < PREKEY_MINIMUM_COUNT) { - refreshPreKeys(); - save(); - } - } catch (AuthorizationFailedException e) { - System.err.println("Authorization failed, was the number registered elsewhere?"); - } } private void save() {