Add Sone-specific options object.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 25 Aug 2014 18:55:31 +0000 (20:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 25 Aug 2014 18:55:31 +0000 (20:55 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/SoneImpl.java
src/main/java/net/pterodactylus/sone/data/SoneOptions.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java
src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java
src/main/java/net/pterodactylus/sone/web/OptionsPage.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java

index 1448ac8..87f858b 100644 (file)
@@ -752,12 +752,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();
@@ -793,7 +787,7 @@ 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());
                                                }
                                        }
@@ -1104,14 +1098,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);
@@ -1279,12 +1265,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) {
@@ -1866,12 +1852,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();
 
index 166591f..2263192 100644 (file)
@@ -535,7 +535,7 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
         *
         * @return The options of this Sone
         */
-       Options getOptions();
+       SoneOptions getOptions();
 
        /**
         * Sets the options of this Sone.
@@ -544,6 +544,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
         *              The options of this Sone
         */
        /* TODO - remove this method again, maybe add an option provider */
-       void setOptions(Options options);
+       void setOptions(SoneOptions options);
 
 }
index f57bac9..848fc04 100644 (file)
@@ -28,7 +28,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import net.pterodactylus.sone.core.Options;
+import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.util.logging.Logging;
 
@@ -102,7 +102,7 @@ public class SoneImpl implements Sone {
        private final Album rootAlbum = new AlbumImpl().setSone(this);
 
        /** Sone-specific options. */
-       private Options options = new Options();
+       private SoneOptions options = new DefaultSoneOptions();
 
        /**
         * Creates a new Sone.
@@ -643,7 +643,7 @@ public class SoneImpl implements Sone {
         *
         * @return The options of this Sone
         */
-       public Options getOptions() {
+       public SoneOptions getOptions() {
                return options;
        }
 
@@ -654,7 +654,7 @@ public class SoneImpl implements Sone {
         *              The options of this Sone
         */
        /* TODO - remove this method again, maybe add an option provider */
-       public void setOptions(Options options) {
+       public void setOptions(SoneOptions options) {
                this.options = options;
        }
 
diff --git a/src/main/java/net/pterodactylus/sone/data/SoneOptions.java b/src/main/java/net/pterodactylus/sone/data/SoneOptions.java
new file mode 100644 (file)
index 0000000..819e94a
--- /dev/null
@@ -0,0 +1,108 @@
+package net.pterodactylus.sone.data;
+
+import static net.pterodactylus.sone.data.Sone.ShowCustomAvatars.NEVER;
+
+import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
+
+/**
+ * All Sone-specific options.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface SoneOptions {
+
+       boolean isAutoFollow();
+       void setAutoFollow(boolean autoFollow);
+
+       boolean isSoneInsertNotificationEnabled();
+       void setSoneInsertNotificationEnabled(boolean soneInsertNotificationEnabled);
+
+       boolean isShowNewSoneNotifications();
+       void setShowNewSoneNotifications(boolean showNewSoneNotifications);
+
+       boolean isShowNewPostNotifications();
+       void setShowNewPostNotifications(boolean showNewPostNotifications);
+
+       boolean isShowNewReplyNotifications();
+       void setShowNewReplyNotifications(boolean showNewReplyNotifications);
+
+       ShowCustomAvatars getShowCustomAvatars();
+       void setShowCustomAvatars(ShowCustomAvatars showCustomAvatars);
+
+       /**
+        * {@link SoneOptions} implementation.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public class DefaultSoneOptions implements SoneOptions {
+
+               private boolean autoFollow = false;
+               private boolean soneInsertNotificationsEnabled = false;
+               private boolean showNewSoneNotifications = true;
+               private boolean showNewPostNotifications = true;
+               private boolean showNewReplyNotifications = true;
+               private ShowCustomAvatars showCustomAvatars = NEVER;
+
+               @Override
+               public boolean isAutoFollow() {
+                       return autoFollow;
+               }
+
+               @Override
+               public void setAutoFollow(boolean autoFollow) {
+                       this.autoFollow = autoFollow;
+               }
+
+               @Override
+               public boolean isSoneInsertNotificationEnabled() {
+                       return soneInsertNotificationsEnabled;
+               }
+
+               @Override
+               public void setSoneInsertNotificationEnabled(boolean soneInsertNotificationEnabled) {
+                       this.soneInsertNotificationsEnabled = soneInsertNotificationEnabled;
+               }
+
+               @Override
+               public boolean isShowNewSoneNotifications() {
+                       return showNewSoneNotifications;
+               }
+
+               @Override
+               public void setShowNewSoneNotifications(boolean showNewSoneNotifications) {
+                       this.showNewSoneNotifications = showNewSoneNotifications;
+               }
+
+               @Override
+               public boolean isShowNewPostNotifications() {
+                       return showNewPostNotifications;
+               }
+
+               @Override
+               public void setShowNewPostNotifications(boolean showNewPostNotifications) {
+                       this.showNewPostNotifications = showNewPostNotifications;
+               }
+
+               @Override
+               public boolean isShowNewReplyNotifications() {
+                       return showNewReplyNotifications;
+               }
+
+               @Override
+               public void setShowNewReplyNotifications(boolean showNewReplyNotifications) {
+                       this.showNewReplyNotifications = showNewReplyNotifications;
+               }
+
+               @Override
+               public ShowCustomAvatars getShowCustomAvatars() {
+                       return showCustomAvatars;
+               }
+
+               @Override
+               public void setShowCustomAvatars(ShowCustomAvatars showCustomAvatars) {
+                       this.showCustomAvatars = showCustomAvatars;
+               }
+
+       }
+
+}
index f431bb7..5289081 100644 (file)
@@ -59,12 +59,12 @@ public class ListNotificationFilters {
                List<Notification> filteredNotifications = new ArrayList<Notification>();
                for (Notification notification : notifications) {
                        if (notification.getId().equals("new-sone-notification")) {
-                               if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get())) {
+                               if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) {
                                        continue;
                                }
                                filteredNotifications.add(notification);
                        } else if (notification.getId().equals("new-post-notification")) {
-                               if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get())) {
+                               if ((currentSone != null) && !currentSone.getOptions().isShowNewPostNotifications()) {
                                        continue;
                                }
                                ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone, true);
@@ -72,7 +72,7 @@ public class ListNotificationFilters {
                                        filteredNotifications.add(filteredNotification);
                                }
                        } else if (notification.getId().equals("new-reply-notification")) {
-                               if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get())) {
+                               if ((currentSone != null) && !currentSone.getOptions().isShowNewReplyNotifications()) {
                                        continue;
                                }
                                ListNotification<PostReply> filteredNotification = filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
index 30c0a52..762bc14 100644 (file)
@@ -74,7 +74,7 @@ public class ProfileAccessor extends ReflectionAccessor {
                                /* always show your own avatars. */
                                return avatarId;
                        }
-                       ShowCustomAvatars showCustomAvatars = currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").get();
+                       ShowCustomAvatars showCustomAvatars = currentSone.getOptions().getShowCustomAvatars();
                        if (showCustomAvatars == ShowCustomAvatars.NEVER) {
                                return null;
                        }
index 094a9cf..5001a73 100644 (file)
@@ -65,17 +65,17 @@ public class OptionsPage extends SoneTemplatePage {
                        List<String> fieldErrors = new ArrayList<String>();
                        if (currentSone != null) {
                                boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
-                               currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow);
+                               currentSone.getOptions().setAutoFollow(autoFollow);
                                boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
-                               currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(enableSoneInsertNotifications);
+                               currentSone.getOptions().setSoneInsertNotificationEnabled(enableSoneInsertNotifications);
                                boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones");
-                               currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").set(showNotificationNewSones);
+                               currentSone.getOptions().setShowNewSoneNotifications(showNotificationNewSones);
                                boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts");
-                               currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(showNotificationNewPosts);
+                               currentSone.getOptions().setShowNewPostNotifications(showNotificationNewPosts);
                                boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies");
-                               currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(showNotificationNewReplies);
+                               currentSone.getOptions().setShowNewReplyNotifications(showNotificationNewReplies);
                                String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32);
-                               currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(showCustomAvatars));
+                               currentSone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars));
                                webInterface.getCore().touchConfiguration();
                        }
                        Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
