[glib/wip/oholy/gappinfo-async: 1/4] gfile: Add g_file_query_default_handler_async()



commit 055f93332148cb4cd766aa643d2bad18ee5d92dc
Author: Ondrej Holy <oholy redhat com>
Date:   Tue Jan 22 15:37:44 2019 +0100

    gfile: Add g_file_query_default_handler_async()
    
    This is needed as a first step to fix the
    g_app_info_launch_default_for_uri_async() function to be really
    asynchronous.
    
    It still uses the g_app_info_get_default_for_uri_scheme() and
    g_app_info_get_default_for_type() functions, which may use synchronous
    calls. I suppose that this shouldn't be such a big issue in ordinary
    cases.
    
    https://gitlab.gnome.org/GNOME/glib/issues/1347
    https://gitlab.gnome.org/GNOME/glib/issues/1249

 gio/gfile.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gio/gfile.h |  11 ++++++
 2 files changed, 132 insertions(+)
---
diff --git a/gio/gfile.c b/gio/gfile.c
index a5709a4cc..86dad9118 100644
--- a/gio/gfile.c
+++ b/gio/gfile.c
@@ -6883,6 +6883,127 @@ g_file_query_default_handler (GFile         *file,
   return NULL;
 }
 
+static void
+query_default_handler_query_info_cb (GObject      *object,
+                                     GAsyncResult *result,
+                                     gpointer      user_data)
+{
+  GFile *file = G_FILE (object);
+  GTask *task = G_TASK (user_data);
+  GError *error = NULL;
+  GFileInfo *info;
+  const char *content_type;
+  GAppInfo *appinfo = NULL;
+
+  info = g_file_query_info_finish (file, result, &error);
+  if (info == NULL)
+    {
+      g_task_return_error (task, error);
+      return;
+    }
+
+  content_type = g_file_info_get_content_type (info);
+  if (content_type)
+    {
+      char *path;
+
+      /* Don't use is_native(), as we want to support fuse paths if available */
+      path = g_file_get_path (file);
+
+      /* FIXME: The following still uses blocking calls. */
+      appinfo = g_app_info_get_default_for_type (content_type,
+                                                 path == NULL);
+      g_free (path);
+    }
+
+  g_object_unref (info);
+
+  if (appinfo != NULL)
+    {
+      g_task_return_pointer (task, appinfo, g_object_unref);
+      return;
+    }
+
+  g_task_return_new_error (task,
+                           G_IO_ERROR,
+                           G_IO_ERROR_NOT_SUPPORTED,
+                           _("No application is registered as handling this file"));
+}
+
+/**
+ * g_file_query_default_handler_async:
+ * @file: a #GFile to open
+ * @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
+ *
+ * Async version of g_file_query_default_handler().
+ *
+ * Since: 2.60
+ */
+void
+g_file_query_default_handler_async (GFile              *file,
+                                    int                 io_priority,
+                                    GCancellable       *cancellable,
+                                    GAsyncReadyCallback callback,
+                                    gpointer            user_data)
+{
+  GTask *task;
+  char *uri_scheme;
+
+  task = g_task_new (file, cancellable, callback, user_data);
+  g_task_set_source_tag (task, g_file_query_default_handler_async);
+
+  uri_scheme = g_file_get_uri_scheme (file);
+  if (uri_scheme && uri_scheme[0] != '\0')
+    {
+      GAppInfo *appinfo;
+
+      /* FIXME: The following still uses blocking calls. */
+      appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
+      g_free (uri_scheme);
+
+      if (appinfo != NULL)
+        {
+          g_task_return_pointer (task, appinfo, g_object_unref);
+          return;
+        }
+    }
+
+  g_file_query_info_async (file,
+                           G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                           0,
+                           io_priority,
+                           cancellable,
+                           query_default_handler_query_info_cb,
+                           task);
+}
+
+/**
+ * g_file_query_default_handler_finish:
+ * @file: a #GFile to open
+ * @result: a #GAsyncResult
+ * @error: (nullable): a #GError
+ *
+ * Finishes g_file_query_default_handler_async() operation.
+ *
+ * Returns: (transfer full): a #GAppInfo if the handle was found,
+ *     %NULL if there were errors.
+ *     When you are done with it, release it with g_object_unref()
+ *
+ * Since: 2.60
+ */
+GAppInfo *
+g_file_query_default_handler_finish (GFile        *file,
+                                     GAsyncResult *result,
+                                     GError      **error)
+{
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+  g_return_val_if_fail (g_task_is_valid (result, file), NULL);
+
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
 #define GET_CONTENT_BLOCK_SIZE 8192
 
 /**
diff --git a/gio/gfile.h b/gio/gfile.h
index 4aff644c3..6e25b0de0 100644
--- a/gio/gfile.h
+++ b/gio/gfile.h
@@ -1183,6 +1183,17 @@ GLIB_AVAILABLE_IN_ALL
 GAppInfo *g_file_query_default_handler       (GFile                  *file,
                                              GCancellable           *cancellable,
                                              GError                **error);
+GLIB_AVAILABLE_IN_2_60
+void      g_file_query_default_handler_async (GFile                  *file,
+                                              int                     io_priority,
+                                              GCancellable           *cancellable,
+                                              GAsyncReadyCallback     callback,
+                                              gpointer                user_data);
+GLIB_AVAILABLE_IN_2_60
+GAppInfo *g_file_query_default_handler_finish (GFile                 *file,
+                                               GAsyncResult          *result,
+                                               GError               **error);
+
 GLIB_AVAILABLE_IN_ALL
 gboolean g_file_load_contents                (GFile                  *file,
                                              GCancellable           *cancellable,


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