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.data.Album;
41 import net.pterodactylus.sone.data.Image;
42 import net.pterodactylus.sone.data.Post;
43 import net.pterodactylus.sone.data.PostReply;
44 import net.pterodactylus.sone.data.Profile;
45 import net.pterodactylus.sone.data.Reply;
46 import net.pterodactylus.sone.data.Sone;
47 import net.pterodactylus.sone.freenet.L10nFilter;
48 import net.pterodactylus.sone.freenet.wot.Identity;
49 import net.pterodactylus.sone.freenet.wot.Trust;
50 import net.pterodactylus.sone.main.SonePlugin;
51 import net.pterodactylus.sone.notify.ListNotification;
52 import net.pterodactylus.sone.template.AlbumAccessor;
53 import net.pterodactylus.sone.template.CollectionAccessor;
54 import net.pterodactylus.sone.template.CssClassNameFilter;
55 import net.pterodactylus.sone.template.HttpRequestAccessor;
56 import net.pterodactylus.sone.template.IdentityAccessor;
57 import net.pterodactylus.sone.template.ImageAccessor;
58 import net.pterodactylus.sone.template.ImageLinkFilter;
59 import net.pterodactylus.sone.template.JavascriptFilter;
60 import net.pterodactylus.sone.template.ParserFilter;
61 import net.pterodactylus.sone.template.PostAccessor;
62 import net.pterodactylus.sone.template.ProfileAccessor;
63 import net.pterodactylus.sone.template.ReplyAccessor;
64 import net.pterodactylus.sone.template.ReplyGroupFilter;
65 import net.pterodactylus.sone.template.RequestChangeFilter;
66 import net.pterodactylus.sone.template.SoneAccessor;
67 import net.pterodactylus.sone.template.SubstringFilter;
68 import net.pterodactylus.sone.template.TrustAccessor;
69 import net.pterodactylus.sone.template.UniqueElementFilter;
70 import net.pterodactylus.sone.template.UnknownDateFilter;
71 import net.pterodactylus.sone.text.Part;
72 import net.pterodactylus.sone.text.SonePart;
73 import net.pterodactylus.sone.text.SoneTextParser;
74 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
75 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
76 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
77 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
78 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
79 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
80 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
81 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
82 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
83 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
84 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
85 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
86 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
87 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
88 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
89 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
90 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
91 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
92 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
93 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
94 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
95 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
96 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
97 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
98 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
99 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
100 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
101 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
102 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
103 import net.pterodactylus.sone.web.page.FreenetRequest;
104 import net.pterodactylus.sone.web.page.PageToadlet;
105 import net.pterodactylus.sone.web.page.PageToadletFactory;
106 import net.pterodactylus.util.collection.SetBuilder;
107 import net.pterodactylus.util.collection.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.ClassPathTemplateProvider;
113 import net.pterodactylus.util.template.CollectionSortFilter;
114 import net.pterodactylus.util.template.ContainsFilter;
115 import net.pterodactylus.util.template.DateFilter;
116 import net.pterodactylus.util.template.FormatFilter;
117 import net.pterodactylus.util.template.HtmlFilter;
118 import net.pterodactylus.util.template.MatchFilter;
119 import net.pterodactylus.util.template.ModFilter;
120 import net.pterodactylus.util.template.PaginationFilter;
121 import net.pterodactylus.util.template.ReflectionAccessor;
122 import net.pterodactylus.util.template.ReplaceFilter;
123 import net.pterodactylus.util.template.StoreFilter;
124 import net.pterodactylus.util.template.Template;
125 import net.pterodactylus.util.template.TemplateContextFactory;
126 import net.pterodactylus.util.template.TemplateParser;
127 import net.pterodactylus.util.template.TemplateProvider;
128 import net.pterodactylus.util.template.XmlFilter;
129 import net.pterodactylus.util.thread.Ticker;
130 import net.pterodactylus.util.version.Version;
131 import net.pterodactylus.util.web.RedirectPage;
132 import net.pterodactylus.util.web.StaticPage;
133 import net.pterodactylus.util.web.TemplatePage;
135 import com.google.inject.Inject;
137 import freenet.clients.http.SessionManager;
138 import freenet.clients.http.SessionManager.Session;
139 import freenet.clients.http.ToadletContainer;
140 import freenet.clients.http.ToadletContext;
141 import freenet.l10n.BaseL10n;
142 import freenet.support.api.HTTPRequest;
145 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
146 * references to l10n helpers.
148 * @author <a href="mailto:bombe@pterodactylus.net">David âBombeâ Roden</a>
150 public class WebInterface implements CoreListener {
153 private static final Logger logger = Logging.getLogger(WebInterface.class);
155 /** The notification manager. */
156 private final NotificationManager notificationManager = new NotificationManager();
158 /** The Sone plugin. */
159 private final SonePlugin sonePlugin;
161 /** The registered toadlets. */
162 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
164 /** The form password. */
165 private final String formPassword;
167 /** The template context factory. */
168 private final TemplateContextFactory templateContextFactory;
170 /** The Sone text parser. */
171 private final SoneTextParser soneTextParser;
173 /** The parser filter. */
174 private final ParserFilter parserFilter;
176 /** The ânew Soneâ notification. */
177 private final ListNotification<Sone> newSoneNotification;
179 /** The ânew postâ notification. */
180 private final ListNotification<Post> newPostNotification;
182 /** The ânew replyâ notification. */
183 private final ListNotification<PostReply> newReplyNotification;
185 /** The invisible âlocal postâ notification. */
186 private final ListNotification<Post> localPostNotification;
188 /** The invisible âlocal replyâ notification. */
189 private final ListNotification<PostReply> localReplyNotification;
191 /** The âyou have been mentionedâ notification. */
192 private final ListNotification<Post> mentionNotification;
194 /** Notifications for sone inserts. */
195 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
197 /** Sone locked notification ticker objects. */
198 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
200 /** The âSone lockedâ notification. */
201 private final ListNotification<Sone> lockedSonesNotification;
203 /** The ânew versionâ notification. */
204 private final TemplateNotification newVersionNotification;
206 /** The âinserting imagesâ notification. */
207 private final ListNotification<Image> insertingImagesNotification;
209 /** The âinserted imagesâ notification. */
210 private final ListNotification<Image> insertedImagesNotification;
212 /** The âimage insert failedâ notification. */
213 private final ListNotification<Image> imageInsertFailedNotification;
216 * Creates a new web interface.
222 public WebInterface(SonePlugin sonePlugin) {
223 this.sonePlugin = sonePlugin;
224 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
225 soneTextParser = new SoneTextParser(getCore(), getCore());
227 templateContextFactory = new TemplateContextFactory();
228 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
229 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
230 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
231 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
232 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
233 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
234 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
235 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
236 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
237 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
238 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
239 templateContextFactory.addFilter("date", new DateFilter());
240 templateContextFactory.addFilter("html", new HtmlFilter());
241 templateContextFactory.addFilter("replace", new ReplaceFilter());
242 templateContextFactory.addFilter("store", new StoreFilter());
243 templateContextFactory.addFilter("l10n", new L10nFilter(this));
244 templateContextFactory.addFilter("substring", new SubstringFilter());
245 templateContextFactory.addFilter("xml", new XmlFilter());
246 templateContextFactory.addFilter("change", new RequestChangeFilter());
247 templateContextFactory.addFilter("match", new MatchFilter());
248 templateContextFactory.addFilter("css", new CssClassNameFilter());
249 templateContextFactory.addFilter("js", new JavascriptFilter());
250 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
251 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
252 templateContextFactory.addFilter("format", new FormatFilter());
253 templateContextFactory.addFilter("sort", new CollectionSortFilter());
254 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
255 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
256 templateContextFactory.addFilter("in", new ContainsFilter());
257 templateContextFactory.addFilter("unique", new UniqueElementFilter());
258 templateContextFactory.addFilter("mod", new ModFilter());
259 templateContextFactory.addFilter("paginate", new PaginationFilter());
260 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
261 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
262 templateContextFactory.addTemplateObject("webInterface", this);
263 templateContextFactory.addTemplateObject("formPassword", formPassword);
265 /* create notifications. */
266 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
267 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
269 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
270 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
272 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
273 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
275 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
276 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
278 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
279 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
281 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
282 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
284 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
285 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
287 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
288 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
290 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
291 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
293 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
294 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
296 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
297 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
305 * Returns the Sone core used by the Sone plugin.
307 * @return The Sone core
309 public Core getCore() {
310 return sonePlugin.core();
314 * Returns the template context factory of the web interface.
316 * @return The template context factory
318 public TemplateContextFactory getTemplateContextFactory() {
319 return templateContextFactory;
323 * Returns the current session, creating a new session if there is no
326 * @param toadletContenxt
327 * The toadlet context
328 * @return The current session, or {@code null} if there is no current
331 public Session getCurrentSession(ToadletContext toadletContenxt) {
332 return getCurrentSession(toadletContenxt, true);
336 * Returns the current session, creating a new session if there is no
337 * current session and {@code create} is {@code true}.
339 * @param toadletContenxt
340 * The toadlet context
342 * {@code true} to create a new session if there is no current
343 * session, {@code false} otherwise
344 * @return The current session, or {@code null} if there is no current
347 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
348 Session session = getSessionManager().useSession(toadletContenxt);
349 if (create && (session == null)) {
350 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
356 * Returns the currently logged in Sone.
358 * @param toadletContext
359 * The toadlet context
360 * @return The currently logged in Sone, or {@code null} if no Sone is
361 * currently logged in
363 public Sone getCurrentSone(ToadletContext toadletContext) {
364 return getCurrentSone(toadletContext, true);
368 * Returns the currently logged in Sone.
370 * @param toadletContext
371 * The toadlet context
373 * {@code true} to create a new session if no session exists,
374 * {@code false} to not create a new session
375 * @return The currently logged in Sone, or {@code null} if no Sone is
376 * currently logged in
378 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
379 Collection<Sone> localSones = getCore().getLocalSones();
380 if (localSones.size() == 1) {
381 return localSones.iterator().next();
383 return getCurrentSone(getCurrentSession(toadletContext, create));
387 * Returns the currently logged in Sone.
391 * @return The currently logged in Sone, or {@code null} if no Sone is
392 * currently logged in
394 public Sone getCurrentSone(Session session) {
395 if (session == null) {
398 String soneId = (String) session.getAttribute("Sone.CurrentSone");
399 if (soneId == null) {
402 return getCore().getLocalSone(soneId, false);
406 * Sets the currently logged in Sone.
408 * @param toadletContext
409 * The toadlet context
411 * The Sone to set as currently logged in
413 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
414 Session session = getCurrentSession(toadletContext);
416 session.removeAttribute("Sone.CurrentSone");
418 session.setAttribute("Sone.CurrentSone", sone.getId());
423 * Returns the notification manager.
425 * @return The notification manager
427 public NotificationManager getNotifications() {
428 return notificationManager;
432 * Returns the l10n helper of the node.
434 * @return The nodeâs l10n helper
436 public BaseL10n getL10n() {
437 return sonePlugin.l10n().getBase();
441 * Returns the session manager of the node.
443 * @return The nodeâs session manager
445 public SessionManager getSessionManager() {
446 return sonePlugin.pluginRespirator().getSessionManager("Sone");
450 * Returns the nodeâs form password.
452 * @return The form password
454 public String getFormPassword() {
459 * Returns the posts that have been announced as new in the
460 * {@link #newPostNotification}.
462 * @return The new posts
464 public Set<Post> getNewPosts() {
465 return new SetBuilder<Post>().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get();
469 * Returns the replies that have been announced as new in the
470 * {@link #newReplyNotification}.
472 * @return The new replies
474 public Set<PostReply> getNewReplies() {
475 return new SetBuilder<PostReply>().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get();
479 * Sets whether the current start of the plugin is the first start. It is
480 * considered a first start if the configuration file does not exist.
483 * {@code true} if no configuration file existed when Sone was
484 * loaded, {@code false} otherwise
486 public void setFirstStart(boolean firstStart) {
488 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
489 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
490 notificationManager.addNotification(firstStartNotification);
495 * Sets whether Sone was started with a fresh configuration file.
498 * {@code true} if Sone was started with a fresh configuration,
499 * {@code false} if the existing configuration could be read
501 public void setNewConfig(boolean newConfig) {
502 if (newConfig && !hasFirstStartNotification()) {
503 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
504 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
505 notificationManager.addNotification(configNotReadNotification);
514 * Returns whether the first start notification is currently displayed.
516 * @return {@code true} if the first-start notification is currently
517 * displayed, {@code false} otherwise
519 private boolean hasFirstStartNotification() {
520 return notificationManager.getNotification("first-start-notification") != null;
528 * Starts the web interface and registers all toadlets.
530 public void start() {
533 /* notification templates. */
534 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
536 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
537 notificationManager.addNotification(startupNotification);
539 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
543 startupNotification.dismiss();
545 }, "Sone Startup Notification Remover");
547 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
548 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
549 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
552 @SuppressWarnings("synthetic-access")
554 if (getCore().getIdentityManager().isConnected()) {
555 wotMissingNotification.dismiss();
557 notificationManager.addNotification(wotMissingNotification);
559 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
562 }, "Sone WoT Connector Checker");
566 * Stops the web interface and unregisters all toadlets.
569 unregisterToadlets();
570 Ticker.getInstance().stop();
578 * Register all toadlets.
580 private void registerToadlets() {
581 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
582 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
583 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
584 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
585 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
586 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
587 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
588 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
589 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
590 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
591 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
592 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
593 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
594 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
595 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
596 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
597 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
598 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
599 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
600 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
601 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
602 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
603 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
604 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
605 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
606 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
607 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
608 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
609 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
610 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
612 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
616 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
617 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
692 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
693 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
694 for (PageToadlet toadlet : pageToadlets) {
695 String menuName = toadlet.getMenuName();
696 if (menuName != null) {
697 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
699 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
705 * Unregisters all toadlets.
707 private void unregisterToadlets() {
708 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
709 for (PageToadlet pageToadlet : pageToadlets) {
710 toadletContainer.unregister(pageToadlet);
712 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
716 * Creates a {@link Reader} from the {@link InputStream} for the resource
717 * with the given name.
719 * @param resourceName
720 * The name of the resource
721 * @return A {@link Reader} for the resource
723 private Reader createReader(String resourceName) {
725 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
726 } catch (UnsupportedEncodingException uee1) {
732 * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are
733 * referenced by {@link SonePart}s in the given text (after parsing it using
734 * {@link SoneTextParser}).
738 * @return All mentioned local Sones
740 private Set<Sone> getMentionedSones(String text) {
741 /* we need no context to find mentioned Sones. */
742 Set<Sone> mentionedSones = new HashSet<Sone>();
744 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
745 if (part instanceof SonePart) {
746 mentionedSones.add(((SonePart) part).getSone());
749 } catch (IOException ioe1) {
750 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
752 return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
756 * Returns the Sone insert notification for the given Sone. If no
757 * notification for the given Sone exists, a new notification is created and
761 * The Sone to get the insert notification for
762 * @return The Sone insert notification
764 private TemplateNotification getSoneInsertNotification(Sone sone) {
765 synchronized (soneInsertNotifications) {
766 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
767 if (templateNotification == null) {
768 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
769 templateNotification.set("insertSone", sone);
770 soneInsertNotifications.put(sone, templateNotification);
772 return templateNotification;
777 // CORELISTENER METHODS
784 public void newSoneFound(Sone sone) {
785 newSoneNotification.add(sone);
786 if (!hasFirstStartNotification()) {
787 notificationManager.addNotification(newSoneNotification);
795 public void newPostFound(Post post) {
796 boolean isLocal = post.getSone().isLocal();
798 localPostNotification.add(post);
800 newPostNotification.add(post);
802 if (!hasFirstStartNotification()) {
803 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
804 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
805 mentionNotification.add(post);
806 notificationManager.addNotification(mentionNotification);
809 getCore().markPostKnown(post);
817 public void newReplyFound(PostReply reply) {
818 boolean isLocal = reply.getSone().isLocal();
820 localReplyNotification.add(reply);
822 newReplyNotification.add(reply);
824 if (!hasFirstStartNotification()) {
825 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
826 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
827 mentionNotification.add(reply.getPost());
828 notificationManager.addNotification(mentionNotification);
831 getCore().markReplyKnown(reply);
839 public void markSoneKnown(Sone sone) {
840 newSoneNotification.remove(sone);
847 public void markPostKnown(Post post) {
848 newPostNotification.remove(post);
849 localPostNotification.remove(post);
850 mentionNotification.remove(post);
857 public void markReplyKnown(PostReply reply) {
858 newReplyNotification.remove(reply);
859 localReplyNotification.remove(reply);
860 mentionNotification.remove(reply.getPost());
867 public void soneRemoved(Sone sone) {
868 newSoneNotification.remove(sone);
875 public void postRemoved(Post post) {
876 newPostNotification.remove(post);
877 localPostNotification.remove(post);
878 mentionNotification.remove(post);
885 public void replyRemoved(PostReply reply) {
886 newReplyNotification.remove(reply);
887 localReplyNotification.remove(reply);
888 if (!getMentionedSones(reply.getText()).isEmpty()) {
889 boolean isMentioned = false;
890 for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
891 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
894 mentionNotification.remove(reply.getPost());
903 public void soneLocked(final Sone sone) {
904 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
907 @SuppressWarnings("synthetic-access")
909 lockedSonesNotification.add(sone);
910 lockedSonesTickerObjects.remove(sone);
911 notificationManager.addNotification(lockedSonesNotification);
913 }, "Sone Locked Notification");
914 lockedSonesTickerObjects.put(sone, tickerObject);
921 public void soneUnlocked(Sone sone) {
922 lockedSonesNotification.remove(sone);
923 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
930 public void soneInserting(Sone sone) {
931 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
932 soneInsertNotification.set("soneStatus", "inserting");
933 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
934 notificationManager.addNotification(soneInsertNotification);
942 public void soneInserted(Sone sone, long insertDuration) {
943 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
944 soneInsertNotification.set("soneStatus", "inserted");
945 soneInsertNotification.set("insertDuration", insertDuration / 1000);
946 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
947 notificationManager.addNotification(soneInsertNotification);
955 public void soneInsertAborted(Sone sone, Throwable cause) {
956 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
957 soneInsertNotification.set("soneStatus", "insert-aborted");
958 soneInsertNotification.set("insert-error", cause);
959 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
960 notificationManager.addNotification(soneInsertNotification);
968 public void updateFound(Version version, long releaseTime, long latestEdition) {
969 newVersionNotification.getTemplateContext().set("latestVersion", version);
970 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
971 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
972 notificationManager.addNotification(newVersionNotification);
979 public void imageInsertStarted(Image image) {
980 insertingImagesNotification.add(image);
981 notificationManager.addNotification(insertingImagesNotification);
988 public void imageInsertAborted(Image image) {
989 insertingImagesNotification.remove(image);
996 public void imageInsertFinished(Image image) {
997 insertingImagesNotification.remove(image);
998 insertedImagesNotification.add(image);
999 notificationManager.addNotification(insertedImagesNotification);
1006 public void imageInsertFailed(Image image, Throwable cause) {
1007 insertingImagesNotification.remove(image);
1008 imageInsertFailedNotification.add(image);
1009 notificationManager.addNotification(imageInsertFailedNotification);