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