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