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