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