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