From 37e3e9e2cfac666c90e669bf695f09ecc1ad308b Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Wed, 9 Nov 2011 18:38:39 +0000
Subject: [PATCH]   * Console: Add ability to hide news

---
 .../src/net/i2p/router/web/CSSHelper.java     | 11 +++
 .../src/net/i2p/router/web/NewsFetcher.java   | 86 +++++++++++++------
 .../src/net/i2p/router/web/NewsHelper.java    |  5 ++
 .../i2p/router/web/RouterConsoleRunner.java   | 11 +--
 apps/routerconsole/jsp/configupdate.jsp       |  2 +-
 apps/routerconsole/jsp/css.jsi                |  1 +
 apps/routerconsole/jsp/index.jsp              | 14 ++-
 .../themes/console/light/console.css          |  4 -
 8 files changed, 93 insertions(+), 41 deletions(-)

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 269f0c7768..cc5b415c32 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/CSSHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/CSSHelper.java
@@ -48,6 +48,17 @@ public class CSSHelper extends HelperBase {
         return Messages.getLanguage(_context);
     }
 
+    /**
+     *  Show / hide news on home page
+     *  @param val if non-null, "1" to show, else hide
+     *  @since 0.8.12
+     */
+    public void setNews(String val) {
+        // Protected with nonce in css.jsi
+        if (val != null)
+            NewsFetcher.getInstance(_context).showNews(val.equals("1"));
+    }
+
     /** change refresh and save it */
     public void setRefresh(String r) {
         _context.router().setConfigSetting(PROP_REFRESH, r);
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java b/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java
index c7a0b672bf..253c36bd45 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java
@@ -24,8 +24,8 @@ import net.i2p.util.Log;
  * track of whether that has an announcement for a new version.
  */
 public class NewsFetcher implements Runnable, EepGet.StatusListener {
-    private I2PAppContext _context;
-    private Log _log;
+    private final RouterContext _context;
+    private final Log _log;
     private boolean _updateAvailable;
     private boolean _unsignedUpdateAvailable;
     private long _lastFetch;
@@ -34,13 +34,13 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
     private String _unsignedUpdateVersion;
     private String _lastModified;
     private boolean _invalidated;
-    private File _newsFile;
-    private File _tempFile;
+    private final File _newsFile;
+    private final File _tempFile;
     private static NewsFetcher _instance;
     private volatile boolean _isRunning;
 
     //public static final synchronized NewsFetcher getInstance() { return _instance; }
-    public static final synchronized NewsFetcher getInstance(I2PAppContext ctx) { 
+    public static final synchronized NewsFetcher getInstance(RouterContext ctx) { 
         if (_instance != null)
             return _instance;
         _instance = new NewsFetcher(ctx);
@@ -52,8 +52,10 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
     /** @since 0.7.14 not configurable */
     private static final String BACKUP_NEWS_URL = "http://www.i2p2.i2p/_static/news/news.xml";
     private static final String PROP_LAST_CHECKED = "router.newsLastChecked";
+    /** @since 0.8.12 */
+    private static final String PROP_LAST_HIDDEN = "routerconsole.newsLastHidden";
     
-    private NewsFetcher(I2PAppContext ctx) {
+    private NewsFetcher(RouterContext ctx) {
         _context = ctx;
         _log = ctx.logManager().getLog(NewsFetcher.class);
         _instance = this;
@@ -94,9 +96,40 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
     public boolean unsignedUpdateAvailable() { return _unsignedUpdateAvailable; }
     public String unsignedUpdateVersion() { return _unsignedUpdateVersion; }
 
+    /**
+     *  Is the news newer than the last time it was hidden?
+     *  @since 0.8.12
+     */
+    public boolean shouldShowNews() {
+        if (_lastUpdated <= 0)
+            return true;
+        String h = _context.getProperty(PROP_LAST_HIDDEN);
+        if (h == null)
+            return true;
+        long last = 0;
+        try {
+            last = Long.parseLong(h);
+        } catch (NumberFormatException nfe) {}
+        return _lastUpdated > last;
+    }
+
+    /**
+     *  Save config with the timestamp of the current news to hide, or 0 to show
+     *  @since 0.8.12
+     */
+    public void showNews(boolean yes) {
+        long stamp = yes ? 0 : _lastUpdated;
+        _context.router().setConfigSetting(PROP_LAST_HIDDEN, Long.toString(stamp));
+        _context.router().saveConfig();
+    }
+
+    /**
+     *  @return HTML
+     */
     public String status() {
          StringBuilder buf = new StringBuilder(128);
          long now = _context.clock().now();
+         buf.append("<i>");
          if (_lastUpdated > 0) {
              buf.append(Messages.getString("News last updated {0} ago.",
                                            DataHelper.formatDuration2(now - _lastUpdated),
@@ -108,6 +141,18 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
                                            DataHelper.formatDuration2(now - _lastFetch),
                                            _context));
          }
+         buf.append("</i>");
+         String consoleNonce = System.getProperty("router.consoleNonce");
+         if (_lastUpdated > 0 && consoleNonce != null) {
+             if (shouldShowNews()) {
+                 buf.append(" <a href=\"/?news=0&amp;consoleNonce=").append(consoleNonce).append("\">")
+                    .append(Messages.getString("Hide news", _context));
+             } else {
+                 buf.append(" <a href=\"/?news=1&amp;consoleNonce=").append(consoleNonce).append("\">")
+                    .append(Messages.getString("Show news", _context));
+             }
+             buf.append("</a>");
+         }
          return buf.toString();
     }
     
@@ -232,16 +277,15 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
             if (get.fetch()) {
                 String lastmod = get.getLastModified();
                 if (lastmod != null) {
-                    if (!(_context.isRouterContext())) return;
                     long modtime = RFC822Date.parse822Date(lastmod);
                     if (modtime <= 0) return;
                     String lastUpdate = _context.getProperty(UpdateHandler.PROP_LAST_UPDATE_TIME);
                     if (lastUpdate == null) {
                         // we don't know what version you have, so stamp it with the current time,
                         // and we'll look for something newer next time around.
-                        ((RouterContext)_context).router().setConfigSetting(UpdateHandler.PROP_LAST_UPDATE_TIME,
-                                                                            "" + _context.clock().now());
-                        ((RouterContext)_context).router().saveConfig();
+                        _context.router().setConfigSetting(UpdateHandler.PROP_LAST_UPDATE_TIME,
+                                                           Long.toString(_context.clock().now()));
+                        _context.router().saveConfig();
                         return;
                     }
                     long ms = 0;
@@ -267,7 +311,7 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
         String url = _context.getProperty(ConfigUpdateHandler.PROP_ZIP_URL);
         if (url == null || url.length() <= 0)
             return;
-        UpdateHandler handler = new UnsignedUpdateHandler((RouterContext)_context, url,
+        UpdateHandler handler = new UnsignedUpdateHandler(_context, url,
                                                           _unsignedUpdateVersion);
         handler.update();
     }
@@ -329,18 +373,8 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
         if (shouldInstall()) {
             if (_log.shouldLog(Log.DEBUG))
                 _log.debug("Policy requests update, so we update");
-            UpdateHandler handler = null;
-            if (_context.isRouterContext()) {
-                handler = new UpdateHandler((RouterContext)_context);
-            } else {
-                List contexts = RouterContext.listContexts();
-                if (!contexts.isEmpty())
-                    handler = new UpdateHandler((RouterContext)contexts.get(0));
-                else
-                    _log.log(Log.CRIT, "No router context to update with?");
-            }
-            if (handler != null)
-                handler.update();
+            UpdateHandler handler = new UpdateHandler(_context);
+            handler.update();
         } else {
             if (_log.shouldLog(Log.DEBUG))
                 _log.debug("Policy requests manual update, so we do nothing");
@@ -373,10 +407,8 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
                 _log.warn("Transfer complete, but no file? - probably 304 Not Modified");
         }
         _lastFetch = now;
-        if (_context.isRouterContext()) {
-            ((RouterContext)_context).router().setConfigSetting(PROP_LAST_CHECKED, "" + now);
-            ((RouterContext)_context).router().saveConfig();
-        }
+        _context.router().setConfigSetting(PROP_LAST_CHECKED, Long.toString(now));
+        _context.router().saveConfig();
     }
     
     public void transferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt) {
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/NewsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/NewsHelper.java
index 26377d28f1..709c4505ff 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/NewsHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/NewsHelper.java
@@ -17,4 +17,9 @@ public class NewsHelper extends ContentHelper {
             _page = (new File(_context.getBaseDir(), "docs/initialNews/initialNews.xml")).getAbsolutePath();
         return super.getContent();
     } 
+
+    /** @since 0.8.12 */
+    public boolean shouldShowNews() {
+        return NewsFetcher.getInstance(_context).shouldShowNews();
+    }
 }
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java b/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java
index a436e3e83b..523690764e 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java
@@ -341,16 +341,17 @@ public class RouterConsoleRunner {
             }
         }
 
-        NewsFetcher fetcher = NewsFetcher.getInstance(I2PAppContext.getGlobalContext());
-        Thread newsThread = new I2PAppThread(fetcher, "NewsFetcher", true);
-        newsThread.start();
-        
         Thread t = new I2PAppThread(new StatSummarizer(), "StatSummarizer", true);
         t.start();
         
         List<RouterContext> contexts = RouterContext.listContexts();
         if (contexts != null) {
             RouterContext ctx = contexts.get(0);
+
+            NewsFetcher fetcher = NewsFetcher.getInstance(ctx);
+            Thread newsThread = new I2PAppThread(fetcher, "NewsFetcher", true);
+            newsThread.start();
+        
             if (PluginStarter.pluginsEnabled(ctx)) {
                 t = new I2PAppThread(new PluginStarter(ctx), "PluginStarter", true);
                 t.start();
@@ -359,7 +360,7 @@ public class RouterConsoleRunner {
             ctx.addShutdownTask(new NewsShutdown(fetcher, newsThread));
             // stat summarizer registers its own hook
             ctx.addShutdownTask(new ServerShutdown());
-        }
+        } // else log CRIT ?
     }
     
     /**
diff --git a/apps/routerconsole/jsp/configupdate.jsp b/apps/routerconsole/jsp/configupdate.jsp
index 2b35fad744..481a3b7ae5 100644
--- a/apps/routerconsole/jsp/configupdate.jsp
+++ b/apps/routerconsole/jsp/configupdate.jsp
@@ -20,7 +20,7 @@
  <jsp:useBean class="net.i2p.router.web.ConfigUpdateHelper" id="updatehelper" scope="request" />
  <jsp:setProperty name="updatehelper" property="contextId" value="<%=(String)session.getAttribute("i2p.contextId")%>" />
 <div class="messages">
-<i><jsp:getProperty name="updatehelper" property="newsStatus" /></i></div>
+ <jsp:getProperty name="updatehelper" property="newsStatus" /></div>
 <div class="configure">
  <form action="" method="POST">
  <input type="hidden" name="nonce" value="<jsp:getProperty name="formhandler" property="newNonce" />" >
diff --git a/apps/routerconsole/jsp/css.jsi b/apps/routerconsole/jsp/css.jsi
index 2dbd19053c..33c9312c6a 100644
--- a/apps/routerconsole/jsp/css.jsi
+++ b/apps/routerconsole/jsp/css.jsi
@@ -32,6 +32,7 @@
    String conNonceParam = request.getParameter("consoleNonce");
    if (conNonceParam != null && conNonceParam.equals(System.getProperty("router.consoleNonce"))) {
        intl.setLang(request.getParameter("lang"));
+       intl.setNews(request.getParameter("news"));
    }
 %>
 <link href="<%=intl.getTheme(request.getHeader("User-Agent"))%>console.css" rel="stylesheet" type="text/css">
diff --git a/apps/routerconsole/jsp/index.jsp b/apps/routerconsole/jsp/index.jsp
index 167dbff9aa..1bae408aec 100644
--- a/apps/routerconsole/jsp/index.jsp
+++ b/apps/routerconsole/jsp/index.jsp
@@ -18,14 +18,20 @@
 <div class="news" id="news">
  <jsp:useBean class="net.i2p.router.web.NewsHelper" id="newshelper" scope="request" />
  <jsp:setProperty name="newshelper" property="contextId" value="<%=(String)session.getAttribute("i2p.contextId")%>" />
- <% java.io.File fpath = new java.io.File(net.i2p.I2PAppContext.getGlobalContext().getRouterDir(), "docs/news.xml"); %>
+<%
+   if (newshelper.shouldShowNews()) {
+       java.io.File fpath = new java.io.File(net.i2p.I2PAppContext.getGlobalContext().getRouterDir(), "docs/news.xml");
+%>
  <jsp:setProperty name="newshelper" property="page" value="<%=fpath.getAbsolutePath()%>" />
  <jsp:setProperty name="newshelper" property="maxLines" value="300" />
  <jsp:getProperty name="newshelper" property="content" />
-
+ <hr>
+<%
+   }  // shouldShowNews()
+%>
  <jsp:useBean class="net.i2p.router.web.ConfigUpdateHelper" id="updatehelper" scope="request" />
  <jsp:setProperty name="updatehelper" property="contextId" value="<%=(String)session.getAttribute("i2p.contextId")%>" />
- <hr><i><jsp:getProperty name="updatehelper" property="newsStatus" /></i><br>
+ <jsp:getProperty name="updatehelper" property="newsStatus" /><br>
 </div><div class="main" id="main">
  <jsp:useBean class="net.i2p.router.web.ContentHelper" id="contenthelper" scope="request" />
  <div class="welcome">
@@ -50,7 +56,7 @@
   <a name="top"></a>
   <h2><%=intl._("Welcome to I2P")%></h2>
  </div>
- <% fpath = new java.io.File(net.i2p.I2PAppContext.getGlobalContext().getBaseDir(), "docs/readme.html"); %>
+ <% java.io.File fpath = new java.io.File(net.i2p.I2PAppContext.getGlobalContext().getBaseDir(), "docs/readme.html"); %>
  <jsp:setProperty name="contenthelper" property="page" value="<%=fpath.getAbsolutePath()%>" />
  <jsp:setProperty name="contenthelper" property="maxLines" value="300" />
  <jsp:setProperty name="contenthelper" property="contextId" value="<%=(String)session.getAttribute("i2p.contextId")%>" />
diff --git a/installer/resources/themes/console/light/console.css b/installer/resources/themes/console/light/console.css
index 84bf69dba0..73e57a9e19 100644
--- a/installer/resources/themes/console/light/console.css
+++ b/installer/resources/themes/console/light/console.css
@@ -419,10 +419,6 @@ div.news h3 {
      text-shadow: 0px 0px 0px #77f;
 }
 
-div.news i {
-     font-style: normal;
-}
-
 div.news h4 {
      border-bottom: 0px;
      padding: 0;
-- 
GitLab