From 182a9e079ec51fa4ad50cd67223b7a1d2a60950c Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Mon, 28 Jan 2019 13:32:24 +0100 Subject: [PATCH 1/3] Update active recording counter, when a MODEL_ONLINE event arrives --- client/src/main/java/ctbrec/ui/CamrecApplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/ctbrec/ui/CamrecApplication.java b/client/src/main/java/ctbrec/ui/CamrecApplication.java index 057915aa..e8cec189 100644 --- a/client/src/main/java/ctbrec/ui/CamrecApplication.java +++ b/client/src/main/java/ctbrec/ui/CamrecApplication.java @@ -248,7 +248,7 @@ public class CamrecApplication extends Application { EventBusHolder.BUS.register(new Object() { @Subscribe public void handleEvent(Event evt) { - if(evt.getType() == Event.Type.MODEL_STATUS_CHANGED || evt.getType() == Event.Type.RECORDING_STATUS_CHANGED) { + if(evt.getType() == Event.Type.MODEL_ONLINE || evt.getType() == Event.Type.MODEL_STATUS_CHANGED || evt.getType() == Event.Type.RECORDING_STATUS_CHANGED) { try { List models = recorder.getOnlineModels(); long count = models.stream().filter(m -> !recorder.isSuspended(m)).count(); From 626d13f87a1b8ad3ef997deb32c7fe6b93e3063c Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Mon, 28 Jan 2019 13:32:42 +0100 Subject: [PATCH 2/3] Implement createModelFromUrl --- .../java/ctbrec/sites/jasmin/LiveJasmin.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/src/main/java/ctbrec/sites/jasmin/LiveJasmin.java b/common/src/main/java/ctbrec/sites/jasmin/LiveJasmin.java index 9bdcb55f..9f77389e 100644 --- a/common/src/main/java/ctbrec/sites/jasmin/LiveJasmin.java +++ b/common/src/main/java/ctbrec/sites/jasmin/LiveJasmin.java @@ -178,4 +178,20 @@ public class LiveJasmin extends AbstractSite { private LiveJasminHttpClient getLiveJasminHttpClient() { return (LiveJasminHttpClient) httpClient; } + + @Override + public Model createModelFromUrl(String url) { + Matcher m = Pattern.compile("http.*?livejasmin\\.com.*?#!chat/(.*)").matcher(url); + if(m.find()) { + String name = m.group(1); + return createModel(name); + } + m = Pattern.compile("http.*?livejasmin\\.com.*?/chat-html5/(.*)").matcher(url); + if(m.find()) { + String name = m.group(1); + return createModel(name); + } + + return super.createModelFromUrl(url); + } } From 2fe6ec0e8fb803f9ee4074c7a4e4561ba53b6ee4 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Mon, 28 Jan 2019 17:26:53 +0100 Subject: [PATCH 3/3] Fix #156 Make sure, that the tray icon is created only once. Also move the notification methods from OS to DesktopIntegration. --- .../java/ctbrec/ui/DesktopIntegration.java | 72 +++++++++++++++++++ .../ctbrec/ui/event/ShowNotification.java | 4 +- .../ui/settings/ActionSettingsPanel.java | 4 +- common/src/main/java/ctbrec/OS.java | 66 ----------------- 4 files changed, 76 insertions(+), 70 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/DesktopIntegration.java b/client/src/main/java/ctbrec/ui/DesktopIntegration.java index 5601732a..26743502 100644 --- a/client/src/main/java/ctbrec/ui/DesktopIntegration.java +++ b/client/src/main/java/ctbrec/ui/DesktopIntegration.java @@ -1,6 +1,12 @@ package ctbrec.ui; +import java.awt.AWTException; import java.awt.Desktop; +import java.awt.Image; +import java.awt.SystemTray; +import java.awt.Toolkit; +import java.awt.TrayIcon; +import java.awt.TrayIcon.MessageType; import java.io.File; import java.io.IOException; import java.net.URI; @@ -8,6 +14,8 @@ import java.net.URI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ctbrec.OS; +import ctbrec.io.StreamRedirectThread; import javafx.geometry.Insets; import javafx.scene.control.Alert; import javafx.scene.control.Label; @@ -18,6 +26,9 @@ public class DesktopIntegration { private static final transient Logger LOG = LoggerFactory.getLogger(DesktopIntegration.class); + private static SystemTray tray; + private static TrayIcon trayIcon; + public static void open(String uri) { try { CamrecApplication.hostServices.showDocument(uri); @@ -95,4 +106,65 @@ public class DesktopIntegration { info.getDialogPane().setExpanded(true); info.show(); } + + public static void notification(String title, String header, String msg) { + if(OS.getOsType() == OS.TYPE.LINUX) { + notifyLinux(title, header, msg); + } else if(OS.getOsType() == OS.TYPE.WINDOWS) { + notifyWindows(title, header, msg); + } else if(OS.getOsType() == OS.TYPE.MAC) { + notifyMac(title, header, msg); + } else { + // unknown system, try systemtray notification anyways + notifySystemTray(title, header, msg); + } + } + + private static void notifyLinux(String title, String header, String msg) { + try { + Process p = Runtime.getRuntime().exec(new String[] { + "notify-send", + "-u", "normal", + "-t", "5000", + "-a", title, + header, + msg.replaceAll("-", "\\\\-").replaceAll("\\s", "\\\\ "), + "--icon=dialog-information" + }); + new Thread(new StreamRedirectThread(p.getInputStream(), System.out)).start(); + new Thread(new StreamRedirectThread(p.getErrorStream(), System.err)).start(); + } catch (IOException e1) { + LOG.error("Notification failed", e1); + } + } + + private static void notifyWindows(String title, String header, String msg) { + notifySystemTray(title, header, msg); + } + + private static void notifyMac(String title, String header, String msg) { + notifySystemTray(title, header, msg); + } + + private synchronized static void notifySystemTray(String title, String header, String msg) { + if(SystemTray.isSupported()) { + if(tray == null) { + LOG.debug("Creating tray icon"); + tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage(DesktopIntegration.class.getResource("/icon64.png")); + trayIcon = new TrayIcon(image, title); + trayIcon.setImageAutoSize(true); + trayIcon.setToolTip(title); + try { + tray.add(trayIcon); + } catch (AWTException e) { + LOG.error("Coulnd't add tray icon", e); + } + } + LOG.debug("Display tray message"); + trayIcon.displayMessage(header, msg, MessageType.INFO); + } else { + LOG.error("SystemTray notifications not supported by this OS"); + } + } } diff --git a/client/src/main/java/ctbrec/ui/event/ShowNotification.java b/client/src/main/java/ctbrec/ui/event/ShowNotification.java index 4d91350d..58c371ae 100644 --- a/client/src/main/java/ctbrec/ui/event/ShowNotification.java +++ b/client/src/main/java/ctbrec/ui/event/ShowNotification.java @@ -1,13 +1,13 @@ package ctbrec.ui.event; import ctbrec.Model; -import ctbrec.OS; import ctbrec.event.Action; import ctbrec.event.Event; import ctbrec.event.EventHandlerConfiguration.ActionConfiguration; import ctbrec.event.ModelStateChangedEvent; import ctbrec.event.RecordingStateChangedEvent; import ctbrec.ui.CamrecApplication; +import ctbrec.ui.DesktopIntegration; public class ShowNotification extends Action { @@ -33,7 +33,7 @@ public class ShowNotification extends Action { default: msg = evt.getDescription(); } - OS.notification(CamrecApplication.title, header, msg); + DesktopIntegration.notification(CamrecApplication.title, header, msg); } @Override diff --git a/client/src/main/java/ctbrec/ui/settings/ActionSettingsPanel.java b/client/src/main/java/ctbrec/ui/settings/ActionSettingsPanel.java index 182ca2d5..012e6515 100644 --- a/client/src/main/java/ctbrec/ui/settings/ActionSettingsPanel.java +++ b/client/src/main/java/ctbrec/ui/settings/ActionSettingsPanel.java @@ -14,7 +14,6 @@ import org.slf4j.LoggerFactory; import ctbrec.Config; import ctbrec.Model; -import ctbrec.OS; import ctbrec.Recording; import ctbrec.StringUtil; import ctbrec.event.Event; @@ -29,6 +28,7 @@ import ctbrec.event.ModelStatePredicate; import ctbrec.event.RecordingStatePredicate; import ctbrec.recorder.Recorder; import ctbrec.ui.CamrecApplication; +import ctbrec.ui.DesktopIntegration; import ctbrec.ui.controls.FileSelectionBox; import ctbrec.ui.controls.ProgramSelectionBox; import ctbrec.ui.controls.Wizard; @@ -266,7 +266,7 @@ public class ActionSettingsPanel extends TitledPane { testNotification.setOnAction(evt -> { DateTimeFormatter format = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM); ZonedDateTime time = ZonedDateTime.now(); - OS.notification(CamrecApplication.title, "Test Notification", "Oi, what's up! " + format.format(time)); + DesktopIntegration.notification(CamrecApplication.title, "Test Notification", "Oi, what's up! " + format.format(time)); }); testNotification.disableProperty().bind(showNotification.selectedProperty().not()); diff --git a/common/src/main/java/ctbrec/OS.java b/common/src/main/java/ctbrec/OS.java index 268e8cbf..4214d4d0 100644 --- a/common/src/main/java/ctbrec/OS.java +++ b/common/src/main/java/ctbrec/OS.java @@ -1,13 +1,6 @@ package ctbrec; -import java.awt.AWTException; -import java.awt.Image; -import java.awt.SystemTray; -import java.awt.Toolkit; -import java.awt.TrayIcon; -import java.awt.TrayIcon.MessageType; import java.io.File; -import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; @@ -18,8 +11,6 @@ import java.util.Map.Entry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import ctbrec.io.StreamRedirectThread; - public class OS { private static final transient Logger LOG = LoggerFactory.getLogger(OS.class); @@ -129,61 +120,4 @@ public class OS { } return env; } - - public static void notification(String title, String header, String msg) { - if(OS.getOsType() == OS.TYPE.LINUX) { - notifyLinux(title, header, msg); - } else if(OS.getOsType() == OS.TYPE.WINDOWS) { - notifyWindows(title, header, msg); - } else if(OS.getOsType() == OS.TYPE.MAC) { - notifyMac(title, header, msg); - } else { - // unknown system, try systemtray notification anyways - notifySystemTray(title, header, msg); - } - } - - private static void notifyLinux(String title, String header, String msg) { - try { - Process p = Runtime.getRuntime().exec(new String[] { - "notify-send", - "-u", "normal", - "-t", "5000", - "-a", title, - header, - msg.replaceAll("-", "\\\\-").replaceAll("\\s", "\\\\ "), - "--icon=dialog-information" - }); - new Thread(new StreamRedirectThread(p.getInputStream(), System.out)).start(); - new Thread(new StreamRedirectThread(p.getErrorStream(), System.err)).start(); - } catch (IOException e1) { - LOG.error("Notification failed", e1); - } - } - - private static void notifyWindows(String title, String header, String msg) { - notifySystemTray(title, header, msg); - } - - private static void notifyMac(String title, String header, String msg) { - notifySystemTray(title, header, msg); - } - - private static void notifySystemTray(String title, String header, String msg) { - if(SystemTray.isSupported()) { - SystemTray tray = SystemTray.getSystemTray(); - Image image = Toolkit.getDefaultToolkit().createImage(OS.class.getResource("/icon64.png")); - TrayIcon trayIcon = new TrayIcon(image, title); - trayIcon.setImageAutoSize(true); - trayIcon.setToolTip(title); - try { - tray.add(trayIcon); - } catch (AWTException e) { - LOG.error("Coulnd't add tray icon", e); - } - trayIcon.displayMessage(header, msg, MessageType.INFO); - } else { - LOG.error("SystemTray notifications not supported by this OS"); - } - } }