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