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