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