From 4881ac32b6377bf7cb5327293e2e2cbfe58b64a2 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Thu, 9 Feb 2023 14:09:10 -0500
Subject: [PATCH] DTG: Support notification disable/enable in non-router
 context

by moving all the code from InternalTrayManager to TrayManager
and adding to the ExternalTrayManager menu
As requested by R4SAS
---
 .../i2p/desktopgui/ExternalTrayManager.java   | 17 +++-
 .../i2p/desktopgui/InternalTrayManager.java   | 77 +++-------------
 .../src/net/i2p/desktopgui/TrayManager.java   | 87 +++++++++++++++++++
 3 files changed, 113 insertions(+), 68 deletions(-)

diff --git a/apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java
index c9317b0678..ea24f21942 100644
--- a/apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java
+++ b/apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java
@@ -51,6 +51,9 @@ class ExternalTrayManager extends TrayManager {
             }
         });
         popup.add(startItem);
+        initializeNotificationItems();
+        popup.add(_notificationItem2);
+        popup.add(_notificationItem1);
         return popup;
     }
 
@@ -79,6 +82,9 @@ class ExternalTrayManager extends TrayManager {
             }
         });
         popup.add(startItem);
+        initializeJNotificationItems();
+        popup.add(_jnotificationItem2);
+        popup.add(_jnotificationItem1);
         return popup;
     }
 
@@ -86,5 +92,14 @@ class ExternalTrayManager extends TrayManager {
      * Update the menu
      * @since 0.9.26
      */
-    protected void updateMenu() {}
+    protected void updateMenu() {
+        if (_notificationItem1 != null)
+            _notificationItem1.setEnabled(_showNotifications);
+        if (_notificationItem2 != null)
+            _notificationItem2.setEnabled(!_showNotifications);
+        if (_jnotificationItem1 != null)
+            _jnotificationItem1.setVisible(_showNotifications);
+        if (_jnotificationItem2 != null)
+            _jnotificationItem2.setVisible(!_showNotifications);
+    }
 }
