How to use `org.freedesktop.portal.GlobalShortcuts`? (Or, why I got `"Invalid session"`?)

,

I am trying to use org.freedesktop.portal.GlobalShortcuts, but the related doc is insanely cryptic.

This is what I got so far. I can get a return from CreateSession, but then trying to do anything else, I get “invalid session”.

Environment is Qt6 on KDE6.

QDBusMessage(type=MethodReturn, service=":1.8", signature="o", contents=([ObjectPath: /org/freedesktop/portal/desktop/request/1_205/What_should_be_in_here]) )
QDBusMessage(type=Error, service=":1.8", error name="org.freedesktop.DBus.Error.AccessDenied", error message="Invalid session", signature="s", contents=("Invalid session") )

The source code, create a session, then try to get a list of registered shortcuts (which I think should return an empty list).

Code with cmake build file temp_wayland_shortcut · GitHub

#include <QApplication>
#include <QtDBus/QtDBus>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    QDBusMessage create_session = QDBusMessage::createMethodCall(
        "org.freedesktop.portal.Desktop",
        "/org/freedesktop/portal/desktop",
        "org.freedesktop.portal.GlobalShortcuts",
        "CreateSession"
    );

    const QString handle_token = "What_should_be_in_here";
    const QString session_handle_token = "And_here";

    QList<QVariant> args_create_session;

    QMap<QString, QVariant> options_create_session;
    options_create_session.insert("handle_token", handle_token);
    options_create_session.insert("session_handle_token", session_handle_token);

    args_create_session.append(options_create_session);

    create_session.setArguments(args_create_session);

    QDBusMessage c = QDBusConnection::sessionBus().call(create_session);
    auto session_handle = c.arguments().first().value<QDBusObjectPath>();

    qDebug() << c;

    QDBusMessage list_shortcut = QDBusMessage::createMethodCall(
        "org.freedesktop.portal.Desktop",
        "/org/freedesktop/portal/desktop",
        "org.freedesktop.portal.GlobalShortcuts",
        "ListShortcuts"
    );

    QList<QVariant> args_list_shotcuts;
    args_list_shotcuts.append(session_handle);
    QMap<QString, QVariant> options_list_shortcut;
    options_list_shortcut.insert("handle_token", handle_token);

    args_list_shotcuts.append(options_list_shortcut);

    list_shortcut.setArguments(args_list_shotcuts);
    QDBusMessage list_ret = QDBusConnection::sessionBus().call(list_shortcut);
    qDebug() << list_ret;

    return QApplication::exec();
}

Running this test’s global shortcut on my machine works somehow.

Solved.

The return of CreateSession is a response that contains the session_handle.

Thanks to src/xdgportaltest.cpp · 544a7a56124022004197a657c9bfbd4d432962d3 · Libraries / KDE XDG Portal Test · GitLab

I hate untyped things.