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