diff --git a/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java
index 4a5afb5608..6e3100eadb 100644
--- a/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java
+++ b/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java
@@ -32,11 +32,9 @@ class InternalTrayManager extends TrayManager {
     private final Log log;
     private final Main _main;
     private MenuItem _statusItem, _browserItem, _configItem, _restartItem, _stopItem,
-                     _restartHardItem, _stopHardItem, _cancelItem,
-                     _notificationItem1, _notificationItem2;
+                     _restartHardItem, _stopHardItem, _cancelItem;
     private JMenuItem _jstatusItem, _jbrowserItem, _jconfigItem, _jrestartItem, _jstopItem,
-                      _jrestartHardItem, _jstopHardItem, _jcancelItem,
-                      _jnotificationItem1, _jnotificationItem2;
+                      _jrestartHardItem, _jstopHardItem, _jcancelItem;
 
     private static final boolean CONSOLE_ENABLED = Desktop.isDesktopSupported() &&
                                                    Desktop.getDesktop().isSupported(Action.BROWSE);
@@ -86,33 +84,6 @@ class InternalTrayManager extends TrayManager {
         }
 
         PopupMenu desktopguiConfigurationLauncher = new PopupMenu(_t("Configure I2P System Tray"));
-        final MenuItem notificationItem2 = new MenuItem(_t("Enable notifications"));
-        notificationItem2.addActionListener(new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent arg0) {
-                new SwingWorker<Object, Object>() {
-                    @Override
-                    protected Object doInBackground() throws Exception {
-                        configureNotifications(true);
-                        return null;
-                    }
-                }.execute();
-            }
-        });
-
-        final MenuItem notificationItem1 = new MenuItem(_t("Disable notifications"));
-        notificationItem1.addActionListener(new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent arg0) {
-                new SwingWorker<Object, Object>() {
-                    @Override
-                    protected Object doInBackground() throws Exception {
-                        configureNotifications(false);
-                        return null;
-                    }
-                }.execute();
-            }
-        });
 
         MenuItem configSubmenu = new MenuItem(_t("Disable system tray"));
         configSubmenu.addActionListener(new ActionListener() {
@@ -214,8 +185,9 @@ class InternalTrayManager extends TrayManager {
             popup.add(browserLauncher);
             popup.addSeparator();
         }
-        desktopguiConfigurationLauncher.add(notificationItem2);
-        desktopguiConfigurationLauncher.add(notificationItem1);
+        initializeNotificationItems();
+        desktopguiConfigurationLauncher.add(_notificationItem2);
+        desktopguiConfigurationLauncher.add(_notificationItem1);
         desktopguiConfigurationLauncher.add(configSubmenu);
         popup.add(desktopguiConfigurationLauncher);
         popup.addSeparator();
@@ -230,8 +202,6 @@ class InternalTrayManager extends TrayManager {
         _statusItem = statusItem;
         _browserItem = browserLauncher;
         _configItem = desktopguiConfigurationLauncher;
-        _notificationItem1 = notificationItem1;
-        _notificationItem2 = notificationItem2;
         _restartItem = restartItem;
         _stopItem = stopItem;
         _restartHardItem = restartItem2;
@@ -270,33 +240,6 @@ class InternalTrayManager extends TrayManager {
         }
 
         JMenu desktopguiConfigurationLauncher = new JMenu(_t("Configure I2P System Tray"));
-        final JMenuItem notificationItem2 = new JMenuItem(_t("Enable notifications"));
-        notificationItem2.addActionListener(new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent arg0) {
-                new SwingWorker<Object, Object>() {
-                    @Override
-                    protected Object doInBackground() throws Exception {
-                        configureNotifications(true);
-                        return null;
-                    }
-                }.execute();
-            }
-        });
-
-        final JMenuItem notificationItem1 = new JMenuItem(_t("Disable notifications"));
-        notificationItem1.addActionListener(new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent arg0) {
-                new SwingWorker<Object, Object>() {
-                    @Override
-                    protected Object doInBackground() throws Exception {
-                        configureNotifications(false);
-                        return null;
-                    }
-                }.execute();
-            }
-        });
 
         JMenuItem configSubmenu = new JMenuItem(_t("Disable system tray"));
         configSubmenu.addActionListener(new ActionListener() {
@@ -398,8 +341,9 @@ class InternalTrayManager extends TrayManager {
             popup.add(browserLauncher);
             popup.addSeparator();
         }
-        desktopguiConfigurationLauncher.add(notificationItem2);
-        desktopguiConfigurationLauncher.add(notificationItem1);
+        initializeJNotificationItems();
+        desktopguiConfigurationLauncher.add(_jnotificationItem2);
+        desktopguiConfigurationLauncher.add(_jnotificationItem1);
         desktopguiConfigurationLauncher.add(configSubmenu);
         popup.add(desktopguiConfigurationLauncher);
         popup.addSeparator();
@@ -414,8 +358,6 @@ class InternalTrayManager extends TrayManager {
         _jstatusItem = statusItem;
         _jbrowserItem = browserLauncher;
         _jconfigItem = desktopguiConfigurationLauncher;
-        _jnotificationItem1 = notificationItem1;
-        _jnotificationItem2 = notificationItem2;
         _jrestartItem = restartItem;
         _jstopItem = stopItem;
         _jrestartHardItem = restartItem2;
@@ -511,7 +453,8 @@ class InternalTrayManager extends TrayManager {
     /**
      *  @since 0.9.53
      */
-    private void configureNotifications(boolean enable) {
+    @Override
+    protected void configureNotifications(boolean enable) {
         _showNotifications = enable;
         String value = Boolean.toString(enable);
         if (!_context.router().saveConfig(PROP_NOTIFICATIONS, value))
diff --git a/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java
index f17f8b2d04..0261c107d3 100644
--- a/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java
+++ b/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java
@@ -17,7 +17,9 @@ import java.awt.event.MouseListener;
 import java.io.IOException;
 import java.net.URL;
 
+import java.awt.MenuItem;
 import javax.swing.JFrame;
+import javax.swing.JMenuItem;
 import javax.swing.JPopupMenu;
 import javax.swing.SwingWorker;
 import javax.swing.event.MenuKeyEvent;
@@ -43,6 +45,8 @@ abstract class TrayManager {
     ///Our tray icon, or null if unsupported
     protected TrayIcon trayIcon;
     protected volatile boolean _showNotifications;
+    protected MenuItem  _notificationItem1, _notificationItem2;
+    protected JMenuItem _jnotificationItem1, _jnotificationItem2;
 
     private static final String PNG_DIR = "/desktopgui/resources/images/";
     private static final String MAC_ICON = "itoopie_black_24.png";
@@ -288,6 +292,89 @@ abstract class TrayManager {
         return 0;
     }
 
+    /**
+     *  Does not save. See InternalTrayManager.
+     *
+     *  @since 0.9.58 moved up from InternalTrayManager
+     */
+    protected void configureNotifications(boolean enable) {
+        _showNotifications = enable;
+    }
+
+    /**
+     *  Initializes _notificationItem 1 and 2
+     *
+     *  @since 0.9.58 pulled out of InternalTrayManager
+     */
+    protected void initializeNotificationItems() {
+        final MenuItem notificationItem2 = new MenuItem(_t("Enable notifications"));
+        notificationItem2.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent arg0) {
+                new SwingWorker<Object, Object>() {
+                    @Override
+                    protected Object doInBackground() throws Exception {
+                        configureNotifications(true);
+                        return null;
+                    }
+                }.execute();
+            }
+        });
+        _notificationItem2 = notificationItem2;
+
+        final MenuItem notificationItem1 = new MenuItem(_t("Disable notifications"));
+        notificationItem1.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent arg0) {
+                new SwingWorker<Object, Object>() {
+                    @Override
+                    protected Object doInBackground() throws Exception {
+                        configureNotifications(false);
+                        return null;
+                    }
+                }.execute();
+            }
+        });
+        _notificationItem1 = notificationItem1;
+    }
+
+    /**
+     *  Initializes _jnotificationItem 1 and 2
+     *
+     *  @since 0.9.58 pulled out of InternalTrayManager
+     */
+    protected void initializeJNotificationItems() {
+        final JMenuItem notificationItem2 = new JMenuItem(_t("Enable notifications"));
+        notificationItem2.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent arg0) {
+                new SwingWorker<Object, Object>() {
+                    @Override
+                    protected Object doInBackground() throws Exception {
+                        configureNotifications(true);
+                        return null;
+                    }
+                }.execute();
+            }
+        });
+        _jnotificationItem2 = notificationItem2;
+
+        final JMenuItem notificationItem1 = new JMenuItem(_t("Disable notifications"));
+        notificationItem1.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent arg0) {
+                new SwingWorker<Object, Object>() {
+                    @Override
+                    protected Object doInBackground() throws Exception {
+                        configureNotifications(false);
+                        return null;
+                    }
+                }.execute();
+            }
+        });
+        _jnotificationItem1 = notificationItem1;
+    }
+
     protected String _t(String s) {
         return DesktopguiTranslator._t(_appContext, s);
     }
-- 
GitLab