[rygel-grilo] Implement ListChildren in client side



commit 8fbf955e99156dbbe9633759d24e65369e8fac6d
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Wed May 12 17:38:33 2010 +0200

    Implement ListChildren in client side

 lib/media-server1-client.c |  109 ++++++++++++++++----------------------------
 lib/media-server1-client.h |   14 +++---
 lib/media-server1-server.c |   31 ++++++------
 lib/media-server1-server.h |   18 ++++----
 src/rygel-grilo.c          |   32 ++++++------
 src/test-client.c          |   16 +++---
 6 files changed, 95 insertions(+), 125 deletions(-)
---
diff --git a/lib/media-server1-client.c b/lib/media-server1-client.c
index 6482c45..da9d921 100644
--- a/lib/media-server1-client.c
+++ b/lib/media-server1-client.c
@@ -27,15 +27,6 @@
 #include "media-server1-private.h"
 #include "media-server1-client.h"
 
-#define DBUS_TYPE_G_ARRAY_OF_STRING                             \
-  (dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING))
-
-#define DBUS_TYPE_PROPERTIES                                    \
-  dbus_g_type_get_collection ("GPtrArray", G_TYPE_VALUE)
-
-#define DBUS_TYPE_CHILDREN                                              \
-  dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_PROPERTIES)
-
 #define IMEDIAOBJECT1_INDEX 0
 #define IMEDIAITEM1_INDEX   1
 
@@ -125,53 +116,22 @@ split_properties_by_interface (gchar **properties)
   return split;
 }
 
-/* Given a GPtrArray result (dbus answer of getting properties), returns a
-   ghashtable with pairs <keys, values> */
-static GHashTable *
-get_properties_table (GPtrArray *result,
-                      const gchar **properties)
-{
-  GHashTable *table;
-  gint i;
-
-  table = g_hash_table_new_full (g_str_hash,
-                                 g_str_equal,
-                                 (GDestroyNotify) g_free,
-                                 (GDestroyNotify) free_gvalue);
-
-  for (i = 0; i < result->len; i++) {
-    g_hash_table_insert (table,
-                         g_strdup (properties[i]),
-                         g_boxed_copy (G_TYPE_VALUE,
-                                       g_ptr_array_index (result, i)));
-  }
-
-  return table;
-}
-
-/* Given a GPtrArray result (dbus answer of getting children), returns a list
-   of children, which in turn are tables with pairs <keys, values>. Note that
-   child id is included in those pairs */
+/* Converts GPtrArray in a GList */
 static GList *
