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.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;
33 import java.util.UUID;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
37 import net.pterodactylus.sone.core.Core;
38 import net.pterodactylus.sone.core.CoreListener;
39 import net.pterodactylus.sone.data.Post;
40 import net.pterodactylus.sone.data.Reply;
41 import net.pterodactylus.sone.data.Sone;
42 import net.pterodactylus.sone.freenet.L10nFilter;
43 import net.pterodactylus.sone.freenet.wot.Identity;
44 import net.pterodactylus.sone.freenet.wot.Trust;
45 import net.pterodactylus.sone.main.SonePlugin;
46 import net.pterodactylus.sone.notify.ListNotification;
47 import net.pterodactylus.sone.template.CollectionAccessor;
48 import net.pterodactylus.sone.template.CssClassNameFilter;
49 import net.pterodactylus.sone.template.GetPagePlugin;
50 import net.pterodactylus.sone.template.IdentityAccessor;
51 import net.pterodactylus.sone.template.JavascriptFilter;
52 import net.pterodactylus.sone.template.NotificationManagerAccessor;
53 import net.pterodactylus.sone.template.ParserFilter;
54 import net.pterodactylus.sone.template.PostAccessor;
55 import net.pterodactylus.sone.template.ReplyAccessor;
56 import net.pterodactylus.sone.template.RequestChangeFilter;
57 import net.pterodactylus.sone.template.SoneAccessor;
58 import net.pterodactylus.sone.template.SubstringFilter;
59 import net.pterodactylus.sone.template.TrustAccessor;
60 import net.pterodactylus.sone.template.UnknownDateFilter;
61 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
62 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
63 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
64 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
65 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
66 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
67 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
68 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
69 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
70 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
71 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
72 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
73 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
74 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
75 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
76 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
77 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
78 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
79 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
80 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
81 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
82 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
83 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
84 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
85 import net.pterodactylus.sone.web.page.PageToadlet;
86 import net.pterodactylus.sone.web.page.PageToadletFactory;
87 import net.pterodactylus.sone.web.page.StaticPage;
88 import net.pterodactylus.util.logging.Logging;
89 import net.pterodactylus.util.notify.Notification;
90 import net.pterodactylus.util.notify.NotificationManager;
91 import net.pterodactylus.util.notify.TemplateNotification;
92 import net.pterodactylus.util.template.DateFilter;
93 import net.pterodactylus.util.template.DefaultTemplateFactory;
94 import net.pterodactylus.util.template.MatchFilter;
95 import net.pterodactylus.util.template.PaginationPlugin;
96 import net.pterodactylus.util.template.ReflectionAccessor;
97 import net.pterodactylus.util.template.Template;
98 import net.pterodactylus.util.template.TemplateException;
99 import net.pterodactylus.util.template.TemplateFactory;
100 import net.pterodactylus.util.template.TemplateProvider;
101 import net.pterodactylus.util.template.XmlFilter;
102 import net.pterodactylus.util.thread.Ticker;
103 import net.pterodactylus.util.version.Version;
104 import freenet.clients.http.SessionManager;
105 import freenet.clients.http.SessionManager.Session;
106 import freenet.clients.http.ToadletContainer;
107 import freenet.clients.http.ToadletContext;
108 import freenet.l10n.BaseL10n;
111 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
112 * references to l10n helpers.
114 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
116 public class WebInterface implements CoreListener {
119 private static final Logger logger = Logging.getLogger(WebInterface.class);
121 /** The notification manager. */
122 private final NotificationManager notificationManager = new NotificationManager();
124 /** The Sone plugin. */
125 private final SonePlugin sonePlugin;
127 /** The registered toadlets. */
128 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
130 /** The form password. */
131 private final String formPassword;
133 /** The template factory. */
134 private DefaultTemplateFactory templateFactory;
136 /** The “new Sone” notification. */
137 private final ListNotification<Sone> newSoneNotification;
139 /** The “new post” notification. */
140 private final ListNotification<Post> newPostNotification;
142 /** The “new reply” notification. */
143 private final ListNotification<Reply> newReplyNotification;
145 /** The “rescuing Sone” notification. */
146 private final ListNotification<Sone> rescuingSonesNotification;
148 /** The “Sone rescued” notification. */
149 private final ListNotification<Sone> sonesRescuedNotification;
151 /** Sone locked notification ticker objects. */
152 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
154 /** The “Sone locked” notification. */
155 private final ListNotification<Sone> lockedSonesNotification;
157 /** The “new version” notification. */
158 private final TemplateNotification newVersionNotification;
161 * Creates a new web interface.
166 public WebInterface(SonePlugin sonePlugin) {
167 this.sonePlugin = sonePlugin;
168 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
170 templateFactory = new DefaultTemplateFactory();
171 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
172 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
173 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
174 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
175 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
176 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
177 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
178 templateFactory.addAccessor(Trust.class, new TrustAccessor());
179 templateFactory.addFilter("date", new DateFilter());
180 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
181 templateFactory.addFilter("substring", new SubstringFilter());
182 templateFactory.addFilter("xml", new XmlFilter());
183 templateFactory.addFilter("change", new RequestChangeFilter());
184 templateFactory.addFilter("match", new MatchFilter());
185 templateFactory.addFilter("css", new CssClassNameFilter());
186 templateFactory.addFilter("js", new JavascriptFilter());
187 templateFactory.addFilter("parse", new ParserFilter(templateFactory));
188 templateFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
189 templateFactory.addPlugin("getpage", new GetPagePlugin());
190 templateFactory.addPlugin("paginate", new PaginationPlugin());
191 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
192 templateFactory.addTemplateObject("formPassword", formPassword);
194 /* create notifications. */
195 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
196 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
198 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
199 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
201 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
202 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
204 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
205 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
207 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
208 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
210 Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
211 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
213 Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
214 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
222 * Returns the Sone core used by the Sone plugin.
224 * @return The Sone core
226 public Core getCore() {
227 return sonePlugin.core();
231 * Returns the current session, creating a new session if there is no
234 * @param toadletContenxt
235 * The toadlet context
236 * @return The current session, or {@code null} if there is no current
239 public Session getCurrentSession(ToadletContext toadletContenxt) {
240 return getCurrentSession(toadletContenxt, true);
244 * Returns the current session, creating a new session if there is no
245 * current session and {@code create} is {@code true}.
247 * @param toadletContenxt
248 * The toadlet context
250 * {@code true} to create a new session if there is no current
251 * session, {@code false} otherwise
252 * @return The current session, or {@code null} if there is no current
255 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
256 Session session = getSessionManager().useSession(toadletContenxt);
257 if (create && (session == null)) {
258 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
264 * Returns the currently logged in Sone.
266 * @param toadletContext
267 * The toadlet context
268 * @return The currently logged in Sone, or {@code null} if no Sone is
269 * currently logged in
271 public Sone getCurrentSone(ToadletContext toadletContext) {
272 return getCurrentSone(toadletContext, true);
276 * Returns the currently logged in Sone.
278 * @param toadletContext
279 * The toadlet context
281 * {@code true} to create a new session if no session exists,
282 * {@code false} to not create a new session
283 * @return The currently logged in Sone, or {@code null} if no Sone is
284 * currently logged in
286 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
287 return getCurrentSone(getCurrentSession(toadletContext, create));
291 * Returns the currently logged in Sone.
295 * @return The currently logged in Sone, or {@code null} if no Sone is
296 * currently logged in
298 public Sone getCurrentSone(Session session) {
299 if (session == null) {
302 String soneId = (String) session.getAttribute("Sone.CurrentSone");
303 if (soneId == null) {
306 return getCore().getLocalSone(soneId, false);
310 * Sets the currently logged in Sone.
312 * @param toadletContext
313 * The toadlet context
315 * The Sone to set as currently logged in
317 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
318 Session session = getCurrentSession(toadletContext);
320 session.removeAttribute("Sone.CurrentSone");
322 session.setAttribute("Sone.CurrentSone", sone.getId());
327 * Returns the notification manager.
329 * @return The notification manager
331 public NotificationManager getNotifications() {
332 return notificationManager;
336 * Returns the l10n helper of the node.
338 * @return The node’s l10n helper
340 public BaseL10n getL10n() {
341 return sonePlugin.l10n().getBase();
345 * Returns the session manager of the node.
347 * @return The node’s session manager
349 public SessionManager getSessionManager() {
350 return sonePlugin.pluginRespirator().getSessionManager("Sone");
354 * Returns the node’s form password.
356 * @return The form password
358 public String getFormPassword() {
363 * Returns the posts that have been announced as new in the
364 * {@link #newPostNotification}.
366 * @return The new posts
368 public Set<Post> getNewPosts() {
369 return new HashSet<Post>(newPostNotification.getElements());
373 * Returns the replies that have been announced as new in the
374 * {@link #newReplyNotification}.
376 * @return The new replies
378 public Set<Reply> getNewReplies() {
379 return new HashSet<Reply>(newReplyNotification.getElements());
383 * Sets whether the current start of the plugin is the first start. It is
384 * considered a first start if the configuration file does not exist.
387 * {@code true} if no configuration file existed when Sone was
388 * loaded, {@code false} otherwise
390 public void setFirstStart(boolean firstStart) {
392 Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
393 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
394 notificationManager.addNotification(firstStartNotification);
399 * Sets whether Sone was started with a fresh configuration file.
402 * {@code true} if Sone was started with a fresh configuration,
403 * {@code false} if the existing configuration could be read
405 public void setNewConfig(boolean newConfig) {
406 if (newConfig && !hasFirstStartNotification()) {
407 Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
408 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
409 notificationManager.addNotification(configNotReadNotification);
418 * Returns whether the first start notification is currently displayed.
420 * @return {@code true} if the first-start notification is currently
421 * displayed, {@code false} otherwise
423 private boolean hasFirstStartNotification() {
424 return notificationManager.getNotification("first-start-notification") != null;
432 * Starts the web interface and registers all toadlets.
434 public void start() {
437 /* notification templates. */
438 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
440 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
441 notificationManager.addNotification(startupNotification);
443 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
447 startupNotification.dismiss();
449 }, "Sone Startup Notification Remover");
451 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
452 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
453 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
456 @SuppressWarnings("synthetic-access")
458 if (getCore().getIdentityManager().isConnected()) {
459 wotMissingNotification.dismiss();
461 notificationManager.addNotification(wotMissingNotification);
463 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
466 }, "Sone WoT Connector Checker");
470 * Stops the web interface and unregisters all toadlets.
473 unregisterToadlets();
474 Ticker.getInstance().stop();
482 * Register all toadlets.
484 private void registerToadlets() {
485 Template emptyTemplate = templateFactory.createTemplate(new StringReader(""));
486 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
487 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
488 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
489 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
490 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
491 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
492 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
493 Template editProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/editProfileField.html"));
494 Template deleteProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/deleteProfileField.html"));
495 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
496 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
497 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
498 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
499 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
500 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
501 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
502 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
503 Template invalidTemplate = templateFactory.createTemplate(createReader("/templates/invalid.html"));
504 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
505 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
507 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
508 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
509 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
510 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
511 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
512 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
513 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
514 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
515 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
516 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
517 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
518 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
519 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
520 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
521 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
522 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
523 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
524 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
525 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
540 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
541 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
542 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
543 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
544 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
545 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
546 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
547 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
548 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
549 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
554 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
555 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
556 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
557 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
558 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
559 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
560 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
561 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
562 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
563 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
565 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
566 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
567 for (PageToadlet toadlet : pageToadlets) {
568 String menuName = toadlet.getMenuName();
569 if (menuName != null) {
570 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
572 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
578 * Unregisters all toadlets.
580 private void unregisterToadlets() {
581 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
582 for (PageToadlet pageToadlet : pageToadlets) {
583 toadletContainer.unregister(pageToadlet);
585 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
589 * Creates a {@link Reader} from the {@link InputStream} for the resource
590 * with the given name.
592 * @param resourceName
593 * The name of the resource
594 * @return A {@link Reader} for the resource
596 private Reader createReader(String resourceName) {
598 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
599 } catch (UnsupportedEncodingException uee1) {
605 // CORELISTENER METHODS
612 public void rescuingSone(Sone sone) {
613 rescuingSonesNotification.add(sone);
614 notificationManager.addNotification(rescuingSonesNotification);
621 public void rescuedSone(Sone sone) {
622 rescuingSonesNotification.remove(sone);
623 sonesRescuedNotification.add(sone);
624 notificationManager.addNotification(sonesRescuedNotification);
631 public void newSoneFound(Sone sone) {
632 newSoneNotification.add(sone);
633 if (!hasFirstStartNotification()) {
634 notificationManager.addNotification(newSoneNotification);
642 public void newPostFound(Post post) {
643 newPostNotification.add(post);
644 if (!hasFirstStartNotification()) {
645 notificationManager.addNotification(newPostNotification);
647 getCore().markPostKnown(post);
655 public void newReplyFound(Reply reply) {
656 if (reply.getPost().getSone() == null) {
659 newReplyNotification.add(reply);
660 if (!hasFirstStartNotification()) {
661 notificationManager.addNotification(newReplyNotification);
663 getCore().markReplyKnown(reply);
671 public void markSoneKnown(Sone sone) {
672 newSoneNotification.remove(sone);
679 public void markPostKnown(Post post) {
680 newPostNotification.remove(post);
687 public void markReplyKnown(Reply reply) {
688 newReplyNotification.remove(reply);
695 public void postRemoved(Post post) {
696 newPostNotification.remove(post);
703 public void replyRemoved(Reply reply) {
704 newReplyNotification.remove(reply);
711 public void soneLocked(final Sone sone) {
712 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
715 @SuppressWarnings("synthetic-access")
717 lockedSonesNotification.add(sone);
718 lockedSonesTickerObjects.remove(sone);
719 notificationManager.addNotification(lockedSonesNotification);
721 }, "Sone Locked Notification");
722 lockedSonesTickerObjects.put(sone, tickerObject);
729 public void soneUnlocked(Sone sone) {
730 lockedSonesNotification.remove(sone);
731 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
738 public void updateFound(Version version, long releaseTime) {
739 newVersionNotification.set("version", version);
740 newVersionNotification.set("releaseTime", releaseTime);
741 notificationManager.addNotification(newVersionNotification);
745 * Template provider implementation that uses
746 * {@link WebInterface#createReader(String)} to load templates for
749 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
751 private class ClassPathTemplateProvider implements TemplateProvider {
753 /** The template factory. */
754 @SuppressWarnings("hiding")
755 private final TemplateFactory templateFactory;
758 * Creates a new template provider that locates templates on the
761 * @param templateFactory
762 * The template factory to create the templates
764 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
765 this.templateFactory = templateFactory;
772 @SuppressWarnings("synthetic-access")
773 public Template getTemplate(String templateName) {
774 Reader templateReader = createReader("/templates/" + templateName);
775 if (templateReader == null) {
778 Template template = templateFactory.createTemplate(templateReader);
781 } catch (TemplateException te1) {
782 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);