Check for new posts and replies in the correct places.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 895f45e..7872c94 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;
 
@@ -150,6 +155,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
        //
 
@@ -301,9 +330,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,11 +438,26 @@ 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;
                }
        }
 
        /**
+        * Returns whether the given Sone has been modified.
+        *
+        * @param sone
+        *            The Sone to check for modifications
+        * @return {@code true} if a modification has been detected in the Sone,
+        *         {@code false} otherwise
+        */
+       public boolean isModifiedSone(Sone sone) {
+               return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
+       }
+
+       /**
         * Returns the post with the given ID.
         *
         * @param postId
@@ -430,6 +488,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;
                }
        }
@@ -485,6 +546,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;
                }
        }
@@ -566,18 +630,19 @@ 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);
                        soneInserters.put(sone, soneInserter);
-                       soneInserter.start();
                        setSoneStatus(sone, SoneStatus.idle);
                        loadSone(sone);
+                       soneInserter.start();
                        new Thread(new Runnable() {
 
                                @Override
@@ -601,10 +666,6 @@ public class Core implements IdentityListener {
        public Sone createSone(OwnIdentity ownIdentity) {
                identityManager.addContext(ownIdentity, "Sone");
                Sone sone = addLocalSone(ownIdentity);
-               synchronized (sone) {
-                       /* mark as modified so that it gets inserted immediately. */
-                       sone.setModificationCounter(sone.getModificationCounter() + 1);
-               }
                return sone;
        }
 
@@ -627,7 +688,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);
@@ -665,7 +732,7 @@ 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());
                                                }
                                                posts.put(post.getId(), post);
@@ -678,7 +745,7 @@ 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());
                                                }
                                                replies.put(reply.getId(), reply);
@@ -687,13 +754,13 @@ 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());
                                storedSone.setLikePostIds(sone.getLikedPostIds());
                                storedSone.setLikeReplyIds(sone.getLikedReplyIds());
                                storedSone.setLatestEdition(sone.getRequestUri().getEdition());
-                               storedSone.setModificationCounter(0);
                        }
                }
        }
@@ -748,7 +815,7 @@ public class Core implements IdentityListener {
                        logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
                        return;
                }
-               long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
+               String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
 
                /* load profile. */
                Profile profile = new Profile();
@@ -833,7 +900,7 @@ public class Core implements IdentityListener {
                        sone.setLikePostIds(likedPostIds);
                        sone.setLikeReplyIds(likedReplyIds);
                        sone.setFriends(friends);
-                       sone.setModificationCounter(soneModificationCounter);
+                       soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                }
                synchronized (newSones) {
                        for (String friend : friends) {
@@ -875,7 +942,7 @@ public class Core implements IdentityListener {
                        /* save Sone into configuration. */
                        String sonePrefix = "Sone/" + sone.getId();
                        configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
-                       configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
+                       configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
 
                        /* save profile. */
                        Profile profile = sone.getProfile();
@@ -999,9 +1066,10 @@ public class Core implements IdentityListener {
         *            The post that this reply refers to
         * @param text
         *            The text of the reply
+        * @return The created reply
         */
-       public void createReply(Sone sone, Post post, String text) {
-               createReply(sone, post, System.currentTimeMillis(), text);
+       public Reply createReply(Sone sone, Post post, String text) {
+               return createReply(sone, post, System.currentTimeMillis(), text);
        }
 
        /**
@@ -1015,11 +1083,12 @@ public class Core implements IdentityListener {
         *            The time of the reply
         * @param text
         *            The text of the reply
+        * @return The created reply
         */
-       public void createReply(Sone sone, Post post, long time, String text) {
+       public Reply createReply(Sone sone, Post post, long time, String text) {
                if (!isLocalSone(sone)) {
                        logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
-                       return;
+                       return null;
                }
                Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
                synchronized (replies) {
@@ -1030,6 +1099,7 @@ public class Core implements IdentityListener {
                }
                sone.addReply(reply);
                saveSone(sone);
+               return reply;
        }
 
        /**