-get_children_list (GPtrArray *result,
-                   const gchar **properties)
+gptrarray_to_glist (GPtrArray *result)
 {
-  GList *children = NULL;
-  GPtrArray *prop_array;
+  GList *list = NULL;
   gint i;
 
-  if (!result || result->len == 0) {
+  if (!result) {
     return NULL;
   }
 
   for (i = 0; i < result->len; i++) {
-    prop_array = g_ptr_array_index (result, i);
-    children = g_list_prepend (children,
-                               get_properties_table (prop_array,
-                                                     properties));
+    list = g_list_prepend (list, g_ptr_array_index (result, i));
   }
 
-  return children;
+  return g_list_reverse (list);
 }
 
 /* Dispose function */
@@ -392,7 +352,7 @@ ms1_client_get_provider_name (MS1Client *client)
 /**
  * ms1_client_get_properties:
  * @client: a #MS1Client
- * @id: media identifier to obtain properties from
+ * @object_path: media identifier to obtain properties from
  * @properties: @NULL-terminated array of properties to request
  * @error: a #GError location to store the error ocurring, or @NULL to ignore
  *
@@ -487,11 +447,11 @@ ms1_client_get_properties (MS1Client *client,
 }
 
 /**
- * ms1_client_get_children:
+ * ms1_client_list_children:
  * @client: a #MS1Client
- * @id: container identifier to get children from
+ * @object_path: container identifier to get children from
  * @offset: number of children to skip
- * @max_count: maximum number of children to return, or -1 for no limit
+ * @max_count: maximum number of children to return, or 0 for no limit
  * @properties: @NULL-terminated array of properties to request for each child
  * @error: a #GError location to store the error ocurring, or @NULL to ignore
  *
@@ -502,32 +462,41 @@ ms1_client_get_properties (MS1Client *client,
  * (g_hash_table_unref()) and finally the list itself (g_list_free())
  **/
 GList *
-ms1_client_get_children (MS1Client *client,
-                         const gchar *id,
-                         guint offset,
-                         gint max_count,
-                         const gchar **properties,
-                         GError **error)
+ms1_client_list_children (MS1Client *client,
+                          const gchar *object_path,
+                          guint offset,
+                          guint max_count,
+                          const gchar **properties,
+                          GError **error)
 {
-  GPtrArray *result = NULL;
+  DBusGProxy *gproxy;
   GList *children = NULL;
+  GPtrArray *result = NULL;
 
   g_return_val_if_fail (MS1_IS_CLIENT (client), NULL);
+  g_return_val_if_fail (properties, NULL);
 
-  /* if (!org_gnome_UPnP_MediaServer1_get_children (client->priv->proxy_provider, */
-  /*                                                id, */
-  /*                                                offset, */
-  /*                                                max_count, */
-  /*                                                properties, */
-  /*                                                &result, */
-  /*                                                error)) { */
-  /*   return NULL; */
-  /* } */
-
-
-  children = get_children_list (result, properties);
+  gproxy = dbus_g_proxy_new_for_name (client->priv->bus,
+                                      client->priv->fullname,
+                                      object_path,
+                                      "org.gnome.UPnP.MediaContainer1");
+
+  if (dbus_g_proxy_call (gproxy,
+                         "ListChildren", error,
+                         G_TYPE_UINT, offset,
+                         G_TYPE_UINT, max_count,
+                         G_TYPE_STRV, properties,
+                         G_TYPE_INVALID,
+                         dbus_g_type_get_collection ("GPtrArray",
+                                                     dbus_g_type_get_map ("GHashTable",
+                                                                          G_TYPE_STRING,
+                                                                          G_TYPE_VALUE)), &result,
+                         G_TYPE_INVALID)) {
+    children = gptrarray_to_glist (result);
+    g_ptr_array_free (result, TRUE);
+  }
 
-  g_boxed_free (DBUS_TYPE_CHILDREN, result);
+  g_object_unref (gproxy);
 
   return children;
 }
diff --git a/lib/media-server1-client.h b/lib/media-server1-client.h
index ffab42e..fb4a3b6 100644
--- a/lib/media-server1-client.h
+++ b/lib/media-server1-client.h
@@ -87,16 +87,16 @@ MS1Client *ms1_client_new (const gchar *provider);
 const gchar *ms1_client_get_provider_name (MS1Client *client);
 
 GHashTable *ms1_client_get_properties (MS1Client *client,
-                                       const gchar *id,
+                                       const gchar *object_path,
                                        gchar **properties,
                                        GError **error);
 
-GList *ms1_client_get_children (MS1Client *client,
-                                const gchar *id,
-                                guint offset,
-                                gint max_count,
-                                const gchar **properties,
-                                GError **error);
+GList *ms1_client_list_children (MS1Client *client,
+                                 const gchar *object_path,
+                                 guint offset,
+                                 guint max_count,
+                                 const gchar **properties,
+                                 GError **error);
 
 const gchar *ms1_client_get_root_path (MS1Client *client);
 
diff --git a/lib/media-server1-server.c b/lib/media-server1-server.c
index 5078458..4261efe 100644
--- a/lib/media-server1-server.c
+++ b/lib/media-server1-server.c
@@ -49,13 +49,14 @@ enum {
  * Private MS1Server structure
  *   name: provider name
  *   data: holds stuff for owner
- *   get_children: function to get children
+ *   list_children: function to get children
+ *   search_objects: function to search objects
  *   get_properties: function to get properties
  */
 struct _MS1ServerPrivate {
   gchar *name;
   gpointer *data;
-  GetChildrenFunc get_children;
+  ListChildrenFunc list_children;
   SearchObjectsFunc search_objects;
   GetPropertiesFunc get_properties;
 };
@@ -754,20 +755,20 @@ handle_list_children_message (DBusConnection *c,
                            DBUS_TYPE_UINT32, &max_count,
                            DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &filter, &nitems,
                            DBUS_TYPE_INVALID);
-    if (!server->priv->get_children || nitems == 0) {
+    if (!server->priv->list_children || nitems == 0) {
       children = NULL;
     } else {
       id = get_id_from_message (m);
       if (!id) {
         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
       }
-      children = server->priv->get_children (server,
-                                             id,
-                                             offset,
-                                             max_count? max_count: G_MAXUINT,
-                                             (const gchar **) filter,
-                                             server->priv->data,
-                                             NULL);
+      children = server->priv->list_children (server,
+                                              id,
+                                              offset,
+                                              max_count? max_count: G_MAXUINT,
+                                              (const gchar **) filter,
+                                              server->priv->data,
+                                              NULL);
       g_free (id);
       dbus_free_string_array (filter);
     }
@@ -1096,19 +1097,19 @@ ms1_server_set_get_properties_func (MS1Server *server,
 }
 
 /**
- * ms1_server_set_get_children_func:
+ * ms1_server_set_list_children_func:
  * @server: a #MS1Server
- * @get_children_func: user-defined function to request children
+ * @list_children_func: user-defined function to request children
  *
  * Defines which function must be used when requesting children.
  **/
 void
-ms1_server_set_get_children_func (MS1Server *server,
-                                  GetChildrenFunc get_children_func)
+ms1_server_set_list_children_func (MS1Server *server,
+                                   ListChildrenFunc list_children_func)
 {
   g_return_if_fail (MS1_IS_SERVER (server));
 
-  server->priv->get_children = get_children_func;
+  server->priv->list_children = list_children_func;
 }
 
 /**
diff --git a/lib/media-server1-server.h b/lib/media-server1-server.h
index d76d893..a1e3daf 100644
--- a/lib/media-server1-server.h
+++ b/lib/media-server1-server.h
@@ -81,13 +81,13 @@ typedef GHashTable * (*GetPropertiesFunc) (MS1Server *server,
                                            gpointer data,
                                            GError **error);
 
-typedef GList * (*GetChildrenFunc) (MS1Server *server,
-                                    const gchar *id,
-                                    guint offset,
-                                    guint max_count,
-                                    const gchar **properties,
-                                    gpointer data,
-                                    GError **error);
+typedef GList * (*ListChildrenFunc) (MS1Server *server,
+                                     const gchar *id,
+                                     guint offset,
+                                     guint max_count,
+                                     const gchar **properties,
+                                     gpointer data,
+                                     GError **error);
 
 typedef GList * (*SearchObjectsFunc) (MS1Server *server,
                                       const gchar *id,
@@ -106,8 +106,8 @@ MS1Server *ms1_server_new (const gchar *name,
 void ms1_server_set_get_properties_func (MS1Server *server,
                                          GetPropertiesFunc get_properties_func);
 
-void ms1_server_set_get_children_func (MS1Server *server,
-                                       GetChildrenFunc get_children_func);
+void ms1_server_set_list_children_func (MS1Server *server,
+                                        ListChildrenFunc list_children_func);
 
 void ms1_server_set_search_objects_func (MS1Server *server,
                                          SearchObjectsFunc search_objects_func);
diff --git a/src/rygel-grilo.c b/src/rygel-grilo.c
index c907cc1..f2f8554 100644
--- a/src/rygel-grilo.c
+++ b/src/rygel-grilo.c
@@ -85,13 +85,13 @@ get_properties_cb (MS1Server *server,
                    GError **error);
 
 static GList *
-get_children_cb (MS1Server *server,
-                 const gchar *id,
-                 guint offset,
-                 guint max_count,
-                 const gchar **properties,
-                 gpointer data,
-                 GError **error);
+list_children_cb (MS1Server *server,
+                  const gchar *id,
+                  guint offset,
+                  guint max_count,
+                  const gchar **properties,
+                  gpointer data,
+                  GError **error);
 
 /* Fix invalid characters so string can be used in a dbus name */
 static void
@@ -261,7 +261,7 @@ get_items_and_containers (MS1Server *server,
   }
 
   children =
-    get_children_cb (server, container_id, 0, (guint) limit, properties, source, NULL);
+    list_children_cb (server, container_id, 0, (guint) limit, properties, source, NULL);
 
   /* Separate containers from items */
   for (child = children; child; child = g_list_next (child)) {
@@ -672,13 +672,13 @@ get_properties_cb (MS1Server *server,
 }
 
 static GList *
-get_children_cb (MS1Server *server,
-                 const gchar *id,
-                 guint offset,
-                 guint max_count,
-                 const gchar **properties,
-                 gpointer data,
-                 GError **error)
+list_children_cb (MS1Server *server,
+                  const gchar *id,
+                  guint offset,
+                  guint max_count,
+                  const gchar **properties,
+                  gpointer data,
+                  GError **error)
 {
   GList *children;
   GrlMedia *media;
@@ -767,7 +767,7 @@ source_added_cb (GrlPluginRegistry *registry, gpointer user_data)
       g_free (source_id);
     } else {
       ms1_server_set_get_properties_func (server, get_properties_cb);
-      ms1_server_set_get_children_func (server, get_children_cb);
+      ms1_server_set_list_children_func (server, list_children_cb);
       /* Save reference */
       if (!dups) {
         providers_names = g_list_prepend (providers_names,
diff --git a/src/test-client.c b/src/test-client.c
index 1d6d06d..dd0a621 100644
--- a/src/test-client.c
+++ b/src/test-client.c
@@ -87,12 +87,12 @@ test_children ()
       return;
     }
 
-    children  = ms1_client_get_children (client,
-                                         ms1_client_get_root_path (client),
-                                         0,
-                                         -1,
-                                         properties,
-                                         &error);
+    children  = ms1_client_list_children (client,
+                                          ms1_client_get_root_path (client),
+                                          0,
+                                          10,
+                                          properties,
+                                          &error);
 
     g_print ("\n* Provider '%s'\n", *provider);
     if (!children) {
@@ -204,8 +204,8 @@ int main (int argc, char **argv)
 
   g_type_init ();
 
-  if (1) test_properties ();
-  if (0) test_children ();
+  if (0) test_properties ();
+  if (1) test_children ();
   if (0) test_provider_free ();
   if (0) test_dynamic_providers ();
 



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