Move dependency injection configuration closer to where it’s useful.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 9b60076..1229448 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.sone.core;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Predicates.not;
+import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
 
 import java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -37,8 +39,7 @@ import java.util.logging.Level;
 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.SoneInserter.SetInsertionDelay;
 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
@@ -62,6 +63,7 @@ import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.SoneImpl;
 import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.DatabaseException;
@@ -71,7 +73,6 @@ import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.database.PostReplyProvider;
 import net.pterodactylus.sone.database.SoneProvider;
 import net.pterodactylus.sone.fcp.FcpInterface;
-import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
@@ -89,8 +90,8 @@ import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultimap;
@@ -100,6 +101,7 @@ import com.google.common.collect.Multimaps;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
 import com.google.inject.Inject;
+import com.google.inject.Singleton;
 
 import freenet.keys.FreenetURI;
 
@@ -108,6 +110,7 @@ import freenet.keys.FreenetURI;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
 
        /** The logger. */
@@ -126,7 +129,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        private final EventBus eventBus;
 
        /** The configuration. */
-       private Configuration configuration;
+       private final Configuration configuration;
 
        /** Whether we’re currently saving the configuration. */
        private boolean storingConfiguration = false;
@@ -187,12 +190,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /** Trusted identities, sorted by own identities. */
        private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
 
-       /** All known albums. */
-       private final Map<String, Album> albums = new HashMap<String, Album>();
-
-       /** All known images. */
-       private final Map<String, Image> images = new HashMap<String, Image>();
-
        /** All temporary images. */
        private final Map<String, TemporaryImage> temporaryImages = new HashMap<String, TemporaryImage>();
 
@@ -224,8 +221,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                this.configuration = configuration;
                this.freenetInterface = freenetInterface;
                this.identityManager = identityManager;
-               this.soneDownloader = new SoneDownloader(this, freenetInterface);
-               this.imageInserter = new ImageInserter(freenetInterface);
+               this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
+               this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
                this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
                this.webOfTrustUpdater = webOfTrustUpdater;
                this.eventBus = eventBus;
