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