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.Album;
41 import net.pterodactylus.sone.data.Image;
42 import net.pterodactylus.sone.data.Post;
43 import net.pterodactylus.sone.data.Reply;
44 import net.pterodactylus.sone.data.Sone;
45 import net.pterodactylus.sone.freenet.L10nFilter;
46 import net.pterodactylus.sone.freenet.wot.Identity;
47 import net.pterodactylus.sone.freenet.wot.Trust;
48 import net.pterodactylus.sone.main.SonePlugin;
49 import net.pterodactylus.sone.notify.ListNotification;
50 import net.pterodactylus.sone.template.AlbumAccessor;
51 import net.pterodactylus.sone.template.CollectionAccessor;
52 import net.pterodactylus.sone.template.CssClassNameFilter;
53 import net.pterodactylus.sone.template.HttpRequestAccessor;
54 import net.pterodactylus.sone.template.IdentityAccessor;
55 import net.pterodactylus.sone.template.ImageLinkFilter;
56 import net.pterodactylus.sone.template.JavascriptFilter;
57 import net.pterodactylus.sone.template.ParserFilter;
58 import net.pterodactylus.sone.template.PostAccessor;
59 import net.pterodactylus.sone.template.ReplyAccessor;
60 import net.pterodactylus.sone.template.ReplyGroupFilter;
61 import net.pterodactylus.sone.template.RequestChangeFilter;
62 import net.pterodactylus.sone.template.SoneAccessor;
63 import net.pterodactylus.sone.template.SubstringFilter;
64 import net.pterodactylus.sone.template.TrustAccessor;
65 import net.pterodactylus.sone.template.UniqueElementFilter;
66 import net.pterodactylus.sone.template.UnknownDateFilter;
67 import net.pterodactylus.sone.text.Part;
68 import net.pterodactylus.sone.text.SonePart;
69 import net.pterodactylus.sone.text.SoneTextParser;
70 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
71 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
72 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
73 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
74 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
75 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
76 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
77 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
78 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
79 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
80 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
81 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
82 import net.pterodactylus.sone.web.ajax.GetNotificationAjaxPage;
83 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
84 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
85 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
86 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
87 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
88 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
89 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
90 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
91 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
92 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
93 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
94 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
95 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
96 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
97 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
98 import net.pterodactylus.sone.web.page.FreenetRequest;
99 import net.pterodactylus.sone.web.page.PageToadlet;
100 import net.pterodactylus.sone.web.page.PageToadletFactory;
101 import net.pterodactylus.util.cache.Cache;
102 import net.pterodactylus.util.cache.CacheException;
103 import net.pterodactylus.util.cache.CacheItem;
104 import net.pterodactylus.util.cache.DefaultCacheItem;
105 import net.pterodactylus.util.cache.MemoryCache;
106 import net.pterodactylus.util.cache.ValueRetriever;
107 import net.pterodactylus.util.collection.SetBuilder;
108 import net.pterodactylus.util.filter.Filters;
109 import net.pterodactylus.util.logging.Logging;
110 import net.pterodactylus.util.notify.Notification;
111 import net.pterodactylus.util.notify.NotificationManager;
112 import net.pterodactylus.util.notify.TemplateNotification;
113 import net.pterodactylus.util.template.CollectionSortFilter;
114 import net.pterodactylus.util.template.ContainsFilter;
115 import net.pterodactylus.util.template.DateFilter;
116 import net.pterodactylus.util.template.FormatFilter;
117 import net.pterodactylus.util.template.HtmlFilter;
118 import net.pterodactylus.util.template.MatchFilter;
119 import net.pterodactylus.util.template.ModFilter;
120 import net.pterodactylus.util.template.Provider;
121 import net.pterodactylus.util.template.ReflectionAccessor;
122 import net.pterodactylus.util.template.ReplaceFilter;
123 import net.pterodactylus.util.template.StoreFilter;
124 import net.pterodactylus.util.template.Template;
125 import net.pterodactylus.util.template.TemplateContext;
126 import net.pterodactylus.util.template.TemplateContextFactory;
127 import net.pterodactylus.util.template.TemplateException;
128 import net.pterodactylus.util.template.TemplateParser;
129 import net.pterodactylus.util.template.XmlFilter;
130 import net.pterodactylus.util.thread.Ticker;
131 import net.pterodactylus.util.version.Version;
132 import net.pterodactylus.util.web.RedirectPage;
133 import net.pterodactylus.util.web.StaticPage;
134 import net.pterodactylus.util.web.TemplatePage;
135 import freenet.clients.http.SessionManager;
136 import freenet.clients.http.ToadletContainer;
137 import freenet.clients.http.ToadletContext;
138 import freenet.clients.http.SessionManager.Session;
139 import freenet.l10n.BaseL10n;
140 import freenet.support.api.HTTPRequest;
143 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
144 * references to l10n helpers.
146 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
148 public class WebInterface implements CoreListener {
151 private static final Logger logger = Logging.getLogger(WebInterface.class);
153 /** The notification manager. */
154 private final NotificationManager notificationManager = new NotificationManager();
156 /** The Sone plugin. */
157 private final SonePlugin sonePlugin;
159 /** The registered toadlets. */
160 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
162 /** The form password. */
163 private final String formPassword;
165 /** The template context factory. */
166 private final TemplateContextFactory templateContextFactory;
168 /** The Sone text parser. */
169 private final SoneTextParser soneTextParser;
171 /** The “new Sone” notification. */
172 private final ListNotification<Sone> newSoneNotification;
174 /** The “new post” notification. */
175 private final ListNotification<Post> newPostNotification;
177 /** The “new reply” notification. */
178 private final ListNotification<Reply> newReplyNotification;
180 /** The invisible “local post” notification. */
181 private final ListNotification<Post> localPostNotification;
183 /** The invisible “local reply” notification. */
184 private final ListNotification<Reply> localReplyNotification;
186 /** The “you have been mentioned” notification. */
187 private final ListNotification<Post> mentionNotification;
189 /** Notifications for sone inserts. */
190 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
192 /** Sone locked notification ticker objects. */
193 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
195 /** The “Sone locked” notification. */
196 private final ListNotification<Sone> lockedSonesNotification;
198 /** The “new version” notification. */
199 private final TemplateNotification newVersionNotification;
201 /** The “inserting images” notification. */
202 private final ListNotification<Image> insertingImagesNotification;
204 /** The “inserted images” notification. */
205 private final ListNotification<Image> insertedImagesNotification;
207 /** The “image insert failed” notification. */
208 private final ListNotification<Image> imageInsertFailedNotification;
211 * Creates a new web interface.
216 @SuppressWarnings("synthetic-access")
217 public WebInterface(SonePlugin sonePlugin) {
218 this.sonePlugin = sonePlugin;
219 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
220 soneTextParser = new SoneTextParser(getCore(), getCore());
222 templateContextFactory = new TemplateContextFactory();
223 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
224 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
225 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
226 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
227 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
228 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
229 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
230 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
231 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
232 templateContextFactory.addFilter("date", new DateFilter());
233 templateContextFactory.addFilter("html", new HtmlFilter());
234 templateContextFactory.addFilter("replace", new ReplaceFilter());
235 templateContextFactory.addFilter("store", new StoreFilter());
236 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
237 templateContextFactory.addFilter("substring", new SubstringFilter());
238 templateContextFactory.addFilter("xml", new XmlFilter());
239 templateContextFactory.addFilter("change", new RequestChangeFilter());
240 templateContextFactory.addFilter("match", new MatchFilter());
241 templateContextFactory.addFilter("css", new CssClassNameFilter());
242 templateContextFactory.addFilter("js", new JavascriptFilter());
243 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory, soneTextParser));
244 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
245 templateContextFactory.addFilter("format", new FormatFilter());
246 templateContextFactory.addFilter("sort", new CollectionSortFilter());
247 templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory));
248 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
249 templateContextFactory.addFilter("in", new ContainsFilter());
250 templateContextFactory.addFilter("unique", new UniqueElementFilter());
251 templateContextFactory.addFilter("mod", new ModFilter());
252 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
253 templateContextFactory.addProvider(new ClassPathTemplateProvider());
254 templateContextFactory.addTemplateObject("webInterface", this);
255 templateContextFactory.addTemplateObject("formPassword", formPassword);
257 /* create notifications. */
258 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
259 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
261 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
262 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
264 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
265 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
267 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
268 newReplyNotification = new ListNotification<Reply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
270 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
271 localReplyNotification = new ListNotification<Reply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
273 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
274 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
276 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
277 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
279 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
280 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
282 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
283 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
285 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
286 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
288 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
289 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
297 * Returns the Sone core used by the Sone plugin.
299 * @return The Sone core
301 public Core getCore() {
302 return sonePlugin.core();
306 * Returns the template context factory of the web interface.
308 * @return The template context factory
310 public TemplateContextFactory getTemplateContextFactory() {
311 return templateContextFactory;
315 * Returns the current session, creating a new session if there is no
318 * @param toadletContenxt
319 * The toadlet context
320 * @return The current session, or {@code null} if there is no current
323 public Session getCurrentSession(ToadletContext toadletContenxt) {
324 return getCurrentSession(toadletContenxt, true);
328 * Returns the current session, creating a new session if there is no
329 * current session and {@code create} is {@code true}.
331 * @param toadletContenxt
332 * The toadlet context
334 * {@code true} to create a new session if there is no current
335 * session, {@code false} otherwise
336 * @return The current session, or {@code null} if there is no current
339 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
340 Session session = getSessionManager().useSession(toadletContenxt);
341 if (create && (session == null)) {
342 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
348 * Returns the currently logged in Sone.
350 * @param toadletContext
351 * The toadlet context
352 * @return The currently logged in Sone, or {@code null} if no Sone is
353 * currently logged in
355 public Sone getCurrentSone(ToadletContext toadletContext) {
356 return getCurrentSone(toadletContext, true);
360 * Returns the currently logged in Sone.
362 * @param toadletContext
363 * The toadlet context
365 * {@code true} to create a new session if no session exists,
366 * {@code false} to not create a new session
367 * @return The currently logged in Sone, or {@code null} if no Sone is
368 * currently logged in
370 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
371 Set<Sone> localSones = getCore().getLocalSones();
372 if (localSones.size() == 1) {
373 return localSones.iterator().next();
375 return getCurrentSone(getCurrentSession(toadletContext, create));
379 * Returns the currently logged in Sone.
383 * @return The currently logged in Sone, or {@code null} if no Sone is
384 * currently logged in
386 public Sone getCurrentSone(Session session) {
387 if (session == null) {
390 String soneId = (String) session.getAttribute("Sone.CurrentSone");
391 if (soneId == null) {
394 return getCore().getLocalSone(soneId, false);
398 * Sets the currently logged in Sone.
400 * @param toadletContext
401 * The toadlet context
403 * The Sone to set as currently logged in
405 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
406 Session session = getCurrentSession(toadletContext);
408 session.removeAttribute("Sone.CurrentSone");
410 session.setAttribute("Sone.CurrentSone", sone.getId());
415 * Returns the notification manager.
417 * @return The notification manager
419 public NotificationManager getNotifications() {
420 return notificationManager;
424 * Returns the l10n helper of the node.
426 * @return The node’s l10n helper
428 public BaseL10n getL10n() {
429 return sonePlugin.l10n().getBase();
433 * Returns the session manager of the node.
435 * @return The node’s session manager
437 public SessionManager getSessionManager() {
438 return sonePlugin.pluginRespirator().getSessionManager("Sone");
442 * Returns the node’s form password.
444 * @return The form password
446 public String getFormPassword() {
451 * Returns the posts that have been announced as new in the
452 * {@link #newPostNotification}.
454 * @return The new posts
456 public Set<Post> getNewPosts() {
457 return new SetBuilder<Post>().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get();
461 * Returns the replies that have been announced as new in the
462 * {@link #newReplyNotification}.
464 * @return The new replies
466 public Set<Reply> getNewReplies() {
467 return new SetBuilder<Reply>().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get();
471 * Sets whether the current start of the plugin is the first start. It is
472 * considered a first start if the configuration file does not exist.
475 * {@code true} if no configuration file existed when Sone was
476 * loaded, {@code false} otherwise
478 public void setFirstStart(boolean firstStart) {
480 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
481 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
482 notificationManager.addNotification(firstStartNotification);
487 * Sets whether Sone was started with a fresh configuration file.
490 * {@code true} if Sone was started with a fresh configuration,
491 * {@code false} if the existing configuration could be read
493 public void setNewConfig(boolean newConfig) {
494 if (newConfig && !hasFirstStartNotification()) {
495 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
496 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
497 notificationManager.addNotification(configNotReadNotification);
506 * Returns whether the first start notification is currently displayed.
508 * @return {@code true} if the first-start notification is currently
509 * displayed, {@code false} otherwise
511 private boolean hasFirstStartNotification() {
512 return notificationManager.getNotification("first-start-notification") != null;
520 * Starts the web interface and registers all toadlets.
522 public void start() {
525 /* notification templates. */
526 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
528 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
529 notificationManager.addNotification(startupNotification);
531 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
535 startupNotification.dismiss();
537 }, "Sone Startup Notification Remover");
539 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
540 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
541 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
544 @SuppressWarnings("synthetic-access")
546 if (getCore().getIdentityManager().isConnected()) {
547 wotMissingNotification.dismiss();
549 notificationManager.addNotification(wotMissingNotification);
551 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
554 }, "Sone WoT Connector Checker");
558 * Stops the web interface and unregisters all toadlets.
561 unregisterToadlets();
562 Ticker.getInstance().stop();
570 * Register all toadlets.
572 private void registerToadlets() {
573 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
574 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
575 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
576 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
577 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
578 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
579 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
580 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
581 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
582 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
583 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
584 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
585 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
586 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
587 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
588 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
589 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
590 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
591 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
592 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
593 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
594 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
595 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
596 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
597 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
598 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
599 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
600 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
601 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
603 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
604 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
605 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
607 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
608 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
609 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
610 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
611 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
612 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
616 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
617 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationAjaxPage(this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
681 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
682 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
683 for (PageToadlet toadlet : pageToadlets) {
684 String menuName = toadlet.getMenuName();
685 if (menuName != null) {
686 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
688 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
694 * Unregisters all toadlets.
696 private void unregisterToadlets() {
697 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
698 for (PageToadlet pageToadlet : pageToadlets) {
699 toadletContainer.unregister(pageToadlet);
701 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
705 * Creates a {@link Reader} from the {@link InputStream} for the resource
706 * with the given name.
708 * @param resourceName
709 * The name of the resource
710 * @return A {@link Reader} for the resource
712 private Reader createReader(String resourceName) {
714 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
715 } catch (UnsupportedEncodingException uee1) {
721 * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are
722 * referenced by {@link SonePart}s in the given text (after parsing it using
723 * {@link SoneTextParser}).
727 * @return All mentioned local Sones
729 private Set<Sone> getMentionedSones(String text) {
730 /* we need no context to find mentioned Sones. */
731 Set<Sone> mentionedSones = new HashSet<Sone>();
733 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
734 if (part instanceof SonePart) {
735 mentionedSones.add(((SonePart) part).getSone());
738 } catch (IOException ioe1) {
739 logger.log(Level.WARNING, "Could not parse post text: " + text, ioe1);
741 return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
745 * Returns the Sone insert notification for the given Sone. If no
746 * notification for the given Sone exists, a new notification is created and
750 * The Sone to get the insert notification for
751 * @return The Sone insert notification
753 private TemplateNotification getSoneInsertNotification(Sone sone) {
754 synchronized (soneInsertNotifications) {
755 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
756 if (templateNotification == null) {
757 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
758 templateNotification.set("insertSone", sone);
759 soneInsertNotifications.put(sone, templateNotification);
761 return templateNotification;
766 // CORELISTENER METHODS
773 public void newSoneFound(Sone sone) {
774 newSoneNotification.add(sone);
775 if (!hasFirstStartNotification()) {
776 notificationManager.addNotification(newSoneNotification);
784 public void newPostFound(Post post) {
785 boolean isLocal = getCore().isLocalSone(post.getSone());
787 localPostNotification.add(post);
789 newPostNotification.add(post);
791 if (!hasFirstStartNotification()) {
792 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
793 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
794 mentionNotification.add(post);
795 notificationManager.addNotification(mentionNotification);
798 getCore().markPostKnown(post);
806 public void newReplyFound(Reply reply) {
807 boolean isLocal = getCore().isLocalSone(reply.getSone());
809 localReplyNotification.add(reply);
811 newReplyNotification.add(reply);
813 if (!hasFirstStartNotification()) {
814 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
815 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null)) {
816 mentionNotification.add(reply.getPost());
817 notificationManager.addNotification(mentionNotification);
820 getCore().markReplyKnown(reply);
828 public void markSoneKnown(Sone sone) {
829 newSoneNotification.remove(sone);
836 public void markPostKnown(Post post) {
837 newPostNotification.remove(post);
838 localPostNotification.remove(post);
839 mentionNotification.remove(post);
846 public void markReplyKnown(Reply reply) {
847 newReplyNotification.remove(reply);
848 localReplyNotification.remove(reply);
849 mentionNotification.remove(reply.getPost());
856 public void soneRemoved(Sone sone) {
857 newSoneNotification.remove(sone);
864 public void postRemoved(Post post) {
865 newPostNotification.remove(post);
866 localPostNotification.remove(post);
873 public void replyRemoved(Reply reply) {
874 newReplyNotification.remove(reply);
875 localReplyNotification.remove(reply);
882 public void soneLocked(final Sone sone) {
883 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
886 @SuppressWarnings("synthetic-access")
888 lockedSonesNotification.add(sone);
889 lockedSonesTickerObjects.remove(sone);
890 notificationManager.addNotification(lockedSonesNotification);
892 }, "Sone Locked Notification");
893 lockedSonesTickerObjects.put(sone, tickerObject);
900 public void soneUnlocked(Sone sone) {
901 lockedSonesNotification.remove(sone);
902 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
909 public void soneInserting(Sone sone) {
910 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
911 soneInsertNotification.set("soneStatus", "inserting");
912 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
913 notificationManager.addNotification(soneInsertNotification);
921 public void soneInserted(Sone sone, long insertDuration) {
922 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
923 soneInsertNotification.set("soneStatus", "inserted");
924 soneInsertNotification.set("insertDuration", insertDuration / 1000);
925 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
926 notificationManager.addNotification(soneInsertNotification);
934 public void soneInsertAborted(Sone sone, Throwable cause) {
935 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
936 soneInsertNotification.set("soneStatus", "insert-aborted");
937 soneInsertNotification.set("insert-error", cause);
938 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
939 notificationManager.addNotification(soneInsertNotification);
947 public void updateFound(Version version, long releaseTime, long latestEdition) {
948 newVersionNotification.getTemplateContext().set("latestVersion", version);
949 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
950 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
951 notificationManager.addNotification(newVersionNotification);
958 public void imageInsertStarted(Image image) {
959 insertingImagesNotification.add(image);
960 notificationManager.addNotification(insertingImagesNotification);
967 public void imageInsertAborted(Image image) {
968 insertingImagesNotification.remove(image);
975 public void imageInsertFinished(Image image) {
976 insertingImagesNotification.remove(image);
977 insertedImagesNotification.add(image);
978 notificationManager.addNotification(insertedImagesNotification);
985 public void imageInsertFailed(Image image, Throwable cause) {
986 insertingImagesNotification.remove(image);
987 imageInsertFailedNotification.add(image);
988 notificationManager.addNotification(imageInsertFailedNotification);
992 * Template provider implementation that uses
993 * {@link WebInterface#createReader(String)} to load templates for
996 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
998 private class ClassPathTemplateProvider implements Provider {
1000 /** Cache for templates. */
1001 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
1004 @SuppressWarnings("synthetic-access")
1005 public CacheItem<Template> retrieve(String key) throws CacheException {
1006 Template template = findTemplate(key);
1007 if (template != null) {
1008 return new DefaultCacheItem<Template>(template);
1018 @SuppressWarnings("synthetic-access")
1019 public Template getTemplate(TemplateContext templateContext, String templateName) {
1021 return templateCache.get(templateName);
1022 } catch (CacheException ce1) {
1023 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
1029 * Locates a template in the class path.
1031 * @param templateName
1032 * The name of the template to load
1033 * @return The loaded template, or {@code null} if no template could be
1036 @SuppressWarnings("synthetic-access")
1037 private Template findTemplate(String templateName) {
1038 Reader templateReader = createReader("/templates/" + templateName);
1039 if (templateReader == null) {
1042 Template template = null;
1044 template = TemplateParser.parse(templateReader);
1045 } catch (TemplateException te1) {
1046 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);