2 * FreenetSone - WebInterface.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.StringReader;
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
33 import java.util.UUID;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
37 import net.pterodactylus.sone.core.Core;
38 import net.pterodactylus.sone.core.CoreListener;
39 import net.pterodactylus.sone.data.Album;
40 import net.pterodactylus.sone.data.Image;
41 import net.pterodactylus.sone.data.Post;
42 import net.pterodactylus.sone.data.Reply;
43 import net.pterodactylus.sone.data.Sone;
44 import net.pterodactylus.sone.freenet.L10nFilter;
45 import net.pterodactylus.sone.freenet.wot.Identity;
46 import net.pterodactylus.sone.freenet.wot.Trust;
47 import net.pterodactylus.sone.main.SonePlugin;
48 import net.pterodactylus.sone.notify.ListNotification;
49 import net.pterodactylus.sone.template.AlbumAccessor;
50 import net.pterodactylus.sone.template.CollectionAccessor;
51 import net.pterodactylus.sone.template.CssClassNameFilter;
52 import net.pterodactylus.sone.template.HttpRequestAccessor;
53 import net.pterodactylus.sone.template.IdentityAccessor;
54 import net.pterodactylus.sone.template.ImageLinkFilter;
55 import net.pterodactylus.sone.template.JavascriptFilter;
56 import net.pterodactylus.sone.template.NotificationManagerAccessor;
57 import net.pterodactylus.sone.template.ParserFilter;
58 import net.pterodactylus.sone.template.PostAccessor;
59 import net.pterodactylus.sone.template.ReplyAccessor;
60 import net.pterodactylus.sone.template.ReplyGroupFilter;
61 import net.pterodactylus.sone.template.RequestChangeFilter;
62 import net.pterodactylus.sone.template.SoneAccessor;
63 import net.pterodactylus.sone.template.SubstringFilter;
64 import net.pterodactylus.sone.template.TrustAccessor;
65 import net.pterodactylus.sone.template.UnknownDateFilter;
66 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
67 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
68 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
69 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
70 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
71 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
72 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
73 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
74 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
75 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
76 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
77 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
78 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
79 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
80 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
81 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
82 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
83 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
84 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
85 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
86 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
87 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
88 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
89 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
90 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
91 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
92 import net.pterodactylus.sone.web.page.PageToadlet;
93 import net.pterodactylus.sone.web.page.PageToadletFactory;
94 import net.pterodactylus.sone.web.page.RedirectPage;
95 import net.pterodactylus.sone.web.page.StaticPage;
96 import net.pterodactylus.sone.web.page.TemplatePage;
97 import net.pterodactylus.util.cache.Cache;
98 import net.pterodactylus.util.cache.CacheException;
99 import net.pterodactylus.util.cache.CacheItem;
100 import net.pterodactylus.util.cache.DefaultCacheItem;
101 import net.pterodactylus.util.cache.MemoryCache;
102 import net.pterodactylus.util.cache.ValueRetriever;
103 import net.pterodactylus.util.logging.Logging;
104 import net.pterodactylus.util.notify.Notification;
105 import net.pterodactylus.util.notify.NotificationManager;
106 import net.pterodactylus.util.notify.TemplateNotification;
107 import net.pterodactylus.util.template.CollectionSortFilter;
108 import net.pterodactylus.util.template.DateFilter;
109 import net.pterodactylus.util.template.FormatFilter;
110 import net.pterodactylus.util.template.HtmlFilter;
111 import net.pterodactylus.util.template.MatchFilter;
112 import net.pterodactylus.util.template.Provider;
113 import net.pterodactylus.util.template.ReflectionAccessor;
114 import net.pterodactylus.util.template.ReplaceFilter;
115 import net.pterodactylus.util.template.StoreFilter;
116 import net.pterodactylus.util.template.Template;
117 import net.pterodactylus.util.template.TemplateContext;
118 import net.pterodactylus.util.template.TemplateContextFactory;
119 import net.pterodactylus.util.template.TemplateException;
120 import net.pterodactylus.util.template.TemplateParser;
121 import net.pterodactylus.util.template.XmlFilter;
122 import net.pterodactylus.util.thread.Ticker;
123 import net.pterodactylus.util.version.Version;
124 import freenet.clients.http.SessionManager;
125 import freenet.clients.http.SessionManager.Session;
126 import freenet.clients.http.ToadletContainer;
127 import freenet.clients.http.ToadletContext;
128 import freenet.l10n.BaseL10n;
129 import freenet.support.api.HTTPRequest;
132 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
133 * references to l10n helpers.
135 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
137 public class WebInterface implements CoreListener {
140 private static final Logger logger = Logging.getLogger(WebInterface.class);
142 /** The notification manager. */
143 private final NotificationManager notificationManager = new NotificationManager();
145 /** The Sone plugin. */
146 private final SonePlugin sonePlugin;
148 /** The registered toadlets. */
149 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
151 /** The form password. */
152 private final String formPassword;
154 /** The template context factory. */
155 private final TemplateContextFactory templateContextFactory;
157 /** The “new Sone” notification. */
158 private final ListNotification<Sone> newSoneNotification;
160 /** The “new post” notification. */
161 private final ListNotification<Post> newPostNotification;
163 /** The “new reply” notification. */
164 private final ListNotification<Reply> newReplyNotification;
166 /** The “rescuing Sone” notification. */
167 private final ListNotification<Sone> rescuingSonesNotification;
169 /** The “Sone rescued” notification. */
170 private final ListNotification<Sone> sonesRescuedNotification;
172 /** Sone locked notification ticker objects. */
173 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
175 /** The “Sone locked” notification. */
176 private final ListNotification<Sone> lockedSonesNotification;
178 /** The “new version” notification. */
179 private final TemplateNotification newVersionNotification;
181 /** The “inserting images” notification. */
182 private final ListNotification<Image> insertingImagesNotification;
184 /** The “inserted images” notification. */
185 private final ListNotification<Image> insertedImagesNotification;
187 /** The “image insert failed” notification. */
188 private final ListNotification<Image> imageInsertFailedNotification;
191 * Creates a new web interface.
196 @SuppressWarnings("synthetic-access")
197 public WebInterface(SonePlugin sonePlugin) {
198 this.sonePlugin = sonePlugin;
199 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
201 templateContextFactory = new TemplateContextFactory();
202 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
203 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
204 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
205 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
206 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
207 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
208 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
209 templateContextFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
210 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
211 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
212 templateContextFactory.addFilter("date", new DateFilter());
213 templateContextFactory.addFilter("html", new HtmlFilter());
214 templateContextFactory.addFilter("replace", new ReplaceFilter());
215 templateContextFactory.addFilter("store", new StoreFilter());
216 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
217 templateContextFactory.addFilter("substring", new SubstringFilter());
218 templateContextFactory.addFilter("xml", new XmlFilter());
219 templateContextFactory.addFilter("change", new RequestChangeFilter());
220 templateContextFactory.addFilter("match", new MatchFilter());
221 templateContextFactory.addFilter("css", new CssClassNameFilter());
222 templateContextFactory.addFilter("js", new JavascriptFilter());
223 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory));
224 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
225 templateContextFactory.addFilter("format", new FormatFilter());
226 templateContextFactory.addFilter("sort", new CollectionSortFilter());
227 templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory));
228 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
229 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
230 templateContextFactory.addProvider(new ClassPathTemplateProvider());
231 templateContextFactory.addTemplateObject("formPassword", formPassword);
233 /* create notifications. */
234 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
235 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
237 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
238 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
240 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
241 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate, false);
243 Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html"));
244 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
246 Template sonesRescuedTemplate = TemplateParser.parse(createReader("/templates/notify/sonesRescuedNotification.html"));
247 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
249 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
250 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
252 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
253 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
255 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
256 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
258 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
259 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
261 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
262 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
270 * Returns the Sone core used by the Sone plugin.
272 * @return The Sone core
274 public Core getCore() {
275 return sonePlugin.core();
279 * Returns the template context factory of the web interface.
281 * @return The template context factory
283 public TemplateContextFactory getTemplateContextFactory() {
284 return templateContextFactory;
288 * Returns the current session, creating a new session if there is no
291 * @param toadletContenxt
292 * The toadlet context
293 * @return The current session, or {@code null} if there is no current
296 public Session getCurrentSession(ToadletContext toadletContenxt) {
297 return getCurrentSession(toadletContenxt, true);
301 * Returns the current session, creating a new session if there is no
302 * current session and {@code create} is {@code true}.
304 * @param toadletContenxt
305 * The toadlet context
307 * {@code true} to create a new session if there is no current
308 * session, {@code false} otherwise
309 * @return The current session, or {@code null} if there is no current
312 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
313 Session session = getSessionManager().useSession(toadletContenxt);
314 if (create && (session == null)) {
315 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
321 * Returns the currently logged in Sone.
323 * @param toadletContext
324 * The toadlet context
325 * @return The currently logged in Sone, or {@code null} if no Sone is
326 * currently logged in
328 public Sone getCurrentSone(ToadletContext toadletContext) {
329 return getCurrentSone(toadletContext, true);
333 * Returns the currently logged in Sone.
335 * @param toadletContext
336 * The toadlet context
338 * {@code true} to create a new session if no session exists,
339 * {@code false} to not create a new session
340 * @return The currently logged in Sone, or {@code null} if no Sone is
341 * currently logged in
343 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
344 Set<Sone> localSones = getCore().getLocalSones();
345 if (localSones.size() == 1) {
346 return localSones.iterator().next();
348 return getCurrentSone(getCurrentSession(toadletContext, create));
352 * Returns the currently logged in Sone.
356 * @return The currently logged in Sone, or {@code null} if no Sone is
357 * currently logged in
359 public Sone getCurrentSone(Session session) {
360 if (session == null) {
363 String soneId = (String) session.getAttribute("Sone.CurrentSone");
364 if (soneId == null) {
367 return getCore().getLocalSone(soneId, false);
371 * Sets the currently logged in Sone.
373 * @param toadletContext
374 * The toadlet context
376 * The Sone to set as currently logged in
378 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
379 Session session = getCurrentSession(toadletContext);
381 session.removeAttribute("Sone.CurrentSone");
383 session.setAttribute("Sone.CurrentSone", sone.getId());
388 * Returns the notification manager.
390 * @return The notification manager
392 public NotificationManager getNotifications() {
393 return notificationManager;
397 * Returns the l10n helper of the node.
399 * @return The node’s l10n helper
401 public BaseL10n getL10n() {
402 return sonePlugin.l10n().getBase();
406 * Returns the session manager of the node.
408 * @return The node’s session manager
410 public SessionManager getSessionManager() {
411 return sonePlugin.pluginRespirator().getSessionManager("Sone");
415 * Returns the node’s form password.
417 * @return The form password
419 public String getFormPassword() {
424 * Returns the posts that have been announced as new in the
425 * {@link #newPostNotification}.
427 * @return The new posts
429 public Set<Post> getNewPosts() {
430 return new HashSet<Post>(newPostNotification.getElements());
434 * Returns the replies that have been announced as new in the
435 * {@link #newReplyNotification}.
437 * @return The new replies
439 public Set<Reply> getNewReplies() {
440 return new HashSet<Reply>(newReplyNotification.getElements());
444 * Sets whether the current start of the plugin is the first start. It is
445 * considered a first start if the configuration file does not exist.
448 * {@code true} if no configuration file existed when Sone was
449 * loaded, {@code false} otherwise
451 public void setFirstStart(boolean firstStart) {
453 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
454 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
455 notificationManager.addNotification(firstStartNotification);
460 * Sets whether Sone was started with a fresh configuration file.
463 * {@code true} if Sone was started with a fresh configuration,
464 * {@code false} if the existing configuration could be read
466 public void setNewConfig(boolean newConfig) {
467 if (newConfig && !hasFirstStartNotification()) {
468 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
469 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
470 notificationManager.addNotification(configNotReadNotification);
479 * Returns whether the first start notification is currently displayed.
481 * @return {@code true} if the first-start notification is currently
482 * displayed, {@code false} otherwise
484 private boolean hasFirstStartNotification() {
485 return notificationManager.getNotification("first-start-notification") != null;
493 * Starts the web interface and registers all toadlets.
495 public void start() {
498 /* notification templates. */
499 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
501 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
502 notificationManager.addNotification(startupNotification);
504 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
508 startupNotification.dismiss();
510 }, "Sone Startup Notification Remover");
512 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
513 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
514 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
517 @SuppressWarnings("synthetic-access")
519 if (getCore().getIdentityManager().isConnected()) {
520 wotMissingNotification.dismiss();
522 notificationManager.addNotification(wotMissingNotification);
524 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
527 }, "Sone WoT Connector Checker");
531 * Stops the web interface and unregisters all toadlets.
534 unregisterToadlets();
535 Ticker.getInstance().stop();
543 * Register all toadlets.
545 private void registerToadlets() {
546 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
547 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
548 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
549 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
550 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
551 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
552 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
553 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
554 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
555 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
556 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
557 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
558 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
559 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
560 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
561 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
562 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
563 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
564 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
565 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
566 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
567 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
568 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
569 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
570 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
571 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
572 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
573 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
575 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
576 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage("", "index.html")));
577 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
578 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
579 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
580 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
581 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
582 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
583 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
584 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
585 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
586 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
587 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
588 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
589 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
590 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
591 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
592 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
593 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
594 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
595 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
596 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
597 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
598 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
599 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
600 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
601 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
602 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
603 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
604 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
605 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
607 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
608 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
609 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
610 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
611 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
612 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
616 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
617 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
650 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
651 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
652 for (PageToadlet toadlet : pageToadlets) {
653 String menuName = toadlet.getMenuName();
654 if (menuName != null) {
655 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
657 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
663 * Unregisters all toadlets.
665 private void unregisterToadlets() {
666 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
667 for (PageToadlet pageToadlet : pageToadlets) {
668 toadletContainer.unregister(pageToadlet);
670 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
674 * Creates a {@link Reader} from the {@link InputStream} for the resource
675 * with the given name.
677 * @param resourceName
678 * The name of the resource
679 * @return A {@link Reader} for the resource
681 private Reader createReader(String resourceName) {
683 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
684 } catch (UnsupportedEncodingException uee1) {
685 System.out.println(" fail.");
691 // CORELISTENER METHODS
698 public void rescuingSone(Sone sone) {
699 rescuingSonesNotification.add(sone);
700 notificationManager.addNotification(rescuingSonesNotification);
707 public void rescuedSone(Sone sone) {
708 rescuingSonesNotification.remove(sone);
709 sonesRescuedNotification.add(sone);
710 notificationManager.addNotification(sonesRescuedNotification);
717 public void newSoneFound(Sone sone) {
718 newSoneNotification.add(sone);
719 if (!hasFirstStartNotification()) {
720 notificationManager.addNotification(newSoneNotification);
728 public void newPostFound(Post post) {
729 newPostNotification.add(post);
730 if (!hasFirstStartNotification()) {
731 notificationManager.addNotification(newPostNotification);
733 getCore().markPostKnown(post);
741 public void newReplyFound(Reply reply) {
742 if (reply.getPost().getSone() == null) {
745 newReplyNotification.add(reply);
746 if (!hasFirstStartNotification()) {
747 notificationManager.addNotification(newReplyNotification);
749 getCore().markReplyKnown(reply);
757 public void markSoneKnown(Sone sone) {
758 newSoneNotification.remove(sone);
765 public void markPostKnown(Post post) {
766 newPostNotification.remove(post);
773 public void markReplyKnown(Reply reply) {
774 newReplyNotification.remove(reply);
781 public void postRemoved(Post post) {
782 newPostNotification.remove(post);
789 public void replyRemoved(Reply reply) {
790 newReplyNotification.remove(reply);
797 public void soneLocked(final Sone sone) {
798 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
801 @SuppressWarnings("synthetic-access")
803 lockedSonesNotification.add(sone);
804 lockedSonesTickerObjects.remove(sone);
805 notificationManager.addNotification(lockedSonesNotification);
807 }, "Sone Locked Notification");
808 lockedSonesTickerObjects.put(sone, tickerObject);
815 public void soneUnlocked(Sone sone) {
816 lockedSonesNotification.remove(sone);
817 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
824 public void updateFound(Version version, long releaseTime, long latestEdition) {
825 newVersionNotification.getTemplateContext().set("latestVersion", version);
826 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
827 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
828 notificationManager.addNotification(newVersionNotification);
835 public void imageInsertStarted(Image image) {
836 insertingImagesNotification.add(image);
837 notificationManager.addNotification(insertingImagesNotification);
844 public void imageInsertAborted(Image image) {
845 insertingImagesNotification.remove(image);
852 public void imageInsertFinished(Image image) {
853 insertingImagesNotification.remove(image);
854 insertedImagesNotification.add(image);
855 notificationManager.addNotification(insertedImagesNotification);
862 public void imageInsertFailed(Image image, Throwable cause) {
863 insertingImagesNotification.remove(image);
864 imageInsertFailedNotification.add(image);
865 notificationManager.addNotification(imageInsertFailedNotification);
869 * Template provider implementation that uses
870 * {@link WebInterface#createReader(String)} to load templates for
873 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
875 private class ClassPathTemplateProvider implements Provider {
877 /** Cache for templates. */
878 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
881 @SuppressWarnings("synthetic-access")
882 public CacheItem<Template> retrieve(String key) throws CacheException {
883 Template template = findTemplate(key);
884 if (template != null) {
885 return new DefaultCacheItem<Template>(template);
895 @SuppressWarnings("synthetic-access")
896 public Template getTemplate(TemplateContext templateContext, String templateName) {
898 return templateCache.get(templateName);
899 } catch (CacheException ce1) {
900 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
906 * Locates a template in the class path.
908 * @param templateName
909 * The name of the template to load
910 * @return The loaded template, or {@code null} if no template could be
913 @SuppressWarnings("synthetic-access")
914 private Template findTemplate(String templateName) {
915 Reader templateReader = createReader("/templates/" + templateName);
916 if (templateReader == null) {
919 Template template = null;
921 template = TemplateParser.parse(templateReader);
922 } catch (TemplateException te1) {
923 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);