Remove booleans from method signatures, create explicit methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / OptionsPage.java
1 /*
2  * Sone - OptionsPage.java - Copyright © 2010–2013 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.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.number.Numbers;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import net.pterodactylus.util.web.Method;
32
33 /**
34  * This page lets the user edit the options of the Sone plugin.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class OptionsPage extends SoneTemplatePage {
39
40         /**
41          * Creates a new options page.
42          *
43          * @param template
44          *            The template to render
45          * @param webInterface
46          *            The Sone web interface
47          */
48         public OptionsPage(Template template, WebInterface webInterface) {
49                 super("options.html", template, "Page.Options.Title", webInterface, false);
50         }
51
52         //
53         // TEMPLATEPAGE METHODS
54         //
55
56         @Override
57         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
58                 super.processTemplate(request, templateContext);
59                 Preferences preferences = webInterface.getCore().getPreferences();
60                 Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
61                 if (request.getMethod() == Method.POST) {
62                         List<String> fieldErrors = new ArrayList<String>();
63                         if (currentSone != null) {
64                                 boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
65                                 currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow);
66                                 boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
67                                 currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(enableSoneInsertNotifications);
68                                 boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones");
69                                 currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").set(showNotificationNewSones);
70                                 boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts");
71                                 currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(showNotificationNewPosts);
72                                 boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies");
73                                 currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(showNotificationNewReplies);
74                                 String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32);
75                                 currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(showCustomAvatars));
76                                 webInterface.getCore().touchConfiguration();
77                         }
78                         Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
79                         if (!preferences.validateInsertionDelay(insertionDelay)) {
80                                 fieldErrors.add("insertion-delay");
81                         } else {
82                                 preferences.setInsertionDelay(insertionDelay);
83                         }
84                         Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
85                         if (!preferences.validatePostsPerPage(postsPerPage)) {
86                                 fieldErrors.add("posts-per-page");
87                         } else {
88                                 preferences.setPostsPerPage(postsPerPage);
89                         }
90                         Integer imagesPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null);
91                         if (!preferences.validateImagesPerPage(imagesPerPage)) {
92                                 fieldErrors.add("images-per-page");
93                         } else {
94                                 preferences.setImagesPerPage(imagesPerPage);
95                         }
96                         Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
97                         if (!preferences.validateCharactersPerPost(charactersPerPost)) {
98                                 fieldErrors.add("characters-per-post");
99                         } else {
100                                 preferences.setCharactersPerPost(charactersPerPost);
101                         }
102                         Integer postCutOffLength = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null);
103                         if (!preferences.validatePostCutOffLength(postCutOffLength)) {
104                                 fieldErrors.add("post-cut-off-length");
105                         } else {
106                                 preferences.setPostCutOffLength(postCutOffLength);
107                         }
108                         boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
109                         preferences.setRequireFullAccess(requireFullAccess);
110                         Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
111                         if (!preferences.validatePositiveTrust(positiveTrust)) {
112                                 fieldErrors.add("positive-trust");
113                         } else {
114                                 preferences.setPositiveTrust(positiveTrust);
115                         }
116                         Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
117                         if (!preferences.validateNegativeTrust(negativeTrust)) {
118                                 fieldErrors.add("negative-trust");
119                         } else {
120                                 preferences.setNegativeTrust(negativeTrust);
121                         }
122                         String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
123                         if (trustComment.trim().length() == 0) {
124                                 trustComment = null;
125                         }
126                         preferences.setTrustComment(trustComment);
127                         boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
128                         preferences.setFcpInterfaceActive(fcpInterfaceActive);
129                         Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
130                         FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
131                         preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
132                         webInterface.getCore().touchConfiguration();
133                         if (fieldErrors.isEmpty()) {
134                                 throw new RedirectException(getPath());
135                         }
136                         templateContext.set("fieldErrors", fieldErrors);
137                 }
138                 if (currentSone != null) {
139                         templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
140                         templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get());
141                         templateContext.set("show-notification-new-sones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
142                         templateContext.set("show-notification-new-posts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
143                         templateContext.set("show-notification-new-replies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
144                         templateContext.set("show-custom-avatars", currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").get().name());
145                 }
146                 templateContext.set("insertion-delay", preferences.getInsertionDelay());
147                 templateContext.set("posts-per-page", preferences.getPostsPerPage());
148                 templateContext.set("images-per-page", preferences.getImagesPerPage());
149                 templateContext.set("characters-per-post", preferences.getCharactersPerPost());
150                 templateContext.set("post-cut-off-length", preferences.getPostCutOffLength());
151                 templateContext.set("require-full-access", preferences.isRequireFullAccess());
152                 templateContext.set("positive-trust", preferences.getPositiveTrust());
153                 templateContext.set("negative-trust", preferences.getNegativeTrust());
154                 templateContext.set("trust-comment", preferences.getTrustComment());
155                 templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
156                 templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
157         }
158
159 }