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