Process Sone templates in their own method
[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 static net.pterodactylus.sone.utils.NumberParsers.parseInt;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import net.pterodactylus.sone.core.CompatibilityMode;
26 import net.pterodactylus.sone.core.Preferences;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
29 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
30 import net.pterodactylus.sone.web.page.FreenetRequest;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
33 import net.pterodactylus.util.web.Method;
34
35 /**
36  * This page lets the user edit the options of the Sone plugin.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class OptionsPage extends SoneTemplatePage {
41
42         /**
43          * Creates a new options page.
44          *
45          * @param template
46          *            The template to render
47          * @param webInterface
48          *            The Sone web interface
49          */
50         public OptionsPage(Template template, WebInterface webInterface) {
51                 super("options.html", template, "Page.Options.Title", webInterface, false);
52         }
53
54         //
55         // TEMPLATEPAGE METHODS
56         //
57
58         /**
59          * {@inheritDoc}
60          */
61         @Override
62         protected void processSonePage(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
63                 Preferences preferences = webInterface.getCore().getPreferences();
64                 Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
65                 if (request.getMethod() == Method.POST) {
66                         List<String> fieldErrors = new ArrayList<String>();
67                         if (currentSone != null) {
68                                 boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
69                                 currentSone.getOptions().setAutoFollow(autoFollow);
70                                 boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
71                                 currentSone.getOptions().setSoneInsertNotificationEnabled(enableSoneInsertNotifications);
72                                 boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones");
73                                 currentSone.getOptions().setShowNewSoneNotifications(showNotificationNewSones);
74                                 boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts");
75                                 currentSone.getOptions().setShowNewPostNotifications(showNotificationNewPosts);
76                                 boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies");
77                                 currentSone.getOptions().setShowNewReplyNotifications(showNotificationNewReplies);
78                                 String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32);
79                                 currentSone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars));
80                                 webInterface.getCore().touchConfiguration();
81                         }
82                         Integer insertionDelay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16), null);
83                         if (!preferences.validateInsertionDelay(insertionDelay)) {
84                                 fieldErrors.add("insertion-delay");
85                         } else {
86                                 preferences.setInsertionDelay(insertionDelay);
87                         }
88                         Integer postsPerPage = parseInt(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
89                         if (!preferences.validatePostsPerPage(postsPerPage)) {
90                                 fieldErrors.add("posts-per-page");
91                         } else {
92                                 preferences.setPostsPerPage(postsPerPage);
93                         }
94                         Integer imagesPerPage = parseInt(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null);
95                         if (!preferences.validateImagesPerPage(imagesPerPage)) {
96                                 fieldErrors.add("images-per-page");
97                         } else {
98                                 preferences.setImagesPerPage(imagesPerPage);
99                         }
100                         Integer charactersPerPost = parseInt(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
101                         if (!preferences.validateCharactersPerPost(charactersPerPost)) {
102                                 fieldErrors.add("characters-per-post");
103                         } else {
104                                 preferences.setCharactersPerPost(charactersPerPost);
105                         }
106                         Integer postCutOffLength = parseInt(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null);
107                         if (!preferences.validatePostCutOffLength(postCutOffLength)) {
108                                 fieldErrors.add("post-cut-off-length");
109                         } else {
110                                 preferences.setPostCutOffLength(postCutOffLength);
111                         }
112                         boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
113                         preferences.setRequireFullAccess(requireFullAccess);
114                         Integer positiveTrust = parseInt(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3), null);
115                         if (!preferences.validatePositiveTrust(positiveTrust)) {
116                                 fieldErrors.add("positive-trust");
117                         } else {
118                                 preferences.setPositiveTrust(positiveTrust);
119                         }
120                         Integer negativeTrust = parseInt(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4), null);
121                         if (!preferences.validateNegativeTrust(negativeTrust)) {
122                                 fieldErrors.add("negative-trust");
123                         } else {
124                                 preferences.setNegativeTrust(negativeTrust);
125                         }
126                         String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
127                         if (trustComment.trim().length() == 0) {
128                                 trustComment = null;
129                         }
130                         preferences.setTrustComment(trustComment);
131                         boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
132                         preferences.setFcpInterfaceActive(fcpInterfaceActive);
133                         Integer fcpFullAccessRequiredInteger = parseInt(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
134                         FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
135                         preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
136
137                         boolean compatOldElementIds = request.getHttpRequest().isPartSet("compat-old-element-ids");
138                         if (compatOldElementIds) {
139                                 webInterface.getCore().setCompatibilityMode(CompatibilityMode.oldElementIds);
140                         } else {
141                                 webInterface.getCore().clearCompatibilityMod(CompatibilityMode.oldElementIds);
142                         }
143
144                         webInterface.getCore().touchConfiguration();
145                         if (fieldErrors.isEmpty()) {
146                                 throw new RedirectException(getPath());
147                         }
148                         templateContext.set("fieldErrors", fieldErrors);
149                 }
150                 if (currentSone != null) {
151                         templateContext.set("auto-follow", currentSone.getOptions().isAutoFollow());
152                         templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().isSoneInsertNotificationEnabled());
153                         templateContext.set("show-notification-new-sones", currentSone.getOptions().isShowNewSoneNotifications());
154                         templateContext.set("show-notification-new-posts", currentSone.getOptions().isShowNewPostNotifications());
155                         templateContext.set("show-notification-new-replies", currentSone.getOptions().isShowNewReplyNotifications());
156                         templateContext.set("show-custom-avatars", currentSone.getOptions().getShowCustomAvatars().name());
157                 }
158                 templateContext.set("insertion-delay", preferences.getInsertionDelay());
159                 templateContext.set("posts-per-page", preferences.getPostsPerPage());
160                 templateContext.set("images-per-page", preferences.getImagesPerPage());
161                 templateContext.set("characters-per-post", preferences.getCharactersPerPost());
162                 templateContext.set("post-cut-off-length", preferences.getPostCutOffLength());
163                 templateContext.set("require-full-access", preferences.isRequireFullAccess());
164                 templateContext.set("positive-trust", preferences.getPositiveTrust());
165                 templateContext.set("negative-trust", preferences.getNegativeTrust());
166                 templateContext.set("trust-comment", preferences.getTrustComment());
167                 templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
168                 templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
169                 templateContext.set("compat-old-element-ids", webInterface.getCore().isCompatibilityMode(CompatibilityMode.oldElementIds));
170         }
171
172 }