2 * Sone - WebInterface.java - Copyright © 2010–2012 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.core.event.MarkSoneKnownEvent;
41 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
42 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
43 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
44 import net.pterodactylus.sone.data.Album;
45 import net.pterodactylus.sone.data.Image;
46 import net.pterodactylus.sone.data.Post;
47 import net.pterodactylus.sone.data.PostReply;
48 import net.pterodactylus.sone.data.Profile;
49 import net.pterodactylus.sone.data.Reply;
50 import net.pterodactylus.sone.data.Sone;
51 import net.pterodactylus.sone.freenet.L10nFilter;
52 import net.pterodactylus.sone.freenet.wot.Identity;
53 import net.pterodactylus.sone.freenet.wot.Trust;
54 import net.pterodactylus.sone.main.SonePlugin;
55 import net.pterodactylus.sone.notify.ListNotification;
56 import net.pterodactylus.sone.template.AlbumAccessor;
57 import net.pterodactylus.sone.template.CollectionAccessor;
58 import net.pterodactylus.sone.template.CssClassNameFilter;
59 import net.pterodactylus.sone.template.HttpRequestAccessor;
60 import net.pterodactylus.sone.template.IdentityAccessor;
61 import net.pterodactylus.sone.template.ImageAccessor;
62 import net.pterodactylus.sone.template.ImageLinkFilter;
63 import net.pterodactylus.sone.template.JavascriptFilter;
64 import net.pterodactylus.sone.template.ParserFilter;
65 import net.pterodactylus.sone.template.PostAccessor;
66 import net.pterodactylus.sone.template.ProfileAccessor;
67 import net.pterodactylus.sone.template.ReplyAccessor;
68 import net.pterodactylus.sone.template.ReplyGroupFilter;
69 import net.pterodactylus.sone.template.RequestChangeFilter;
70 import net.pterodactylus.sone.template.SoneAccessor;
71 import net.pterodactylus.sone.template.SubstringFilter;
72 import net.pterodactylus.sone.template.TrustAccessor;
73 import net.pterodactylus.sone.template.UniqueElementFilter;
74 import net.pterodactylus.sone.template.UnknownDateFilter;
75 import net.pterodactylus.sone.text.Part;
76 import net.pterodactylus.sone.text.SonePart;
77 import net.pterodactylus.sone.text.SoneTextParser;
78 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
79 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
80 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
81 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
82 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
83 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
84 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
85 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
86 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
87 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
88 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
89 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
90 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
91 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
92 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
93 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
94 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
95 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
96 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
97 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
98 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
99 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
100 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
101 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
102 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
103 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
104 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
105 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
106 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
107 import net.pterodactylus.sone.web.page.FreenetRequest;
108 import net.pterodactylus.sone.web.page.PageToadlet;
109 import net.pterodactylus.sone.web.page.PageToadletFactory;
110 import net.pterodactylus.util.logging.Logging;
111 import net.pterodactylus.util.notify.Notification;
112 import net.pterodactylus.util.notify.NotificationManager;
113 import net.pterodactylus.util.notify.TemplateNotification;
114 import net.pterodactylus.util.template.ClassPathTemplateProvider;
115 import net.pterodactylus.util.template.CollectionSortFilter;
116 import net.pterodactylus.util.template.ContainsFilter;
117 import net.pterodactylus.util.template.DateFilter;
118 import net.pterodactylus.util.template.FormatFilter;
119 import net.pterodactylus.util.template.HtmlFilter;
120 import net.pterodactylus.util.template.MatchFilter;
121 import net.pterodactylus.util.template.ModFilter;
122 import net.pterodactylus.util.template.PaginationFilter;
123 import net.pterodactylus.util.template.ReflectionAccessor;
124 import net.pterodactylus.util.template.ReplaceFilter;
125 import net.pterodactylus.util.template.StoreFilter;
126 import net.pterodactylus.util.template.Template;
127 import net.pterodactylus.util.template.TemplateContextFactory;
128 import net.pterodactylus.util.template.TemplateParser;
129 import net.pterodactylus.util.template.TemplateProvider;
130 import net.pterodactylus.util.template.XmlFilter;
131 import net.pterodactylus.util.thread.Ticker;
132 import net.pterodactylus.util.version.Version;
133 import net.pterodactylus.util.web.RedirectPage;
134 import net.pterodactylus.util.web.StaticPage;
135 import net.pterodactylus.util.web.TemplatePage;
137 import com.google.common.collect.Collections2;
138 import com.google.common.collect.ImmutableSet;
139 import com.google.common.eventbus.Subscribe;
140 import com.google.inject.Inject;
142 import freenet.clients.http.SessionManager;
143 import freenet.clients.http.SessionManager.Session;
144 import freenet.clients.http.ToadletContainer;
145 import freenet.clients.http.ToadletContext;
146 import freenet.l10n.BaseL10n;
147 import freenet.support.api.HTTPRequest;
150 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
151 * references to l10n helpers.
153 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
155 public class WebInterface implements CoreListener {
158 private static final Logger logger = Logging.getLogger(WebInterface.class);
160 /** The notification manager. */
161 private final NotificationManager notificationManager = new NotificationManager();
163 /** The Sone plugin. */
164 private final SonePlugin sonePlugin;
166 /** The registered toadlets. */
167 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
169 /** The form password. */
170 private final String formPassword;
172 /** The template context factory. */
173 private final TemplateContextFactory templateContextFactory;
175 /** The Sone text parser. */
176 private final SoneTextParser soneTextParser;
178 /** The parser filter. */
179 private final ParserFilter parserFilter;
181 /** The “new Sone” notification. */
182 private final ListNotification<Sone> newSoneNotification;
184 /** The “new post” notification. */
185 private final ListNotification<Post> newPostNotification;
187 /** The “new reply” notification. */
188 private final ListNotification<PostReply> newReplyNotification;
190 /** The invisible “local post” notification. */
191 private final ListNotification<Post> localPostNotification;
193 /** The invisible “local reply” notification. */
194 private final ListNotification<PostReply> localReplyNotification;
196 /** The “you have been mentioned” notification. */
197 private final ListNotification<Post> mentionNotification;
199 /** Notifications for sone inserts. */
200 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
202 /** Sone locked notification ticker objects. */
203 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
205 /** The “Sone locked” notification. */
206 private final ListNotification<Sone> lockedSonesNotification;
208 /** The “new version” notification. */
209 private final TemplateNotification newVersionNotification;
211 /** The “inserting images” notification. */
212 private final ListNotification<Image> insertingImagesNotification;
214 /** The “inserted images” notification. */
215 private final ListNotification<Image> insertedImagesNotification;
217 /** The “image insert failed” notification. */
218 private final ListNotification<Image> imageInsertFailedNotification;
221 * Creates a new web interface.
227 public WebInterface(SonePlugin sonePlugin) {
228 this.sonePlugin = sonePlugin;
229 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
230 soneTextParser = new SoneTextParser(getCore(), getCore());
232 templateContextFactory = new TemplateContextFactory();
233 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
234 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
235 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
236 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
237 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
238 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
239 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
240 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
241 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
242 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
243 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
244 templateContextFactory.addFilter("date", new DateFilter());
245 templateContextFactory.addFilter("html", new HtmlFilter());
246 templateContextFactory.addFilter("replace", new ReplaceFilter());
247 templateContextFactory.addFilter("store", new StoreFilter());
248 templateContextFactory.addFilter("l10n", new L10nFilter(this));
249 templateContextFactory.addFilter("substring", new SubstringFilter());
250 templateContextFactory.addFilter("xml", new XmlFilter());
251 templateContextFactory.addFilter("change", new RequestChangeFilter());
252 templateContextFactory.addFilter("match", new MatchFilter());
253 templateContextFactory.addFilter("css", new CssClassNameFilter());
254 templateContextFactory.addFilter("js", new JavascriptFilter());
255 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
256 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
257 templateContextFactory.addFilter("format", new FormatFilter());
258 templateContextFactory.addFilter("sort", new CollectionSortFilter());
259 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
260 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
261 templateContextFactory.addFilter("in", new ContainsFilter());
262 templateContextFactory.addFilter("unique", new UniqueElementFilter());
263 templateContextFactory.addFilter("mod", new ModFilter());
264 templateContextFactory.addFilter("paginate", new PaginationFilter());
265 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
266 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
267 templateContextFactory.addTemplateObject("webInterface", this);
268 templateContextFactory.addTemplateObject("formPassword", formPassword);
270 /* create notifications. */
271 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
272 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
274 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
275 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
277 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
278 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
280 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
281 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
283 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
284 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
286 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
287 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
289 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
290 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
292 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
293 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
295 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
296 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
298 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
299 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
301 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
302 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
310 * Returns the Sone core used by the Sone plugin.
312 * @return The Sone core
314 public Core getCore() {
315 return sonePlugin.core();
319 * Returns the template context factory of the web interface.
321 * @return The template context factory
323 public TemplateContextFactory getTemplateContextFactory() {
324 return templateContextFactory;
328 * Returns the current session, creating a new session if there is no
331 * @param toadletContenxt
332 * The toadlet context
333 * @return The current session, or {@code null} if there is no current
336 public Session getCurrentSession(ToadletContext toadletContenxt) {
337 return getCurrentSession(toadletContenxt, true);
341 * Returns the current session, creating a new session if there is no
342 * current session and {@code create} is {@code true}.
344 * @param toadletContenxt
345 * The toadlet context
347 * {@code true} to create a new session if there is no current
348 * session, {@code false} otherwise
349 * @return The current session, or {@code null} if there is no current
352 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
353 Session session = getSessionManager().useSession(toadletContenxt);
354 if (create && (session == null)) {
355 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
361 * Returns the currently logged in Sone.
363 * @param toadletContext
364 * The toadlet context
365 * @return The currently logged in Sone, or {@code null} if no Sone is
366 * currently logged in
368 public Sone getCurrentSone(ToadletContext toadletContext) {
369 return getCurrentSone(toadletContext, true);
373 * Returns the currently logged in Sone.
375 * @param toadletContext
376 * The toadlet context
378 * {@code true} to create a new session if no session exists,
379 * {@code false} to not create a new session
380 * @return The currently logged in Sone, or {@code null} if no Sone is
381 * currently logged in
383 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
384 Collection<Sone> localSones = getCore().getLocalSones();
385 if (localSones.size() == 1) {
386 return localSones.iterator().next();
388 return getCurrentSone(getCurrentSession(toadletContext, create));
392 * Returns the currently logged in Sone.
396 * @return The currently logged in Sone, or {@code null} if no Sone is
397 * currently logged in
399 public Sone getCurrentSone(Session session) {
400 if (session == null) {
403 String soneId = (String) session.getAttribute("Sone.CurrentSone");
404 if (soneId == null) {
407 return getCore().getLocalSone(soneId, false);
411 * Sets the currently logged in Sone.
413 * @param toadletContext
414 * The toadlet context
416 * The Sone to set as currently logged in
418 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
419 Session session = getCurrentSession(toadletContext);
421 session.removeAttribute("Sone.CurrentSone");
423 session.setAttribute("Sone.CurrentSone", sone.getId());
428 * Returns the notification manager.
430 * @return The notification manager
432 public NotificationManager getNotifications() {
433 return notificationManager;
437 * Returns the l10n helper of the node.
439 * @return The node’s l10n helper
441 public BaseL10n getL10n() {
442 return sonePlugin.l10n().getBase();
446 * Returns the session manager of the node.
448 * @return The node’s session manager
450 public SessionManager getSessionManager() {
451 return sonePlugin.pluginRespirator().getSessionManager("Sone");
455 * Returns the node’s form password.
457 * @return The form password
459 public String getFormPassword() {
464 * Returns the posts that have been announced as new in the
465 * {@link #newPostNotification}.
467 * @return The new posts
469 public Set<Post> getNewPosts() {
470 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
474 * Returns the replies that have been announced as new in the
475 * {@link #newReplyNotification}.
477 * @return The new replies
479 public Set<PostReply> getNewReplies() {
480 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
484 * Sets whether the current start of the plugin is the first start. It is
485 * considered a first start if the configuration file does not exist.
488 * {@code true} if no configuration file existed when Sone was
489 * loaded, {@code false} otherwise
491 public void setFirstStart(boolean firstStart) {
493 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
494 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
495 notificationManager.addNotification(firstStartNotification);
500 * Sets whether Sone was started with a fresh configuration file.
503 * {@code true} if Sone was started with a fresh configuration,
504 * {@code false} if the existing configuration could be read
506 public void setNewConfig(boolean newConfig) {
507 if (newConfig && !hasFirstStartNotification()) {
508 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
509 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
510 notificationManager.addNotification(configNotReadNotification);
519 * Returns whether the first start notification is currently displayed.
521 * @return {@code true} if the first-start notification is currently
522 * displayed, {@code false} otherwise
524 private boolean hasFirstStartNotification() {
525 return notificationManager.getNotification("first-start-notification") != null;
533 * Starts the web interface and registers all toadlets.
535 public void start() {
538 /* notification templates. */
539 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
541 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
542 notificationManager.addNotification(startupNotification);
544 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
548 startupNotification.dismiss();
550 }, "Sone Startup Notification Remover");
552 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
553 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
554 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
557 @SuppressWarnings("synthetic-access")
559 if (getCore().getIdentityManager().isConnected()) {
560 wotMissingNotification.dismiss();
562 notificationManager.addNotification(wotMissingNotification);
564 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
567 }, "Sone WoT Connector Checker");
571 * Stops the web interface and unregisters all toadlets.
574 unregisterToadlets();
575 Ticker.getInstance().stop();
583 * Register all toadlets.
585 private void registerToadlets() {
586 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
587 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
588 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
589 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
590 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
591 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
592 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
593 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
594 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
595 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
596 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
597 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
598 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
599 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
600 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
601 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
602 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
603 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
604 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
605 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
606 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
607 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
608 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
609 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
610 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
611 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
612 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
613 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
614 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
615 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
617 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
697 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
698 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
699 for (PageToadlet toadlet : pageToadlets) {
700 String menuName = toadlet.getMenuName();
701 if (menuName != null) {
702 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
704 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
710 * Unregisters all toadlets.
712 private void unregisterToadlets() {
713 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
714 for (PageToadlet pageToadlet : pageToadlets) {
715 toadletContainer.unregister(pageToadlet);
717 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
721 * Creates a {@link Reader} from the {@link InputStream} for the resource
722 * with the given name.
724 * @param resourceName
725 * The name of the resource
726 * @return A {@link Reader} for the resource
728 private Reader createReader(String resourceName) {
730 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
731 } catch (UnsupportedEncodingException uee1) {
737 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
738 * {@link SonePart}s in the given text (after parsing it using
739 * {@link SoneTextParser}).
743 * @return All mentioned local Sones
745 private Collection<Sone> getMentionedSones(String text) {
746 /* we need no context to find mentioned Sones. */
747 Set<Sone> mentionedSones = new HashSet<Sone>();
749 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
750 if (part instanceof SonePart) {
751 mentionedSones.add(((SonePart) part).getSone());
754 } catch (IOException ioe1) {
755 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
757 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
761 * Returns the Sone insert notification for the given Sone. If no
762 * notification for the given Sone exists, a new notification is created and
766 * The Sone to get the insert notification for
767 * @return The Sone insert notification
769 private TemplateNotification getSoneInsertNotification(Sone sone) {
770 synchronized (soneInsertNotifications) {
771 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
772 if (templateNotification == null) {
773 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
774 templateNotification.set("insertSone", sone);
775 soneInsertNotifications.put(sone, templateNotification);
777 return templateNotification;
789 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
790 newSoneNotification.add(newSoneFoundEvent.sone());
791 if (!hasFirstStartNotification()) {
792 notificationManager.addNotification(newSoneNotification);
800 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
801 Post post = newPostFoundEvent.post();
802 boolean isLocal = post.getSone().isLocal();
804 localPostNotification.add(post);
806 newPostNotification.add(post);
808 if (!hasFirstStartNotification()) {
809 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
810 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
811 mentionNotification.add(post);
812 notificationManager.addNotification(mentionNotification);
815 getCore().markPostKnown(post);
823 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
824 PostReply reply = newPostReplyFoundEvent.postReply();
825 boolean isLocal = reply.getSone().isLocal();
827 localReplyNotification.add(reply);
829 newReplyNotification.add(reply);
831 if (!hasFirstStartNotification()) {
832 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
833 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
834 mentionNotification.add(reply.getPost());
835 notificationManager.addNotification(mentionNotification);
838 getCore().markReplyKnown(reply);
846 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
847 newSoneNotification.remove(markSoneKnownEvent.sone());
851 // CORELISTENER METHODS
858 public void markPostKnown(Post post) {
859 newPostNotification.remove(post);
860 localPostNotification.remove(post);
861 mentionNotification.remove(post);
868 public void markReplyKnown(PostReply reply) {
869 newReplyNotification.remove(reply);
870 localReplyNotification.remove(reply);
871 mentionNotification.remove(reply.getPost());
878 public void soneRemoved(Sone sone) {
879 newSoneNotification.remove(sone);
886 public void postRemoved(Post post) {
887 newPostNotification.remove(post);
888 localPostNotification.remove(post);
889 mentionNotification.remove(post);
896 public void replyRemoved(PostReply reply) {
897 newReplyNotification.remove(reply);
898 localReplyNotification.remove(reply);
899 if (!getMentionedSones(reply.getText()).isEmpty()) {
900 boolean isMentioned = false;
901 for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
902 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
905 mentionNotification.remove(reply.getPost());
914 public void soneLocked(final Sone sone) {
915 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
918 @SuppressWarnings("synthetic-access")
920 lockedSonesNotification.add(sone);
921 lockedSonesTickerObjects.remove(sone);
922 notificationManager.addNotification(lockedSonesNotification);
924 }, "Sone Locked Notification");
925 lockedSonesTickerObjects.put(sone, tickerObject);
932 public void soneUnlocked(Sone sone) {
933 lockedSonesNotification.remove(sone);
934 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
941 public void soneInserting(Sone sone) {
942 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
943 soneInsertNotification.set("soneStatus", "inserting");
944 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
945 notificationManager.addNotification(soneInsertNotification);
953 public void soneInserted(Sone sone, long insertDuration) {
954 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
955 soneInsertNotification.set("soneStatus", "inserted");
956 soneInsertNotification.set("insertDuration", insertDuration / 1000);
957 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
958 notificationManager.addNotification(soneInsertNotification);
966 public void soneInsertAborted(Sone sone, Throwable cause) {
967 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
968 soneInsertNotification.set("soneStatus", "insert-aborted");
969 soneInsertNotification.set("insert-error", cause);
970 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
971 notificationManager.addNotification(soneInsertNotification);
979 public void updateFound(Version version, long releaseTime, long latestEdition) {
980 newVersionNotification.getTemplateContext().set("latestVersion", version);
981 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
982 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
983 notificationManager.addNotification(newVersionNotification);
990 public void imageInsertStarted(Image image) {
991 insertingImagesNotification.add(image);
992 notificationManager.addNotification(insertingImagesNotification);
999 public void imageInsertAborted(Image image) {
1000 insertingImagesNotification.remove(image);
1007 public void imageInsertFinished(Image image) {
1008 insertingImagesNotification.remove(image);
1009 insertedImagesNotification.add(image);
1010 notificationManager.addNotification(insertedImagesNotification);
1017 public void imageInsertFailed(Image image, Throwable cause) {
1018 insertingImagesNotification.remove(image);
1019 imageInsertFailedNotification.add(image);
1020 notificationManager.addNotification(imageInsertFailedNotification);