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