[PATCH plugins 10/14] shoutcast: add GrlNet



Signed-off-by: Víctor Manuel Jáquez Leal <vjaquez igalia com>
---
 configure.ac                  |    6 ++--
 src/shoutcast/Makefile.am     |    4 +-
 src/shoutcast/grl-shoutcast.c |   62 +++++++++++++++++++++++++----------------
 3 files changed, 43 insertions(+), 29 deletions(-)

diff --git a/configure.ac b/configure.ac
index 32227cc..b78780c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -490,8 +490,8 @@ AC_ARG_ENABLE(shoutcast,
         [
                 case "$enableval" in
                      yes)
-                        if test "x$HAVE_GIO" = "xno"; then
-                           AC_MSG_ERROR([GIO not found, install it or use --disable-shoutcast])
+                        if test "x$HAVE_GRLNET" = "xno"; then
+                           AC_MSG_ERROR([grilo-net not found, install it or use --disable-shoutcast])
                         fi
                         if test "x$HAVE_XML" = "xno"; then
                            AC_MSG_ERROR([xml2 not found, install it or use --disable-shoutcast])
@@ -500,7 +500,7 @@ AC_ARG_ENABLE(shoutcast,
                 esac
         ],
         [
-                if test "x$HAVE_GIO" = "xyes" -a "x$HAVE_XML" = "xyes"; then
+                if test "x$HAVE_GRLNET" = "xyes" -a "x$HAVE_XML" = "xyes"; then
                    enable_shoutcast=yes
                 else
                    enable_shoutcast=no
diff --git a/src/shoutcast/Makefile.am b/src/shoutcast/Makefile.am
index 470b386..f32eaee 100644
--- a/src/shoutcast/Makefile.am
+++ b/src/shoutcast/Makefile.am
@@ -9,12 +9,12 @@ lib_LTLIBRARIES		 = libgrlshoutcast.la
 
 libgrlshoutcast_la_CFLAGS =	\
 	$(DEPS_CFLAGS)		\
-	$(GIO_CFLAGS)		\
+	$(GRLNET_CFLAGS)	\
 	$(XML_CFLAGS)
 
 libgrlshoutcast_la_LIBADD =	\
 	$(DEPS_LIBS)		\
-	$(GIO_LIBS)		\
+	$(GRLNET_LIBS)		\
 	$(XML_LIBS)
 
 libgrlshoutcast_la_SOURCES	 = grl-shoutcast.c grl-shoutcast.h
diff --git a/src/shoutcast/grl-shoutcast.c b/src/shoutcast/grl-shoutcast.c
index f68d244..6782f3d 100644
--- a/src/shoutcast/grl-shoutcast.c
+++ b/src/shoutcast/grl-shoutcast.c
@@ -27,7 +27,7 @@
 #endif
 
 #include <grilo.h>
-#include <gio/gio.h>
+#include <net/grl-net.h>
 #include <libxml/parser.h>
 #include <libxml/xmlmemory.h>
 #include <libxml/xpath.h>
@@ -83,6 +83,8 @@ typedef struct {
   xmlNodePtr xml_entries;
 } OperationData;
 
+static GrlNetWc *wc = NULL;
+static GCancellable *cancellable;
 static gchar *cached_page = NULL;
 static gboolean cached_page_expired = TRUE;
 
@@ -108,6 +110,8 @@ static void grl_shoutcast_source_cancel (GrlMediaSource *source,
 
 static void read_url_async (const gchar *url, OperationData *op_data);
 
+static void grl_shoutcast_source_finalize (GObject *object);
+
 /* =================== SHOUTcast Plugin  =============== */
 
 gboolean
@@ -148,11 +152,13 @@ grl_shoutcast_source_class_init (GrlShoutcastSourceClass * klass)
 {
   GrlMediaSourceClass *source_class = GRL_MEDIA_SOURCE_CLASS (klass);
   GrlMetadataSourceClass *metadata_class = GRL_METADATA_SOURCE_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   source_class->metadata = grl_shoutcast_source_metadata;
   source_class->browse = grl_shoutcast_source_browse;
   source_class->search = grl_shoutcast_source_search;
   source_class->cancel = grl_shoutcast_source_cancel;
   metadata_class->supported_keys = grl_shoutcast_source_supported_keys;
+  gobject_class->finalize = grl_shoutcast_source_finalize;
 }
 
 static void
@@ -162,6 +168,18 @@ grl_shoutcast_source_init (GrlShoutcastSource *source)
 
 G_DEFINE_TYPE (GrlShoutcastSource, grl_shoutcast_source, GRL_TYPE_MEDIA_SOURCE);
 
+static void
+grl_shoutcast_source_finalize (GObject *object)
+{
+  if (wc && GRL_IS_NET_WC (wc))
+    g_object_unref (wc);
+
+  if (cancellable && G_IS_CANCELLABLE (cancellable))
+    g_cancellable_cancel (cancellable);
+
+  G_OBJECT_CLASS (grl_shoutcast_source_parent_class)->finalize (object);
+}
+
 /* ======================= Private ==================== */
 
 static gint
@@ -468,32 +486,31 @@ read_done_cb (GObject *source_object,
               gpointer user_data)
 {
   GError *error = NULL;
-  GError *vfs_error = NULL;
+  GError *wc_error = NULL;
   OperationData *op_data = (OperationData *) user_data;
   gboolean cache;
   gchar *content = NULL;
 
-  if (!g_file_load_contents_finish (G_FILE (source_object),
-                                    res,
-                                    &content,
-                                    NULL,
-                                    NULL,
-                                    &vfs_error)) {
+  if (!grl_net_wc_request_finish (GRL_NET_WC (source_object),
+                            res,
+                            &content,
+                            NULL,
+                            &wc_error)) {
     error = g_error_new (GRL_CORE_ERROR,
                          op_data->error_code,
                          "Failed to connect SHOUTcast: '%s'",
-                         vfs_error->message);
+                         wc_error->message);
     op_data->result_cb (op_data->source,
                         op_data->operation_id,
                         NULL,
                         0,
                         op_data->user_data,
                         error);
-    g_error_free (vfs_error);
+    g_error_free (wc_error);
     g_error_free (error);
     g_slice_free (OperationData, op_data);
 
-    goto end_func;
+    return;
   }
 
   cache = op_data->cache;
@@ -501,15 +518,10 @@ read_done_cb (GObject *source_object,
   if (cache && cached_page_expired) {
     GRL_DEBUG ("Caching page");
     g_free (cached_page);
-    cached_page = content;
+    cached_page = g_strdup (content);
     cached_page_expired = FALSE;
     g_timeout_add_seconds (EXPIRE_CACHE_TIMEOUT, expire_cache, NULL);
-  } else {
-    g_free (content);
   }
-
-end_func:
-  g_object_unref (source_object);
 }
 
 static gboolean
@@ -522,17 +534,15 @@ read_cached_page (OperationData *op_data)
 static void
 read_url_async (const gchar *url, OperationData *op_data)
 {
-  GVfs *vfs;
-  GFile *uri;
-
   if (op_data->cache && !cached_page_expired) {
     GRL_DEBUG ("Using cached page");
     g_idle_add ((GSourceFunc) read_cached_page, op_data);
   } else {
-    vfs = g_vfs_get_default ();
-    GRL_DEBUG ("Opening '%s'", url);
-    uri = g_vfs_get_file_for_uri (vfs, url);
-    g_file_load_contents_async (uri, NULL, read_done_cb, op_data);
+    if (!wc)
+      wc = grl_net_wc_new ();
+
+    cancellable = g_cancellable_new ();
+    grl_net_wc_request_async (wc, url, cancellable, read_done_cb, op_data);
   }
 }
 
@@ -707,6 +717,10 @@ grl_shoutcast_source_cancel (GrlMediaSource *source, guint operation_id)
 
   GRL_DEBUG ("grl_shoutcast_source_cancel");
 
+  if (cancellable && G_IS_CANCELLABLE (cancellable))
+    g_cancellable_cancel (cancellable);
+  cancellable = NULL;
+
   op_data = (OperationData *) grl_media_source_get_operation_data (source, operation_id);
 
   if (op_data) {
-- 
1.7.1



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