Merge branch 'fix-start-with-broken-config' into next
[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          * Sets the currently logged in Sone.
128          *
129          * @param toadletContext
130          *            The toadlet context
131          * @param sone
132          *            The Sone to set as currently logged in
133          */
134         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
135                 webInterface.setCurrentSone(toadletContext, sone);
136         }
137
138         //
139         // TEMPLATEPAGE METHODS
140         //
141
142         /**
143          * {@inheritDoc}
144          */
145         @Override
146         protected Collection<String> getStyleSheets() {
147                 return Arrays.asList("css/sone.css");
148         }
149
150         /**
151          * {@inheritDoc}
152          */
153         @Override
154         protected String getShortcutIcon() {
155                 return "images/icon.png";
156         }
157
158         /**
159          * Returns whether this page requires the user to log in.
160          *
161          * @return {@code true} if the user is required to be logged in to use this
162          *         page, {@code false} otherwise
163          */
164         protected boolean requiresLogin() {
165                 return requireLogin;
166         }
167
168         /**
169          * {@inheritDoc}
170          */
171         @Override
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);
176         }
177
178         /**
179          * {@inheritDoc}
180          */
181         @Override
182         protected String getRedirectTarget(Page.Request request) {
183                 if (requiresLogin() && (getCurrentSone(request.getToadletContext()) == null)) {
184                         return "login.html";
185                 }
186                 return null;
187         }
188
189         /**
190          * {@inheritDoc}
191          */
192         @Override
193         public boolean isEnabled(ToadletContext toadletContext) {
194                 if (requiresLogin()) {
195                         return getCurrentSone(toadletContext) != null;
196                 }
197                 return true;
198         }
199
200 }