Merge branch 'release-0.9.6'
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 95b4bfe..91825a6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - Core.java - Copyright © 2010–2013 David Roden
+ * Sone - Core.java - Copyright © 2010–2016 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -116,7 +116,7 @@ import com.google.inject.Singleton;
 public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
 
        /** The logger. */
-       private static final Logger logger = getLogger("Sone.Core");
+       private static final Logger logger = getLogger(Core.class.getName());
 
        /** The start time. */
        private final long startupTime = System.currentTimeMillis();
@@ -204,14 +204,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The database
         */
        @Inject
-       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
                super("Sone Core");
                this.configuration = configuration;
                this.freenetInterface = freenetInterface;
                this.identityManager = identityManager;
                this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
                this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
-               this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
+               this.updateChecker = updateChecker;
                this.webOfTrustUpdater = webOfTrustUpdater;
                this.eventBus = eventBus;
                this.database = database;
@@ -643,16 +643,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
                String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
                sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
-               sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
+               sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
                sone.setKnown(true);
-               loadSone(sone);
-               sone.setStatus(SoneStatus.idle);
-               database.storeSone(sone);
                SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
                eventBus.register(soneInserter);
                synchronized (soneInserters) {
                        soneInserters.put(sone, soneInserter);
                }
+               loadSone(sone);
+               database.storeSone(sone);
+               sone.setStatus(SoneStatus.idle);
                soneInserter.start();
                return sone;
        }
@@ -688,8 +688,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.WARNING, "Given Identity is null!");
                        return null;
                }
-               final Long latestEdition = tryParse(fromNullable(
-                               identity.getProperty("Sone.LatestEdition")).or("0"));
+               String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+               long latestEdition = fromNullable(tryParse(property)).or(0L);
                Optional<Sone> existingSone = getSone(identity.getId());
                if (existingSone.isPresent() && existingSone.get().isLocal()) {
                        return existingSone.get();
@@ -1065,11 +1065,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
 
                /* load options. */
-               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().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
+               sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
+               sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
+               sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
+               sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
                sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
 
                /* if we’re still here, Sone was loaded successfully. */
@@ -1518,7 +1518,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
                                configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
                                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);
 
@@ -1730,8 +1729,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        /* TODO - we don’t have the Sone anymore. should this happen? */
                        return;
                }
-               database.removeSone(sone.get());
+               for (PostReply postReply : sone.get().getReplies()) {
+                       eventBus.post(new PostReplyRemovedEvent(postReply));
+               }
+               for (Post post : sone.get().getPosts()) {
+                       eventBus.post(new PostRemovedEvent(post));
+               }
                eventBus.post(new SoneRemovedEvent(sone.get()));
+               database.removeSone(sone.get());
        }
 
        /**