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.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.UUID;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import net.pterodactylus.sone.core.Core;
39 import net.pterodactylus.sone.core.CoreListener;
40 import net.pterodactylus.sone.data.Post;
41 import net.pterodactylus.sone.data.Reply;
42 import net.pterodactylus.sone.data.Sone;
43 import net.pterodactylus.sone.freenet.L10nFilter;
44 import net.pterodactylus.sone.freenet.wot.Identity;
45 import net.pterodactylus.sone.freenet.wot.Trust;
46 import net.pterodactylus.sone.main.SonePlugin;
47 import net.pterodactylus.sone.notify.ListNotification;
48 import net.pterodactylus.sone.template.CollectionAccessor;
49 import net.pterodactylus.sone.template.CssClassNameFilter;
50 import net.pterodactylus.sone.template.HttpRequestAccessor;
51 import net.pterodactylus.sone.template.IdentityAccessor;
52 import net.pterodactylus.sone.template.JavascriptFilter;
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.ReplyGroupFilter;
57 import net.pterodactylus.sone.template.RequestChangeFilter;
58 import net.pterodactylus.sone.template.SoneAccessor;
59 import net.pterodactylus.sone.template.SubstringFilter;
60 import net.pterodactylus.sone.template.TrustAccessor;
61 import net.pterodactylus.sone.template.UniqueElementFilter;
62 import net.pterodactylus.sone.template.UnknownDateFilter;
63 import net.pterodactylus.sone.text.Part;
64 import net.pterodactylus.sone.text.SonePart;
65 import net.pterodactylus.sone.text.SoneTextParser;
66 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
67 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
68 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
69 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
70 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
71 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
72 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
73 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
74 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
75 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
76 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
77 import net.pterodactylus.sone.web.ajax.GetNotificationAjaxPage;
78 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
79 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
80 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
81 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
82 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
83 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
84 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
85 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
86 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
87 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
88 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
89 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
90 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
91 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
92 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
93 import net.pterodactylus.sone.web.page.PageToadlet;
94 import net.pterodactylus.sone.web.page.PageToadletFactory;
95 import net.pterodactylus.sone.web.page.RedirectPage;
96 import net.pterodactylus.sone.web.page.StaticPage;
97 import net.pterodactylus.sone.web.page.TemplatePage;
98 import net.pterodactylus.util.cache.Cache;
99 import net.pterodactylus.util.cache.CacheException;
100 import net.pterodactylus.util.cache.CacheItem;
101 import net.pterodactylus.util.cache.DefaultCacheItem;
102 import net.pterodactylus.util.cache.MemoryCache;
103 import net.pterodactylus.util.cache.ValueRetriever;
104 import net.pterodactylus.util.collection.SetBuilder;
105 import net.pterodactylus.util.filter.Filters;
106 import net.pterodactylus.util.logging.Logging;
107 import net.pterodactylus.util.notify.Notification;
108 import net.pterodactylus.util.notify.NotificationManager;
109 import net.pterodactylus.util.notify.TemplateNotification;
110 import net.pterodactylus.util.template.CollectionSortFilter;
111 import net.pterodactylus.util.template.ContainsFilter;
112 import net.pterodactylus.util.template.DateFilter;
113 import net.pterodactylus.util.template.FormatFilter;
114 import net.pterodactylus.util.template.HtmlFilter;
115 import net.pterodactylus.util.template.MatchFilter;
116 import net.pterodactylus.util.template.Provider;
117 import net.pterodactylus.util.template.ReflectionAccessor;
118 import net.pterodactylus.util.template.ReplaceFilter;
119 import net.pterodactylus.util.template.StoreFilter;
120 import net.pterodactylus.util.template.Template;
121 import net.pterodactylus.util.template.TemplateContext;
122 import net.pterodactylus.util.template.TemplateContextFactory;
123 import net.pterodactylus.util.template.TemplateException;
124 import net.pterodactylus.util.template.TemplateParser;
125 import net.pterodactylus.util.template.XmlFilter;
126 import net.pterodactylus.util.thread.Ticker;
127 import net.pterodactylus.util.version.Version;
128 import freenet.clients.http.SessionManager;
129 import freenet.clients.http.SessionManager.Session;
130 import freenet.clients.http.ToadletContainer;
131 import freenet.clients.http.ToadletContext;
132 import freenet.l10n.BaseL10n;
133 import freenet.support.api.HTTPRequest;
136 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
137 * references to l10n helpers.
139 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
141 public class WebInterface implements CoreListener {
144 private static final Logger logger = Logging.getLogger(WebInterface.class);
146 /** The notification manager. */
147 private final NotificationManager notificationManager = new NotificationManager();
149 /** The Sone plugin. */
150 private final SonePlugin sonePlugin;
152 /** The registered toadlets. */
153 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
155 /** The form password. */
156 private final String formPassword;
158 /** The template context factory. */
159 private final TemplateContextFactory templateContextFactory;
161 /** The Sone text parser. */
162 private final SoneTextParser soneTextParser;
164 /** The “new Sone” notification. */
165 private final ListNotification<Sone> newSoneNotification;
167 /** The “new post” notification. */
168 private final ListNotification<Post> newPostNotification;
170 /** The “new reply” notification. */
171 private final ListNotification<Reply> newReplyNotification;
173 /** The invisible “local post” notification. */
174 private final ListNotification<Post> localPostNotification;
176 /** The invisible “local reply” notification. */
177 private final ListNotification<Reply> localReplyNotification;
179 /** The “you have been mentioned” notification. */
180 private final ListNotification<Post> mentionNotification;
182 /** Notifications for sone inserts. */
183 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
185 /** Sone locked notification ticker objects. */
186 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
188 /** The “Sone locked” notification. */
189 private final ListNotification<Sone> lockedSonesNotification;
191 /** The “new version” notification. */
192 private final TemplateNotification newVersionNotification;
195 * Creates a new web interface.
200 @SuppressWarnings("synthetic-access")
201 public WebInterface(SonePlugin sonePlugin) {
202 this.sonePlugin = sonePlugin;
203 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
204 soneTextParser = new SoneTextParser(getCore(), getCore());
206 templateContextFactory = new TemplateContextFactory();
207 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
208 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
209 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
210 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
211 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
212 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
213 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
214 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
215 templateContextFactory.addFilter("date", new DateFilter());
216 templateContextFactory.addFilter("html", new HtmlFilter());
217 templateContextFactory.addFilter("replace", new ReplaceFilter());
218 templateContextFactory.addFilter("store", new StoreFilter());
219 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
220 templateContextFactory.addFilter("substring", new SubstringFilter());
221 templateContextFactory.addFilter("xml", new XmlFilter());
222 templateContextFactory.addFilter("change", new RequestChangeFilter());
223 templateContextFactory.addFilter("match", new MatchFilter());
224 templateContextFactory.addFilter("css", new CssClassNameFilter());
225 templateContextFactory.addFilter("js", new JavascriptFilter());
226 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory, soneTextParser));
227 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
228 templateContextFactory.addFilter("format", new FormatFilter());
229 templateContextFactory.addFilter("sort", new CollectionSortFilter());
230 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
231 templateContextFactory.addFilter("in", new ContainsFilter());
232 templateContextFactory.addFilter("unique", new UniqueElementFilter());
233 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
234 templateContextFactory.addProvider(new ClassPathTemplateProvider());
235 templateContextFactory.addTemplateObject("webInterface", this);
236 templateContextFactory.addTemplateObject("formPassword", formPassword);
238 /* create notifications. */
239 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
240 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
242 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
243 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
245 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
246 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
248 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
249 newReplyNotification = new ListNotification<Reply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
251 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
252 localReplyNotification = new ListNotification<Reply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
254 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
255 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
257 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
258 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
260 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
261 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
269 * Returns the Sone core used by the Sone plugin.
271 * @return The Sone core
273 public Core getCore() {
274 return sonePlugin.core();
278 * Returns the template context factory of the web interface.
280 * @return The template context factory
282 public TemplateContextFactory getTemplateContextFactory() {
283 return templateContextFactory;
287 * Returns the current session, creating a new session if there is no
290 * @param toadletContenxt
291 * The toadlet context
292 * @return The current session, or {@code null} if there is no current
295 public Session getCurrentSession(ToadletContext toadletContenxt) {
296 return getCurrentSession(toadletContenxt, true);
300 * Returns the current session, creating a new session if there is no
301 * current session and {@code create} is {@code true}.
303 * @param toadletContenxt
304 * The toadlet context
306 * {@code true} to create a new session if there is no current
307 * session, {@code false} otherwise
308 * @return The current session, or {@code null} if there is no current
311 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
312 Session session = getSessionManager().useSession(toadletContenxt);
313 if (create && (session == null)) {
314 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
320 * Returns the currently logged in Sone.
322 * @param toadletContext
323 * The toadlet context
324 * @return The currently logged in Sone, or {@code null} if no Sone is
325 * currently logged in
327 public Sone getCurrentSone(ToadletContext toadletContext) {
328 return getCurrentSone(toadletContext, true);
332 * Returns the currently logged in Sone.
334 * @param toadletContext
335 * The toadlet context
337 * {@code true} to create a new session if no session exists,
338 * {@code false} to not create a new session
339 * @return The currently logged in Sone, or {@code null} if no Sone is
340 * currently logged in
342 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
343 Set<Sone> localSones = getCore().getLocalSones();
344 if (localSones.size() == 1) {
345 return localSones.iterator().next();
347 return getCurrentSone(getCurrentSession(toadletContext, create));
351 * Returns the currently logged in Sone.
355 * @return The currently logged in Sone, or {@code null} if no Sone is
356 * currently logged in
358 public Sone getCurrentSone(Session session) {
359 if (session == null) {
362 String soneId = (String) session.getAttribute("Sone.CurrentSone");
363 if (soneId == null) {
366 return getCore().getLocalSone(soneId, false);
370 * Sets the currently logged in Sone.
372 * @param toadletContext
373 * The toadlet context
375 * The Sone to set as currently logged in
377 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
378 Session session = getCurrentSession(toadletContext);
380 session.removeAttribute("Sone.CurrentSone");
382 session.setAttribute("Sone.CurrentSone", sone.getId());
387 * Returns the notification manager.
389 * @return The notification manager
391 public NotificationManager getNotifications() {
392 return notificationManager;
396 * Returns the l10n helper of the node.
398 * @return The node’s l10n helper
400 public BaseL10n getL10n() {
401 return sonePlugin.l10n().getBase();
405 * Returns the session manager of the node.
407 * @return The node’s session manager
409 public SessionManager getSessionManager() {
410 return sonePlugin.pluginRespirator().getSessionManager("Sone");
414 * Returns the node’s form password.
416 * @return The form password
418 public String getFormPassword() {
423 * Returns the posts that have been announced as new in the
424 * {@link #newPostNotification}.
426 * @return The new posts
428 public Set<Post> getNewPosts() {
429 return new SetBuilder<Post>().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get();
433 * Returns the replies that have been announced as new in the
434 * {@link #newReplyNotification}.
436 * @return The new replies
438 public Set<Reply> getNewReplies() {
439 return new SetBuilder<Reply>().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get();
443 * Sets whether the current start of the plugin is the first start. It is
444 * considered a first start if the configuration file does not exist.
447 * {@code true} if no configuration file existed when Sone was
448 * loaded, {@code false} otherwise
450 public void setFirstStart(boolean firstStart) {
452 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
453 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
454 notificationManager.addNotification(firstStartNotification);
459 * Sets whether Sone was started with a fresh configuration file.
462 * {@code true} if Sone was started with a fresh configuration,
463 * {@code false} if the existing configuration could be read
465 public void setNewConfig(boolean newConfig) {
466 if (newConfig && !hasFirstStartNotification()) {
467 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
468 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
469 notificationManager.addNotification(configNotReadNotification);
478 * Returns whether the first start notification is currently displayed.
480 * @return {@code true} if the first-start notification is currently
481 * displayed, {@code false} otherwise
483 private boolean hasFirstStartNotification() {
484 return notificationManager.getNotification("first-start-notification") != null;
492 * Starts the web interface and registers all toadlets.
494 public void start() {
497 /* notification templates. */
498 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
500 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
501 notificationManager.addNotification(startupNotification);
503 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
507 startupNotification.dismiss();
509 }, "Sone Startup Notification Remover");
511 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
512 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
513 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
516 @SuppressWarnings("synthetic-access")
518 if (getCore().getIdentityManager().isConnected()) {
519 wotMissingNotification.dismiss();
521 notificationManager.addNotification(wotMissingNotification);
523 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
526 }, "Sone WoT Connector Checker");
530 * Stops the web interface and unregisters all toadlets.
533 unregisterToadlets();
534 Ticker.getInstance().stop();
542 * Register all toadlets.
544 private void registerToadlets() {
545 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
546 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
547 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
548 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
549 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
550 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
551 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
552 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
553 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
554 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
555 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
556 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
557 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
558 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
559 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
560 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
561 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
562 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
563 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
564 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
565 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
566 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
567 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
568 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
569 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
571 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
572 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage("", "index.html")));
573 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
574 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
575 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
576 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
577 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
578 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
579 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
580 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
581 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
582 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
583 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
584 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
585 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
586 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
587 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
588 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
589 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
590 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
591 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
592 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
593 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
594 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
595 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
596 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
597 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
598 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
599 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
600 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
601 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
602 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
603 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
604 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
605 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
607 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
608 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
609 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
610 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
611 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
612 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationAjaxPage(this)));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
616 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
617 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
640 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
641 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
642 for (PageToadlet toadlet : pageToadlets) {
643 String menuName = toadlet.getMenuName();
644 if (menuName != null) {
645 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
647 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
653 * Unregisters all toadlets.
655 private void unregisterToadlets() {
656 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
657 for (PageToadlet pageToadlet : pageToadlets) {
658 toadletContainer.unregister(pageToadlet);
660 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
664 * Creates a {@link Reader} from the {@link InputStream} for the resource
665 * with the given name.
667 * @param resourceName
668 * The name of the resource
669 * @return A {@link Reader} for the resource
671 private Reader createReader(String resourceName) {
673 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
674 } catch (UnsupportedEncodingException uee1) {
680 * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are
681 * referenced by {@link SonePart}s in the given text (after parsing it using
682 * {@link SoneTextParser}).
686 * @return All mentioned local Sones
688 private Set<Sone> getMentionedSones(String text) {
689 /* we need no context to find mentioned Sones. */
690 Set<Sone> mentionedSones = new HashSet<Sone>();
692 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
693 if (part instanceof SonePart) {
694 mentionedSones.add(((SonePart) part).getSone());
697 } catch (IOException ioe1) {
698 logger.log(Level.WARNING, "Could not parse post text: " + text, ioe1);
700 return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
704 * Returns the Sone insert notification for the given Sone. If no
705 * notification for the given Sone exists, a new notification is created and
709 * The Sone to get the insert notification for
710 * @return The Sone insert notification
712 private TemplateNotification getSoneInsertNotification(Sone sone) {
713 synchronized (soneInsertNotifications) {
714 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
715 if (templateNotification == null) {
716 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
717 templateNotification.set("insertSone", sone);
718 soneInsertNotifications.put(sone, templateNotification);
720 return templateNotification;
725 // CORELISTENER METHODS
732 public void newSoneFound(Sone sone) {
733 newSoneNotification.add(sone);
734 if (!hasFirstStartNotification()) {
735 notificationManager.addNotification(newSoneNotification);
743 public void newPostFound(Post post) {
744 boolean isLocal = getCore().isLocalSone(post.getSone());
746 localPostNotification.add(post);
748 newPostNotification.add(post);
750 if (!hasFirstStartNotification()) {
751 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
752 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
753 mentionNotification.add(post);
754 notificationManager.addNotification(mentionNotification);
757 getCore().markPostKnown(post);
765 public void newReplyFound(Reply reply) {
766 boolean isLocal = getCore().isLocalSone(reply.getSone());
768 localReplyNotification.add(reply);
770 newReplyNotification.add(reply);
772 if (!hasFirstStartNotification()) {
773 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
774 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal) {
775 mentionNotification.add(reply.getPost());
776 notificationManager.addNotification(mentionNotification);
779 getCore().markReplyKnown(reply);
787 public void markSoneKnown(Sone sone) {
788 newSoneNotification.remove(sone);
795 public void markPostKnown(Post post) {
796 newPostNotification.remove(post);
797 localPostNotification.remove(post);
798 mentionNotification.remove(post);
805 public void markReplyKnown(Reply reply) {
806 newReplyNotification.remove(reply);
807 localReplyNotification.remove(reply);
808 mentionNotification.remove(reply.getPost());
815 public void soneRemoved(Sone sone) {
816 newSoneNotification.remove(sone);
823 public void postRemoved(Post post) {
824 newPostNotification.remove(post);
825 localPostNotification.remove(post);
832 public void replyRemoved(Reply reply) {
833 newReplyNotification.remove(reply);
834 localReplyNotification.remove(reply);
841 public void soneLocked(final Sone sone) {
842 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
845 @SuppressWarnings("synthetic-access")
847 lockedSonesNotification.add(sone);
848 lockedSonesTickerObjects.remove(sone);
849 notificationManager.addNotification(lockedSonesNotification);
851 }, "Sone Locked Notification");
852 lockedSonesTickerObjects.put(sone, tickerObject);
859 public void soneUnlocked(Sone sone) {
860 lockedSonesNotification.remove(sone);
861 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
868 public void soneInserting(Sone sone) {
869 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
870 soneInsertNotification.set("soneStatus", "inserting");
871 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
872 notificationManager.addNotification(soneInsertNotification);
880 public void soneInserted(Sone sone, long insertDuration) {
881 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
882 soneInsertNotification.set("soneStatus", "inserted");
883 soneInsertNotification.set("insertDuration", insertDuration / 1000);
884 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
885 notificationManager.addNotification(soneInsertNotification);
893 public void soneInsertAborted(Sone sone, Throwable cause) {
894 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
895 soneInsertNotification.set("soneStatus", "insert-aborted");
896 soneInsertNotification.set("insert-error", cause);
897 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
898 notificationManager.addNotification(soneInsertNotification);
906 public void updateFound(Version version, long releaseTime, long latestEdition) {
907 newVersionNotification.getTemplateContext().set("latestVersion", version);
908 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
909 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
910 notificationManager.addNotification(newVersionNotification);
914 * Template provider implementation that uses
915 * {@link WebInterface#createReader(String)} to load templates for
918 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
920 private class ClassPathTemplateProvider implements Provider {
922 /** Cache for templates. */
923 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
926 @SuppressWarnings("synthetic-access")
927 public CacheItem<Template> retrieve(String key) throws CacheException {
928 Template template = findTemplate(key);
929 if (template != null) {
930 return new DefaultCacheItem<Template>(template);
940 @SuppressWarnings("synthetic-access")
941 public Template getTemplate(TemplateContext templateContext, String templateName) {
943 return templateCache.get(templateName);
944 } catch (CacheException ce1) {
945 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
951 * Locates a template in the class path.
953 * @param templateName
954 * The name of the template to load
955 * @return The loaded template, or {@code null} if no template could be
958 @SuppressWarnings("synthetic-access")
959 private Template findTemplate(String templateName) {
960 Reader templateReader = createReader("/templates/" + templateName);
961 if (templateReader == null) {
964 Template template = null;
966 template = TemplateParser.parse(templateReader);
967 } catch (TemplateException te1) {
968 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);