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