2 * Sone - 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.HttpRequestAccessor;
50 import net.pterodactylus.sone.template.IdentityAccessor;
51 import net.pterodactylus.sone.template.JavascriptFilter;
52 import net.pterodactylus.sone.template.ParserFilter;
53 import net.pterodactylus.sone.template.PostAccessor;
54 import net.pterodactylus.sone.template.ReplyAccessor;
55 import net.pterodactylus.sone.template.ReplyGroupFilter;
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.BookmarkAjaxPage;
62 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
63 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
64 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
65 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
66 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
67 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
68 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
69 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
70 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
71 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
72 import net.pterodactylus.sone.web.ajax.GetNotificationAjaxPage;
73 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
74 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
75 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
76 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
77 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
78 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
79 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
80 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
81 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
82 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
83 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
84 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
85 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
86 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
87 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
88 import net.pterodactylus.sone.web.page.PageToadlet;
89 import net.pterodactylus.sone.web.page.PageToadletFactory;
90 import net.pterodactylus.sone.web.page.RedirectPage;
91 import net.pterodactylus.sone.web.page.StaticPage;
92 import net.pterodactylus.sone.web.page.TemplatePage;
93 import net.pterodactylus.util.cache.Cache;
94 import net.pterodactylus.util.cache.CacheException;
95 import net.pterodactylus.util.cache.CacheItem;
96 import net.pterodactylus.util.cache.DefaultCacheItem;
97 import net.pterodactylus.util.cache.MemoryCache;
98 import net.pterodactylus.util.cache.ValueRetriever;
99 import net.pterodactylus.util.logging.Logging;
100 import net.pterodactylus.util.notify.Notification;
101 import net.pterodactylus.util.notify.NotificationManager;
102 import net.pterodactylus.util.notify.TemplateNotification;
103 import net.pterodactylus.util.template.CollectionSortFilter;
104 import net.pterodactylus.util.template.ContainsFilter;
105 import net.pterodactylus.util.template.DateFilter;
106 import net.pterodactylus.util.template.FormatFilter;
107 import net.pterodactylus.util.template.HtmlFilter;
108 import net.pterodactylus.util.template.MatchFilter;
109 import net.pterodactylus.util.template.Provider;
110 import net.pterodactylus.util.template.ReflectionAccessor;
111 import net.pterodactylus.util.template.ReplaceFilter;
112 import net.pterodactylus.util.template.StoreFilter;
113 import net.pterodactylus.util.template.Template;
114 import net.pterodactylus.util.template.TemplateContext;
115 import net.pterodactylus.util.template.TemplateContextFactory;
116 import net.pterodactylus.util.template.TemplateException;
117 import net.pterodactylus.util.template.TemplateParser;
118 import net.pterodactylus.util.template.XmlFilter;
119 import net.pterodactylus.util.thread.Ticker;
120 import net.pterodactylus.util.version.Version;
121 import freenet.clients.http.SessionManager;
122 import freenet.clients.http.SessionManager.Session;
123 import freenet.clients.http.ToadletContainer;
124 import freenet.clients.http.ToadletContext;
125 import freenet.l10n.BaseL10n;
126 import freenet.support.api.HTTPRequest;
129 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
130 * references to l10n helpers.
132 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
134 public class WebInterface implements CoreListener {
137 private static final Logger logger = Logging.getLogger(WebInterface.class);
139 /** The notification manager. */
140 private final NotificationManager notificationManager = new NotificationManager();
142 /** The Sone plugin. */
143 private final SonePlugin sonePlugin;
145 /** The registered toadlets. */
146 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
148 /** The form password. */
149 private final String formPassword;
151 /** The template context factory. */
152 private final TemplateContextFactory templateContextFactory;
154 /** The “new Sone” notification. */
155 private final ListNotification<Sone> newSoneNotification;
157 /** The “new post” notification. */
158 private final ListNotification<Post> newPostNotification;
160 /** The “new reply” notification. */
161 private final ListNotification<Reply> newReplyNotification;
163 /** The “rescuing Sone” notification. */
164 private final ListNotification<Sone> rescuingSonesNotification;
166 /** The “Sone rescued” notification. */
167 private final ListNotification<Sone> sonesRescuedNotification;
169 /** Sone locked notification ticker objects. */
170 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
172 /** The “Sone locked” notification. */
173 private final ListNotification<Sone> lockedSonesNotification;
175 /** The “new version” notification. */
176 private final TemplateNotification newVersionNotification;
179 * Creates a new web interface.
184 @SuppressWarnings("synthetic-access")
185 public WebInterface(SonePlugin sonePlugin) {
186 this.sonePlugin = sonePlugin;
187 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
189 templateContextFactory = new TemplateContextFactory();
190 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
191 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
192 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
193 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
194 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
195 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
196 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
197 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
198 templateContextFactory.addFilter("date", new DateFilter());
199 templateContextFactory.addFilter("html", new HtmlFilter());
200 templateContextFactory.addFilter("replace", new ReplaceFilter());
201 templateContextFactory.addFilter("store", new StoreFilter());
202 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
203 templateContextFactory.addFilter("substring", new SubstringFilter());
204 templateContextFactory.addFilter("xml", new XmlFilter());
205 templateContextFactory.addFilter("change", new RequestChangeFilter());
206 templateContextFactory.addFilter("match", new MatchFilter());
207 templateContextFactory.addFilter("css", new CssClassNameFilter());
208 templateContextFactory.addFilter("js", new JavascriptFilter());
209 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory));
210 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
211 templateContextFactory.addFilter("format", new FormatFilter());
212 templateContextFactory.addFilter("sort", new CollectionSortFilter());
213 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
214 templateContextFactory.addFilter("in", new ContainsFilter());
215 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
216 templateContextFactory.addProvider(new ClassPathTemplateProvider());
217 templateContextFactory.addTemplateObject("webInterface", this);
218 templateContextFactory.addTemplateObject("formPassword", formPassword);
220 /* create notifications. */
221 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
222 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
224 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
225 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
227 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
228 newReplyNotification = new ListNotification<Reply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
230 Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html"));
231 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
233 Template sonesRescuedTemplate = TemplateParser.parse(createReader("/templates/notify/sonesRescuedNotification.html"));
234 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
236 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
237 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
239 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
240 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
248 * Returns the Sone core used by the Sone plugin.
250 * @return The Sone core
252 public Core getCore() {
253 return sonePlugin.core();
257 * Returns the template context factory of the web interface.
259 * @return The template context factory
261 public TemplateContextFactory getTemplateContextFactory() {
262 return templateContextFactory;
266 * Returns the current session, creating a new session if there is no
269 * @param toadletContenxt
270 * The toadlet context
271 * @return The current session, or {@code null} if there is no current
274 public Session getCurrentSession(ToadletContext toadletContenxt) {
275 return getCurrentSession(toadletContenxt, true);
279 * Returns the current session, creating a new session if there is no
280 * current session and {@code create} is {@code true}.
282 * @param toadletContenxt
283 * The toadlet context
285 * {@code true} to create a new session if there is no current
286 * session, {@code false} otherwise
287 * @return The current session, or {@code null} if there is no current
290 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
291 Session session = getSessionManager().useSession(toadletContenxt);
292 if (create && (session == null)) {
293 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
299 * Returns the currently logged in Sone.
301 * @param toadletContext
302 * The toadlet context
303 * @return The currently logged in Sone, or {@code null} if no Sone is
304 * currently logged in
306 public Sone getCurrentSone(ToadletContext toadletContext) {
307 return getCurrentSone(toadletContext, true);
311 * Returns the currently logged in Sone.
313 * @param toadletContext
314 * The toadlet context
316 * {@code true} to create a new session if no session exists,
317 * {@code false} to not create a new session
318 * @return The currently logged in Sone, or {@code null} if no Sone is
319 * currently logged in
321 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
322 Set<Sone> localSones = getCore().getLocalSones();
323 if (localSones.size() == 1) {
324 return localSones.iterator().next();
326 return getCurrentSone(getCurrentSession(toadletContext, create));
330 * Returns the currently logged in Sone.
334 * @return The currently logged in Sone, or {@code null} if no Sone is
335 * currently logged in
337 public Sone getCurrentSone(Session session) {
338 if (session == null) {
341 String soneId = (String) session.getAttribute("Sone.CurrentSone");
342 if (soneId == null) {
345 return getCore().getLocalSone(soneId, false);
349 * Sets the currently logged in Sone.
351 * @param toadletContext
352 * The toadlet context
354 * The Sone to set as currently logged in
356 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
357 Session session = getCurrentSession(toadletContext);
359 session.removeAttribute("Sone.CurrentSone");
361 session.setAttribute("Sone.CurrentSone", sone.getId());
366 * Returns the notification manager.
368 * @return The notification manager
370 public NotificationManager getNotifications() {
371 return notificationManager;
375 * Returns the l10n helper of the node.
377 * @return The node’s l10n helper
379 public BaseL10n getL10n() {
380 return sonePlugin.l10n().getBase();
384 * Returns the session manager of the node.
386 * @return The node’s session manager
388 public SessionManager getSessionManager() {
389 return sonePlugin.pluginRespirator().getSessionManager("Sone");
393 * Returns the node’s form password.
395 * @return The form password
397 public String getFormPassword() {
402 * Returns the posts that have been announced as new in the
403 * {@link #newPostNotification}.
405 * @return The new posts
407 public Set<Post> getNewPosts() {
408 return new HashSet<Post>(newPostNotification.getElements());
412 * Returns the replies that have been announced as new in the
413 * {@link #newReplyNotification}.
415 * @return The new replies
417 public Set<Reply> getNewReplies() {
418 return new HashSet<Reply>(newReplyNotification.getElements());
422 * Sets whether the current start of the plugin is the first start. It is
423 * considered a first start if the configuration file does not exist.
426 * {@code true} if no configuration file existed when Sone was
427 * loaded, {@code false} otherwise
429 public void setFirstStart(boolean firstStart) {
431 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
432 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
433 notificationManager.addNotification(firstStartNotification);
438 * Sets whether Sone was started with a fresh configuration file.
441 * {@code true} if Sone was started with a fresh configuration,
442 * {@code false} if the existing configuration could be read
444 public void setNewConfig(boolean newConfig) {
445 if (newConfig && !hasFirstStartNotification()) {
446 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
447 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
448 notificationManager.addNotification(configNotReadNotification);
457 * Returns whether the first start notification is currently displayed.
459 * @return {@code true} if the first-start notification is currently
460 * displayed, {@code false} otherwise
462 private boolean hasFirstStartNotification() {
463 return notificationManager.getNotification("first-start-notification") != null;
471 * Starts the web interface and registers all toadlets.
473 public void start() {
476 /* notification templates. */
477 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
479 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
480 notificationManager.addNotification(startupNotification);
482 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
486 startupNotification.dismiss();
488 }, "Sone Startup Notification Remover");
490 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
491 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
492 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
495 @SuppressWarnings("synthetic-access")
497 if (getCore().getIdentityManager().isConnected()) {
498 wotMissingNotification.dismiss();
500 notificationManager.addNotification(wotMissingNotification);
502 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
505 }, "Sone WoT Connector Checker");
509 * Stops the web interface and unregisters all toadlets.
512 unregisterToadlets();
513 Ticker.getInstance().stop();
521 * Register all toadlets.
523 private void registerToadlets() {
524 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
525 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
526 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
527 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
528 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
529 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
530 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
531 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
532 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
533 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
534 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
535 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
536 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
537 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
538 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
539 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
540 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
541 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
542 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
543 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
544 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
545 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
546 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
547 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
549 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage("", "index.html")));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
554 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
555 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
556 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
557 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
558 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
559 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
560 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
561 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
562 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
563 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
564 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
565 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
566 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
567 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
568 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
569 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
570 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
571 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
572 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
573 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
574 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
575 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
576 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
577 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
578 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
579 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
580 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
581 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
582 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
583 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
584 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
585 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
586 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
587 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
588 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
589 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
590 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
591 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationAjaxPage(this)));
592 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
593 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
594 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
595 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
596 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
597 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
598 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
599 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
600 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
601 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
602 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
603 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
604 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
605 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
607 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
608 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
609 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
610 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
611 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
612 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
617 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
618 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
619 for (PageToadlet toadlet : pageToadlets) {
620 String menuName = toadlet.getMenuName();
621 if (menuName != null) {
622 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
624 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
630 * Unregisters all toadlets.
632 private void unregisterToadlets() {
633 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
634 for (PageToadlet pageToadlet : pageToadlets) {
635 toadletContainer.unregister(pageToadlet);
637 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
641 * Creates a {@link Reader} from the {@link InputStream} for the resource
642 * with the given name.
644 * @param resourceName
645 * The name of the resource
646 * @return A {@link Reader} for the resource
648 private Reader createReader(String resourceName) {
650 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
651 } catch (UnsupportedEncodingException uee1) {
652 System.out.println(" fail.");
658 // CORELISTENER METHODS
665 public void rescuingSone(Sone sone) {
666 rescuingSonesNotification.add(sone);
667 notificationManager.addNotification(rescuingSonesNotification);
674 public void rescuedSone(Sone sone) {
675 rescuingSonesNotification.remove(sone);
676 sonesRescuedNotification.add(sone);
677 notificationManager.addNotification(sonesRescuedNotification);
684 public void newSoneFound(Sone sone) {
685 newSoneNotification.add(sone);
686 if (!hasFirstStartNotification()) {
687 notificationManager.addNotification(newSoneNotification);
695 public void newPostFound(Post post) {
696 newPostNotification.add(post);
697 if (!hasFirstStartNotification()) {
698 notificationManager.addNotification(newPostNotification);
700 getCore().markPostKnown(post);
708 public void newReplyFound(Reply reply) {
709 if (reply.getPost().getSone() == null) {
712 newReplyNotification.add(reply);
713 if (!hasFirstStartNotification()) {
714 notificationManager.addNotification(newReplyNotification);
716 getCore().markReplyKnown(reply);
724 public void markSoneKnown(Sone sone) {
725 newSoneNotification.remove(sone);
732 public void markPostKnown(Post post) {
733 newPostNotification.remove(post);
740 public void markReplyKnown(Reply reply) {
741 newReplyNotification.remove(reply);
748 public void soneRemoved(Sone sone) {
749 newSoneNotification.remove(sone);
756 public void postRemoved(Post post) {
757 newPostNotification.remove(post);
764 public void replyRemoved(Reply reply) {
765 newReplyNotification.remove(reply);
772 public void soneLocked(final Sone sone) {
773 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
776 @SuppressWarnings("synthetic-access")
778 lockedSonesNotification.add(sone);
779 lockedSonesTickerObjects.remove(sone);
780 notificationManager.addNotification(lockedSonesNotification);
782 }, "Sone Locked Notification");
783 lockedSonesTickerObjects.put(sone, tickerObject);
790 public void soneUnlocked(Sone sone) {
791 lockedSonesNotification.remove(sone);
792 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
799 public void updateFound(Version version, long releaseTime, long latestEdition) {
800 newVersionNotification.getTemplateContext().set("latestVersion", version);
801 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
802 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
803 notificationManager.addNotification(newVersionNotification);
807 * Template provider implementation that uses
808 * {@link WebInterface#createReader(String)} to load templates for
811 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
813 private class ClassPathTemplateProvider implements Provider {
815 /** Cache for templates. */
816 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
819 @SuppressWarnings("synthetic-access")
820 public CacheItem<Template> retrieve(String key) throws CacheException {
821 Template template = findTemplate(key);
822 if (template != null) {
823 return new DefaultCacheItem<Template>(template);
833 @SuppressWarnings("synthetic-access")
834 public Template getTemplate(TemplateContext templateContext, String templateName) {
836 return templateCache.get(templateName);
837 } catch (CacheException ce1) {
838 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
844 * Locates a template in the class path.
846 * @param templateName
847 * The name of the template to load
848 * @return The loaded template, or {@code null} if no template could be
851 @SuppressWarnings("synthetic-access")
852 private Template findTemplate(String templateName) {
853 Reader templateReader = createReader("/templates/" + templateName);
854 if (templateReader == null) {
857 Template template = null;
859 template = TemplateParser.parse(templateReader);
860 } catch (TemplateException te1) {
861 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);