Add method to set a configuration after creation time.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 0f95d56..5563bf6 100644 (file)
@@ -84,7 +84,7 @@ public class Core implements IdentityListener {
        private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
 
        /** The configuration. */
-       private final Configuration configuration;
+       private Configuration configuration;
 
        /** The identity manager. */
        private final IdentityManager identityManager;
@@ -190,6 +190,18 @@ public class Core implements IdentityListener {
        //
 
        /**
+        * Sets the configuration to use. This will automatically save the current
+        * configuration to the given configuration.
+        *
+        * @param configuration
+        *            The new configuration to use
+        */
+       public void setConfiguration(Configuration configuration) {
+               this.configuration = configuration;
+               saveConfiguration();
+       }
+
+       /**
         * Returns the options used by the core.
         *
         * @return The options of the core
@@ -495,9 +507,23 @@ public class Core implements IdentityListener {
         * @return The post, or {@code null} if there is no such post
         */
        public Post getPost(String postId) {
+               return getPost(postId, true);
+       }
+
+       /**
+        * Returns the post with the given ID, optionally creating a new post.
+        *
+        * @param postId
+        *            The ID of the post to get
+        * @param create
+        *            {@code true} it create a new post if no post with the given ID
+        *            exists, {@code false} to return {@code null}
+        * @return The post, or {@code null} if there is no such post
+        */
+       public Post getPost(String postId, boolean create) {
                synchronized (posts) {
                        Post post = posts.get(postId);
-                       if (post == null) {
+                       if ((post == null) && create) {
                                post = new Post(postId);
                                posts.put(postId, post);
                        }
@@ -712,6 +738,8 @@ public class Core implements IdentityListener {
                                                return;
                                        }
                                        logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
+                                       coreListenerManager.fireRescuingSone(sone);
+                                       lockSone(sone);
                                        long edition = sone.getLatestEdition();
                                        while (!stopped && (edition >= 0) && isSoneRescueMode()) {
                                                logger.log(Level.FINE, "Downloading edition " + edition + "…");
@@ -719,6 +747,8 @@ public class Core implements IdentityListener {
                                                --edition;
                                        }
                                        logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
+                                       saveSone(sone);
+                                       coreListenerManager.fireRescuedSone(sone);
                                        soneInserter.start();
                                }
 
@@ -806,8 +836,8 @@ public class Core implements IdentityListener {
                                }
                                synchronized (newPosts) {
                                        for (Post post : sone.getPosts()) {
+                                               post.setSone(getSone(post.getSone().getId()));
                                                if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
-                                                       post.setSone(getSone(post.getSone().getId()));
                                                        newPosts.add(post.getId());
                                                        coreListenerManager.fireNewPostFound(post);
                                                }
@@ -823,6 +853,7 @@ public class Core implements IdentityListener {
                                }
                                synchronized (newReplies) {
                                        for (Reply reply : sone.getReplies()) {
+                                               reply.setSone(getSone(reply.getSone().getId()));
                                                if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
                                                        newReplies.add(reply.getId());
                                                        coreListenerManager.fireNewReplyFound(reply);
@@ -832,7 +863,9 @@ public class Core implements IdentityListener {
                                }
                        }
                        synchronized (storedSone) {
-                               storedSone.setTime(sone.getTime());
+                               if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
+                                       storedSone.setTime(sone.getTime());
+                               }
                                storedSone.setClient(sone.getClient());
                                storedSone.setProfile(sone.getProfile());
                                if (soneRescueMode) {
@@ -854,7 +887,7 @@ public class Core implements IdentityListener {
                                        storedSone.setLikePostIds(sone.getLikedPostIds());
                                        storedSone.setLikeReplyIds(sone.getLikedReplyIds());
                                }
-                               storedSone.setLatestEdition(sone.getRequestUri().getEdition());
+                               storedSone.setLatestEdition(sone.getLatestEdition());
                        }
                }
        }