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.MarkPostKnownEvent;
41 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
42 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
43 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
44 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
45 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
46 import net.pterodactylus.sone.core.event.PostRemovedEvent;
47 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
48 import net.pterodactylus.sone.data.Album;
49 import net.pterodactylus.sone.data.Image;
50 import net.pterodactylus.sone.data.Post;
51 import net.pterodactylus.sone.data.PostReply;
52 import net.pterodactylus.sone.data.Profile;
53 import net.pterodactylus.sone.data.Reply;
54 import net.pterodactylus.sone.data.Sone;
55 import net.pterodactylus.sone.freenet.L10nFilter;
56 import net.pterodactylus.sone.freenet.wot.Identity;
57 import net.pterodactylus.sone.freenet.wot.Trust;
58 import net.pterodactylus.sone.main.SonePlugin;
59 import net.pterodactylus.sone.notify.ListNotification;
60 import net.pterodactylus.sone.template.AlbumAccessor;
61 import net.pterodactylus.sone.template.CollectionAccessor;
62 import net.pterodactylus.sone.template.CssClassNameFilter;
63 import net.pterodactylus.sone.template.HttpRequestAccessor;
64 import net.pterodactylus.sone.template.IdentityAccessor;
65 import net.pterodactylus.sone.template.ImageAccessor;
66 import net.pterodactylus.sone.template.ImageLinkFilter;
67 import net.pterodactylus.sone.template.JavascriptFilter;
68 import net.pterodactylus.sone.template.ParserFilter;
69 import net.pterodactylus.sone.template.PostAccessor;
70 import net.pterodactylus.sone.template.ProfileAccessor;
71 import net.pterodactylus.sone.template.ReplyAccessor;
72 import net.pterodactylus.sone.template.ReplyGroupFilter;
73 import net.pterodactylus.sone.template.RequestChangeFilter;
74 import net.pterodactylus.sone.template.SoneAccessor;
75 import net.pterodactylus.sone.template.SubstringFilter;
76 import net.pterodactylus.sone.template.TrustAccessor;
77 import net.pterodactylus.sone.template.UniqueElementFilter;
78 import net.pterodactylus.sone.template.UnknownDateFilter;
79 import net.pterodactylus.sone.text.Part;
80 import net.pterodactylus.sone.text.SonePart;
81 import net.pterodactylus.sone.text.SoneTextParser;
82 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
83 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
84 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
85 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
86 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
87 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
88 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
89 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
90 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
91 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
92 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
93 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
94 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
95 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
96 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
97 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
98 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
99 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
100 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
101 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
102 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
103 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
104 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
105 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
106 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
107 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
108 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
109 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
110 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
111 import net.pterodactylus.sone.web.page.FreenetRequest;
112 import net.pterodactylus.sone.web.page.PageToadlet;
113 import net.pterodactylus.sone.web.page.PageToadletFactory;
114 import net.pterodactylus.util.logging.Logging;
115 import net.pterodactylus.util.notify.Notification;
116 import net.pterodactylus.util.notify.NotificationManager;
117 import net.pterodactylus.util.notify.TemplateNotification;
118 import net.pterodactylus.util.template.ClassPathTemplateProvider;
119 import net.pterodactylus.util.template.CollectionSortFilter;
120 import net.pterodactylus.util.template.ContainsFilter;
121 import net.pterodactylus.util.template.DateFilter;
122 import net.pterodactylus.util.template.FormatFilter;
123 import net.pterodactylus.util.template.HtmlFilter;
124 import net.pterodactylus.util.template.MatchFilter;
125 import net.pterodactylus.util.template.ModFilter;
126 import net.pterodactylus.util.template.PaginationFilter;
127 import net.pterodactylus.util.template.ReflectionAccessor;
128 import net.pterodactylus.util.template.ReplaceFilter;
129 import net.pterodactylus.util.template.StoreFilter;
130 import net.pterodactylus.util.template.Template;
131 import net.pterodactylus.util.template.TemplateContextFactory;
132 import net.pterodactylus.util.template.TemplateParser;
133 import net.pterodactylus.util.template.TemplateProvider;
134 import net.pterodactylus.util.template.XmlFilter;
135 import net.pterodactylus.util.thread.Ticker;
136 import net.pterodactylus.util.version.Version;
137 import net.pterodactylus.util.web.RedirectPage;
138 import net.pterodactylus.util.web.StaticPage;
139 import net.pterodactylus.util.web.TemplatePage;
141 import com.google.common.collect.Collections2;
142 import com.google.common.collect.ImmutableSet;
143 import com.google.common.eventbus.Subscribe;
144 import com.google.inject.Inject;
146 import freenet.clients.http.SessionManager;
147 import freenet.clients.http.SessionManager.Session;
148 import freenet.clients.http.ToadletContainer;
149 import freenet.clients.http.ToadletContext;
150 import freenet.l10n.BaseL10n;
151 import freenet.support.api.HTTPRequest;
154 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
155 * references to l10n helpers.
157 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
159 public class WebInterface implements CoreListener {
162 private static final Logger logger = Logging.getLogger(WebInterface.class);
164 /** The notification manager. */
165 private final NotificationManager notificationManager = new NotificationManager();
167 /** The Sone plugin. */
168 private final SonePlugin sonePlugin;
170 /** The registered toadlets. */
171 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
173 /** The form password. */
174 private final String formPassword;
176 /** The template context factory. */
177 private final TemplateContextFactory templateContextFactory;
179 /** The Sone text parser. */
180 private final SoneTextParser soneTextParser;
182 /** The parser filter. */
183 private final ParserFilter parserFilter;
185 /** The “new Sone” notification. */
186 private final ListNotification<Sone> newSoneNotification;
188 /** The “new post” notification. */
189 private final ListNotification<Post> newPostNotification;
191 /** The “new reply” notification. */
192 private final ListNotification<PostReply> newReplyNotification;
194 /** The invisible “local post” notification. */
195 private final ListNotification<Post> localPostNotification;
197 /** The invisible “local reply” notification. */
198 private final ListNotification<PostReply> localReplyNotification;
200 /** The “you have been mentioned” notification. */
201 private final ListNotification<Post> mentionNotification;
203 /** Notifications for sone inserts. */
204 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
206 /** Sone locked notification ticker objects. */
207 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
209 /** The “Sone locked” notification. */
210 private final ListNotification<Sone> lockedSonesNotification;
212 /** The “new version” notification. */
213 private final TemplateNotification newVersionNotification;
215 /** The “inserting images” notification. */
216 private final ListNotification<Image> insertingImagesNotification;
218 /** The “inserted images” notification. */
219 private final ListNotification<Image> insertedImagesNotification;
221 /** The “image insert failed” notification. */
222 private final ListNotification<Image> imageInsertFailedNotification;
225 * Creates a new web interface.
231 public WebInterface(SonePlugin sonePlugin) {
232 this.sonePlugin = sonePlugin;
233 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
234 soneTextParser = new SoneTextParser(getCore(), getCore());
236 templateContextFactory = new TemplateContextFactory();
237 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
238 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
239 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
240 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
241 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
242 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
243 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
244 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
245 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
246 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
247 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
248 templateContextFactory.addFilter("date", new DateFilter());
249 templateContextFactory.addFilter("html", new HtmlFilter());
250 templateContextFactory.addFilter("replace", new ReplaceFilter());
251 templateContextFactory.addFilter("store", new StoreFilter());
252 templateContextFactory.addFilter("l10n", new L10nFilter(this));
253 templateContextFactory.addFilter("substring", new SubstringFilter());
254 templateContextFactory.addFilter("xml", new XmlFilter());
255 templateContextFactory.addFilter("change", new RequestChangeFilter());
256 templateContextFactory.addFilter("match", new MatchFilter());
257 templateContextFactory.addFilter("css", new CssClassNameFilter());
258 templateContextFactory.addFilter("js", new JavascriptFilter());
259 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
260 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
261 templateContextFactory.addFilter("format", new FormatFilter());
262 templateContextFactory.addFilter("sort", new CollectionSortFilter());
263 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
264 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
265 templateContextFactory.addFilter("in", new ContainsFilter());
266 templateContextFactory.addFilter("unique", new UniqueElementFilter());
267 templateContextFactory.addFilter("mod", new ModFilter());
268 templateContextFactory.addFilter("paginate", new PaginationFilter());
269 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
270 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
271 templateContextFactory.addTemplateObject("webInterface", this);
272 templateContextFactory.addTemplateObject("formPassword", formPassword);
274 /* create notifications. */
275 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
276 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
278 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
279 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
281 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
282 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
284 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
285 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
287 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
288 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
290 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
291 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
293 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
294 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
296 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
297 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
299 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
300 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
302 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
303 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
305 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
306 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
314 * Returns the Sone core used by the Sone plugin.
316 * @return The Sone core
318 public Core getCore() {
319 return sonePlugin.core();
323 * Returns the template context factory of the web interface.
325 * @return The template context factory
327 public TemplateContextFactory getTemplateContextFactory() {
328 return templateContextFactory;
332 * Returns the current session, creating a new session if there is no
335 * @param toadletContenxt
336 * The toadlet context
337 * @return The current session, or {@code null} if there is no current
340 public Session getCurrentSession(ToadletContext toadletContenxt) {
341 return getCurrentSession(toadletContenxt, true);
345 * Returns the current session, creating a new session if there is no
346 * current session and {@code create} is {@code true}.
348 * @param toadletContenxt
349 * The toadlet context
351 * {@code true} to create a new session if there is no current
352 * session, {@code false} otherwise
353 * @return The current session, or {@code null} if there is no current
356 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
357 Session session = getSessionManager().useSession(toadletContenxt);
358 if (create && (session == null)) {
359 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
365 * Returns the currently logged in Sone.
367 * @param toadletContext
368 * The toadlet context
369 * @return The currently logged in Sone, or {@code null} if no Sone is
370 * currently logged in
372 public Sone getCurrentSone(ToadletContext toadletContext) {
373 return getCurrentSone(toadletContext, true);
377 * Returns the currently logged in Sone.
379 * @param toadletContext
380 * The toadlet context
382 * {@code true} to create a new session if no session exists,
383 * {@code false} to not create a new session
384 * @return The currently logged in Sone, or {@code null} if no Sone is
385 * currently logged in
387 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
388 Collection<Sone> localSones = getCore().getLocalSones();
389 if (localSones.size() == 1) {
390 return localSones.iterator().next();
392 return getCurrentSone(getCurrentSession(toadletContext, create));
396 * Returns the currently logged in Sone.
400 * @return The currently logged in Sone, or {@code null} if no Sone is
401 * currently logged in
403 public Sone getCurrentSone(Session session) {
404 if (session == null) {
407 String soneId = (String) session.getAttribute("Sone.CurrentSone");
408 if (soneId == null) {
411 return getCore().getLocalSone(soneId, false);
415 * Sets the currently logged in Sone.
417 * @param toadletContext
418 * The toadlet context
420 * The Sone to set as currently logged in
422 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
423 Session session = getCurrentSession(toadletContext);
425 session.removeAttribute("Sone.CurrentSone");
427 session.setAttribute("Sone.CurrentSone", sone.getId());
432 * Returns the notification manager.
434 * @return The notification manager
436 public NotificationManager getNotifications() {
437 return notificationManager;
441 * Returns the l10n helper of the node.
443 * @return The node’s l10n helper
445 public BaseL10n getL10n() {
446 return sonePlugin.l10n().getBase();
450 * Returns the session manager of the node.
452 * @return The node’s session manager
454 public SessionManager getSessionManager() {
455 return sonePlugin.pluginRespirator().getSessionManager("Sone");
459 * Returns the node’s form password.
461 * @return The form password
463 public String getFormPassword() {
468 * Returns the posts that have been announced as new in the
469 * {@link #newPostNotification}.
471 * @return The new posts
473 public Set<Post> getNewPosts() {
474 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
478 * Returns the replies that have been announced as new in the
479 * {@link #newReplyNotification}.
481 * @return The new replies
483 public Set<PostReply> getNewReplies() {
484 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
488 * Sets whether the current start of the plugin is the first start. It is
489 * considered a first start if the configuration file does not exist.
492 * {@code true} if no configuration file existed when Sone was
493 * loaded, {@code false} otherwise
495 public void setFirstStart(boolean firstStart) {
497 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
498 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
499 notificationManager.addNotification(firstStartNotification);
504 * Sets whether Sone was started with a fresh configuration file.
507 * {@code true} if Sone was started with a fresh configuration,
508 * {@code false} if the existing configuration could be read
510 public void setNewConfig(boolean newConfig) {
511 if (newConfig && !hasFirstStartNotification()) {
512 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
513 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
514 notificationManager.addNotification(configNotReadNotification);
523 * Returns whether the first start notification is currently displayed.
525 * @return {@code true} if the first-start notification is currently
526 * displayed, {@code false} otherwise
528 private boolean hasFirstStartNotification() {
529 return notificationManager.getNotification("first-start-notification") != null;
537 * Starts the web interface and registers all toadlets.
539 public void start() {
542 /* notification templates. */
543 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
545 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
546 notificationManager.addNotification(startupNotification);
548 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
552 startupNotification.dismiss();
554 }, "Sone Startup Notification Remover");
556 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
557 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
558 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
561 @SuppressWarnings("synthetic-access")
563 if (getCore().getIdentityManager().isConnected()) {
564 wotMissingNotification.dismiss();
566 notificationManager.addNotification(wotMissingNotification);
568 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
571 }, "Sone WoT Connector Checker");
575 * Stops the web interface and unregisters all toadlets.
578 unregisterToadlets();
579 Ticker.getInstance().stop();
587 * Register all toadlets.
589 private void registerToadlets() {
590 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
591 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
592 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
593 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
594 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
595 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
596 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
597 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
598 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
599 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
600 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
601 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
602 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
603 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
604 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
605 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
606 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
607 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
608 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
609 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
610 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
611 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
612 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
613 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
614 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
615 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
616 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
617 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
618 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
619 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
621 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
701 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
702 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
703 for (PageToadlet toadlet : pageToadlets) {
704 String menuName = toadlet.getMenuName();
705 if (menuName != null) {
706 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
708 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
714 * Unregisters all toadlets.
716 private void unregisterToadlets() {
717 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
718 for (PageToadlet pageToadlet : pageToadlets) {
719 toadletContainer.unregister(pageToadlet);
721 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
725 * Creates a {@link Reader} from the {@link InputStream} for the resource
726 * with the given name.
728 * @param resourceName
729 * The name of the resource
730 * @return A {@link Reader} for the resource
732 private Reader createReader(String resourceName) {
734 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
735 } catch (UnsupportedEncodingException uee1) {
741 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
742 * {@link SonePart}s in the given text (after parsing it using
743 * {@link SoneTextParser}).
747 * @return All mentioned local Sones
749 private Collection<Sone> getMentionedSones(String text) {
750 /* we need no context to find mentioned Sones. */
751 Set<Sone> mentionedSones = new HashSet<Sone>();
753 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
754 if (part instanceof SonePart) {
755 mentionedSones.add(((SonePart) part).getSone());
758 } catch (IOException ioe1) {
759 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
761 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
765 * Returns the Sone insert notification for the given Sone. If no
766 * notification for the given Sone exists, a new notification is created and
770 * The Sone to get the insert notification for
771 * @return The Sone insert notification
773 private TemplateNotification getSoneInsertNotification(Sone sone) {
774 synchronized (soneInsertNotifications) {
775 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
776 if (templateNotification == null) {
777 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
778 templateNotification.set("insertSone", sone);
779 soneInsertNotifications.put(sone, templateNotification);
781 return templateNotification;
790 * Notifies the web interface that a new {@link Sone} was found.
792 * @param newSoneFoundEvent
796 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
797 newSoneNotification.add(newSoneFoundEvent.sone());
798 if (!hasFirstStartNotification()) {
799 notificationManager.addNotification(newSoneNotification);
804 * Notifies the web interface that a new {@link Post} was found.
806 * @param newPostFoundEvent
810 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
811 Post post = newPostFoundEvent.post();
812 boolean isLocal = post.getSone().isLocal();
814 localPostNotification.add(post);
816 newPostNotification.add(post);
818 if (!hasFirstStartNotification()) {
819 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
820 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
821 mentionNotification.add(post);
822 notificationManager.addNotification(mentionNotification);
825 getCore().markPostKnown(post);
830 * Notifies the web interface that a new {@link PostReply} was found.
832 * @param newPostReplyFoundEvent
836 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
837 PostReply reply = newPostReplyFoundEvent.postReply();
838 boolean isLocal = reply.getSone().isLocal();
840 localReplyNotification.add(reply);
842 newReplyNotification.add(reply);
844 if (!hasFirstStartNotification()) {
845 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
846 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
847 mentionNotification.add(reply.getPost());
848 notificationManager.addNotification(mentionNotification);
851 getCore().markReplyKnown(reply);
856 * Notifies the web interface that a {@link Sone} was marked as known.
858 * @param markSoneKnownEvent
862 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
863 newSoneNotification.remove(markSoneKnownEvent.sone());
867 * Notifies the web interface that a {@link Post} was marked as known.
869 * @param markPostKnownEvent
873 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
874 newPostNotification.remove(markPostKnownEvent.post());
875 localPostNotification.remove(markPostKnownEvent.post());
876 mentionNotification.remove(markPostKnownEvent.post());
880 * Notifies the web interface that a {@link PostReply} was marked as known.
882 * @param markPostReplyKnownEvent
886 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
887 newReplyNotification.remove(markPostReplyKnownEvent.postReply());
888 localReplyNotification.remove(markPostReplyKnownEvent.postReply());
889 mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
893 * Notifies the web interface that a {@link Sone} was removed.
895 * @param soneRemovedEvent
899 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
900 newSoneNotification.remove(soneRemovedEvent.sone());
904 * Notifies the web interface that a {@link Post} was removed.
906 * @param postRemovedEvent
910 public void postRemoved(PostRemovedEvent postRemovedEvent) {
911 newPostNotification.remove(postRemovedEvent.post());
912 localPostNotification.remove(postRemovedEvent.post());
913 mentionNotification.remove(postRemovedEvent.post());
917 // CORELISTENER METHODS
924 public void replyRemoved(PostReply reply) {
925 newReplyNotification.remove(reply);
926 localReplyNotification.remove(reply);
927 if (!getMentionedSones(reply.getText()).isEmpty()) {
928 boolean isMentioned = false;
929 for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
930 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
933 mentionNotification.remove(reply.getPost());
942 public void soneLocked(final Sone sone) {
943 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
946 @SuppressWarnings("synthetic-access")
948 lockedSonesNotification.add(sone);
949 lockedSonesTickerObjects.remove(sone);
950 notificationManager.addNotification(lockedSonesNotification);
952 }, "Sone Locked Notification");
953 lockedSonesTickerObjects.put(sone, tickerObject);
960 public void soneUnlocked(Sone sone) {
961 lockedSonesNotification.remove(sone);
962 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
969 public void soneInserting(Sone sone) {
970 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
971 soneInsertNotification.set("soneStatus", "inserting");
972 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
973 notificationManager.addNotification(soneInsertNotification);
981 public void soneInserted(Sone sone, long insertDuration) {
982 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
983 soneInsertNotification.set("soneStatus", "inserted");
984 soneInsertNotification.set("insertDuration", insertDuration / 1000);
985 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
986 notificationManager.addNotification(soneInsertNotification);
994 public void soneInsertAborted(Sone sone, Throwable cause) {
995 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
996 soneInsertNotification.set("soneStatus", "insert-aborted");
997 soneInsertNotification.set("insert-error", cause);
998 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
999 notificationManager.addNotification(soneInsertNotification);
1007 public void updateFound(Version version, long releaseTime, long latestEdition) {
1008 newVersionNotification.getTemplateContext().set("latestVersion", version);
1009 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
1010 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
1011 notificationManager.addNotification(newVersionNotification);
1018 public void imageInsertStarted(Image image) {
1019 insertingImagesNotification.add(image);
1020 notificationManager.addNotification(insertingImagesNotification);
1027 public void imageInsertAborted(Image image) {
1028 insertingImagesNotification.remove(image);
1035 public void imageInsertFinished(Image image) {
1036 insertingImagesNotification.remove(image);
1037 insertedImagesNotification.add(image);
1038 notificationManager.addNotification(insertedImagesNotification);
1045 public void imageInsertFailed(Image image, Throwable cause) {
1046 insertingImagesNotification.remove(image);
1047 imageInsertFailedNotification.add(image);
1048 notificationManager.addNotification(imageInsertFailedNotification);