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