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