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