[rygel-grilo] Implement get_children() function



commit 9f559e3525a33ff6a8b464308b4e3b9505f18bd9
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Tue Apr 13 11:28:08 2010 +0200

    Implement get_children() function
    
    Wrapper around dbus function to get children of an id.

 lib/media-server2-client.c |   99 ++++++++++++++++++++++++++++++++++++++------
 lib/media-server2-client.h |   12 +++++-
 src/test-client.c          |   35 +++++++++++++++-
 3 files changed, 131 insertions(+), 15 deletions(-)
---
diff --git a/lib/media-server2-client.c b/lib/media-server2-client.c
index baca8f3..12c5c0b 100644
--- a/lib/media-server2-client.c
+++ b/lib/media-server2-client.c
@@ -34,8 +34,14 @@
 #define ENTRY_POINT_IFACE "/org/gnome/UPnP/MediaServer2/"
 #define ENTRY_POINT_NAME  "org.gnome.UPnP.MediaServer2."
 
-#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_map ("GHashTable",                    \
+                       G_TYPE_STRING,                   \
+                       DBUS_TYPE_PROPERTIES)            \
 
 #define MS2_CLIENT_GET_PRIVATE(o)                                    \
   G_TYPE_INSTANCE_GET_PRIVATE((o), MS2_TYPE_CLIENT, MS2ClientPrivate)
@@ -167,6 +173,37 @@ free_gvalue (GValue *v)
   g_free (v);
 }
 
+static GHashTable *
+get_properties_table (const gchar *id,
+                      const gchar **properties,
+                      GPtrArray *result)
+{
+  GHashTable *table;
+  GValue *id_value;
+  gint i;
+
+  table = g_hash_table_new_full (g_str_hash,
+                                 g_str_equal,
+                                 (GDestroyNotify) g_free,
+                                 (GDestroyNotify) free_gvalue);
+
+  id_value = g_new0 (GValue, 1);
+  g_value_init (id_value, G_TYPE_STRING);
+  g_value_set_string (id_value, id);
+  g_hash_table_insert (table,
+                       g_strdup (MS2_PROP_ID),
+                       id_value);
+
+  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;
+}
+
 GHashTable *
 ms2_client_get_properties (MS2Client *client,
                            const gchar *id,
@@ -175,7 +212,6 @@ ms2_client_get_properties (MS2Client *client,
 {
   GHashTable *prop_result;
   GPtrArray *result = NULL;
-  gint i;
 
   g_return_val_if_fail (MS2_IS_CLIENT (client), NULL);
 
@@ -187,17 +223,54 @@ ms2_client_get_properties (MS2Client *client,
     return NULL;
   }
 
-  prop_result = 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 (prop_result,
-                         g_strdup (properties[i]),
-                         g_ptr_array_index (result, i));
+  prop_result = get_properties_table (id, properties, result);
+  g_boxed_free (DBUS_TYPE_PROPERTIES, result);
+
+  return prop_result;
+}
+
+GList *
+ms2_client_get_children (MS2Client *client,
+                         const gchar *id,
+                         guint offset,
+                         gint max_count,
+                         const gchar **properties,
+                         GError **error)
+{
+  GHashTable *result;
+  GList *child_id;
+  GList *children = NULL;
+  GList *children_id;
+  GPtrArray *prop_array;
+
+  g_return_val_if_fail (MS2_IS_CLIENT (client), NULL);
+
+  if (!org_gnome_UPnP_MediaServer2_get_children (client->priv->proxy_provider,
+                                                 id,
+                                                 offset,
+                                                 max_count,
+                                                 properties,
+                                                 &result,
+                                                 error)) {
+    return NULL;
   }
 
-  g_ptr_array_free (result, TRUE);
+  if (!result || g_hash_table_size (result) == 0) {
+    return NULL;
+  }
 
-  return prop_result;
+  children_id = g_hash_table_get_keys (result);
+
+  for (child_id = children_id; child_id; child_id = g_list_next (child_id)) {
+    prop_array = g_hash_table_lookup (result, child_id->data);
+    children = g_list_prepend (children,
+                               get_properties_table (child_id->data,
+                                                     properties,
+                                                     prop_array));
+  }
+
+  g_list_free (children_id);
+  g_boxed_free (DBUS_TYPE_CHILDREN, result);
+
+  return children;
 }
diff --git a/lib/media-server2-client.h b/lib/media-server2-client.h
index b4801d6..5816ea7 100644
--- a/lib/media-server2-client.h
+++ b/lib/media-server2-client.h
@@ -78,6 +78,16 @@ gchar **ms2_client_get_providers (void);
 
 MS2Client *ms2_client_new (const gchar *provider);
 
-GHashTable *ms2_client_get_properties (MS2Client *client, const gchar *id, const gchar **properties, GError **error);
+GHashTable *ms2_client_get_properties (MS2Client *client,
+                                       const gchar *id,
+                                       const gchar **properties,
+                                       GError **error);
+
+GList *ms2_client_get_children (MS2Client *client,
+                                const gchar *id,
+                                guint offset,
+                                gint max_count,
+                                const gchar **properties,
+                                GError **error);
 
 #endif /* _MEDIA_SERVER2_CLIENT_H_ */
diff --git a/src/test-client.c b/src/test-client.c
index b836ae7..ca9847d 100644
--- a/src/test-client.c
+++ b/src/test-client.c
@@ -6,6 +6,8 @@ int main (int argc, char **argv)
 {
   GError *error = NULL;
   GHashTable *result;
+  GList *children;
+  GList *child;
   GValue *v;
   MS2Client *client;
   const gchar **p;
@@ -50,7 +52,38 @@ int main (int argc, char **argv)
         g_print ("\t* '%s' value: ---\n", *p);
       }
     }
+    g_hash_table_unref (result);
+    g_object_unref (client);
   }
 
-  g_hash_table_unref (result);
+  g_print ("\n================================================================================\n");
+
+  for (provider = providers; *provider; provider ++) {
+    client = ms2_client_new (*provider);
+
+    if (!client) {
+      g_printerr ("Unable to create a client\n");
+      return 0;
+    }
+
+    children  = ms2_client_get_children (client, MS2_ROOT, 0, -1, properties, &error);
+
+    if (!result) {
+      g_print ("Did not get any child, %s\n", error->message);
+      return 0;
+    }
+
+    g_print ("\n* Provider '%s'\n", *provider);
+    for (child = children; child; child = g_list_next (child)) {
+      g_print ("\t* '%s', '%s'\n",
+               g_value_get_string(g_hash_table_lookup (child->data, MS2_PROP_ID)),
+               g_value_get_string(g_hash_table_lookup (child->data, MS2_PROP_DISPLAY_NAME)));
+    }
+
+    g_list_foreach (children, (GFunc) g_hash_table_unref, NULL);
+    g_list_free (children);
+    g_object_unref (client);
+  }
+
+  g_strfreev (providers);
 }



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