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