[eog/set-wallpaper-with-portal] EogWindow, EogUtil: Use portals for setting wallpaper when possible



commit 6e6b1695fc5dc2d2fc76c67d6ed1f11b3b119ce3
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Jul 14 11:25:39 2020 +0200

    EogWindow, EogUtil: Use portals for setting wallpaper when possible
    
    If running in a Flatpak container, we can use the xdg-desktop-portal
    Wallpaper Portal for setting the desktop background image. This is
    DE agnostic and handles the file copy itself (which could be
    problematic in a sandbox).

 src/eog-util.c   | 30 ++++++++++++++++++++++++++++++
 src/eog-util.h   |  3 +++
 src/eog-window.c | 14 ++++++++++----
 3 files changed, 43 insertions(+), 4 deletions(-)
---
diff --git a/src/eog-util.c b/src/eog-util.c
index 02cc802cc..7a125aa10 100644
--- a/src/eog-util.c
+++ b/src/eog-util.c
@@ -547,3 +547,33 @@ eog_util_open_file_with_flatpak_portal (GFile *file, GtkWindow *window)
                             NULL);
        xdp_parent_free (parent);
 }
+
+static void
+set_wallpaper_with_portal_cb (GObject *source, GAsyncResult *result, gpointer user_data) {
+        XdpPortal *portal = XDP_PORTAL (source);
+        g_autoptr(GError) error = NULL;
+
+        if (!xdp_portal_set_wallpaper_finish (portal, result, &error))
+        {
+                g_warning ("Failed to set wallpaper via portal: %s", error->message);
+        }
+}
+
+void
+eog_util_set_wallpaper_with_portal (GFile *file, GtkWindow *window) {
+        g_autoptr(XdpPortal) portal = NULL;
+        g_autofree gchar *uri = NULL;
+        XdpParent *parent = NULL;
+
+        portal = xdp_portal_new ();
+        parent = xdp_parent_new_gtk (window);
+        uri = g_file_get_uri (file);
+
+        xdp_portal_set_wallpaper (portal,
+                                  parent,
+                                  uri,
+                                  XDP_WALLPAPER_FLAG_PREVIEW,
+                                  NULL,
+                                  set_wallpaper_with_portal_cb,
+                                  window);
+}
diff --git a/src/eog-util.h b/src/eog-util.h
index a87c97c3d..136364258 100644
--- a/src/eog-util.h
+++ b/src/eog-util.h
@@ -76,6 +76,9 @@ G_GNUC_INTERNAL
 void     eog_util_open_file_with_flatpak_portal (GFile *file,
                                                  GtkWindow *window);
 
+void     eog_util_set_wallpaper_with_portal     (GFile *file,
+                                                GtkWindow *window);
+
 G_END_DECLS
 
 #endif /* __EOG_UTIL_H__ */
diff --git a/src/eog-window.c b/src/eog-window.c
index 76844556f..c244cce35 100644
--- a/src/eog-window.c
+++ b/src/eog-window.c
@@ -71,6 +71,9 @@
 #include <libpeas/peas-extension-set.h>
 #include <libpeas/peas-activatable.h>
 
+#include <libportal/portal.h>
+#include <libportal/portal-gtk3.h>
+
 #if defined(HAVE_LCMS) && defined(GDK_WINDOWING_X11)
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
@@ -3115,8 +3118,8 @@ eog_window_action_wallpaper (GSimpleAction *action,
        EogWindow *window;
        EogWindowPrivate *priv;
        EogImage *image;
-       GFile *file;
-       char *filename = NULL;
+       g_autoptr(GFile) file = NULL;
+       g_autofree gchar *filename = NULL;
 
        g_return_if_fail (EOG_IS_WINDOW (user_data));
 
@@ -3168,11 +3171,14 @@ eog_window_action_wallpaper (GSimpleAction *action,
                return;
        }
 
-       g_object_unref (file);
+       if (eog_util_is_running_inside_flatpak ()) {
+               eog_util_set_wallpaper_with_portal (file, GTK_WINDOW (window));
+
+               return;
+       }
 
        eog_window_set_wallpaper (window, filename, NULL);
 
-       g_free (filename);
 }
 
 static gboolean


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]