This commit is contained in:
Will Hunt 2020-08-10 18:44:39 +01:00
parent 688ebb7171
commit dcd6e3f8b9

@ -2,6 +2,8 @@
// Always false if the platform doesn't support it. // Always false if the platform doesn't support it.
global.isDoNotDisturb = false; global.isDoNotDisturb = false;
const LINUX_SETTING_COMMAND = "gsettings get org.gnome.desktop.notifications show-banners";
function init() { function init() {
if (process.platform === "linux") { if (process.platform === "linux") {
return initForLinux(); return initForLinux();
@ -12,15 +14,15 @@ async function initForLinux() {
// TODO: This is specific to the GNOME desktop implementation of DND // TODO: This is specific to the GNOME desktop implementation of DND
const DBus = require('dbus-next'); const DBus = require('dbus-next');
const child_process = require('child_process'); const { execSync } = require('child_process');
// First we need to determine the value of dnd. // First we need to determine the value of dnd.
try { try {
// XXX: After much flailing about, I couldn't find another acceptable way to fetch this setting value. // XXX: After much flailing about, I couldn't find another acceptable way to fetch this setting value.
const value = child_process.execSync("gsettings get org.gnome.desktop.notifications show-banners", { encoding: "utf-8"}).trim(); const value = execSync(LINUX_SETTING_COMMAND, { encoding: "utf-8" }).trim();
if (value) { if (value) {
// Anything other than true will be safely false. // Anything other than true will be safely false.
// Invert because show-banners === do-disturb :) // Invert because show-banners === do-disturb :)
global.isDoNotDisturb = !Boolean(value === 'true'); global.isDoNotDisturb = value !== 'true';
console.log(`do-not-disturb value has been detected as: ${global.isDoNotDisturb}`); console.log(`do-not-disturb value has been detected as: ${global.isDoNotDisturb}`);
} }
} catch (ex) { } catch (ex) {
@ -34,8 +36,8 @@ async function initForLinux() {
process.on("exit", () => { process.on("exit", () => {
session.disconnect(); session.disconnect();
}); });
let obj = await session.getProxyObject('ca.desrt.dconf', '/ca/desrt/dconf/Writer/user'); const obj = await session.getProxyObject('ca.desrt.dconf', '/ca/desrt/dconf/Writer/user');
let signaller = obj.getInterface('ca.desrt.dconf.Writer'); const signaller = obj.getInterface('ca.desrt.dconf.Writer');
signaller.on('Notify', (settingName) => { signaller.on('Notify', (settingName) => {
if (settingName == '/org/gnome/desktop/notifications/show-banners') { if (settingName == '/org/gnome/desktop/notifications/show-banners') {
// The D-BUS signal will only tell you that the setting changed, but annoyingly // The D-BUS signal will only tell you that the setting changed, but annoyingly
@ -48,4 +50,4 @@ async function initForLinux() {
module.exports = { module.exports = {
init, init,
} };