Merge branch 'release-0.7'
[Sone.git] / src / main / java / net / pterodactylus / sone / web / OptionsPage.java
1 /*
2  * Sone - OptionsPage.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
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;
31
32 /**
33  * This page lets the user edit the options of the Sone plugin.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class OptionsPage extends SoneTemplatePage {
38
39         /**
40          * Creates a new options page.
41          *
42          * @param template
43          *            The template to render
44          * @param webInterface
45          *            The Sone web interface
46          */
47         public OptionsPage(Template template, WebInterface webInterface) {
48                 super("options.html", template, "Page.Options.Title", webInterface, false);
49         }
50
51         //
52         // TEMPLATEPAGE METHODS
53         //
54
55         /**
56          * {@inheritDoc}
57          */
58         @Override
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();
71                         }
72                         Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
73                         if (!preferences.validateInsertionDelay(insertionDelay)) {
74                                 fieldErrors.add("insertion-delay");
75                         } else {
76                                 preferences.setInsertionDelay(insertionDelay);
77                         }
78                         Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
79                         if (!preferences.validatePostsPerPage(postsPerPage)) {
80                                 fieldErrors.add("posts-per-page");
81                         } else {
82                                 preferences.setPostsPerPage(postsPerPage);
83                         }
84                         Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
85                         if (!preferences.validateCharactersPerPost(charactersPerPost)) {
86                                 fieldErrors.add("characters-per-post");
87                         } else {
88                                 preferences.setCharactersPerPost(charactersPerPost);
89                         }
90                         boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
91                         preferences.setRequireFullAccess(requireFullAccess);
92                         Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
93                         if (!preferences.validatePositiveTrust(positiveTrust)) {
94                                 fieldErrors.add("positive-trust");
95                         } else {
96                                 preferences.setPositiveTrust(positiveTrust);
97                         }
98                         Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
99                         if (!preferences.validateNegativeTrust(negativeTrust)) {
100                                 fieldErrors.add("negative-trust");
101                         } else {
102                                 preferences.setNegativeTrust(negativeTrust);
103                         }
104                         String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
105                         if (trustComment.trim().length() == 0) {
106                                 trustComment = null;
107                         }
108                         preferences.setTrustComment(trustComment);
109                         boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
110                         preferences.setFcpInterfaceActive(fcpInterfaceActive);
111                         Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
112                         FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
113                         preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
114                         boolean clearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("clear-on-next-restart", 5));
115                         preferences.setClearOnNextRestart(clearOnNextRestart);
116                         boolean reallyClearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("really-clear-on-next-restart", 5));
117                         preferences.setReallyClearOnNextRestart(reallyClearOnNextRestart);
118                         webInterface.getCore().touchConfiguration();
119                         if (fieldErrors.isEmpty()) {
120                                 throw new RedirectException(getPath());
121                         }
122                         templateContext.set("fieldErrors", fieldErrors);
123                 }
124                 if (currentSone != null) {
125                         templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
126                         templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get());
127                 }
128                 templateContext.set("insertion-delay", preferences.getInsertionDelay());
129                 templateContext.set("posts-per-page", preferences.getPostsPerPage());
130                 templateContext.set("characters-per-post", preferences.getCharactersPerPost());
131                 templateContext.set("require-full-access", preferences.isRequireFullAccess());
132                 templateContext.set("positive-trust", preferences.getPositiveTrust());
133                 templateContext.set("negative-trust", preferences.getNegativeTrust());
134                 templateContext.set("trust-comment", preferences.getTrustComment());
135                 templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
136                 templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
137                 templateContext.set("clear-on-next-restart", preferences.isClearOnNextRestart());
138                 templateContext.set("really-clear-on-next-restart", preferences.isReallyClearOnNextRestart());
139         }
140
141 }