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