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