Returning struct with Qt DBus?

Florian Bruhin me at the-compiler.org
Wed Mar 24 17:31:19 GMT 2021


Hey,

I'm currently trying to write a simple DBus notification server for end
to end tests.

If I run the attached server (after killing my usual notification
daemon), invoking e.g. GetCapabilities (which returns a list of strings)
works fine:

    $ dbus-send --session --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.GetCapabilities
    method return time=1616606628.700096 sender=:1.451 -> destination=:1.457 serial=16 reply_serial=2
       array [
          string "actions"
       ]

However, I can't figure out how to get GetServerInformation to work,
which is supposed to return a struct of four strings ("ssss" as DBus
signature). I've tried returning a QDBusArgument and using:

    @pyqtSlot(QDBusMessage, result="QDBusArgument")
    def GetServerInformation(self, message):
        arg = QDBusArgument()
        arg.beginStructure()
        arg.add("qutebrowser")
        arg.add("notification test server")
        arg.add("v13.37")
        arg.add("1.2")
        arg.endStructure()
        return arg

But that doesn't seem to work:

    $ dbus-send --session --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.GetServerInformation
    Error org.freedesktop.DBus.Error.UnknownMethod: No such method 'GetServerInformation' in interface 'org.freedesktop.Notifications' at object path '/org/freedesktop/Notifications' (signature '')

If possible, I'd prefer using Qt DBus only rather than the python-dbus
integration.

Any idea how I can get this to work? Or is this just not possible
without being able to register Qt metatypes or something?

Thanks,
Florian

-- 
            me at the-compiler.org | https://www.qutebrowser.org 
       https://bruhin.software/ | https://github.com/sponsors/The-Compiler/
       GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
             I love long mails! | https://email.is-not-s.ms/
-------------- next part --------------
from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtDBus import QDBusConnection, QDBusArgument, QDBusMessage
from PyQt5.QtWidgets import QApplication


class NotificationServer(QObject):

    def __init__(self):
        super().__init__()
        self._service = 'org.freedesktop.Notifications'
        self._bus = QDBusConnection.sessionBus()

    def register(self):
        assert self._bus.isConnected()
        assert self._bus.registerService(self._service)
        assert self._bus.registerObject(
            '/org/freedesktop/Notifications',  # path
            self._service,  # interface
            self,
            QDBusConnection.ExportAllSlots,
        )

    @pyqtSlot(QDBusMessage, result="uint")
    def Notify(self, dbus_message):
        print(f"Notify: {dbus_message.arguments()}")
        return 1  # notification id

    @pyqtSlot(QDBusMessage, result="QStringList")
    def GetCapabilities(self, message):
        return ["actions"]

    #@pyqtSlot(QDBusMessage, result="struct")
    @pyqtSlot(QDBusMessage, result="QDBusArgument")
    def GetServerInformation(self, message):
        arg = QDBusArgument()
        arg.beginStructure()
        arg.add("qutebrowser")
        arg.add("notification test server")
        arg.add("v13.37")
        arg.add("1.2")
        arg.endStructure()
        return arg

        # return (
        #     "qutebrowser",
        #     "notification test server",
        #     "v13.37",
        #     "1.2",
        # )


if __name__ == '__main__':
    app = QApplication([])
    server = NotificationServer()
    server.register()
    app.exec_()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210324/ef953b44/attachment.sig>


More information about the PyQt mailing list