Add page that marks posts, replies, and Sones as read.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * FreenetSone - WebInterface.java - Copyright © 2010 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.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.StringReader;
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.UUID;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36
37 import net.pterodactylus.sone.core.Core;
38 import net.pterodactylus.sone.core.CoreListener;
39 import net.pterodactylus.sone.data.Post;
40 import net.pterodactylus.sone.data.Reply;
41 import net.pterodactylus.sone.data.Sone;
42 import net.pterodactylus.sone.freenet.L10nFilter;
43 import net.pterodactylus.sone.freenet.wot.Identity;
44 import net.pterodactylus.sone.freenet.wot.Trust;
45 import net.pterodactylus.sone.main.SonePlugin;
46 import net.pterodactylus.sone.notify.ListNotification;
47 import net.pterodactylus.sone.template.CollectionAccessor;
48 import net.pterodactylus.sone.template.CssClassNameFilter;
49 import net.pterodactylus.sone.template.GetPagePlugin;
50 import net.pterodactylus.sone.template.IdentityAccessor;
51 import net.pterodactylus.sone.template.JavascriptFilter;
52 import net.pterodactylus.sone.template.NotificationManagerAccessor;
53 import net.pterodactylus.sone.template.ParserFilter;
54 import net.pterodactylus.sone.template.PostAccessor;
55 import net.pterodactylus.sone.template.ReplyAccessor;
56 import net.pterodactylus.sone.template.RequestChangeFilter;
57 import net.pterodactylus.sone.template.SoneAccessor;
58 import net.pterodactylus.sone.template.SubstringFilter;
59 import net.pterodactylus.sone.template.TrustAccessor;
60 import net.pterodactylus.sone.template.UnknownDateFilter;
61 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
62 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
63 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
64 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
65 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
66 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
67 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
68 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
69 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
70 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
71 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
72 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
73 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
74 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
75 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
76 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
77 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
78 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
79 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
80 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
81 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
82 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
83 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
84 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
85 import net.pterodactylus.sone.web.page.PageToadlet;
86 import net.pterodactylus.sone.web.page.PageToadletFactory;
87 import net.pterodactylus.sone.web.page.StaticPage;
88 import net.pterodactylus.util.logging.Logging;
89 import net.pterodactylus.util.notify.Notification;
90 import net.pterodactylus.util.notify.NotificationManager;
91 import net.pterodactylus.util.notify.TemplateNotification;
92 import net.pterodactylus.util.template.DateFilter;
93 import net.pterodactylus.util.template.DefaultTemplateFactory;
94 import net.pterodactylus.util.template.MatchFilter;
95 import net.pterodactylus.util.template.PaginationPlugin;
96 import net.pterodactylus.util.template.ReflectionAccessor;
97 import net.pterodactylus.util.template.Template;
98 import net.pterodactylus.util.template.TemplateException;
99 import net.pterodactylus.util.template.TemplateFactory;
100 import net.pterodactylus.util.template.TemplateProvider;
101 import net.pterodactylus.util.template.XmlFilter;
102 import net.pterodactylus.util.thread.Ticker;
103 import net.pterodactylus.util.version.Version;
104 import freenet.clients.http.SessionManager;
105 import freenet.clients.http.SessionManager.Session;
106 import freenet.clients.http.ToadletContainer;
107 import freenet.clients.http.ToadletContext;
108 import freenet.l10n.BaseL10n;
109
110 /**
111  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
112  * references to l10n helpers.
113  *
114  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
115  */
116 public class WebInterface implements CoreListener {
117
118         /** The logger. */
119         private static final Logger logger = Logging.getLogger(WebInterface.class);
120
121         /** The notification manager. */
122         private final NotificationManager notificationManager = new NotificationManager();
123
124         /** The Sone plugin. */
125         private final SonePlugin sonePlugin;
126
127         /** The registered toadlets. */
128         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
129
130         /** The form password. */
131         private final String formPassword;
132
133         /** The template factory. */
134         private DefaultTemplateFactory templateFactory;
135
136         /** The “new Sone” notification. */
137         private final ListNotification<Sone> newSoneNotification;
138
139         /** The “new post” notification. */
140         private final ListNotification<Post> newPostNotification;
141
142         /** The “new reply” notification. */
143         private final ListNotification<Reply> newReplyNotification;
144
145         /** The “rescuing Sone” notification. */
146         private final ListNotification<Sone> rescuingSonesNotification;
147
148         /** The “Sone rescued” notification. */
149         private final ListNotification<Sone> sonesRescuedNotification;
150
151         /** Sone locked notification ticker objects. */
152         private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
153
154         /** The “Sone locked” notification. */
155         private final ListNotification<Sone> lockedSonesNotification;
156
157         /** The “new version” notification. */
158         private final TemplateNotification newVersionNotification;
159
160         /**
161          * Creates a new web interface.
162          *
163          * @param sonePlugin
164          *            The Sone plugin
165          */
166         public WebInterface(SonePlugin sonePlugin) {
167                 this.sonePlugin = sonePlugin;
168                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
169
170                 templateFactory = new DefaultTemplateFactory();
171                 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
172                 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
173                 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
174                 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
175                 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
176                 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
177                 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
178                 templateFactory.addAccessor(Trust.class, new TrustAccessor());
179                 templateFactory.addFilter("date", new DateFilter());
180                 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
181                 templateFactory.addFilter("substring", new SubstringFilter());
182                 templateFactory.addFilter("xml", new XmlFilter());
183                 templateFactory.addFilter("change", new RequestChangeFilter());
184                 templateFactory.addFilter("match", new MatchFilter());
185                 templateFactory.addFilter("css", new CssClassNameFilter());
186                 templateFactory.addFilter("js", new JavascriptFilter());
187                 templateFactory.addFilter("parse", new ParserFilter(templateFactory));
188                 templateFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
189                 templateFactory.addPlugin("getpage", new GetPagePlugin());
190                 templateFactory.addPlugin("paginate", new PaginationPlugin());
191                 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
192                 templateFactory.addTemplateObject("formPassword", formPassword);
193
194                 /* create notifications. */
195                 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
196                 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
197
198                 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
199                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
200
201                 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
202                 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
203
204                 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
205                 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
206
207                 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
208                 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
209
210                 Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
211                 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
212
213                 Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
214                 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
215         }
216
217         //
218         // ACCESSORS
219         //
220
221         /**
222          * Returns the Sone core used by the Sone plugin.
223          *
224          * @return The Sone core
225          */
226         public Core getCore() {
227                 return sonePlugin.core();
228         }
229
230         /**
231          * Returns the current session, creating a new session if there is no
232          * current session.
233          *
234          * @param toadletContenxt
235          *            The toadlet context
236          * @return The current session, or {@code null} if there is no current
237          *         session
238          */
239         public Session getCurrentSession(ToadletContext toadletContenxt) {
240                 return getCurrentSession(toadletContenxt, true);
241         }
242
243         /**
244          * Returns the current session, creating a new session if there is no
245          * current session and {@code create} is {@code true}.
246          *
247          * @param toadletContenxt
248          *            The toadlet context
249          * @param create
250          *            {@code true} to create a new session if there is no current
251          *            session, {@code false} otherwise
252          * @return The current session, or {@code null} if there is no current
253          *         session
254          */
255         public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
256                 Session session = getSessionManager().useSession(toadletContenxt);
257                 if (create && (session == null)) {
258                         session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
259                 }
260                 return session;
261         }
262
263         /**
264          * Returns the currently logged in Sone.
265          *
266          * @param toadletContext
267          *            The toadlet context
268          * @return The currently logged in Sone, or {@code null} if no Sone is
269          *         currently logged in
270          */
271         public Sone getCurrentSone(ToadletContext toadletContext) {
272                 return getCurrentSone(toadletContext, true);
273         }
274
275         /**
276          * Returns the currently logged in Sone.
277          *
278          * @param toadletContext
279          *            The toadlet context
280          * @param create
281          *            {@code true} to create a new session if no session exists,
282          *            {@code false} to not create a new session
283          * @return The currently logged in Sone, or {@code null} if no Sone is
284          *         currently logged in
285          */
286         public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
287                 return getCurrentSone(getCurrentSession(toadletContext, create));
288         }
289
290         /**
291          * Returns the currently logged in Sone.
292          *
293          * @param session
294          *            The session
295          * @return The currently logged in Sone, or {@code null} if no Sone is
296          *         currently logged in
297          */
298         public Sone getCurrentSone(Session session) {
299                 if (session == null) {
300                         return null;
301                 }
302                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
303                 if (soneId == null) {
304                         return null;
305                 }
306                 return getCore().getLocalSone(soneId, false);
307         }
308
309         /**
310          * Sets the currently logged in Sone.
311          *
312          * @param toadletContext
313          *            The toadlet context
314          * @param sone
315          *            The Sone to set as currently logged in
316          */
317         public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
318                 Session session = getCurrentSession(toadletContext);
319                 if (sone == null) {
320                         session.removeAttribute("Sone.CurrentSone");
321                 } else {
322                         session.setAttribute("Sone.CurrentSone", sone.getId());
323                 }
324         }
325
326         /**
327          * Returns the notification manager.
328          *
329          * @return The notification manager
330          */
331         public NotificationManager getNotifications() {
332                 return notificationManager;
333         }
334
335         /**
336          * Returns the l10n helper of the node.
337          *
338          * @return The node’s l10n helper
339          */
340         public BaseL10n getL10n() {
341                 return sonePlugin.l10n().getBase();
342         }
343
344         /**
345          * Returns the session manager of the node.
346          *
347          * @return The node’s session manager
348          */
349         public SessionManager getSessionManager() {
350                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
351         }
352
353         /**
354          * Returns the node’s form password.
355          *
356          * @return The form password
357          */
358         public String getFormPassword() {
359                 return formPassword;
360         }
361
362         /**
363          * Returns the posts that have been announced as new in the
364          * {@link #newPostNotification}.
365          *
366          * @return The new posts
367          */
368         public Set<Post> getNewPosts() {
369                 return new HashSet<Post>(newPostNotification.getElements());
370         }
371
372         /**
373          * Returns the replies that have been announced as new in the
374          * {@link #newReplyNotification}.
375          *
376          * @return The new replies
377          */
378         public Set<Reply> getNewReplies() {
379                 return new HashSet<Reply>(newReplyNotification.getElements());
380         }
381
382         /**
383          * Sets whether the current start of the plugin is the first start. It is
384          * considered a first start if the configuration file does not exist.
385          *
386          * @param firstStart
387          *            {@code true} if no configuration file existed when Sone was
388          *            loaded, {@code false} otherwise
389          */
390         public void setFirstStart(boolean firstStart) {
391                 if (firstStart) {
392                         Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
393                         Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
394                         notificationManager.addNotification(firstStartNotification);
395                 }
396         }
397
398         /**
399          * Sets whether Sone was started with a fresh configuration file.
400          *
401          * @param newConfig
402          *            {@code true} if Sone was started with a fresh configuration,
403          *            {@code false} if the existing configuration could be read
404          */
405         public void setNewConfig(boolean newConfig) {
406                 if (newConfig && !hasFirstStartNotification()) {
407                         Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
408                         Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
409                         notificationManager.addNotification(configNotReadNotification);
410                 }
411         }
412
413         //
414         // PRIVATE ACCESSORS
415         //
416
417         /**
418          * Returns whether the first start notification is currently displayed.
419          *
420          * @return {@code true} if the first-start notification is currently
421          *         displayed, {@code false} otherwise
422          */
423         private boolean hasFirstStartNotification() {
424                 return notificationManager.getNotification("first-start-notification") != null;
425         }
426
427         //
428         // ACTIONS
429         //
430
431         /**
432          * Starts the web interface and registers all toadlets.
433          */
434         public void start() {
435                 registerToadlets();
436
437                 /* notification templates. */
438                 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
439
440                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
441                 notificationManager.addNotification(startupNotification);
442
443                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
444
445                         @Override
446                         public void run() {
447                                 startupNotification.dismiss();
448                         }
449                 }, "Sone Startup Notification Remover");
450
451                 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
452                 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
453                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
454
455                         @Override
456                         @SuppressWarnings("synthetic-access")
457                         public void run() {
458                                 if (getCore().getIdentityManager().isConnected()) {
459                                         wotMissingNotification.dismiss();
460                                 } else {
461                                         notificationManager.addNotification(wotMissingNotification);
462                                 }
463                                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
464                         }
465
466                 }, "Sone WoT Connector Checker");
467         }
468
469         /**
470          * Stops the web interface and unregisters all toadlets.
471          */
472         public void stop() {
473                 unregisterToadlets();
474                 Ticker.getInstance().stop();
475         }
476
477         //
478         // PRIVATE METHODS
479         //
480
481         /**
482          * Register all toadlets.
483          */
484         private void registerToadlets() {
485                 Template emptyTemplate = templateFactory.createTemplate(new StringReader(""));
486                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
487                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
488                 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
489                 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
490                 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
491                 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
492                 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
493                 Template editProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/editProfileField.html"));
494                 Template deleteProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/deleteProfileField.html"));
495                 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
496                 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
497                 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
498                 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
499                 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
500                 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
501                 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
502                 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
503                 Template invalidTemplate = templateFactory.createTemplate(createReader("/templates/invalid.html"));
504                 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
505                 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
506
507                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
508                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
509                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
510                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
511                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
512                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
513                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
514                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
515                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
516                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
517                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
518                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
519                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
520                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
521                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
522                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
523                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
524                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
525                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
526                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
527                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
528                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
529                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
530                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
531                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
532                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
533                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
534                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
535                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
536                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
537                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
538                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
539                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
540                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
541                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
542                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
543                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
544                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
545                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
546                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
547                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
548                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
549                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
550                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
551                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
552                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
553                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
554                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
555                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
556                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
557                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
558                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
559                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
560                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
561                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
562                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
563                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
564                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
565
566                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
567                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
568                 for (PageToadlet toadlet : pageToadlets) {
569                         String menuName = toadlet.getMenuName();
570                         if (menuName != null) {
571                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
572                         } else {
573                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
574                         }
575                 }
576         }
577
578         /**
579          * Unregisters all toadlets.
580          */
581         private void unregisterToadlets() {
582                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
583                 for (PageToadlet pageToadlet : pageToadlets) {
584                         toadletContainer.unregister(pageToadlet);
585                 }
586                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
587         }
588
589         /**
590          * Creates a {@link Reader} from the {@link InputStream} for the resource
591          * with the given name.
592          *
593          * @param resourceName
594          *            The name of the resource
595          * @return A {@link Reader} for the resource
596          */
597         private Reader createReader(String resourceName) {
598                 try {
599                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
600                 } catch (UnsupportedEncodingException uee1) {
601                         return null;
602                 }
603         }
604
605         //
606         // CORELISTENER METHODS
607         //
608
609         /**
610          * {@inheritDoc}
611          */
612         @Override
613         public void rescuingSone(Sone sone) {
614                 rescuingSonesNotification.add(sone);
615                 notificationManager.addNotification(rescuingSonesNotification);
616         }
617
618         /**
619          * {@inheritDoc}
620          */
621         @Override
622         public void rescuedSone(Sone sone) {
623                 rescuingSonesNotification.remove(sone);
624                 sonesRescuedNotification.add(sone);
625                 notificationManager.addNotification(sonesRescuedNotification);
626         }
627
628         /**
629          * {@inheritDoc}
630          */
631         @Override
632         public void newSoneFound(Sone sone) {
633                 newSoneNotification.add(sone);
634                 if (!hasFirstStartNotification()) {
635                         notificationManager.addNotification(newSoneNotification);
636                 }
637         }
638
639         /**
640          * {@inheritDoc}
641          */
642         @Override
643         public void newPostFound(Post post) {
644                 newPostNotification.add(post);
645                 if (!hasFirstStartNotification()) {
646                         notificationManager.addNotification(newPostNotification);
647                 } else {
648                         getCore().markPostKnown(post);
649                 }
650         }
651
652         /**
653          * {@inheritDoc}
654          */
655         @Override
656         public void newReplyFound(Reply reply) {
657                 if (reply.getPost().getSone() == null) {
658                         return;
659                 }
660                 newReplyNotification.add(reply);
661                 if (!hasFirstStartNotification()) {
662                         notificationManager.addNotification(newReplyNotification);
663                 } else {
664                         getCore().markReplyKnown(reply);
665                 }
666         }
667
668         /**
669          * {@inheritDoc}
670          */
671         @Override
672         public void markSoneKnown(Sone sone) {
673                 newSoneNotification.remove(sone);
674         }
675
676         /**
677          * {@inheritDoc}
678          */
679         @Override
680         public void markPostKnown(Post post) {
681                 newPostNotification.remove(post);
682         }
683
684         /**
685          * {@inheritDoc}
686          */
687         @Override
688         public void markReplyKnown(Reply reply) {
689                 newReplyNotification.remove(reply);
690         }
691
692         /**
693          * {@inheritDoc}
694          */
695         @Override
696         public void postRemoved(Post post) {
697                 newPostNotification.remove(post);
698         }
699
700         /**
701          * {@inheritDoc}
702          */
703         @Override
704         public void replyRemoved(Reply reply) {
705                 newReplyNotification.remove(reply);
706         }
707
708         /**
709          * {@inheritDoc}
710          */
711         @Override
712         public void soneLocked(final Sone sone) {
713                 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
714
715                         @Override
716                         @SuppressWarnings("synthetic-access")
717                         public void run() {
718                                 lockedSonesNotification.add(sone);
719                                 lockedSonesTickerObjects.remove(sone);
720                                 notificationManager.addNotification(lockedSonesNotification);
721                         }
722                 }, "Sone Locked Notification");
723                 lockedSonesTickerObjects.put(sone, tickerObject);
724         }
725
726         /**
727          * {@inheritDoc}
728          */
729         @Override
730         public void soneUnlocked(Sone sone) {
731                 lockedSonesNotification.remove(sone);
732                 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
733         }
734
735         /**
736          * {@inheritDoc}
737          */
738         @Override
739         public void updateFound(Version version, long releaseTime) {
740                 newVersionNotification.set("version", version);
741                 newVersionNotification.set("releaseTime", releaseTime);
742                 notificationManager.addNotification(newVersionNotification);
743         }
744
745         /**
746          * Template provider implementation that uses
747          * {@link WebInterface#createReader(String)} to load templates for
748          * inclusion.
749          *
750          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
751          */
752         private class ClassPathTemplateProvider implements TemplateProvider {
753
754                 /** The template factory. */
755                 @SuppressWarnings("hiding")
756                 private final TemplateFactory templateFactory;
757
758                 /**
759                  * Creates a new template provider that locates templates on the
760                  * classpath.
761                  *
762                  * @param templateFactory
763                  *            The template factory to create the templates
764                  */
765                 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
766                         this.templateFactory = templateFactory;
767                 }
768
769                 /**
770                  * {@inheritDoc}
771                  */
772                 @Override
773                 @SuppressWarnings("synthetic-access")
774                 public Template getTemplate(String templateName) {
775                         Reader templateReader = createReader("/templates/" + templateName);
776                         if (templateReader == null) {
777                                 return null;
778                         }
779                         Template template = templateFactory.createTemplate(templateReader);
780                         try {
781                                 template.parse();
782                         } catch (TemplateException te1) {
783                                 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
784                         }
785                         return template;
786                 }
787
788         }
789
790 }