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