2 * FreenetSone - WebInterface.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
32 import java.util.UUID;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
36 import net.pterodactylus.sone.core.Core;
37 import net.pterodactylus.sone.core.CoreListener;
38 import net.pterodactylus.sone.data.Post;
39 import net.pterodactylus.sone.data.Reply;
40 import net.pterodactylus.sone.data.Sone;
41 import net.pterodactylus.sone.freenet.L10nFilter;
42 import net.pterodactylus.sone.freenet.wot.Identity;
43 import net.pterodactylus.sone.main.SonePlugin;
44 import net.pterodactylus.sone.notify.ListNotification;
45 import net.pterodactylus.sone.template.CollectionAccessor;
46 import net.pterodactylus.sone.template.CssClassNameFilter;
47 import net.pterodactylus.sone.template.GetPagePlugin;
48 import net.pterodactylus.sone.template.IdentityAccessor;
49 import net.pterodactylus.sone.template.NotificationManagerAccessor;
50 import net.pterodactylus.sone.template.PostAccessor;
51 import net.pterodactylus.sone.template.ReplyAccessor;
52 import net.pterodactylus.sone.template.RequestChangeFilter;
53 import net.pterodactylus.sone.template.SoneAccessor;
54 import net.pterodactylus.sone.template.SubstringFilter;
55 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
56 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
57 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
58 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
59 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
60 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
61 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
62 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
63 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
64 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
65 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
66 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
67 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
68 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
69 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
70 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
71 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
72 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
73 import net.pterodactylus.sone.web.page.PageToadlet;
74 import net.pterodactylus.sone.web.page.PageToadletFactory;
75 import net.pterodactylus.sone.web.page.StaticPage;
76 import net.pterodactylus.util.logging.Logging;
77 import net.pterodactylus.util.notify.Notification;
78 import net.pterodactylus.util.notify.NotificationManager;
79 import net.pterodactylus.util.notify.TemplateNotification;
80 import net.pterodactylus.util.template.DateFilter;
81 import net.pterodactylus.util.template.DefaultTemplateFactory;
82 import net.pterodactylus.util.template.MatchFilter;
83 import net.pterodactylus.util.template.PaginationPlugin;
84 import net.pterodactylus.util.template.ReflectionAccessor;
85 import net.pterodactylus.util.template.Template;
86 import net.pterodactylus.util.template.TemplateException;
87 import net.pterodactylus.util.template.TemplateFactory;
88 import net.pterodactylus.util.template.TemplateProvider;
89 import net.pterodactylus.util.template.XmlFilter;
90 import net.pterodactylus.util.thread.Ticker;
91 import net.pterodactylus.util.version.Version;
92 import freenet.clients.http.SessionManager;
93 import freenet.clients.http.SessionManager.Session;
94 import freenet.clients.http.ToadletContainer;
95 import freenet.clients.http.ToadletContext;
96 import freenet.l10n.BaseL10n;
99 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
100 * references to l10n helpers.
102 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
104 public class WebInterface implements CoreListener {
107 private static final Logger logger = Logging.getLogger(WebInterface.class);
109 /** The notification manager. */
110 private final NotificationManager notificationManager = new NotificationManager();
112 /** The Sone plugin. */
113 private final SonePlugin sonePlugin;
115 /** The registered toadlets. */
116 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
118 /** The form password. */
119 private final String formPassword;
121 /** The template factory. */
122 private DefaultTemplateFactory templateFactory;
124 /** The “new Sone” notification. */
125 private final ListNotification<Sone> newSoneNotification;
127 /** The “new post” notification. */
128 private final ListNotification<Post> newPostNotification;
130 /** The “new reply” notification. */
131 private final ListNotification<Reply> newReplyNotification;
133 /** The “rescuing Sone” notification. */
134 private final ListNotification<Sone> rescuingSonesNotification;
136 /** The “Sone rescued” notification. */
137 private final ListNotification<Sone> sonesRescuedNotification;
139 /** Sone locked notification ticker objects. */
140 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
142 /** The “Sone locked” notification. */
143 private final ListNotification<Sone> lockedSonesNotification;
145 /** The “new version” notification. */
146 private final TemplateNotification newVersionNotification;
149 * Creates a new web interface.
154 public WebInterface(SonePlugin sonePlugin) {
155 this.sonePlugin = sonePlugin;
156 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
158 templateFactory = new DefaultTemplateFactory();
159 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
160 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
161 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
162 templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
163 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
164 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
165 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
166 templateFactory.addFilter("date", new DateFilter());
167 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
168 templateFactory.addFilter("substring", new SubstringFilter());
169 templateFactory.addFilter("xml", new XmlFilter());
170 templateFactory.addFilter("change", new RequestChangeFilter());
171 templateFactory.addFilter("match", new MatchFilter());
172 templateFactory.addFilter("css", new CssClassNameFilter());
173 templateFactory.addPlugin("getpage", new GetPagePlugin());
174 templateFactory.addPlugin("paginate", new PaginationPlugin());
175 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
176 templateFactory.addTemplateObject("formPassword", formPassword);
178 /* create notifications. */
179 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
180 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
182 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
183 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
185 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
186 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
188 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
189 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
191 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
192 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
194 Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
195 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
197 Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
198 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
206 * Returns the Sone core used by the Sone plugin.
208 * @return The Sone core
210 public Core getCore() {
211 return sonePlugin.core();
215 * Returns the current session, creating a new session if there is no
218 * @param toadletContenxt
219 * The toadlet context
220 * @return The current session, or {@code null} if there is no current
223 public Session getCurrentSession(ToadletContext toadletContenxt) {
224 return getCurrentSession(toadletContenxt, true);
228 * Returns the current session, creating a new session if there is no
229 * current session and {@code create} is {@code true}.
231 * @param toadletContenxt
232 * The toadlet context
234 * {@code true} to create a new session if there is no current
235 * session, {@code false} otherwise
236 * @return The current session, or {@code null} if there is no current
239 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
240 Session session = getSessionManager().useSession(toadletContenxt);
241 if (create && (session == null)) {
242 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
248 * Returns the currently logged in Sone.
250 * @param toadletContext
251 * The toadlet context
252 * @return The currently logged in Sone, or {@code null} if no Sone is
253 * currently logged in
255 public Sone getCurrentSone(ToadletContext toadletContext) {
256 return getCurrentSone(toadletContext, true);
260 * Returns the currently logged in Sone.
262 * @param toadletContext
263 * The toadlet context
265 * {@code true} to create a new session if no session exists,
266 * {@code false} to not create a new session
267 * @return The currently logged in Sone, or {@code null} if no Sone is
268 * currently logged in
270 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
271 return getCurrentSone(getCurrentSession(toadletContext, create));
275 * Returns the currently logged in Sone.
279 * @return The currently logged in Sone, or {@code null} if no Sone is
280 * currently logged in
282 public Sone getCurrentSone(Session session) {
283 if (session == null) {
286 String soneId = (String) session.getAttribute("Sone.CurrentSone");
287 if (soneId == null) {
290 return getCore().getLocalSone(soneId, false);
294 * Sets the currently logged in Sone.
296 * @param toadletContext
297 * The toadlet context
299 * The Sone to set as currently logged in
301 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
302 Session session = getCurrentSession(toadletContext);
304 session.removeAttribute("Sone.CurrentSone");
306 session.setAttribute("Sone.CurrentSone", sone.getId());
311 * Returns the notification manager.
313 * @return The notification manager
315 public NotificationManager getNotifications() {
316 return notificationManager;
320 * Returns the l10n helper of the node.
322 * @return The node’s l10n helper
324 public BaseL10n getL10n() {
325 return sonePlugin.l10n().getBase();
329 * Returns the session manager of the node.
331 * @return The node’s session manager
333 public SessionManager getSessionManager() {
334 return sonePlugin.pluginRespirator().getSessionManager("Sone");
338 * Returns the node’s form password.
340 * @return The form password
342 public String getFormPassword() {
347 * Returns the posts that have been announced as new in the
348 * {@link #newPostNotification}.
350 * @return The new posts
352 public Set<Post> getNewPosts() {
353 return new HashSet<Post>(newPostNotification.getElements());
357 * Returns the replies that have been announced as new in the
358 * {@link #newReplyNotification}.
360 * @return The new replies
362 public Set<Reply> getNewReplies() {
363 return new HashSet<Reply>(newReplyNotification.getElements());
367 * Sets whether the current start of the plugin is the first start. It is
368 * considered a first start if the configuration file does not exist.
371 * {@code true} if no configuration file existed when Sone was
372 * loaded, {@code false} otherwise
374 public void setFirstStart(boolean firstStart) {
376 Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
377 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
378 notificationManager.addNotification(firstStartNotification);
383 * Sets whether Sone was started with a fresh configuration file.
386 * {@code true} if Sone was started with a fresh configuration,
387 * {@code false} if the existing configuration could be read
389 public void setNewConfig(boolean newConfig) {
390 if (newConfig && !hasFirstStartNotification()) {
391 Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
392 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
393 notificationManager.addNotification(configNotReadNotification);
402 * Returns whether the first start notification is currently displayed.
404 * @return {@code true} if the first-start notification is currently
405 * displayed, {@code false} otherwise
407 private boolean hasFirstStartNotification() {
408 return notificationManager.getNotification("first-start-notification") != null;
416 * Starts the web interface and registers all toadlets.
418 public void start() {
421 /* notification templates. */
422 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
424 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
425 notificationManager.addNotification(startupNotification);
427 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
431 startupNotification.dismiss();
433 }, "Sone Startup Notification Remover");
435 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
436 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
437 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
440 @SuppressWarnings("synthetic-access")
442 if (getCore().getIdentityManager().isConnected()) {
443 wotMissingNotification.dismiss();
445 notificationManager.addNotification(wotMissingNotification);
447 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
450 }, "Sone WoT Connector Checker");
454 * Stops the web interface and unregisters all toadlets.
457 unregisterToadlets();
458 Ticker.getInstance().stop();
466 * Register all toadlets.
468 private void registerToadlets() {
469 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
470 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
471 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
472 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
473 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
474 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
475 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
476 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
477 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
478 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
479 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
480 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
481 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
482 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
483 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
484 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
485 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
486 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
487 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
488 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
489 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
490 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
491 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
492 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
493 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
495 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
496 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
497 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
498 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
499 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
500 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
501 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
502 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
503 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
504 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
505 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
506 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
507 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
508 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
509 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
510 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
511 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
512 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
513 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
514 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
515 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
516 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
517 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
518 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
519 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
520 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
521 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
522 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
523 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
524 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
525 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
541 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
542 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
543 for (PageToadlet toadlet : pageToadlets) {
544 String menuName = toadlet.getMenuName();
545 if (menuName != null) {
546 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
548 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
554 * Unregisters all toadlets.
556 private void unregisterToadlets() {
557 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
558 for (PageToadlet pageToadlet : pageToadlets) {
559 toadletContainer.unregister(pageToadlet);
561 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
565 * Creates a {@link Reader} from the {@link InputStream} for the resource
566 * with the given name.
568 * @param resourceName
569 * The name of the resource
570 * @return A {@link Reader} for the resource
572 private Reader createReader(String resourceName) {
574 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
575 } catch (UnsupportedEncodingException uee1) {
581 // CORELISTENER METHODS
588 public void rescuingSone(Sone sone) {
589 rescuingSonesNotification.add(sone);
590 notificationManager.addNotification(rescuingSonesNotification);
597 public void rescuedSone(Sone sone) {
598 rescuingSonesNotification.remove(sone);
599 sonesRescuedNotification.add(sone);
600 notificationManager.addNotification(sonesRescuedNotification);
607 public void newSoneFound(Sone sone) {
608 newSoneNotification.add(sone);
609 if (!hasFirstStartNotification()) {
610 notificationManager.addNotification(newSoneNotification);
618 public void newPostFound(Post post) {
619 newPostNotification.add(post);
620 if (!hasFirstStartNotification()) {
621 notificationManager.addNotification(newPostNotification);
623 getCore().markPostKnown(post);
631 public void newReplyFound(Reply reply) {
632 if (reply.getPost().getSone() == null) {
635 newReplyNotification.add(reply);
636 if (!hasFirstStartNotification()) {
637 notificationManager.addNotification(newReplyNotification);
639 getCore().markReplyKnown(reply);
647 public void markSoneKnown(Sone sone) {
648 newSoneNotification.remove(sone);
655 public void markPostKnown(Post post) {
656 newPostNotification.remove(post);
663 public void markReplyKnown(Reply reply) {
664 newReplyNotification.remove(reply);
671 public void postRemoved(Post post) {
672 newPostNotification.remove(post);
679 public void replyRemoved(Reply reply) {
680 newReplyNotification.remove(reply);
687 public void soneLocked(final Sone sone) {
688 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
691 @SuppressWarnings("synthetic-access")
693 lockedSonesNotification.add(sone);
694 lockedSonesTickerObjects.remove(sone);
695 notificationManager.addNotification(lockedSonesNotification);
697 }, "Sone Locked Notification");
698 lockedSonesTickerObjects.put(sone, tickerObject);
705 public void soneUnlocked(Sone sone) {
706 lockedSonesNotification.remove(sone);
707 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
714 public void updateFound(Version version, long releaseTime) {
715 newVersionNotification.set("version", version);
716 newVersionNotification.set("releaseTime", releaseTime);
717 notificationManager.addNotification(newVersionNotification);
721 * Template provider implementation that uses
722 * {@link WebInterface#createReader(String)} to load templates for
725 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
727 private class ClassPathTemplateProvider implements TemplateProvider {
729 /** The template factory. */
730 @SuppressWarnings("hiding")
731 private final TemplateFactory templateFactory;
734 * Creates a new template provider that locates templates on the
737 * @param templateFactory
738 * The template factory to create the templates
740 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
741 this.templateFactory = templateFactory;
748 @SuppressWarnings("synthetic-access")
749 public Template getTemplate(String templateName) {
750 Reader templateReader = createReader("/templates/" + templateName);
751 if (templateReader == null) {
754 Template template = templateFactory.createTemplate(templateReader);
757 } catch (TemplateException te1) {
758 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);