fda6afd0a9c6e1c5df20de79991c7c4a490abd0c
[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.Page.Request.Method;
27 import net.pterodactylus.util.number.Numbers;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
30
31 /**
32  * This page lets the user edit the options of the Sone plugin.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class OptionsPage extends SoneTemplatePage {
37
38         /**
39          * Creates a new options page.
40          *
41          * @param template
42          *            The template to render
43          * @param webInterface
44          *            The Sone web interface
45          */
46         public OptionsPage(Template template, WebInterface webInterface) {
47                 super("options.html", template, "Page.Options.Title", webInterface, false);
48         }
49
50         //
51         // TEMPLATEPAGE METHODS
52         //
53
54         /**
55          * {@inheritDoc}
56          */
57         @Override
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();
70                         }
71                         Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
72                         if (!preferences.validateInsertionDelay(insertionDelay)) {
73                                 fieldErrors.add("insertion-delay");
74                         } else {
75                                 preferences.setInsertionDelay(insertionDelay);
76                         }
77                         Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
78                         if (!preferences.validatePostsPerPage(postsPerPage)) {
79                                 fieldErrors.add("posts-per-page");
80                         } else {
81                                 preferences.setPostsPerPage(postsPerPage);
82                         }
83                         Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
84                         if (!preferences.validateCharactersPerPost(charactersPerPost)) {
85                                 fieldErrors.add("characters-per-post");
86                         } else {
87                                 preferences.setCharactersPerPost(charactersPerPost);
88                         }
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");
94                         } else {
95                                 preferences.setPositiveTrust(positiveTrust);
96                         }
97                         Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
98                         if (!preferences.validateNegativeTrust(negativeTrust)) {
99                                 fieldErrors.add("negative-trust");
100                         } else {
101                                 preferences.setNegativeTrust(negativeTrust);
102                         }
103                         String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
104                         if (trustComment.trim().length() == 0) {
105                                 trustComment = null;
106                         }
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());
120                         }
121                         templateContext.set("fieldErrors", fieldErrors);
122                 }
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());
126                 }
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());
138         }
139
140 }