Using xdg-desktop-portal-kde to take screenshots

Damir Porobic damir_porobic at live.com
Sun Sep 20 17:36:48 BST 2020


Hello Jan,

finally I  found some time to upgrade my OS and got a newer Plasma Version but I'm still struggling with the Screenshot Portal.

Here is my implementation, almost identical to yours from the test that you have linked me (pasting here only the relevant methods):

void WaylandImageGrabber::grab()
{
   auto message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"),
                                                         QLatin1String("/org/freedesktop/portal/desktop"),
                                                         QLatin1String("org.freedesktop.portal.Screenshot"),
                                                         QLatin1String("Screenshot"));

   message << QLatin1String("wayland:") << QVariantMap{{QLatin1String("interactive"), false}, {QLatin1String("handle_token"), getRequestToken()}};

   auto pendingCall = QDBusConnection::sessionBus().asyncCall(message);
   auto watcher = new QDBusPendingCallWatcher(pendingCall);
   connect(watcher, &QDBusPendingCallWatcher::finished, [this] (QDBusPendingCallWatcher *watcher) {
      QDBusPendingReply<QDBusObjectPath> reply = *watcher;
      if (reply.isError()) {
         qDebug("Couldn't get reply");
         qWarning("Error: %s", qPrintable(reply.error().message()));
      } else {
          qDebug("Got reply");
         QDBusConnection::sessionBus().connect(QString(),
                                               reply.value().path(),
                                               QLatin1String("org.freedesktop.portal.Request"),
                                               QLatin1String("Response"),
                                               this,
                                               SLOT(gotScreenshotResponse(uint,QVariantMap)));
      }
   });
}

void WaylandImageGrabber::gotScreenshotResponse(uint response, const QVariantMap& results)
{
    qDebug("Response called");
   if (!response) {
      if (results.contains(QLatin1String("uri"))) {
         qDebug("Success");
         emit finished(CaptureDto(QPixmap::fromImage(QImage(results.value(QLatin1String("uri")).toString()))));
      }
   } else {
      qDebug("Failed to take screenshot");
   }
}

QString WaylandImageGrabber::getRequestToken()
{
   mRequestTokenCounter += 1;
   return QString("u%1").arg(mRequestTokenCounter);
}

I get the dialog which asks me what type of screenshot I want to take I select it, a small image is show in the dialog and I can click on save (which saves the image in my home directory) but the gotScreenshotResponse is never called. Any idea why?
Can I tell the dialog where to save those images?
Can I tell the dialog right away what type of screenshot I want to take?
Can I just share the screenshot with my application without saving it?

Thanks in advance for your help!

Best regards,
Damir

________________________________
From: kde-devel <kde-devel-bounces at kde.org> on behalf of Damir Porobic <damir_porobic at live.com>
Sent: Monday, August 31, 2020 13:22
To: kde-devel at kde.org <kde-devel at kde.org>
Subject: Re: Using xdg-desktop-portal-kde to take screenshots

Thanks, that helps. I'll upgrade my OS, the latest version seems to ship Plasma 5.18

Best regards,
Damir

________________________________
From: kde-devel <kde-devel-bounces at kde.org> on behalf of Jan Grulich <jgrulich at redhat.com>
Sent: Monday, August 31, 2020 11:04
To: kde-devel at kde.org <kde-devel at kde.org>
Subject: Re: Using xdg-desktop-portal-kde to take screenshots

Hi,

unfortunately the Screenshot portal doesn't seem to be present in Plasma 5.12, it was added into Plasma 5.13.

Regards,
Jan

ne 30. 8. 2020 v 10:38 odesílatel Damir Porobic <damir_porobic at live.com<mailto:damir_porobic at live.com>> napsal:
Hey Jan,

thanks for the Feedback, that is indeed a good tip, I'll have a look at it.
Another question, do I need to activate it or is it available only after a specific version?
I'm using Plasma 5.12.8 and it doesn't seem to be available.

Regards,
Damir

