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