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.HtmlFilter;
94 import net.pterodactylus.util.template.MatchFilter;
95 import net.pterodactylus.util.template.PaginationPlugin;
96 import net.pterodactylus.util.template.Provider;
97 import net.pterodactylus.util.template.ReflectionAccessor;
98 import net.pterodactylus.util.template.ReplaceFilter;
99 import net.pterodactylus.util.template.StoreFilter;
100 import net.pterodactylus.util.template.Template;
101 import net.pterodactylus.util.template.TemplateContext;
102 import net.pterodactylus.util.template.TemplateContextFactory;
103 import net.pterodactylus.util.template.TemplateException;
104 import net.pterodactylus.util.template.TemplateParser;
105 import net.pterodactylus.util.template.XmlFilter;
106 import net.pterodactylus.util.thread.Ticker;
107 import net.pterodactylus.util.version.Version;
108 import freenet.clients.http.SessionManager;
109 import freenet.clients.http.SessionManager.Session;
110 import freenet.clients.http.ToadletContainer;
111 import freenet.clients.http.ToadletContext;
112 import freenet.l10n.BaseL10n;
115 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
116 * references to l10n helpers.
118 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
120 public class WebInterface implements CoreListener {
123 private static final Logger logger = Logging.getLogger(WebInterface.class);
125 /** The notification manager. */
126 private final NotificationManager notificationManager = new NotificationManager();
128 /** The Sone plugin. */
129 private final SonePlugin sonePlugin;
131 /** The registered toadlets. */
132 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
134 /** The form password. */
135 private final String formPassword;
137 /** The template context factory. */
138 private final TemplateContextFactory templateContextFactory;
140 /** The “new Sone” notification. */
141 private final ListNotification<Sone> newSoneNotification;
143 /** The “new post” notification. */
144 private final ListNotification<Post> newPostNotification;
146 /** The “new reply” notification. */
147 private final ListNotification<Reply> newReplyNotification;
149 /** The “rescuing Sone” notification. */
150 private final ListNotification<Sone> rescuingSonesNotification;
152 /** The “Sone rescued” notification. */
153 private final ListNotification<Sone> sonesRescuedNotification;
155 /** Sone locked notification ticker objects. */
156 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
158 /** The “Sone locked” notification. */
159 private final ListNotification<Sone> lockedSonesNotification;
161 /** The “new version” notification. */
162 private final TemplateNotification newVersionNotification;
165 * Creates a new web interface.
170 @SuppressWarnings("synthetic-access")
171 public WebInterface(SonePlugin sonePlugin) {
172 this.sonePlugin = sonePlugin;
173 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
175 templateContextFactory = new TemplateContextFactory();
176 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
177 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
178 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
179 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
180 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
181 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
182 templateContextFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
183 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
184 templateContextFactory.addFilter("date", new DateFilter());
185 templateContextFactory.addFilter("html", new HtmlFilter());
186 templateContextFactory.addFilter("replace", new ReplaceFilter());
187 templateContextFactory.addFilter("store", new StoreFilter());
188 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
189 templateContextFactory.addFilter("substring", new SubstringFilter());
190 templateContextFactory.addFilter("xml", new XmlFilter());
191 templateContextFactory.addFilter("change", new RequestChangeFilter());
192 templateContextFactory.addFilter("match", new MatchFilter());
193 templateContextFactory.addFilter("css", new CssClassNameFilter());
194 templateContextFactory.addFilter("js", new JavascriptFilter());
195 templateContextFactory.addFilter("parse", new ParserFilter(templateContextFactory));
196 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
197 templateContextFactory.addPlugin("getpage", new GetPagePlugin());
198 templateContextFactory.addPlugin("paginate", new PaginationPlugin());
199 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
200 templateContextFactory.addProvider(new ClassPathTemplateProvider());
201 templateContextFactory.addTemplateObject("formPassword", formPassword);
203 /* create notifications. */
204 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
205 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
207 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
208 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
210 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
211 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
213 Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html"));
214 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
216 Template sonesRescuedTemplate = TemplateParser.parse(createReader("/templates/notify/sonesRescuedNotification.html"));
217 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
219 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
220 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
222 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
223 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
231 * Returns the Sone core used by the Sone plugin.
233 * @return The Sone core
235 public Core getCore() {
236 return sonePlugin.core();
240 * Returns the template context factory of the web interface.
242 * @return The template context factory
244 public TemplateContextFactory getTemplateContextFactory() {
245 return templateContextFactory;
249 * Returns the current session, creating a new session if there is no
252 * @param toadletContenxt
253 * The toadlet context
254 * @return The current session, or {@code null} if there is no current
257 public Session getCurrentSession(ToadletContext toadletContenxt) {
258 return getCurrentSession(toadletContenxt, true);
262 * Returns the current session, creating a new session if there is no
263 * current session and {@code create} is {@code true}.
265 * @param toadletContenxt
266 * The toadlet context
268 * {@code true} to create a new session if there is no current
269 * session, {@code false} otherwise
270 * @return The current session, or {@code null} if there is no current
273 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
274 Session session = getSessionManager().useSession(toadletContenxt);
275 if (create && (session == null)) {
276 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
282 * Returns the currently logged in Sone.
284 * @param toadletContext
285 * The toadlet context
286 * @return The currently logged in Sone, or {@code null} if no Sone is
287 * currently logged in
289 public Sone getCurrentSone(ToadletContext toadletContext) {
290 return getCurrentSone(toadletContext, true);
294 * Returns the currently logged in Sone.
296 * @param toadletContext
297 * The toadlet context
299 * {@code true} to create a new session if no session exists,
300 * {@code false} to not create a new session
301 * @return The currently logged in Sone, or {@code null} if no Sone is
302 * currently logged in
304 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
305 return getCurrentSone(getCurrentSession(toadletContext, create));
309 * Returns the currently logged in Sone.
313 * @return The currently logged in Sone, or {@code null} if no Sone is
314 * currently logged in
316 public Sone getCurrentSone(Session session) {
317 if (session == null) {
320 String soneId = (String) session.getAttribute("Sone.CurrentSone");
321 if (soneId == null) {
324 return getCore().getLocalSone(soneId, false);
328 * Sets the currently logged in Sone.
330 * @param toadletContext
331 * The toadlet context
333 * The Sone to set as currently logged in
335 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
336 Session session = getCurrentSession(toadletContext);
338 session.removeAttribute("Sone.CurrentSone");
340 session.setAttribute("Sone.CurrentSone", sone.getId());
345 * Returns the notification manager.
347 * @return The notification manager
349 public NotificationManager getNotifications() {
350 return notificationManager;
354 * Returns the l10n helper of the node.
356 * @return The node’s l10n helper
358 public BaseL10n getL10n() {
359 return sonePlugin.l10n().getBase();
363 * Returns the session manager of the node.
365 * @return The node’s session manager
367 public SessionManager getSessionManager() {
368 return sonePlugin.pluginRespirator().getSessionManager("Sone");
372 * Returns the node’s form password.
374 * @return The form password
376 public String getFormPassword() {
381 * Returns the posts that have been announced as new in the
382 * {@link #newPostNotification}.
384 * @return The new posts
386 public Set<Post> getNewPosts() {
387 return new HashSet<Post>(newPostNotification.getElements());
391 * Returns the replies that have been announced as new in the
392 * {@link #newReplyNotification}.
394 * @return The new replies
396 public Set<Reply> getNewReplies() {
397 return new HashSet<Reply>(newReplyNotification.getElements());
401 * Sets whether the current start of the plugin is the first start. It is
402 * considered a first start if the configuration file does not exist.
405 * {@code true} if no configuration file existed when Sone was
406 * loaded, {@code false} otherwise
408 public void setFirstStart(boolean firstStart) {
410 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
411 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
412 notificationManager.addNotification(firstStartNotification);
417 * Sets whether Sone was started with a fresh configuration file.
420 * {@code true} if Sone was started with a fresh configuration,
421 * {@code false} if the existing configuration could be read
423 public void setNewConfig(boolean newConfig) {
424 if (newConfig && !hasFirstStartNotification()) {
425 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
426 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
427 notificationManager.addNotification(configNotReadNotification);
436 * Returns whether the first start notification is currently displayed.
438 * @return {@code true} if the first-start notification is currently
439 * displayed, {@code false} otherwise
441 private boolean hasFirstStartNotification() {
442 return notificationManager.getNotification("first-start-notification") != null;
450 * Starts the web interface and registers all toadlets.
452 public void start() {
455 /* notification templates. */
456 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
458 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
459 notificationManager.addNotification(startupNotification);
461 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
465 startupNotification.dismiss();
467 }, "Sone Startup Notification Remover");
469 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
470 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
471 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
474 @SuppressWarnings("synthetic-access")
476 if (getCore().getIdentityManager().isConnected()) {
477 wotMissingNotification.dismiss();
479 notificationManager.addNotification(wotMissingNotification);
481 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
484 }, "Sone WoT Connector Checker");
488 * Stops the web interface and unregisters all toadlets.
491 unregisterToadlets();
492 Ticker.getInstance().stop();
500 * Register all toadlets.
502 private void registerToadlets() {
503 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
504 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
505 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
506 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
507 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
508 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
509 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
510 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
511 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
512 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
513 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
514 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
515 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
516 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
517 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
518 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
519 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
520 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
521 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
522 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
523 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
525 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
540 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
541 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
542 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
543 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
544 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
545 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
546 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
547 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
548 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
549 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
554 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
555 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
556 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
557 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
558 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
559 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
560 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
561 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
562 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
563 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
564 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
565 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
566 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
567 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
568 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
569 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
570 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
571 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
572 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
573 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
574 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
575 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
576 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
577 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
578 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
579 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
580 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
581 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
582 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
584 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
585 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
586 for (PageToadlet toadlet : pageToadlets) {
587 String menuName = toadlet.getMenuName();
588 if (menuName != null) {
589 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
591 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
597 * Unregisters all toadlets.
599 private void unregisterToadlets() {
600 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
601 for (PageToadlet pageToadlet : pageToadlets) {
602 toadletContainer.unregister(pageToadlet);
604 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
608 * Creates a {@link Reader} from the {@link InputStream} for the resource
609 * with the given name.
611 * @param resourceName
612 * The name of the resource
613 * @return A {@link Reader} for the resource
615 private Reader createReader(String resourceName) {
617 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
618 } catch (UnsupportedEncodingException uee1) {
619 System.out.println(" fail.");
625 // CORELISTENER METHODS
632 public void rescuingSone(Sone sone) {
633 rescuingSonesNotification.add(sone);
634 notificationManager.addNotification(rescuingSonesNotification);
641 public void rescuedSone(Sone sone) {
642 rescuingSonesNotification.remove(sone);
643 sonesRescuedNotification.add(sone);
644 notificationManager.addNotification(sonesRescuedNotification);
651 public void newSoneFound(Sone sone) {
652 newSoneNotification.add(sone);
653 if (!hasFirstStartNotification()) {
654 notificationManager.addNotification(newSoneNotification);
662 public void newPostFound(Post post) {
663 newPostNotification.add(post);
664 if (!hasFirstStartNotification()) {
665 notificationManager.addNotification(newPostNotification);
667 getCore().markPostKnown(post);
675 public void newReplyFound(Reply reply) {
676 if (reply.getPost().getSone() == null) {
679 newReplyNotification.add(reply);
680 if (!hasFirstStartNotification()) {
681 notificationManager.addNotification(newReplyNotification);
683 getCore().markReplyKnown(reply);
691 public void markSoneKnown(Sone sone) {
692 newSoneNotification.remove(sone);
699 public void markPostKnown(Post post) {
700 newPostNotification.remove(post);
707 public void markReplyKnown(Reply reply) {
708 newReplyNotification.remove(reply);
715 public void postRemoved(Post post) {
716 newPostNotification.remove(post);
723 public void replyRemoved(Reply reply) {
724 newReplyNotification.remove(reply);
731 public void soneLocked(final Sone sone) {
732 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
735 @SuppressWarnings("synthetic-access")
737 lockedSonesNotification.add(sone);
738 lockedSonesTickerObjects.remove(sone);
739 notificationManager.addNotification(lockedSonesNotification);
741 }, "Sone Locked Notification");
742 lockedSonesTickerObjects.put(sone, tickerObject);
749 public void soneUnlocked(Sone sone) {
750 lockedSonesNotification.remove(sone);
751 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
758 public void updateFound(Version version, long releaseTime) {
759 newVersionNotification.getTemplateContext().set("version", version);
760 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
761 notificationManager.addNotification(newVersionNotification);
765 * Template provider implementation that uses
766 * {@link WebInterface#createReader(String)} to load templates for
769 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
771 private class ClassPathTemplateProvider implements Provider {
777 @SuppressWarnings("synthetic-access")
778 public Template getTemplate(TemplateContext templateContext, String templateName) {
779 Reader templateReader = createReader("/templates/" + templateName);
780 if (templateReader == null) {
783 Template template = null;
785 template = TemplateParser.parse(templateReader);
786 } catch (TemplateException te1) {
787 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);