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