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.MarkAsKnownAjaxPage;
78 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
79 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
80 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
81 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
82 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
83 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
84 import net.pterodactylus.sone.web.page.PageToadlet;
85 import net.pterodactylus.sone.web.page.PageToadletFactory;
86 import net.pterodactylus.sone.web.page.StaticPage;
87 import net.pterodactylus.util.logging.Logging;
88 import net.pterodactylus.util.notify.Notification;
89 import net.pterodactylus.util.notify.NotificationManager;
90 import net.pterodactylus.util.notify.TemplateNotification;
91 import net.pterodactylus.util.template.DateFilter;
92 import net.pterodactylus.util.template.HtmlFilter;
93 import net.pterodactylus.util.template.MatchFilter;
94 import net.pterodactylus.util.template.PaginationPlugin;
95 import net.pterodactylus.util.template.Provider;
96 import net.pterodactylus.util.template.ReflectionAccessor;
97 import net.pterodactylus.util.template.ReplaceFilter;
98 import net.pterodactylus.util.template.StoreFilter;
99 import net.pterodactylus.util.template.Template;
100 import net.pterodactylus.util.template.TemplateContext;
101 import net.pterodactylus.util.template.TemplateContextFactory;
102 import net.pterodactylus.util.template.TemplateException;
103 import net.pterodactylus.util.template.TemplateParser;
104 import net.pterodactylus.util.template.XmlFilter;
105 import net.pterodactylus.util.thread.Ticker;
106 import net.pterodactylus.util.version.Version;
107 import freenet.clients.http.SessionManager;
108 import freenet.clients.http.SessionManager.Session;
109 import freenet.clients.http.ToadletContainer;
110 import freenet.clients.http.ToadletContext;
111 import freenet.l10n.BaseL10n;
114 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
115 * references to l10n helpers.
117 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
119 public class WebInterface implements CoreListener {
122 private static final Logger logger = Logging.getLogger(WebInterface.class);
124 /** The notification manager. */
125 private final NotificationManager notificationManager = new NotificationManager();
127 /** The Sone plugin. */
128 private final SonePlugin sonePlugin;
130 /** The registered toadlets. */
131 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
133 /** The form password. */
134 private final String formPassword;
136 /** The template context factory. */
137 private final TemplateContextFactory templateContextFactory;
139 /** The “new Sone” notification. */
140 private final ListNotification<Sone> newSoneNotification;
142 /** The “new post” notification. */
143 private final ListNotification<Post> newPostNotification;
145 /** The “new reply” notification. */
146 private final ListNotification<Reply> newReplyNotification;
148 /** The “rescuing Sone” notification. */
149 private final ListNotification<Sone> rescuingSonesNotification;
151 /** The “Sone rescued” notification. */
152 private final ListNotification<Sone> sonesRescuedNotification;
154 /** Sone locked notification ticker objects. */
155 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
157 /** The “Sone locked” notification. */
158 private final ListNotification<Sone> lockedSonesNotification;
160 /** The “new version” notification. */
161 private final TemplateNotification newVersionNotification;
164 * Creates a new web interface.
169 @SuppressWarnings("synthetic-access")
170 public WebInterface(SonePlugin sonePlugin) {
171 this.sonePlugin = sonePlugin;
172 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
174 templateContextFactory = new TemplateContextFactory();
175 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
176 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
177 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
178 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
179 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
180 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
181 templateContextFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
182 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
183 templateContextFactory.addFilter("date", new DateFilter());
184 templateContextFactory.addFilter("html", new HtmlFilter());
185 templateContextFactory.addFilter("replace", new ReplaceFilter());
186 templateContextFactory.addFilter("store", new StoreFilter());
187 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
188 templateContextFactory.addFilter("substring", new SubstringFilter());
189 templateContextFactory.addFilter("xml", new XmlFilter());
190 templateContextFactory.addFilter("change", new RequestChangeFilter());
191 templateContextFactory.addFilter("match", new MatchFilter());
192 templateContextFactory.addFilter("css", new CssClassNameFilter());
193 templateContextFactory.addFilter("js", new JavascriptFilter());
194 templateContextFactory.addFilter("parse", new ParserFilter(templateContextFactory));
195 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
196 templateContextFactory.addPlugin("getpage", new GetPagePlugin());
197 templateContextFactory.addPlugin("paginate", new PaginationPlugin());
198 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
199 templateContextFactory.addProvider(new ClassPathTemplateProvider());
200 templateContextFactory.addTemplateObject("formPassword", formPassword);
202 /* create notifications. */
203 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
204 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
206 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
207 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
209 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
210 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
212 Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html"));
213 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
215 Template sonesRescuedTemplate = TemplateParser.parse(createReader("/templates/notify/sonesRescuedNotification.html"));
216 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
218 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
219 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
221 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
222 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
230 * Returns the Sone core used by the Sone plugin.
232 * @return The Sone core
234 public Core getCore() {
235 return sonePlugin.core();
239 * Returns the template context factory of the web interface.
241 * @return The template context factory
243 public TemplateContextFactory getTemplateContextFactory() {
244 return templateContextFactory;
248 * Returns the current session, creating a new session if there is no
251 * @param toadletContenxt
252 * The toadlet context
253 * @return The current session, or {@code null} if there is no current
256 public Session getCurrentSession(ToadletContext toadletContenxt) {
257 return getCurrentSession(toadletContenxt, true);
261 * Returns the current session, creating a new session if there is no
262 * current session and {@code create} is {@code true}.
264 * @param toadletContenxt
265 * The toadlet context
267 * {@code true} to create a new session if there is no current
268 * session, {@code false} otherwise
269 * @return The current session, or {@code null} if there is no current
272 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
273 Session session = getSessionManager().useSession(toadletContenxt);
274 if (create && (session == null)) {
275 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
281 * Returns the currently logged in Sone.
283 * @param toadletContext
284 * The toadlet context
285 * @return The currently logged in Sone, or {@code null} if no Sone is
286 * currently logged in
288 public Sone getCurrentSone(ToadletContext toadletContext) {
289 return getCurrentSone(toadletContext, true);
293 * Returns the currently logged in Sone.
295 * @param toadletContext
296 * The toadlet context
298 * {@code true} to create a new session if no session exists,
299 * {@code false} to not create a new session
300 * @return The currently logged in Sone, or {@code null} if no Sone is
301 * currently logged in
303 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
304 return getCurrentSone(getCurrentSession(toadletContext, create));
308 * Returns the currently logged in Sone.
312 * @return The currently logged in Sone, or {@code null} if no Sone is
313 * currently logged in
315 public Sone getCurrentSone(Session session) {
316 if (session == null) {
319 String soneId = (String) session.getAttribute("Sone.CurrentSone");
320 if (soneId == null) {
323 return getCore().getLocalSone(soneId, false);
327 * Sets the currently logged in Sone.
329 * @param toadletContext
330 * The toadlet context
332 * The Sone to set as currently logged in
334 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
335 Session session = getCurrentSession(toadletContext);
337 session.removeAttribute("Sone.CurrentSone");
339 session.setAttribute("Sone.CurrentSone", sone.getId());
344 * Returns the notification manager.
346 * @return The notification manager
348 public NotificationManager getNotifications() {
349 return notificationManager;
353 * Returns the l10n helper of the node.
355 * @return The node’s l10n helper
357 public BaseL10n getL10n() {
358 return sonePlugin.l10n().getBase();
362 * Returns the session manager of the node.
364 * @return The node’s session manager
366 public SessionManager getSessionManager() {
367 return sonePlugin.pluginRespirator().getSessionManager("Sone");
371 * Returns the node’s form password.
373 * @return The form password
375 public String getFormPassword() {
380 * Returns the posts that have been announced as new in the
381 * {@link #newPostNotification}.
383 * @return The new posts
385 public Set<Post> getNewPosts() {
386 return new HashSet<Post>(newPostNotification.getElements());
390 * Returns the replies that have been announced as new in the
391 * {@link #newReplyNotification}.
393 * @return The new replies
395 public Set<Reply> getNewReplies() {
396 return new HashSet<Reply>(newReplyNotification.getElements());
400 * Sets whether the current start of the plugin is the first start. It is
401 * considered a first start if the configuration file does not exist.
404 * {@code true} if no configuration file existed when Sone was
405 * loaded, {@code false} otherwise
407 public void setFirstStart(boolean firstStart) {
409 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
410 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
411 notificationManager.addNotification(firstStartNotification);
416 * Sets whether Sone was started with a fresh configuration file.
419 * {@code true} if Sone was started with a fresh configuration,
420 * {@code false} if the existing configuration could be read
422 public void setNewConfig(boolean newConfig) {
423 if (newConfig && !hasFirstStartNotification()) {
424 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
425 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
426 notificationManager.addNotification(configNotReadNotification);
435 * Returns whether the first start notification is currently displayed.
437 * @return {@code true} if the first-start notification is currently
438 * displayed, {@code false} otherwise
440 private boolean hasFirstStartNotification() {
441 return notificationManager.getNotification("first-start-notification") != null;
449 * Starts the web interface and registers all toadlets.
451 public void start() {
454 /* notification templates. */
455 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
457 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
458 notificationManager.addNotification(startupNotification);
460 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
464 startupNotification.dismiss();
466 }, "Sone Startup Notification Remover");
468 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
469 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
470 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
473 @SuppressWarnings("synthetic-access")
475 if (getCore().getIdentityManager().isConnected()) {
476 wotMissingNotification.dismiss();
478 notificationManager.addNotification(wotMissingNotification);
480 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
483 }, "Sone WoT Connector Checker");
487 * Stops the web interface and unregisters all toadlets.
490 unregisterToadlets();
491 Ticker.getInstance().stop();
499 * Register all toadlets.
501 private void registerToadlets() {
502 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
503 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
504 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
505 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
506 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
507 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
508 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
509 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
510 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
511 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
512 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
513 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
514 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
515 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
516 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
517 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
518 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
519 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
520 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
521 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
522 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
524 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
525 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
540 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
541 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
542 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
543 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
544 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
545 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
546 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
547 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
548 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
549 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
554 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
555 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
556 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
557 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
558 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
559 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
560 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
561 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
562 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
563 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
564 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
565 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
566 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
567 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
568 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
569 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
570 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
571 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
572 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
573 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
574 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
575 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
576 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
577 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
578 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
579 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
580 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
582 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
583 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
584 for (PageToadlet toadlet : pageToadlets) {
585 String menuName = toadlet.getMenuName();
586 if (menuName != null) {
587 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
589 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
595 * Unregisters all toadlets.
597 private void unregisterToadlets() {
598 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
599 for (PageToadlet pageToadlet : pageToadlets) {
600 toadletContainer.unregister(pageToadlet);
602 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
606 * Creates a {@link Reader} from the {@link InputStream} for the resource
607 * with the given name.
609 * @param resourceName
610 * The name of the resource
611 * @return A {@link Reader} for the resource
613 private Reader createReader(String resourceName) {
615 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
616 } catch (UnsupportedEncodingException uee1) {
617 System.out.println(" fail.");
623 // CORELISTENER METHODS
630 public void rescuingSone(Sone sone) {
631 rescuingSonesNotification.add(sone);
632 notificationManager.addNotification(rescuingSonesNotification);
639 public void rescuedSone(Sone sone) {
640 rescuingSonesNotification.remove(sone);
641 sonesRescuedNotification.add(sone);
642 notificationManager.addNotification(sonesRescuedNotification);
649 public void newSoneFound(Sone sone) {
650 newSoneNotification.add(sone);
651 if (!hasFirstStartNotification()) {
652 notificationManager.addNotification(newSoneNotification);
660 public void newPostFound(Post post) {
661 newPostNotification.add(post);
662 if (!hasFirstStartNotification()) {
663 notificationManager.addNotification(newPostNotification);
665 getCore().markPostKnown(post);
673 public void newReplyFound(Reply reply) {
674 if (reply.getPost().getSone() == null) {
677 newReplyNotification.add(reply);
678 if (!hasFirstStartNotification()) {
679 notificationManager.addNotification(newReplyNotification);
681 getCore().markReplyKnown(reply);
689 public void markSoneKnown(Sone sone) {
690 newSoneNotification.remove(sone);
697 public void markPostKnown(Post post) {
698 newPostNotification.remove(post);
705 public void markReplyKnown(Reply reply) {
706 newReplyNotification.remove(reply);
713 public void postRemoved(Post post) {
714 newPostNotification.remove(post);
721 public void replyRemoved(Reply reply) {
722 newReplyNotification.remove(reply);
729 public void soneLocked(final Sone sone) {
730 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
733 @SuppressWarnings("synthetic-access")
735 lockedSonesNotification.add(sone);
736 lockedSonesTickerObjects.remove(sone);
737 notificationManager.addNotification(lockedSonesNotification);
739 }, "Sone Locked Notification");
740 lockedSonesTickerObjects.put(sone, tickerObject);
747 public void soneUnlocked(Sone sone) {
748 lockedSonesNotification.remove(sone);
749 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
756 public void updateFound(Version version, long releaseTime) {
757 newVersionNotification.getTemplateContext().set("version", version);
758 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
759 notificationManager.addNotification(newVersionNotification);
763 * Template provider implementation that uses
764 * {@link WebInterface#createReader(String)} to load templates for
767 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
769 private class ClassPathTemplateProvider implements Provider {
775 @SuppressWarnings("synthetic-access")
776 public Template getTemplate(TemplateContext templateContext, String templateName) {
777 Reader templateReader = createReader("/templates/" + templateName);
778 if (templateReader == null) {
781 Template template = null;
783 template = TemplateParser.parse(templateReader);
784 } catch (TemplateException te1) {
785 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);