edbus: Add example for reading str array inside a variant using eina_value
authorJosé Roberto de Souza <zehortigoza@profusion.mobi>
Tue, 11 Dec 2012 19:51:12 +0000 (19:51 +0000)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 11 Dec 2012 19:51:12 +0000 (19:51 +0000)
Patch by: José Roberto de Souza  <zehortigoza@profusion.mobi>

SVN revision: 80700

legacy/edbus/src/examples/complex_types_client_eina_value.c
legacy/edbus/src/examples/complex_types_server.c

index 10e8f72..5f7fa48 100644 (file)
@@ -148,6 +148,35 @@ on_plus_one(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
 }
 
 static void
+receive_variant_cb(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
+{
+   Eina_Value *v, variant, array;
+   unsigned i;
+
+   printf("4 - receive a variant with an array of strings\n");
+   if (edbus_message_error_get(msg, NULL, NULL))
+     {
+        printf("Message error\n\n");
+        return;
+     }
+
+   v = edbus_message_to_eina_value(msg);
+
+   eina_value_struct_value_get(v, "arg0", &variant);
+   eina_value_struct_value_get(&variant, "arg0", &array);
+   for (i = 0; i < eina_value_array_count(&array); i++)
+     {
+        const char *txt;
+        eina_value_array_get(&array, i, &txt);
+        printf("\t%s\n", txt);
+     }
+
+   eina_value_flush(&array);
+   eina_value_flush(&variant);
+   eina_value_free(v);
+}
+
+static void
 _property_removed(void *data, EDBus_Proxy *proxy, void *event_info)
 {
    EDBus_Proxy_Event_Property_Removed *event = event_info;
@@ -280,6 +309,8 @@ main(void)
    edbus_proxy_properties_monitor(proxy, EINA_TRUE);
    ecore_timer_add(10, _read_cache, proxy);
 
+   edbus_proxy_call(proxy, "ReceiveVariantData", receive_variant_cb, NULL, -1, "");
+
    ecore_main_loop_begin();
 
    edbus_proxy_event_callback_del(proxy, EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
index dd92683..2a8fb2a 100644 (file)
@@ -64,6 +64,26 @@ _receive_array_of_string_int_with_size(const EDBus_Service_Interface *iface, con
 }
 
 static EDBus_Message *
+_receive_variant(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
+{
+   EDBus_Message *reply = edbus_message_method_return_new(msg);
+   EDBus_Message_Iter *var, *array, *main_iter;
+
+   main_iter = edbus_message_iter_get(reply);
+   var = edbus_message_iter_container_new(main_iter, 'v', "as");
+   edbus_message_iter_arguments_set(var, "as", &array);
+
+   edbus_message_iter_arguments_set(array, "s", "item1");
+   edbus_message_iter_arguments_set(array, "s", "item2");
+   edbus_message_iter_arguments_set(array, "s", "item3");
+
+   edbus_message_iter_container_close(var, array);
+   edbus_message_iter_container_close(main_iter, var);
+
+   return reply;
+}
+
+static EDBus_Message *
 _send_variant(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
 {
    EDBus_Message *reply = edbus_message_method_return_new(msg);
@@ -263,6 +283,10 @@ static const EDBus_Method methods[] = {
         NULL, _send_variant
       },
       {
+       "ReceiveVariantData", NULL, EDBUS_ARGS({"v", "variant_data"}),
+       _receive_variant
+      },
+      {
         "SendArrayInt", NULL,
         EDBUS_ARGS({"ai", "array_of_int"}), _send_array_int, 0
       },