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