aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/eldbus/eldbus_model.c
blob: ff37b609055fd62b486b0588ff90c440b39582e4 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "eldbus_model_private.h"

#include <Ecore.h>
#include <Eina.h>
#include <Eldbus.h>

#define MY_CLASS ELDBUS_MODEL_CLASS
#define MY_CLASS_NAME "Eldbus_Model"

static void
eldbus_model_connect_do(Eldbus_Model_Data *pd)
{
   if (pd->type == ELDBUS_CONNECTION_TYPE_ADDRESS)
     {
        if (pd->private)
          pd->connection = eldbus_address_connection_get(pd->address);
        else
          pd->connection = eldbus_private_address_connection_get(pd->address);
     }
   else
     {
        if (pd->private)
          pd->connection = eldbus_private_connection_get(pd->type);
        else
          pd->connection = eldbus_connection_get(pd->type);
     }

   // TODO: Register for disconnection event
   if (!pd->connection)
     {
        DBG("Unable to setup a connection [%i - %s] %i",
            pd->type, pd->address, pd->private);
     }
}

static void
_eldbus_model_connect(Eo *obj EINA_UNUSED,
                      Eldbus_Model_Data *pd,
                      Eldbus_Connection_Type type,
                      const char *address,
                      Eina_Bool priv)
{
   pd->type = type;
   pd->address = eina_stringshare_add(address);
   pd->private = priv;
}

static void
_eldbus_model_connection_set(Eo *obj EINA_UNUSED,
                             Eldbus_Model_Data *pd,
                             Eldbus_Connection *dbus)
{
   eldbus_connection_ref(dbus);
   if (pd->connection) eldbus_connection_unref(pd->connection);
   pd->connection = dbus;
}

static Eldbus_Connection *
_eldbus_model_connection_get(const Eo *obj EINA_UNUSED, Eldbus_Model_Data *pd)
{
   return pd->connection;
}

static Efl_Object *
_eldbus_model_efl_object_finalize(Eo *obj, Eldbus_Model_Data *pd)
{
   if (!pd->connection) eldbus_model_connect_do(pd);
   if (!pd->connection) return NULL;

   return efl_finalize(efl_super(obj, ELDBUS_MODEL_CLASS));
}

static void
_eldbus_model_efl_object_invalidate(Eo *obj, Eldbus_Model_Data *pd)
{
   if (pd->connection) eldbus_connection_unref(pd->connection);
   pd->connection = NULL;

   efl_invalidate(efl_super(obj, MY_CLASS));
}

static void
_eldbus_model_efl_object_destructor(Eo *obj, Eldbus_Model_Data *pd)
{
   eina_stringshare_del(pd->unique_name);
   pd->unique_name = NULL;

   eina_stringshare_del(pd->address);
   pd->address = NULL;

   efl_destructor(efl_super(obj, MY_CLASS));
}

static const char *
_eldbus_model_address_get(const Eo *obj EINA_UNUSED, Eldbus_Model_Data *pd)
{
   return pd->address;
}

static Eina_Bool
_eldbus_model_private_get(const Eo *obj EINA_UNUSED, Eldbus_Model_Data *pd)
{
   return pd->private;
}

static Eldbus_Connection_Type
_eldbus_model_type_get(const Eo *obj EINA_UNUSED, Eldbus_Model_Data *pd)
{
   return pd->type;
}

static Eina_Future *
_eldbus_model_efl_model_property_set(Eo *obj,
                                     Eldbus_Model_Data *pd EINA_UNUSED,
                                     const char *property,
                                     Eina_Value *value EINA_UNUSED)
{
   Eina_Error err = EFL_MODEL_ERROR_READ_ONLY;

   if (strcmp(property, UNIQUE_NAME_PROPERTY))
     err = EFL_MODEL_ERROR_NOT_FOUND;
   return efl_loop_future_rejected(obj, err);
}

static Eina_Value *
_eldbus_model_efl_model_property_get(const Eo *obj,
                                     Eldbus_Model_Data *pd,
                                     const char *property)
{
   DBG("(%p): property=%s", obj, property);

   if (strcmp(property, UNIQUE_NAME_PROPERTY) != 0) goto on_error;

   if (!pd->connection) eldbus_model_connect_do(pd);

   if (pd->unique_name == NULL)
     {
        const char *unique_name;

        unique_name = eldbus_connection_unique_name_get(pd->connection);
        if (!unique_name) goto on_error;
        pd->unique_name = eina_stringshare_add(unique_name);
     }

   return eina_value_string_new(pd->unique_name);

 on_error:
   return eina_value_error_new(EFL_MODEL_ERROR_NOT_FOUND);
}

static Eina_Iterator *
_eldbus_model_efl_model_properties_get(const Eo *obj EINA_UNUSED,
                                       Eldbus_Model_Data *pd EINA_UNUSED)
{
   char *unique[] = { UNIQUE_NAME_PROPERTY };

   return EINA_C_ARRAY_ITERATOR_NEW(unique);
}

static Efl_Object *
_eldbus_model_efl_model_child_add(Eo *obj EINA_UNUSED,
                                  Eldbus_Model_Data *pd EINA_UNUSED)
{
   return NULL;
}

static void
_eldbus_model_efl_model_child_del(Eo *obj EINA_UNUSED,
                                  Eldbus_Model_Data *pd EINA_UNUSED,
                                  Efl_Object *child EINA_UNUSED)
{
}

static Eina_Future *
_eldbus_model_efl_model_children_slice_get(Eo *obj EINA_UNUSED,
                                           Eldbus_Model_Data *pd EINA_UNUSED,
                                           unsigned int start EINA_UNUSED,
                                           unsigned int count EINA_UNUSED)
{
   return efl_loop_future_rejected(obj,
                               EFL_MODEL_ERROR_NOT_SUPPORTED);
}

static unsigned int
_eldbus_model_efl_model_children_count_get(const Eo *obj EINA_UNUSED,
                                           Eldbus_Model_Data *pd EINA_UNUSED)
{
   return 0;
}

#include "eldbus_model.eo.c"