[glib: 21/24] GAppInfo: Add async API to get default Application for URI scheme




commit 8aee5fc6282ae64ed95565bd3fca0620ebd467d0
Author: Marco Trevisan (Treviño) <mail 3v1n0 net>
Date:   Wed Jun 1 21:45:58 2022 +0200

    GAppInfo: Add async API to get default Application for URI scheme
    
    Make possible to fetch the default application for URI scheme in a
    thread without using blocking I/O.

 docs/reference/gio/gio-sections-common.txt |  2 +
 gio/gappinfo.c                             | 82 ++++++++++++++++++++++++++++++
 gio/gappinfo.h                             |  9 ++++
 gio/tests/desktop-app-info.c               | 71 ++++++++++++++++++++++----
 4 files changed, 153 insertions(+), 11 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections-common.txt b/docs/reference/gio/gio-sections-common.txt
index be4311701e..0ef50d747c 100644
--- a/docs/reference/gio/gio-sections-common.txt
+++ b/docs/reference/gio/gio-sections-common.txt
@@ -1445,6 +1445,8 @@ g_app_info_get_default_for_type
 g_app_info_get_default_for_type_async
 g_app_info_get_default_for_type_finish
 g_app_info_get_default_for_uri_scheme
+g_app_info_get_default_for_uri_scheme_async
+g_app_info_get_default_for_uri_scheme_finish
 g_app_info_get_fallback_for_type
 g_app_info_get_recommended_for_type
 g_app_info_launch_default_for_uri
diff --git a/gio/gappinfo.c b/gio/gappinfo.c
index cced4d0220..286b0bdbab 100644
--- a/gio/gappinfo.c
+++ b/gio/gappinfo.c
@@ -851,6 +851,88 @@ g_app_info_get_default_for_type_async  (const char          *content_type,
   g_object_unref (task);
 }
 
