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.GetPagePlugin;
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.RequestChangeFilter;
61 import net.pterodactylus.sone.template.SoneAccessor;
62 import net.pterodactylus.sone.template.SubstringFilter;
63 import net.pterodactylus.sone.template.TrustAccessor;
64 import net.pterodactylus.sone.template.UnknownDateFilter;
65 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
66 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
67 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
68 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
69 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
70 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
71 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
72 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
73 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
74 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
75 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
76 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
77 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
78 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
79 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
80 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
81 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
82 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
83 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
84 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
85 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
86 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
87 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
88 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
89 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
90 import net.pterodactylus.sone.web.page.PageToadlet;
91 import net.pterodactylus.sone.web.page.PageToadletFactory;
92 import net.pterodactylus.sone.web.page.StaticPage;
93 import net.pterodactylus.util.cache.Cache;
94 import net.pterodactylus.util.cache.CacheException;
95 import net.pterodactylus.util.cache.CacheItem;
96 import net.pterodactylus.util.cache.DefaultCacheItem;
97 import net.pterodactylus.util.cache.MemoryCache;
98 import net.pterodactylus.util.cache.ValueRetriever;
99 import net.pterodactylus.util.logging.Logging;
100 import net.pterodactylus.util.notify.Notification;
101 import net.pterodactylus.util.notify.NotificationManager;
102 import net.pterodactylus.util.notify.TemplateNotification;
103 import net.pterodactylus.util.template.CollectionSortFilter;
104 import net.pterodactylus.util.template.DateFilter;
105 import net.pterodactylus.util.template.FormatFilter;
106 import net.pterodactylus.util.template.HtmlFilter;
107 import net.pterodactylus.util.template.MatchFilter;
108 import net.pterodactylus.util.template.PaginationPlugin;
109 import net.pterodactylus.util.template.Provider;
110 import net.pterodactylus.util.template.ReflectionAccessor;
111 import net.pterodactylus.util.template.ReplaceFilter;
112 import net.pterodactylus.util.template.StoreFilter;
113 import net.pterodactylus.util.template.Template;
114 import net.pterodactylus.util.template.TemplateContext;
115 import net.pterodactylus.util.template.TemplateContextFactory;
116 import net.pterodactylus.util.template.TemplateException;
117 import net.pterodactylus.util.template.TemplateParser;
118 import net.pterodactylus.util.template.XmlFilter;
119 import net.pterodactylus.util.thread.Ticker;
120 import net.pterodactylus.util.version.Version;
121 import freenet.clients.http.SessionManager;
122 import freenet.clients.http.SessionManager.Session;
123 import freenet.clients.http.ToadletContainer;
124 import freenet.clients.http.ToadletContext;
125 import freenet.l10n.BaseL10n;
128 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
129 * references to l10n helpers.
131 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
133 public class WebInterface implements CoreListener {
136 private static final Logger logger = Logging.getLogger(WebInterface.class);
138 /** The notification manager. */
139 private final NotificationManager notificationManager = new NotificationManager();
141 /** The Sone plugin. */
142 private final SonePlugin sonePlugin;
144 /** The registered toadlets. */
145 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
147 /** The form password. */
148 private final String formPassword;
150 /** The template context factory. */
151 private final TemplateContextFactory templateContextFactory;
153 /** The “new Sone” notification. */
154 private final ListNotification<Sone> newSoneNotification;
156 /** The “new post” notification. */
157 private final ListNotification<Post> newPostNotification;
159 /** The “new reply” notification. */
160 private final ListNotification<Reply> newReplyNotification;
162 /** The “rescuing Sone” notification. */
163 private final ListNotification<Sone> rescuingSonesNotification;
165 /** The “Sone rescued” notification. */
166 private final ListNotification<Sone> sonesRescuedNotification;
168 /** Sone locked notification ticker objects. */
169 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
171 /** The “Sone locked” notification. */
172 private final ListNotification<Sone> lockedSonesNotification;
174 /** The “new version” notification. */
175 private final TemplateNotification newVersionNotification;
177 /** The “inserting images” notification. */
178 private final ListNotification<Image> insertingImagesNotification;
180 /** The “inserted images” notification. */
181 private final ListNotification<Image> insertedImagesNotification;
183 /** The “image insert failed” notification. */
184 private final ListNotification<Image> imageInsertFailedNotification;
187 * Creates a new web interface.
192 @SuppressWarnings("synthetic-access")
193 public WebInterface(SonePlugin sonePlugin) {
194 this.sonePlugin = sonePlugin;
195 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
197 templateContextFactory = new TemplateContextFactory();
198 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
199 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
200 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
201 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
202 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
203 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
204 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
205 templateContextFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
206 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
207 templateContextFactory.addFilter("date", new DateFilter());
208 templateContextFactory.addFilter("html", new HtmlFilter());
209 templateContextFactory.addFilter("replace", new ReplaceFilter());
210 templateContextFactory.addFilter("store", new StoreFilter());
211 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
212 templateContextFactory.addFilter("substring", new SubstringFilter());
213 templateContextFactory.addFilter("xml", new XmlFilter());
214 templateContextFactory.addFilter("change", new RequestChangeFilter());
215 templateContextFactory.addFilter("match", new MatchFilter());
216 templateContextFactory.addFilter("css", new CssClassNameFilter());
217 templateContextFactory.addFilter("js", new JavascriptFilter());
218 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory));
219 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
220 templateContextFactory.addFilter("format", new FormatFilter());
221 templateContextFactory.addFilter("sort", new CollectionSortFilter());
222 templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory));
223 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
224 templateContextFactory.addProvider(new ClassPathTemplateProvider());
225 templateContextFactory.addTemplateObject("formPassword", formPassword);
227 /* create notifications. */
228 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
229 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
231 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
232 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
234 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
235 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
237 Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html"));
238 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
240 Template sonesRescuedTemplate = TemplateParser.parse(createReader("/templates/notify/sonesRescuedNotification.html"));
241 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
243 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
244 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
246 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
247 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
249 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
250 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
252 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
253 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
255 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
256 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
264 * Returns the Sone core used by the Sone plugin.
266 * @return The Sone core
268 public Core getCore() {
269 return sonePlugin.core();
273 * Returns the template context factory of the web interface.
275 * @return The template context factory
277 public TemplateContextFactory getTemplateContextFactory() {
278 return templateContextFactory;
282 * Returns the current session, creating a new session if there is no
285 * @param toadletContenxt
286 * The toadlet context
287 * @return The current session, or {@code null} if there is no current
290 public Session getCurrentSession(ToadletContext toadletContenxt) {
291 return getCurrentSession(toadletContenxt, true);
295 * Returns the current session, creating a new session if there is no
296 * current session and {@code create} is {@code true}.
298 * @param toadletContenxt
299 * The toadlet context
301 * {@code true} to create a new session if there is no current
302 * session, {@code false} otherwise
303 * @return The current session, or {@code null} if there is no current
306 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
307 Session session = getSessionManager().useSession(toadletContenxt);
308 if (create && (session == null)) {
309 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
315 * Returns the currently logged in Sone.
317 * @param toadletContext
318 * The toadlet context
319 * @return The currently logged in Sone, or {@code null} if no Sone is
320 * currently logged in
322 public Sone getCurrentSone(ToadletContext toadletContext) {
323 return getCurrentSone(toadletContext, true);
327 * Returns the currently logged in Sone.
329 * @param toadletContext
330 * The toadlet context
332 * {@code true} to create a new session if no session exists,
333 * {@code false} to not create a new session
334 * @return The currently logged in Sone, or {@code null} if no Sone is
335 * currently logged in
337 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
338 Set<Sone> localSones = getCore().getLocalSones();
339 if (localSones.size() == 1) {
340 return localSones.iterator().next();
342 return getCurrentSone(getCurrentSession(toadletContext, create));
346 * Returns the currently logged in Sone.
350 * @return The currently logged in Sone, or {@code null} if no Sone is
351 * currently logged in
353 public Sone getCurrentSone(Session session) {
354 if (session == null) {
357 String soneId = (String) session.getAttribute("Sone.CurrentSone");
358 if (soneId == null) {
361 return getCore().getLocalSone(soneId, false);
365 * Sets the currently logged in Sone.
367 * @param toadletContext
368 * The toadlet context
370 * The Sone to set as currently logged in
372 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
373 Session session = getCurrentSession(toadletContext);
375 session.removeAttribute("Sone.CurrentSone");
377 session.setAttribute("Sone.CurrentSone", sone.getId());
382 * Returns the notification manager.
384 * @return The notification manager
386 public NotificationManager getNotifications() {
387 return notificationManager;
391 * Returns the l10n helper of the node.
393 * @return The node’s l10n helper
395 public BaseL10n getL10n() {
396 return sonePlugin.l10n().getBase();
400 * Returns the session manager of the node.
402 * @return The node’s session manager
404 public SessionManager getSessionManager() {
405 return sonePlugin.pluginRespirator().getSessionManager("Sone");
409 * Returns the node’s form password.
411 * @return The form password
413 public String getFormPassword() {
418 * Returns the posts that have been announced as new in the
419 * {@link #newPostNotification}.
421 * @return The new posts
423 public Set<Post> getNewPosts() {
424 return new HashSet<Post>(newPostNotification.getElements());
428 * Returns the replies that have been announced as new in the
429 * {@link #newReplyNotification}.
431 * @return The new replies
433 public Set<Reply> getNewReplies() {
434 return new HashSet<Reply>(newReplyNotification.getElements());
438 * Sets whether the current start of the plugin is the first start. It is
439 * considered a first start if the configuration file does not exist.
442 * {@code true} if no configuration file existed when Sone was
443 * loaded, {@code false} otherwise
445 public void setFirstStart(boolean firstStart) {
447 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
448 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
449 notificationManager.addNotification(firstStartNotification);
454 * Sets whether Sone was started with a fresh configuration file.
457 * {@code true} if Sone was started with a fresh configuration,
458 * {@code false} if the existing configuration could be read
460 public void setNewConfig(boolean newConfig) {
461 if (newConfig && !hasFirstStartNotification()) {
462 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
463 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
464 notificationManager.addNotification(configNotReadNotification);
473 * Returns whether the first start notification is currently displayed.
475 * @return {@code true} if the first-start notification is currently
476 * displayed, {@code false} otherwise
478 private boolean hasFirstStartNotification() {
479 return notificationManager.getNotification("first-start-notification") != null;
487 * Starts the web interface and registers all toadlets.
489 public void start() {
492 /* notification templates. */
493 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
495 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
496 notificationManager.addNotification(startupNotification);
498 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
502 startupNotification.dismiss();
504 }, "Sone Startup Notification Remover");
506 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
507 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
508 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
511 @SuppressWarnings("synthetic-access")
513 if (getCore().getIdentityManager().isConnected()) {
514 wotMissingNotification.dismiss();
516 notificationManager.addNotification(wotMissingNotification);
518 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
521 }, "Sone WoT Connector Checker");
525 * Stops the web interface and unregisters all toadlets.
528 unregisterToadlets();
529 Ticker.getInstance().stop();
537 * Register all toadlets.
539 private void registerToadlets() {
540 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
541 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
542 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
543 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
544 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
545 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
546 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
547 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
548 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
549 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
550 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
551 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
552 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
553 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
554 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
555 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
556 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
557 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
558 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
559 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
560 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
561 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
562 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
563 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
564 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
566 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
567 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
568 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
569 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
570 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
571 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
572 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
573 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
574 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
575 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
576 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
577 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
578 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
579 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
580 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
581 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
582 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
583 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
584 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
585 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
586 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
587 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
588 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
589 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
590 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
591 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(emptyTemplate, this)));
592 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
593 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
594 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
595 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
596 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
597 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
598 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
599 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
600 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
601 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
602 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
603 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
604 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
605 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
607 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
608 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
609 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
610 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
611 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
612 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
616 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
617 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
638 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
639 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
640 for (PageToadlet toadlet : pageToadlets) {
641 String menuName = toadlet.getMenuName();
642 if (menuName != null) {
643 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
645 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
651 * Unregisters all toadlets.
653 private void unregisterToadlets() {
654 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
655 for (PageToadlet pageToadlet : pageToadlets) {
656 toadletContainer.unregister(pageToadlet);
658 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
662 * Creates a {@link Reader} from the {@link InputStream} for the resource
663 * with the given name.
665 * @param resourceName
666 * The name of the resource
667 * @return A {@link Reader} for the resource
669 private Reader createReader(String resourceName) {
671 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
672 } catch (UnsupportedEncodingException uee1) {
673 System.out.println(" fail.");
679 // CORELISTENER METHODS
686 public void rescuingSone(Sone sone) {
687 rescuingSonesNotification.add(sone);
688 notificationManager.addNotification(rescuingSonesNotification);
695 public void rescuedSone(Sone sone) {
696 rescuingSonesNotification.remove(sone);
697 sonesRescuedNotification.add(sone);
698 notificationManager.addNotification(sonesRescuedNotification);
705 public void newSoneFound(Sone sone) {
706 newSoneNotification.add(sone);
707 if (!hasFirstStartNotification()) {
708 notificationManager.addNotification(newSoneNotification);
716 public void newPostFound(Post post) {
717 newPostNotification.add(post);
718 if (!hasFirstStartNotification()) {
719 notificationManager.addNotification(newPostNotification);
721 getCore().markPostKnown(post);
729 public void newReplyFound(Reply reply) {
730 if (reply.getPost().getSone() == null) {
733 newReplyNotification.add(reply);
734 if (!hasFirstStartNotification()) {
735 notificationManager.addNotification(newReplyNotification);
737 getCore().markReplyKnown(reply);
745 public void markSoneKnown(Sone sone) {
746 newSoneNotification.remove(sone);
753 public void markPostKnown(Post post) {
754 newPostNotification.remove(post);
761 public void markReplyKnown(Reply reply) {
762 newReplyNotification.remove(reply);
769 public void postRemoved(Post post) {
770 newPostNotification.remove(post);
777 public void replyRemoved(Reply reply) {
778 newReplyNotification.remove(reply);
785 public void soneLocked(final Sone sone) {
786 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
789 @SuppressWarnings("synthetic-access")
791 lockedSonesNotification.add(sone);
792 lockedSonesTickerObjects.remove(sone);
793 notificationManager.addNotification(lockedSonesNotification);
795 }, "Sone Locked Notification");
796 lockedSonesTickerObjects.put(sone, tickerObject);
803 public void soneUnlocked(Sone sone) {
804 lockedSonesNotification.remove(sone);
805 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
812 public void updateFound(Version version, long releaseTime, long latestEdition) {
813 newVersionNotification.getTemplateContext().set("latestVersion", version);
814 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
815 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
816 notificationManager.addNotification(newVersionNotification);
823 public void imageInsertStarted(Image image) {
824 insertingImagesNotification.add(image);
825 notificationManager.addNotification(insertingImagesNotification);
832 public void imageInsertAborted(Image image) {
833 insertingImagesNotification.remove(image);
840 public void imageInsertFinished(Image image) {
841 insertingImagesNotification.remove(image);
842 insertedImagesNotification.add(image);
843 notificationManager.addNotification(insertedImagesNotification);
850 public void imageInsertFailed(Image image, Throwable cause) {
851 insertingImagesNotification.remove(image);
852 imageInsertFailedNotification.add(image);
853 notificationManager.addNotification(imageInsertFailedNotification);
857 * Template provider implementation that uses
858 * {@link WebInterface#createReader(String)} to load templates for
861 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
863 private class ClassPathTemplateProvider implements Provider {
865 /** Cache for templates. */
866 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
869 @SuppressWarnings("synthetic-access")
870 public CacheItem<Template> retrieve(String key) throws CacheException {
871 Template template = findTemplate(key);
872 if (template != null) {
873 return new DefaultCacheItem<Template>(template);
883 @SuppressWarnings("synthetic-access")
884 public Template getTemplate(TemplateContext templateContext, String templateName) {
886 return templateCache.get(templateName);
887 } catch (CacheException ce1) {
888 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
894 * Locates a template in the class path.
896 * @param templateName
897 * The name of the template to load
898 * @return The loaded template, or {@code null} if no template could be
901 @SuppressWarnings("synthetic-access")
902 private Template findTemplate(String templateName) {
903 Reader templateReader = createReader("/templates/" + templateName);
904 if (templateReader == null) {
907 Template template = null;
909 template = TemplateParser.parse(templateReader);
910 } catch (TemplateException te1) {
911 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);