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.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
32 import java.util.UUID;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
36 import net.pterodactylus.sone.core.Core;
37 import net.pterodactylus.sone.core.CoreListener;
38 import net.pterodactylus.sone.data.Post;
39 import net.pterodactylus.sone.data.Reply;
40 import net.pterodactylus.sone.data.Sone;
41 import net.pterodactylus.sone.freenet.L10nFilter;
42 import net.pterodactylus.sone.freenet.wot.Identity;
43 import net.pterodactylus.sone.main.SonePlugin;
44 import net.pterodactylus.sone.notify.ListNotification;
45 import net.pterodactylus.sone.template.CollectionAccessor;
46 import net.pterodactylus.sone.template.CssClassNameFilter;
47 import net.pterodactylus.sone.template.GetPagePlugin;
48 import net.pterodactylus.sone.template.IdentityAccessor;
49 import net.pterodactylus.sone.template.JavascriptFilter;
50 import net.pterodactylus.sone.template.NotificationManagerAccessor;
51 import net.pterodactylus.sone.template.PostAccessor;
52 import net.pterodactylus.sone.template.ReplyAccessor;
53 import net.pterodactylus.sone.template.RequestChangeFilter;
54 import net.pterodactylus.sone.template.SoneAccessor;
55 import net.pterodactylus.sone.template.SubstringFilter;
56 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
57 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
58 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
59 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
60 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
61 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
62 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
63 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
64 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
65 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
66 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
67 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
68 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
69 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
70 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
71 import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
72 import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
73 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
74 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
75 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
76 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
77 import net.pterodactylus.sone.web.page.PageToadlet;
78 import net.pterodactylus.sone.web.page.PageToadletFactory;
79 import net.pterodactylus.sone.web.page.StaticPage;
80 import net.pterodactylus.util.logging.Logging;
81 import net.pterodactylus.util.notify.Notification;
82 import net.pterodactylus.util.notify.NotificationManager;
83 import net.pterodactylus.util.notify.TemplateNotification;
84 import net.pterodactylus.util.template.DateFilter;
85 import net.pterodactylus.util.template.DefaultTemplateFactory;
86 import net.pterodactylus.util.template.MatchFilter;
87 import net.pterodactylus.util.template.PaginationPlugin;
88 import net.pterodactylus.util.template.ReflectionAccessor;
89 import net.pterodactylus.util.template.Template;
90 import net.pterodactylus.util.template.TemplateException;
91 import net.pterodactylus.util.template.TemplateFactory;
92 import net.pterodactylus.util.template.TemplateProvider;
93 import net.pterodactylus.util.template.XmlFilter;
94 import net.pterodactylus.util.thread.Ticker;
95 import net.pterodactylus.util.version.Version;
96 import freenet.clients.http.SessionManager;
97 import freenet.clients.http.SessionManager.Session;
98 import freenet.clients.http.ToadletContainer;
99 import freenet.clients.http.ToadletContext;
100 import freenet.l10n.BaseL10n;
103 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
104 * references to l10n helpers.
106 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
108 public class WebInterface implements CoreListener {
111 private static final Logger logger = Logging.getLogger(WebInterface.class);
113 /** The notification manager. */
114 private final NotificationManager notificationManager = new NotificationManager();
116 /** The Sone plugin. */
117 private final SonePlugin sonePlugin;
119 /** The registered toadlets. */
120 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
122 /** The form password. */
123 private final String formPassword;
125 /** The template factory. */
126 private DefaultTemplateFactory templateFactory;
128 /** The “new Sone” notification. */
129 private final ListNotification<Sone> newSoneNotification;
131 /** The “new post” notification. */
132 private final ListNotification<Post> newPostNotification;
134 /** The “new reply” notification. */
135 private final ListNotification<Reply> newReplyNotification;
137 /** The “rescuing Sone” notification. */
138 private final ListNotification<Sone> rescuingSonesNotification;
140 /** The “Sone rescued” notification. */
141 private final ListNotification<Sone> sonesRescuedNotification;
143 /** Sone locked notification ticker objects. */
144 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
146 /** The “Sone locked” notification. */
147 private final ListNotification<Sone> lockedSonesNotification;
149 /** The “new version” notification. */
150 private final TemplateNotification newVersionNotification;
153 * Creates a new web interface.
158 public WebInterface(SonePlugin sonePlugin) {
159 this.sonePlugin = sonePlugin;
160 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
162 templateFactory = new DefaultTemplateFactory();
163 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
164 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
165 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
166 templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
167 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
168 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
169 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
170 templateFactory.addFilter("date", new DateFilter());
171 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
172 templateFactory.addFilter("substring", new SubstringFilter());
173 templateFactory.addFilter("xml", new XmlFilter());
174 templateFactory.addFilter("change", new RequestChangeFilter());
175 templateFactory.addFilter("match", new MatchFilter());
176 templateFactory.addFilter("css", new CssClassNameFilter());
177 templateFactory.addFilter("js", new JavascriptFilter());
178 templateFactory.addPlugin("getpage", new GetPagePlugin());
179 templateFactory.addPlugin("paginate", new PaginationPlugin());
180 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
181 templateFactory.addTemplateObject("formPassword", formPassword);
183 /* create notifications. */
184 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
185 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
187 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
188 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
190 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
191 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
193 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
194 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
196 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
197 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
199 Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
200 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
202 Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
203 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
211 * Returns the Sone core used by the Sone plugin.
213 * @return The Sone core
215 public Core getCore() {
216 return sonePlugin.core();
220 * Returns the current session, creating a new session if there is no
223 * @param toadletContenxt
224 * The toadlet context
225 * @return The current session, or {@code null} if there is no current
228 public Session getCurrentSession(ToadletContext toadletContenxt) {
229 return getCurrentSession(toadletContenxt, true);
233 * Returns the current session, creating a new session if there is no
234 * current session and {@code create} is {@code true}.
236 * @param toadletContenxt
237 * The toadlet context
239 * {@code true} to create a new session if there is no current
240 * session, {@code false} otherwise
241 * @return The current session, or {@code null} if there is no current
244 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
245 Session session = getSessionManager().useSession(toadletContenxt);
246 if (create && (session == null)) {
247 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
253 * Returns the currently logged in Sone.
255 * @param toadletContext
256 * The toadlet context
257 * @return The currently logged in Sone, or {@code null} if no Sone is
258 * currently logged in
260 public Sone getCurrentSone(ToadletContext toadletContext) {
261 return getCurrentSone(toadletContext, true);
265 * Returns the currently logged in Sone.
267 * @param toadletContext
268 * The toadlet context
270 * {@code true} to create a new session if no session exists,
271 * {@code false} to not create a new session
272 * @return The currently logged in Sone, or {@code null} if no Sone is
273 * currently logged in
275 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
276 return getCurrentSone(getCurrentSession(toadletContext, create));
280 * Returns the currently logged in Sone.
284 * @return The currently logged in Sone, or {@code null} if no Sone is
285 * currently logged in
287 public Sone getCurrentSone(Session session) {
288 if (session == null) {
291 String soneId = (String) session.getAttribute("Sone.CurrentSone");
292 if (soneId == null) {
295 return getCore().getLocalSone(soneId, false);
299 * Sets the currently logged in Sone.
301 * @param toadletContext
302 * The toadlet context
304 * The Sone to set as currently logged in
306 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
307 Session session = getCurrentSession(toadletContext);
309 session.removeAttribute("Sone.CurrentSone");
311 session.setAttribute("Sone.CurrentSone", sone.getId());
316 * Returns the notification manager.
318 * @return The notification manager
320 public NotificationManager getNotifications() {
321 return notificationManager;
325 * Returns the l10n helper of the node.
327 * @return The node’s l10n helper
329 public BaseL10n getL10n() {
330 return sonePlugin.l10n().getBase();
334 * Returns the session manager of the node.
336 * @return The node’s session manager
338 public SessionManager getSessionManager() {
339 return sonePlugin.pluginRespirator().getSessionManager("Sone");
343 * Returns the node’s form password.
345 * @return The form password
347 public String getFormPassword() {
352 * Returns the posts that have been announced as new in the
353 * {@link #newPostNotification}.
355 * @return The new posts
357 public Set<Post> getNewPosts() {
358 return new HashSet<Post>(newPostNotification.getElements());
362 * Returns the replies that have been announced as new in the
363 * {@link #newReplyNotification}.
365 * @return The new replies
367 public Set<Reply> getNewReplies() {
368 return new HashSet<Reply>(newReplyNotification.getElements());
372 * Sets whether the current start of the plugin is the first start. It is
373 * considered a first start if the configuration file does not exist.
376 * {@code true} if no configuration file existed when Sone was
377 * loaded, {@code false} otherwise
379 public void setFirstStart(boolean firstStart) {
381 Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
382 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
383 notificationManager.addNotification(firstStartNotification);
388 * Sets whether Sone was started with a fresh configuration file.
391 * {@code true} if Sone was started with a fresh configuration,
392 * {@code false} if the existing configuration could be read
394 public void setNewConfig(boolean newConfig) {
395 if (newConfig && !hasFirstStartNotification()) {
396 Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
397 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
398 notificationManager.addNotification(configNotReadNotification);
407 * Returns whether the first start notification is currently displayed.
409 * @return {@code true} if the first-start notification is currently
410 * displayed, {@code false} otherwise
412 private boolean hasFirstStartNotification() {
413 return notificationManager.getNotification("first-start-notification") != null;
421 * Starts the web interface and registers all toadlets.
423 public void start() {
426 /* notification templates. */
427 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
429 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
430 notificationManager.addNotification(startupNotification);
432 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
436 startupNotification.dismiss();
438 }, "Sone Startup Notification Remover");
440 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
441 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
442 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
445 @SuppressWarnings("synthetic-access")
447 if (getCore().getIdentityManager().isConnected()) {
448 wotMissingNotification.dismiss();
450 notificationManager.addNotification(wotMissingNotification);
452 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
455 }, "Sone WoT Connector Checker");
459 * Stops the web interface and unregisters all toadlets.
462 unregisterToadlets();
463 Ticker.getInstance().stop();
471 * Register all toadlets.
473 private void registerToadlets() {
474 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
475 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
476 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
477 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
478 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
479 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
480 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
481 Template editProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/editProfileField.html"));
482 Template deleteProfileFieldTemplate = templateFactory.createTemplate(createReader("/templates/deleteProfileField.html"));
483 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
484 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
485 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
486 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
487 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
488 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
489 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
490 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
491 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
492 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
493 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
494 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
495 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
496 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.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(likePostTemplate, this)));
515 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, 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(lockSoneTemplate, this)));
519 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
520 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
521 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
522 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
523 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
524 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
525 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
526 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
527 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
528 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
529 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
530 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
531 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
532 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
533 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
534 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
535 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
536 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
537 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
538 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
539 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
540 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
541 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
542 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
543 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
544 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
545 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
546 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
547 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
548 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
549 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
555 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
556 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
557 for (PageToadlet toadlet : pageToadlets) {
558 String menuName = toadlet.getMenuName();
559 if (menuName != null) {
560 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
562 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
568 * Unregisters all toadlets.
570 private void unregisterToadlets() {
571 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
572 for (PageToadlet pageToadlet : pageToadlets) {
573 toadletContainer.unregister(pageToadlet);
575 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
579 * Creates a {@link Reader} from the {@link InputStream} for the resource
580 * with the given name.
582 * @param resourceName
583 * The name of the resource
584 * @return A {@link Reader} for the resource
586 private Reader createReader(String resourceName) {
588 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
589 } catch (UnsupportedEncodingException uee1) {
595 // CORELISTENER METHODS
602 public void rescuingSone(Sone sone) {
603 rescuingSonesNotification.add(sone);
604 notificationManager.addNotification(rescuingSonesNotification);
611 public void rescuedSone(Sone sone) {
612 rescuingSonesNotification.remove(sone);
613 sonesRescuedNotification.add(sone);
614 notificationManager.addNotification(sonesRescuedNotification);
621 public void newSoneFound(Sone sone) {
622 newSoneNotification.add(sone);
623 if (!hasFirstStartNotification()) {
624 notificationManager.addNotification(newSoneNotification);
632 public void newPostFound(Post post) {
633 newPostNotification.add(post);
634 if (!hasFirstStartNotification()) {
635 notificationManager.addNotification(newPostNotification);
637 getCore().markPostKnown(post);
645 public void newReplyFound(Reply reply) {
646 if (reply.getPost().getSone() == null) {
649 newReplyNotification.add(reply);
650 if (!hasFirstStartNotification()) {
651 notificationManager.addNotification(newReplyNotification);
653 getCore().markReplyKnown(reply);
661 public void markSoneKnown(Sone sone) {
662 newSoneNotification.remove(sone);
669 public void markPostKnown(Post post) {
670 newPostNotification.remove(post);
677 public void markReplyKnown(Reply reply) {
678 newReplyNotification.remove(reply);
685 public void postRemoved(Post post) {
686 newPostNotification.remove(post);
693 public void replyRemoved(Reply reply) {
694 newReplyNotification.remove(reply);
701 public void soneLocked(final Sone sone) {
702 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
705 @SuppressWarnings("synthetic-access")
707 lockedSonesNotification.add(sone);
708 lockedSonesTickerObjects.remove(sone);
709 notificationManager.addNotification(lockedSonesNotification);
711 }, "Sone Locked Notification");
712 lockedSonesTickerObjects.put(sone, tickerObject);
719 public void soneUnlocked(Sone sone) {
720 lockedSonesNotification.remove(sone);
721 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
728 public void updateFound(Version version, long releaseTime) {
729 newVersionNotification.set("version", version);
730 newVersionNotification.set("releaseTime", releaseTime);
731 notificationManager.addNotification(newVersionNotification);
735 * Template provider implementation that uses
736 * {@link WebInterface#createReader(String)} to load templates for
739 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
741 private class ClassPathTemplateProvider implements TemplateProvider {
743 /** The template factory. */
744 @SuppressWarnings("hiding")
745 private final TemplateFactory templateFactory;
748 * Creates a new template provider that locates templates on the
751 * @param templateFactory
752 * The template factory to create the templates
754 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
755 this.templateFactory = templateFactory;
762 @SuppressWarnings("synthetic-access")
763 public Template getTemplate(String templateName) {
764 Reader templateReader = createReader("/templates/" + templateName);
765 if (templateReader == null) {
768 Template template = templateFactory.createTemplate(templateReader);
771 } catch (TemplateException te1) {
772 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);