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.web.ajax.CreatePostAjaxPage;
61 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
62 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
63 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
64 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
65 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
66 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
67 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
68 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
69 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
70 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
71 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
72 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
73 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
74 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
75 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
76 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
77 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
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.DefaultTemplateFactory;
93 import net.pterodactylus.util.template.MatchFilter;
94 import net.pterodactylus.util.template.PaginationPlugin;
95 import net.pterodactylus.util.template.ReflectionAccessor;
96 import net.pterodactylus.util.template.Template;
97 import net.pterodactylus.util.template.TemplateException;
98 import net.pterodactylus.util.template.TemplateFactory;
99 import net.pterodactylus.util.template.TemplateProvider;
100 import net.pterodactylus.util.template.XmlFilter;
101 import net.pterodactylus.util.thread.Ticker;
102 import net.pterodactylus.util.version.Version;
103 import freenet.clients.http.SessionManager;
104 import freenet.clients.http.SessionManager.Session;
105 import freenet.clients.http.ToadletContainer;
106 import freenet.clients.http.ToadletContext;
107 import freenet.l10n.BaseL10n;
110 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
111 * references to l10n helpers.
113 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
115 public class WebInterface implements CoreListener {
118 private static final Logger logger = Logging.getLogger(WebInterface.class);
120 /** The notification manager. */
121 private final NotificationManager notificationManager = new NotificationManager();
123 /** The Sone plugin. */
124 private final SonePlugin sonePlugin;
126 /** The registered toadlets. */
127 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
129 /** The form password. */
130 private final String formPassword;
132 /** The template factory. */
133 private DefaultTemplateFactory templateFactory;
135 /** The “new Sone” notification. */
136 private final ListNotification<Sone> newSoneNotification;
138 /** The “new post” notification. */
139 private final ListNotification<Post> newPostNotification;
141 /** The “new reply” notification. */
142 private final ListNotification<Reply> newReplyNotification;
144 /** The “rescuing Sone” notification. */
145 private final ListNotification<Sone> rescuingSonesNotification;
147 /** The “Sone rescued” notification. */
148 private final ListNotification<Sone> sonesRescuedNotification;
150 /** Sone locked notification ticker objects. */
151 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
153 /** The “Sone locked” notification. */
154 private final ListNotification<Sone> lockedSonesNotification;
156 /** The “new version” notification. */
157 private final TemplateNotification newVersionNotification;
160 * Creates a new web interface.
165 public WebInterface(SonePlugin sonePlugin) {
166 this.sonePlugin = sonePlugin;
167 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
169 templateFactory = new DefaultTemplateFactory();
170 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
171 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
172 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
173 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
174 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
175 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
176 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
177 templateFactory.addAccessor(Trust.class, new TrustAccessor());
178 templateFactory.addFilter("date", new DateFilter());
179 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
180 templateFactory.addFilter("substring", new SubstringFilter());
181 templateFactory.addFilter("xml", new XmlFilter());
182 templateFactory.addFilter("change", new RequestChangeFilter());
183 templateFactory.addFilter("match", new MatchFilter());
184 templateFactory.addFilter("css", new CssClassNameFilter());
185 templateFactory.addFilter("js", new JavascriptFilter());
186 templateFactory.addFilter("parse", new ParserFilter(templateFactory));
187 templateFactory.addPlugin("getpage", new GetPagePlugin());
188 templateFactory.addPlugin("paginate", new PaginationPlugin());
189 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
190 templateFactory.addTemplateObject("formPassword", formPassword);
192 /* create notifications. */
193 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
194 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
196 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
197 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
199 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
200 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
202 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
203 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
205 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
206 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
208 Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
209 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
211 Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
212 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
220 * Returns the Sone core used by the Sone plugin.
222 * @return The Sone core
224 public Core getCore() {
225 return sonePlugin.core();
229 * Returns the current session, creating a new session if there is no
232 * @param toadletContenxt
233 * The toadlet context
234 * @return The current session, or {@code null} if there is no current
237 public Session getCurrentSession(ToadletContext toadletContenxt) {
238 return getCurrentSession(toadletContenxt, true);
242 * Returns the current session, creating a new session if there is no
243 * current session and {@code create} is {@code true}.
245 * @param toadletContenxt
246 * The toadlet context
248 * {@code true} to create a new session if there is no current
249 * session, {@code false} otherwise
250 * @return The current session, or {@code null} if there is no current
253 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
254 Session session = getSessionManager().useSession(toadletContenxt);
255 if (create && (session == null)) {
256 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
262 * Returns the currently logged in Sone.
264 * @param toadletContext
265 * The toadlet context
266 * @return The currently logged in Sone, or {@code null} if no Sone is
267 * currently logged in
269 public Sone getCurrentSone(ToadletContext toadletContext) {
270 return getCurrentSone(toadletContext, true);
274 * Returns the currently logged in Sone.
276 * @param toadletContext
277 * The toadlet context
279 * {@code true} to create a new session if no session exists,
280 * {@code false} to not create a new session
281 * @return The currently logged in Sone, or {@code null} if no Sone is
282 * currently logged in
284 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
285 return getCurrentSone(getCurrentSession(toadletContext, create));
289 * Returns the currently logged in Sone.
293 * @return The currently logged in Sone, or {@code null} if no Sone is
294 * currently logged in
296 public Sone getCurrentSone(Session session) {
297 if (session == null) {
300 String soneId = (String) session.getAttribute("Sone.CurrentSone");
301 if (soneId == null) {
304 return getCore().getLocalSone(soneId, false);
308 * Sets the currently logged in Sone.
310 * @param toadletContext
311 * The toadlet context
313 * The Sone to set as currently logged in
315 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
316 Session session = getCurrentSession(toadletContext);
318 session.removeAttribute("Sone.CurrentSone");
320 session.setAttribute("Sone.CurrentSone", sone.getId());
325 * Returns the notification manager.
327 * @return The notification manager
329 public NotificationManager getNotifications() {
330 return notificationManager;
334 * Returns the l10n helper of the node.
336 * @return The node’s l10n helper
338 public BaseL10n getL10n() {
339 return sonePlugin.l10n().getBase();
343 * Returns the session manager of the node.
345 * @return The node’s session manager
347 public SessionManager getSessionManager() {
348 return sonePlugin.pluginRespirator().getSessionManager("Sone");
352 * Returns the node’s form password.
354 * @return The form password
356 public String getFormPassword() {
361 * Returns the posts that have been announced as new in the
362 * {@link #newPostNotification}.
364 * @return The new posts
366 public Set<Post> getNewPosts() {
367 return new HashSet<Post>(newPostNotification.getElements());
371 * Returns the replies that have been announced as new in the
372 * {@link #newReplyNotification}.
374 * @return The new replies
376 public Set<Reply> getNewReplies() {
377 return new HashSet<Reply>(newReplyNotification.getElements());
381 * Sets whether the current start of the plugin is the first start. It is
382 * considered a first start if the configuration file does not exist.
385 * {@code true} if no configuration file existed when Sone was
386 * loaded, {@code false} otherwise
388 public void setFirstStart(boolean firstStart) {
390 Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
391 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
392 notificationManager.addNotification(firstStartNotification);
397 * Sets whether Sone was started with a fresh configuration file.
400 * {@code true} if Sone was started with a fresh configuration,
401 * {@code false} if the existing configuration could be read
403 public void setNewConfig(boolean newConfig) {
404 if (newConfig && !hasFirstStartNotification()) {
405 Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
406 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
407 notificationManager.addNotification(configNotReadNotification);
416 * Returns whether the first start notification is currently displayed.
418 * @return {@code true} if the first-start notification is currently
419 * displayed, {@code false} otherwise
421 private boolean hasFirstStartNotification() {
422 return notificationManager.getNotification("first-start-notification") != null;
430 * Starts the web interface and registers all toadlets.
432 public void start() {
435 /* notification templates. */
436 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
438 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
439 notificationManager.addNotification(startupNotification);
441 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
445 startupNotification.dismiss();
447 }, "Sone Startup Notification Remover");
449 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
450 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
451 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
454 @SuppressWarnings("synthetic-access")
456 if (getCore().getIdentityManager().isConnected()) {
457 wotMissingNotification.dismiss();
459 notificationManager.addNotification(wotMissingNotification);
461 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
464 }, "Sone WoT Connector Checker");
468 * Stops the web interface and unregisters all toadlets.
471 unregisterToadlets();
472 Ticker.getInstance().stop();
480 * Register all toadlets.
482 private void registerToadlets() {
483 Template emptyTemplate = templateFactory.createTemplate(new StringReader(""));
484 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
485 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
486 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
487 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
488 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
489 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
490 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
491 Template editProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/editProfileField.html"));
492 Template deleteProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/deleteProfileField.html"));
493 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
494 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
495 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
496 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
497 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
498 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
499 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
500 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
501 Template invalidTemplate = templateFactory.createTemplate(createReader("/templates/invalid.html"));
502 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
503 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
505 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
506 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
507 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
508 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
509 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
510 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
511 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
512 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
513 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
514 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
515 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
516 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
517 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
518 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
519 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
520 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
521 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
522 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
523 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
524 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
525 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
540 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
541 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
542 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
543 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
544 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
545 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
546 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
547 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
548 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
549 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
554 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
555 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
556 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
557 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
558 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
559 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
560 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
561 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
563 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
564 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
565 for (PageToadlet toadlet : pageToadlets) {
566 String menuName = toadlet.getMenuName();
567 if (menuName != null) {
568 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
570 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
576 * Unregisters all toadlets.
578 private void unregisterToadlets() {
579 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
580 for (PageToadlet pageToadlet : pageToadlets) {
581 toadletContainer.unregister(pageToadlet);
583 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
587 * Creates a {@link Reader} from the {@link InputStream} for the resource
588 * with the given name.
590 * @param resourceName
591 * The name of the resource
592 * @return A {@link Reader} for the resource
594 private Reader createReader(String resourceName) {
596 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
597 } catch (UnsupportedEncodingException uee1) {
603 // CORELISTENER METHODS
610 public void rescuingSone(Sone sone) {
611 rescuingSonesNotification.add(sone);
612 notificationManager.addNotification(rescuingSonesNotification);
619 public void rescuedSone(Sone sone) {
620 rescuingSonesNotification.remove(sone);
621 sonesRescuedNotification.add(sone);
622 notificationManager.addNotification(sonesRescuedNotification);
629 public void newSoneFound(Sone sone) {
630 newSoneNotification.add(sone);
631 if (!hasFirstStartNotification()) {
632 notificationManager.addNotification(newSoneNotification);
640 public void newPostFound(Post post) {
641 newPostNotification.add(post);
642 if (!hasFirstStartNotification()) {
643 notificationManager.addNotification(newPostNotification);
645 getCore().markPostKnown(post);
653 public void newReplyFound(Reply reply) {
654 if (reply.getPost().getSone() == null) {
657 newReplyNotification.add(reply);
658 if (!hasFirstStartNotification()) {
659 notificationManager.addNotification(newReplyNotification);
661 getCore().markReplyKnown(reply);
669 public void markSoneKnown(Sone sone) {
670 newSoneNotification.remove(sone);
677 public void markPostKnown(Post post) {
678 newPostNotification.remove(post);
685 public void markReplyKnown(Reply reply) {
686 newReplyNotification.remove(reply);
693 public void postRemoved(Post post) {
694 newPostNotification.remove(post);
701 public void replyRemoved(Reply reply) {
702 newReplyNotification.remove(reply);
709 public void soneLocked(final Sone sone) {
710 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
713 @SuppressWarnings("synthetic-access")
715 lockedSonesNotification.add(sone);
716 lockedSonesTickerObjects.remove(sone);
717 notificationManager.addNotification(lockedSonesNotification);
719 }, "Sone Locked Notification");
720 lockedSonesTickerObjects.put(sone, tickerObject);
727 public void soneUnlocked(Sone sone) {
728 lockedSonesNotification.remove(sone);
729 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
736 public void updateFound(Version version, long releaseTime) {
737 newVersionNotification.set("version", version);
738 newVersionNotification.set("releaseTime", releaseTime);
739 notificationManager.addNotification(newVersionNotification);
743 * Template provider implementation that uses
744 * {@link WebInterface#createReader(String)} to load templates for
747 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
749 private class ClassPathTemplateProvider implements TemplateProvider {
751 /** The template factory. */
752 @SuppressWarnings("hiding")
753 private final TemplateFactory templateFactory;
756 * Creates a new template provider that locates templates on the
759 * @param templateFactory
760 * The template factory to create the templates
762 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
763 this.templateFactory = templateFactory;
770 @SuppressWarnings("synthetic-access")
771 public Template getTemplate(String templateName) {
772 Reader templateReader = createReader("/templates/" + templateName);
773 if (templateReader == null) {
776 Template template = templateFactory.createTemplate(templateReader);
779 } catch (TemplateException te1) {
780 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);