2 * Sone - OptionsPage.java - Copyright © 2010 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.Core.Preferences;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
26 import net.pterodactylus.sone.web.page.FreenetRequest;
27 import net.pterodactylus.util.number.Numbers;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
30 import net.pterodactylus.util.web.Method;
33 * This page lets the user edit the options of the Sone plugin.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class OptionsPage extends SoneTemplatePage {
40 * Creates a new options page.
43 * The template to render
45 * The Sone web interface
47 public OptionsPage(Template template, WebInterface webInterface) {
48 super("options.html", template, "Page.Options.Title", webInterface, false);
52 // TEMPLATEPAGE METHODS
59 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
60 super.processTemplate(request, templateContext);
61 Preferences preferences = webInterface.getCore().getPreferences();
62 Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
63 if (request.getMethod() == Method.POST) {
64 List<String> fieldErrors = new ArrayList<String>();
65 if (currentSone != null) {
66 boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
67 currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow);
68 boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
69 currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(enableSoneInsertNotifications);
70 webInterface.getCore().touchConfiguration();
72 Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
73 if (!preferences.validateInsertionDelay(insertionDelay)) {
74 fieldErrors.add("insertion-delay");
76 preferences.setInsertionDelay(insertionDelay);
78 Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
79 if (!preferences.validatePostsPerPage(postsPerPage)) {
80 fieldErrors.add("posts-per-page");
82 preferences.setPostsPerPage(postsPerPage);
84 Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
85 if (!preferences.validateCharactersPerPost(charactersPerPost)) {
86 fieldErrors.add("characters-per-post");
88 preferences.setCharactersPerPost(charactersPerPost);
90 Integer postCutOffLength = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null);
91 if (!preferences.validatePostCutOffLength(postCutOffLength)) {
92 fieldErrors.add("post-cut-off-length");
94 preferences.setPostCutOffLength(postCutOffLength);
96 boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
97 preferences.setRequireFullAccess(requireFullAccess);
98 Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
99 if (!preferences.validatePositiveTrust(positiveTrust)) {
100 fieldErrors.add("positive-trust");
102 preferences.setPositiveTrust(positiveTrust);
104 Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
105 if (!preferences.validateNegativeTrust(negativeTrust)) {
106 fieldErrors.add("negative-trust");
108 preferences.setNegativeTrust(negativeTrust);
110 String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
111 if (trustComment.trim().length() == 0) {
114 preferences.setTrustComment(trustComment);
115 boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
116 preferences.setFcpInterfaceActive(fcpInterfaceActive);
117 Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
118 FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
119 preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
120 boolean clearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("clear-on-next-restart", 5));
121 preferences.setClearOnNextRestart(clearOnNextRestart);
122 boolean reallyClearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("really-clear-on-next-restart", 5));
123 preferences.setReallyClearOnNextRestart(reallyClearOnNextRestart);
124 webInterface.getCore().touchConfiguration();
125 if (fieldErrors.isEmpty()) {
126 throw new RedirectException(getPath());
128 templateContext.set("fieldErrors", fieldErrors);
130 if (currentSone != null) {
131 templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
132 templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get());
134 templateContext.set("insertion-delay", preferences.getInsertionDelay());
135 templateContext.set("posts-per-page", preferences.getPostsPerPage());
136 templateContext.set("characters-per-post", preferences.getCharactersPerPost());
137 templateContext.set("post-cut-off-length", preferences.getPostCutOffLength());
138 templateContext.set("require-full-access", preferences.isRequireFullAccess());
139 templateContext.set("positive-trust", preferences.getPositiveTrust());
140 templateContext.set("negative-trust", preferences.getNegativeTrust());
141 templateContext.set("trust-comment", preferences.getTrustComment());
142 templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
143 templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
144 templateContext.set("clear-on-next-restart", preferences.isClearOnNextRestart());
145 templateContext.set("really-clear-on-next-restart", preferences.isReallyClearOnNextRestart());