Deliver static files from filesystem
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * Sone - WebInterface.java - Copyright © 2010–2013 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 static java.util.logging.Logger.getLogger;
21 import static net.pterodactylus.util.template.TemplateParser.parse;
22
23 import java.io.IOException;
24 import java.io.StringReader;
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.concurrent.Executors;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.ScheduledFuture;
37 import java.util.concurrent.TimeUnit;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40
41 import net.pterodactylus.sone.core.Core;
42 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
43 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
44 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
45 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
46 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
47 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
48 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
49 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
50 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
51 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
52 import net.pterodactylus.sone.core.event.PostRemovedEvent;
53 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
54 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
55 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
56 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
57 import net.pterodactylus.sone.core.event.SoneLockedEvent;
58 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
59 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
60 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
61 import net.pterodactylus.sone.data.Album;
62 import net.pterodactylus.sone.data.Image;
63 import net.pterodactylus.sone.data.Post;
64 import net.pterodactylus.sone.data.PostReply;
65 import net.pterodactylus.sone.data.Profile;
66 import net.pterodactylus.sone.data.Reply;
67 import net.pterodactylus.sone.data.Sone;
68 import net.pterodactylus.sone.freenet.L10nFilter;
69 import net.pterodactylus.sone.freenet.wot.Identity;
70 import net.pterodactylus.sone.freenet.wot.Trust;
71 import net.pterodactylus.sone.main.Loaders;
72 import net.pterodactylus.sone.main.ReparseFilter;
73 import net.pterodactylus.sone.main.SonePlugin;
74 import net.pterodactylus.sone.notify.ListNotification;
75 import net.pterodactylus.sone.template.AlbumAccessor;
76 import net.pterodactylus.sone.template.CollectionAccessor;
77 import net.pterodactylus.sone.template.CssClassNameFilter;
78 import net.pterodactylus.sone.template.HttpRequestAccessor;
79 import net.pterodactylus.sone.template.IdentityAccessor;
80 import net.pterodactylus.sone.template.ImageAccessor;
81 import net.pterodactylus.sone.template.ImageLinkFilter;
82 import net.pterodactylus.sone.template.JavascriptFilter;
83 import net.pterodactylus.sone.template.ParserFilter;
84 import net.pterodactylus.sone.template.PostAccessor;
85 import net.pterodactylus.sone.template.ProfileAccessor;
86 import net.pterodactylus.sone.template.ReplyAccessor;
87 import net.pterodactylus.sone.template.ReplyGroupFilter;
88 import net.pterodactylus.sone.template.RequestChangeFilter;
89 import net.pterodactylus.sone.template.SoneAccessor;
90 import net.pterodactylus.sone.template.SubstringFilter;
91 import net.pterodactylus.sone.template.TrustAccessor;
92 import net.pterodactylus.sone.template.UniqueElementFilter;
93 import net.pterodactylus.sone.template.UnknownDateFilter;
94 import net.pterodactylus.sone.text.Part;
95 import net.pterodactylus.sone.text.SonePart;
96 import net.pterodactylus.sone.text.SoneTextParser;
97 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
98 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
99 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
100 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
101 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
102 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
103 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
104 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
105 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
106 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
107 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
108 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
109 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
110 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
111 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
112 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
113 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
114 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
115 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
116 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
117 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
118 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
119 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
120 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
121 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
122 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
123 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
124 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
125 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
126 import net.pterodactylus.sone.web.page.FreenetRequest;
127 import net.pterodactylus.sone.web.page.PageToadlet;
128 import net.pterodactylus.sone.web.page.PageToadletFactory;
129 import net.pterodactylus.util.notify.Notification;
130 import net.pterodactylus.util.notify.NotificationManager;
131 import net.pterodactylus.util.notify.TemplateNotification;
132 import net.pterodactylus.util.template.ClassPathTemplateProvider;
133 import net.pterodactylus.util.template.CollectionSortFilter;
134 import net.pterodactylus.util.template.ContainsFilter;
135 import net.pterodactylus.util.template.DateFilter;
136 import net.pterodactylus.util.template.FormatFilter;
137 import net.pterodactylus.util.template.HtmlFilter;
138 import net.pterodactylus.util.template.MatchFilter;
139 import net.pterodactylus.util.template.ModFilter;
140 import net.pterodactylus.util.template.PaginationFilter;
141 import net.pterodactylus.util.template.ReflectionAccessor;
142 import net.pterodactylus.util.template.ReplaceFilter;
143 import net.pterodactylus.util.template.StoreFilter;
144 import net.pterodactylus.util.template.Template;
145 import net.pterodactylus.util.template.TemplateContextFactory;
146 import net.pterodactylus.util.template.TemplateProvider;
147 import net.pterodactylus.util.template.XmlFilter;
148 import net.pterodactylus.util.web.RedirectPage;
149 import net.pterodactylus.util.web.TemplatePage;
150
151 import com.google.common.collect.Collections2;
152 import com.google.common.collect.ImmutableSet;
153 import com.google.common.eventbus.Subscribe;
154 import com.google.inject.Inject;
155
156 import freenet.clients.http.SessionManager;
157 import freenet.clients.http.SessionManager.Session;
158 import freenet.clients.http.ToadletContainer;
159 import freenet.clients.http.ToadletContext;
160 import freenet.l10n.BaseL10n;
161 import freenet.support.api.HTTPRequest;
162
163 /**
164  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
165  * references to l10n helpers.
166  *
167  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
168  */
169 public class WebInterface {
170
171         /** The logger. */
172         private static final Logger logger = getLogger(WebInterface.class.getName());
173
174         /** The loaders for templates, pages, and classpath providers. */
175         private final Loaders loaders;
176
177         /** The notification manager. */
178         private final NotificationManager notificationManager = new NotificationManager();
179
180         /** The Sone plugin. */
181         private final SonePlugin sonePlugin;
182
183         /** The registered toadlets. */
184         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
185
186         /** The form password. */
187         private final String formPassword;
188
189         /** The template context factory. */
190         private final TemplateContextFactory templateContextFactory;
191
192         /** The Sone text parser. */
193         private final SoneTextParser soneTextParser;
194
195         /** The parser filter. */
196         private final ParserFilter parserFilter;
197
198         /** The “new Sone” notification. */
199         private final ListNotification<Sone> newSoneNotification;
200
201         /** The “new post” notification. */
202         private final ListNotification<Post> newPostNotification;
203
204         /** The “new reply” notification. */
205         private final ListNotification<PostReply> newReplyNotification;
206
207         /** The invisible “local post” notification. */
208         private final ListNotification<Post> localPostNotification;
209
210         /** The invisible “local reply” notification. */
211         private final ListNotification<PostReply> localReplyNotification;
212
213         /** The “you have been mentioned” notification. */
214         private final ListNotification<Post> mentionNotification;
215
216         /** Notifications for sone inserts. */
217         private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
218
219         /** Sone locked notification ticker objects. */
220         private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
221
222         /** The “Sone locked” notification. */
223         private final ListNotification<Sone> lockedSonesNotification;
224
225         /** The “new version” notification. */
226         private final TemplateNotification newVersionNotification;
227
228         /** The “inserting images” notification. */
229         private final ListNotification<Image> insertingImagesNotification;
230
231         /** The “inserted images” notification. */
232         private final ListNotification<Image> insertedImagesNotification;
233
234         /** The “image insert failed” notification. */
235         private final ListNotification<Image> imageInsertFailedNotification;
236
237         /** Scheduled executor for time-based notifications. */
238         private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
239
240         /**
241          * Creates a new web interface.
242          *
243          * @param sonePlugin
244          *            The Sone plugin
245          */
246         @Inject
247         public WebInterface(SonePlugin sonePlugin, Loaders loaders) {
248                 this.sonePlugin = sonePlugin;
249                 this.loaders = loaders;
250                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
251                 soneTextParser = new SoneTextParser(getCore(), getCore());
252
253                 templateContextFactory = new TemplateContextFactory();
254                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
255                 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
256                 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
257                 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
258                 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
259                 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
260                 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
261                 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
262                 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
263                 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
264                 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
265                 templateContextFactory.addFilter("date", new DateFilter());
266                 templateContextFactory.addFilter("html", new HtmlFilter());
267                 templateContextFactory.addFilter("replace", new ReplaceFilter());
268                 templateContextFactory.addFilter("store", new StoreFilter());
269                 templateContextFactory.addFilter("l10n", new L10nFilter(this));
270                 templateContextFactory.addFilter("substring", new SubstringFilter());
271                 templateContextFactory.addFilter("xml", new XmlFilter());
272                 templateContextFactory.addFilter("change", new RequestChangeFilter());
273                 templateContextFactory.addFilter("match", new MatchFilter());
274                 templateContextFactory.addFilter("css", new CssClassNameFilter());
275                 templateContextFactory.addFilter("js", new JavascriptFilter());
276                 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
277                 templateContextFactory.addFilter("reparse", new ReparseFilter());
278                 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
279                 templateContextFactory.addFilter("format", new FormatFilter());
280                 templateContextFactory.addFilter("sort", new CollectionSortFilter());
281                 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
282                 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
283                 templateContextFactory.addFilter("in", new ContainsFilter());
284                 templateContextFactory.addFilter("unique", new UniqueElementFilter());
285                 templateContextFactory.addFilter("mod", new ModFilter());
286                 templateContextFactory.addFilter("paginate", new PaginationFilter());
287                 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
288                 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
289                 templateContextFactory.addTemplateObject("webInterface", this);
290                 templateContextFactory.addTemplateObject("formPassword", formPassword);
291
292                 /* create notifications. */
293                 Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
294                 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
295
296                 Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
297                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
298
299                 Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
300                 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
301
302                 Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
303                 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
304
305                 Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
306                 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
307
308                 Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
309                 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
310
311                 Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
312                 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
313
314                 Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
315                 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
316
317                 Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
318                 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
319
320                 Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
321                 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
322
323                 Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
324                 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
325         }
326
327         //
328         // ACCESSORS
329         //
330
331         /**
332          * Returns the Sone core used by the Sone plugin.
333          *
334          * @return The Sone core
335          */
336         public Core getCore() {
337                 return sonePlugin.core();
338         }
339
340         /**
341          * Returns the template context factory of the web interface.
342          *
343          * @return The template context factory
344          */
345         public TemplateContextFactory getTemplateContextFactory() {
346                 return templateContextFactory;
347         }
348
349         /**
350          * Returns the current session, creating a new session if there is no
351          * current session.
352          *
353          * @param toadletContenxt
354          *            The toadlet context
355          * @return The current session, or {@code null} if there is no current
356          *         session
357          */
358         public Session getCurrentSession(ToadletContext toadletContenxt) {
359                 return getCurrentSession(toadletContenxt, true);
360         }
361
362         /**
363          * Returns the current session, creating a new session if there is no
364          * current session and {@code create} is {@code true}.
365          *
366          * @param toadletContenxt
367          *            The toadlet context
368          * @param create
369          *            {@code true} to create a new session if there is no current
370          *            session, {@code false} otherwise
371          * @return The current session, or {@code null} if there is no current
372          *         session
373          */
374         public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
375                 Session session = getSessionManager().useSession(toadletContenxt);
376                 if (create && (session == null)) {
377                         session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
378                 }
379                 return session;
380         }
381
382         /**
383          * Returns the currently logged in Sone.
384          *
385          * @param toadletContext
386          *            The toadlet context
387          * @return The currently logged in Sone, or {@code null} if no Sone is
388          *         currently logged in
389          */
390         public Sone getCurrentSone(ToadletContext toadletContext) {
391                 return getCurrentSone(toadletContext, true);
392         }
393
394         /**
395          * Returns the currently logged in Sone.
396          *
397          * @param toadletContext
398          *            The toadlet context
399          * @param create
400          *            {@code true} to create a new session if no session exists,
401          *            {@code false} to not create a new session
402          * @return The currently logged in Sone, or {@code null} if no Sone is
403          *         currently logged in
404          */
405         public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
406                 Collection<Sone> localSones = getCore().getLocalSones();
407                 if (localSones.size() == 1) {
408                         return localSones.iterator().next();
409                 }
410                 return getCurrentSone(getCurrentSession(toadletContext, create));
411         }
412
413         /**
414          * Returns the currently logged in Sone.
415          *
416          * @param session
417          *            The session
418          * @return The currently logged in Sone, or {@code null} if no Sone is
419          *         currently logged in
420          */
421         public Sone getCurrentSone(Session session) {
422                 if (session == null) {
423                         return null;
424                 }
425                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
426                 if (soneId == null) {
427                         return null;
428                 }
429                 return getCore().getLocalSone(soneId);
430         }
431
432         /**
433          * Sets the currently logged in Sone.
434          *
435          * @param toadletContext
436          *            The toadlet context
437          * @param sone
438          *            The Sone to set as currently logged in
439          */
440         public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
441                 Session session = getCurrentSession(toadletContext);
442                 if (sone == null) {
443                         session.removeAttribute("Sone.CurrentSone");
444                 } else {
445                         session.setAttribute("Sone.CurrentSone", sone.getId());
446                 }
447         }
448
449         /**
450          * Returns the notification manager.
451          *
452          * @return The notification manager
453          */
454         public NotificationManager getNotifications() {
455                 return notificationManager;
456         }
457
458         /**
459          * Returns the l10n helper of the node.
460          *
461          * @return The node’s l10n helper
462          */
463         public BaseL10n getL10n() {
464                 return sonePlugin.l10n().getBase();
465         }
466
467         /**
468          * Returns the session manager of the node.
469          *
470          * @return The node’s session manager
471          */
472         public SessionManager getSessionManager() {
473                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
474         }
475
476         /**
477          * Returns the node’s form password.
478          *
479          * @return The form password
480          */
481         public String getFormPassword() {
482                 return formPassword;
483         }
484
485         /**
486          * Returns the posts that have been announced as new in the
487          * {@link #newPostNotification}.
488          *
489          * @return The new posts
490          */
491         public Set<Post> getNewPosts() {
492                 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
493         }
494
495         /**
496          * Returns the replies that have been announced as new in the
497          * {@link #newReplyNotification}.
498          *
499          * @return The new replies
500          */
501         public Set<PostReply> getNewReplies() {
502                 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
503         }
504
505         /**
506          * Sets whether the current start of the plugin is the first start. It is
507          * considered a first start if the configuration file does not exist.
508          *
509          * @param firstStart
510          *            {@code true} if no configuration file existed when Sone was
511          *            loaded, {@code false} otherwise
512          */
513         public void setFirstStart(boolean firstStart) {
514                 if (firstStart) {
515                         Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html");
516                         Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
517                         notificationManager.addNotification(firstStartNotification);
518                 }
519         }
520
521         /**
522          * Sets whether Sone was started with a fresh configuration file.
523          *
524          * @param newConfig
525          *            {@code true} if Sone was started with a fresh configuration,
526          *            {@code false} if the existing configuration could be read
527          */
528         public void setNewConfig(boolean newConfig) {
529                 if (newConfig && !hasFirstStartNotification()) {
530                         Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html");
531                         Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
532                         notificationManager.addNotification(configNotReadNotification);
533                 }
534         }
535
536         //
537         // PRIVATE ACCESSORS
538         //
539
540         /**
541          * Returns whether the first start notification is currently displayed.
542          *
543          * @return {@code true} if the first-start notification is currently
544          *         displayed, {@code false} otherwise
545          */
546         private boolean hasFirstStartNotification() {
547                 return notificationManager.getNotification("first-start-notification") != null;
548         }
549
550         //
551         // ACTIONS
552         //
553
554         /**
555          * Starts the web interface and registers all toadlets.
556          */
557         public void start() {
558                 registerToadlets();
559
560                 /* notification templates. */
561                 Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html");
562
563                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
564                 notificationManager.addNotification(startupNotification);
565
566                 ticker.schedule(new Runnable() {
567
568                         @Override
569                         public void run() {
570                                 startupNotification.dismiss();
571                         }
572                 }, 2, TimeUnit.MINUTES);
573
574                 Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
575                 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
576                 ticker.scheduleAtFixedRate(new Runnable() {
577
578                         @Override
579                         @SuppressWarnings("synthetic-access")
580                         public void run() {
581                                 if (getCore().getIdentityManager().isConnected()) {
582                                         wotMissingNotification.dismiss();
583                                 } else {
584                                         notificationManager.addNotification(wotMissingNotification);
585                                 }
586                         }
587
588                 }, 15, 15, TimeUnit.SECONDS);
589         }
590
591         /**
592          * Stops the web interface and unregisters all toadlets.
593          */
594         public void stop() {
595                 unregisterToadlets();
596                 ticker.shutdownNow();
597         }
598
599         //
600         // PRIVATE METHODS
601         //
602
603         /**
604          * Register all toadlets.
605          */
606         private void registerToadlets() {
607                 Template emptyTemplate = parse(new StringReader(""));
608                 Template loginTemplate = loaders.loadTemplate("/templates/login.html");
609                 Template indexTemplate = loaders.loadTemplate("/templates/index.html");
610                 Template newTemplate = loaders.loadTemplate("/templates/new.html");
611                 Template knownSonesTemplate = loaders.loadTemplate("/templates/knownSones.html");
612                 Template createSoneTemplate = loaders.loadTemplate("/templates/createSone.html");
613                 Template createPostTemplate = loaders.loadTemplate("/templates/createPost.html");
614                 Template createReplyTemplate = loaders.loadTemplate("/templates/createReply.html");
615                 Template bookmarksTemplate = loaders.loadTemplate("/templates/bookmarks.html");
616                 Template searchTemplate = loaders.loadTemplate("/templates/search.html");
617                 Template editProfileTemplate = loaders.loadTemplate("/templates/editProfile.html");
618                 Template editProfileFieldTemplate = loaders.loadTemplate("/templates/editProfileField.html");
619                 Template deleteProfileFieldTemplate = loaders.loadTemplate("/templates/deleteProfileField.html");
620                 Template viewSoneTemplate = loaders.loadTemplate("/templates/viewSone.html");
621                 Template viewPostTemplate = loaders.loadTemplate("/templates/viewPost.html");
622                 Template deletePostTemplate = loaders.loadTemplate("/templates/deletePost.html");
623                 Template deleteReplyTemplate = loaders.loadTemplate("/templates/deleteReply.html");
624                 Template deleteSoneTemplate = loaders.loadTemplate("/templates/deleteSone.html");
625                 Template imageBrowserTemplate = loaders.loadTemplate("/templates/imageBrowser.html");
626                 Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html");
627                 Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html");
628                 Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html");
629                 Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html");
630                 Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html");
631                 Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html");
632                 Template optionsTemplate = loaders.loadTemplate("/templates/options.html");
633                 Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html");
634                 Template aboutTemplate = loaders.loadTemplate("/templates/about.html");
635                 Template invalidTemplate = loaders.loadTemplate("/templates/invalid.html");
636                 Template postTemplate = loaders.loadTemplate("/templates/include/viewPost.html");
637                 Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html");
638                 Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml");
639
640                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
641                 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
642                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
643                 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
644                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
645                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
646                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
647                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
648                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
649                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
650                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
651                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
652                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
653                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
654                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
655                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
656                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
657                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
658                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
659                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
660                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
661                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
662                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
663                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
664                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
665                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
666                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
667                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
668                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
669                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
670                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
671                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
672                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
673                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
674                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
675                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
676                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
677                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
678                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
679                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
680                 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
681                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION, SonePlugin.getYear(), SonePlugin.getHomepage()), "About"));
682                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
683                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
684                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
685                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
686                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
687                 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css")));
688                 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("javascript/", "/static/javascript/", "text/javascript")));
689                 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("images/", "/static/images/", "image/png")));
690                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
691                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
692                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
693                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
694                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
695                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
696                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
697                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
698                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
699                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
700                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
701                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
702                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
703                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
704                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
705                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
706                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
707                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
708                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
709                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
710                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
711                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
712                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
713                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
714                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
715                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
716                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
717                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
718                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
719                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
720                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
721
722                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
723                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
724                 for (PageToadlet toadlet : pageToadlets) {
725                         String menuName = toadlet.getMenuName();
726                         if (menuName != null) {
727                                 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
728                         } else {
729                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
730                         }
731                 }
732         }
733
734         /**
735          * Unregisters all toadlets.
736          */
737         private void unregisterToadlets() {
738                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
739                 for (PageToadlet pageToadlet : pageToadlets) {
740                         toadletContainer.unregister(pageToadlet);
741                 }
742                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
743         }
744
745         /**
746          * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
747          * {@link SonePart}s in the given text (after parsing it using
748          * {@link SoneTextParser}).
749          *
750          * @param text
751          *            The text to parse
752          * @return All mentioned local Sones
753          */
754         private Collection<Sone> getMentionedSones(String text) {
755                 /* we need no context to find mentioned Sones. */
756                 Set<Sone> mentionedSones = new HashSet<Sone>();
757                 try {
758                         for (Part part : soneTextParser.parse(null, new StringReader(text))) {
759                                 if (part instanceof SonePart) {
760                                         mentionedSones.add(((SonePart) part).getSone());
761                                 }
762                         }
763                 } catch (IOException ioe1) {
764                         logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
765                 }
766                 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
767         }
768
769         /**
770          * Returns the Sone insert notification for the given Sone. If no
771          * notification for the given Sone exists, a new notification is created and
772          * cached.
773          *
774          * @param sone
775          *            The Sone to get the insert notification for
776          * @return The Sone insert notification
777          */
778         private TemplateNotification getSoneInsertNotification(Sone sone) {
779                 synchronized (soneInsertNotifications) {
780                         TemplateNotification templateNotification = soneInsertNotifications.get(sone);
781                         if (templateNotification == null) {
782                                 templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html"));
783                                 templateNotification.set("insertSone", sone);
784                                 soneInsertNotifications.put(sone, templateNotification);
785                         }
786                         return templateNotification;
787                 }
788         }
789
790         private boolean localSoneMentionedInNewPostOrReply(Post post) {
791                 if (!post.getSone().isLocal()) {
792                         if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
793                                 return true;
794                         }
795                 }
796                 for (PostReply postReply : getCore().getReplies(post.getId())) {
797                         if (postReply.getSone().isLocal()) {
798                                 continue;
799                         }
800                         if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
801                                 return true;
802                         }
803                 }
804                 return false;
805         }
806
807         //
808         // EVENT HANDLERS
809         //
810
811         /**
812          * Notifies the web interface that a new {@link Sone} was found.
813          *
814          * @param newSoneFoundEvent
815          *            The event
816          */
817         @Subscribe
818         public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
819                 newSoneNotification.add(newSoneFoundEvent.sone());
820                 if (!hasFirstStartNotification()) {
821                         notificationManager.addNotification(newSoneNotification);
822                 }
823         }
824
825         /**
826          * Notifies the web interface that a new {@link Post} was found.
827          *
828          * @param newPostFoundEvent
829          *            The event
830          */
831         @Subscribe
832         public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
833                 Post post = newPostFoundEvent.post();
834                 boolean isLocal = post.getSone().isLocal();
835                 if (isLocal) {
836                         localPostNotification.add(post);
837                 } else {
838                         newPostNotification.add(post);
839                 }
840                 if (!hasFirstStartNotification()) {
841                         notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
842                         if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
843                                 mentionNotification.add(post);
844                                 notificationManager.addNotification(mentionNotification);
845                         }
846                 } else {
847                         getCore().markPostKnown(post);
848                 }
849         }
850
851         /**
852          * Notifies the web interface that a new {@link PostReply} was found.
853          *
854          * @param newPostReplyFoundEvent
855          *            The event
856          */
857         @Subscribe
858         public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
859                 PostReply reply = newPostReplyFoundEvent.postReply();
860                 boolean isLocal = reply.getSone().isLocal();
861                 if (isLocal) {
862                         localReplyNotification.add(reply);
863                 } else {
864                         newReplyNotification.add(reply);
865                 }
866                 if (!hasFirstStartNotification()) {
867                         notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
868                         if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
869                                 mentionNotification.add(reply.getPost().get());
870                                 notificationManager.addNotification(mentionNotification);
871                         }
872                 } else {
873                         getCore().markReplyKnown(reply);
874                 }
875         }
876
877         /**
878          * Notifies the web interface that a {@link Sone} was marked as known.
879          *
880          * @param markSoneKnownEvent
881          *            The event
882          */
883         @Subscribe
884         public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
885                 newSoneNotification.remove(markSoneKnownEvent.sone());
886         }
887
888         /**
889          * Notifies the web interface that a {@link Post} was marked as known.
890          *
891          * @param markPostKnownEvent
892          *            The event
893          */
894         @Subscribe
895         public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
896                 newPostNotification.remove(markPostKnownEvent.post());
897                 localPostNotification.remove(markPostKnownEvent.post());
898                 if (!localSoneMentionedInNewPostOrReply(markPostKnownEvent.post())) {
899                         mentionNotification.remove(markPostKnownEvent.post());
900                 }
901         }
902
903         /**
904          * Notifies the web interface that a {@link PostReply} was marked as known.
905          *
906          * @param markPostReplyKnownEvent
907          *            The event
908          */
909         @Subscribe
910         public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
911                 PostReply postReply = markPostReplyKnownEvent.postReply();
912                 newReplyNotification.remove(postReply);
913                 localReplyNotification.remove(postReply);
914                 if (postReply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(postReply.getPost().get())) {
915                         mentionNotification.remove(postReply.getPost().get());
916                 }
917         }
918
919         /**
920          * Notifies the web interface that a {@link Sone} was removed.
921          *
922          * @param soneRemovedEvent
923          *            The event
924          */
925         @Subscribe
926         public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
927                 newSoneNotification.remove(soneRemovedEvent.sone());
928         }
929
930         /**
931          * Notifies the web interface that a {@link Post} was removed.
932          *
933          * @param postRemovedEvent
934          *            The event
935          */
936         @Subscribe
937         public void postRemoved(PostRemovedEvent postRemovedEvent) {
938                 newPostNotification.remove(postRemovedEvent.post());
939                 localPostNotification.remove(postRemovedEvent.post());
940                 if (!localSoneMentionedInNewPostOrReply(postRemovedEvent.post())) {
941                         mentionNotification.remove(postRemovedEvent.post());
942                 }
943         }
944
945         /**
946          * Notifies the web interface that a {@link PostReply} was removed.
947          *
948          * @param postReplyRemovedEvent
949          *            The event
950          */
951         @Subscribe
952         public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
953                 PostReply reply = postReplyRemovedEvent.postReply();
954                 newReplyNotification.remove(reply);
955                 localReplyNotification.remove(reply);
956                 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
957                         mentionNotification.remove(reply.getPost().get());
958                 }
959         }
960
961         /**
962          * Notifies the web interface that a Sone was locked.
963          *
964          * @param soneLockedEvent
965          *            The event
966          */
967         @Subscribe
968         public void soneLocked(SoneLockedEvent soneLockedEvent) {
969                 final Sone sone = soneLockedEvent.sone();
970                 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
971
972                         @Override
973                         @SuppressWarnings("synthetic-access")
974                         public void run() {
975                                 lockedSonesNotification.add(sone);
976                                 notificationManager.addNotification(lockedSonesNotification);
977                         }
978                 }, 5, TimeUnit.MINUTES);
979                 lockedSonesTickerObjects.put(sone, tickerObject);
980         }
981
982         /**
983          * Notifies the web interface that a Sone was unlocked.
984          *
985          * @param soneUnlockedEvent
986          *            The event
987          */
988         @Subscribe
989         public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
990                 lockedSonesNotification.remove(soneUnlockedEvent.sone());
991                 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
992         }
993
994         /**
995          * Notifies the web interface that a {@link Sone} is being inserted.
996          *
997          * @param soneInsertingEvent
998          *            The event
999          */
1000         @Subscribe
1001         public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
1002                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
1003                 soneInsertNotification.set("soneStatus", "inserting");
1004                 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1005                         notificationManager.addNotification(soneInsertNotification);
1006                 }
1007         }
1008
1009         /**
1010          * Notifies the web interface that a {@link Sone} was inserted.
1011          *
1012          * @param soneInsertedEvent
1013          *            The event
1014          */
1015         @Subscribe
1016         public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1017                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1018                 soneInsertNotification.set("soneStatus", "inserted");
1019                 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1020                 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1021                         notificationManager.addNotification(soneInsertNotification);
1022                 }
1023         }
1024
1025         /**
1026          * Notifies the web interface that a {@link Sone} insert was aborted.
1027          *
1028          * @param soneInsertAbortedEvent
1029          *            The event
1030          */
1031         @Subscribe
1032         public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1033                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1034                 soneInsertNotification.set("soneStatus", "insert-aborted");
1035                 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1036                 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1037                         notificationManager.addNotification(soneInsertNotification);
1038                 }
1039         }
1040
1041         /**
1042          * Notifies the web interface that a new Sone version was found.
1043          *
1044          * @param updateFoundEvent
1045          *            The event
1046          */
1047         @Subscribe
1048         public void updateFound(UpdateFoundEvent updateFoundEvent) {
1049                 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1050                 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1051                 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1052                 notificationManager.addNotification(newVersionNotification);
1053         }
1054
1055         /**
1056          * Notifies the web interface that an image insert was started
1057          *
1058          * @param imageInsertStartedEvent
1059          *            The event
1060          */
1061         @Subscribe
1062         public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1063                 insertingImagesNotification.add(imageInsertStartedEvent.image());
1064                 notificationManager.addNotification(insertingImagesNotification);
1065         }
1066
1067         /**
1068          * Notifies the web interface that an {@link Image} insert was aborted.
1069          *
1070          * @param imageInsertAbortedEvent
1071          *            The event
1072          */
1073         @Subscribe
1074         public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1075                 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1076         }
1077
1078         /**
1079          * Notifies the web interface that an {@link Image} insert is finished.
1080          *
1081          * @param imageInsertFinishedEvent
1082          *            The event
1083          */
1084         @Subscribe
1085         public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1086                 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1087                 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1088                 notificationManager.addNotification(insertedImagesNotification);
1089         }
1090
1091         /**
1092          * Notifies the web interface that an {@link Image} insert has failed.
1093          *
1094          * @param imageInsertFailedEvent
1095          *            The event
1096          */
1097         @Subscribe
1098         public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1099                 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1100                 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1101                 notificationManager.addNotification(imageInsertFailedNotification);
1102         }
1103
1104 }