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