+static void
+get_default_for_scheme_thread (GTask         *task,
+                               gpointer       object,
+                               gpointer       task_data,
+                               GCancellable  *cancellable)
+{
+  const char *uri_scheme = task_data;
+  GAppInfo *info;
+
+  info = g_app_info_get_default_for_uri_scheme (uri_scheme);
+
+  if (!info)
+    {
+      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                               _("Failed to find default application for "
+                                 "URI Scheme ‘%s’"), uri_scheme);
+      return;
+    }
+
+  g_task_return_pointer (task, g_steal_pointer (&info), g_object_unref);
+}
+
+/**
+ * g_app_info_get_default_for_uri_scheme_async:
+ * @uri_scheme: a string containing a URI scheme.
+ * @cancellable: optional #GCancellable object, %NULL to ignore
+ * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
+ * @user_data: (nullable): data to pass to @callback
+ *
+ * Asynchronously gets the default application for handling URIs with
+ * the given URI scheme. A URI scheme is the initial part
+ * of the URI, up to but not including the ':', e.g. "http",
+ * "ftp" or "sip".
+ *
+ * Since: 2.74
+ */
+void
+g_app_info_get_default_for_uri_scheme_async (const char          *uri_scheme,
+                                             GCancellable        *cancellable,
+                                             GAsyncReadyCallback  callback,
+                                             gpointer             user_data)
+{
+  GTask *task;
+
+  g_return_if_fail (uri_scheme != NULL && *uri_scheme != '\0');
+  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (NULL, cancellable, callback, user_data);
+  g_task_set_source_tag (task, g_app_info_get_default_for_uri_scheme_async);
+  g_task_set_task_data (task, g_strdup (uri_scheme), g_free);
+  g_task_set_check_cancellable (task, TRUE);
+  g_task_run_in_thread (task, get_default_for_scheme_thread);
+  g_object_unref (task);
+}
+
+/**
+ * g_app_info_get_default_for_uri_scheme_finish:
+ * @result: a #GAsyncResult
+ * @error: (nullable): a #GError
+ *
+ * Finishes a default #GAppInfo lookup started by
+ * g_app_info_get_default_for_uri_scheme_async().
+ *
+ * If no #GAppInfo is found, then @error will be set to %G_IO_ERROR_NOT_FOUND.
+ *
+ * Returns: (transfer full): #GAppInfo for given @uri_scheme or
+ *     %NULL on error.
+ *
+ * Since: 2.74
+ */
+GAppInfo *
+g_app_info_get_default_for_uri_scheme_finish (GAsyncResult  *result,
+                                              GError       **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
+  g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
+                        g_app_info_get_default_for_uri_scheme_async, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
 /**
  * g_app_info_get_default_for_type_finish:
  * @result: a #GAsyncResult
diff --git a/gio/gappinfo.h b/gio/gappinfo.h
index 722e8b36d4..84162f0e23 100644
--- a/gio/gappinfo.h
+++ b/gio/gappinfo.h
@@ -258,6 +258,15 @@ GAppInfo *g_app_info_get_default_for_type_finish (GAsyncResult         *result,
 GLIB_AVAILABLE_IN_ALL
 GAppInfo *g_app_info_get_default_for_uri_scheme  (const char  *uri_scheme);
 
+GLIB_AVAILABLE_IN_2_74
+void      g_app_info_get_default_for_uri_scheme_async (const char          *uri_scheme,
+                                                       GCancellable        *cancellable,
+                                                       GAsyncReadyCallback  callback,
+                                                       gpointer             user_data);
+GLIB_AVAILABLE_IN_2_74
+GAppInfo *g_app_info_get_default_for_uri_scheme_finish (GAsyncResult         *result,
+                                                        GError              **error);
+
 GLIB_AVAILABLE_IN_ALL
 gboolean  g_app_info_launch_default_for_uri      (const char              *uri,
                                                   GAppLaunchContext       *context,
diff --git a/gio/tests/desktop-app-info.c b/gio/tests/desktop-app-info.c
index 5cc8d06182..05b45c6462 100644
--- a/gio/tests/desktop-app-info.c
+++ b/gio/tests/desktop-app-info.c
@@ -195,18 +195,10 @@ typedef struct
 } DefaultForTypeData;
 
 static void
-on_default_for_type_cb (GObject      *object,
-                        GAsyncResult *result,
-                        gpointer      user_data)
+ensure_default_type_result (GAppInfo           *info,
+                            DefaultForTypeData *data,
+                            GError             *error)
 {
-  GAppInfo *info;
-  GError *error = NULL;
-  DefaultForTypeData *data = user_data;
-
-  g_assert_null (object);
-
-  info = g_app_info_get_default_for_type_finish (result, &error);
-
   if (data->expected_info)
     {
       g_assert_nonnull (info);
@@ -224,6 +216,38 @@ on_default_for_type_cb (GObject      *object,
   g_clear_error (&error);
 }
 
+static void
+on_default_for_type_cb (GObject      *object,
+                        GAsyncResult *result,
+                        gpointer      user_data)
+{
+  GAppInfo *info;
+  GError *error = NULL;
+  DefaultForTypeData *data = user_data;
+
+  g_assert_null (object);
+
+  info = g_app_info_get_default_for_type_finish (result, &error);
+
+  ensure_default_type_result (info, data, error);
+}
+
+static void
+on_default_for_uri_cb (GObject      *object,
+                       GAsyncResult *result,
+                       gpointer      user_data)
+{
+  GAppInfo *info;
+  GError *error = NULL;
+  DefaultForTypeData *data = user_data;
+
+  g_assert_null (object);
+
+  info = g_app_info_get_default_for_uri_scheme_finish (result, &error);
+
+  ensure_default_type_result (info, data, error);
+}
+
 static void
 test_default_async (void)
 {
@@ -279,6 +303,24 @@ test_default_async (void)
                                          NULL, on_default_for_type_cb, &data);
   g_main_loop_run (data.loop);
 
+  g_app_info_set_as_default_for_type (info3, "x-scheme-handler/glib-async", &error);
+  g_assert_no_error (error);
+
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*assertion*uri_scheme*failed*");
+  g_assert_null (g_app_info_get_default_for_uri_scheme (NULL));
+  g_test_assert_expected_messages ();
+
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*assertion*uri_scheme*failed*");
+  g_assert_null (g_app_info_get_default_for_uri_scheme (""));
+  g_test_assert_expected_messages ();
+
+  data.expected_info = info3;
+  g_app_info_get_default_for_uri_scheme_async ("glib-async", NULL,
+                                               on_default_for_uri_cb, &data);
+  g_main_loop_run (data.loop);
+
   /* now clean it all up */
   g_app_info_reset_type_associations ("application/x-test");
 
@@ -287,6 +329,13 @@ test_default_async (void)
                                          NULL, on_default_for_type_cb, &data);
   g_main_loop_run (data.loop);
 
+  g_app_info_reset_type_associations ("x-scheme-handler/glib-async");
+
+  data.expected_info = NULL;
+  g_app_info_get_default_for_uri_scheme_async ("glib-async", NULL,
+                                               on_default_for_uri_cb, &data);
+  g_main_loop_run (data.loop);
+
   list = g_app_info_get_all_for_type ("application/x-test");
   g_assert_null (list);
 


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