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