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.HttpRequestAccessor;
50 import net.pterodactylus.sone.template.IdentityAccessor;
51 import net.pterodactylus.sone.template.JavascriptFilter;
52 import net.pterodactylus.sone.template.NotificationManagerAccessor;
53 import net.pterodactylus.sone.template.ParserFilter;
54 import net.pterodactylus.sone.template.PostAccessor;
55 import net.pterodactylus.sone.template.ReplyAccessor;
56 import net.pterodactylus.sone.template.RequestChangeFilter;
57 import net.pterodactylus.sone.template.SoneAccessor;
58 import net.pterodactylus.sone.template.SubstringFilter;
59 import net.pterodactylus.sone.template.TrustAccessor;
60 import net.pterodactylus.sone.template.UnknownDateFilter;
61 import net.pterodactylus.sone.web.ajax.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.GetPostAjaxPage;
73 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
74 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
75 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
76 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
77 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
78 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
79 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
80 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
81 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
82 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
83 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
84 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
85 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
86 import net.pterodactylus.sone.web.page.PageToadlet;
87 import net.pterodactylus.sone.web.page.PageToadletFactory;
88 import net.pterodactylus.sone.web.page.StaticPage;
89 import net.pterodactylus.sone.web.page.TemplatePage;
90 import net.pterodactylus.util.cache.Cache;
91 import net.pterodactylus.util.cache.CacheException;
92 import net.pterodactylus.util.cache.CacheItem;
93 import net.pterodactylus.util.cache.DefaultCacheItem;
94 import net.pterodactylus.util.cache.MemoryCache;
95 import net.pterodactylus.util.cache.ValueRetriever;
96 import net.pterodactylus.util.logging.Logging;
97 import net.pterodactylus.util.notify.Notification;
98 import net.pterodactylus.util.notify.NotificationManager;
99 import net.pterodactylus.util.notify.TemplateNotification;
100 import net.pterodactylus.util.template.CollectionSortFilter;
101 import net.pterodactylus.util.template.DateFilter;
102 import net.pterodactylus.util.template.FormatFilter;
103 import net.pterodactylus.util.template.HtmlFilter;
104 import net.pterodactylus.util.template.MatchFilter;
105 import net.pterodactylus.util.template.Provider;
106 import net.pterodactylus.util.template.ReflectionAccessor;
107 import net.pterodactylus.util.template.ReplaceFilter;
108 import net.pterodactylus.util.template.StoreFilter;
109 import net.pterodactylus.util.template.Template;
110 import net.pterodactylus.util.template.TemplateContext;
111 import net.pterodactylus.util.template.TemplateContextFactory;
112 import net.pterodactylus.util.template.TemplateException;
113 import net.pterodactylus.util.template.TemplateParser;
114 import net.pterodactylus.util.template.XmlFilter;
115 import net.pterodactylus.util.thread.Ticker;
116 import net.pterodactylus.util.version.Version;
117 import freenet.clients.http.SessionManager;
118 import freenet.clients.http.SessionManager.Session;
119 import freenet.clients.http.ToadletContainer;
120 import freenet.clients.http.ToadletContext;
121 import freenet.l10n.BaseL10n;
122 import freenet.support.api.HTTPRequest;
125 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
126 * references to l10n helpers.
128 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
130 public class WebInterface implements CoreListener {
133 private static final Logger logger = Logging.getLogger(WebInterface.class);
135 /** The notification manager. */
136 private final NotificationManager notificationManager = new NotificationManager();
138 /** The Sone plugin. */
139 private final SonePlugin sonePlugin;
141 /** The registered toadlets. */
142 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
144 /** The form password. */
145 private final String formPassword;
147 /** The template context factory. */
148 private final TemplateContextFactory templateContextFactory;
150 /** The “new Sone” notification. */
151 private final ListNotification<Sone> newSoneNotification;
153 /** The “new post” notification. */
154 private final ListNotification<Post> newPostNotification;
156 /** The “new reply” notification. */
157 private final ListNotification<Reply> newReplyNotification;
159 /** The “rescuing Sone” notification. */
160 private final ListNotification<Sone> rescuingSonesNotification;
162 /** The “Sone rescued” notification. */
163 private final ListNotification<Sone> sonesRescuedNotification;
165 /** Sone locked notification ticker objects. */
166 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
168 /** The “Sone locked” notification. */
169 private final ListNotification<Sone> lockedSonesNotification;
171 /** The “new version” notification. */
172 private final TemplateNotification newVersionNotification;
175 * Creates a new web interface.
180 @SuppressWarnings("synthetic-access")
181 public WebInterface(SonePlugin sonePlugin) {
182 this.sonePlugin = sonePlugin;
183 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
185 templateContextFactory = new TemplateContextFactory();
186 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
187 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
188 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
189 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
190 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
191 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
192 templateContextFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
193 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
194 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
195 templateContextFactory.addFilter("date", new DateFilter());
196 templateContextFactory.addFilter("html", new HtmlFilter());
197 templateContextFactory.addFilter("replace", new ReplaceFilter());
198 templateContextFactory.addFilter("store", new StoreFilter());
199 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
200 templateContextFactory.addFilter("substring", new SubstringFilter());
201 templateContextFactory.addFilter("xml", new XmlFilter());
202 templateContextFactory.addFilter("change", new RequestChangeFilter());
203 templateContextFactory.addFilter("match", new MatchFilter());
204 templateContextFactory.addFilter("css", new CssClassNameFilter());
205 templateContextFactory.addFilter("js", new JavascriptFilter());
206 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory));
207 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
208 templateContextFactory.addFilter("format", new FormatFilter());
209 templateContextFactory.addFilter("sort", new CollectionSortFilter());
210 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
211 templateContextFactory.addProvider(new ClassPathTemplateProvider());
212 templateContextFactory.addTemplateObject("formPassword", formPassword);
214 /* create notifications. */
215 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
216 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
218 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
219 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
221 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
222 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
224 Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html"));
225 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
227 Template sonesRescuedTemplate = TemplateParser.parse(createReader("/templates/notify/sonesRescuedNotification.html"));
228 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
230 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
231 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
233 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
234 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
242 * Returns the Sone core used by the Sone plugin.
244 * @return The Sone core
246 public Core getCore() {
247 return sonePlugin.core();
251 * Returns the template context factory of the web interface.
253 * @return The template context factory
255 public TemplateContextFactory getTemplateContextFactory() {
256 return templateContextFactory;
260 * Returns the current session, creating a new session if there is no
263 * @param toadletContenxt
264 * The toadlet context
265 * @return The current session, or {@code null} if there is no current
268 public Session getCurrentSession(ToadletContext toadletContenxt) {
269 return getCurrentSession(toadletContenxt, true);
273 * Returns the current session, creating a new session if there is no
274 * current session and {@code create} is {@code true}.
276 * @param toadletContenxt
277 * The toadlet context
279 * {@code true} to create a new session if there is no current
280 * session, {@code false} otherwise
281 * @return The current session, or {@code null} if there is no current
284 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
285 Session session = getSessionManager().useSession(toadletContenxt);
286 if (create && (session == null)) {
287 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
293 * Returns the currently logged in Sone.
295 * @param toadletContext
296 * The toadlet context
297 * @return The currently logged in Sone, or {@code null} if no Sone is
298 * currently logged in
300 public Sone getCurrentSone(ToadletContext toadletContext) {
301 return getCurrentSone(toadletContext, true);
305 * Returns the currently logged in Sone.
307 * @param toadletContext
308 * The toadlet context
310 * {@code true} to create a new session if no session exists,
311 * {@code false} to not create a new session
312 * @return The currently logged in Sone, or {@code null} if no Sone is
313 * currently logged in
315 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
316 Set<Sone> localSones = getCore().getLocalSones();
317 if (localSones.size() == 1) {
318 return localSones.iterator().next();
320 return getCurrentSone(getCurrentSession(toadletContext, create));
324 * Returns the currently logged in Sone.
328 * @return The currently logged in Sone, or {@code null} if no Sone is
329 * currently logged in
331 public Sone getCurrentSone(Session session) {
332 if (session == null) {
335 String soneId = (String) session.getAttribute("Sone.CurrentSone");
336 if (soneId == null) {
339 return getCore().getLocalSone(soneId, false);
343 * Sets the currently logged in Sone.
345 * @param toadletContext
346 * The toadlet context
348 * The Sone to set as currently logged in
350 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
351 Session session = getCurrentSession(toadletContext);
353 session.removeAttribute("Sone.CurrentSone");
355 session.setAttribute("Sone.CurrentSone", sone.getId());
360 * Returns the notification manager.
362 * @return The notification manager
364 public NotificationManager getNotifications() {
365 return notificationManager;
369 * Returns the l10n helper of the node.
371 * @return The node’s l10n helper
373 public BaseL10n getL10n() {
374 return sonePlugin.l10n().getBase();
378 * Returns the session manager of the node.
380 * @return The node’s session manager
382 public SessionManager getSessionManager() {
383 return sonePlugin.pluginRespirator().getSessionManager("Sone");
387 * Returns the node’s form password.
389 * @return The form password
391 public String getFormPassword() {
396 * Returns the posts that have been announced as new in the
397 * {@link #newPostNotification}.
399 * @return The new posts
401 public Set<Post> getNewPosts() {
402 return new HashSet<Post>(newPostNotification.getElements());
406 * Returns the replies that have been announced as new in the
407 * {@link #newReplyNotification}.
409 * @return The new replies
411 public Set<Reply> getNewReplies() {
412 return new HashSet<Reply>(newReplyNotification.getElements());
416 * Sets whether the current start of the plugin is the first start. It is
417 * considered a first start if the configuration file does not exist.
420 * {@code true} if no configuration file existed when Sone was
421 * loaded, {@code false} otherwise
423 public void setFirstStart(boolean firstStart) {
425 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
426 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
427 notificationManager.addNotification(firstStartNotification);
432 * Sets whether Sone was started with a fresh configuration file.
435 * {@code true} if Sone was started with a fresh configuration,
436 * {@code false} if the existing configuration could be read
438 public void setNewConfig(boolean newConfig) {
439 if (newConfig && !hasFirstStartNotification()) {
440 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
441 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
442 notificationManager.addNotification(configNotReadNotification);
451 * Returns whether the first start notification is currently displayed.
453 * @return {@code true} if the first-start notification is currently
454 * displayed, {@code false} otherwise
456 private boolean hasFirstStartNotification() {
457 return notificationManager.getNotification("first-start-notification") != null;
465 * Starts the web interface and registers all toadlets.
467 public void start() {
470 /* notification templates. */
471 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
473 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
474 notificationManager.addNotification(startupNotification);
476 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
480 startupNotification.dismiss();
482 }, "Sone Startup Notification Remover");
484 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
485 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
486 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
489 @SuppressWarnings("synthetic-access")
491 if (getCore().getIdentityManager().isConnected()) {
492 wotMissingNotification.dismiss();
494 notificationManager.addNotification(wotMissingNotification);
496 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
499 }, "Sone WoT Connector Checker");
503 * Stops the web interface and unregisters all toadlets.
506 unregisterToadlets();
507 Ticker.getInstance().stop();
515 * Register all toadlets.
517 private void registerToadlets() {
518 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
519 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
520 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
521 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
522 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
523 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
524 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
525 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
526 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
527 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
528 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
529 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
530 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
531 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
532 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
533 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
534 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
535 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
536 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
537 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
538 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
539 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
540 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
541 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
543 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
544 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
545 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
546 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
547 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
548 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
549 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
550 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
551 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
552 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
553 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
554 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
555 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
556 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
557 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
558 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
559 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
560 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
561 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
562 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
563 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
564 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
565 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
566 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
567 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
568 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
569 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
570 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
571 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
572 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
573 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
574 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
575 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
576 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
577 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
578 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
579 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
580 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
581 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
582 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
583 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
584 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
585 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
586 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
587 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
588 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
589 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
590 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
591 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
592 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
593 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
594 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
595 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
596 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
597 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
598 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
599 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
600 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
601 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
602 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
603 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
604 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
605 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
608 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
609 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
610 for (PageToadlet toadlet : pageToadlets) {
611 String menuName = toadlet.getMenuName();
612 if (menuName != null) {
613 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
615 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
621 * Unregisters all toadlets.
623 private void unregisterToadlets() {
624 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
625 for (PageToadlet pageToadlet : pageToadlets) {
626 toadletContainer.unregister(pageToadlet);
628 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
632 * Creates a {@link Reader} from the {@link InputStream} for the resource
633 * with the given name.
635 * @param resourceName
636 * The name of the resource
637 * @return A {@link Reader} for the resource
639 private Reader createReader(String resourceName) {
641 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
642 } catch (UnsupportedEncodingException uee1) {
643 System.out.println(" fail.");
649 // CORELISTENER METHODS
656 public void rescuingSone(Sone sone) {
657 rescuingSonesNotification.add(sone);
658 notificationManager.addNotification(rescuingSonesNotification);
665 public void rescuedSone(Sone sone) {
666 rescuingSonesNotification.remove(sone);
667 sonesRescuedNotification.add(sone);
668 notificationManager.addNotification(sonesRescuedNotification);
675 public void newSoneFound(Sone sone) {
676 newSoneNotification.add(sone);
677 if (!hasFirstStartNotification()) {
678 notificationManager.addNotification(newSoneNotification);
686 public void newPostFound(Post post) {
687 newPostNotification.add(post);
688 if (!hasFirstStartNotification()) {
689 notificationManager.addNotification(newPostNotification);
691 getCore().markPostKnown(post);
699 public void newReplyFound(Reply reply) {
700 if (reply.getPost().getSone() == null) {
703 newReplyNotification.add(reply);
704 if (!hasFirstStartNotification()) {
705 notificationManager.addNotification(newReplyNotification);
707 getCore().markReplyKnown(reply);
715 public void markSoneKnown(Sone sone) {
716 newSoneNotification.remove(sone);
723 public void markPostKnown(Post post) {
724 newPostNotification.remove(post);
731 public void markReplyKnown(Reply reply) {
732 newReplyNotification.remove(reply);
739 public void postRemoved(Post post) {
740 newPostNotification.remove(post);
747 public void replyRemoved(Reply reply) {
748 newReplyNotification.remove(reply);
755 public void soneLocked(final Sone sone) {
756 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
759 @SuppressWarnings("synthetic-access")
761 lockedSonesNotification.add(sone);
762 lockedSonesTickerObjects.remove(sone);
763 notificationManager.addNotification(lockedSonesNotification);
765 }, "Sone Locked Notification");
766 lockedSonesTickerObjects.put(sone, tickerObject);
773 public void soneUnlocked(Sone sone) {
774 lockedSonesNotification.remove(sone);
775 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
782 public void updateFound(Version version, long releaseTime, long latestEdition) {
783 newVersionNotification.getTemplateContext().set("latestVersion", version);
784 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
785 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
786 notificationManager.addNotification(newVersionNotification);
790 * Template provider implementation that uses
791 * {@link WebInterface#createReader(String)} to load templates for
794 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
796 private class ClassPathTemplateProvider implements Provider {
798 /** Cache for templates. */
799 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
802 @SuppressWarnings("synthetic-access")
803 public CacheItem<Template> retrieve(String key) throws CacheException {
804 Template template = findTemplate(key);
805 if (template != null) {
806 return new DefaultCacheItem<Template>(template);
816 @SuppressWarnings("synthetic-access")
817 public Template getTemplate(TemplateContext templateContext, String templateName) {
819 return templateCache.get(templateName);
820 } catch (CacheException ce1) {
821 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
827 * Locates a template in the class path.
829 * @param templateName
830 * The name of the template to load
831 * @return The loaded template, or {@code null} if no template could be
834 @SuppressWarnings("synthetic-access")
835 private Template findTemplate(String templateName) {
836 Reader templateReader = createReader("/templates/" + templateName);
837 if (templateReader == null) {
840 Template template = null;
842 template = TemplateParser.parse(templateReader);
843 } catch (TemplateException te1) {
844 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);