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