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