2 * Sone - OptionsPage.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web;
20 import java.util.ArrayList;
21 import java.util.List;
23 import net.pterodactylus.sone.core.Preferences;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
26 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
30 import net.pterodactylus.util.web.Method;
32 import com.google.common.base.Optional;
33 import com.google.common.primitives.Ints;
36 * This page lets the user edit the options of the Sone plugin.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class OptionsPage extends SoneTemplatePage {
43 * Creates a new options page.
46 * The template to render
48 * The Sone web interface
50 public OptionsPage(Template template, WebInterface webInterface) {
51 super("options.html", template, "Page.Options.Title", webInterface, false);
55 // TEMPLATEPAGE METHODS
62 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
63 super.processTemplate(request, templateContext);
64 Preferences preferences = webInterface.getCore().getPreferences();
65 Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
66 if (request.getMethod() == Method.POST) {
67 List<String> fieldErrors = new ArrayList<String>();
68 if (currentSone != null) {
69 boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
70 currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow);
71 boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
72 currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(enableSoneInsertNotifications);
73 boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones");
74 currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").set(showNotificationNewSones);
75 boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts");
76 currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(showNotificationNewPosts);
77 boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies");
78 currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(showNotificationNewReplies);
79 String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32);
80 currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(showCustomAvatars));
81 webInterface.getCore().touchConfiguration();
83 Integer insertionDelay = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
84 if (!preferences.validateInsertionDelay(insertionDelay)) {
85 fieldErrors.add("insertion-delay");
87 preferences.setInsertionDelay(insertionDelay);
89 Integer postsPerPage = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4));
90 if (!preferences.validatePostsPerPage(postsPerPage)) {
91 fieldErrors.add("posts-per-page");
93 preferences.setPostsPerPage(postsPerPage);
95 Integer imagesPerPage = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4));
96 if (!preferences.validateImagesPerPage(imagesPerPage)) {
97 fieldErrors.add("images-per-page");
99 preferences.setImagesPerPage(imagesPerPage);
101 Integer charactersPerPost = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10));
102 if (!preferences.validateCharactersPerPost(charactersPerPost)) {
103 fieldErrors.add("characters-per-post");
105 preferences.setCharactersPerPost(charactersPerPost);
107 Integer postCutOffLength = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10));
108 if (!preferences.validatePostCutOffLength(postCutOffLength)) {
109 fieldErrors.add("post-cut-off-length");
111 preferences.setPostCutOffLength(postCutOffLength);
113 boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
114 preferences.setRequireFullAccess(requireFullAccess);
115 Integer positiveTrust = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
116 if (!preferences.validatePositiveTrust(positiveTrust)) {
117 fieldErrors.add("positive-trust");
119 preferences.setPositiveTrust(positiveTrust);
121 Integer negativeTrust = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
122 if (!preferences.validateNegativeTrust(negativeTrust)) {
123 fieldErrors.add("negative-trust");
125 preferences.setNegativeTrust(negativeTrust);
127 String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
128 if (trustComment.trim().length() == 0) {
131 preferences.setTrustComment(trustComment);
132 boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
133 preferences.setFcpInterfaceActive(fcpInterfaceActive);
134 Integer fcpFullAccessRequiredInteger = Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1))).or(preferences.getFcpFullAccessRequired().ordinal());
135 FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
136 preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
137 webInterface.getCore().touchConfiguration();
138 if (fieldErrors.isEmpty()) {
139 throw new RedirectException(getPath());
141 templateContext.set("fieldErrors", fieldErrors);
143 if (currentSone != null) {
144 templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
145 templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get());
146 templateContext.set("show-notification-new-sones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
147 templateContext.set("show-notification-new-posts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
148 templateContext.set("show-notification-new-replies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
149 templateContext.set("show-custom-avatars", currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").get().name());
151 templateContext.set("insertion-delay", preferences.getInsertionDelay());
152 templateContext.set("posts-per-page", preferences.getPostsPerPage());
153 templateContext.set("images-per-page", preferences.getImagesPerPage());
154 templateContext.set("characters-per-post", preferences.getCharactersPerPost());
155 templateContext.set("post-cut-off-length", preferences.getPostCutOffLength());
156 templateContext.set("require-full-access", preferences.isRequireFullAccess());
157 templateContext.set("positive-trust", preferences.getPositiveTrust());
158 templateContext.set("negative-trust", preferences.getNegativeTrust());
159 templateContext.set("trust-comment", preferences.getTrustComment());
160 templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
161 templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());