Merge branch 'release-0.3.7'
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplatePage.java
1 /*
2  * Freetalk - FreetalkTemplatePage.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.util.Arrays;
21 import java.util.Collection;
22
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.main.SonePlugin;
25 import net.pterodactylus.sone.web.page.Page;
26 import net.pterodactylus.sone.web.page.TemplatePage;
27 import net.pterodactylus.util.template.DataProvider;
28 import net.pterodactylus.util.template.Template;
29 import freenet.clients.http.SessionManager.Session;
30 import freenet.clients.http.ToadletContext;
31
32 /**
33  * Base page for the Freetalk web interface.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class SoneTemplatePage extends TemplatePage {
38
39         /** The Sone core. */
40         protected final WebInterface webInterface;
41
42         /** Whether to require a login. */
43         private final boolean requireLogin;
44
45         /**
46          * Creates a new template page for Freetalk that does not require the user
47          * to be logged in.
48          *
49          * @param path
50          *            The path of the page
51          * @param template
52          *            The template to render
53          * @param pageTitleKey
54          *            The l10n key of the page title
55          * @param webInterface
56          *            The Sone web interface
57          */
58         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
59                 this(path, template, pageTitleKey, webInterface, false);
60         }
61
62         /**
63          * Creates a new template page for Freetalk.
64          *
65          * @param path
66          *            The path of the page
67          * @param template
68          *            The template to render
69          * @param pageTitleKey
70          *            The l10n key of the page title
71          * @param webInterface
72          *            The Sone web interface
73          * @param requireLogin
74          *            Whether this page requires a login
75          */
76         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
77                 super(path, template, webInterface.getL10n(), pageTitleKey, "noPermission.html");
78                 this.webInterface = webInterface;
79                 this.requireLogin = requireLogin;
80                 template.set("webInterface", webInterface);
81         }
82
83         //
84         // PROTECTED METHODS
85         //
86
87         /**
88          * Returns the current session, creating a new session if there is no
89          * current session.
90          *
91          * @param toadletContenxt
92          *            The toadlet context
93          * @return The current session, or {@code null} if there is no current
94          *         session
95          */
96         protected Session getCurrentSession(ToadletContext toadletContenxt) {
97                 return webInterface.getCurrentSession(toadletContenxt);
98         }
99
100         /**
101          * Returns the current session, creating a new session if there is no
102          * current session and {@code create} is {@code true}.
103          *
104          * @param toadletContenxt
105          *            The toadlet context
106          * @param create
107          *            {@code true} to create a new session if there is no current
108          *            session, {@code false} otherwise
109          * @return The current session, or {@code null} if there is no current
110          *         session
111          */
112         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
113                 return webInterface.getCurrentSession(toadletContenxt, create);
114         }
115
116         /**
117          * Returns the currently logged in Sone.
118          *
119          * @param toadletContext
120          *            The toadlet context
121          * @return The currently logged in Sone, or {@code null} if no Sone is
122          *         currently logged in
123          */
124         protected Sone getCurrentSone(ToadletContext toadletContext) {
125                 return webInterface.getCurrentSone(toadletContext);
126         }
127
128         /**
129          * Returns the currently logged in Sone.
130          *
131          * @param toadletContext
132          *            The toadlet context
133          * @param create
134          *            {@code true} to create a new session if no session exists,
135          *            {@code false} to not create a new session
136          * @return The currently logged in Sone, or {@code null} if no Sone is
137          *         currently logged in
138          */
139         protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
140                 return webInterface.getCurrentSone(toadletContext, create);
141         }
142
143         /**
144          * Sets the currently logged in Sone.
145          *
146          * @param toadletContext
147          *            The toadlet context
148          * @param sone
149          *            The Sone to set as currently logged in
150          */
151         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
152                 webInterface.setCurrentSone(toadletContext, sone);
153         }
154
155         //
156         // TEMPLATEPAGE METHODS
157         //
158
159         /**
160          * {@inheritDoc}
161          */
162         @Override
163         protected Collection<String> getStyleSheets() {
164                 return Arrays.asList("css/sone.css");
165         }
166
167         /**
168          * {@inheritDoc}
169          */
170         @Override
171         protected String getShortcutIcon() {
172                 return "images/icon.png";
173         }
174
175         /**
176          * Returns whether this page requires the user to log in.
177          *
178          * @return {@code true} if the user is required to be logged in to use this
179          *         page, {@code false} otherwise
180          */
181         protected boolean requiresLogin() {
182                 return requireLogin;
183         }
184
185         /**
186          * {@inheritDoc}
187          */
188         @Override
189         protected void processTemplate(Request request, DataProvider dataProvider) throws RedirectException {
190                 super.processTemplate(request, dataProvider);
191                 dataProvider.set("currentSone", getCurrentSone(request.getToadletContext(), false));
192                 dataProvider.set("request", request);
193                 dataProvider.set("currentVersion", SonePlugin.VERSION);
194                 dataProvider.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
195                 dataProvider.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
196                 dataProvider.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
197         }
198
199         /**
200          * {@inheritDoc}
201          */
202         @Override
203         protected String getRedirectTarget(Page.Request request) {
204                 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
205                         return "login.html";
206                 }
207                 return null;
208         }
209
210         /**
211          * {@inheritDoc}
212          */
213         @Override
214         public boolean isEnabled(ToadletContext toadletContext) {
215                 if (requiresLogin()) {
216                         return getCurrentSone(toadletContext, false) != null;
217                 }
218                 return true;
219         }
220
221 }