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.main.SonePlugin;
25 import net.pterodactylus.sone.web.page.Page;
26 import net.pterodactylus.sone.web.page.TemplatePage;
27 import net.pterodactylus.util.template.Template;
28 import freenet.clients.http.SessionManager.Session;
29 import freenet.clients.http.ToadletContext;
32 * Base page for the Freetalk web interface.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class SoneTemplatePage extends TemplatePage {
39 protected final WebInterface webInterface;
41 /** Whether to require a login. */
42 private final boolean requireLogin;
45 * Creates a new template page for Freetalk that does not require the user
49 * The path of the page
51 * The template to render
53 * The l10n key of the page title
55 * The Sone web interface
57 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
58 this(path, template, pageTitleKey, webInterface, false);
62 * Creates a new template page for Freetalk.
65 * The path of the page
67 * The template to render
69 * The l10n key of the page title
71 * The Sone web interface
73 * Whether this page requires a login
75 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
76 super(path, template, webInterface.getL10n(), pageTitleKey, "noPermission.html");
77 this.webInterface = webInterface;
78 this.requireLogin = requireLogin;
79 template.set("webInterface", webInterface);
87 * Returns the current session, creating a new session if there is no
90 * @param toadletContenxt
92 * @return The current session, or {@code null} if there is no current
95 protected Session getCurrentSession(ToadletContext toadletContenxt) {
96 return webInterface.getCurrentSession(toadletContenxt);
100 * Returns the current session, creating a new session if there is no
101 * current session and {@code create} is {@code true}.
103 * @param toadletContenxt
104 * The toadlet context
106 * {@code true} to create a new session if there is no current
107 * session, {@code false} otherwise
108 * @return The current session, or {@code null} if there is no current
111 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
112 return webInterface.getCurrentSession(toadletContenxt, create);
116 * Returns the currently logged in Sone.
118 * @param toadletContext
119 * The toadlet context
120 * @return The currently logged in Sone, or {@code null} if no Sone is
121 * currently logged in
123 protected Sone getCurrentSone(ToadletContext toadletContext) {
124 return webInterface.getCurrentSone(toadletContext);
128 * Returns the currently logged in Sone.
130 * @param toadletContext
131 * The toadlet context
133 * {@code true} to create a new session if no session exists,
134 * {@code false} to not create a new session
135 * @return The currently logged in Sone, or {@code null} if no Sone is
136 * currently logged in
138 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
139 return webInterface.getCurrentSone(toadletContext, create);
143 * Sets the currently logged in Sone.
145 * @param toadletContext
146 * The toadlet context
148 * The Sone to set as currently logged in
150 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
151 webInterface.setCurrentSone(toadletContext, sone);
155 // TEMPLATEPAGE METHODS
162 protected Collection<String> getStyleSheets() {
163 return Arrays.asList("css/sone.css");
170 protected String getShortcutIcon() {
171 return "images/icon.png";
175 * Returns whether this page requires the user to log in.
177 * @return {@code true} if the user is required to be logged in to use this
178 * page, {@code false} otherwise
180 protected boolean requiresLogin() {
188 protected void processTemplate(Request request, Template template) throws RedirectException {
189 super.processTemplate(request, template);
190 template.set("currentSone", getCurrentSone(request.getToadletContext(), false));
191 template.set("request", request);
192 template.set("currentVersion", SonePlugin.VERSION);
193 template.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
194 template.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
195 template.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
202 protected String getRedirectTarget(Page.Request request) {
203 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
213 public boolean isEnabled(ToadletContext toadletContext) {
214 if (requiresLogin()) {
215 return getCurrentSone(toadletContext, false) != null;