@@ -246,18 +243,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * 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;
-               touchConfiguration();
-       }
-
-       /**
         * Returns the options used by the core.
         *
         * @return The options of the core
@@ -360,13 +345,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Override
        public Collection<Sone> getLocalSones() {
                synchronized (sones) {
-                       return FluentIterable.from(sones.values()).filter(new Predicate<Sone>() {
-
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return sone.isLocal();
-                               }
-                       }).toSet();
+                       return FluentIterable.from(sones.values()).filter(LOCAL_SONE_FILTER).toSet();
                }
        }
 
@@ -384,11 +363,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                synchronized (sones) {
                        Sone sone = sones.get(id);
                        if ((sone == null) && create) {
-                               sone = new Sone(id, true);
+                               sone = new SoneImpl(id, true);
                                sones.put(id, sone);
                        }
                        if ((sone != null) && !sone.isLocal()) {
-                               sone = new Sone(id, true);
+                               sone = new SoneImpl(id, true);
                                sones.put(id, sone);
                        }
                        return sone;
@@ -401,13 +380,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Override
        public Collection<Sone> getRemoteSones() {
                synchronized (sones) {
-                       return FluentIterable.from(sones.values()).filter(new Predicate<Sone>() {
-
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return !sone.isLocal();
-                               }
-                       }).toSet();
+                       return FluentIterable.from(sones.values()).filter(not(LOCAL_SONE_FILTER)).toSet();
                }
        }
 
@@ -425,7 +398,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                synchronized (sones) {
                        Sone sone = sones.get(id);
                        if ((sone == null) && create && (id != null) && (id.length() == 43)) {
-                               sone = new Sone(id, false);
+                               sone = new SoneImpl(id, false);
                                sones.put(id, sone);
                        }
                        return sone;
@@ -441,7 +414,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         {@code false} otherwise
         */
        public boolean isModifiedSone(Sone sone) {
-               return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
+               return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
        }
 
        /**
@@ -603,7 +576,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                synchronized (bookmarkedPosts) {
                        for (String bookmarkedPostId : bookmarkedPosts) {
                                Optional<Post> post = getPost(bookmarkedPostId);
-                               if (!post.isPresent()) {
+                               if (post.isPresent()) {
                                        posts.add(post.get());
                                }
                        }
@@ -636,14 +609,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         given ID exists and {@code create} is {@code false}
         */
        public Album getAlbum(String albumId, boolean create) {
-               synchronized (albums) {
-                       Album album = albums.get(albumId);
-                       if (create && (album == null)) {
-                               album = new Album(albumId);
-                               albums.put(albumId, album);
-                       }
-                       return album;
+               Optional<Album> album = database.getAlbum(albumId);
+               if (album.isPresent()) {
+                       return album.get();
+               }
+               if (!create) {
+                       return null;
                }
+               Album newAlbum = database.newAlbumBuilder().withId(albumId).build();
+               database.storeAlbum(newAlbum);
+               return newAlbum;
        }
 
        /**
@@ -670,14 +645,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         none was created
         */
        public Image getImage(String imageId, boolean create) {
-               synchronized (images) {
-                       Image image = images.get(imageId);
-                       if (create && (image == null)) {
-                               image = new Image(imageId);
-                               images.put(imageId, image);
-                       }
-                       return image;
+               Optional<Image> image = database.getImage(imageId);
+               if (image.isPresent()) {
+                       return image.get();
                }
+               if (!create) {
+                       return null;
+               }
+               Image newImage = database.newImageBuilder().withId(imageId).build();
+               database.storeImage(newImage);
+               return newImage;
        }
 
        /**
@@ -777,12 +754,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                Sone sone = addLocalSone(ownIdentity);
-               sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
-               sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption<Boolean>(false));
-               sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption<Boolean>(true));
-               sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption<Boolean>(true));
-               sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption<Boolean>(true));
-               sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption<ShowCustomAvatars>(ShowCustomAvatars.NEVER));
 
                followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
                touchConfiguration();
@@ -818,22 +789,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                if (newSone) {
                                        eventBus.post(new NewSoneFoundEvent(sone));
                                        for (Sone localSone : getLocalSones()) {
-                                               if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
+                                               if (localSone.getOptions().isAutoFollow()) {
                                                        followSone(localSone, sone.getId());
                                                }
                                        }
                                }
                        }
                        soneDownloader.addSone(sone);
-                       soneDownloaders.execute(new Runnable() {
-
-                               @Override
-                               @SuppressWarnings("synthetic-access")
-                               public void run() {
-                                       soneDownloader.fetchSone(sone, sone.getRequestUri());
-                               }
-
-                       });
+                       soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
                        return sone;
                }
        }
@@ -995,10 +958,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                return;
                        }
                        /* find removed posts. */
+                       Collection<Post> removedPosts = new ArrayList<Post>();
+                       Collection<Post> newPosts = new ArrayList<Post>();
                        Collection<Post> existingPosts = database.getPosts(sone.getId());
                        for (Post oldPost : existingPosts) {
                                if (!sone.getPosts().contains(oldPost)) {
-                                       eventBus.post(new PostRemovedEvent(oldPost));
+                                       removedPosts.add(oldPost);
                                }
                        }
                        /* find new posts. */
@@ -1009,15 +974,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                if (newPost.getTime() < getSoneFollowingTime(sone)) {
                                        newPost.setKnown(true);
                                } else if (!newPost.isKnown()) {
-                                       eventBus.post(new NewPostFoundEvent(newPost));
+                                       newPosts.add(newPost);
                                }
                        }
                        /* store posts. */
                        database.storePosts(sone, sone.getPosts());
+                       Collection<PostReply> newPostReplies = new ArrayList<PostReply>();
+                       Collection<PostReply> removedPostReplies = new ArrayList<PostReply>();
                        if (!soneRescueMode) {
                                for (PostReply reply : storedSone.get().getReplies()) {
                                        if (!sone.getReplies().contains(reply)) {
-                                               eventBus.post(new PostReplyRemovedEvent(reply));
+                                               removedPostReplies.add(reply);
                                        }
                                }
                        }
@@ -1029,29 +996,42 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                if (reply.getTime() < getSoneFollowingTime(sone)) {
                                        reply.setKnown(true);
                                } else if (!reply.isKnown()) {
-                                       eventBus.post(new NewPostReplyFoundEvent(reply));
+                                       newPostReplies.add(reply);
                                }
                        }
                        database.storePostReplies(sone, sone.getReplies());
