[grilo] [core] resolve_async implementation



commit baa2a69b2001e5599c279e2889077bb868a0cc8a
Author: Víctor Manuel Jáquez Leal <vjaquez igalia com>
Date:   Mon Apr 5 16:06:17 2010 +0200

    [core] resolve_async implementation
    
    resolve_async is a reimplementation of the resolve method in
    GrlMetadataSource using the GAsyncResult mechanism.

 configure.ac              |    3 +-
 src/Makefile.am           |    2 +-
 src/grl-metadata-source.c |  105 +++++++++++++++++++++++++++++++++++++++++++++
 src/grl-metadata-source.h |   17 +++++++
 4 files changed, 125 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 47d0841..636c644 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,7 +143,8 @@ GOBJECT_INTROSPECTION_CHECK([0.6.7])
 
 PKG_CHECK_MODULES(DEPS, glib-2.0 \
 			gobject-2.0 \
-			gmodule-2.0)
+			gmodule-2.0 \
+			gio-2.0)
 
 
 AC_SUBST(DEPS_CFLAGS)
diff --git a/src/Makefile.am b/src/Makefile.am
index 34d768b..4403eba 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -84,7 +84,7 @@ introspection_sources = \
 	$(lib GRL_NAME@_la_SOURCES)
 
 Grilo- GRL_MAJORMINOR@.gir: lib GRL_NAME@.la
-Grilo_0_1_gir_INCLUDES = GObject-2.0
+Grilo_0_1_gir_INCLUDES = GObject-2.0 Gio-2.0
 Grilo_0_1_gir_CFLAGS = $(lib GRL_NAME@_la_CFLAGS)
 Grilo_0_1_gir_LIBS = lib GRL_NAME@.la
 Grilo_0_1_gir_FILES = $(addprefix $(srcdir)/,$(introspection_sources))
diff --git a/src/grl-metadata-source.c b/src/grl-metadata-source.c
index da9705d..238c3a1 100644
--- a/src/grl-metadata-source.c
+++ b/src/grl-metadata-source.c
@@ -595,6 +595,111 @@ grl_metadata_source_resolve (GrlMetadataSource *source,
   g_idle_add (resolve_idle, rs);
 }
 
+typedef struct {
+  GrlMetadataSource *source;
+  GList *keys;
+  GrlMedia *media;
+  GrlMetadataResolutionFlags flags;
+} ResolveAsyncData;
+
+static void
+resolve_async_data_free (gpointer user_data)
+{
+  ResolveAsyncData *data = (ResolveAsyncData *) user_data;
+  g_object_unref (data->source);
+  g_object_unref (data->media);
+  g_list_free (data->keys);
+  g_slice_free (ResolveAsyncData, data);
+}
+
+static gboolean
+resolve_async_idle (gpointer user_data)
+{
+  g_debug (G_STRFUNC);
+  GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
+
+  ResolveAsyncData *data = g_simple_async_result_get_op_res_gpointer (res);
+  GRL_METADATA_SOURCE_GET_CLASS (data->source)->resolve_async (data->source,
+                                                               data->keys,
+                                                               data->media,
+                                                               data->flags);
+
+  g_simple_async_result_complete (res);
+  g_object_unref (res);
+
+  return FALSE;
+}
+
+GrlMedia *
+grl_metadata_source_resolve_finish (GrlMetadataSource *source,
+                                    GAsyncResult *res,
+                                    GError **error)
+{
+  g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) ==
+                  grl_metadata_source_resolve_async);
+
+  ResolveAsyncData *data =
+    g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
+  return data->media;
+}
+
+/**
+ * grl_metadata_source_resolve_async:
+ * @source: a metadata source
+ * @keys: the #GList of #GrlKeyID to retrieve
+ * @media: Transfer object where all the metadata is stored.
+ * @flags: bitwise mask of #GrlMetadataResolutionFlags with the resolution
+ * strategy
+ * @callback: the callback to call when the request is satisfied
+ * @user_data: data to pass to @callback function
+ *
+ * This is the main method of the #GrlMetadataSource class. It will fetch the
+ * metadata of the requested keys.
+ *
+ * This function is asynchronic and uses the Glib's main loop.
+ */
+void
+grl_metadata_source_resolve_async (GrlMetadataSource *source,
+                                   const GList *keys,
+                                   GrlMedia *media,
+                                   GrlMetadataResolutionFlags flags,
+                                   GAsyncReadyCallback callback,
+                                   gpointer user_data)
+{
+  GList *_keys;
+  GSimpleAsyncResult *res;
+  ResolveAsyncData *data;
+
+  g_debug (G_STRFUNC);
+
+  g_return_if_fail (GRL_IS_METADATA_SOURCE (source));
+  g_return_if_fail (callback != NULL);
+  g_return_if_fail (media != NULL);
+  g_return_if_fail (grl_metadata_source_supported_operations (source) &
+		    GRL_OP_RESOLVE);
+
+  _keys = g_list_copy ((GList *) keys);
+
+  if (flags & GRL_RESOLVE_FAST_ONLY) {
+    grl_metadata_source_filter_slow (source, &_keys, FALSE);
+  }
+
+  data = g_slice_new0 (ResolveAsyncData);
+  {
+    data->source = g_object_ref (source);
+    data->keys = _keys;
+    data->media = g_object_ref (media);
+    data->flags = flags;
+  }
+
+  res = g_simple_async_result_new (G_OBJECT (source), callback, user_data,
+                                   grl_metadata_source_resolve_async);
+
+  g_simple_async_result_set_op_res_gpointer (res, data, resolve_async_data_free);
+
+  g_idle_add (resolve_async_idle, res);
+}
+
 /**
  * grl_metadata_source_filter_supported:
  * @source: a metadata source
diff --git a/src/grl-metadata-source.h b/src/grl-metadata-source.h
index f0a482e..f7b11cc 100644
--- a/src/grl-metadata-source.h
+++ b/src/grl-metadata-source.h
@@ -33,6 +33,7 @@
 
 #include <glib.h>
 #include <glib-object.h>
+#include <gio/gio.h>
 
 /* Macros */
 
@@ -245,6 +246,11 @@ struct _GrlMetadataSourceClass {
   void (*resolve) (GrlMetadataSource *source,
 		   GrlMetadataSourceResolveSpec *rs);
 
+  void (*resolve_async) (GrlMetadataSource *source,
+                         const GList *keys,
+                         GrlMedia *media,
+                         GrlMetadataResolutionFlags flags);
+
   void (*set_metadata) (GrlMetadataSource *source,
 			GrlMetadataSourceSetMetadataSpec *sms);
 };
@@ -283,6 +289,17 @@ void grl_metadata_source_resolve (GrlMetadataSource *source,
                                   GrlMetadataSourceResolveCb callback,
                                   gpointer user_data);
 
+GrlMedia *grl_metadata_source_resolve_finish (GrlMetadataSource *source,
+                                              GAsyncResult *res,
+                                              GError **error);
+
+void grl_metadata_source_resolve_async (GrlMetadataSource *source,
+                                        const GList *keys,
+                                        GrlMedia *media,
+                                        GrlMetadataResolutionFlags flags,
+                                        GAsyncReadyCallback callback,
+                                        gpointer user_data);
+
 void grl_metadata_source_set_metadata (GrlMetadataSource *source,
 				       GrlMedia *media,
 				       GList *keys,



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