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