-                       synchronized (albums) {
-                               synchronized (images) {
-                                       for (Album album : storedSone.get().getAlbums()) {
-                                               albums.remove(album.getId());
-                                               for (Image image : album.getImages()) {
-                                                       images.remove(image.getId());
-                                               }
-                                       }
-                                       for (Album album : sone.getAlbums()) {
-                                               albums.put(album.getId(), album);
-                                               for (Image image : album.getImages()) {
-                                                       images.put(image.getId(), image);
-                                               }
-                                       }
+                       for (Album album : storedSone.get().getRootAlbum().getAlbums()) {
+                               database.removeAlbum(album);
+                               for (Image image : album.getImages()) {
+                                       database.removeImage(image);
+                               }
+                       }
+                       for (Post removedPost : removedPosts) {
+                               eventBus.post(new PostRemovedEvent(removedPost));
+                       }
+                       for (Post newPost : newPosts) {
+                               eventBus.post(new NewPostFoundEvent(newPost));
+                       }
+                       for (PostReply removedPostReply : removedPostReplies) {
+                               eventBus.post(new PostReplyRemovedEvent(removedPostReply));
+                       }
+                       for (PostReply newPostReply : newPostReplies) {
+                               eventBus.post(new NewPostReplyFoundEvent(newPostReply));
+                       }
+                       for (Album album : sone.getRootAlbum().getAlbums()) {
+                               database.storeAlbum(album);
+                               for (Image image : album.getImages()) {
+                                       database.storeImage(image);
                                }
                        }
                        synchronized (sones) {
                                sone.setOptions(storedSone.get().getOptions());
                                sone.setKnown(storedSone.get().isKnown());
+                               sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
+                               if (sone.isLocal()) {
+                                       soneInserters.get(storedSone.get()).setSone(sone);
+                                       touchConfiguration();
+                               }
                                sones.put(sone.getId(), sone);
                        }
                }
@@ -1120,14 +1100,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
                logger.info(String.format("Loading local Sone: %s", sone));
 
-               /* initialize options. */
-               sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
-               sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption<Boolean>(false));
-               sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption<Boolean>(true));
-               sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption<Boolean>(true));
-               sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption<Boolean>(true));
-               sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption<ShowCustomAvatars>(ShowCustomAvatars.NEVER));
-
                /* load Sone. */
                String sonePrefix = "Sone/" + sone.getId();
                Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
@@ -1245,7 +1217,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid album found, aborting load!");
                                return;
                        }
-                       Album album = getAlbum(albumId).setSone(sone).setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId);
+                       Album album = getAlbum(albumId).setSone(sone).modify().setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId).update();
                        if (albumParentId != null) {
                                Album parentAlbum = getAlbum(albumParentId, false);
                                if (parentAlbum == null) {
@@ -1284,8 +1256,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid album image encountered, aborting load!");
                                return;
                        }
-                       Image image = getImage(imageId).setSone(sone).setCreationTime(creationTime).setKey(key);
-                       image.setTitle(title).setDescription(description).setWidth(width).setHeight(height);
+                       Image image = getImage(imageId).modify().setSone(sone).setCreationTime(creationTime).setKey(key).setTitle(title).setDescription(description).setWidth(width).setHeight(height).update();
                        album.addImage(image);
                }
 
@@ -1296,12 +1267,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
 
                /* load options. */
