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