2 * Freetalk - FreetalkTemplatePage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
20 import java.util.Arrays;
21 import java.util.Collection;
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;
31 * Base page for the Freetalk web interface.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class SoneTemplatePage extends TemplatePage {
38 protected final WebInterface webInterface;
40 /** Whether to require a login. */
41 private final boolean requireLogin;
44 * Creates a new template page for Freetalk that does not require the user
48 * The path of the page
50 * The template to render
52 * The l10n key of the page title
54 * The Sone web interface
56 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
57 this(path, template, pageTitleKey, webInterface, false);
61 * Creates a new template page for Freetalk.
64 * The path of the page
66 * The template to render
68 * The l10n key of the page title
70 * The Sone web interface
72 * Whether this page requires a login
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);
86 * Returns the current session, creating a new session if there is no
89 * @param toadletContenxt
91 * @return The current session, or {@code null} if there is no current
94 protected Session getCurrentSession(ToadletContext toadletContenxt) {
95 return webInterface.getCurrentSession(toadletContenxt);
99 * Returns the current session, creating a new session if there is no
100 * current session and {@code create} is {@code true}.
102 * @param toadletContenxt
103 * The toadlet context
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
110 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
111 return webInterface.getCurrentSession(toadletContenxt, create);
115 * Returns the currently logged in Sone.
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
122 protected Sone getCurrentSone(ToadletContext toadletContext) {
123 return webInterface.getCurrentSone(toadletContext);
127 * Sets the currently logged in Sone.
129 * @param toadletContext
130 * The toadlet context
132 * The Sone to set as currently logged in
134 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
135 webInterface.setCurrentSone(toadletContext, sone);
139 // TEMPLATEPAGE METHODS
146 protected Collection<String> getStyleSheets() {
147 return Arrays.asList("css/sone.css");
154 protected String getShortcutIcon() {
155 return "images/icon.png";
159 * Returns whether this page requires the user to log in.
161 * @return {@code true} if the user is required to be logged in to use this
162 * page, {@code false} otherwise
164 protected boolean requiresLogin() {
172 protected void processTemplate(Request request, Template template) throws RedirectException {
173 super.processTemplate(request, template);
174 template.set("currentSone", getCurrentSone(request.getToadletContext()));
175 template.set("request", request);
182 protected String getRedirectTarget(Page.Request request) {
183 if (requiresLogin() && (getCurrentSone(request.getToadletContext()) == null)) {
193 public boolean isEnabled(ToadletContext toadletContext) {
194 if (requiresLogin()) {
195 return getCurrentSone(toadletContext) != null;