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