First extremely basic, kind-of-working version.
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / ui / web / WebInterface.java
1 /*
2  * WoTNS - WebInterface.java - Copyright © 2011 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.wotns.ui.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.util.ArrayList;
25 import java.util.List;
26
27 import net.pterodactylus.util.template.HtmlFilter;
28 import net.pterodactylus.util.template.ReflectionAccessor;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContextFactory;
31 import net.pterodactylus.util.template.TemplateParser;
32 import net.pterodactylus.wotns.freenet.wot.Identity;
33 import net.pterodactylus.wotns.main.WoTNSPlugin;
34 import net.pterodactylus.wotns.template.IdentityAccessor;
35 import net.pterodactylus.wotns.web.PageToadlet;
36 import net.pterodactylus.wotns.web.PageToadletFactory;
37 import freenet.clients.http.ToadletContainer;
38
39 /**
40  * TODO
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class WebInterface {
45
46         private final WoTNSPlugin wotNSPlugin;
47
48         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
49
50         /** The registered toadlets. */
51         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
52
53         public WebInterface(WoTNSPlugin woTNSPlugin) {
54                 this.wotNSPlugin = woTNSPlugin;
55
56                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
57                 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor());
58                 templateContextFactory.addFilter("html", new HtmlFilter());
59         }
60
61         //
62         // ACCESSORS
63         //
64
65         public WoTNSPlugin getWoTNSPlugin() {
66                 return wotNSPlugin;
67         }
68
69         public TemplateContextFactory getTemplateContextFactory() {
70                 return templateContextFactory;
71         }
72
73         //
74         // ACTIONS
75         //
76
77         public void start() {
78                 registerToadlets();
79         }
80
81         public void stop() {
82                 unregisterToadlets();
83         }
84
85         //
86         // PRIVATE METHODS
87         //
88
89         private void registerToadlets() {
90                 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
91                 Template unknownTemplate = TemplateParser.parse(createReader("/templates/unknown.html"));
92                 Template manageTemplate = TemplateParser.parse(createReader("/templates/manage.html"));
93                 Template addTargetTemplate = TemplateParser.parse(createReader("/templates/addTarget.html"));
94
95                 PageToadletFactory pageToadletFactory = new PageToadletFactory(wotNSPlugin.getHighLevelSimpleClient(), "/tns/");
96                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ResolverPage(unknownTemplate, this, wotNSPlugin.getResolver())));
97                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
98                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ManagePage(manageTemplate, this)));
99                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddTargetPage(addTargetTemplate, this)));
100
101                 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
102                 toadletContainer.getPageMaker().addNavigationCategory("/tns/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", wotNSPlugin);
103                 for (PageToadlet toadlet : pageToadlets) {
104                         String menuName = toadlet.getMenuName();
105                         if (menuName != null) {
106                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
107                         } else {
108                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
109                         }
110                 }
111         }
112
113         /**
114          * Unregisters all toadlets.
115          */
116         private void unregisterToadlets() {
117                 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
118                 for (PageToadlet pageToadlet : pageToadlets) {
119                         toadletContainer.unregister(pageToadlet);
120                 }
121                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
122         }
123
124         /**
125          * Creates a {@link Reader} from the {@link InputStream} for the resource
126          * with the given name.
127          *
128          * @param resourceName
129          *            The name of the resource
130          * @return A {@link Reader} for the resource
131          */
132         private Reader createReader(String resourceName) {
133                 try {
134                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
135                 } catch (UnsupportedEncodingException uee1) {
136                         return null;
137                 }
138         }
139
140 }