From 63f0355680e86a6a692a3d16e7e82ae39e60d3e8 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 28 Jul 2018 13:47:08 +0000
Subject: [PATCH] Console: Prep for removing themes (ticket #2272)

---
 .../src/org/klomp/snark/SnarkManager.java     | 34 +++++++++---
 .../src/net/i2p/i2ptunnel/web/IndexBean.java  | 15 +++++-
 .../src/net/i2p/router/web/CSSHelper.java     | 14 ++++-
 .../router/web/helpers/ConfigUIHelper.java    | 21 +++++++-
 .../src/java/src/i2p/susi/dns/BaseBean.java   | 43 ++++++++++-----
 .../src/src/i2p/susi/webmail/WebMail.java     | 53 +++++++++++--------
 6 files changed, 134 insertions(+), 46 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java
index 2731753cc8..f6e65dc10e 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java
@@ -132,6 +132,9 @@ public class SnarkManager implements CompleteListener, ClientApp {
     public static final String RC_PROP_UNIVERSAL_THEMING = "routerconsole.universal.theme";
     public static final String PROP_THEME = "i2psnark.theme";
     public static final String DEFAULT_THEME = "ubergine";
+    /** From CSSHelper */
+    private static final String PROP_DISABLE_OLD = "routerconsole.disableOldThemes";
+    private static final boolean DEFAULT_DISABLE_OLD = false;
     /** @since 0.9.32 */
     public static final String PROP_COLLAPSE_PANELS = "i2psnark.collapsePanels";
     private static final String PROP_USE_OPENTRACKERS = "i2psnark.useOpentrackers";
@@ -832,7 +835,7 @@ public class SnarkManager implements CompleteListener, ClientApp {
      * @return String -- the current theme
      */
     public String getTheme() {
-        String theme = _config.getProperty(PROP_THEME);
+        String theme;
         if (getUniversalTheming()) {
             // Fetch routerconsole theme (or use our default if it doesn't exist)
             theme = _context.getProperty(RC_PROP_THEME, DEFAULT_THEME);
@@ -840,8 +843,10 @@ public class SnarkManager implements CompleteListener, ClientApp {
             String[] themes = getThemes();
             boolean themeExists = false;
             for (int i = 0; i < themes.length; i++) {
-                if (themes[i].equals(theme))
+                if (themes[i].equals(theme)) {
                     themeExists = true;
+                    break;
+                }
             }
             if (!themeExists) {
                 // Since the default is not "light", explicitly check if universal theme is "classic"
@@ -851,6 +856,16 @@ public class SnarkManager implements CompleteListener, ClientApp {
                     theme = DEFAULT_THEME;
                 _config.setProperty(PROP_THEME, DEFAULT_THEME);
             }
+        } else {
+            theme = _config.getProperty(PROP_THEME, DEFAULT_THEME);
+        }
+        // remap deprecated themes
+        if (theme.equals("midnight")) {
+            if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                theme = "dark";
+        } else if (theme.equals("classic")) {
+            if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                theme = "light";
         }
         return theme;
     }
@@ -862,21 +877,24 @@ public class SnarkManager implements CompleteListener, ClientApp {
     public String[] getThemes() {
          String[] themes;
          if (_context.isRouterContext()) {
-            // "docs/themes/snark/"
             File dir = new File(_context.getBaseDir(), "docs/themes/snark");
             FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } };
-            // Walk the themes dir, collecting the theme names, and append them to the map
             File[] dirnames = dir.listFiles(fileFilter);
             if (dirnames != null) {
-                themes = new String[dirnames.length];
-                for(int i = 0; i < dirnames.length; i++) {
-                    themes[i] = dirnames[i].getName();
+                List<String> th = new ArrayList<String>(dirnames.length);
+                boolean skipOld = _context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD);
+                for (int i = 0; i < dirnames.length; i++) {
+                    String name = dirnames[i].getName();
+                    if (skipOld && (name.equals("midnight") || name.equals("classic")))
+                        continue;
+                    th.add(name);
                 }
+                themes = th.toArray(new String[th.size()]);
             } else {
                 themes = new String[0];
             }
         } else {
-            themes = new String[] { "classic", "dark", "light", "midnight", "ubergine", "vanilla" };
+            themes = new String[] { "dark", "light", "ubergine", "vanilla" };
         }
         return themes;
     }
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java
index 47ae4eb5a6..fea4fd94df 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java
@@ -77,8 +77,11 @@ public class IndexBean {
     private static final List<String> _nonces = new ArrayList<String>(MAX_NONCES + 1);
     private static final UIMessages _messages = new UIMessages(100);
 
-    public static final String PROP_THEME_NAME = "routerconsole.theme";
-    public static final String DEFAULT_THEME = "light";
+    private static final String PROP_THEME_NAME = "routerconsole.theme";
+    private static final String DEFAULT_THEME = "light";
+    /** From CSSHelper */
+    private static final String PROP_DISABLE_OLD = "routerconsole.disableOldThemes";
+    private static final boolean DEFAULT_DISABLE_OLD = false;
     public static final String PROP_CSS_DISABLED = "routerconsole.css.disabled";
     public static final String PROP_JS_DISABLED = "routerconsole.javascript.disabled";
     private static final String PROP_PW_ENABLE = "routerconsole.auth.enable";
@@ -336,6 +339,14 @@ public class IndexBean {
     
     public String getTheme() {
         String theme = _context.getProperty(PROP_THEME_NAME, DEFAULT_THEME);
+        // remap deprecated themes
+        if (theme.equals("midnight")) {
+            if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                theme = "dark";
+        } else if (theme.equals("classic")) {
+            if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                theme = "light";
+        }
         return "/themes/console/" + theme + "/";
     }
 
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/CSSHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/CSSHelper.java
index 87bdc9b2f7..f1301fa9f4 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/CSSHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/CSSHelper.java
@@ -31,6 +31,9 @@ public class CSSHelper extends HelperBase {
     public static final String PROP_FORCE_MOBILE_CONSOLE = "routerconsole.forceMobileConsole";
     /** @since 0.9.32 */
     public static final String PROP_EMBED_APPS = "routerconsole.embedApps";
+    /** @since 0.9.36 */
+    public static final String PROP_DISABLE_OLD = "routerconsole.disableOldThemes";
+    public static final boolean DEFAULT_DISABLE_OLD = false;
 
     private static final String _consoleNonce = Long.toString(RandomSource.getInstance().nextLong());
 
@@ -44,13 +47,22 @@ public class CSSHelper extends HelperBase {
 
     public String getTheme(String userAgent) {
         String url = BASE_THEME_PATH;
-        if (userAgent != null && (userAgent.contains("MSIE") && !userAgent.contains("Trident/6"))) {
+        if (userAgent != null && userAgent.contains("MSIE") && !userAgent.contains("Trident/6") &&
+            !_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD)) {
             url += FORCE + "/";
         } else {
             // This is the first thing to use _context on most pages
             if (_context == null)
                 throw new IllegalStateException("No contexts. This is usually because the router is either starting up or shutting down.");
             String theme = _context.getProperty(PROP_THEME_NAME, DEFAULT_THEME);
+            // remap deprecated themes
+            if (theme.equals("midnight")) {
+                if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                    theme = "dark";
+            } else if (theme.equals("classic")) {
+                if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                    theme = "light";
+            }
             url += theme + "/";
         }
         return url;
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigUIHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigUIHelper.java
index 29cc1c6b4a..fac2989e08 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigUIHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigUIHelper.java
@@ -11,12 +11,23 @@ import net.i2p.router.web.HelperBase;
 import net.i2p.router.web.Messages;
 import net.i2p.router.web.RouterConsoleRunner;
 
+/**
+ *  Helper for /configui
+ */
 public class ConfigUIHelper extends HelperBase {
 
     public String getSettings() {
         StringBuilder buf = new StringBuilder(512);
         buf.append("<div id=\"availablethemes\">");
         String current = _context.getProperty(CSSHelper.PROP_THEME_NAME, CSSHelper.DEFAULT_THEME);
+        // remap deprecated themes
+        if (current.equals("midnight")) {
+            if (_context.getProperty(CSSHelper.PROP_DISABLE_OLD, CSSHelper.DEFAULT_DISABLE_OLD))
+                current = "dark";
+        } else if (current.equals("classic")) {
+            if (_context.getProperty(CSSHelper.PROP_DISABLE_OLD, CSSHelper.DEFAULT_DISABLE_OLD))
+                current = "light";
+        }
         Set<String> themes = themeSet();
         for (String theme : themes) {
             buf.append("<label for=\"").append(theme).append("\"><div class=\"themechoice\">" +
@@ -71,10 +82,16 @@ public class ConfigUIHelper extends HelperBase {
          File[] files = dir.listFiles();
          if (files == null)
              return rv;
+         boolean skipOld = _context.getProperty(CSSHelper.PROP_DISABLE_OLD, CSSHelper.DEFAULT_DISABLE_OLD);
          for (int i = 0; i < files.length; i++) {
+             if (!files[i].isDirectory())
+                 continue;
              String name = files[i].getName();
-             if (files[i].isDirectory() && ! name.equals("images"))
-                 rv.add(name);
+             if (name.equals("images"))
+                 continue;
+             if (skipOld && (name.equals("midnight") || name.equals("classic")))
+                 continue;
+             rv.add(name);
          }
          // user themes
          Set<String> props = _context.getPropertyNames();
diff --git a/apps/susidns/src/java/src/i2p/susi/dns/BaseBean.java b/apps/susidns/src/java/src/i2p/susi/dns/BaseBean.java
index d1f8d942b5..d8640bd1d1 100644
--- a/apps/susidns/src/java/src/i2p/susi/dns/BaseBean.java
+++ b/apps/susidns/src/java/src/i2p/susi/dns/BaseBean.java
@@ -3,6 +3,8 @@ package i2p.susi.dns;
 import java.io.File;
 import java.io.FileFilter;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Properties;
 
 import net.i2p.I2PAppContext;
@@ -24,10 +26,13 @@ public class BaseBean
     private static final String PRIVATE_BOOK = "private_addressbook";
     private static final String DEFAULT_PRIVATE_BOOK = "../privatehosts.txt";
 
-    public static final String RC_PROP_THEME_NAME = "routerconsole.theme";
-    public static final String PROP_THEME_NAME = "theme";
-    public static final String DEFAULT_THEME = "light";
-    public static final String BASE_THEME_PATH = "/themes/susidns/";
+    private static final String RC_PROP_THEME_NAME = "routerconsole.theme";
+    private static final String PROP_THEME_NAME = "theme";
+    private static final String DEFAULT_THEME = "light";
+    private static final String BASE_THEME_PATH = "/themes/susidns/";
+    /** From CSSHelper */
+    private static final String PROP_DISABLE_OLD = "routerconsole.disableOldThemes";
+    private static final boolean DEFAULT_DISABLE_OLD = false;
     public static final String PROP_PW_ENABLE = "routerconsole.auth.enable";
     private static final String ADDRESSBOOK_DIR = "addressbook";
     private static final String CONFIG_FILE = "config.txt";
@@ -93,12 +98,22 @@ public class BaseBean
         String theme = _context.getProperty(RC_PROP_THEME_NAME, DEFAULT_THEME);
         // Apply any override
         theme = properties.getProperty(PROP_THEME_NAME, theme);
+        // remap deprecated themes
+        if (theme.equals("midnight")) {
+            if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                theme = "dark";
+        } else if (theme.equals("classic")) {
+            if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+                theme = "light";
+        }
         // Ensure that theme exists
         String[] themes = getThemes();
         boolean themeExists = false;
         for (int i = 0; i < themes.length; i++) {
-            if (themes[i].equals(theme))
+            if (themes[i].equals(theme)) {
                 themeExists = true;
+                break;
+            }
         }
         if (!themeExists)
             theme = DEFAULT_THEME;
@@ -112,19 +127,23 @@ public class BaseBean
      * @since 0.9.2
      */
     public String[] getThemes() {
-            String[] themes = null;
-            // "docs/themes/susidns/"
+            String[] themes;
             File dir = new File(_context.getBaseDir(), "docs/themes/susidns");
             FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } };
-            // Walk the themes dir, collecting the theme names, and append them to the map
             File[] dirnames = dir.listFiles(fileFilter);
             if (dirnames != null) {
-                themes = new String[dirnames.length];
-                for(int i = 0; i < dirnames.length; i++) {
-                    themes[i] = dirnames[i].getName();
+                List<String> th = new ArrayList<String>(dirnames.length);
+                boolean skipOld = _context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD);
+                for (int i = 0; i < dirnames.length; i++) {
+                    String name = dirnames[i].getName();
+                    if (skipOld && (name.equals("midnight") || name.equals("classic")))
+                        continue;
+                    th.add(name);
                 }
+                themes = th.toArray(new String[th.size()]);
+            } else {
+                themes = new String[0];
             }
-            // return the map.
             return themes;
     }
 
diff --git a/apps/susimail/src/src/i2p/susi/webmail/WebMail.java b/apps/susimail/src/src/i2p/susi/webmail/WebMail.java
index 9c570e658c..d36e494cf4 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/WebMail.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/WebMail.java
@@ -100,11 +100,6 @@ public class WebMail extends HttpServlet
 {
 	private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(WebMail.class);
 
-	/*
-	 * increase version number for every release
-	 */
-	private static final int version = 13;
-	
 	private static final long serialVersionUID = 1L;
 	private static final String LOGIN_NONCE = Long.toString(I2PAppContext.getGlobalContext().random().nextLong());
 	
@@ -258,6 +253,9 @@ public class WebMail extends HttpServlet
 	private static final String RC_PROP_FORCE_MOBILE_CONSOLE = "routerconsole.forceMobileConsole";
 	private static final String CONFIG_THEME = "theme";
 	private static final String DEFAULT_THEME = "light";
+	/** From CSSHelper */
+	private static final String PROP_DISABLE_OLD = "routerconsole.disableOldThemes";
+	private static final boolean DEFAULT_DISABLE_OLD = false;
 
 	private static final String spacer = ""; /* this is best done with css */
 	private static final String thSpacer = "<th>&nbsp;</th>\n";
@@ -1948,22 +1946,31 @@ public class WebMail extends HttpServlet
 		I2PAppContext ctx = I2PAppContext.getGlobalContext();
 		// Fetch routerconsole theme (or use our default if it doesn't exist)
 		String theme = ctx.getProperty(RC_PROP_THEME, DEFAULT_THEME);
-		// Apply any override
-		theme = Config.getProperty(CONFIG_THEME, theme);
 		boolean universalTheming = ctx.getBooleanProperty(RC_PROP_UNIVERSAL_THEMING);
 		if (universalTheming) {
-			// Fetch routerconsole theme (or use our default if it doesn't exist)
-			theme = ctx.getProperty(RC_PROP_THEME, DEFAULT_THEME);
 			// Ensure that theme exists
-			String[] themes = getThemes();
+			String[] themes = getThemes(ctx);
 			boolean themeExists = false;
 			for (int i = 0; i < themes.length; i++) {
-				if (themes[i].equals(theme))
+				if (themes[i].equals(theme)) {
 					themeExists = true;
+					break;
+				}
 			}
 			if (!themeExists) {
 				theme = DEFAULT_THEME;
 			}
+		} else {
+			// Apply any override
+			theme = Config.getProperty(CONFIG_THEME, theme);
+		}
+		// remap deprecated themes
+		if (theme.equals("midnight")) {
+			if (ctx.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+				theme = "dark";
+		} else if (theme.equals("classic")) {
+			if (ctx.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
+				theme = "light";
 		}
 		boolean forceMobileConsole = ctx.getBooleanProperty(RC_PROP_FORCE_MOBILE_CONSOLE);
 		boolean isMobile = (forceMobileConsole || isMobile(httpRequest.getHeader("User-Agent")));
@@ -3520,20 +3527,24 @@ public class WebMail extends HttpServlet
      * Get all themes
      * @return String[] -- Array of all the themes found.
      */
-    private static String[] getThemes() {
-            String[] themes = null;
-            // "docs/themes/susimail/"
-            File dir = new File(I2PAppContext.getGlobalContext().getBaseDir(), "docs/themes/susimail");
+    private static String[] getThemes(I2PAppContext ctx) {
+            String[] themes;
+            File dir = new File(ctx.getBaseDir(), "docs/themes/susimail");
             FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } };
-            // Walk the themes dir, collecting the theme names, and append them to the map
             File[] dirnames = dir.listFiles(fileFilter);
             if (dirnames != null) {
-                themes = new String[dirnames.length];
-                for(int i = 0; i < dirnames.length; i++) {
-                    themes[i] = dirnames[i].getName();
-                }
+		List<String> th = new ArrayList<String>(dirnames.length);
+		boolean skipOld = ctx.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD);
+		for (int i = 0; i < dirnames.length; i++) {
+			String name = dirnames[i].getName();
+			if (skipOld && (name.equals("midnight") || name.equals("classic")))
+				continue;
+			th.add(name);
+		}
+		themes = th.toArray(new String[th.size()]);
+            } else {
+                themes = new String[0];
             }
-            // return the map.
             return themes;
     }
 }
-- 
GitLab