Add a “Sone rescue mode.”
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 2f69bee..ccf2cd3 100644 (file)
@@ -95,6 +95,9 @@ public class Core implements IdentityListener {
        /** The Sone downloader. */
        private final SoneDownloader soneDownloader;
 
+       /** Whether the core has been stopped. */
+       private volatile boolean stopped;
+
        /** The Sones’ statuses. */
        /* synchronize access on itself. */
        private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
@@ -192,6 +195,16 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the “Sone rescue mode” is currently activated.
+        *
+        * @return {@code true} if the “Sone rescue mode” is currently activated,
+        *         {@code false} if it is not
+        */
+       public boolean isSoneRescueMode() {
+               return options.getBooleanOption("SoneRescueMode").get();
+       }
+
+       /**
         * Returns the identity manager used by the core.
         *
         * @return The identity manager
@@ -438,6 +451,9 @@ public class Core implements IdentityListener {
                synchronized (newSones) {
                        boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
                        knownSones.add(sone.getId());
+                       if (isNew) {
+                               coreListenerManager.fireMarkSoneKnown(sone);
+                       }
                        return isNew;
                }
        }
@@ -485,6 +501,9 @@ public class Core implements IdentityListener {
                synchronized (newPosts) {
                        boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
                        knownPosts.add(postId);
+                       if (isNew) {
+                               coreListenerManager.fireMarkPostKnown(getPost(postId));
+                       }
                        return isNew;
                }
        }
@@ -540,6 +559,9 @@ public class Core implements IdentityListener {
                synchronized (newReplies) {
                        boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
                        knownReplies.add(replyId);
+                       if (isNew) {
+                               coreListenerManager.fireMarkReplyKnown(getReply(replyId));
+                       }
                        return isNew;
                }
        }
@@ -679,7 +701,13 @@ public class Core implements IdentityListener {
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
                        if (newSone) {
                                synchronized (newSones) {
-                                       newSones.add(sone.getId());
+                                       newSone = !knownSones.contains(sone.getId());
+                                       if (newSone) {
+                                               newSones.add(sone.getId());
+                                       }
+                               }
+                               if (newSone) {
+                                       coreListenerManager.fireNewSoneFound(sone);
                                }
                        }
                        remoteSones.put(identity.getId(), sone);
@@ -717,8 +745,9 @@ public class Core implements IdentityListener {
                                }
                                synchronized (newPosts) {
                                        for (Post post : sone.getPosts()) {
-                                               if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
+                                               if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
                                                        newPosts.add(post.getId());
+                                                       coreListenerManager.fireNewPostFound(post);
                                                }
                                                posts.put(post.getId(), post);
                                        }
@@ -730,8 +759,9 @@ public class Core implements IdentityListener {
                                }
                                synchronized (newReplies) {
                                        for (Reply reply : sone.getReplies()) {
-                                               if (!storedSone.getReplies().contains(reply) && !knownSones.contains(reply.getId())) {
+                                               if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
                                                        newReplies.add(reply.getId());
+                                                       coreListenerManager.fireNewReplyFound(reply);
                                                }
                                                replies.put(reply.getId(), reply);
                                        }
@@ -1123,6 +1153,7 @@ public class Core implements IdentityListener {
                        }
                }
                saveConfiguration();
+               stopped = true;
        }
 
        //
@@ -1143,6 +1174,7 @@ public class Core implements IdentityListener {
                        }
 
                }));
+               options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
                options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
                options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
 
@@ -1158,6 +1190,7 @@ public class Core implements IdentityListener {
                }
 
                options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
+               options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
 
                /* load known Sones. */
                int soneCounter = 0;
@@ -1204,6 +1237,7 @@ public class Core implements IdentityListener {
                /* store the options first. */
                try {
                        configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
+                       configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
                        configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
                        configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());