Convert “image insert aborted” into EventBus-based event.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / OptionsPage.java
1 /*
2  * Sone - OptionsPage.java - Copyright © 2010–2012 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.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         /**
57          * {@inheritDoc}
58          */
59         @Override
60         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
61                 super.processTemplate(request, templateContext);
62                 Preferences preferences = webInterface.getCore().getPreferences();
63                 Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
64                 if (request.getMethod() == Method.POST) {
65                         List<String> fieldErrors = new ArrayList<String>();
66                         if (currentSone != null) {
67                                 boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
68                                 currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow);
69                                 boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications");
70                                 currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(enableSoneInsertNotifications);
71                                 boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones");
72                                 currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").set(showNotificationNewSones);
73                                 boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts");
74                                 currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(showNotificationNewPosts);
75                                 boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies");
76                                 currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(showNotificationNewReplies);
77                                 String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32);
78                                 currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(showCustomAvatars));
79                                 webInterface.getCore().touchConfiguration();
80                         }
81                         Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
82                         if (!preferences.validateInsertionDelay(insertionDelay)) {
83                                 fieldErrors.add("insertion-delay");
84                         } else {
85                                 preferences.setInsertionDelay(insertionDelay);
86                         }
87                         Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
88                         if (!preferences.validatePostsPerPage(postsPerPage)) {
89                                 fieldErrors.add("posts-per-page");
90                         } else {
91                                 preferences.setPostsPerPage(postsPerPage);
92                         }
93                         Integer imagesPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null);
94                         if (!preferences.validateImagesPerPage(imagesPerPage)) {
95                                 fieldErrors.add("images-per-page");
96                         } else {
97                                 preferences.setImagesPerPage(imagesPerPage);
98                         }
99                         Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
100                         if (!preferences.validateCharactersPerPost(charactersPerPost)) {
101                                 fieldErrors.add("characters-per-post");
102                         } else {
103                                 preferences.setCharactersPerPost(charactersPerPost);
104                         }
105                         Integer postCutOffLength = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null);
106                         if (!preferences.validatePostCutOffLength(postCutOffLength)) {
107                                 fieldErrors.add("post-cut-off-length");
108                         } else {
109                                 preferences.setPostCutOffLength(postCutOffLength);
110                         }
111                         boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
112                         preferences.setRequireFullAccess(requireFullAccess);
113                         Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
114                         if (!preferences.validatePositiveTrust(positiveTrust)) {
115                                 fieldErrors.add("positive-trust");
116                         } else {
117                                 preferences.setPositiveTrust(positiveTrust);
118                         }
119                         Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
120                         if (!preferences.validateNegativeTrust(negativeTrust)) {
121                                 fieldErrors.add("negative-trust");
122                         } else {
123                                 preferences.setNegativeTrust(negativeTrust);
124                         }
125                         String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256);
126                         if (trustComment.trim().length() == 0) {
127                                 trustComment = null;
128                         }
129                         preferences.setTrustComment(trustComment);
130                         boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
131                         preferences.setFcpInterfaceActive(fcpInterfaceActive);
132                         Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
133                         FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
134                         preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
135                         webInterface.getCore().touchConfiguration();
136                         if (fieldErrors.isEmpty()) {
137                                 throw new RedirectException(getPath());
138                         }
139                         templateContext.set("fieldErrors", fieldErrors);
140                 }
141                 if (currentSone != null) {
142                         templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
143                         templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get());
144                         templateContext.set("show-notification-new-sones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
145                         templateContext.set("show-notification-new-posts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
146                         templateContext.set("show-notification-new-replies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
147                         templateContext.set("show-custom-avatars", currentSone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").get().name());
148                 }
149                 templateContext.set("insertion-delay", preferences.getInsertionDelay());
150                 templateContext.set("posts-per-page", preferences.getPostsPerPage());
151                 templateContext.set("images-per-page", preferences.getImagesPerPage());
152                 templateContext.set("characters-per-post", preferences.getCharactersPerPost());
153                 templateContext.set("post-cut-off-length", preferences.getPostCutOffLength());
154                 templateContext.set("require-full-access", preferences.isRequireFullAccess());
155                 templateContext.set("positive-trust", preferences.getPositiveTrust());
156                 templateContext.set("negative-trust", preferences.getNegativeTrust());
157                 templateContext.set("trust-comment", preferences.getTrustComment());
158                 templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
159                 templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
160         }
161
162 }