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