0

I have been trying to write a flatpak application in python, which uses the freedesktop portal to take screenshots. I am adapting Gimp's freedesktop screenshot plugin which is written in c and I am running into trouble when it comes to accessing the screenshot after it is taken and shared with my application. I get a uri of the form '/org/freedesktop/portal/desktop/request/1_326/t' and the freedesktop portal documentation says that the uri points into a fuse file system mounted at run/user/$UID/doc/ and I can confirm that the screenshots are being saved there. However, t I can't seem to be able to get a useful identifier for them so that I can attach them to an email with my app.

I have experimented with all of the methods in the freedesktop Documents portal and have had no success there. After calling a dbus proxy which returns a uri which is saved in the opath variable the Gimp implementation sets up another dbus proxy and then connects that proxy to a callback function here: the full file that I copied these snippets from is available here <https://github.com/GNOME/gimp/blob/master/plug-ins/screenshot/screenshot-freedesktop.c>

if (opath)
    {
      GDBusProxy *proxy2 = NULL;

      proxy2 = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                              G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
                                              NULL,
                                              "org.freedesktop.portal.Desktop",
                                              opath,
                                              "org.freedesktop.portal.Request",
                                              NULL, NULL);
      *image_ID = 0;
      g_signal_connect (proxy2, "g-signal",
                        G_CALLBACK (screenshot_freedesktop_dbus_signal),
                        image_ID);

      gtk_main ();
      g_object_unref (proxy2);
      g_free (opath);

And the screenshot_freedesktop_dbus_signal function looks like this:

static void
screenshot_freedesktop_dbus_signal (GDBusProxy *proxy,
                                    gchar      *sender_name,
                                    gchar      *signal_name,
                                    GVariant   *parameters,
                                    gint32     *image_ID)
{
  if (g_strcmp0 (signal_name, "Response") == 0)
    {
      GVariant *results;
      guint32   response;

      g_variant_get (parameters, "(u@a{sv})",
                     &response,
                     &results);

      /* Possible values:
       * 0: Success, the request is carried out
       * 1: The user cancelled the interaction
       * 2: The user interaction was ended in some other way
       * Cf. https://github.com/flatpak/xdg-desktop-portal/blob/master/data/org.freedesktop.portal.Request.xml
       */
      if (response == 0)
        {
          gchar *uri;

          if (g_variant_lookup (results, "uri", "s", &uri))
            {
              GFile *file = g_file_new_for_uri (uri);
              gchar *path = g_file_get_path (file);

              *image_ID = gimp_file_load (GIMP_RUN_NONINTERACTIVE,
                                          path, path);
              gimp_image_set_filename (*image_ID, "screenshot.png");

              /* Delete the actual file. */
              g_file_delete (file, NULL, NULL);

              g_object_unref (file);
              g_free (path);
              g_free (uri);
            }
        }

      g_variant_unref (results);
      gtk_main_quit ();
    }
}

My code works the same way until the callback function. I can't find the pythonic equivalent of the g_signal_connect function. I have tried adding the callback to the new proxy's initialization like this

proxy2 = Gio.DBusProxy.new_for_bus(Gio.BusType.SESSION,
                                            Gio.DBusProxyFlags.NONE,
                                            None,
                                            "org.freedesktop.portal.Desktop",
                                            returned_uri[0],
                                            "org.freedesktop.portal.Request",
                                            None,
                                            G_CALLBACK(self.receive_screenshot_signal),
                                            None)

but my receive_screenshot_signal function is not passed any values and the task objects which are passed in say that they are not finished.

I am not sure where to go from here so any advice or insight into how I can use the dbusproxy library and/or the freedesktop portals more effectively would be appreciated. Thanks!

Josh Bell
  • 31
  • 1
  • 5

1 Answers1

0

I resolved this issue by subscribing to the signal from the bus on the path of the request handle.

        args = GLib.Variant('(sa{sv})', (filename, {}))
        result = self.proxy.call_sync('Screenshot',
                                      args,
                                      Gio.DBusCallFlags.NONE,
                                      -1,
                                      None)
        request_handle = result.unpack()[0]
        self.bus.signal_subscribe("org.freedesktop.portal.Desktop",
                                  "org.freedesktop.portal.Request",
                                  "Response",
                                  request_handle,
                                  None,
                                  Gio.DBusSignalFlags.NO_MATCH_RULE,
                                  self.receive_screenshot_signal)

I couldn't find any specific documention on Gio.bus.signal_subscribe but here is how the callback function handles the return:

  def receive_screenshot_signal(self,
                                  connection,
                                  sender,
                                  path,
                                  interface,
                                  signal,
                                  params):
        if not isinstance(params, GLib.Variant):
            print("It's not a variant")
            return
        response_code, results = params.unpack()
        # Response code 0 signifies success
        if response_code != 0 or 'uri' not in results:
            print("Error taking screenshot")
            return
        parsed_uri = urlparse(results['uri'])
        assert parsed_uri.scheme == "file"
        self.screenshot = unquote(parsed_uri.path)
Josh Bell
  • 31
  • 1
  • 5