[grilo] core: Externalize helper functions to create synchronous versions



commit 141c7e1a9db7ef558e12be97dc951fc0ec20367e
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Thu Jul 15 14:22:06 2010 +0200

    core: Externalize helper functions to create synchronous versions
    
    Move out of grl-media-source.c functions and structures used to create
    synchronous functions based on asynchronous ones.
    
    So they can be used by other functions besides GrlMediaSource.

 src/Makefile.am        |    4 +-
 src/grl-media-source.c |  163 +++++++++++++++++++++---------------------------
 src/grl-sync-priv.h    |   37 +++++++++++
 src/grl-sync.c         |   40 ++++++++++++
 4 files changed, 151 insertions(+), 93 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 4b76905..526dba4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,6 +28,7 @@ lib GRL_NAME@_la_SOURCES =					\
 	grl-media-source.c grl-util.c				\
 	grl-multiple.c						\
 	grl-log.c						\
+	grl-sync.c						\
 	grilo.c
 
 data_c_sources =		\
@@ -70,7 +71,8 @@ lib GRL_NAME@inc_HEADERS += $(data_h_headers)
 noinst_HEADERS =			\
 	grl-media-plugin-priv.h		\
 	grl-metadata-source-priv.h	\
-	grl-metadata-key-priv.h
+	grl-metadata-key-priv.h		\
+	grl-sync-priv.h
 
 MAINTAINERCLEANFILES =	\
 	*.in		\
diff --git a/src/grl-media-source.c b/src/grl-media-source.c
index bca0eb4..5d1369f 100644
--- a/src/grl-media-source.c
+++ b/src/grl-media-source.c
@@ -41,6 +41,7 @@
 
 #include "grl-media-source.h"
 #include "grl-metadata-source-priv.h"
+#include "grl-sync-priv.h"
 #include "data/grl-media.h"
 #include "data/grl-media-box.h"
 #include "grl-error.h"
@@ -139,12 +140,6 @@ struct MetadataRelayCb {
   GrlMediaSourceMetadataSpec *spec;
 };
 
-struct OperationAsyncCb {
-  gboolean complete;
-  gpointer data;
-  GError *error;
-};
-
 struct OperationState {
   gboolean cancelled;
   gboolean completed;
@@ -778,29 +773,29 @@ multiple_result_async_cb (GrlMediaSource *source,
                           gpointer user_data,
                           const GError *error)
 {
-  struct OperationAsyncCb *oa = (struct OperationAsyncCb *) user_data;
+  GrlDataSync *ds = (GrlDataSync *) user_data;
 
   g_debug ("multiple_result_async_cb");
 
   if (error) {
-    oa->error = g_error_copy (error);
+    ds->error = g_error_copy (error);
 
     /* Free previous results */
-    g_list_foreach (oa->data, (GFunc) g_object_unref, NULL);
-    g_list_free (oa->data);
+    g_list_foreach (ds->data, (GFunc) g_object_unref, NULL);
+    g_list_free (ds->data);
 
-    oa->data = NULL;
-    oa->complete = TRUE;
+    ds->data = NULL;
+    ds->complete = TRUE;
     return;
   }
 
   if (media) {
-    oa->data = g_list_prepend (oa->data, media);
+    ds->data = g_list_prepend (ds->data, media);
   }
 
   if (remaining == 0) {
-    oa->data = g_list_reverse (oa->data);
-    oa->complete = TRUE;
+    ds->data = g_list_reverse (ds->data);
+    ds->complete = TRUE;
   }
 }
 
@@ -838,16 +833,16 @@ metadata_result_async_cb (GrlMediaSource *source,
                           gpointer user_data,
                           const GError *error)
 {
-  struct OperationAsyncCb *oa = (struct OperationAsyncCb *) user_data;
+  GrlDataSync *ds = (GrlDataSync *) user_data;
 
   g_debug ("metadata_result_async_cb");
 
   if (error) {
-    oa->error = g_error_copy (error);
+    ds->error = g_error_copy (error);
   }
 
-  oa->data = media;
-  oa->complete = TRUE;
+  ds->data = media;
+  ds->complete = TRUE;
 }
 
 static void
@@ -857,15 +852,15 @@ store_async_cb (GrlMediaSource *source,
                 gpointer user_data,
                 const GError *error)
 {
-  struct OperationAsyncCb *oa = (struct OperationAsyncCb *) user_data;
+  GrlDataSync *ds = (GrlDataSync *) user_data;
 
   g_debug ("store_async_cb");
 
   if (error) {
-    oa->error = g_error_copy (error);
+    ds->error = g_error_copy (error);
   }
 
-  oa->complete = TRUE;
+  ds->complete = TRUE;
 }
 
 static void
@@ -874,15 +869,15 @@ remove_async_cb (GrlMediaSource *source,
                  gpointer user_data,
                  const GError *error)
 {
-  struct OperationAsyncCb *oa = (struct OperationAsyncCb *) user_data;
+  GrlDataSync *ds = (GrlDataSync *) user_data;
 
   g_debug ("remove_async_cb");
 
   if (error) {
-    oa->error = g_error_copy (error);
+    ds->error = g_error_copy (error);
   }
 
-  oa->complete = TRUE;
+  ds->complete = TRUE;
 }
 
 static gint
@@ -1184,22 +1179,6 @@ metadata_full_resolution_ctl_cb (GrlMediaSource *source,
   }
 }
 
