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