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.Profile;
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.main.SonePlugin;
45 import net.pterodactylus.sone.notify.ListNotification;
46 import net.pterodactylus.sone.template.CollectionAccessor;
47 import net.pterodactylus.sone.template.CssClassNameFilter;
48 import net.pterodactylus.sone.template.GetPagePlugin;
49 import net.pterodactylus.sone.template.IdentityAccessor;
50 import net.pterodactylus.sone.template.NotificationManagerAccessor;
51 import net.pterodactylus.sone.template.PostAccessor;
52 import net.pterodactylus.sone.template.ProfileAccessor;
53 import net.pterodactylus.sone.template.ReplyAccessor;
54 import net.pterodactylus.sone.template.RequestChangeFilter;
55 import net.pterodactylus.sone.template.SoneAccessor;
56 import net.pterodactylus.sone.template.SubstringFilter;
57 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
58 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
59 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
60 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
61 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
62 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
63 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
64 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
65 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
66 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
67 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
68 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
69 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
70 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
71 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
72 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
73 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
74 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
75 import net.pterodactylus.sone.web.page.PageToadlet;
76 import net.pterodactylus.sone.web.page.PageToadletFactory;
77 import net.pterodactylus.sone.web.page.StaticPage;
78 import net.pterodactylus.util.logging.Logging;
79 import net.pterodactylus.util.notify.Notification;
80 import net.pterodactylus.util.notify.NotificationManager;
81 import net.pterodactylus.util.notify.TemplateNotification;
82 import net.pterodactylus.util.template.DateFilter;
83 import net.pterodactylus.util.template.DefaultTemplateFactory;
84 import net.pterodactylus.util.template.MatchFilter;
85 import net.pterodactylus.util.template.PaginationPlugin;
86 import net.pterodactylus.util.template.ReflectionAccessor;
87 import net.pterodactylus.util.template.Template;
88 import net.pterodactylus.util.template.TemplateException;
89 import net.pterodactylus.util.template.TemplateFactory;
90 import net.pterodactylus.util.template.TemplateProvider;
91 import net.pterodactylus.util.template.XmlFilter;
92 import net.pterodactylus.util.thread.Ticker;
93 import net.pterodactylus.util.version.Version;
94 import freenet.clients.http.SessionManager;
95 import freenet.clients.http.SessionManager.Session;
96 import freenet.clients.http.ToadletContainer;
97 import freenet.clients.http.ToadletContext;
98 import freenet.l10n.BaseL10n;
101 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
102 * references to l10n helpers.
104 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
106 public class WebInterface implements CoreListener {
109 private static final Logger logger = Logging.getLogger(WebInterface.class);
111 /** The notification manager. */
112 private final NotificationManager notificationManager = new NotificationManager();
114 /** The Sone plugin. */
115 private final SonePlugin sonePlugin;
117 /** The registered toadlets. */
118 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
120 /** The form password. */
121 private final String formPassword;
123 /** The template factory. */
124 private DefaultTemplateFactory templateFactory;
126 /** The “new Sone” notification. */
127 private final ListNotification<Sone> newSoneNotification;
129 /** The “new post” notification. */
130 private final ListNotification<Post> newPostNotification;
132 /** The “new reply” notification. */
133 private final ListNotification<Reply> newReplyNotification;
135 /** The “rescuing Sone” notification. */
136 private final ListNotification<Sone> rescuingSonesNotification;
138 /** The “Sone rescued” notification. */
139 private final ListNotification<Sone> sonesRescuedNotification;
141 /** Sone locked notification ticker objects. */
142 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
144 /** The “Sone locked” notification. */
145 private final ListNotification<Sone> lockedSonesNotification;
147 /** The “new version” notification. */
148 private final TemplateNotification newVersionNotification;
151 * Creates a new web interface.
156 public WebInterface(SonePlugin sonePlugin) {
157 this.sonePlugin = sonePlugin;
158 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
160 templateFactory = new DefaultTemplateFactory();
161 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
162 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
163 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
164 templateFactory.addAccessor(Profile.class, new ProfileAccessor());
165 templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
166 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
167 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
168 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
169 templateFactory.addFilter("date", new DateFilter());
170 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
171 templateFactory.addFilter("substring", new SubstringFilter());
172 templateFactory.addFilter("xml", new XmlFilter());
173 templateFactory.addFilter("change", new RequestChangeFilter());
174 templateFactory.addFilter("match", new MatchFilter());
175 templateFactory.addFilter("css", new CssClassNameFilter());
176 templateFactory.addPlugin("getpage", new GetPagePlugin());
177 templateFactory.addPlugin("paginate", new PaginationPlugin());
178 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
179 templateFactory.addTemplateObject("formPassword", formPassword);
181 /* create notifications. */
182 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
183 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
185 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
186 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
188 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
189 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
191 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
192 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
194 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
195 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
197 Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
198 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
200 Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
201 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
209 * Returns the Sone core used by the Sone plugin.
211 * @return The Sone core
213 public Core getCore() {
214 return sonePlugin.core();
218 * Returns the current session, creating a new session if there is no
221 * @param toadletContenxt
222 * The toadlet context
223 * @return The current session, or {@code null} if there is no current
226 public Session getCurrentSession(ToadletContext toadletContenxt) {
227 return getCurrentSession(toadletContenxt, true);
231 * Returns the current session, creating a new session if there is no
232 * current session and {@code create} is {@code true}.
234 * @param toadletContenxt
235 * The toadlet context
237 * {@code true} to create a new session if there is no current
238 * session, {@code false} otherwise
239 * @return The current session, or {@code null} if there is no current
242 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
243 Session session = getSessionManager().useSession(toadletContenxt);
244 if (create && (session == null)) {
245 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
251 * Returns the currently logged in Sone.
253 * @param toadletContext
254 * The toadlet context
255 * @return The currently logged in Sone, or {@code null} if no Sone is
256 * currently logged in
258 public Sone getCurrentSone(ToadletContext toadletContext) {
259 return getCurrentSone(toadletContext, true);
263 * Returns the currently logged in Sone.
265 * @param toadletContext
266 * The toadlet context
268 * {@code true} to create a new session if no session exists,
269 * {@code false} to not create a new session
270 * @return The currently logged in Sone, or {@code null} if no Sone is
271 * currently logged in
273 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
274 return getCurrentSone(getCurrentSession(toadletContext, create));
278 * Returns the currently logged in Sone.
282 * @return The currently logged in Sone, or {@code null} if no Sone is
283 * currently logged in
285 public Sone getCurrentSone(Session session) {
286 if (session == null) {
289 String soneId = (String) session.getAttribute("Sone.CurrentSone");
290 if (soneId == null) {
293 return getCore().getLocalSone(soneId, false);
297 * Sets the currently logged in Sone.
299 * @param toadletContext
300 * The toadlet context
302 * The Sone to set as currently logged in
304 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
305 Session session = getCurrentSession(toadletContext);
307 session.removeAttribute("Sone.CurrentSone");
309 session.setAttribute("Sone.CurrentSone", sone.getId());
314 * Returns the notification manager.
316 * @return The notification manager
318 public NotificationManager getNotifications() {
319 return notificationManager;
323 * Returns the l10n helper of the node.
325 * @return The node’s l10n helper
327 public BaseL10n getL10n() {
328 return sonePlugin.l10n().getBase();
332 * Returns the session manager of the node.
334 * @return The node’s session manager
336 public SessionManager getSessionManager() {
337 return sonePlugin.pluginRespirator().getSessionManager("Sone");
341 * Returns the node’s form password.
343 * @return The form password
345 public String getFormPassword() {
350 * Returns the posts that have been announced as new in the
351 * {@link #newPostNotification}.
353 * @return The new posts
355 public Set<Post> getNewPosts() {
356 return new HashSet<Post>(newPostNotification.getElements());
360 * Returns the replies that have been announced as new in the
361 * {@link #newReplyNotification}.
363 * @return The new replies
365 public Set<Reply> getNewReplies() {
366 return new HashSet<Reply>(newReplyNotification.getElements());
370 * Sets whether the current start of the plugin is the first start. It is
371 * considered a first start if the configuration file does not exist.
374 * {@code true} if no configuration file existed when Sone was
375 * loaded, {@code false} otherwise
377 public void setFirstStart(boolean firstStart) {
379 Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
380 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
381 notificationManager.addNotification(firstStartNotification);
386 * Sets whether Sone was started with a fresh configuration file.
389 * {@code true} if Sone was started with a fresh configuration,
390 * {@code false} if the existing configuration could be read
392 public void setNewConfig(boolean newConfig) {
393 if (newConfig && !hasFirstStartNotification()) {
394 Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
395 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
396 notificationManager.addNotification(configNotReadNotification);
405 * Returns whether the first start notification is currently displayed.
407 * @return {@code true} if the first-start notification is currently
408 * displayed, {@code false} otherwise
410 private boolean hasFirstStartNotification() {
411 return notificationManager.getNotification("first-start-notification") != null;
419 * Starts the web interface and registers all toadlets.
421 public void start() {
424 /* notification templates. */
425 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
427 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
428 notificationManager.addNotification(startupNotification);
430 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
434 startupNotification.dismiss();
436 }, "Sone Startup Notification Remover");
438 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
439 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
440 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
443 @SuppressWarnings("synthetic-access")
445 if (getCore().getIdentityManager().isConnected()) {
446 wotMissingNotification.dismiss();
448 notificationManager.addNotification(wotMissingNotification);
450 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
453 }, "Sone WoT Connector Checker");
457 * Stops the web interface and unregisters all toadlets.
460 unregisterToadlets();
461 Ticker.getInstance().stop();
469 * Register all toadlets.
471 private void registerToadlets() {
472 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
473 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
474 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
475 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
476 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
477 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
478 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
479 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
480 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
481 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
482 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
483 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
484 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
485 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
486 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
487 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
488 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
489 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
490 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
491 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
492 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
493 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
494 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
495 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
496 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
498 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
499 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
500 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
501 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
502 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
503 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
504 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
505 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
506 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
507 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
508 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
509 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
510 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
511 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
512 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
513 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
514 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
515 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
516 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
517 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
518 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
519 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
520 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
521 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
522 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
523 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
524 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
525 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
540 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
541 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
542 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
544 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
545 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
546 for (PageToadlet toadlet : pageToadlets) {
547 String menuName = toadlet.getMenuName();
548 if (menuName != null) {
549 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
551 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
557 * Unregisters all toadlets.
559 private void unregisterToadlets() {
560 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
561 for (PageToadlet pageToadlet : pageToadlets) {
562 toadletContainer.unregister(pageToadlet);
564 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
568 * Creates a {@link Reader} from the {@link InputStream} for the resource
569 * with the given name.
571 * @param resourceName
572 * The name of the resource
573 * @return A {@link Reader} for the resource
575 private Reader createReader(String resourceName) {
577 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
578 } catch (UnsupportedEncodingException uee1) {
584 // CORELISTENER METHODS
591 public void rescuingSone(Sone sone) {
592 rescuingSonesNotification.add(sone);
593 notificationManager.addNotification(rescuingSonesNotification);
600 public void rescuedSone(Sone sone) {
601 rescuingSonesNotification.remove(sone);
602 sonesRescuedNotification.add(sone);
603 notificationManager.addNotification(sonesRescuedNotification);
610 public void newSoneFound(Sone sone) {
611 newSoneNotification.add(sone);
612 if (!hasFirstStartNotification()) {
613 notificationManager.addNotification(newSoneNotification);
621 public void newPostFound(Post post) {
622 newPostNotification.add(post);
623 if (!hasFirstStartNotification()) {
624 notificationManager.addNotification(newPostNotification);
626 getCore().markPostKnown(post);
634 public void newReplyFound(Reply reply) {
635 if (reply.getPost().getSone() == null) {
638 newReplyNotification.add(reply);
639 if (!hasFirstStartNotification()) {
640 notificationManager.addNotification(newReplyNotification);
642 getCore().markReplyKnown(reply);
650 public void markSoneKnown(Sone sone) {
651 newSoneNotification.remove(sone);
658 public void markPostKnown(Post post) {
659 newPostNotification.remove(post);
666 public void markReplyKnown(Reply reply) {
667 newReplyNotification.remove(reply);
674 public void postRemoved(Post post) {
675 newPostNotification.remove(post);
682 public void replyRemoved(Reply reply) {
683 newReplyNotification.remove(reply);
690 public void soneLocked(final Sone sone) {
691 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
694 @SuppressWarnings("synthetic-access")
696 lockedSonesNotification.add(sone);
697 lockedSonesTickerObjects.remove(sone);
698 notificationManager.addNotification(lockedSonesNotification);
700 }, "Sone Locked Notification");
701 lockedSonesTickerObjects.put(sone, tickerObject);
708 public void soneUnlocked(Sone sone) {
709 lockedSonesNotification.remove(sone);
710 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
717 public void updateFound(Version version, long releaseTime) {
718 newVersionNotification.set("version", version);
719 newVersionNotification.set("releaseTime", releaseTime);
720 notificationManager.addNotification(newVersionNotification);
724 * Template provider implementation that uses
725 * {@link WebInterface#createReader(String)} to load templates for
728 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
730 private class ClassPathTemplateProvider implements TemplateProvider {
732 /** The template factory. */
733 @SuppressWarnings("hiding")
734 private final TemplateFactory templateFactory;
737 * Creates a new template provider that locates templates on the
740 * @param templateFactory
741 * The template factory to create the templates
743 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
744 this.templateFactory = templateFactory;
751 @SuppressWarnings("synthetic-access")
752 public Template getTemplate(String templateName) {
753 Reader templateReader = createReader("/templates/" + templateName);
754 if (templateReader == null) {
757 Template template = templateFactory.createTemplate(templateReader);
760 } catch (TemplateException te1) {
761 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);