[libdazzle: 1/3] util: use async calls to show folder in file manager
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libdazzle: 1/3] util: use async calls to show folder in file manager
- Date: Mon, 22 Jun 2020 19:08:17 +0000 (UTC)
commit a01be16067cc3c92d8b32276312d694845561b37
Author: Germán Poo-Caamaño <gpoo gnome org>
Date: Sun Jun 21 12:37:42 2020 -0400
util: use async calls to show folder in file manager
Avoid blocking the UI if dzl_file_manage_show is called from the
main thread if there were any issue with dbus.
To my eyes, this looks ugly, as dzl_file_manage_show is a sync
call in three systems (Win32, OSX, and Linux). Ideally, we could
have dzl_file_manage_show and dzl_file_manage_show_sync, or
one async in all systems.
If the API user check error, still it will work for Win32 and
OSX. For Linux, it will be managed by dzl_file_manage_show.
src/util/dzl-file-manager.c | 106 +++++++++++++++++++++++++++-----------------
1 file changed, 65 insertions(+), 41 deletions(-)
---
diff --git a/src/util/dzl-file-manager.c b/src/util/dzl-file-manager.c
index 52052b5..417a57a 100644
--- a/src/util/dzl-file-manager.c
+++ b/src/util/dzl-file-manager.c
@@ -2,6 +2,7 @@
*
* Copyright (C) 1995-2017 GIMP Authors
* Copyright (C) 2015-2017 Christian Hergert <christian hergert me>
+ * Copyright (C) 2020 Germán Poo-Caamaño <gpoo gnome org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -39,6 +40,60 @@
#include "util/dzl-file-manager.h"
+#if !(defined(G_OS_WIN32) || defined(PLATFORM_OSX))
+static void
+select_file_in_containing_folder_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GError *error = NULL;
+
+ g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), result, &error);
+
+ if (error != NULL)
+ {
+ g_prefix_error (&error, _("Calling ShowItems failed: "));
+ g_error_free (error);
+ }
+}
+
+static void
+select_file_in_containing_folder (GObject *source_object,
+ GAsyncResult *result,
+ gchar *uri)
+{
+ GDBusProxy *proxy;
+ GError *error = NULL;
+ GVariantBuilder *builder;
+
+ proxy = g_dbus_proxy_new_finish (result, &error);
+
+ if (proxy != NULL)
+ {
+ g_prefix_error (&error,
+ _("Connecting to org.freedesktop.FileManager1 failed: "));
+ g_error_free (error);
+ return;
+ }
+
+ builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
+ g_variant_builder_add (builder, "s", uri);
+
+ g_free (uri);
+
+ g_dbus_proxy_call (proxy,
+ "ShowItems",
+ g_variant_new ("(ass)", builder, ""),
+ G_DBUS_CALL_FLAGS_NONE,
+ -1,
+ NULL,
+ (GAsyncReadyCallback) select_file_in_containing_folder_cb,
+ NULL);
+
+ g_variant_builder_unref (builder);
+}
+#endif /* !(defined(G_OS_WIN32) || defined(PLATFORM_OSX)) */
+
/* Copied from the GIMP */
gboolean
dzl_file_manager_show (GFile *file,
@@ -143,51 +198,20 @@ dzl_file_manager_show (GFile *file,
#else /* UNIX */
{
- GDBusProxy *proxy;
- GVariant *retval;
- GVariantBuilder *builder;
gchar *uri;
- proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.freedesktop.FileManager1",
- "/org/freedesktop/FileManager1",
- "org.freedesktop.FileManager1",
- NULL, error);
-
- if (!proxy)
- {
- g_prefix_error (error,
- _("Connecting to org.freedesktop.FileManager1 failed: "));
- return FALSE;
- }
-
uri = g_file_get_uri (file);
- builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
- g_variant_builder_add (builder, "s", uri);
-
- g_free (uri);
-
- retval = g_dbus_proxy_call_sync (proxy,
- "ShowItems",
- g_variant_new ("(ass)",
- builder,
- ""),
- G_DBUS_CALL_FLAGS_NONE,
- -1, NULL, error);
-
- g_variant_builder_unref (builder);
- g_object_unref (proxy);
-
- if (!retval)
- {
- g_prefix_error (error, _("Calling ShowItems failed: "));
- return FALSE;
- }
-
- g_variant_unref (retval);
+ g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
+ G_DBUS_PROXY_FLAGS_NONE,
+ NULL,
+ "org.freedesktop.FileManager1",
+ "/org/freedesktop/FileManager1",
+ "org.freedesktop.FileManager1",
+ NULL,
+ (GAsyncReadyCallback) select_file_in_containing_folder,
+ uri);
+ /* select_file_in_containing_folder should free `uri` when no longer needed */
return TRUE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]