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



commit b96936dd81d5f7879983d4df27959a00637bc9de
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   | 34 ++++++++++++++++++++++++++++++++++
 src/eog-util.h   |  3 +++
 src/eog-window.c | 13 +++++++++----
 3 files changed, 46 insertions(+), 4 deletions(-)
---
diff --git a/src/eog-util.c b/src/eog-util.c
index 076fe1084..90b9768ea 100644
--- a/src/eog-util.c
+++ b/src/eog-util.c
@@ -550,4 +550,38 @@ 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_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        {
+                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_BACKGROUND | XDP_WALLPAPER_FLAG_PREVIEW,
+                                  NULL,
+                                  set_wallpaper_with_portal_cb,
+                                  NULL);
+        xdp_parent_free (parent);
+}
 #endif
diff --git a/src/eog-util.h b/src/eog-util.h
index c3a2150fb..ae3c02448 100644
--- a/src/eog-util.h
+++ b/src/eog-util.h
@@ -76,6 +76,9 @@ gboolean eog_util_is_running_inside_flatpak  (void);
 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);
 #endif
 
 G_END_DECLS
diff --git a/src/eog-window.c b/src/eog-window.c
index afbf5e3a9..885de04af 100644
--- a/src/eog-window.c
+++ b/src/eog-window.c
@@ -3118,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));
 
@@ -3171,11 +3171,16 @@ eog_window_action_wallpaper (GSimpleAction *action,
                return;
        }
 
-       g_object_unref (file);
+#ifdef HAVE_LIBPORTAL
+       if (eog_util_is_running_inside_flatpak ()) {
+               eog_util_set_wallpaper_with_portal (file, GTK_WINDOW (window));
+
+               return;
+       }
+#endif
 
        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]