-
-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathdndManager.js
More file actions
189 lines (177 loc) · 6.05 KB
/
dndManager.js
File metadata and controls
189 lines (177 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import EventEmitter from 'events'
import log from 'electron-log/main.js'
import { getFocusAssist } from 'windows-focus-assist'
import dbus from '@particle/dbus-next'
import { exec } from 'node:child_process'
import { promisify } from 'node:util'
import { readFile } from 'node:fs/promises'
class DndManager extends EventEmitter {
constructor (settings) {
super()
this.settings = settings
this.monitorDnd = settings.get('monitorDnd')
this.timer = null
this.isOnDnd = false
this._unsupDEErrorShown = false
this._errorLogged = {}
if (this.monitorDnd) {
this.start()
}
}
start () {
this.monitorDnd = true
this._checkDnd()
log.info('Stretchly: starting Do Not Disturb monitoring')
if (process.platform === 'linux') {
log.info(`System: Your Desktop seems to be ${this._desktopEnviroment}.`)
}
}
stop () {
this.monitorDnd = false
this.isOnDnd = false
clearTimeout(this.timer)
this.timer = null
log.info('Stretchly: stopping Do Not Disturb monitoring')
}
get _desktopEnviroment () {
// https://github.com/electron/electron/issues/40795
// https://specifications.freedesktop.org/mime-apps-spec/latest/file.html
// https://specifications.freedesktop.org/menu-spec/latest/onlyshowin-registry.html
return process.env.ORIGINAL_XDG_CURRENT_DESKTOP ||
process.env.XDG_CURRENT_DESKTOP || 'unknown'
}
async _isDndEnabledLinux () {
const de = this._desktopEnviroment.toLowerCase()
const sessionBus = dbus.sessionBus()
switch (true) {
case de.includes('kde'):
try {
const obj = await sessionBus.getProxyObject('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
const properties = obj.getInterface('org.freedesktop.DBus.Properties')
const dndEnabled = await properties.Get('org.freedesktop.Notifications', 'Inhibited')
if (await dndEnabled.value) {
return true
}
} catch (e) {
this._logErrorOnce('kde', e)
}
break
case de.includes('xfce'):
try {
const obj = await sessionBus.getProxyObject('org.xfce.Xfconf', '/org/xfce/Xfconf')
const properties = obj.getInterface('org.xfce.Xfconf')
const dndEnabled = await properties.GetProperty('xfce4-notifyd', '/do-not-disturb')
if (await dndEnabled.value) {
return true
}
} catch (e) {
this._logErrorOnce('xfce', e)
}
break
case de.includes('gnome') || de.includes('unity'):
try {
const asyncExec = promisify(exec)
const { stdout } = await asyncExec('gsettings get org.gnome.desktop.notifications show-banners')
if (stdout.replace(/[^0-9a-zA-Z]/g, '') === 'false') {
return true
}
} catch (e) {
this._logErrorOnce('gnome/unity', e)
}
break
case de.includes('cinnamon'):
try {
const asyncExec = promisify(exec)
const { stdout } = await asyncExec('gsettings get org.cinnamon.desktop.notifications display-notifications')
if (stdout.replace(/[^0-9a-zA-Z]/g, '') === 'false') {
return true
}
} catch (e) {
this._logErrorOnce('cinnamon', e)
}
break
case de.includes('mate'):
try {
const asyncExec = promisify(exec)
const { stdout } = await asyncExec('gsettings get org.mate.NotificationDaemon do-not-disturb')
if (stdout.replace(/[^0-9a-zA-Z]/g, '') === 'true') {
return true
}
} catch (e) {
this._logErrorOnce('mate', e)
}
break
case de.includes('lxqt'):
return await this._getConfigValue('~/.config/lxqt/notifications.conf', 'doNotDisturb')
default:
if (!this._unsupDEErrorShown) {
log.info(`Stretchly: ${this._desktopEnviroment} not supported for DND detection, yet.`)
this._unsupDEErrorShown = true
}
return false
}
}
async _doNotDisturb () {
// TODO also check for session state? https://github.com/felixrieseberg/electron-notification-state/tree/master#session-state
if (this.monitorDnd) {
if (process.platform === 'win32') {
let wfa = 0
try {
wfa = getFocusAssist().value
} catch (e) { wfa = -1 } // getFocusAssist() throw an error if OS isn't windows
return wfa !== -1 && wfa !== 0
} else if (process.platform === 'darwin') {
try {
const asyncExec = promisify(exec)
const { stdout } = await asyncExec('defaults read com.apple.controlcenter "NSStatusItem Visible FocusModes"')
if (stdout.replace(/[^0-9a-zA-Z]/g, '') === '1') {
return true
}
} catch (e) {
this._logErrorOnce('macos', e)
}
} else if (process.platform === 'linux') {
return await this._isDndEnabledLinux()
}
} else {
return false
}
}
async _getConfigValue (filePath, key) {
try {
const data = await readFile(filePath, 'utf8')
const lines = data.split('\n')
for (const line of lines) {
const [configKey, value] = line.split('=')
if (configKey.trim() === key) {
return value.trim().toLowerCase() === 'true'
}
}
return false
} catch (e) {
this._logErrorOnce(`config-read-${filePath}`, e)
return false
}
}
_logErrorOnce (environment, error) {
const errorKey = `${environment}-${error.code || error.message.substring(0, 20)}`
if (!this._errorLogged[errorKey]) {
log.error(`Stretchly: DND detection error in ${environment}:`, error)
this._errorLogged[errorKey] = true
}
}
_checkDnd () {
this.timer = setInterval(async () => {
const doNotDisturb = await this._doNotDisturb()
if (!this.isOnDnd && doNotDisturb) {
this.isOnDnd = true
this.emit('dndStarted')
}
if (this.isOnDnd && !doNotDisturb) {
this.isOnDnd = false
this.emit('dndFinished')
}
}, 1000)
}
}
export default DndManager