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