edbus: Add dictionary iterator
authorJosé Roberto de Souza <zehortigoza@profusion.mobi>
Tue, 16 Oct 2012 22:35:44 +0000 (22:35 +0000)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 16 Oct 2012 22:35:44 +0000 (22:35 +0000)
Patch by: José Roberto de Souza  <zehortigoza@profusion.mobi>

SVN revision: 78071

Makefile.am
src/examples/banshee.c
src/lib/EDBus.h
src/lib/edbus_message_helper.c [new file with mode: 0644]
src/lib/edbus_message_helper.h [new file with mode: 0644]

index 30d2608..d125bd8 100644 (file)
@@ -63,7 +63,8 @@ include_HEADERS = \
        src/lib/edbus_pending.h \
        src/lib/edbus_proxy.h \
        src/lib/edbus_service.h \
-       src/lib/edbus_signal_handler.h
+       src/lib/edbus_signal_handler.h \
+       src/lib/edbus_message_helper.h
 
 libedbus2_la_LIBADD = @ECORE_LIBS@ @DBUS_LIBS@
 libedbus2_la_SOURCES = \
@@ -76,7 +77,8 @@ libedbus2_la_SOURCES = \
        src/lib/edbus_pending.c \
        src/lib/edbus_freedesktop.c \
        src/lib/edbus_service.c \
-       src/lib/edbus_signal_handler.c
+       src/lib/edbus_signal_handler.c \
+       src/lib/edbus_message_helper.c
 
 if EFL_BUILD_EXAMPLES
 noinst_PROGRAMS = \
index 389962f..b750239 100644 (file)
@@ -46,6 +46,45 @@ on_get_playlists(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
 }
 
 static void
+iterate_dict(void *data, const void *key, EDBus_Message_Iter *var)
+{
+   const char *skey = key;
+
+   if (!strcmp(skey, "PlaylistCount"))
+     {
+        unsigned count;
+        if (!edbus_message_iter_arguments_get(var, "u", &count))
+          printf("error2\n");
+        printf("PlaylistCount=%d\n", count);
+     }
+   else if (!strcmp(skey, "Orderings"))
+     {
+        EDBus_Message_Iter *as;
+        const char *txt;
+        printf("- Orderings\n");
+        if (!edbus_message_iter_arguments_get(var, "as", &as))
+          printf("error1\n");
+        while (edbus_message_iter_get_and_next(as, 's', &txt))
+          printf("\t%s\n", txt);
+     }
+}
+
+static void
+playlist_get_all_cb(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
+{
+   const char *errname, *errmsg;
+   EDBus_Message_Iter *array;
+
+   if (edbus_message_error_get(msg, &errname, &errmsg))
+     {
+        fprintf(stderr, "Error: %s %s\n", errname, errmsg);
+        return;
+     }
+   if (edbus_message_arguments_get(msg, "a{sv}", &array))
+     edbus_message_iter_dict_iterate(array, "sv", iterate_dict, NULL);
+}
+
+static void
 on_introspect(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
 {
    const char *errname, *errmsg, *string;
@@ -216,6 +255,8 @@ main(void)
 
    playlists = edbus_proxy_get(mediaplayer2_obj, "org.mpris.MediaPlayer2.Playlists");
 
+   edbus_proxy_property_get_all(playlists, playlist_get_all_cb, NULL);
+
    pending = edbus_proxy_call(introspectable, "Introspect", on_introspect, NULL, -1, "");
    if (!pending)
      {
index 784139c..7fee3f3 100644 (file)
@@ -133,6 +133,7 @@ typedef void (*EDBus_Signal_Cb)(void *data, const EDBus_Message *msg);
 #include "edbus_proxy.h"
 #include "edbus_freedesktop.h"
 #include "edbus_service.h"
+#include "edbus_message_helper.h"
 
 #ifdef __cplusplus
 }
diff --git a/src/lib/edbus_message_helper.c b/src/lib/edbus_message_helper.c
new file mode 100644 (file)
index 0000000..dad50bb
--- /dev/null
@@ -0,0 +1,31 @@
+#include "edbus_private.h"
+#include "edbus_private_types.h"
+
+EAPI void
+edbus_message_iter_dict_iterate(EDBus_Message_Iter *array, const char *signature, EDBus_Dict_Cb_Get cb, const void *data)
+{
+   EDBus_Message_Iter *entry;
+   char *iter_sig;
+   unsigned len;
+   EINA_SAFETY_ON_FALSE_RETURN(array);
+   EINA_SAFETY_ON_NULL_RETURN(signature);
+
+   iter_sig = edbus_message_iter_signature_get(array);
+   len = strlen(iter_sig + 1);
+   if (strncmp(signature, iter_sig + 1, len - 1))
+     {
+        ERR("Unexpected signature, expected is: %s", iter_sig);
+        free(iter_sig);
+        return;
+     }
+   free(iter_sig);
+
+   while (edbus_message_iter_get_and_next(array, 'e', &entry))
+     {
+        const void *key;
+        EDBus_Message_Iter *var;
+        if (!edbus_message_iter_arguments_get(entry, signature, &key, &var))
+          continue;
+        cb((void *)data, key, var);
+     }
+}
diff --git a/src/lib/edbus_message_helper.h b/src/lib/edbus_message_helper.h
new file mode 100644 (file)
index 0000000..1dedfd4
--- /dev/null
@@ -0,0 +1,11 @@
+typedef void (*EDBus_Dict_Cb_Get)(void *data, const void *key, EDBus_Message_Iter *var);
+
+/**
+ * Iterate over a dictionary.
+ *
+ * @param dict iterator with array of entry
+ * @param signature of entry, example: "sv"
+ * @param cb callback that will be called in each entry
+ * @param data context data
+ */
+EAPI void edbus_message_iter_dict_iterate(EDBus_Message_Iter *dict, const char *signature, EDBus_Dict_Cb_Get cb, const void *data);