Notifier improvements and fixes

* Correct an issue where if libnotify-bin is missing then runelite
doesn't do anything
* Add an icon to the Linux notifications using libnotify-bin if it's
present
This commit is contained in:
David Kosub
2018-02-27 20:25:48 -06:00
committed by Adam
parent d9679842d3
commit 865acfb2e8

View File

@@ -30,14 +30,19 @@ import com.google.inject.Inject;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.TrayIcon; import java.awt.TrayIcon;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import javax.inject.Provider; import javax.inject.Provider;
import javax.inject.Singleton; import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.client.config.RuneLiteConfig; import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.util.OSType;
import net.runelite.client.ui.ClientUI; import net.runelite.client.ui.ClientUI;
import net.runelite.client.util.OSType;
@Singleton @Singleton
@Slf4j @Slf4j
@@ -53,14 +58,23 @@ public class Notifier
private final String appName; private final String appName;
private final RuneLiteConfig runeLiteConfig; private final RuneLiteConfig runeLiteConfig;
private final Provider<ClientUI> clientUI; private final Provider<ClientUI> clientUI;
private final ScheduledExecutorService executorService;
private final Path notifyIconPath;
@Inject @Inject
private Notifier(final Provider<ClientUI> clientUI, final RuneLiteConfig runeliteConfig, final RuneLiteProperties private Notifier(
runeLiteProperties) final Provider<ClientUI> clientUI,
final RuneLiteConfig runeliteConfig,
final RuneLiteProperties runeLiteProperties,
final ScheduledExecutorService executorService)
{ {
this.appName = runeLiteProperties.getTitle(); this.appName = runeLiteProperties.getTitle();
this.clientUI = clientUI; this.clientUI = clientUI;
this.runeLiteConfig = runeliteConfig; this.runeLiteConfig = runeliteConfig;
this.executorService = executorService;
this.notifyIconPath = RuneLite.RUNELITE_DIR.toPath().resolve("icon.png");
storeIcon();
} }
public void notify(String message) public void notify(String message)
@@ -150,11 +164,24 @@ public class Notifier
commands.add("notify-send"); commands.add("notify-send");
commands.add(title); commands.add(title);
commands.add(message); commands.add(message);
commands.add("-i");
commands.add(SHELL_ESCAPE.escape(notifyIconPath.toAbsolutePath().toString()));
commands.add("-u"); commands.add("-u");
commands.add(toUrgency(type)); commands.add(toUrgency(type));
commands.add("-t"); commands.add("-t");
commands.add(String.valueOf(DEFAULT_TIMEOUT)); commands.add(String.valueOf(DEFAULT_TIMEOUT));
sendCommand(commands);
executorService.submit(() ->
{
final boolean success = sendCommand(commands)
.map(process -> process.exitValue() == 0)
.orElse(false);
if (!success)
{
sendTrayNotification(title, message, type);
}
});
} }
private void sendMacNotification( private void sendMacNotification(
@@ -189,18 +216,35 @@ public class Notifier
sendCommand(commands); sendCommand(commands);
} }
private void sendCommand(final List<String> commands) private Optional<Process> sendCommand(final List<String> commands)
{ {
try try
{ {
new ProcessBuilder(commands.toArray(new String[commands.size()])) return Optional.of(new ProcessBuilder(commands.toArray(new String[commands.size()]))
.redirectErrorStream(true) .redirectErrorStream(true)
.start(); .start());
} }
catch (IOException ex) catch (IOException ex)
{ {
log.warn(null, ex); log.warn(null, ex);
} }
return Optional.empty();
}
private void storeIcon()
{
if (OSType.getOSType() == OSType.Linux && !Files.exists(notifyIconPath))
{
try (InputStream stream = Notifier.class.getResourceAsStream("/runelite.png"))
{
Files.copy(stream, notifyIconPath);
}
catch (IOException ex)
{
log.warn(null, ex);
}
}
} }
private static String toUrgency(TrayIcon.MessageType type) private static String toUrgency(TrayIcon.MessageType type)