summaryrefslogtreecommitdiff
path: root/src/examples/eldbus/dbusmodel.c
blob: 5edbcc82eda501449bb1721d8796cb3135a96888 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//Compile with:
// gcc -o busmodel busmodel.c `pkg-config --cflags --libs eldbus ecore eina`

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eldbus.h>
#include <Eldbus_Model.h>
#include <Efl.h>
#include <Ecore.h>

#define DEFAULT_BUS  "org.freedesktop.DBus"
#define DEFAULT_PATH "/"

static int prop_count = 0;

static void
promise_then_prop_c(void* priv_obj, void* data)
{
   Eo* obj = priv_obj;
   Eina_Value * property_value;
   const Eina_Array *properties_list;
   Eina_Array_Iterator a_it;
   char *property, *prop_str;
   const char *name;
   Eina_Iterator* it = data;

   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)
     {
        if (eina_iterator_next(it, (void **)&property_value) && property_value)
          {
             prop_str = eina_value_to_string(property_value);
             printf("    * %s=%s \n", property, prop_str);
             free(prop_str);
          }
     }

   prop_count--;
   if (prop_count == 0)
     ecore_main_loop_quit();
}

static void
error_cb(void* data EINA_UNUSED, Eina_Error error EINA_UNUSED)
{
   printf(" ERROR\n");
   ecore_main_loop_quit();
}

static void
promise_then_a(void* priv_obj EINA_UNUSED, void* data)
{
   const Eina_Array *properties_list;
   Eina_Array_Iterator a_it;
   Eina_Promise **promises;
   const char *name;
   char *property;
   Eo* child;
   int i = 0;
   Eina_Accessor* accessor = data;

   EINA_ACCESSOR_FOREACH(accessor, i, child)
     {
        properties_list = efl_model_properties_get(child);
        name = eldbus_model_proxy_name_get(child);

        unsigned p_count = eina_array_count(properties_list);

        if (p_count)
          {
             promises = (Eina_Promise **)calloc(p_count + 1, sizeof(Eina_Promise *));
             promises[p_count] = NULL;

             unsigned j = 0;
             EINA_ARRAY_ITER_NEXT(properties_list, j, property, a_it)
               {
                  promises[j] = efl_model_property_get(child, property);
               }
             eina_promise_then(eina_promise_all(eina_carray_iterator_new((void **)promises)),
                               &promise_then_prop_c, &error_cb, child);
             prop_count++;
          }
        else
          {
             printf(" -> %s\n", name);
          }
     }

   if (prop_count == 0)
     ecore_main_loop_quit();
}
static void
promise_then(void* obj EINA_UNUSED, void* data)
{
   Eina_Accessor *accessor;
   unsigned int* count;

   Eina_Iterator* iterator = data;

   if (!eina_iterator_next(iterator, (void**)&accessor))
     {
        printf("bye\n");
        ecore_main_loop_quit();
        return;
     }

   if(eina_iterator_next(iterator, (void **)&count))
     {
        printf("efl_model_loaded count %d\n", (int)*count); fflush(stdout);
        printf("efl_model_loaded accessor %p\n", accessor); fflush(stdout);

        promise_then_a(NULL, accessor);
     }
}

int
main(int argc, char **argv EINA_UNUSED)
{
   const char *bus, *path;
   Eo *root;

   ecore_init();
   eldbus_init();

   bus = DEFAULT_BUS;
   path = DEFAULT_PATH;

   if (argc > 1) bus = argv[1];
   if (argc > 2) path = argv[2];

   root = efl_add_ref(ELDBUS_MODEL_OBJECT_CLASS, NULL, eldbus_model_object_constructor(efl_self, ELDBUS_CONNECTION_TYPE_SESSION, NULL, EINA_FALSE, bus, path));

   Eina_Promise *promises[] = { NULL, NULL, NULL};
   promises[0] = efl_model_children_slice_get(root, 0, 0);
   promises[1] = efl_model_children_count_get(root);

   eina_promise_then(eina_promise_all(eina_carray_iterator_new((void **)promises)),
                     &promise_then, &error_cb, root);

   ecore_main_loop_begin();

   efl_unref(root);
   eldbus_shutdown();
}