cedric pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=2afe201fff741a49ee67d59e3ee48afaaf807d77

commit 2afe201fff741a49ee67d59e3ee48afaaf807d77
Author: Cedric BAIL <ced...@osg.samsung.com>
Date:   Mon Apr 30 13:41:38 2018 -0700

    eldbus: update dbus efl.model example.
---
 src/examples/eldbus/dbusmodel.c | 196 ++++++++++++++++++----------------------
 1 file changed, 87 insertions(+), 109 deletions(-)

diff --git a/src/examples/eldbus/dbusmodel.c b/src/examples/eldbus/dbusmodel.c
index fb197b650c..10150c40f2 100644
--- a/src/examples/eldbus/dbusmodel.c
+++ b/src/examples/eldbus/dbusmodel.c
@@ -14,150 +14,126 @@
 #define DEFAULT_BUS_NAME  "org.freedesktop.DBus"
 #define DEFAULT_PATH "/"
 
-static int prop_count = 0;
 static int retval = EXIT_SUCCESS;
 static Eina_Bool quit_on_done = EINA_TRUE;
 
 static void
-future_properties_then(void* data, const Efl_Event *event)
+_on_properties_changed(void *data EINA_UNUSED, const Efl_Event *event)
 {
-   Eo* obj = data;
-   Efl_Future_Event_Success *future = event->info;
-   Eina_Value * property_value;
-   const Eina_Array *properties_list;
-   Eina_Array_Iterator a_it;
-   char *property, *prop_str;
-   const char *name;
-   Eina_Iterator* it = future->value;
-
-   name = eldbus_model_proxy_name_get(obj);
-   properties_list = efl_model_properties_get(obj);
-
-   printf(" -> %s\n   Properties:\n", name);
-   unsigned i = 0;
-   EINA_ARRAY_ITER_NEXT(properties_list, i, property, a_it)
+   Efl_Model_Property_Event *ev = event->info;
+   Eina_Array_Iterator it;
+   const char *property;
+   unsigned int i;
+
+   printf("Properties changed:\n");
+   EINA_ARRAY_ITER_NEXT(ev->changed_properties, i, property, it)
      {
-        if (eina_iterator_next(it, (void **)&property_value) && property_value)
+        Eina_Value *v;
+        char *str;
+
+        v = efl_model_property_get(event->object, property);
+
+        if (!v)
           {
-             prop_str = eina_value_to_string(property_value);
-             printf("    * %s=%s \n", property, prop_str);
-             free(prop_str);
+             EINA_LOG_CRIT("Property '%s' returned nothing.", property);
+             abort();
           }
-     }
 
-   prop_count--;
-   if (prop_count == 0)
-     {
-        if (quit_on_done)
-          ecore_main_loop_quit();
-        else
-          printf("monitoring events...\n");
+        str = eina_value_to_string(v);
+        printf("\t%s: '%s'\n", property, str);
+        free(str);
+
+        eina_value_free(v);
      }
+
+   printf("Properties invalidated:\n");
+   EINA_ARRAY_ITER_NEXT(ev->invalidated_properties, i, property, it)
+     printf("\t%s\n", property);
 }
 
 static void
-error_cb(void* data EINA_UNUSED, const Efl_Event *event)
+_on_invalidate(void *data EINA_UNUSED, const Efl_Event *event)
 {
-   Efl_Future_Event_Failure *future = event->info;
-   printf(" ERROR: #%d '%s'\n", future->error, 
eina_error_msg_get(future->error));
-   ecore_main_loop_quit();
-   retval = EXIT_FAILURE;
+   efl_unref(event->object);
 }
 
+EFL_CALLBACKS_ARRAY_DEFINE(child_cbs,
+                           { EFL_MODEL_EVENT_PROPERTIES_CHANGED, 
_on_properties_changed },
+                           { EFL_EVENT_INVALIDATE, _on_invalidate });
+
 static void
-loop_children(Eina_Accessor *children)
+process(Eo *child, unsigned int index)
 {
-   Eo* child;
-   int i = 0;
+   Eina_Array *properties = efl_model_properties_get(child);
+   const char *property;
+   Eina_Array_Iterator it;
+   Eina_Strbuf *buf;
+   unsigned int i;
+
+   buf = eina_strbuf_new();
 
-   EINA_ACCESSOR_FOREACH(children, i, child)
+   const char *name = eldbus_model_proxy_name_get(child);
+
+   EINA_ARRAY_ITER_NEXT(properties, i, property, it)
      {
-        const Eina_Array *properties_list = efl_model_properties_get(child);
-        unsigned p_count = eina_array_count(properties_list);
-        const char *name = eldbus_model_proxy_name_get(child);
+        Eina_Value *v = efl_model_property_get(child, property);
+        char *str;
 
-        if (p_count)
-          {
-             Efl_Future **futures = calloc(p_count + 1, sizeof(Efl_Future *));
-             Eina_Array_Iterator a_it;
-             const char *property;
-             unsigned j = 0;
-
-             EINA_ARRAY_ITER_NEXT(properties_list, j, property, a_it)
-               {
-                  futures[j] = efl_model_property_get(child, property);
-               }
-             
efl_future_then(efl_future_iterator_all(eina_carray_iterator_new((void 
**)futures)),
-                             &future_properties_then, &error_cb, NULL, child);
-             prop_count++;
-             free(futures);
-             printf(" %2d: %s (loading %u properties asynchronously)\n", i, 
name, j);
-          }
-        else
+        if (!v)
           {
-             printf(" %2d: %s (no properties yet)\n", i, name);
+             EINA_LOG_CRIT("Property '%s' returned nothing.", property);
+             abort();
           }
-     }
 
-   if (prop_count == 0)
-     {
-        if (quit_on_done)
-          ecore_main_loop_quit();
-        else
-          printf("monitoring events...\n");
+        str = eina_value_to_string(v);
+        eina_strbuf_append_printf(buf, " \t* %s: '%s'\n", property, str);
+        free(str);
+
+        eina_value_free(v);
      }
-}
 
