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