diff options
Diffstat (limited to 'src/lib/DBus.js')
-rw-r--r-- | src/lib/DBus.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/lib/DBus.js b/src/lib/DBus.js new file mode 100644 index 000000000..957912efa --- /dev/null +++ b/src/lib/DBus.js | |||
@@ -0,0 +1,49 @@ | |||
1 | import { | ||
2 | sessionBus, | ||
3 | } from 'dbus-next'; | ||
4 | import { | ||
5 | isLinux, | ||
6 | } from '../environment'; | ||
7 | |||
8 | export default class DBus { | ||
9 | bus = null; | ||
10 | |||
11 | constructor(trayIcon) { | ||
12 | this.trayIcon = trayIcon; | ||
13 | } | ||
14 | |||
15 | start() { | ||
16 | if (!isLinux || this.bus) return; | ||
17 | |||
18 | try { | ||
19 | this.bus = sessionBus(); | ||
20 | } catch { | ||
21 | // Error connecting to the bus. | ||
22 | return; | ||
23 | } | ||
24 | |||
25 | // HACK Hook onto the MessageBus to track StatusNotifierWatchers | ||
26 | this.bus._addMatch("type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',path='/org/freedesktop/DBus',member='NameOwnerChanged'"); | ||
27 | const mangled = JSON.stringify({ | ||
28 | path: '/org/freedesktop/DBus', | ||
29 | interface: 'org.freedesktop.DBus', | ||
30 | member: 'NameOwnerChanged', | ||
31 | }); | ||
32 | this.bus._signals.on(mangled, (msg) => { | ||
33 | const [name, oldOwner, newOwner] = msg.body; | ||
34 | if (name === 'org.kde.StatusNotifierWatcher' && oldOwner !== newOwner && newOwner !== '') { | ||
35 | // Leave ample time for the StatusNotifierWatcher to be initialized | ||
36 | setTimeout(() => { | ||
37 | this.trayIcon.recreateIfVisible(); | ||
38 | }, 400); | ||
39 | } | ||
40 | }); | ||
41 | } | ||
42 | |||
43 | stop() { | ||
44 | if (!this.bus) return; | ||
45 | |||
46 | this.bus.disconnect(); | ||
47 | this.bus = null; | ||
48 | } | ||
49 | } | ||