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