-               sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
-               sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
-               sone.getOptions().getBooleanOption("ShowNotification/NewSones").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
-               sone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
-               sone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
-               sone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
+               sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
+               sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
+               sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
+               sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
+               sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
+               sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
 
                /* if we’re still here, Sone was loaded successfully. */
                synchronized (sone) {
@@ -1314,7 +1285,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        for (String friendId : friends) {
                                followSone(sone, friendId);
                        }
-                       sone.setAlbums(topLevelAlbums);
+                       for (Album album : sone.getRootAlbum().getAlbums()) {
+                               sone.getRootAlbum().removeAlbum(album);
+                       }
+                       for (Album album : topLevelAlbums) {
+                               sone.getRootAlbum().addAlbum(album);
+                       }
                        soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                }
                synchronized (knownSones) {
@@ -1409,16 +1385,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                eventBus.post(new NewPostFoundEvent(post));
                sone.addPost(post);
                touchConfiguration();
-               localElementTicker.schedule(new Runnable() {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @Override
-                       public void run() {
-                               markPostKnown(post);
-                       }
-               }, 10, TimeUnit.SECONDS);
+               localElementTicker.schedule(new MarkPostKnown(post), 10, TimeUnit.SECONDS);
                return post;
        }
 
@@ -1524,16 +1491,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                eventBus.post(new NewPostReplyFoundEvent(reply));
                sone.addReply(reply);
                touchConfiguration();
-               localElementTicker.schedule(new Runnable() {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @Override
-                       public void run() {
-                               markReplyKnown(reply);
-                       }
-               }, 10, TimeUnit.SECONDS);
+               localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS);
                return reply;
        }
 
@@ -1579,7 +1537,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return The new album
         */
        public Album createAlbum(Sone sone) {
-               return createAlbum(sone, null);
+               return createAlbum(sone, sone.getRootAlbum());
        }
 
        /**
@@ -1593,16 +1551,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return The new album
         */
        public Album createAlbum(Sone sone, Album parent) {
-               Album album = new Album();
-               synchronized (albums) {
-                       albums.put(album.getId(), album);
-               }
+               Album album = database.newAlbumBuilder().randomId().build();
+               database.storeAlbum(album);
                album.setSone(sone);
-               if (parent != null) {
-                       parent.addAlbum(album);
-               } else {
-                       sone.addAlbum(album);
-               }
+               parent.addAlbum(album);
                return album;
        }
 
@@ -1619,14 +1571,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                if (!album.isEmpty()) {
                        return;
                }
-               if (album.getParent() == null) {
-                       album.getSone().removeAlbum(album);
-               } else {
-                       album.getParent().removeAlbum(album);
-               }
-               synchronized (albums) {
-                       albums.remove(album.getId());
-               }
+               album.getParent().removeAlbum(album);
+               database.removeAlbum(album);
                touchConfiguration();
        }
 
@@ -1647,11 +1593,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                checkNotNull(temporaryImage, "temporaryImage must not be null");
                checkArgument(sone.isLocal(), "sone must be a local Sone");
                checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
-               Image image = new Image(temporaryImage.getId()).setSone(sone).setCreationTime(System.currentTimeMillis());
+               Image image = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update();
                album.addImage(image);
-               synchronized (images) {
-                       images.put(image.getId(), image);
-               }
+               database.storeImage(image);
                imageInserter.insertImage(temporaryImage, image);
                return image;
        }
@@ -1669,9 +1613,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
                deleteTemporaryImage(image.getId());
                image.getAlbum().removeImage(image);
-               synchronized (images) {
-                       images.remove(image.getId());
-               }
+               database.removeImage(image);
                touchConfiguration();
        }
 
@@ -1775,7 +1717,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                synchronized (sones) {
                        for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
                                soneInserter.getValue().stop();
-                               saveSone(soneInserter.getKey());
+                               saveSone(getLocalSone(soneInserter.getKey().getId(), false));
                        }
                }
                saveConfiguration();
@@ -1878,7 +1820,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
                        /* save albums. first, collect in a flat structure, top-level first. */
