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