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