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