7

如果我有总线名称、对象路径和接口,我如何从 Gjs(在 gnome-shell 扩展中)调用 DBus 方法?

我正在寻找以下 python 代码的等价物:

import dbus
bus = dbus.SessionBus()
obj = bus.get_object("org.gnome.Caribou.Keyboard", "/org/gnome/SessionManager/EndSessionDialog")
obj.Open(0, 0, 120, dbus.Array(signature="o"))

(请注意,由于某些 python-dbus 魔术,我没有显式使用该接口,但我可以使用iface = dbus.interface(obj, "org.gnome.SessionManager.EndSessionDialog")。由于我有接口名称,我可以使用查询它的解决方案。另请注意,此示例将是在 Gjs 中很傻,因为它回调到 gnome-shell)

4

2 回答 2

10

The import imports.dbus is deprecated since gnome-shell 3.4. The new way is to use Gio as described here:

const Gio = imports.gi.Gio;

const MyIface = '<interface name="org.example.MyInterface">\
<method name="Activate" />\
</interface>';
const MyProxy = Gio.DBusProxy.makeProxyWrapper(MyIface);

let instance = new MyProxy(Gio.DBus.session, 'org.example.BusName',
'/org/example/Path');

(Note that the original post uses makeProxyClass, correct is makeProxyWrapper.)

You can get the interface definition, for example, by using introspection. For pidgin/purple do:

$ dbus-send --print-reply --dest=im.pidgin.purple.PurpleService \
/im/pidgin/purple/PurpleObject org.freedesktop.DBus.Introspectable.Introspect

Further explanations on introspection and inspection of interfaces can be found here.

于 2013-05-27T20:39:42.810 回答
1

这应该给你一个更好的主意:

gjs> const DBus = imports.dbus;
gjs> for (let i in DBus) log(i);
于 2012-10-15T13:37:06.380 回答