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.EditProfileFieldAjaxPage;
79 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
80 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
81 import net.pterodactylus.sone.web.ajax.GetNotificationAjaxPage;
82 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
83 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
84 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
85 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
86 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
87 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
88 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
89 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
90 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
91 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
92 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
93 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
94 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
95 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
96 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
97 import net.pterodactylus.sone.web.page.FreenetRequest;
98 import net.pterodactylus.sone.web.page.PageToadlet;
99 import net.pterodactylus.sone.web.page.PageToadletFactory;
100 import net.pterodactylus.util.cache.Cache;
101 import net.pterodactylus.util.cache.CacheException;
102 import net.pterodactylus.util.cache.CacheItem;
103 import net.pterodactylus.util.cache.DefaultCacheItem;
104 import net.pterodactylus.util.cache.MemoryCache;
105 import net.pterodactylus.util.cache.ValueRetriever;
106 import net.pterodactylus.util.collection.SetBuilder;
107 import net.pterodactylus.util.filter.Filters;
108 import net.pterodactylus.util.logging.Logging;
109 import net.pterodactylus.util.notify.Notification;
110 import net.pterodactylus.util.notify.NotificationManager;
111 import net.pterodactylus.util.notify.TemplateNotification;
112 import net.pterodactylus.util.template.CollectionSortFilter;
113 import net.pterodactylus.util.template.ContainsFilter;
114 import net.pterodactylus.util.template.DateFilter;
115 import net.pterodactylus.util.template.FormatFilter;
116 import net.pterodactylus.util.template.HtmlFilter;
117 import net.pterodactylus.util.template.MatchFilter;
118 import net.pterodactylus.util.template.Provider;
119 import net.pterodactylus.util.template.ReflectionAccessor;
120 import net.pterodactylus.util.template.ReplaceFilter;
121 import net.pterodactylus.util.template.StoreFilter;
122 import net.pterodactylus.util.template.Template;
123 import net.pterodactylus.util.template.TemplateContext;
124 import net.pterodactylus.util.template.TemplateContextFactory;
125 import net.pterodactylus.util.template.TemplateException;
126 import net.pterodactylus.util.template.TemplateParser;
127 import net.pterodactylus.util.template.XmlFilter;
128 import net.pterodactylus.util.thread.Ticker;
129 import net.pterodactylus.util.version.Version;
130 import net.pterodactylus.util.web.RedirectPage;
131 import net.pterodactylus.util.web.StaticPage;
132 import net.pterodactylus.util.web.TemplatePage;
133 import freenet.clients.http.SessionManager;
134 import freenet.clients.http.ToadletContainer;
135 import freenet.clients.http.ToadletContext;
136 import freenet.clients.http.SessionManager.Session;
137 import freenet.l10n.BaseL10n;
138 import freenet.support.api.HTTPRequest;
141 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
142 * references to l10n helpers.
144 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
146 public class WebInterface implements CoreListener {
149 private static final Logger logger = Logging.getLogger(WebInterface.class);
151 /** The notification manager. */
152 private final NotificationManager notificationManager = new NotificationManager();
154 /** The Sone plugin. */
155 private final SonePlugin sonePlugin;
157 /** The registered toadlets. */
158 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
160 /** The form password. */
161 private final String formPassword;
163 /** The template context factory. */
164 private final TemplateContextFactory templateContextFactory;
166 /** The Sone text parser. */
167 private final SoneTextParser soneTextParser;
169 /** The “new Sone” notification. */
170 private final ListNotification<Sone> newSoneNotification;
172 /** The “new post” notification. */
173 private final ListNotification<Post> newPostNotification;
175 /** The “new reply” notification. */
176 private final ListNotification<Reply> newReplyNotification;
178 /** The invisible “local post” notification. */
179 private final ListNotification<Post> localPostNotification;
181 /** The invisible “local reply” notification. */
182 private final ListNotification<Reply> localReplyNotification;
184 /** The “you have been mentioned” notification. */
185 private final ListNotification<Post> mentionNotification;
187 /** Notifications for sone inserts. */
188 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
190 /** Sone locked notification ticker objects. */
191 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
193 /** The “Sone locked” notification. */
194 private final ListNotification<Sone> lockedSonesNotification;
196 /** The “new version” notification. */
197 private final TemplateNotification newVersionNotification;
199 /** The “inserting images” notification. */
200 private final ListNotification<Image> insertingImagesNotification;
202 /** The “inserted images” notification. */
203 private final ListNotification<Image> insertedImagesNotification;
205 /** The “image insert failed” notification. */
206 private final ListNotification<Image> imageInsertFailedNotification;
209 * Creates a new web interface.
214 @SuppressWarnings("synthetic-access")
215 public WebInterface(SonePlugin sonePlugin) {
216 this.sonePlugin = sonePlugin;
217 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
218 soneTextParser = new SoneTextParser(getCore(), getCore());
220 templateContextFactory = new TemplateContextFactory();
221 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
222 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
223 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
224 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
225 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
226 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
227 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
228 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
229 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
230 templateContextFactory.addFilter("date", new DateFilter());
231 templateContextFactory.addFilter("html", new HtmlFilter());
232 templateContextFactory.addFilter("replace", new ReplaceFilter());
233 templateContextFactory.addFilter("store", new StoreFilter());
234 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
235 templateContextFactory.addFilter("substring", new SubstringFilter());
236 templateContextFactory.addFilter("xml", new XmlFilter());
237 templateContextFactory.addFilter("change", new RequestChangeFilter());
238 templateContextFactory.addFilter("match", new MatchFilter());
239 templateContextFactory.addFilter("css", new CssClassNameFilter());
240 templateContextFactory.addFilter("js", new JavascriptFilter());
241 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory, soneTextParser));
242 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
243 templateContextFactory.addFilter("format", new FormatFilter());
244 templateContextFactory.addFilter("sort", new CollectionSortFilter());
245 templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory));
246 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
247 templateContextFactory.addFilter("in", new ContainsFilter());
248 templateContextFactory.addFilter("unique", new UniqueElementFilter());
249 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
250 templateContextFactory.addProvider(new ClassPathTemplateProvider());
251 templateContextFactory.addTemplateObject("webInterface", this);
252 templateContextFactory.addTemplateObject("formPassword", formPassword);
254 /* create notifications. */
255 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
256 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
258 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
259 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
261 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
262 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
264 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
265 newReplyNotification = new ListNotification<Reply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
267 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
268 localReplyNotification = new ListNotification<Reply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
270 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
271 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
273 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
274 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
276 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
277 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
279 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
280 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
282 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
283 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
285 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
286 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
294 * Returns the Sone core used by the Sone plugin.
296 * @return The Sone core
298 public Core getCore() {
299 return sonePlugin.core();
303 * Returns the template context factory of the web interface.
305 * @return The template context factory
307 public TemplateContextFactory getTemplateContextFactory() {
308 return templateContextFactory;
312 * Returns the current session, creating a new session if there is no
315 * @param toadletContenxt
316 * The toadlet context
317 * @return The current session, or {@code null} if there is no current
320 public Session getCurrentSession(ToadletContext toadletContenxt) {
321 return getCurrentSession(toadletContenxt, true);
325 * Returns the current session, creating a new session if there is no
326 * current session and {@code create} is {@code true}.
328 * @param toadletContenxt
329 * The toadlet context
331 * {@code true} to create a new session if there is no current
332 * session, {@code false} otherwise
333 * @return The current session, or {@code null} if there is no current
336 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
337 Session session = getSessionManager().useSession(toadletContenxt);
338 if (create && (session == null)) {
339 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
345 * Returns the currently logged in Sone.
347 * @param toadletContext
348 * The toadlet context
349 * @return The currently logged in Sone, or {@code null} if no Sone is
350 * currently logged in
352 public Sone getCurrentSone(ToadletContext toadletContext) {
353 return getCurrentSone(toadletContext, true);
357 * Returns the currently logged in Sone.
359 * @param toadletContext
360 * The toadlet context
362 * {@code true} to create a new session if no session exists,
363 * {@code false} to not create a new session
364 * @return The currently logged in Sone, or {@code null} if no Sone is
365 * currently logged in
367 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
368 Set<Sone> localSones = getCore().getLocalSones();
369 if (localSones.size() == 1) {
370 return localSones.iterator().next();
372 return getCurrentSone(getCurrentSession(toadletContext, create));
376 * Returns the currently logged in Sone.
380 * @return The currently logged in Sone, or {@code null} if no Sone is
381 * currently logged in
383 public Sone getCurrentSone(Session session) {
384 if (session == null) {
387 String soneId = (String) session.getAttribute("Sone.CurrentSone");
388 if (soneId == null) {
391 return getCore().getLocalSone(soneId, false);
395 * Sets the currently logged in Sone.
397 * @param toadletContext
398 * The toadlet context
400 * The Sone to set as currently logged in
402 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
403 Session session = getCurrentSession(toadletContext);
405 session.removeAttribute("Sone.CurrentSone");
407 session.setAttribute("Sone.CurrentSone", sone.getId());
412 * Returns the notification manager.
414 * @return The notification manager
416 public NotificationManager getNotifications() {
417 return notificationManager;
421 * Returns the l10n helper of the node.
423 * @return The node’s l10n helper
425 public BaseL10n getL10n() {
426 return sonePlugin.l10n().getBase();
430 * Returns the session manager of the node.
432 * @return The node’s session manager
434 public SessionManager getSessionManager() {
435 return sonePlugin.pluginRespirator().getSessionManager("Sone");
439 * Returns the node’s form password.
441 * @return The form password
443 public String getFormPassword() {
448 * Returns the posts that have been announced as new in the
449 * {@link #newPostNotification}.
451 * @return The new posts
453 public Set<Post> getNewPosts() {
454 return new SetBuilder<Post>().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get();
458 * Returns the replies that have been announced as new in the
459 * {@link #newReplyNotification}.
461 * @return The new replies
463 public Set<Reply> getNewReplies() {
464 return new SetBuilder<Reply>().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get();
468 * Sets whether the current start of the plugin is the first start. It is
469 * considered a first start if the configuration file does not exist.
472 * {@code true} if no configuration file existed when Sone was
473 * loaded, {@code false} otherwise
475 public void setFirstStart(boolean firstStart) {
477 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
478 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
479 notificationManager.addNotification(firstStartNotification);
484 * Sets whether Sone was started with a fresh configuration file.
487 * {@code true} if Sone was started with a fresh configuration,
488 * {@code false} if the existing configuration could be read
490 public void setNewConfig(boolean newConfig) {
491 if (newConfig && !hasFirstStartNotification()) {
492 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
493 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
494 notificationManager.addNotification(configNotReadNotification);
503 * Returns whether the first start notification is currently displayed.
505 * @return {@code true} if the first-start notification is currently
506 * displayed, {@code false} otherwise
508 private boolean hasFirstStartNotification() {
509 return notificationManager.getNotification("first-start-notification") != null;
517 * Starts the web interface and registers all toadlets.
519 public void start() {
522 /* notification templates. */
523 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
525 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
526 notificationManager.addNotification(startupNotification);
528 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
532 startupNotification.dismiss();
534 }, "Sone Startup Notification Remover");
536 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
537 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
538 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
541 @SuppressWarnings("synthetic-access")
543 if (getCore().getIdentityManager().isConnected()) {
544 wotMissingNotification.dismiss();
546 notificationManager.addNotification(wotMissingNotification);
548 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
551 }, "Sone WoT Connector Checker");
555 * Stops the web interface and unregisters all toadlets.
558 unregisterToadlets();
559 Ticker.getInstance().stop();
567 * Register all toadlets.
569 private void registerToadlets() {
570 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
571 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
572 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
573 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
574 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
575 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
576 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
577 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
578 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
579 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
580 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
581 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
582 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
583 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
584 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
585 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
586 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
587 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
588 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
589 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
590 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
591 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
592 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
593 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
594 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
595 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
596 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
597 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
598 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
600 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
601 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
602 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
603 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
604 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
605 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
607 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
608 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
609 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
610 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
611 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
612 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
616 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
617 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationAjaxPage(this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
677 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
678 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
679 for (PageToadlet toadlet : pageToadlets) {
680 String menuName = toadlet.getMenuName();
681 if (menuName != null) {
682 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
684 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
690 * Unregisters all toadlets.
692 private void unregisterToadlets() {
693 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
694 for (PageToadlet pageToadlet : pageToadlets) {
695 toadletContainer.unregister(pageToadlet);
697 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
701 * Creates a {@link Reader} from the {@link InputStream} for the resource
702 * with the given name.
704 * @param resourceName
705 * The name of the resource
706 * @return A {@link Reader} for the resource
708 private Reader createReader(String resourceName) {
710 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
711 } catch (UnsupportedEncodingException uee1) {
717 * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are
718 * referenced by {@link SonePart}s in the given text (after parsing it using
719 * {@link SoneTextParser}).
723 * @return All mentioned local Sones
725 private Set<Sone> getMentionedSones(String text) {
726 /* we need no context to find mentioned Sones. */
727 Set<Sone> mentionedSones = new HashSet<Sone>();
729 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
730 if (part instanceof SonePart) {
731 mentionedSones.add(((SonePart) part).getSone());
734 } catch (IOException ioe1) {
735 logger.log(Level.WARNING, "Could not parse post text: " + text, ioe1);
737 return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
741 * Returns the Sone insert notification for the given Sone. If no
742 * notification for the given Sone exists, a new notification is created and
746 * The Sone to get the insert notification for
747 * @return The Sone insert notification
749 private TemplateNotification getSoneInsertNotification(Sone sone) {
750 synchronized (soneInsertNotifications) {
751 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
752 if (templateNotification == null) {
753 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
754 templateNotification.set("insertSone", sone);
755 soneInsertNotifications.put(sone, templateNotification);
757 return templateNotification;
762 // CORELISTENER METHODS
769 public void newSoneFound(Sone sone) {
770 newSoneNotification.add(sone);
771 if (!hasFirstStartNotification()) {
772 notificationManager.addNotification(newSoneNotification);
780 public void newPostFound(Post post) {
781 boolean isLocal = getCore().isLocalSone(post.getSone());
783 localPostNotification.add(post);
785 newPostNotification.add(post);
787 if (!hasFirstStartNotification()) {
788 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
789 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
790 mentionNotification.add(post);
791 notificationManager.addNotification(mentionNotification);
794 getCore().markPostKnown(post);
802 public void newReplyFound(Reply reply) {
803 boolean isLocal = getCore().isLocalSone(reply.getSone());
805 localReplyNotification.add(reply);
807 newReplyNotification.add(reply);
809 if (!hasFirstStartNotification()) {
810 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
811 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null)) {
812 mentionNotification.add(reply.getPost());
813 notificationManager.addNotification(mentionNotification);
816 getCore().markReplyKnown(reply);
824 public void markSoneKnown(Sone sone) {
825 newSoneNotification.remove(sone);
832 public void markPostKnown(Post post) {
833 newPostNotification.remove(post);
834 localPostNotification.remove(post);
835 mentionNotification.remove(post);
842 public void markReplyKnown(Reply reply) {
843 newReplyNotification.remove(reply);
844 localReplyNotification.remove(reply);
845 mentionNotification.remove(reply.getPost());
852 public void soneRemoved(Sone sone) {
853 newSoneNotification.remove(sone);
860 public void postRemoved(Post post) {
861 newPostNotification.remove(post);
862 localPostNotification.remove(post);
869 public void replyRemoved(Reply reply) {
870 newReplyNotification.remove(reply);
871 localReplyNotification.remove(reply);
878 public void soneLocked(final Sone sone) {
879 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
882 @SuppressWarnings("synthetic-access")
884 lockedSonesNotification.add(sone);
885 lockedSonesTickerObjects.remove(sone);
886 notificationManager.addNotification(lockedSonesNotification);
888 }, "Sone Locked Notification");
889 lockedSonesTickerObjects.put(sone, tickerObject);
896 public void soneUnlocked(Sone sone) {
897 lockedSonesNotification.remove(sone);
898 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
905 public void soneInserting(Sone sone) {
906 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
907 soneInsertNotification.set("soneStatus", "inserting");
908 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
909 notificationManager.addNotification(soneInsertNotification);
917 public void soneInserted(Sone sone, long insertDuration) {
918 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
919 soneInsertNotification.set("soneStatus", "inserted");
920 soneInsertNotification.set("insertDuration", insertDuration / 1000);
921 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
922 notificationManager.addNotification(soneInsertNotification);
930 public void soneInsertAborted(Sone sone, Throwable cause) {
931 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
932 soneInsertNotification.set("soneStatus", "insert-aborted");
933 soneInsertNotification.set("insert-error", cause);
934 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
935 notificationManager.addNotification(soneInsertNotification);
943 public void updateFound(Version version, long releaseTime, long latestEdition) {
944 newVersionNotification.getTemplateContext().set("latestVersion", version);
945 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
946 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
947 notificationManager.addNotification(newVersionNotification);
954 public void imageInsertStarted(Image image) {
955 insertingImagesNotification.add(image);
956 notificationManager.addNotification(insertingImagesNotification);
963 public void imageInsertAborted(Image image) {
964 insertingImagesNotification.remove(image);
971 public void imageInsertFinished(Image image) {
972 insertingImagesNotification.remove(image);
973 insertedImagesNotification.add(image);
974 notificationManager.addNotification(insertedImagesNotification);
981 public void imageInsertFailed(Image image, Throwable cause) {
982 insertingImagesNotification.remove(image);
983 imageInsertFailedNotification.add(image);
984 notificationManager.addNotification(imageInsertFailedNotification);
988 * Template provider implementation that uses
989 * {@link WebInterface#createReader(String)} to load templates for
992 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
994 private class ClassPathTemplateProvider implements Provider {
996 /** Cache for templates. */
997 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
1000 @SuppressWarnings("synthetic-access")
1001 public CacheItem<Template> retrieve(String key) throws CacheException {
1002 Template template = findTemplate(key);
1003 if (template != null) {
1004 return new DefaultCacheItem<Template>(template);
1014 @SuppressWarnings("synthetic-access")
1015 public Template getTemplate(TemplateContext templateContext, String templateName) {
1017 return templateCache.get(templateName);
1018 } catch (CacheException ce1) {
1019 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
1025 * Locates a template in the class path.
1027 * @param templateName
1028 * The name of the template to load
1029 * @return The loaded template, or {@code null} if no template could be
1032 @SuppressWarnings("synthetic-access")
1033 private Template findTemplate(String templateName) {
1034 Reader templateReader = createReader("/templates/" + templateName);
1035 if (templateReader == null) {
1038 Template template = null;
1040 template = TemplateParser.parse(templateReader);
1041 } catch (TemplateException te1) {
1042 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);