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.DataProvider;
28 import net.pterodactylus.util.template.Template;
29 import freenet.clients.http.SessionManager.Session;
30 import freenet.clients.http.ToadletContext;
33 * Base page for the Freetalk web interface.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class SoneTemplatePage extends TemplatePage {
40 protected final WebInterface webInterface;
42 /** Whether to require a login. */
43 private final boolean requireLogin;
46 * Creates a new template page for Freetalk that does not require the user
50 * The path of the page
52 * The template to render
54 * The l10n key of the page title
56 * The Sone web interface
58 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
59 this(path, template, pageTitleKey, webInterface, false);
63 * Creates a new template page for Freetalk.
66 * The path of the page
68 * The template to render
70 * The l10n key of the page title
72 * The Sone web interface
74 * Whether this page requires a login
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);
88 * Returns the current session, creating a new session if there is no
91 * @param toadletContenxt
93 * @return The current session, or {@code null} if there is no current
96 protected Session getCurrentSession(ToadletContext toadletContenxt) {
97 return webInterface.getCurrentSession(toadletContenxt);
101 * Returns the current session, creating a new session if there is no
102 * current session and {@code create} is {@code true}.
104 * @param toadletContenxt
105 * The toadlet context
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
112 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
113 return webInterface.getCurrentSession(toadletContenxt, create);
117 * Returns the currently logged in Sone.
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
124 protected Sone getCurrentSone(ToadletContext toadletContext) {
125 return webInterface.getCurrentSone(toadletContext);
129 * Returns the currently logged in Sone.
131 * @param toadletContext
132 * The toadlet context
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
139 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
140 return webInterface.getCurrentSone(toadletContext, create);
144 * Sets the currently logged in Sone.
146 * @param toadletContext
147 * The toadlet context
149 * The Sone to set as currently logged in
151 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
152 webInterface.setCurrentSone(toadletContext, sone);
156 // TEMPLATEPAGE METHODS
163 protected Collection<String> getStyleSheets() {
164 return Arrays.asList("css/sone.css");
171 protected String getShortcutIcon() {
172 return "images/icon.png";
176 * Returns whether this page requires the user to log in.
178 * @return {@code true} if the user is required to be logged in to use this
179 * page, {@code false} otherwise
181 protected boolean requiresLogin() {
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());
203 protected String getRedirectTarget(Page.Request request) {
204 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
214 public boolean isEnabled(ToadletContext toadletContext) {
215 if (requiresLogin()) {
216 return getCurrentSone(toadletContext, false) != null;