summaryrefslogtreecommitdiff
path: root/src/examples/eldbus/dbusmodel.c
blob: b34799b4c5b79ce9cc1400a3184b9d686848adde (plain)
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
//Compile with:
// gcc -o busmodel busmodel.c `pkg-config --cflags --libs eldbus ecore eina`

# include <config.h>

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

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

static unsigned int children_count = 0;

static Eina_Bool
_event_interface_load_status_cb(void *data EINA_UNUSED, Eo *model,
                           const Eo_Event_Description *desc EINA_UNUSED,
                           void *event_info)
{
   Efl_Model_Load *actual_load = (Efl_Model_Load*)event_info;
   Eina_Array *properties_list;
   Eina_Array_Iterator iterator;
   Eina_Value const* property_value;
   char *property, *prop_str;
   const char *name;
   unsigned int i;

   if (EFL_MODEL_LOAD_STATUS_LOADED != actual_load->status)
     return EINA_TRUE;

   name = eldbus_model_proxy_name_get(model);
   efl_model_properties_get(model, &properties_list);

   printf(" -> %s\n", name);
   if (eina_array_count(properties_list))
     printf("   Properties:\n");

   EINA_ARRAY_ITER_NEXT(properties_list, i, property, iterator)
     {
        efl_model_property_get(model, property, &property_value);
        if (property_value)
          {
             prop_str = eina_value_to_string(property_value);
          }
        printf("    * %s: %s \n", property, prop_str);
        free(prop_str);
        prop_str = NULL;
     }

   children_count--;

   if (!children_count)
     ecore_main_loop_quit();

   return EINA_FALSE;
}

static Eina_Bool
_event_load_status_cb(void *data EINA_UNUSED, Eo *model,
                           const Eo_Event_Description *desc EINA_UNUSED,
                           void *event_info)
{
   Efl_Model_Load *actual_load = (Efl_Model_Load*)event_info;
   Eina_Accessor *accessor;
   Eo *child = NULL;
   unsigned int i;

   if (EFL_MODEL_LOAD_STATUS_LOADED != actual_load->status)
     return EINA_TRUE;

   efl_model_children_count_get(model, &children_count);
   if (children_count == 0)
     {
        printf("Don't find Interfaces\n");
        ecore_main_loop_quit();
        return EINA_FALSE;
     }

   efl_model_children_slice_get(model, 0, 0, &accessor);
   printf("\nInterfaces:\n");
   EINA_ACCESSOR_FOREACH(accessor, i, child)
     {
        eo_event_callback_add(child, EFL_MODEL_BASE_EVENT_LOAD_STATUS, _event_interface_load_status_cb, NULL);
        efl_model_load(child);
     }

   return EINA_FALSE;
}

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

   eldbus_init();

   bus = DEFAULT_BUS;
   path = DEFAULT_PATH;

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

   root = eo_add_ref(ELDBUS_MODEL_OBJECT_CLASS, NULL, eldbus_model_object_constructor(eoid, ELDBUS_CONNECTION_TYPE_SESSION, NULL, EINA_FALSE, bus, path));

   eo_event_callback_add(root, EFL_MODEL_BASE_EVENT_LOAD_STATUS, _event_load_status_cb, NULL);
   efl_model_load(root);

   ecore_main_loop_begin();

   eo_event_callback_del(root, EFL_MODEL_BASE_EVENT_LOAD_STATUS, _event_load_status_cb, NULL);

   eo_unref(root);
}