From cd315a55d7a8cf087105f329c235076265408426 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Tue, 29 Sep 2020 17:00:16 +0100 Subject: [PATCH] tweak for review --- src/do-not-disturb.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/do-not-disturb.js b/src/do-not-disturb.js index 37af2ba6..f9af67d6 100644 --- a/src/do-not-disturb.js +++ b/src/do-not-disturb.js @@ -1,29 +1,35 @@ - // Always false if the platform doesn't support it. -global.isDoNotDisturb = false; - const LINUX_SETTING_COMMAND = "gsettings get org.gnome.desktop.notifications show-banners"; +const { promisify } = require('util'); +const childProcess = require('child_process'); +const exec = promisify(childProcess.exec); + +let doNotDisturbMode = false; + function init() { if (process.platform === "linux") { return initForLinux(); } } -async function initForLinux() { - // TODO: This is specific to the GNOME desktop implementation of DND +function isDoNotDisturb() { + return doNotDisturbMode; +} +async function initForLinux() { const DBus = require('dbus-next'); - const { execSync } = require('child_process'); + // This is specific to the GNOME implementation for do-not-distrub + // First we need to determine the value of dnd. try { // XXX: After much flailing about, I couldn't find another acceptable way to fetch this setting value. - const value = execSync(LINUX_SETTING_COMMAND, { encoding: "utf-8" }).trim(); + const value = (await exec(LINUX_SETTING_COMMAND, { encoding: "utf-8" })).trim(); if (value) { // Anything other than true will be safely false. // Invert because show-banners === do-disturb :) - global.isDoNotDisturb = value !== 'true'; - console.log(`do-not-disturb value has been detected as: ${global.isDoNotDisturb}`); + doNotDisturbMode = value !== 'true'; + console.log(`do-not-disturb value has been detected as: ${doNotDisturbMode}`); } } catch (ex) { console.warn("Could not execute gsettings command to determine do-not-disturb value. Functionality disabled"); @@ -33,16 +39,14 @@ async function initForLinux() { const session = DBus.sessionBus(); - process.on("exit", () => { - session.disconnect(); - }); + process.on("exit", () => session.disconnect()); const obj = await session.getProxyObject('ca.desrt.dconf', '/ca/desrt/dconf/Writer/user'); const signaller = obj.getInterface('ca.desrt.dconf.Writer'); signaller.on('Notify', (settingName) => { if (settingName == '/org/gnome/desktop/notifications/show-banners') { // The D-BUS signal will only tell you that the setting changed, but annoyingly // not the value. Since we got the value on startup, assume it's just been toggled. - global.isDoNotDisturb = !global.isDoNotDisturb; + doNotDisturbMode = !doNotDisturbMode; console.log(`do-not-disturb value has been detected as: ${global.isDoNotDisturb}`); } }); @@ -50,4 +54,5 @@ async function initForLinux() { module.exports = { init, + isDoNotDisturb, };