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!