-static void
-wait_for_async_operation_complete (struct OperationAsyncCb *oa)
-{
-  GMainLoop *ml;
-  GMainContext *mc;
-
-  ml = g_main_loop_new (NULL, TRUE);
-  mc = g_main_loop_get_context (ml);
-
- while (!oa->complete) {
-    g_main_context_iteration (mc, TRUE);
-  }
-
-  g_main_loop_unref (ml);
-}
-
 /* ================ API ================ */
 
 /**
@@ -1363,10 +1342,10 @@ grl_media_source_browse_sync (GrlMediaSource *source,
                               GrlMetadataResolutionFlags flags,
                               GError **error)
 {
-  struct OperationAsyncCb *oa;
+  GrlDataSync *ds;
   GList *result;
 
-  oa = g_slice_new0 (struct OperationAsyncCb);
+  ds = g_slice_new0 (GrlDataSync);
 
   grl_media_source_browse (source,
                            container,
@@ -1375,20 +1354,20 @@ grl_media_source_browse_sync (GrlMediaSource *source,
                            count,
                            flags,
                            multiple_result_async_cb,
-                           oa);
+                           ds);
 
-  wait_for_async_operation_complete (oa);
+  grl_wait_for_async_operation_complete (ds);
 
-  if (oa->error) {
+  if (ds->error) {
     if (error) {
-      *error = oa->error;
+      *error = ds->error;
     } else {
-      g_error_free (oa->error);
+      g_error_free (ds->error);
     }
   }
 
-  result = (GList *) oa->data;
-  g_slice_free (struct OperationAsyncCb, oa);
+  result = (GList *) ds->data;
+  g_slice_free (GrlDataSync, ds);
 
   return result;
 }
@@ -1545,10 +1524,10 @@ grl_media_source_search_sync (GrlMediaSource *source,
                               GrlMetadataResolutionFlags flags,
                               GError **error)
 {
-  struct OperationAsyncCb *oa;
+  GrlDataSync *ds;
   GList *result;
 
-  oa = g_slice_new0 (struct OperationAsyncCb);
+  ds = g_slice_new0 (GrlDataSync);
 
   grl_media_source_search (source,
                            text,
@@ -1557,20 +1536,20 @@ grl_media_source_search_sync (GrlMediaSource *source,
                            count,
                            flags,
                            multiple_result_async_cb,
-                           oa);
+                           ds);
 
-  wait_for_async_operation_complete (oa);
+  grl_wait_for_async_operation_complete (ds);
 
-  if (oa->error) {
+  if (ds->error) {
     if (error) {
-      *error = oa->error;
+      *error = ds->error;
     } else {
-      g_error_free (oa->error);
+      g_error_free (ds->error);
     }
   }
 
-  result = (GList *) oa->data;
-  g_slice_free (struct OperationAsyncCb, oa);
+  result = (GList *) ds->data;
+  g_slice_free (GrlDataSync, ds);
 
   return result;
 }
@@ -1733,10 +1712,10 @@ grl_media_source_query_sync (GrlMediaSource *source,
                              GrlMetadataResolutionFlags flags,
                              GError **error)
 {
-  struct OperationAsyncCb *oa;
+  GrlDataSync *ds;
   GList *result;
 
-  oa = g_slice_new0 (struct OperationAsyncCb);
+  ds = g_slice_new0 (GrlDataSync);
 
   grl_media_source_query (source,
                           query,
@@ -1745,20 +1724,20 @@ grl_media_source_query_sync (GrlMediaSource *source,
                           count,
                           flags,
                           multiple_result_async_cb,
-                          oa);
+                          ds);
 
-  wait_for_async_operation_complete (oa);
+  grl_wait_for_async_operation_complete (ds);
 
-  if (oa->error) {
+  if (ds->error) {
     if (error) {
-      *error = oa->error;
+      *error = ds->error;
     } else {
-      g_error_free (oa->error);
+      g_error_free (ds->error);
     }
   }
 
-  result = (GList *) oa->data;
-  g_slice_free (struct OperationAsyncCb, oa);
+  result = (GList *) ds->data;
+  g_slice_free (GrlDataSync, ds);
 
   return result;
 }
@@ -1894,28 +1873,28 @@ grl_media_source_metadata_sync (GrlMediaSource *source,
                                 GrlMetadataResolutionFlags flags,
                                 GError **error)
 {
-  struct OperationAsyncCb *oa;
+  GrlDataSync *ds;
 
-  oa = g_slice_new0 (struct OperationAsyncCb);
+  ds = g_slice_new0 (GrlDataSync);
 
   grl_media_source_metadata (source,
                              media,
                              keys,
                              flags,
                              metadata_result_async_cb,
-                             oa);
+                             ds);
 
-  wait_for_async_operation_complete (oa);
+  grl_wait_for_async_operation_complete (ds);
 
-  if (oa->error) {
+  if (ds->error) {
     if (error) {
-      *error = oa->error;
+      *error = ds->error;
     } else {
-      g_error_free (oa->error);
+      g_error_free (ds->error);
     }
   }
 
-  g_slice_free (struct OperationAsyncCb, oa);
+  g_slice_free (GrlDataSync, ds);
 
   return media;
 }
@@ -2144,27 +2123,27 @@ grl_media_source_store_sync (GrlMediaSource *source,
                              GrlMedia *media,
                              GError **error)
 {
-  struct OperationAsyncCb *oa;
+  GrlDataSync *ds;
 
-  oa = g_slice_new0 (struct OperationAsyncCb);
+  ds = g_slice_new0 (GrlDataSync);
 
   grl_media_source_store (source,
                           parent,
                           media,
                           store_async_cb,
-                          oa);
+                          ds);
 
-  wait_for_async_operation_complete (oa);
+  grl_wait_for_async_operation_complete (ds);
 
-  if (oa->error) {
+  if (ds->error) {
     if (error) {
-      *error = oa->error;
+      *error = ds->error;
     } else {
-      g_error_free (oa->error);
+      g_error_free (ds->error);
     }
   }
 
-  g_slice_free (struct OperationAsyncCb, oa);
+  g_slice_free (GrlDataSync, ds);
 }
 
 /**
@@ -2236,24 +2215,24 @@ grl_media_source_remove_sync (GrlMediaSource *source,
                               GrlMedia *media,
                               GError **error)
 {
-  struct OperationAsyncCb *oa;
+  GrlDataSync *ds;
 
-  oa = g_slice_new0 (struct OperationAsyncCb);
+  ds = g_slice_new0 (GrlDataSync);
 
   grl_media_source_remove (source,
                            media,
                            remove_async_cb,
-                           oa);
+                           ds);
 
-  wait_for_async_operation_complete (oa);
+  grl_wait_for_async_operation_complete (ds);
 
-  if (oa->error) {
+  if (ds->error) {
     if (error) {
-      *error = oa->error;
+      *error = ds->error;
     } else {
-      g_error_free (oa->error);
+      g_error_free (ds->error);
     }
   }
 
-  g_slice_free (struct OperationAsyncCb, oa);
+  g_slice_free (GrlDataSync, ds);
 }
diff --git a/src/grl-sync-priv.h b/src/grl-sync-priv.h
new file mode 100644
index 0000000..cf6b1cc
--- /dev/null
+++ b/src/grl-sync-priv.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2010 Igalia S.L.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef _GRL_SYNC_PRIV_H_
+#define _GRL_SYNC_PRIV_H_
+
+#include <glib.h>
+
+typedef struct {
+  gboolean complete;
+  gpointer data;
+  GError *error;
+} GrlDataSync;
+
+void
+grl_wait_for_async_operation_complete (GrlDataSync *ds);
+
+#endif /* _GRL_SYNC_PRIV_H_ */
diff --git a/src/grl-sync.c b/src/grl-sync.c
new file mode 100644
index 0000000..55fb6c3
--- /dev/null
+++ b/src/grl-sync.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2010 Igalia S.L.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include "grl-sync-priv.h"
+
+void
+grl_wait_for_async_operation_complete (GrlDataSync *ds)
+{
+  GMainLoop *ml;
+  GMainContext *mc;
+
+  ml = g_main_loop_new (NULL, TRUE);
+  mc = g_main_loop_get_context (ml);
+
+ while (!ds->complete) {
+    g_main_context_iteration (mc, TRUE);
+  }
+
+  g_main_loop_unref (ml);
+}
+



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