Add possibility to load templates from the filesystem
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * Sone - WebInterface.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import static java.util.logging.Logger.getLogger;
21 import static net.pterodactylus.util.template.TemplateParser.parse;
22
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.UUID;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.ScheduledFuture;
37 import java.util.concurrent.TimeUnit;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40
41 import net.pterodactylus.sone.core.Core;
42 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
43 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
44 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
45 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
46 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
47 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
48 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
49 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
50 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
51 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
52 import net.pterodactylus.sone.core.event.PostRemovedEvent;
53 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
54 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
55 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
56 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
57 import net.pterodactylus.sone.core.event.SoneLockedEvent;
58 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
59 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
60 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
61 import net.pterodactylus.sone.data.Album;
62 import net.pterodactylus.sone.data.Image;
63 import net.pterodactylus.sone.data.Post;
64 import net.pterodactylus.sone.data.PostReply;
65 import net.pterodactylus.sone.data.Profile;
66 import net.pterodactylus.sone.data.Reply;
67 import net.pterodactylus.sone.data.Sone;
68 import net.pterodactylus.sone.freenet.L10nFilter;
69 import net.pterodactylus.sone.freenet.wot.Identity;
70 import net.pterodactylus.sone.freenet.wot.Trust;
71 import net.pterodactylus.sone.main.Loaders;
72 import net.pterodactylus.sone.main.ReparseFilter;
73 import net.pterodactylus.sone.main.SonePlugin;
74 import net.pterodactylus.sone.notify.ListNotification;
75 import net.pterodactylus.sone.template.AlbumAccessor;
76 import net.pterodactylus.sone.template.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.ClassPathTemplateProvider;
133 import net.pterodactylus.util.template.CollectionSortFilter;
134 import net.pterodactylus.util.template.ContainsFilter;
135 import net.pterodactylus.util.template.DateFilter;
136 import net.pterodactylus.util.template.FormatFilter;
137 import net.pterodactylus.util.template.HtmlFilter;
138 import net.pterodactylus.util.template.MatchFilter;
139 import net.pterodactylus.util.template.ModFilter;
140 import net.pterodactylus.util.template.PaginationFilter;
141 import net.pterodactylus.util.template.ReflectionAccessor;
142 import net.pterodactylus.util.template.ReplaceFilter;
143 import net.pterodactylus.util.template.StoreFilter;
144 import net.pterodactylus.util.template.Template;
145 import net.pterodactylus.util.template.TemplateContextFactory;
146 import net.pterodactylus.util.template.TemplateProvider;
147 import net.pterodactylus.util.template.XmlFilter;
148 import net.pterodactylus.util.web.RedirectPage;
149 import net.pterodactylus.util.web.StaticPage;
150 import net.pterodactylus.util.web.TemplatePage;
151
152 import com.google.common.collect.Collections2;
153 import com.google.common.collect.ImmutableSet;
154 import com.google.common.eventbus.Subscribe;
155 import com.google.inject.Inject;
156
157 import freenet.clients.http.SessionManager;
158 import freenet.clients.http.SessionManager.Session;
159 import freenet.clients.http.ToadletContainer;
160 import freenet.clients.http.ToadletContext;
161 import freenet.l10n.BaseL10n;
162 import freenet.support.api.HTTPRequest;
163
164 /**
165  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
166  * references to l10n helpers.
167  *
168  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
169  */
170 public class WebInterface {
171
172         /** The logger. */
173         private static final Logger logger = getLogger(WebInterface.class.getName());
174
175         /** The loaders for templates, pages, and classpath providers. */
176         private final Loaders loaders;
177
178         /** The notification manager. */
179         private final NotificationManager notificationManager = new NotificationManager();
180
181         /** The Sone plugin. */
182         private final SonePlugin sonePlugin;
183
184         /** The registered toadlets. */
185         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
186
187         /** The form password. */
188         private final String formPassword;
189
190         /** The template context factory. */
191         private final TemplateContextFactory templateContextFactory;
192
193         /** The Sone text parser. */
194         private final SoneTextParser soneTextParser;
195
196         /** The parser filter. */
197         private final ParserFilter parserFilter;
198
199         /** The “new Sone” notification. */
200         private final ListNotification<Sone> newSoneNotification;
201
202         /** The “new post” notification. */
203         private final ListNotification<Post> newPostNotification;
204
205         /** The “new reply” notification. */
206         private final ListNotification<PostReply> newReplyNotification;
207
208         /** The invisible “local post” notification. */
209         private final ListNotification<Post> localPostNotification;
210
211         /** The invisible “local reply” notification. */
212         private final ListNotification<PostReply> localReplyNotification;
213
214         /** The “you have been mentioned” notification. */
215         private final ListNotification<Post> mentionNotification;
216
217         /** Notifications for sone inserts. */
218         private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
219
220         /** Sone locked notification ticker objects. */
221         private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
222
223         /** The “Sone locked” notification. */
224         private final ListNotification<Sone> lockedSonesNotification;
225
226         /** The “new version” notification. */
227         private final TemplateNotification newVersionNotification;
228
229         /** The “inserting images” notification. */
230         private final ListNotification<Image> insertingImagesNotification;
231
232         /** The “inserted images” notification. */
233         private final ListNotification<Image> insertedImagesNotification;
234
235         /** The “image insert failed” notification. */
236         private final ListNotification<Image> imageInsertFailedNotification;
237
238         /** Scheduled executor for time-based notifications. */
239         private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
240
241         /**
242          * Creates a new web interface.
243          *
244          * @param sonePlugin
245          *            The Sone plugin
246          */
247         @Inject
248         public WebInterface(SonePlugin sonePlugin, Loaders loaders) {
249                 this.sonePlugin = sonePlugin;
250                 this.loaders = loaders;
251                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
252                 soneTextParser = new SoneTextParser(getCore(), getCore());
253
254                 templateContextFactory = new TemplateContextFactory();
255                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
256                 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
257                 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
258                 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
259                 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
260                 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
261                 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
262                 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
263                 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
264                 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
265                 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
266                 templateContextFactory.addFilter("date", new DateFilter());
267                 templateContextFactory.addFilter("html", new HtmlFilter());
268                 templateContextFactory.addFilter("replace", new ReplaceFilter());
269                 templateContextFactory.addFilter("store", new StoreFilter());
270                 templateContextFactory.addFilter("l10n", new L10nFilter(this));
271                 templateContextFactory.addFilter("substring", new SubstringFilter());
272                 templateContextFactory.addFilter("xml", new XmlFilter());
273                 templateContextFactory.addFilter("change", new RequestChangeFilter());
274                 templateContextFactory.addFilter("match", new MatchFilter());
275                 templateContextFactory.addFilter("css", new CssClassNameFilter());
276                 templateContextFactory.addFilter("js", new JavascriptFilter());
277                 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
278                 templateContextFactory.addFilter("reparse", new ReparseFilter());
279                 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
280                 templateContextFactory.addFilter("format", new FormatFilter());
281                 templateContextFactory.addFilter("sort", new CollectionSortFilter());
282                 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
283                 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
284                 templateContextFactory.addFilter("in", new ContainsFilter());
285                 templateContextFactory.addFilter("unique", new UniqueElementFilter());
286                 templateContextFactory.addFilter("mod", new ModFilter());
287                 templateContextFactory.addFilter("paginate", new PaginationFilter());
288                 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
289                 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
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<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
296
297                 Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
298                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
299
300                 Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
301                 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
302
303                 Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
304                 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
305
306                 Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
307                 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
308
309                 Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
310                 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
311
312                 Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
313                 lockedSonesNotification = new ListNotification<Sone>("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<Image>("inserting-images-notification", "images", insertingImagesTemplate);
320
321                 Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
322                 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
323
324                 Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
325                 imageInsertFailedNotification = new ListNotification<Image>("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         public Core getCore() {
338                 return sonePlugin.core();
339         }
340
341         /**
342          * Returns the template context factory of the web interface.
343          *
344          * @return The template context factory
345          */
346         public TemplateContextFactory getTemplateContextFactory() {
347                 return templateContextFactory;
348         }
349
350         /**
351          * Returns the current session, creating a new session if there is no
352          * current session.
353          *
354          * @param toadletContenxt
355          *            The toadlet context
356          * @return The current session, or {@code null} if there is no current
357          *         session
358          */
359         public Session getCurrentSession(ToadletContext toadletContenxt) {
360                 return getCurrentSession(toadletContenxt, true);
361         }
362
363         /**
364          * Returns the current session, creating a new session if there is no
365          * current session and {@code create} is {@code true}.
366          *
367          * @param toadletContenxt
368          *            The toadlet context
369          * @param create
370          *            {@code true} to create a new session if there is no current
371          *            session, {@code false} otherwise
372          * @return The current session, or {@code null} if there is no current
373          *         session
374          */
375         public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
376                 Session session = getSessionManager().useSession(toadletContenxt);
377                 if (create && (session == null)) {
378                         session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
379                 }
380                 return session;
381         }
382
383         /**
384          * Returns the currently logged in Sone.
385          *
386          * @param toadletContext
387          *            The toadlet context
388          * @return The currently logged in Sone, or {@code null} if no Sone is
389          *         currently logged in
390          */
391         public Sone getCurrentSone(ToadletContext toadletContext) {
392                 return getCurrentSone(toadletContext, true);
393         }
394
395         /**
396          * Returns the currently logged in Sone.
397          *
398          * @param toadletContext
399          *            The toadlet context
400          * @param create
401          *            {@code true} to create a new session if no session exists,
402          *            {@code false} to not create a new session
403          * @return The currently logged in Sone, or {@code null} if no Sone is
404          *         currently logged in
405          */
406         public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
407                 Collection<Sone> localSones = getCore().getLocalSones();
408                 if (localSones.size() == 1) {
409                         return localSones.iterator().next();
410                 }
411                 return getCurrentSone(getCurrentSession(toadletContext, create));
412         }
413
414         /**
415          * Returns the currently logged in Sone.
416          *
417          * @param session
418          *            The session
419          * @return The currently logged in Sone, or {@code null} if no Sone is
420          *         currently logged in
421          */
422         public Sone getCurrentSone(Session session) {
423                 if (session == null) {
424                         return null;
425                 }
426                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
427                 if (soneId == null) {
428                         return null;
429                 }
430                 return getCore().getLocalSone(soneId);
431         }
432
433         /**
434          * Sets the currently logged in Sone.
435          *
436          * @param toadletContext
437          *            The toadlet context
438          * @param sone
439          *            The Sone to set as currently logged in
440          */
441         public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
442                 Session session = getCurrentSession(toadletContext);
443                 if (sone == null) {
444                         session.removeAttribute("Sone.CurrentSone");
445                 } else {
446                         session.setAttribute("Sone.CurrentSone", sone.getId());
447                 }
448         }
449
450         /**
451          * Returns the notification manager.
452          *
453          * @return The notification manager
454          */
455         public NotificationManager getNotifications() {
456                 return notificationManager;
457         }
458
459         /**
460          * Returns the l10n helper of the node.
461          *
462          * @return The node’s l10n helper
463          */
464         public BaseL10n getL10n() {
465                 return sonePlugin.l10n().getBase();
466         }
467
468         /**
469          * Returns the session manager of the node.
470          *
471          * @return The node’s session manager
472          */
473         public SessionManager getSessionManager() {
474                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
475         }
476
477         /**
478          * Returns the node’s form password.
479          *
480          * @return The form password
481          */
482         public String getFormPassword() {
483                 return formPassword;
484         }
485
486         /**
487          * Returns the posts that have been announced as new in the
488          * {@link #newPostNotification}.
489          *
490          * @return The new posts
491          */
492         public Set<Post> getNewPosts() {
493                 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
494         }
495
496         /**
497          * Returns the replies that have been announced as new in the
498          * {@link #newReplyNotification}.
499          *
500          * @return The new replies
501          */
502         public Set<PostReply> getNewReplies() {
503                 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
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), "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, SonePlugin.VERSION, SonePlugin.getYear(), SonePlugin.getHomepage()), "About"));
683                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
684                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
685                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
686                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
687                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
688                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
689                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
690                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("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 GetTranslationPage(this)));
694                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
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 GetTimesAjaxPage(this)));
702                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
703                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
704                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
705                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
706                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
707                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
708                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
709                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
710                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
711                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
712                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
713                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
714                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
715                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
716                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
717                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
718                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
719                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
720                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
721                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
722
723                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
724                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
725                 for (PageToadlet toadlet : pageToadlets) {
726                         String menuName = toadlet.getMenuName();
727                         if (menuName != null) {
728                                 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
729                         } else {
730                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
731                         }
732                 }
733         }
734
735         /**
736          * Unregisters all toadlets.
737          */
738         private void unregisterToadlets() {
739                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
740                 for (PageToadlet pageToadlet : pageToadlets) {
741                         toadletContainer.unregister(pageToadlet);
742                 }
743                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
744         }
745
746         /**
747          * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
748          * {@link SonePart}s in the given text (after parsing it using
749          * {@link SoneTextParser}).
750          *
751          * @param text
752          *            The text to parse
753          * @return All mentioned local Sones
754          */
755         private Collection<Sone> getMentionedSones(String text) {
756                 /* we need no context to find mentioned Sones. */
757                 Set<Sone> mentionedSones = new HashSet<Sone>();
758                 try {
759                         for (Part part : soneTextParser.parse(null, new StringReader(text))) {
760                                 if (part instanceof SonePart) {
761                                         mentionedSones.add(((SonePart) part).getSone());
762                                 }
763                         }
764                 } catch (IOException ioe1) {
765                         logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
766                 }
767                 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
768         }
769
770         /**
771          * Returns the Sone insert notification for the given Sone. If no
772          * notification for the given Sone exists, a new notification is created and
773          * cached.
774          *
775          * @param sone
776          *            The Sone to get the insert notification for
777          * @return The Sone insert notification
778          */
779         private TemplateNotification getSoneInsertNotification(Sone sone) {
780                 synchronized (soneInsertNotifications) {
781                         TemplateNotification templateNotification = soneInsertNotifications.get(sone);
782                         if (templateNotification == null) {
783                                 templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html"));
784                                 templateNotification.set("insertSone", sone);
785                                 soneInsertNotifications.put(sone, templateNotification);
786                         }
787                         return templateNotification;
788                 }
789         }
790
791         private boolean localSoneMentionedInNewPostOrReply(Post post) {
792                 if (!post.getSone().isLocal()) {
793                         if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
794                                 return true;
795                         }
796                 }
797                 for (PostReply postReply : getCore().getReplies(post.getId())) {
798                         if (postReply.getSone().isLocal()) {
799                                 continue;
800                         }
801                         if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
802                                 return true;
803                         }
804                 }
805                 return false;
806         }
807
808         //
809         // EVENT HANDLERS
810         //
811
812         /**
813          * Notifies the web interface that a new {@link Sone} was found.
814          *
815          * @param newSoneFoundEvent
816          *            The event
817          */
818         @Subscribe
819         public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
820                 newSoneNotification.add(newSoneFoundEvent.sone());
821                 if (!hasFirstStartNotification()) {
822                         notificationManager.addNotification(newSoneNotification);
823                 }
824         }
825
826         /**
827          * Notifies the web interface that a new {@link Post} was found.
828          *
829          * @param newPostFoundEvent
830          *            The event
831          */
832         @Subscribe
833         public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
834                 Post post = newPostFoundEvent.post();
835                 boolean isLocal = post.getSone().isLocal();
836                 if (isLocal) {
837                         localPostNotification.add(post);
838                 } else {
839                         newPostNotification.add(post);
840                 }
841                 if (!hasFirstStartNotification()) {
842                         notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
843                         if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
844                                 mentionNotification.add(post);
845                                 notificationManager.addNotification(mentionNotification);
846                         }
847                 } else {
848                         getCore().markPostKnown(post);
849                 }
850         }
851
852         /**
853          * Notifies the web interface that a new {@link PostReply} was found.
854          *
855          * @param newPostReplyFoundEvent
856          *            The event
857          */
858         @Subscribe
859         public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
860                 PostReply reply = newPostReplyFoundEvent.postReply();
861                 boolean isLocal = reply.getSone().isLocal();
862                 if (isLocal) {
863                         localReplyNotification.add(reply);
864                 } else {
865                         newReplyNotification.add(reply);
866                 }
867                 if (!hasFirstStartNotification()) {
868                         notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
869                         if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
870                                 mentionNotification.add(reply.getPost().get());
871                                 notificationManager.addNotification(mentionNotification);
872                         }
873                 } else {
874                         getCore().markReplyKnown(reply);
875                 }
876         }
877
878         /**
879          * Notifies the web interface that a {@link Sone} was marked as known.
880          *
881          * @param markSoneKnownEvent
882          *            The event
883          */
884         @Subscribe
885         public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
886                 newSoneNotification.remove(markSoneKnownEvent.sone());
887         }
888
889         /**
890          * Notifies the web interface that a {@link Post} was marked as known.
891          *
892          * @param markPostKnownEvent
893          *            The event
894          */
895         @Subscribe
896         public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
897                 newPostNotification.remove(markPostKnownEvent.post());
898                 localPostNotification.remove(markPostKnownEvent.post());
899                 if (!localSoneMentionedInNewPostOrReply(markPostKnownEvent.post())) {
900                         mentionNotification.remove(markPostKnownEvent.post());
901                 }
902         }
903
904         /**
905          * Notifies the web interface that a {@link PostReply} was marked as known.
906          *
907          * @param markPostReplyKnownEvent
908          *            The event
909          */
910         @Subscribe
911         public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
912                 PostReply postReply = markPostReplyKnownEvent.postReply();
913                 newReplyNotification.remove(postReply);
914                 localReplyNotification.remove(postReply);
915                 if (postReply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(postReply.getPost().get())) {
916                         mentionNotification.remove(postReply.getPost().get());
917                 }
918         }
919
920         /**
921          * Notifies the web interface that a {@link Sone} was removed.
922          *
923          * @param soneRemovedEvent
924          *            The event
925          */
926         @Subscribe
927         public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
928                 newSoneNotification.remove(soneRemovedEvent.sone());
929         }
930
931         /**
932          * Notifies the web interface that a {@link Post} was removed.
933          *
934          * @param postRemovedEvent
935          *            The event
936          */
937         @Subscribe
938         public void postRemoved(PostRemovedEvent postRemovedEvent) {
939                 newPostNotification.remove(postRemovedEvent.post());
940                 localPostNotification.remove(postRemovedEvent.post());
941                 if (!localSoneMentionedInNewPostOrReply(postRemovedEvent.post())) {
942                         mentionNotification.remove(postRemovedEvent.post());
943                 }
944         }
945
946         /**
947          * Notifies the web interface that a {@link PostReply} was removed.
948          *
949          * @param postReplyRemovedEvent
950          *            The event
951          */
952         @Subscribe
953         public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
954                 PostReply reply = postReplyRemovedEvent.postReply();
955                 newReplyNotification.remove(reply);
956                 localReplyNotification.remove(reply);
957                 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
958                         mentionNotification.remove(reply.getPost().get());
959                 }
960         }
961
962         /**
963          * Notifies the web interface that a Sone was locked.
964          *
965          * @param soneLockedEvent
966          *            The event
967          */
968         @Subscribe
969         public void soneLocked(SoneLockedEvent soneLockedEvent) {
970                 final Sone sone = soneLockedEvent.sone();
971                 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
972
973                         @Override
974                         @SuppressWarnings("synthetic-access")
975                         public void run() {
976                                 lockedSonesNotification.add(sone);
977                                 notificationManager.addNotification(lockedSonesNotification);
978                         }
979                 }, 5, TimeUnit.MINUTES);
980                 lockedSonesTickerObjects.put(sone, tickerObject);
981         }
982
983         /**
984          * Notifies the web interface that a Sone was unlocked.
985          *
986          * @param soneUnlockedEvent
987          *            The event
988          */
989         @Subscribe
990         public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
991                 lockedSonesNotification.remove(soneUnlockedEvent.sone());
992                 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
993         }
994
995         /**
996          * Notifies the web interface that a {@link Sone} is being inserted.
997          *
998          * @param soneInsertingEvent
999          *            The event
1000          */
1001         @Subscribe
1002         public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
1003                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
1004                 soneInsertNotification.set("soneStatus", "inserting");
1005                 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1006                         notificationManager.addNotification(soneInsertNotification);
1007                 }
1008         }
1009
1010         /**
1011          * Notifies the web interface that a {@link Sone} was inserted.
1012          *
1013          * @param soneInsertedEvent
1014          *            The event
1015          */
1016         @Subscribe
1017         public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1018                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1019                 soneInsertNotification.set("soneStatus", "inserted");
1020                 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1021                 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1022                         notificationManager.addNotification(soneInsertNotification);
1023                 }
1024         }
1025
1026         /**
1027          * Notifies the web interface that a {@link Sone} insert was aborted.
1028          *
1029          * @param soneInsertAbortedEvent
1030          *            The event
1031          */
1032         @Subscribe
1033         public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1034                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1035                 soneInsertNotification.set("soneStatus", "insert-aborted");
1036                 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1037                 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1038                         notificationManager.addNotification(soneInsertNotification);
1039                 }
1040         }
1041
1042         /**
1043          * Notifies the web interface that a new Sone version was found.
1044          *
1045          * @param updateFoundEvent
1046          *            The event
1047          */
1048         @Subscribe
1049         public void updateFound(UpdateFoundEvent updateFoundEvent) {
1050                 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1051                 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1052                 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1053                 notificationManager.addNotification(newVersionNotification);
1054         }
1055
1056         /**
1057          * Notifies the web interface that an image insert was started
1058          *
1059          * @param imageInsertStartedEvent
1060          *            The event
1061          */
1062         @Subscribe
1063         public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1064                 insertingImagesNotification.add(imageInsertStartedEvent.image());
1065                 notificationManager.addNotification(insertingImagesNotification);
1066         }
1067
1068         /**
1069          * Notifies the web interface that an {@link Image} insert was aborted.
1070          *
1071          * @param imageInsertAbortedEvent
1072          *            The event
1073          */
1074         @Subscribe
1075         public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1076                 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1077         }
1078
1079         /**
1080          * Notifies the web interface that an {@link Image} insert is finished.
1081          *
1082          * @param imageInsertFinishedEvent
1083          *            The event
1084          */
1085         @Subscribe
1086         public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1087                 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1088                 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1089                 notificationManager.addNotification(insertedImagesNotification);
1090         }
1091
1092         /**
1093          * Notifies the web interface that an {@link Image} insert has failed.
1094          *
1095          * @param imageInsertFailedEvent
1096          *            The event
1097          */
1098         @Subscribe
1099         public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1100                 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1101                 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1102                 notificationManager.addNotification(imageInsertFailedNotification);
1103         }
1104
1105 }