Add a “Sone rescue mode.”
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 90d3696..ccf2cd3 100644 (file)
@@ -31,6 +31,7 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.core.Options.DefaultOption;
 import net.pterodactylus.sone.core.Options.Option;
 import net.pterodactylus.sone.core.Options.OptionWatcher;
+import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
@@ -39,6 +40,7 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityListener;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
 import net.pterodactylus.util.logging.Logging;
@@ -78,6 +80,9 @@ public class Core implements IdentityListener {
        /** The options. */
        private final Options options = new Options();
 
+       /** The core listener manager. */
+       private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
+
        /** The configuration. */
        private final Configuration configuration;
 
@@ -90,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>();
@@ -150,6 +158,30 @@ public class Core implements IdentityListener {
        }
 
        //
+       // LISTENER MANAGEMENT
+       //
+
+       /**
+        * Adds a new core listener.
+        *
+        * @param coreListener
+        *            The listener to add
+        */
+       public void addCoreListener(CoreListener coreListener) {
+               coreListenerManager.addListener(coreListener);
+       }
+
+       /**
+        * Removes a core listener.
+        *
+        * @param coreListener
+        *            The listener to remove
+        */
+       public void removeCoreListener(CoreListener coreListener) {
+               coreListenerManager.removeListener(coreListener);
+       }
+
+       //
        // ACCESSORS
        //
 
@@ -163,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
@@ -301,9 +343,23 @@ public class Core implements IdentityListener {
         * @return The Sone with the given ID
         */
        public Sone getLocalSone(String id) {
+               return getLocalSone(id, true);
+       }
+
+       /**
+        * Returns the local Sone with the given ID, optionally creating a new Sone.
+        *
+        * @param id
+        *            The ID of the Sone
+        * @param create
+        *            {@code true} to create a new Sone if none exists,
+        *            {@code false} to return null if none exists
+        * @return The Sone with the given ID, or {@code null}
+        */
+       public Sone getLocalSone(String id, boolean create) {
                synchronized (localSones) {
                        Sone sone = localSones.get(id);
-                       if (sone == null) {
+                       if ((sone == null) && create) {
                                sone = new Sone(id);
                                localSones.put(id, sone);
                        }
@@ -395,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;
                }
        }
@@ -442,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;
                }
        }
@@ -497,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;
                }
        }
@@ -578,11 +643,12 @@ public class Core implements IdentityListener {
                        final Sone sone;
                        try {
                                sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
-                               sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
                        } catch (MalformedURLException mue1) {
                                logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
                                return null;
                        }
+                       sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
+                       sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
                        /* TODO - load posts ’n stuff */
                        localSones.put(ownIdentity.getId(), sone);
                        SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
@@ -635,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);
@@ -673,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);
                                        }
@@ -686,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);
                                        }
@@ -695,6 +769,7 @@ public class Core implements IdentityListener {
                        }
                        synchronized (storedSone) {
                                storedSone.setTime(sone.getTime());
+                               storedSone.setClient(sone.getClient());
                                storedSone.setProfile(sone.getProfile());
                                storedSone.setPosts(sone.getPosts());
                                storedSone.setReplies(sone.getReplies());
@@ -1078,6 +1153,7 @@ public class Core implements IdentityListener {
                        }
                }
                saveConfiguration();
+               stopped = true;
        }
 
        //
@@ -1098,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));
 
@@ -1113,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;
@@ -1159,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());