Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / web / LoginPage.java
index c859cf6..4b38a07 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * FreenetSone - LoginPage.java - Copyright © 2010 David Roden
+ * Sone - LoginPage.java - Copyright © 2010–2015 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.web;
 
+import static java.util.logging.Logger.getLogger;
+
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
+import java.util.logging.Logger;
 
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.template.SoneAccessor;
-import net.pterodactylus.sone.web.page.Page.Request.Method;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.web.Method;
 import freenet.clients.http.ToadletContext;
 
 /**
@@ -35,6 +39,10 @@ import freenet.clients.http.ToadletContext;
  */
 public class LoginPage extends SoneTemplatePage {
 
+       /** The logger. */
+       @SuppressWarnings("unused")
+       private static final Logger logger = getLogger(LoginPage.class.getName());
+
        /**
         * Creates a new login page.
         *
@@ -44,7 +52,7 @@ public class LoginPage extends SoneTemplatePage {
         *            The Sone web interface
         */
        public LoginPage(Template template, WebInterface webInterface) {
-               super("login.html", template, "Page.Login.Title", webInterface);
+               super("login.html", template, "Page.Login.Title", webInterface, false);
        }
 
        //
@@ -55,44 +63,52 @@ public class LoginPage extends SoneTemplatePage {
         * {@inheritDoc}
         */
        @Override
-       protected void processTemplate(Request request, Template template) throws RedirectException {
-               super.processTemplate(request, template);
-               List<Sone> localSones = new ArrayList<Sone>(webInterface.core().getSones());
-               Collections.sort(localSones, new Comparator<Sone>() {
-
-                       @Override
-                       public int compare(Sone leftSone, Sone rightSone) {
-                               int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
-                               if (diff != 0) {
-                                       return diff;
-                               }
-                               return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
-                       }
-
-               });
-               template.set("sones", localSones);
+       protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
+               super.processTemplate(request, templateContext);
+               /* get all own identities. */
+               List<Sone> localSones = new ArrayList<Sone>(webInterface.getCore().getLocalSones());
+               Collections.sort(localSones, Sone.NICE_NAME_COMPARATOR);
+               templateContext.set("sones", localSones);
                if (request.getMethod() == Method.POST) {
                        String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone-id", 100);
-                       Sone selectedSone = null;
-                       for (Sone sone : webInterface.core().getSones()) {
-                               if (sone.getId().equals(soneId)) {
-                                       selectedSone = sone;
-                                       break;
-                               }
-                       }
+                       Sone selectedSone = webInterface.getCore().getLocalSone(soneId);
                        if (selectedSone != null) {
                                setCurrentSone(request.getToadletContext(), selectedSone);
-                               throw new RedirectException("index.html");
+                               String target = request.getHttpRequest().getParam("target");
+                               if ((target == null) || (target.length() == 0)) {
+                                       target = "index.html";
+                               }
+                               throw new RedirectException(target);
                        }
                }
+               List<OwnIdentity> ownIdentitiesWithoutSone = CreateSonePage.getOwnIdentitiesWithoutSone(webInterface.getCore());
+               templateContext.set("identitiesWithoutSone", ownIdentitiesWithoutSone);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected String getRedirectTarget(FreenetRequest request) {
+               if (getCurrentSone(request.getToadletContext(), false) != null) {
+                       return "index.html";
+               }
+               return null;
        }
 
+       //
+       // SONETEMPLATEPAGE METHODS
+       //
+
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isEnabled(ToadletContext toadletContext) {
-               return getCurrentSone(toadletContext) == null;
+               if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
+                       return false;
+               }
+               return getCurrentSone(toadletContext, false) == null;
        }
 
 }