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.PostAccessor;
54 import net.pterodactylus.sone.template.ReplyAccessor;
55 import net.pterodactylus.sone.template.RequestChangeFilter;
56 import net.pterodactylus.sone.template.SoneAccessor;
57 import net.pterodactylus.sone.template.SubstringFilter;
58 import net.pterodactylus.sone.template.TrustAccessor;
59 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
60 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
61 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
62 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
63 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
64 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
65 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
66 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
67 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
68 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
69 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
70 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
71 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
72 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
73 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
74 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
75 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
76 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
77 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
78 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
79 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
80 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
81 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
82 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
83 import net.pterodactylus.sone.web.page.PageToadlet;
84 import net.pterodactylus.sone.web.page.PageToadletFactory;
85 import net.pterodactylus.sone.web.page.StaticPage;
86 import net.pterodactylus.util.logging.Logging;
87 import net.pterodactylus.util.notify.Notification;
88 import net.pterodactylus.util.notify.NotificationManager;
89 import net.pterodactylus.util.notify.TemplateNotification;
90 import net.pterodactylus.util.template.DateFilter;
91 import net.pterodactylus.util.template.DefaultTemplateFactory;
92 import net.pterodactylus.util.template.MatchFilter;
93 import net.pterodactylus.util.template.PaginationPlugin;
94 import net.pterodactylus.util.template.ReflectionAccessor;
95 import net.pterodactylus.util.template.Template;
96 import net.pterodactylus.util.template.TemplateException;
97 import net.pterodactylus.util.template.TemplateFactory;
98 import net.pterodactylus.util.template.TemplateProvider;
99 import net.pterodactylus.util.template.XmlFilter;
100 import net.pterodactylus.util.thread.Ticker;
101 import net.pterodactylus.util.version.Version;
102 import freenet.clients.http.SessionManager;
103 import freenet.clients.http.SessionManager.Session;
104 import freenet.clients.http.ToadletContainer;
105 import freenet.clients.http.ToadletContext;
106 import freenet.l10n.BaseL10n;
109 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
110 * references to l10n helpers.
112 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
114 public class WebInterface implements CoreListener {
117 private static final Logger logger = Logging.getLogger(WebInterface.class);
119 /** The notification manager. */
120 private final NotificationManager notificationManager = new NotificationManager();
122 /** The Sone plugin. */
123 private final SonePlugin sonePlugin;
125 /** The registered toadlets. */
126 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
128 /** The form password. */
129 private final String formPassword;
131 /** The template factory. */
132 private DefaultTemplateFactory templateFactory;
134 /** The “new Sone” notification. */
135 private final ListNotification<Sone> newSoneNotification;
137 /** The “new post” notification. */
138 private final ListNotification<Post> newPostNotification;
140 /** The “new reply” notification. */
141 private final ListNotification<Reply> newReplyNotification;
143 /** The “rescuing Sone” notification. */
144 private final ListNotification<Sone> rescuingSonesNotification;
146 /** The “Sone rescued” notification. */
147 private final ListNotification<Sone> sonesRescuedNotification;
149 /** Sone locked notification ticker objects. */
150 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
152 /** The “Sone locked” notification. */
153 private final ListNotification<Sone> lockedSonesNotification;
155 /** The “new version” notification. */
156 private final TemplateNotification newVersionNotification;
159 * Creates a new web interface.
164 public WebInterface(SonePlugin sonePlugin) {
165 this.sonePlugin = sonePlugin;
166 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
168 templateFactory = new DefaultTemplateFactory();
169 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
170 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
171 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
172 templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
173 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
174 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
175 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
176 templateFactory.addAccessor(Trust.class, new TrustAccessor());
177 templateFactory.addFilter("date", new DateFilter());
178 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
179 templateFactory.addFilter("substring", new SubstringFilter());
180 templateFactory.addFilter("xml", new XmlFilter());
181 templateFactory.addFilter("change", new RequestChangeFilter());
182 templateFactory.addFilter("match", new MatchFilter());
183 templateFactory.addFilter("css", new CssClassNameFilter());
184 templateFactory.addFilter("js", new JavascriptFilter());
185 templateFactory.addPlugin("getpage", new GetPagePlugin());
186 templateFactory.addPlugin("paginate", new PaginationPlugin());
187 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
188 templateFactory.addTemplateObject("formPassword", formPassword);
190 /* create notifications. */
191 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
192 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
194 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
195 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
197 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
198 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
200 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
201 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
203 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
204 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
206 Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
207 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
209 Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
210 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
218 * Returns the Sone core used by the Sone plugin.
220 * @return The Sone core
222 public Core getCore() {
223 return sonePlugin.core();
227 * Returns the current session, creating a new session if there is no
230 * @param toadletContenxt
231 * The toadlet context
232 * @return The current session, or {@code null} if there is no current
235 public Session getCurrentSession(ToadletContext toadletContenxt) {
236 return getCurrentSession(toadletContenxt, true);
240 * Returns the current session, creating a new session if there is no
241 * current session and {@code create} is {@code true}.
243 * @param toadletContenxt
244 * The toadlet context
246 * {@code true} to create a new session if there is no current
247 * session, {@code false} otherwise
248 * @return The current session, or {@code null} if there is no current
251 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
252 Session session = getSessionManager().useSession(toadletContenxt);
253 if (create && (session == null)) {
254 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
260 * Returns the currently logged in Sone.
262 * @param toadletContext
263 * The toadlet context
264 * @return The currently logged in Sone, or {@code null} if no Sone is
265 * currently logged in
267 public Sone getCurrentSone(ToadletContext toadletContext) {
268 return getCurrentSone(toadletContext, true);
272 * Returns the currently logged in Sone.
274 * @param toadletContext
275 * The toadlet context
277 * {@code true} to create a new session if no session exists,
278 * {@code false} to not create a new session
279 * @return The currently logged in Sone, or {@code null} if no Sone is
280 * currently logged in
282 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
283 return getCurrentSone(getCurrentSession(toadletContext, create));
287 * Returns the currently logged in Sone.
291 * @return The currently logged in Sone, or {@code null} if no Sone is
292 * currently logged in
294 public Sone getCurrentSone(Session session) {
295 if (session == null) {
298 String soneId = (String) session.getAttribute("Sone.CurrentSone");
299 if (soneId == null) {
302 return getCore().getLocalSone(soneId, false);
306 * Sets the currently logged in Sone.
308 * @param toadletContext
309 * The toadlet context
311 * The Sone to set as currently logged in
313 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
314 Session session = getCurrentSession(toadletContext);
316 session.removeAttribute("Sone.CurrentSone");
318 session.setAttribute("Sone.CurrentSone", sone.getId());
323 * Returns the notification manager.
325 * @return The notification manager
327 public NotificationManager getNotifications() {
328 return notificationManager;
332 * Returns the l10n helper of the node.
334 * @return The node’s l10n helper
336 public BaseL10n getL10n() {
337 return sonePlugin.l10n().getBase();
341 * Returns the session manager of the node.
343 * @return The node’s session manager
345 public SessionManager getSessionManager() {
346 return sonePlugin.pluginRespirator().getSessionManager("Sone");
350 * Returns the node’s form password.
352 * @return The form password
354 public String getFormPassword() {
359 * Returns the posts that have been announced as new in the
360 * {@link #newPostNotification}.
362 * @return The new posts
364 public Set<Post> getNewPosts() {
365 return new HashSet<Post>(newPostNotification.getElements());
369 * Returns the replies that have been announced as new in the
370 * {@link #newReplyNotification}.
372 * @return The new replies
374 public Set<Reply> getNewReplies() {
375 return new HashSet<Reply>(newReplyNotification.getElements());
379 * Sets whether the current start of the plugin is the first start. It is
380 * considered a first start if the configuration file does not exist.
383 * {@code true} if no configuration file existed when Sone was
384 * loaded, {@code false} otherwise
386 public void setFirstStart(boolean firstStart) {
388 Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
389 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
390 notificationManager.addNotification(firstStartNotification);
395 * Sets whether Sone was started with a fresh configuration file.
398 * {@code true} if Sone was started with a fresh configuration,
399 * {@code false} if the existing configuration could be read
401 public void setNewConfig(boolean newConfig) {
402 if (newConfig && !hasFirstStartNotification()) {
403 Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
404 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
405 notificationManager.addNotification(configNotReadNotification);
414 * Returns whether the first start notification is currently displayed.
416 * @return {@code true} if the first-start notification is currently
417 * displayed, {@code false} otherwise
419 private boolean hasFirstStartNotification() {
420 return notificationManager.getNotification("first-start-notification") != null;
428 * Starts the web interface and registers all toadlets.
430 public void start() {
433 /* notification templates. */
434 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
436 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
437 notificationManager.addNotification(startupNotification);
439 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
443 startupNotification.dismiss();
445 }, "Sone Startup Notification Remover");
447 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
448 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
449 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
452 @SuppressWarnings("synthetic-access")
454 if (getCore().getIdentityManager().isConnected()) {
455 wotMissingNotification.dismiss();
457 notificationManager.addNotification(wotMissingNotification);
459 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
462 }, "Sone WoT Connector Checker");
466 * Stops the web interface and unregisters all toadlets.
469 unregisterToadlets();
470 Ticker.getInstance().stop();
478 * Register all toadlets.
480 private void registerToadlets() {
481 Template emptyTemplate = templateFactory.createTemplate(new StringReader(""));
482 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
483 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
484 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
485 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
486 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
487 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
488 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
489 Template editProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/editProfileField.html"));
490 Template deleteProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/deleteProfileField.html"));
491 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
492 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
493 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
494 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
495 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
496 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
497 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
498 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
499 Template invalidTemplate = templateFactory.createTemplate(createReader("/templates/invalid.html"));
500 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
501 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
503 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
504 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
505 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
506 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
507 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
508 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
509 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
510 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
511 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
512 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
513 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
514 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
515 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
516 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
517 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
518 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
519 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
520 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
521 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
522 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
523 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
524 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
525 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
540 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
541 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
542 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
543 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
544 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
545 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
546 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
547 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
548 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
549 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
554 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
555 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
556 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
557 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
558 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
559 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
561 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
562 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
563 for (PageToadlet toadlet : pageToadlets) {
564 String menuName = toadlet.getMenuName();
565 if (menuName != null) {
566 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
568 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
574 * Unregisters all toadlets.
576 private void unregisterToadlets() {
577 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
578 for (PageToadlet pageToadlet : pageToadlets) {
579 toadletContainer.unregister(pageToadlet);
581 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
585 * Creates a {@link Reader} from the {@link InputStream} for the resource
586 * with the given name.
588 * @param resourceName
589 * The name of the resource
590 * @return A {@link Reader} for the resource
592 private Reader createReader(String resourceName) {
594 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
595 } catch (UnsupportedEncodingException uee1) {
601 // CORELISTENER METHODS
608 public void rescuingSone(Sone sone) {
609 rescuingSonesNotification.add(sone);
610 notificationManager.addNotification(rescuingSonesNotification);
617 public void rescuedSone(Sone sone) {
618 rescuingSonesNotification.remove(sone);
619 sonesRescuedNotification.add(sone);
620 notificationManager.addNotification(sonesRescuedNotification);
627 public void newSoneFound(Sone sone) {
628 newSoneNotification.add(sone);
629 if (!hasFirstStartNotification()) {
630 notificationManager.addNotification(newSoneNotification);
638 public void newPostFound(Post post) {
639 newPostNotification.add(post);
640 if (!hasFirstStartNotification()) {
641 notificationManager.addNotification(newPostNotification);
643 getCore().markPostKnown(post);
651 public void newReplyFound(Reply reply) {
652 if (reply.getPost().getSone() == null) {
655 newReplyNotification.add(reply);
656 if (!hasFirstStartNotification()) {
657 notificationManager.addNotification(newReplyNotification);
659 getCore().markReplyKnown(reply);
667 public void markSoneKnown(Sone sone) {
668 newSoneNotification.remove(sone);
675 public void markPostKnown(Post post) {
676 newPostNotification.remove(post);
683 public void markReplyKnown(Reply reply) {
684 newReplyNotification.remove(reply);
691 public void postRemoved(Post post) {
692 newPostNotification.remove(post);
699 public void replyRemoved(Reply reply) {
700 newReplyNotification.remove(reply);
707 public void soneLocked(final Sone sone) {
708 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
711 @SuppressWarnings("synthetic-access")
713 lockedSonesNotification.add(sone);
714 lockedSonesTickerObjects.remove(sone);
715 notificationManager.addNotification(lockedSonesNotification);
717 }, "Sone Locked Notification");
718 lockedSonesTickerObjects.put(sone, tickerObject);
725 public void soneUnlocked(Sone sone) {
726 lockedSonesNotification.remove(sone);
727 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
734 public void updateFound(Version version, long releaseTime) {
735 newVersionNotification.set("version", version);
736 newVersionNotification.set("releaseTime", releaseTime);
737 notificationManager.addNotification(newVersionNotification);
741 * Template provider implementation that uses
742 * {@link WebInterface#createReader(String)} to load templates for
745 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
747 private class ClassPathTemplateProvider implements TemplateProvider {
749 /** The template factory. */
750 @SuppressWarnings("hiding")
751 private final TemplateFactory templateFactory;
754 * Creates a new template provider that locates templates on the
757 * @param templateFactory
758 * The template factory to create the templates
760 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
761 this.templateFactory = templateFactory;
768 @SuppressWarnings("synthetic-access")
769 public Template getTemplate(String templateName) {
770 Reader templateReader = createReader("/templates/" + templateName);
771 if (templateReader == null) {
774 Template template = templateFactory.createTemplate(templateReader);
777 } catch (TemplateException te1) {
778 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);