Implement a bit more of the Fred interaction stuff.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * FreenetSone - WebInterface.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.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.main.SonePlugin;
32 import net.pterodactylus.sone.web.page.CSSPage;
33 import net.pterodactylus.sone.web.page.PageToadlet;
34 import net.pterodactylus.sone.web.page.PageToadletFactory;
35 import net.pterodactylus.util.logging.Logging;
36 import net.pterodactylus.util.service.AbstractService;
37 import net.pterodactylus.util.template.Template;
38 import net.pterodactylus.util.template.TemplateFactory;
39 import freenet.clients.http.LinkEnabledCallback;
40 import freenet.clients.http.SessionManager;
41 import freenet.clients.http.ToadletContainer;
42 import freenet.clients.http.ToadletContext;
43 import freenet.l10n.BaseL10n;
44
45 /**
46  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
47  * references to l10n helpers.
48  *
49  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50  */
51 public class WebInterface extends AbstractService {
52
53         /** The logger. */
54         private static final Logger logger = Logging.getLogger(WebInterface.class);
55
56         /** The Sone plugin. */
57         private final SonePlugin sonePlugin;
58
59         /** The registered toadlets. */
60         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
61
62         /**
63          * Creates a new web interface.
64          *
65          * @param sonePlugin
66          *            The Sone plugin
67          */
68         public WebInterface(SonePlugin sonePlugin) {
69                 this.sonePlugin = sonePlugin;
70         }
71
72         //
73         // ACCESSORS
74         //
75
76         /**
77          * Returns the l10n helper of the node.
78          *
79          * @return The node’s l10n helper
80          */
81         public BaseL10n l10n() {
82                 return sonePlugin.l10n().getBase();
83         }
84
85         /**
86          * Returns the session manager of the node.
87          *
88          * @return The node’s session manager
89          */
90         public SessionManager sessionManager() {
91                 try {
92                         return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
93                 } catch (URISyntaxException use1) {
94                         logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
95                         return null;
96                 }
97         }
98
99         //
100         // SERVICE METHODS
101         //
102
103         /**
104          * {@inheritDoc}
105          */
106         @Override
107         protected void serviceStart() {
108                 registerToadlets();
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         @Override
115         protected void serviceStop() {
116                 unregisterToadlets();
117         }
118
119         //
120         // PRIVATE METHODS
121         //
122
123         /**
124          * Register all toadlets.
125          */
126         private void registerToadlets() {
127                 TemplateFactory templateFactory = new SoneTemplateFactory(l10n());
128
129                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
130                 loginTemplate.set("formPassword", sonePlugin.pluginRespirator().getToadletContainer().getFormPassword());
131
132                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
133                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
134                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CSSPage("css/", "/static/css/")));
135
136                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
137                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
138                 for (PageToadlet toadlet : pageToadlets) {
139                         String menuName = toadlet.getMenuName();
140                         if (menuName != null) {
141                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, new AlwaysEnabledCallback());
142                         } else {
143                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
144                         }
145                 }
146         }
147
148         /**
149          * Unregisters all toadlets.
150          */
151         private void unregisterToadlets() {
152                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
153                 for (PageToadlet pageToadlet : pageToadlets) {
154                         toadletContainer.unregister(pageToadlet);
155                 }
156                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
157         }
158
159         /**
160          * Creates a {@link Reader} from the {@link InputStream} for the resource
161          * with the given name.
162          *
163          * @param resourceName
164          *            The name of the resource
165          * @return A {@link Reader} for the resource
166          */
167         private Reader createReader(String resourceName) {
168                 try {
169                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
170                 } catch (UnsupportedEncodingException uee1) {
171                         return null;
172                 }
173         }
174
175         /**
176          * {@link LinkEnabledCallback} implementation that always returns
177          * {@code true} when {@link LinkEnabledCallback#isEnabled(ToadletContext)}
178          * is called.
179          *
180          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
181          */
182         public class AlwaysEnabledCallback implements LinkEnabledCallback {
183
184                 /**
185                  * {@inheritDoc}
186                  */
187                 @Override
188                 public boolean isEnabled(ToadletContext toadletContext) {
189                         return true;
190                 }
191         }
192
193 }