-static void
-future_then(void* obj EINA_UNUSED, const Efl_Event *event)
-{
-   Efl_Future_Event_Success *future = event->info;
-   Eina_Accessor *values = future->value;
-   Eina_Accessor *children;
-   unsigned int *count;
+   if (eina_array_count(properties) <= 0)
+     eina_strbuf_append_printf(buf,  " %2d: %s (no properties yet)\n", index, 
name);
+   else
+     eina_strbuf_prepend_printf(buf, " -> %s\n   Properties:\n", name);
 
-   if (!eina_accessor_data_get(values, 0, (void**)&children))
-     {
-        fprintf(stderr, "ERROR: missing future fulfillment value #0\n");
-        retval = EXIT_FAILURE;
-        ecore_main_loop_quit();
-        return;
-     }
+   printf("%s", eina_strbuf_string_get(buf));
 
-   if (!eina_accessor_data_get(values, 1, (void**)&count))
-     {
-        fprintf(stderr, "ERROR: missing future fulfillment value #1\n");
-        retval = EXIT_FAILURE;
-        ecore_main_loop_quit();
-        return;
-     }
+   eina_array_free(properties);
+   eina_strbuf_free(buf);
+
+   efl_ref(child);
+
+   efl_event_callback_array_add(child, child_cbs(), NULL);
 
-   printf("efl_model_loaded count %u\n", *count);
-   loop_children(children);
+   printf("monitoring events...\n");
 }
 
-static void
-_on_properties_changed(void *data EINA_UNUSED, const Efl_Event *event)
+static Eina_Value
+_slice(void *data, const Eina_Value v,
+       const Eina_Future *dead_future EINA_UNUSED)
 {
-   Efl_Model_Property_Event *ev = event->info;
-   Eina_Array_Iterator it;
-   const char *str;
-   unsigned int i;
+   unsigned int offset = (unsigned int)(uintptr_t) data;
+   unsigned int i, len;
+   Eo *child;
 
-   printf("Properties changed:\n");
-   EINA_ARRAY_ITER_NEXT(ev->changed_properties, i, str, it)
-     printf("\t%s\n", str);
+   if (eina_value_type_get(&v) == EINA_VALUE_TYPE_ERROR) return v;
 
-   printf("Properties invalidated:\n");
-   EINA_ARRAY_ITER_NEXT(ev->invalidated_properties, i, str, it)
-     printf("\t%s\n", str);
+   EINA_VALUE_ARRAY_FOREACH(&v, len, i, child)
+     process(child, offset + i);
+
+   return v;
 }
 
 static void
 _on_child_added(void *data EINA_UNUSED, const Efl_Event *event)
 {
-   Eo *child = event->info;
-   printf("Children Added: %p\n", child);
+   Efl_Model_Children_Event *ev = event->info;
+
+   printf("Children Added: %i\n", ev->index);
+
+   eina_future_then(efl_model_children_slice_get(event->object, ev->index, 1),
+                    _slice, (void*)(uintptr_t) ev->index);
 }
 
 static void
@@ -250,12 +226,14 @@ main(int argc, char **argv EINA_UNUSED)
                 ELDBUS_CONNECTION_TYPE_SESSION);
 
    root = efl_add_ref(ELDBUS_MODEL_OBJECT_CLASS, efl_main_loop_get(),
-                      eldbus_model_object_custom_constructor(efl_added, 
conn_type, NULL, EINA_FALSE, bus_name, path),
+                      eldbus_model_connect(efl_added, conn_type, NULL, 
EINA_FALSE),
+                      eldbus_model_object_bus_set(efl_added, bus_name),
+                      eldbus_model_object_path_set(efl_added, path),
                       efl_event_callback_array_add(efl_added, event_cbs(), 
NULL));
 
-   efl_future_then(efl_future_all(efl_model_children_slice_get(root, 0, 0),
-                                  efl_model_children_count_get(root)),
-                   &future_then, &error_cb, NULL, root);
+   if (efl_model_children_count_get(root))
+     eina_future_then(efl_model_children_slice_get(root, 0, 
efl_model_children_count_get(root)),
+                      _slice, (uintptr_t) 0);
 
    ecore_main_loop_begin();
    efl_del(root);

-- 


Reply via email to