+ public SessionManager getSessionManager() {
+ return sonePlugin.pluginRespirator().getSessionManager("Sone");
+ }
+
+ /**
+ * Returns the node’s form password.
+ *
+ * @return The form password
+ */
+ public String getFormPassword() {
+ return formPassword;
+ }
+
+ /**
+ * Returns the posts that have been announced as new in the
+ * {@link #newPostNotification}.
+ *
+ * @return The new posts
+ */
+ public Set<Post> getNewPosts() {
+ return new HashSet<Post>(newPostNotification.getElements());
+ }
+
+ /**
+ * Returns the replies that have been announced as new in the
+ * {@link #newReplyNotification}.
+ *
+ * @return The new replies
+ */
+ public Set<Reply> getNewReplies() {
+ return new HashSet<Reply>(newReplyNotification.getElements());
+ }
+
+ /**
+ * Sets whether the current start of the plugin is the first start. It is
+ * considered a first start if the configuration file does not exist.
+ *
+ * @param firstStart
+ * {@code true} if no configuration file existed when Sone was
+ * loaded, {@code false} otherwise
+ */
+ public void setFirstStart(boolean firstStart) {
+ if (firstStart) {
+ Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
+ Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
+ notificationManager.addNotification(firstStartNotification);
+ }
+ }
+
+ /**
+ * Sets whether Sone was started with a fresh configuration file.
+ *
+ * @param newConfig
+ * {@code true} if Sone was started with a fresh configuration,
+ * {@code false} if the existing configuration could be read
+ */
+ public void setNewConfig(boolean newConfig) {
+ if (newConfig && !hasFirstStartNotification()) {
+ Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
+ Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
+ notificationManager.addNotification(configNotReadNotification);
+ }
+ }
+
+ //
+ // PRIVATE ACCESSORS
+ //
+
+ /**
+ * Returns whether the first start notification is currently displayed.
+ *
+ * @return {@code true} if the first-start notification is currently
+ * displayed, {@code false} otherwise
+ */
+ private boolean hasFirstStartNotification() {
+ return notificationManager.getNotification("first-start-notification") != null;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Starts the web interface and registers all toadlets.
+ */
+ public void start() {
+ registerToadlets();
+
+ /* notification templates. */
+ Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
+
+ final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
+ notificationManager.addNotification(startupNotification);
+
+ Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
+
+ @Override
+ public void run() {
+ startupNotification.dismiss();
+ }
+ }, "Sone Startup Notification Remover");
+
+ Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
+ final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
+ Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ if (getCore().getIdentityManager().isConnected()) {
+ wotMissingNotification.dismiss();
+ } else {
+ notificationManager.addNotification(wotMissingNotification);
+ }
+ Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
+ }
+
+ }, "Sone WoT Connector Checker");
+ }
+
+ /**
+ * Stops the web interface and unregisters all toadlets.
+ */
+ public void stop() {
+ unregisterToadlets();
+ Ticker.getInstance().stop();
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Register all toadlets.
+ */
+ private void registerToadlets() {
+ Template emptyTemplate = TemplateParser.parse(new StringReader(""));
+ Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
+ Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
+ Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
+ Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
+ Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
+ Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
+ Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
+ Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
+ Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
+ Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
+ Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
+ Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
+ Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
+ Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
+ Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
+ Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
+ Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
+ Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
+ Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
+ Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
+ Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
+ Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
+ Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
+
+ PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage("", "index.html")));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
+
+ ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
+ toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
+ for (PageToadlet toadlet : pageToadlets) {
+ String menuName = toadlet.getMenuName();
+ if (menuName != null) {
+ toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
+ } else {
+ toadletContainer.register(toadlet, null, toadlet.path(), true, false);
+ }
+ }
+ }
+
+ /**
+ * Unregisters all toadlets.
+ */
+ private void unregisterToadlets() {
+ ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
+ for (PageToadlet pageToadlet : pageToadlets) {
+ toadletContainer.unregister(pageToadlet);
+ }
+ toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
+ }
+
+ /**
+ * Creates a {@link Reader} from the {@link InputStream} for the resource
+ * with the given name.
+ *
+ * @param resourceName
+ * The name of the resource
+ * @return A {@link Reader} for the resource
+ */
+ private Reader createReader(String resourceName) {
+ try {
+ return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
+ } catch (UnsupportedEncodingException uee1) {
+ System.out.println(" fail.");
+ return null;
+ }
+ }
+
+ //
+ // CORELISTENER METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void rescuingSone(Sone sone) {
+ rescuingSonesNotification.add(sone);
+ notificationManager.addNotification(rescuingSonesNotification);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void rescuedSone(Sone sone) {
+ rescuingSonesNotification.remove(sone);
+ sonesRescuedNotification.add(sone);
+ notificationManager.addNotification(sonesRescuedNotification);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void newSoneFound(Sone sone) {
+ newSoneNotification.add(sone);
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(newSoneNotification);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void newPostFound(Post post) {
+ newPostNotification.add(post);
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(newPostNotification);
+ } else {
+ getCore().markPostKnown(post);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void newReplyFound(Reply reply) {
+ if (reply.getPost().getSone() == null) {
+ return;
+ }
+ newReplyNotification.add(reply);
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(newReplyNotification);
+ } else {
+ getCore().markReplyKnown(reply);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void markSoneKnown(Sone sone) {
+ newSoneNotification.remove(sone);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void markPostKnown(Post post) {
+ newPostNotification.remove(post);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void markReplyKnown(Reply reply) {
+ newReplyNotification.remove(reply);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void soneRemoved(Sone sone) {
+ newSoneNotification.remove(sone);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void postRemoved(Post post) {
+ newPostNotification.remove(post);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void replyRemoved(Reply reply) {
+ newReplyNotification.remove(reply);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void soneLocked(final Sone sone) {
+ Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ lockedSonesNotification.add(sone);
+ lockedSonesTickerObjects.remove(sone);
+ notificationManager.addNotification(lockedSonesNotification);
+ }
+ }, "Sone Locked Notification");
+ lockedSonesTickerObjects.put(sone, tickerObject);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void soneUnlocked(Sone sone) {
+ lockedSonesNotification.remove(sone);
+ Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void updateFound(Version version, long releaseTime, long latestEdition) {
+ newVersionNotification.getTemplateContext().set("latestVersion", version);
+ newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
+ newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
+ notificationManager.addNotification(newVersionNotification);
+ }
+
+ /**
+ * Template provider implementation that uses
+ * {@link WebInterface#createReader(String)} to load templates for
+ * inclusion.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ private class ClassPathTemplateProvider implements Provider {
+
+ /** Cache for templates. */
+ private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public CacheItem<Template> retrieve(String key) throws CacheException {
+ Template template = findTemplate(key);
+ if (template != null) {
+ return new DefaultCacheItem<Template>(template);
+ }
+ return null;
+ }
+ });
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public Template getTemplate(TemplateContext templateContext, String templateName) {
+ try {
+ return templateCache.get(templateName);
+ } catch (CacheException ce1) {
+ logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
+ return null;
+ }
+ }
+
+ /**
+ * Locates a template in the class path.
+ *
+ * @param templateName
+ * The name of the template to load
+ * @return The loaded template, or {@code null} if no template could be
+ * found
+ */
+ @SuppressWarnings("synthetic-access")
+ private Template findTemplate(String templateName) {
+ Reader templateReader = createReader("/templates/" + templateName);
+ if (templateReader == null) {
+ return null;
+ }
+ Template template = null;
+ try {
+ template = TemplateParser.parse(templateReader);
+ } catch (TemplateException te1) {
+ logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
+ }
+ return template;
+ }
+