Return an optional Sone from the current session.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / OptionsPage.java
index c38f97b..cbc9686 100644 (file)
@@ -31,6 +31,8 @@ import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 import net.pterodactylus.util.web.Method;
 
+import com.google.common.base.Optional;
+
 /**
  * This page lets the user edit the options of the Sone plugin.
  *
@@ -61,22 +63,22 @@ public class OptionsPage extends SoneTemplatePage {
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                Preferences preferences = webInterface.getCore().getPreferences();
-               Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
+               Optional<Sone> currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
                if (request.getMethod() == Method.POST) {
                        List<String> fieldErrors = new ArrayList<String>();
-                       if (currentSone != null) {
+                       if (currentSone.isPresent()) {
                                boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
-                               currentSone.getOptions().setAutoFollow(autoFollow);
+                               currentSone.get().getOptions().setAutoFollow(autoFollow);
                                boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
-                               currentSone.getOptions().setSoneInsertNotificationEnabled(enableSoneInsertNotifications);
+                               currentSone.get().getOptions().setSoneInsertNotificationEnabled(enableSoneInsertNotifications);
                                boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones");
-                               currentSone.getOptions().setShowNewSoneNotifications(showNotificationNewSones);
+                               currentSone.get().getOptions().setShowNewSoneNotifications(showNotificationNewSones);
                                boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts");
-                               currentSone.getOptions().setShowNewPostNotifications(showNotificationNewPosts);
+                               currentSone.get().getOptions().setShowNewPostNotifications(showNotificationNewPosts);
                                boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies");
-                               currentSone.getOptions().setShowNewReplyNotifications(showNotificationNewReplies);
+                               currentSone.get().getOptions().setShowNewReplyNotifications(showNotificationNewReplies);
                                String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32);
-                               currentSone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars));
+                               currentSone.get().getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars));
                                webInterface.getCore().touchConfiguration();
                        }
                        Integer insertionDelay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16), null);
@@ -139,13 +141,13 @@ public class OptionsPage extends SoneTemplatePage {
                        }
                        templateContext.set("fieldErrors", fieldErrors);
                }
-               if (currentSone != null) {
-                       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());
+               if (currentSone.isPresent()) {
+                       templateContext.set("auto-follow", currentSone.get().getOptions().isAutoFollow());
+                       templateContext.set("enable-sone-insert-notifications", currentSone.get().getOptions().isSoneInsertNotificationEnabled());
+                       templateContext.set("show-notification-new-sones", currentSone.get().getOptions().isShowNewSoneNotifications());
+                       templateContext.set("show-notification-new-posts", currentSone.get().getOptions().isShowNewPostNotifications());
+                       templateContext.set("show-notification-new-replies", currentSone.get().getOptions().isShowNewReplyNotifications());
+                       templateContext.set("show-custom-avatars", currentSone.get().getOptions().getShowCustomAvatars().name());
                }
                templateContext.set("insertion-delay", preferences.getInsertionDelay());
                templateContext.set("posts-per-page", preferences.getPostsPerPage());