________________________________
From: kde-devel <kde-devel-bounces at kde.org<mailto:kde-devel-bounces at kde.org>> on behalf of Jan Grulich <jgrulich at redhat.com<mailto:jgrulich at redhat.com>>
Sent: Sunday, August 23, 2020 14:58
To: kde-devel at kde.org<mailto:kde-devel at kde.org> <kde-devel at kde.org<mailto:kde-devel at kde.org>>
Subject: Re: Using xdg-desktop-portal-kde to take screenshots

Hi,

you can check how we use it in xdg-portal-test-kde (a simple app to test various portals).

The code is here: https://invent.kde.org/libraries/xdg-portal-test-kde/-/blob/master/src/portaltest.cpp#L498

Regards,
Jan

út 18. 8. 2020 v 12:59 odesílatel Méven <meven29 at gmail.com<mailto:meven29 at gmail.com>> napsal:
Le mar. 18 août 2020 à 12:04, <kde-devel-request at kde.org<mailto:kde-devel-request at kde.org>> a écrit :
Date: Mon, 17 Aug 2020 12:44:35 +0000
From: Damir Porobic <damir_porobic at live.com<mailto:damir_porobic at live.com>>
To: "kde-devel at kde.org<mailto:kde-devel at kde.org>" <kde-devel at kde.org<mailto:kde-devel at kde.org>>
Subject: Re: Using xdg-desktop-portal-kde to take screenshots
Message-ID:
        <AM0PR09MB389099FAC1A15B22F0E6D4AFE55F0 at AM0PR09MB3890.eurprd09.prod.outlook.com<mailto:AM0PR09MB389099FAC1A15B22F0E6D4AFE55F0 at AM0PR09MB3890.eurprd09.prod.outlook.com>>

Content-Type: text/plain; charset="iso-8859-1"

Hello,

has anyone experience with this portal?

Best regards,
Damir

________________________________
From: kde-devel <kde-devel-bounces at kde.org<mailto:kde-devel-bounces at kde.org>> on behalf of Damir Porobic <damir_porobic at live.com<mailto:damir_porobic at live.com>>
Sent: Thursday, August 13, 2020 16:16
To: kde-devel at kde.org<mailto:kde-devel at kde.org> <kde-devel at kde.org<mailto:kde-devel at kde.org>>
Subject: Using xdg-desktop-portal-kde to take screenshots

Hello,

I'm trying use the xdg-desktop-portal-kde (https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/blob/master/src/screenshot.cpp) portal in order to take screenshots under Wayland.

If I understand correctly, making a DBus Call like this should open a Dialog with a Screenshot that can be shared with my application.

*********************************
QDBusInterface interface(QStringLiteral("org.freedesktop.portal.Desktop"), QStringLiteral("/org/freedesktop/portal/Screenshot"), QStringLiteral("org.freedesktop.portal.Screenshot"));
QDBusPendingReply<QDBusObjectPath> reply;

reply = interface.call(QStringLiteral("Screenshot"), "", QVariantMap());

if (reply.isError()) {
    qCritical("Invalid reply from DBus: %s", qPrintable(reply.error().message()));
    emit canceled();
} else {
    qCritical("Dbus success: %s", qPrintable(reply.argumentAt<0>().path()));
}
*********************************

But the path to the image is not returned in the result from this call. How am I supposed to get the result?

Is there any example implementation that uses this portal? In Spectacle I have only seen the solution using org.kde.plasmashell, but not the portal solution.

Thanks in advance!

Best regards
Damir

Hi Damir,

You need to pass an instance of QVariantMap that will be filled up with a uri field according to the code:

auto result = QVariantMap();
reply = interface.call(QStringLiteral("Screenshot"), "", &result);
result.value("uri");

But you might be missing parameters also :

    uint Screenshot(const QDBusObjectPath &handle,
                    const QString &app_id,
                    const QString &parent_window,
                    const QVariantMap &options,
                    QVariantMap &results);

Also the file is saved in QStandardPaths::PicturesLocation with a timestamp.

Documentation is at https://flatpak.github.io/xdg-desktop-portal/portal-docs.html#gdbus-org.freedesktop.impl.portal.Screenshot

I hope this helps,

Kind regards,

--
Méven

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-devel/attachments/20200920/c95f1642/attachment.htm>


More information about the kde-devel mailing list