package net.pterodactylus.sone.web;
+import static java.util.logging.Logger.getLogger;
import static net.pterodactylus.util.template.TemplateParser.parse;
import java.io.IOException;
import net.pterodactylus.sone.core.event.UpdateFoundEvent;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Image;
+import net.pterodactylus.sone.data.LocalSone;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.web.page.PageToadlet;
import net.pterodactylus.sone.web.page.PageToadletFactory;
import net.pterodactylus.util.io.Closer;
-import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.notify.Notification;
import net.pterodactylus.util.notify.NotificationManager;
import net.pterodactylus.util.notify.TemplateNotification;
import net.pterodactylus.util.web.StaticPage;
import net.pterodactylus.util.web.TemplatePage;
+import com.google.common.base.Optional;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
import com.google.common.eventbus.Subscribe;
public class WebInterface {
/** The logger. */
- private static final Logger logger = Logging.getLogger(WebInterface.class);
+ private static final Logger logger = getLogger("Sone.Web.Main");
/** The notification manager. */
private final NotificationManager notificationManager = new NotificationManager();
return templateContextFactory;
}
- /**
- * Returns the current session, creating a new session if there is no
- * current session.
- *
- * @param toadletContenxt
- * The toadlet context
- * @return The current session, or {@code null} if there is no current
- * session
- */
public Session getCurrentSession(ToadletContext toadletContenxt) {
- return getCurrentSession(toadletContenxt, true);
+ return getCurrentSession(toadletContenxt, true).get();
}
- /**
- * Returns the current session, creating a new session if there is no
- * current session and {@code create} is {@code true}.
- *
- * @param toadletContenxt
- * The toadlet context
- * @param create
- * {@code true} to create a new session if there is no current
- * session, {@code false} otherwise
- * @return The current session, or {@code null} if there is no current
- * session
- */
- public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
+ public Optional<Session> getCurrentSession(ToadletContext toadletContenxt, boolean createSession) {
Session session = getSessionManager().useSession(toadletContenxt);
- if (create && (session == null)) {
+ if (createSession && (session == null)) {
session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
}
- return session;
+ return Optional.fromNullable(session);
}
/**
* @return The currently logged in Sone, or {@code null} if no Sone is
* currently logged in
*/
- public Sone getCurrentSone(ToadletContext toadletContext) {
+ public Optional<LocalSone> getCurrentSone(ToadletContext toadletContext) {
return getCurrentSone(toadletContext, true);
}
*
* @param toadletContext
* The toadlet context
- * @param create
+ * @param createSession
* {@code true} to create a new session if no session exists,
* {@code false} to not create a new session
* @return The currently logged in Sone, or {@code null} if no Sone is
* currently logged in
*/
- public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
- Collection<Sone> localSones = getCore().getLocalSones();
+ public Optional<LocalSone> getCurrentSone(ToadletContext toadletContext, boolean createSession) {
+ Collection<LocalSone> localSones = getCore().getLocalSones();
if (localSones.size() == 1) {
- return localSones.iterator().next();
+ return Optional.of(localSones.iterator().next());
}
- return getCurrentSone(getCurrentSession(toadletContext, create));
+ return getCurrentSone(getCurrentSession(toadletContext, createSession));
}
/**
* @return The currently logged in Sone, or {@code null} if no Sone is
* currently logged in
*/
- public Sone getCurrentSone(Session session) {
- if (session == null) {
- return null;
+ public Optional<LocalSone> getCurrentSone(Optional<Session> session) {
+ if (!session.isPresent()) {
+ return Optional.absent();
}
- String soneId = (String) session.getAttribute("Sone.CurrentSone");
+ String soneId = (String) session.get().getAttribute("Sone.CurrentSone");
if (soneId == null) {
- return null;
+ return Optional.absent();
}
- return getCore().getLocalSone(soneId, false);
+ return getCore().getLocalSone(soneId);
}
/**
Template deleteAlbumTemplate = parseTemplate("/templates/deleteAlbum.html");
Template deleteImageTemplate = parseTemplate("/templates/deleteImage.html");
Template noPermissionTemplate = parseTemplate("/templates/noPermission.html");
+ Template emptyImageTitleTemplate = parseTemplate("/templates/emptyImageTitle.html");
+ Template emptyAlbumTitleTemplate = parseTemplate("/templates/emptyAlbumTitle.html");
Template optionsTemplate = parseTemplate("/templates/options.html");
Template rescueTemplate = parseTemplate("/templates/rescue.html");
Template aboutTemplate = parseTemplate("/templates/about.html");
pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
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 SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.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<FreenetRequest>("css/", "/static/css/", "text/css")));
public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
soneInsertNotification.set("soneStatus", "inserting");
- if (soneInsertingEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
+ if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
notificationManager.addNotification(soneInsertNotification);
}
}
TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
soneInsertNotification.set("soneStatus", "inserted");
soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
- if (soneInsertedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
+ if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
notificationManager.addNotification(soneInsertNotification);
}
}
TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
soneInsertNotification.set("soneStatus", "insert-aborted");
soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
- if (soneInsertAbortedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
+ if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
notificationManager.addNotification(soneInsertNotification);
}
}