Merge branch 'master' into next
[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.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.UUID;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32
33 import net.pterodactylus.sone.core.Core;
34 import net.pterodactylus.sone.core.CoreListener;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.Reply;
37 import net.pterodactylus.sone.data.Sone;
38 import net.pterodactylus.sone.freenet.L10nFilter;
39 import net.pterodactylus.sone.freenet.wot.Identity;
40 import net.pterodactylus.sone.main.SonePlugin;
41 import net.pterodactylus.sone.notify.ListNotification;
42 import net.pterodactylus.sone.template.CollectionAccessor;
43 import net.pterodactylus.sone.template.CssClassNameFilter;
44 import net.pterodactylus.sone.template.GetPagePlugin;
45 import net.pterodactylus.sone.template.IdentityAccessor;
46 import net.pterodactylus.sone.template.NotificationManagerAccessor;
47 import net.pterodactylus.sone.template.PostAccessor;
48 import net.pterodactylus.sone.template.ReplyAccessor;
49 import net.pterodactylus.sone.template.RequestChangeFilter;
50 import net.pterodactylus.sone.template.SoneAccessor;
51 import net.pterodactylus.sone.template.SubstringFilter;
52 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
53 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
54 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
55 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
56 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
57 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
58 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
59 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
60 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
61 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
62 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
63 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
64 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
65 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
66 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
67 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
68 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
69 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
70 import net.pterodactylus.sone.web.page.PageToadlet;
71 import net.pterodactylus.sone.web.page.PageToadletFactory;
72 import net.pterodactylus.sone.web.page.StaticPage;
73 import net.pterodactylus.util.logging.Logging;
74 import net.pterodactylus.util.notify.NotificationManager;
75 import net.pterodactylus.util.notify.TemplateNotification;
76 import net.pterodactylus.util.template.DateFilter;
77 import net.pterodactylus.util.template.DefaultTemplateFactory;
78 import net.pterodactylus.util.template.MatchFilter;
79 import net.pterodactylus.util.template.PaginationPlugin;
80 import net.pterodactylus.util.template.ReflectionAccessor;
81 import net.pterodactylus.util.template.Template;
82 import net.pterodactylus.util.template.TemplateException;
83 import net.pterodactylus.util.template.TemplateFactory;
84 import net.pterodactylus.util.template.TemplateProvider;
85 import net.pterodactylus.util.template.XmlFilter;
86 import net.pterodactylus.util.thread.Ticker;
87 import freenet.clients.http.SessionManager;
88 import freenet.clients.http.SessionManager.Session;
89 import freenet.clients.http.ToadletContainer;
90 import freenet.clients.http.ToadletContext;
91 import freenet.l10n.BaseL10n;
92
93 /**
94  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
95  * references to l10n helpers.
96  *
97  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
98  */
99 public class WebInterface implements CoreListener {
100
101         /** The logger. */
102         private static final Logger logger = Logging.getLogger(WebInterface.class);
103
104         /** The notification manager. */
105         private final NotificationManager notificationManager = new NotificationManager();
106
107         /** The Sone plugin. */
108         private final SonePlugin sonePlugin;
109
110         /** The registered toadlets. */
111         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
112
113         /** The form password. */
114         private final String formPassword;
115
116         /** The template factory. */
117         private DefaultTemplateFactory templateFactory;
118
119         /** The “new Sone” notification. */
120         private final ListNotification<Sone> newSoneNotification;
121
122         /** The “new post” notification. */
123         private final ListNotification<Post> newPostNotification;
124
125         /** The “new reply” notification. */
126         private final ListNotification<Reply> newReplyNotification;
127
128         /** The “rescuing Sone” notification. */
129         private final ListNotification<Sone> rescuingSonesNotification;
130
131         /** The “Sone rescued” notification. */
132         private final ListNotification<Sone> sonesRescuedNotification;
133
134         /**
135          * Creates a new web interface.
136          *
137          * @param sonePlugin
138          *            The Sone plugin
139          */
140         public WebInterface(SonePlugin sonePlugin) {
141                 this.sonePlugin = sonePlugin;
142                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
143
144                 templateFactory = new DefaultTemplateFactory();
145                 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
146                 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
147                 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
148                 templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
149                 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
150                 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
151                 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
152                 templateFactory.addFilter("date", new DateFilter());
153                 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
154                 templateFactory.addFilter("substring", new SubstringFilter());
155                 templateFactory.addFilter("xml", new XmlFilter());
156                 templateFactory.addFilter("change", new RequestChangeFilter());
157                 templateFactory.addFilter("match", new MatchFilter());
158                 templateFactory.addFilter("css", new CssClassNameFilter());
159                 templateFactory.addPlugin("getpage", new GetPagePlugin());
160                 templateFactory.addPlugin("paginate", new PaginationPlugin());
161                 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
162                 templateFactory.addTemplateObject("formPassword", formPassword);
163
164                 /* create notifications. */
165                 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
166                 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
167
168                 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
169                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
170
171                 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
172                 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
173
174                 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
175                 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
176
177                 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
178                 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
179         }
180
181         //
182         // ACCESSORS
183         //
184
185         /**
186          * Returns the Sone core used by the Sone plugin.
187          *
188          * @return The Sone core
189          */
190         public Core getCore() {
191                 return sonePlugin.core();
192         }
193
194         /**
195          * Returns the current session, creating a new session if there is no
196          * current session.
197          *
198          * @param toadletContenxt
199          *            The toadlet context
200          * @return The current session, or {@code null} if there is no current
201          *         session
202          */
203         public Session getCurrentSession(ToadletContext toadletContenxt) {
204                 return getCurrentSession(toadletContenxt, true);
205         }
206
207         /**
208          * Returns the current session, creating a new session if there is no
209          * current session and {@code create} is {@code true}.
210          *
211          * @param toadletContenxt
212          *            The toadlet context
213          * @param create
214          *            {@code true} to create a new session if there is no current
215          *            session, {@code false} otherwise
216          * @return The current session, or {@code null} if there is no current
217          *         session
218          */
219         public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
220                 Session session = getSessionManager().useSession(toadletContenxt);
221                 if (create && (session == null)) {
222                         session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
223                 }
224                 return session;
225         }
226
227         /**
228          * Returns the currently logged in Sone.
229          *
230          * @param toadletContext
231          *            The toadlet context
232          * @return The currently logged in Sone, or {@code null} if no Sone is
233          *         currently logged in
234          */
235         public Sone getCurrentSone(ToadletContext toadletContext) {
236                 return getCurrentSone(getCurrentSession(toadletContext));
237         }
238
239         /**
240          * Returns the currently logged in Sone.
241          *
242          * @param session
243          *            The session
244          * @return The currently logged in Sone, or {@code null} if no Sone is
245          *         currently logged in
246          */
247         public Sone getCurrentSone(Session session) {
248                 if (session == null) {
249                         return null;
250                 }
251                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
252                 if (soneId == null) {
253                         return null;
254                 }
255                 return getCore().getLocalSone(soneId, false);
256         }
257
258         /**
259          * Sets the currently logged in Sone.
260          *
261          * @param toadletContext
262          *            The toadlet context
263          * @param sone
264          *            The Sone to set as currently logged in
265          */
266         public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
267                 Session session = getCurrentSession(toadletContext);
268                 if (sone == null) {
269                         session.removeAttribute("Sone.CurrentSone");
270                 } else {
271                         session.setAttribute("Sone.CurrentSone", sone.getId());
272                 }
273         }
274
275         /**
276          * Returns the notification manager.
277          *
278          * @return The notification manager
279          */
280         public NotificationManager getNotifications() {
281                 return notificationManager;
282         }
283
284         /**
285          * Returns the l10n helper of the node.
286          *
287          * @return The node’s l10n helper
288          */
289         public BaseL10n getL10n() {
290                 return sonePlugin.l10n().getBase();
291         }
292
293         /**
294          * Returns the session manager of the node.
295          *
296          * @return The node’s session manager
297          */
298         public SessionManager getSessionManager() {
299                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
300         }
301
302         /**
303          * Returns the node’s form password.
304          *
305          * @return The form password
306          */
307         public String getFormPassword() {
308                 return formPassword;
309         }
310
311         /**
312          * Returns the posts that have been announced as new in the
313          * {@link #newPostNotification}.
314          *
315          * @return The new posts
316          */
317         public Set<Post> getNewPosts() {
318                 return new HashSet<Post>(newPostNotification.getElements());
319         }
320
321         /**
322          * Returns the replies that have been announced as new in the
323          * {@link #newReplyNotification}.
324          *
325          * @return The new replies
326          */
327         public Set<Reply> getNewReplies() {
328                 return new HashSet<Reply>(newReplyNotification.getElements());
329         }
330
331         //
332         // ACTIONS
333         //
334
335         /**
336          * Starts the web interface and registers all toadlets.
337          */
338         public void start() {
339                 registerToadlets();
340
341                 /* notification templates. */
342                 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
343
344                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
345                 notificationManager.addNotification(startupNotification);
346
347                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
348
349                         @Override
350                         public void run() {
351                                 startupNotification.dismiss();
352                         }
353                 }, "Sone Startup Notification Remover");
354
355                 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
356                 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
357                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
358
359                         @Override
360                         @SuppressWarnings("synthetic-access")
361                         public void run() {
362                                 if (getCore().getIdentityManager().isConnected()) {
363                                         wotMissingNotification.dismiss();
364                                 } else {
365                                         notificationManager.addNotification(wotMissingNotification);
366                                 }
367                                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
368                         }
369
370                 }, "Sone WoT Connector Checker");
371         }
372
373         /**
374          * Stops the web interface and unregisters all toadlets.
375          */
376         public void stop() {
377                 unregisterToadlets();
378                 Ticker.getInstance().stop();
379         }
380
381         //
382         // PRIVATE METHODS
383         //
384
385         /**
386          * Register all toadlets.
387          */
388         private void registerToadlets() {
389                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
390                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
391                 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
392                 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
393                 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
394                 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
395                 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
396                 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
397                 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
398                 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
399                 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
400                 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
401                 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
402                 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
403                 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
404                 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
405                 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
406                 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
407                 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
408                 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
409                 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
410                 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
411                 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
412                 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
413                 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
414
415                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
416                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
417                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
418                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
419                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
420                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
421                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
422                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
423                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
424                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
425                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
426                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
427                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
428                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
429                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
430                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
431                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
432                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
433                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
434                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
435                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
436                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
437                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
438                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
439                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
440                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
441                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
442                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
443                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
444                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
445                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
446                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
447                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
448                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
449                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
450                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
451                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
452                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
453                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
454                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
455                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
456                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
457                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
458                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
459                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
460
461                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
462                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
463                 for (PageToadlet toadlet : pageToadlets) {
464                         String menuName = toadlet.getMenuName();
465                         if (menuName != null) {
466                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
467                         } else {
468                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
469                         }
470                 }
471         }
472
473         /**
474          * Unregisters all toadlets.
475          */
476         private void unregisterToadlets() {
477                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
478                 for (PageToadlet pageToadlet : pageToadlets) {
479                         toadletContainer.unregister(pageToadlet);
480                 }
481                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
482         }
483
484         /**
485          * Creates a {@link Reader} from the {@link InputStream} for the resource
486          * with the given name.
487          *
488          * @param resourceName
489          *            The name of the resource
490          * @return A {@link Reader} for the resource
491          */
492         private Reader createReader(String resourceName) {
493                 try {
494                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
495                 } catch (UnsupportedEncodingException uee1) {
496                         return null;
497                 }
498         }
499
500         //
501         // CORELISTENER METHODS
502         //
503
504         /**
505          * {@inheritDoc}
506          */
507         @Override
508         public void rescuingSone(Sone sone) {
509                 rescuingSonesNotification.add(sone);
510                 notificationManager.addNotification(rescuingSonesNotification);
511         }
512
513         /**
514          * {@inheritDoc}
515          */
516         @Override
517         public void rescuedSone(Sone sone) {
518                 rescuingSonesNotification.remove(sone);
519                 sonesRescuedNotification.add(sone);
520                 notificationManager.addNotification(sonesRescuedNotification);
521         }
522
523         /**
524          * {@inheritDoc}
525          */
526         @Override
527         public void newSoneFound(Sone sone) {
528                 newSoneNotification.add(sone);
529                 notificationManager.addNotification(newSoneNotification);
530         }
531
532         /**
533          * {@inheritDoc}
534          */
535         @Override
536         public void newPostFound(Post post) {
537                 newPostNotification.add(post);
538                 notificationManager.addNotification(newPostNotification);
539         }
540
541         /**
542          * {@inheritDoc}
543          */
544         @Override
545         public void newReplyFound(Reply reply) {
546                 if (reply.getPost().getSone() == null) {
547                         return;
548                 }
549                 newReplyNotification.add(reply);
550                 notificationManager.addNotification(newReplyNotification);
551         }
552
553         /**
554          * {@inheritDoc}
555          */
556         @Override
557         public void markSoneKnown(Sone sone) {
558                 newSoneNotification.remove(sone);
559         }
560
561         /**
562          * {@inheritDoc}
563          */
564         @Override
565         public void markPostKnown(Post post) {
566                 newPostNotification.remove(post);
567         }
568
569         /**
570          * {@inheritDoc}
571          */
572         @Override
573         public void markReplyKnown(Reply reply) {
574                 newReplyNotification.remove(reply);
575         }
576
577         /**
578          * {@inheritDoc}
579          */
580         @Override
581         public void postRemoved(Post post) {
582                 /* TODO */
583         }
584
585         /**
586          * {@inheritDoc}
587          */
588         @Override
589         public void replyRemoved(Reply reply) {
590                 /* TODO */
591         }
592
593         /**
594          * Template provider implementation that uses
595          * {@link WebInterface#createReader(String)} to load templates for
596          * inclusion.
597          *
598          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
599          */
600         private class ClassPathTemplateProvider implements TemplateProvider {
601
602                 /** The template factory. */
603                 @SuppressWarnings("hiding")
604                 private final TemplateFactory templateFactory;
605
606                 /**
607                  * Creates a new template provider that locates templates on the
608                  * classpath.
609                  *
610                  * @param templateFactory
611                  *            The template factory to create the templates
612                  */
613                 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
614                         this.templateFactory = templateFactory;
615                 }
616
617                 /**
618                  * {@inheritDoc}
619                  */
620                 @Override
621                 @SuppressWarnings("synthetic-access")
622                 public Template getTemplate(String templateName) {
623                         Reader templateReader = createReader("/templates/" + templateName);
624                         if (templateReader == null) {
625                                 return null;
626                         }
627                         Template template = templateFactory.createTemplate(templateReader);
628                         try {
629                                 template.parse();
630                         } catch (TemplateException te1) {
631                                 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
632                         }
633                         return template;
634                 }
635
636         }
637
638 }