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