@@ -139,12 +139,12 @@ public class OptionsPage extends SoneTemplatePage {
                        templateContext.set("fieldErrors", fieldErrors);
                }
                if (currentSone != null) {
-                       templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
-                       templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get());
-                       templateContext.set("show-notification-new-sones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
-                       templateContext.set("show-notification-new-posts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
-                       templateContext.set("show-notification-new-replies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
-                       templateContext.set("show-custom-avatars", currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").get().name());
+                       templateContext.set("auto-follow", currentSone.getOptions().isAutoFollow());
+                       templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().isSoneInsertNotificationEnabled());
+                       templateContext.set("show-notification-new-sones", currentSone.getOptions().isShowNewSoneNotifications());
+                       templateContext.set("show-notification-new-posts", currentSone.getOptions().isShowNewPostNotifications());
+                       templateContext.set("show-notification-new-replies", currentSone.getOptions().isShowNewReplyNotifications());
+                       templateContext.set("show-custom-avatars", currentSone.getOptions().getShowCustomAvatars().name());
                }
                templateContext.set("insertion-delay", preferences.getInsertionDelay());
                templateContext.set("posts-per-page", preferences.getPostsPerPage());
index 8478976..afa324b 100644 (file)
@@ -1011,7 +1011,7 @@ public class WebInterface {
        public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
                TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
                soneInsertNotification.set("soneStatus", "inserting");
-               if (soneInsertingEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
+               if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
                        notificationManager.addNotification(soneInsertNotification);
                }
        }
@@ -1027,7 +1027,7 @@ public class WebInterface {
                TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
                soneInsertNotification.set("soneStatus", "inserted");
                soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
-               if (soneInsertedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
+               if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
                        notificationManager.addNotification(soneInsertNotification);
                }
        }
@@ -1043,7 +1043,7 @@ public class WebInterface {
                TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
                soneInsertNotification.set("soneStatus", "insert-aborted");
                soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
-               if (soneInsertAbortedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
+               if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
                        notificationManager.addNotification(soneInsertNotification);
                }
        }
index e8107ef..a25e665 100644 (file)
@@ -147,9 +147,9 @@ public class GetNotificationsAjaxPage extends JsonPage {
        private static JsonNode createJsonOptions(Sone currentSone) {
                ObjectNode options = new ObjectNode(instance);
                if (currentSone != null) {
-                       options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
-                       options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
-                       options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
+                       options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications());
+                       options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications());
+                       options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications());
                }
                return options;
        }
index e43910c..cc095a2 100644 (file)
@@ -192,9 +192,9 @@ public class GetStatusAjaxPage extends JsonPage {
        private static JsonNode createJsonOptions(Sone currentSone) {
                ObjectNode options = new ObjectNode(instance);
                if (currentSone != null) {
-                       options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
-                       options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
-                       options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
+                       options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications());
+                       options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications());
+                       options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications());
                }
                return options;
        }