Skip to content

Commit

Permalink
Add BurntToast notifier
Browse files Browse the repository at this point in the history
https://github.com/Windos/BurntToast

Use:
    notifier.implementation = burnttoast
    notifier.burnttoast.sound = A sound ID from available sound list below (default: none)

Available sounds: Default;IM;Mail;Reminder;SMS;Alarm;Alarm2;Alarm3;Alarm4;Alarm5;
Alarm6;Alarm7;Alarm8;Alarm9;Alarm10;Call;Call2;Call3;Call4;Call5;Call6;Call7;Call8;Call9;Call10
  • Loading branch information
jcgay committed May 7, 2017
1 parent f6c190d commit 00af537
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Go to [Wiki](https://github.com/jcgay/send-notification/wiki) to read full confi
| **AnyBar** for [OS X](https://github.com/tonsky/AnyBar) and [Linux](https://github.com/limpbrains/somebar) | ![anybar](http://jeanchristophegay.com/images/notifier.anybar_maven.png) |
| **[Toaster](https://github.com/nels-o/toaster)** for Windows 8 | ![toaster](http://jeanchristophegay.com/images/notifier.toaster.png) |
| **[Notify](https://github.com/dorkbox/Notify)** since Java 6 | ![Notify](http://jeanchristophegay.com/images/notifier.notify.png) |
| **[BurntToast](https://github.com/Windos/BurntToast)** for Windows 10 | ![BurntToast](http://jeanchristophegay.com/images/notifier.burnttoast.png) |

# Build status

Expand Down
5 changes: 5 additions & 0 deletions send-notification/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>Notify</artifactId>
<version>2.20</version>
</dependency>
<dependency>
<groupId>com.profesorfalken</groupId>
<artifactId>jPowerShell</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import fr.jcgay.notification.notifier.additional.AdditionalNotifier;
import fr.jcgay.notification.notifier.anybar.AnyBarConfiguration;
import fr.jcgay.notification.notifier.anybar.AnyBarNotifier;
import fr.jcgay.notification.notifier.burnttoast.BurntToastNotifier;
import fr.jcgay.notification.notifier.burnttoast.BurntToastNotifierConfiguration;
import fr.jcgay.notification.notifier.executor.RuntimeExecutor;
import fr.jcgay.notification.notifier.growl.GrowlConfiguration;
import fr.jcgay.notification.notifier.growl.GrowlNotifier;
Expand Down Expand Up @@ -50,6 +52,7 @@ class NotifierProvider {
private static final ChosenNotifiers SIMPLE_NOTIFICATION_CENTER = ChosenNotifiers.from("simplenc");
private static final ChosenNotifiers TOASTER = ChosenNotifiers.from("toaster");
private static final ChosenNotifiers NOTIFY = ChosenNotifiers.from("notify");
private static final ChosenNotifiers BURNT_TOAST = ChosenNotifiers.from("burnttoast");

private final RuntimeExecutor executor = new RuntimeExecutor();
private final OperatingSystem os;
Expand Down Expand Up @@ -108,6 +111,9 @@ public DiscoverableNotifier byName(ChosenNotifiers notifier, Properties properti
if (NOTIFY.equals(notifier)) {
return new NotifyNotifier(application, NotifyConfiguration.create(properties));
}
if (BURNT_TOAST.equals(notifier)) {
return new BurntToastNotifier(application, BurntToastNotifierConfiguration.create(properties));
}

return DoNothingNotifier.doNothing();
}
Expand All @@ -126,6 +132,7 @@ public Set<DiscoverableNotifier> available(Properties configuration, Application
winNotifiers.add(byName(SNARL, configuration, application));
winNotifiers.add(new GrowlNotifier(application, GrowlConfiguration.create(configuration), DEBUG));
winNotifiers.add(byName(TOASTER, configuration, application));
winNotifiers.add(byName(BURNT_TOAST, configuration, application));
winNotifiers.add(byName(SYSTEM_TRAY, configuration, application));
return unmodifiableSet(winNotifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.jcgay.notification.notifier.burnttoast;

import fr.jcgay.notification.UrlSendNotificationException;

public class BurntToastException extends UrlSendNotificationException {

public BurntToastException(String message) {
super(message);
}

@Override
public String getURL() {
return "https://github.com/jcgay/send-notification/wiki/burnttoast";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package fr.jcgay.notification.notifier.burnttoast;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.profesorfalken.jpowershell.PowerShell;
import com.profesorfalken.jpowershell.PowerShellResponse;
import fr.jcgay.notification.Application;
import fr.jcgay.notification.DiscoverableNotifier;
import fr.jcgay.notification.Notification;
import fr.jcgay.notification.Notifier;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

public class BurntToastNotifier implements DiscoverableNotifier {

private static final Logger LOGGER = getLogger(BurntToastNotifier.class);

private final Application application;
private final BurntToastNotifierConfiguration configuration;

public BurntToastNotifier(Application application, BurntToastNotifierConfiguration configuration) {
this.application = application;
this.configuration = configuration;
}

@Override
public void send(Notification notification) {
StringBuilder command = new StringBuilder()
.append("New-BurntToastNotification -Text '")
.append(notification.title())
.append("', '")
.append(notification.message())
.append("' -AppLogo ")
.append(notification.icon().asPath())
.append(" -AppId '")
.append(application.id())
.append("'");

if (configuration.sound() == null) {
command.append(" -Silent");
} else {
command.append(" -Sound ")
.append(configuration.sound());
}

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Will execute PowerShell: {}", command.toString());
}

PowerShellResponse response = PowerShell.executeSingleCommand(command.toString());
LOGGER.debug("Response: {}", response.getCommandOutput());
if (response.isError()) {
throw new BurntToastException(response.getCommandOutput());
}
}

@Override
public void close() {
// do nothing
}

@Override
public boolean isPersistent() {
return false;
}

@Override
public Notifier init() {
return this;
}

@Override
public boolean tryInit() {
PowerShellResponse response = PowerShell.executeSingleCommand("Get-Module -ListAvailable | Format-Table Name");
return !response.isError() && response.getCommandOutput().toLowerCase().contains("burnttoast");
}

@Override
public int hashCode() {
return Objects.hashCode(application, configuration);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final BurntToastNotifier other = (BurntToastNotifier) obj;
return Objects.equal(this.application, other.application)
&& Objects.equal(this.configuration, other.configuration);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("application", application)
.add("configuration", configuration)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.jcgay.notification.notifier.burnttoast;

import com.google.auto.value.AutoValue;

import javax.annotation.Nullable;
import java.util.Properties;

@AutoValue
public abstract class BurntToastNotifierConfiguration {

private static final BurntToastNotifierConfiguration DEFAULT = new AutoValue_BurntToastNotifierConfiguration(null);

BurntToastNotifierConfiguration() {
// prevent external subclasses
}

public static BurntToastNotifierConfiguration byDefault() {
return DEFAULT;
}

public static BurntToastNotifierConfiguration create(Properties properties) {
if (properties == null) {
return byDefault();
}

return new AutoValue_BurntToastNotifierConfiguration(properties.getProperty("notifier.burnttoast.sound"));
}

@Nullable
public abstract String sound();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package fr.jcgay.notification

import fr.jcgay.notification.configuration.ChosenNotifiers
import fr.jcgay.notification.configuration.OperatingSystem
import fr.jcgay.notification.notifier.burnttoast.BurntToastNotifier
import fr.jcgay.notification.notifier.burnttoast.BurntToastNotifierConfiguration
import fr.jcgay.notification.notifier.executor.RuntimeExecutor
import fr.jcgay.notification.notifier.growl.GrowlConfiguration
import fr.jcgay.notification.notifier.growl.GrowlNotifier
Expand Down Expand Up @@ -53,6 +55,7 @@ class NotifierProviderSpec extends Specification {
new SnarlNotifier(application, SnarlConfiguration.byDefault()),
new GrowlNotifier(application, GrowlConfiguration.byDefault(), DEBUG),
new ToasterNotifier(ToasterConfiguration.byDefault(), new RuntimeExecutor()),
new BurntToastNotifier(application, BurntToastNotifierConfiguration.byDefault()),
new SystemTrayNotifier(application)
)
}
Expand Down

0 comments on commit 00af537

Please sign in to comment.