I have learned that Gio.DBus is the right module to use, but I failed to make it run correctly. To demonstrate what I mean, I prepared the following "incorrect" code, which attempts to call the ListNames method in org.freedesktop.DBus interface. I didn't see any output when I run this incorrect code.
Incorrect code:
const Gio = imports.gi.Gio;
const DBusDaemonIface = <interface name='org.freedesktop.DBus'>
<method name='ListNames'>
<arg type='as' direction='out'/>
</method>
</interface>;
const DBusDaemonProxy = Gio.DBusProxy.makeProxyWrapper(DBusDaemonIface);
let main = function () {
var gdbusProxy = new DBusDaemonProxy(Gio.DBus.session, 'org.freedesktop.DBus', '/org/freedesktop/DBus');
gdbusProxy.ListNamesRemote(function(result, error){ print(result); });
};
main();
For comparison, the following code works. The difference I made is to define a TestApp class which extends Gio.Application, which gets instantiated in the main() function.
Correct code:
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const DBusDaemonIface = <interface name='org.freedesktop.DBus'>
<method name='ListNames'>
<arg type='as' direction='out'/>
</method>
</interface>;
const DBusDaemonProxy = Gio.DBusProxy.makeProxyWrapper(DBusDaemonIface);
TestApp = new Lang.Class({
Name: 'TestApp',
Extends: Gio.Application,
_init: function() {
this.parent({application_id: 'testapp_id',
flags: Gio.ApplicationFlags.NON_UNIQUE });
this._gdbusProxy = new DBusDaemonProxy(Gio.DBus.session,
'org.freedesktop.DBus', '/org/freedesktop/DBus');
this._gdbusProxy.ListNamesRemote(Lang.bind(this, this._listNames));
},
_listNames: function(result, error) {
print(result);
},
vfunc_activate: function() {
this.hold();
},
});
let main = function () {
let app = new TestApp();
return app.run(ARGV);
};
main();
So my guess is to make GDBus work, you need a Gio.Application to run?
This could be a very stupid question, because I have zero experience
programming for GNOME.
Thanks,
K.C.