DTG: Add menu items to control notifications

Fix check for successful configuration save
This commit is contained in:
zzz
2021-12-05 06:38:28 -05:00
parent 730b9790d9
commit cbb6a6db65
2 changed files with 98 additions and 13 deletions

View File

@ -31,9 +31,11 @@ class InternalTrayManager extends TrayManager {
private final RouterContext _context; private final RouterContext _context;
private final Log log; private final Log log;
private MenuItem _statusItem, _browserItem, _configItem, _restartItem, _stopItem, private MenuItem _statusItem, _browserItem, _configItem, _restartItem, _stopItem,
_restartHardItem, _stopHardItem, _cancelItem; _restartHardItem, _stopHardItem, _cancelItem,
_notificationItem1, _notificationItem2;
private JMenuItem _jstatusItem, _jbrowserItem, _jconfigItem, _jrestartItem, _jstopItem, private JMenuItem _jstatusItem, _jbrowserItem, _jconfigItem, _jrestartItem, _jstopItem,
_jrestartHardItem, _jstopHardItem, _jcancelItem; _jrestartHardItem, _jstopHardItem, _jcancelItem,
_jnotificationItem1, _jnotificationItem2;
private static final boolean CONSOLE_ENABLED = Desktop.isDesktopSupported() && private static final boolean CONSOLE_ENABLED = Desktop.isDesktopSupported() &&
Desktop.getDesktop().isSupported(Action.BROWSE); Desktop.getDesktop().isSupported(Action.BROWSE);
@ -82,7 +84,35 @@ class InternalTrayManager extends TrayManager {
} }
PopupMenu desktopguiConfigurationLauncher = new PopupMenu(_t("Configure I2P System Tray")); PopupMenu desktopguiConfigurationLauncher = new PopupMenu(_t("Configure I2P System Tray"));
MenuItem configSubmenu = new MenuItem(_t("Disable")); 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() { configSubmenu.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
@ -182,6 +212,8 @@ class InternalTrayManager extends TrayManager {
popup.add(browserLauncher); popup.add(browserLauncher);
popup.addSeparator(); popup.addSeparator();
} }
desktopguiConfigurationLauncher.add(notificationItem2);
desktopguiConfigurationLauncher.add(notificationItem1);
desktopguiConfigurationLauncher.add(configSubmenu); desktopguiConfigurationLauncher.add(configSubmenu);
popup.add(desktopguiConfigurationLauncher); popup.add(desktopguiConfigurationLauncher);
popup.addSeparator(); popup.addSeparator();
@ -196,6 +228,8 @@ class InternalTrayManager extends TrayManager {
_statusItem = statusItem; _statusItem = statusItem;
_browserItem = browserLauncher; _browserItem = browserLauncher;
_configItem = desktopguiConfigurationLauncher; _configItem = desktopguiConfigurationLauncher;
_notificationItem1 = notificationItem1;
_notificationItem2 = notificationItem2;
_restartItem = restartItem; _restartItem = restartItem;
_stopItem = stopItem; _stopItem = stopItem;
_restartHardItem = restartItem2; _restartHardItem = restartItem2;
@ -234,7 +268,35 @@ class InternalTrayManager extends TrayManager {
} }
JMenu desktopguiConfigurationLauncher = new JMenu(_t("Configure I2P System Tray")); JMenu desktopguiConfigurationLauncher = new JMenu(_t("Configure I2P System Tray"));
JMenuItem configSubmenu = new JMenuItem(_t("Disable")); 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() { configSubmenu.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
@ -334,6 +396,8 @@ class InternalTrayManager extends TrayManager {
popup.add(browserLauncher); popup.add(browserLauncher);
popup.addSeparator(); popup.addSeparator();
} }
desktopguiConfigurationLauncher.add(notificationItem2);
desktopguiConfigurationLauncher.add(notificationItem1);
desktopguiConfigurationLauncher.add(configSubmenu); desktopguiConfigurationLauncher.add(configSubmenu);
popup.add(desktopguiConfigurationLauncher); popup.add(desktopguiConfigurationLauncher);
popup.addSeparator(); popup.addSeparator();
@ -348,6 +412,8 @@ class InternalTrayManager extends TrayManager {
_jstatusItem = statusItem; _jstatusItem = statusItem;
_jbrowserItem = browserLauncher; _jbrowserItem = browserLauncher;
_jconfigItem = desktopguiConfigurationLauncher; _jconfigItem = desktopguiConfigurationLauncher;
_jnotificationItem1 = notificationItem1;
_jnotificationItem2 = notificationItem2;
_jrestartItem = restartItem; _jrestartItem = restartItem;
_jstopItem = stopItem; _jstopItem = stopItem;
_jrestartHardItem = restartItem2; _jrestartHardItem = restartItem2;
@ -399,6 +465,10 @@ class InternalTrayManager extends TrayManager {
_stopHardItem.setEnabled(!imminent); _stopHardItem.setEnabled(!imminent);
if (_cancelItem != null) if (_cancelItem != null)
_cancelItem.setEnabled(x && !imminent); _cancelItem.setEnabled(x && !imminent);
if (_notificationItem1 != null)
_notificationItem1.setEnabled(_showNotifications);
if (_notificationItem2 != null)
_notificationItem2.setEnabled(!_showNotifications);
if (_jstatusItem != null) if (_jstatusItem != null)
_jstatusItem.setText(status); _jstatusItem.setText(status);
@ -416,6 +486,10 @@ class InternalTrayManager extends TrayManager {
_jstopHardItem.setVisible(!imminent); _jstopHardItem.setVisible(!imminent);
if (_jcancelItem != null) if (_jcancelItem != null)
_jcancelItem.setVisible(x && !imminent); _jcancelItem.setVisible(x && !imminent);
if (_jnotificationItem1 != null)
_jnotificationItem1.setVisible(_showNotifications);
if (_jnotificationItem2 != null)
_jnotificationItem2.setVisible(!_showNotifications);
} }
/** /**
@ -424,16 +498,22 @@ class InternalTrayManager extends TrayManager {
private void configureDesktopgui(boolean enable) { private void configureDesktopgui(boolean enable) {
String property = Main.PROP_ENABLE; String property = Main.PROP_ENABLE;
String value = Boolean.toString(enable); String value = Boolean.toString(enable);
try { if (!_context.router().saveConfig(property, value))
log.error("Error saving config");
_context.router().saveConfig(property, value);
if (!enable) { if (!enable) {
// TODO popup that explains how to re-enable in console // TODO popup that explains how to re-enable in console
_main.shutdown(null); _main.shutdown(null);
} }
} catch (Exception ex) {
log.error("Error saving config", ex);
} }
/**
* @since 0.9.53
*/
private void configureNotifications(boolean enable) {
_showNotifications = enable;
String value = Boolean.toString(enable);
if (!_context.router().saveConfig(PROP_NOTIFICATIONS, value))
log.error("Error saving config");
} }
/** /**

View File

@ -43,12 +43,14 @@ abstract class TrayManager {
protected SystemTray tray; protected SystemTray tray;
///Our tray icon, or null if unsupported ///Our tray icon, or null if unsupported
protected TrayIcon trayIcon; protected TrayIcon trayIcon;
protected volatile boolean _showNotifications;
private static final String PNG_DIR = "/desktopgui/resources/images/"; private static final String PNG_DIR = "/desktopgui/resources/images/";
private static final String MAC_ICON = "itoopie_black_24.png"; private static final String MAC_ICON = "itoopie_black_24.png";
private static final String WIN_ICON_LIGHT = "itoopie_white_24.png"; private static final String WIN_ICON_LIGHT = "itoopie_white_24.png";
private static final String WIN_ICON_DARK = "itoopie_black_24.png"; private static final String WIN_ICON_DARK = "itoopie_black_24.png";
private static final String LIN_ICON = "logo.png"; private static final String LIN_ICON = "logo.png";
protected static final String PROP_NOTIFICATIONS = "desktopgui.showNotifications";
/** /**
* Instantiate tray manager. * Instantiate tray manager.
@ -65,6 +67,7 @@ abstract class TrayManager {
public synchronized void startManager() throws AWTException { public synchronized void startManager() throws AWTException {
if (!SystemTray.isSupported()) if (!SystemTray.isSupported())
throw new AWTException("SystemTray not supported"); throw new AWTException("SystemTray not supported");
_showNotifications = _appContext.getBooleanPropertyDefaultTrue(PROP_NOTIFICATIONS);
tray = SystemTray.getSystemTray(); tray = SystemTray.getSystemTray();
// Windows typically has tooltips; Linux (at least Ubuntu) doesn't // Windows typically has tooltips; Linux (at least Ubuntu) doesn't
String tooltip = SystemVersion.isWindows() ? _t("I2P: Right-click for menu") : null; String tooltip = SystemVersion.isWindows() ? _t("I2P: Right-click for menu") : null;
@ -228,6 +231,8 @@ abstract class TrayManager {
* @return 0, or -1 on failure * @return 0, or -1 on failure
*/ */
public int displayMessage(int priority, String title, String message, String path) { public int displayMessage(int priority, String title, String message, String path) {
if (!_showNotifications)
return -1;
final TrayIcon ti = trayIcon; final TrayIcon ti = trayIcon;
if (ti == null) if (ti == null)
return -1; return -1;