[gtk] Handle lack of file transfer portal



commit 806e159b8ba18d9044641994d89d2c32dbf09ffa
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jan 14 01:03:48 2020 -0500

    Handle lack of file transfer portal
    
    Don't block for 25 seconds trying to get a bus
    connection. Give up after 1 second.

 gdk/filetransferportal.c | 65 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 9 deletions(-)
---
diff --git a/gdk/filetransferportal.c b/gdk/filetransferportal.c
index 382c8bd299..6f31c917ff 100644
--- a/gdk/filetransferportal.c
+++ b/gdk/filetransferportal.c
@@ -42,6 +42,40 @@
 #include "filetransferportalprivate.h"
 
 static GDBusProxy *file_transfer_proxy = NULL;
+static gboolean done;
+static guint timeout_id;
+
+static void
+got_proxy (GObject *source,
+           GAsyncResult *result,
+           gpointer data)
+{
+  GError *error = NULL;
+
+  file_transfer_proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
+  if (!file_transfer_proxy)
+    {
+      g_message ("failed to get file transfer portal proxy: %s", error->message);
+      g_error_free (error);
+    }
+
+  done = TRUE;
+}
+
+static gboolean
+give_up_on_proxy (gpointer data)
+{
+  GCancellable *cancellable = data;
+
+  g_cancellable_cancel (cancellable);
+
+  timeout_id = 0;
+  done = TRUE;
+
+  g_main_context_wakeup (NULL);
+
+  return G_SOURCE_REMOVE;
+}
 
 static GDBusProxy *
 ensure_file_transfer_portal (void)
@@ -49,16 +83,29 @@ ensure_file_transfer_portal (void)
   if (file_transfer_proxy == NULL)
     {
       GError *error = NULL;
+      GCancellable *cancellable;
+
+      cancellable = g_cancellable_new ();
+
+      g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
+                                G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES
+                                | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS
+                                | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
+                                NULL,
+                                "org.freedesktop.portal.Documents",
+                                "/org/freedesktop/portal/documents",
+                                "org.freedesktop.portal.FileTransfer",
+                                cancellable,
+                                got_proxy,
+                                NULL);
+
+      timeout_id = g_timeout_add_full (1000, 0, give_up_on_proxy, cancellable, g_object_unref);
+
+      while (!done)
+        g_main_context_iteration (NULL, TRUE);
 
-      file_transfer_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
-                                                           G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES
-                                                           | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS
-                                                           | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
-                                                           NULL,
-                                                           "org.freedesktop.portal.Documents",
-                                                           "/org/freedesktop/portal/documents",
-                                                           "org.freedesktop.portal.FileTransfer",
-                                                           NULL, &error);
+      if (timeout_id)
+        g_source_remove (timeout_id);
 
       if (error)
         {


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