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.Page.Request.Method;
27 import net.pterodactylus.util.number.Numbers;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
32 * This page lets the user edit the options of the Sone plugin.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class OptionsPage extends SoneTemplatePage {
39 * Creates a new options page.
42 * The template to render
44 * The Sone web interface
46 public OptionsPage(Template template, WebInterface webInterface) {
47 super("options.html", template, "Page.Options.Title", webInterface, false);
51 // TEMPLATEPAGE METHODS
58 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
59 super.processTemplate(request, templateContext);
60 Preferences preferences = webInterface.getCore().getPreferences();
61 Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
62 if (request.getMethod() == Method.POST) {
63 List<String> fieldErrors = new ArrayList<String>();
64 if (currentSone != null) {
65 boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
66 currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow);
67 boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
68 currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(enableSoneInsertNotifications);
69 webInterface.getCore().touchConfiguration();
71 Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
72 if (!preferences.validateInsertionDelay(insertionDelay)) {
73 fieldErrors.add("insertion-delay");
75 preferences.setInsertionDelay(insertionDelay);
77 Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
78 if (!preferences.validatePostsPerPage(postsPerPage)) {
79 fieldErrors.add("posts-per-page");
81 preferences.setPostsPerPage(postsPerPage);
83 Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
84 if (!preferences.validateCharactersPerPost(charactersPerPost)) {
85 fieldErrors.add("characters-per-post");
87 preferences.setCharactersPerPost(charactersPerPost);
89 boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
90 preferences.setRequireFullAccess(requireFullAccess);
91 Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
92 if (!preferences.validatePositiveTrust(positiveTrust)) {
93 fieldErrors.add("positive-trust");
95 preferences.setPositiveTrust(positiveTrust);
97 Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
98 if (!preferences.validateNegativeTrust(negativeTrust)) {
99 fieldErrors.add("negative-trust");
101 preferences.setNegativeTrust(negativeTrust);
103 String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
104 if (trustComment.trim().length() == 0) {
107 preferences.setTrustComment(trustComment);
108 boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
109 preferences.setFcpInterfaceActive(fcpInterfaceActive);
110 Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
111 FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
112 preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
113 boolean clearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("clear-on-next-restart", 5));
114 preferences.setClearOnNextRestart(clearOnNextRestart);
115 boolean reallyClearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("really-clear-on-next-restart", 5));
116 preferences.setReallyClearOnNextRestart(reallyClearOnNextRestart);
117 webInterface.getCore().touchConfiguration();
118 if (fieldErrors.isEmpty()) {
119 throw new RedirectException(getPath());
121 templateContext.set("fieldErrors", fieldErrors);
123 if (currentSone != null) {
124 templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
125 templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get());
127 templateContext.set("insertion-delay", preferences.getInsertionDelay());
128 templateContext.set("posts-per-page", preferences.getPostsPerPage());
129 templateContext.set("characters-per-post", preferences.getCharactersPerPost());
130 templateContext.set("require-full-access", preferences.isRequireFullAccess());
131 templateContext.set("positive-trust", preferences.getPositiveTrust());
132 templateContext.set("negative-trust", preferences.getNegativeTrust());
133 templateContext.set("trust-comment", preferences.getTrustComment());
134 templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
135 templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
136 templateContext.set("clear-on-next-restart", preferences.isClearOnNextRestart());
137 templateContext.set("really-clear-on-next-restart", preferences.isReallyClearOnNextRestart());