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