-                       List<Album> albums = FluentIterable.from(sone.getAlbums()).transformAndConcat(Album.FLATTENER).toList();
+                       List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
 
                        int albumCounter = 0;
                        for (Album album : albums) {
@@ -1886,7 +1828,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
                                configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
                                configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
-                               configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent() == null ? null : album.getParent().getId());
+                               configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
                                configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId());
                        }
                        configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
@@ -1912,12 +1854,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null);
 
                        /* save options. */
-                       configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
-                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewSones").getReal());
-                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewPosts").getReal());
-                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewReplies").getReal());
-                       configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").getReal());
-                       configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").get().name());
+                       configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow());
+                       configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled());
+                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
+                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
+                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
+                       configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
 
                        configuration.save();
 
@@ -2007,14 +1949,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        private void loadConfiguration() {
                /* create options. */
-               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher<Integer>() {
-
-                       @Override
-                       public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
-                               SoneInserter.setInsertionDelay(newValue);
-                       }
-
-               }));
+               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new SetInsertionDelay()));
                options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
                options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
                options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, Predicates.<Integer> or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
@@ -2023,23 +1958,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
                options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
                options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
-               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, new OptionWatcher<Boolean>() {
-
-                       @Override
-                       @SuppressWarnings("synthetic-access")
-                       public void optionChanged(Option<Boolean> option, Boolean oldValue, Boolean newValue) {
-                               fcpInterface.setActive(newValue);
-                       }
-               }));
-               options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2, new OptionWatcher<Integer>() {
-
-                       @Override
-                       @SuppressWarnings("synthetic-access")
-                       public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
-                               fcpInterface.setFullAccessRequired(FullAccessRequired.values()[newValue]);
-                       }
-
-               }));
+               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, fcpInterface.new SetActive()));
+               options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2, fcpInterface.new SetFullAccessRequired()));
 
                loadConfigurationValue("InsertionDelay");
                loadConfigurationValue("PostsPerPage");
@@ -2158,22 +2078,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        @Subscribe
        public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
-               final Identity identity = identityUpdatedEvent.identity();
-               soneDownloaders.execute(new Runnable() {
-
-                       @Override
-                       @SuppressWarnings("synthetic-access")
-                       public void run() {
-                               Sone sone = getRemoteSone(identity.getId(), false);
-                               if (sone.isLocal()) {
-                                       return;
-                               }
-                               sone.setIdentity(identity);
-                               sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
-                               soneDownloader.addSone(sone);
-                               soneDownloader.fetchSone(sone);
-                       }
-               });
+               Identity identity = identityUpdatedEvent.identity();
+               final Sone sone = getRemoteSone(identity.getId(), false);
+               if (sone.isLocal()) {
+                       return;
+               }
+               sone.setIdentity(identity);
+               sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
+               soneDownloader.addSone(sone);
+               soneDownloaders.execute(soneDownloader.fetchSoneAction(sone));
        }
 
        /**
@@ -2228,9 +2141,41 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Subscribe
        public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
                logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.image(), imageInsertFinishedEvent.resultingUri()));
-               imageInsertFinishedEvent.image().setKey(imageInsertFinishedEvent.resultingUri().toString());
+               imageInsertFinishedEvent.image().modify().setKey(imageInsertFinishedEvent.resultingUri().toString()).update();
                deleteTemporaryImage(imageInsertFinishedEvent.image().getId());
                touchConfiguration();
        }
 
+       @VisibleForTesting
+       class MarkPostKnown implements Runnable {
+
+               private final Post post;
+
+               public MarkPostKnown(Post post) {
+                       this.post = post;
+               }
+
+               @Override
+               public void run() {
+                       markPostKnown(post);
+               }
+
+       }
+
+       @VisibleForTesting
+       class MarkReplyKnown implements Runnable {
+
+               private final PostReply postReply;
+
+               public MarkReplyKnown(PostReply postReply) {
+                       this.postReply = postReply;
+               }
+
+               @Override
+               public void run() {
+                       markReplyKnown(postReply);
+               }
+
+       }
+
 }