[PATCH 2/2] core: Add synchronous version of grl_media_source_browse() function



---
 src/grl-media-source.c |   89 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/grl-media-source.h |    8 ++++
 2 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/src/grl-media-source.c b/src/grl-media-source.c
index 664761d..55aa4ab 100644
--- a/src/grl-media-source.c
+++ b/src/grl-media-source.c
@@ -766,6 +766,40 @@ browse_result_relay_cb (GrlMediaSource *source,
 }
 
 static void
+browse_result_async_cb (GrlMediaSource *source,
+                        guint browse_id,
+                        GrlMedia *media,
+                        guint remaining,
+                        gpointer user_data,
+                        const GError *error)
+{
+  struct OperationAsyncCb *oa = (struct OperationAsyncCb *) user_data;
+
+  g_debug ("browse_result_async_cb");
+
+  if (error) {
+    oa->error = g_error_copy (error);
+
+    /* Free previous results */
+    g_list_foreach (oa->data, (GFunc) g_object_unref, NULL);
+    g_list_free (oa->data);
+
+    oa->data = NULL;
+    oa->complete = TRUE;
+    return;
+  }
+
+  if (media) {
+    oa->data = g_list_prepend (oa->data, media);
+  }
+
+  if (remaining == 0) {
+    oa->data = g_list_reverse (oa->data);
+    oa->complete = TRUE;
+  }
+}
+
+static void
 metadata_result_relay_cb (GrlMediaSource *source,
 			  GrlMedia *media,
 			  gpointer user_data,
@@ -1265,6 +1299,61 @@ grl_media_source_browse (GrlMediaSource *source,
 }
 
 /**
+ * grl_media_source_browse_sync:
+ * @source: a media source
+ * @container: a container of data transfer objects
+ * @keys: the list of #GrlKeyID to request
+ * @skip: the number if elements to skip in the browse operation
+ * @count: the number of elements to retrieve in the browse operation
+ * @flags: the resolution mode
+ * @error: a #GError, or @NULL
+ *
+ * Browse from @skip, a @count number of media elements through an available list.
+ *
+ * This method is synchronous.
+ *
+ * Returns: a list with #GrlMedia elements
+ */
+GList *
+grl_media_source_browse_sync (GrlMediaSource *source,
+                              GrlMedia *container,
+                              const GList *keys,
+                              guint skip,
+                              guint count,
+                              GrlMetadataResolutionFlags flags,
+                              GError **error)
+{
+  struct OperationAsyncCb *oa;
+  GList *result;
+
+  oa = g_slice_new0 (struct OperationAsyncCb);
+
+  grl_media_source_browse (source,
+                           container,
+                           keys,
+                           skip,
+                           count,
+                           flags,
+                           browse_result_async_cb,
+                           oa);
+
+  wait_for_async_operation_complete (oa);
+
+  if (oa->error) {
+    if (error) {
+      *error = oa->error;
+    } else {
+      g_error_free (oa->error);
+    }
+  }
+
+  result = (GList *) oa->data;
+  g_slice_free (struct OperationAsyncCb, oa);
+
+  return result;
+}
+
+/**
  * grl_media_source_search:
  * @source: a media source
  * @text: the text to search
diff --git a/src/grl-media-source.h b/src/grl-media-source.h
index 309108d..503a52a 100644
--- a/src/grl-media-source.h
+++ b/src/grl-media-source.h
@@ -335,6 +335,14 @@ guint grl_media_source_browse (GrlMediaSource *source,
                                GrlMediaSourceResultCb callback,
                                gpointer user_data);
 
+GList *grl_media_source_browse_sync (GrlMediaSource *source,
+                                     GrlMedia *container,
+                                     const GList *keys,
+                                     guint skip,
+                                     guint count,
+                                     GrlMetadataResolutionFlags flags,
+                                     GError **error);
+
 guint grl_media_source_search (GrlMediaSource *source,
                                const gchar *text,
                                const GList *keys,
-- 
1.7.0.4



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