Store filtered notifications in all templates.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplatePage.java
1 /*
2  * Sone - SoneTemplatePage.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.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Map;
26
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.main.SonePlugin;
29 import net.pterodactylus.sone.notify.ListNotificationFilters;
30 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
31 import net.pterodactylus.sone.web.page.Page;
32 import net.pterodactylus.util.collection.ListBuilder;
33 import net.pterodactylus.util.collection.MapBuilder;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateContext;
36 import freenet.clients.http.SessionManager.Session;
37 import freenet.clients.http.ToadletContext;
38 import freenet.support.api.HTTPRequest;
39
40 /**
41  * Base page for the Sone web interface.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class SoneTemplatePage extends FreenetTemplatePage {
46
47         /** The Sone core. */
48         protected final WebInterface webInterface;
49
50         /** The page title l10n key. */
51         private final String pageTitleKey;
52
53         /** Whether to require a login. */
54         private final boolean requireLogin;
55
56         /**
57          * Creates a new template page for Sone that does not require the user to be
58          * logged in.
59          *
60          * @param path
61          *            The path of the page
62          * @param template
63          *            The template to render
64          * @param webInterface
65          *            The Sone web interface
66          */
67         public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
68                 this(path, template, null, webInterface, false);
69         }
70
71         /**
72          * Creates a new template page for Sone that does not require the user to be
73          * logged in.
74          *
75          * @param path
76          *            The path of the page
77          * @param template
78          *            The template to render
79          * @param pageTitleKey
80          *            The l10n key of the page title
81          * @param webInterface
82          *            The Sone web interface
83          */
84         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
85                 this(path, template, pageTitleKey, webInterface, false);
86         }
87
88         /**
89          * Creates a new template page for Sone.
90          *
91          * @param path
92          *            The path of the page
93          * @param template
94          *            The template to render
95          * @param webInterface
96          *            The Sone web interface
97          * @param requireLogin
98          *            Whether this page requires a login
99          */
100         public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
101                 this(path, template, null, webInterface, requireLogin);
102         }
103
104         /**
105          * Creates a new template page for Sone.
106          *
107          * @param path
108          *            The path of the page
109          * @param template
110          *            The template to render
111          * @param pageTitleKey
112          *            The l10n key of the page title
113          * @param webInterface
114          *            The Sone web interface
115          * @param requireLogin
116          *            Whether this page requires a login
117          */
118         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
119                 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
120                 this.pageTitleKey = pageTitleKey;
121                 this.webInterface = webInterface;
122                 this.requireLogin = requireLogin;
123                 template.getInitialContext().set("webInterface", webInterface);
124         }
125
126         //
127         // PROTECTED METHODS
128         //
129
130         /**
131          * Returns the current session, creating a new session if there is no
132          * current session.
133          *
134          * @param toadletContenxt
135          *            The toadlet context
136          * @return The current session, or {@code null} if there is no current
137          *         session
138          */
139         protected Session getCurrentSession(ToadletContext toadletContenxt) {
140                 return webInterface.getCurrentSession(toadletContenxt);
141         }
142
143         /**
144          * Returns the current session, creating a new session if there is no
145          * current session and {@code create} is {@code true}.
146          *
147          * @param toadletContenxt
148          *            The toadlet context
149          * @param create
150          *            {@code true} to create a new session if there is no current
151          *            session, {@code false} otherwise
152          * @return The current session, or {@code null} if there is no current
153          *         session
154          */
155         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
156                 return webInterface.getCurrentSession(toadletContenxt, create);
157         }
158
159         /**
160          * Returns the currently logged in Sone.
161          *
162          * @param toadletContext
163          *            The toadlet context
164          * @return The currently logged in Sone, or {@code null} if no Sone is
165          *         currently logged in
166          */
167         protected Sone getCurrentSone(ToadletContext toadletContext) {
168                 return webInterface.getCurrentSone(toadletContext);
169         }
170
171         /**
172          * Returns the currently logged in Sone.
173          *
174          * @param toadletContext
175          *            The toadlet context
176          * @param create
177          *            {@code true} to create a new session if no session exists,
178          *            {@code false} to not create a new session
179          * @return The currently logged in Sone, or {@code null} if no Sone is
180          *         currently logged in
181          */
182         protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
183                 return webInterface.getCurrentSone(toadletContext, create);
184         }
185
186         /**
187          * Sets the currently logged in Sone.
188          *
189          * @param toadletContext
190          *            The toadlet context
191          * @param sone
192          *            The Sone to set as currently logged in
193          */
194         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
195                 webInterface.setCurrentSone(toadletContext, sone);
196         }
197
198         //
199         // TEMPLATEPAGE METHODS
200         //
201
202         /**
203          * {@inheritDoc}
204          */
205         @Override
206         protected String getPageTitle(Request request) {
207                 if (pageTitleKey != null) {
208                         return webInterface.getL10n().getString(pageTitleKey);
209                 }
210                 return "";
211         }
212
213         /**
214          * {@inheritDoc}
215          */
216         @Override
217         protected List<Map<String, String>> getAdditionalLinkNodes(Request request) {
218                 return new ListBuilder<Map<String, String>>().add(new MapBuilder<String, String>().put("rel", "search").put("type", "application/opensearchdescription+xml").put("title", "Sone").put("href", "http://" + request.getHttpRequest().getHeader("host") + "/Sone/OpenSearch.xml").get()).get();
219         }
220
221         /**
222          * {@inheritDoc}
223          */
224         @Override
225         protected Collection<String> getStyleSheets() {
226                 return Arrays.asList("css/sone.css");
227         }
228
229         /**
230          * {@inheritDoc}
231          */
232         @Override
233         protected String getShortcutIcon() {
234                 return "images/icon.png";
235         }
236
237         /**
238          * Returns whether this page requires the user to log in.
239          *
240          * @return {@code true} if the user is required to be logged in to use this
241          *         page, {@code false} otherwise
242          */
243         protected boolean requiresLogin() {
244                 return requireLogin;
245         }
246
247         /**
248          * {@inheritDoc}
249          */
250         @Override
251         protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
252                 super.processTemplate(request, templateContext);
253                 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
254                 templateContext.set("currentSone", currentSone);
255                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
256                 templateContext.set("request", request);
257                 templateContext.set("currentVersion", SonePlugin.VERSION);
258                 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
259                 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
260                 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
261                 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
262                 templateContext.set("notifications", ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone));
263         }
264
265         /**
266          * {@inheritDoc}
267          */
268         @Override
269         protected String getRedirectTarget(Page.Request request) {
270                 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
271                         HTTPRequest httpRequest = request.getHttpRequest();
272                         String originalUrl = httpRequest.getPath();
273                         if (httpRequest.hasParameters()) {
274                                 StringBuilder requestParameters = new StringBuilder();
275                                 for (String parameterName : httpRequest.getParameterNames()) {
276                                         if (requestParameters.length() > 0) {
277                                                 requestParameters.append("%26");
278                                         }
279                                         String[] parameterValues = httpRequest.getMultipleParam(parameterName);
280                                         for (String parameterValue : parameterValues) {
281                                                 try {
282                                                         requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
283                                                 } catch (UnsupportedEncodingException uee1) {
284                                                         /* A JVM without UTF-8? I don’t think so. */
285                                                 }
286                                         }
287                                 }
288                                 originalUrl += "?" + requestParameters.toString();
289                         }
290                         return "login.html?target=" + originalUrl;
291                 }
292                 return null;
293         }
294
295         /**
296          * {@inheritDoc}
297          */
298         @Override
299         public boolean isEnabled(ToadletContext toadletContext) {
300                 if (requiresLogin()) {
301                         return getCurrentSone(toadletContext, false) != null;
302                 }
303                 return true;
304         }
305
306 }