Convert “new post reply found” into EventBus-based event.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index fcde685..0a2c534 100644 (file)
@@ -35,6 +35,9 @@ 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.core.event.NewPostFoundEvent;
+import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
+import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
@@ -70,6 +73,9 @@ import net.pterodactylus.util.version.Version;
 
 import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Inject;
+
 import freenet.keys.FreenetURI;
 
 /**
@@ -94,6 +100,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        /** The core listener manager. */
        private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
 
+       /** The event bus. */
+       private final EventBus eventBus;
+
        /** The configuration. */
        private Configuration configuration;
 
@@ -191,8 +200,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The identity manager
         * @param webOfTrustUpdater
         *            The WebOfTrust updater
+        * @param eventBus
+        *            The event bus
         */
-       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater) {
+       @Inject
+       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus) {
                super("Sone Core");
                this.configuration = configuration;
                this.freenetInterface = freenetInterface;
@@ -201,6 +213,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                this.imageInserter = new ImageInserter(this, freenetInterface);
                this.updateChecker = new UpdateChecker(freenetInterface);
                this.webOfTrustUpdater = webOfTrustUpdater;
+               this.eventBus = eventBus;
        }
 
        //
@@ -416,7 +429,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                sone = new Sone(id, true);
                                sones.put(id, sone);
                        }
-                       if (!sone.isLocal()) {
+                       if ((sone != null) && !sone.isLocal()) {
                                sone = new Sone(id, true);
                                sones.put(id, sone);
                        }
@@ -882,7 +895,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                }
                                sone.setKnown(!newSone);
                                if (newSone) {
-                                       coreListenerManager.fireNewSoneFound(sone);
+                                       eventBus.post(new NewSoneFoundEvent(sone));
                                        for (Sone localSone : getLocalSones()) {
                                                if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
                                                        followSone(localSone, sone);
@@ -1103,7 +1116,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                                                knownPosts.add(post.getId());
                                                                post.setKnown(true);
                                                        } else if (!knownPosts.contains(post.getId())) {
-                                                               coreListenerManager.fireNewPostFound(post);
+                                                               eventBus.post(new NewPostFoundEvent(post));
                                                        }
                                                }
                                                posts.put(post.getId(), post);
@@ -1128,7 +1141,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                                                knownReplies.add(reply.getId());
                                                                reply.setKnown(true);
                                                        } else if (!knownReplies.contains(reply.getId())) {
-                                                               coreListenerManager.fireNewReplyFound(reply);
+                                                               eventBus.post(new NewPostReplyFoundEvent(reply));
                                                        }
                                                }
                                                replies.put(reply.getId(), reply);
@@ -1532,7 +1545,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                synchronized (posts) {
                        posts.put(post.getId(), post);
                }
-               coreListenerManager.fireNewPostFound(post);
+               eventBus.post(new NewPostFoundEvent(post));
                sone.addPost(post);
                touchConfiguration();
                localElementTicker.registerEvent(System.currentTimeMillis() + 10 * 1000, new Runnable() {
@@ -1671,7 +1684,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        replies.put(reply.getId(), reply);
                }
                synchronized (knownReplies) {
-                       coreListenerManager.fireNewReplyFound(reply);
+                       eventBus.post(new NewPostReplyFoundEvent(reply));
                }
                sone.addReply(reply);
                touchConfiguration();
@@ -1893,6 +1906,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                loadConfiguration();
                updateChecker.addUpdateListener(this);
                updateChecker.start();
+               identityManager.addIdentityListener(this);
+               identityManager.start();
+               webOfTrustUpdater.init();
                webOfTrustUpdater.start();
        }
 
@@ -1932,6 +1948,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                updateChecker.stop();
                updateChecker.removeUpdateListener(this);
                soneDownloader.stop();
+               identityManager.removeIdentityListener(this);
+               identityManager.stop();
        }
 
        //