summaryrefslogtreecommitdiff
path: root/src/lib/ecore/efl_model_container.c
blob: c3307259e442f60710d92f960194be8101c0a730 (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Efl.h>
#include <Eina.h>
#include <Eo.h>
#include <Ecore.h>

#include "efl_model_container_private.h"

#define MY_CLASS EFL_MODEL_CONTAINER_CLASS

void *
_value_copy_alloc(void *v, const Eina_Value_Type *type)
{
   if (!v)
     return v;

   if (type == EINA_VALUE_TYPE_STRINGSHARE)
     return (void*) eina_stringshare_ref(v);
   else if (type == EINA_VALUE_TYPE_STRING)
     return (void*) strdup(v);
   else
     {
        void *ret = malloc(type->value_size);
        memcpy(ret, v, type->value_size);
        return ret;
     }
}

void
_value_free(void *v, const Eina_Value_Type *type)
{
   if (!v)
     return;

   if (type == EINA_VALUE_TYPE_STRINGSHARE)
     return eina_stringshare_del(v);
   else
     free(v);
}

static void
_values_free(Eina_Array *values, const Eina_Value_Type *type)
{
   unsigned int i;
   void *v;
   Eina_Array_Iterator it;
   EINA_ARRAY_ITER_NEXT(values, i, v, it)
     {
        _value_free(v, type);
     }
   eina_array_free(values);
}

static Eina_Bool
_stringshared_keys_free(const Eina_Hash *hash EINA_UNUSED, const void *key, void *data EINA_UNUSED, void *fdata EINA_UNUSED)
{
   eina_stringshare_del(key);
   return EINA_TRUE;
}

static void
_property_data_free_cb(void *data)
{
   Child_Property_Data *cpd = data;
   _values_free(cpd->property_values, cpd->property_type);
   free(cpd);
}

static Efl_Object *
_efl_model_container_efl_object_constructor(Eo *obj, Efl_Model_Container_Data *sd)
{
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   if (!obj)
     return NULL;

   sd->obj = obj;
   sd->property_data = eina_hash_stringshared_new(_property_data_free_cb);
   sd->defined_properties = eina_array_new(8);

   return obj;
}

static void
_efl_model_container_efl_object_destructor(Eo *obj, Efl_Model_Container_Data *sd)
{
   Efl_Model *child;

   EINA_LIST_FREE(sd->children, child)
     {
        if (child) efl_parent_set(child, NULL);
     }

   eina_array_free(sd->defined_properties);

   eina_hash_foreach(sd->property_data, _stringshared_keys_free, NULL);
   eina_hash_free(sd->property_data);

   efl_destructor(efl_super(obj, MY_CLASS));
}

static const Eina_Value_Type *
_efl_model_container_child_property_value_type_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd, const char *property)
{
   Eina_Stringshare *sshared = eina_stringshare_add(property);
   Child_Property_Data *cpd = eina_hash_find(sd->property_data, sshared);
   eina_stringshare_del(sshared);
   if (!cpd)
     return NULL;

   return cpd->property_type;
}

static Eina_Iterator *
_efl_model_container_child_property_values_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd, const char *property)
{
   Eina_Stringshare *sshared = eina_stringshare_add(property);
   Child_Property_Data *cpd = eina_hash_find(sd->property_data, sshared);
   eina_stringshare_del(sshared);
   if (!cpd)
     return NULL;

   return eina_array_iterator_new(cpd->property_values);
}

static Eina_Bool
_efl_model_container_child_property_add(Eo *obj, Efl_Model_Container_Data *sd, const char *name, const Eina_Value_Type *type, Eina_Iterator *values)
{
   Eina_Array *arr = NULL;
   void *data = NULL;
   Eina_Stringshare *prop_name = NULL;
   Child_Property_Data *cpd = NULL;
   unsigned int i, in_count, children_count;

   if (!type || !values)
     {
        EINA_LOG_WARN("Invalid input data");
        eina_error_set(EFL_MODEL_ERROR_INCORRECT_VALUE);
        return EINA_FALSE;
     }

   arr = eina_array_new(32);
   if (!arr)
     {
        eina_error_set(EFL_MODEL_ERROR_UNKNOWN);
        return EINA_FALSE;
     }

   EINA_ITERATOR_FOREACH(values, data)
     {
        void *new_data = _value_copy_alloc(data, type);
        if ((data && !new_data) || !eina_array_push(arr, new_data))
          {
             if (new_data)
               _value_free(new_data, type);
             goto error;
          }
     }
   eina_iterator_free(values);

   prop_name = eina_stringshare_add(name);
   cpd = eina_hash_find(sd->property_data, prop_name);
   if (!cpd)
     {
        cpd = calloc(1, sizeof(Child_Property_Data));
        if (!cpd)
          goto error;

        cpd->property_type = type;
        cpd->property_values = arr;

        if (!eina_array_push(sd->defined_properties, prop_name))
          goto error;

        if (!eina_hash_direct_add(sd->property_data, prop_name, cpd))
          {
             eina_array_pop(sd->defined_properties);
             goto error;
          }
     }
   else
     {
        eina_stringshare_del(prop_name);
        _values_free(cpd->property_values, cpd->property_type);

        cpd->property_type = type;
        cpd->property_values = arr;
     }

   in_count = eina_array_count(arr);
   children_count = eina_list_count(sd->children);

   for (i = children_count; i < in_count; ++i)
     {
        Efl_Model_Children_Event cevt;
        Efl_Model *child;

        child = efl_add(EFL_MODEL_CONTAINER_ITEM_CLASS, obj,
                       efl_model_container_item_define(efl_added, sd, i));
        sd->children = eina_list_append(sd->children, child);

        cevt.child = child;
        cevt.index = i;
        efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILD_ADDED, &cevt);
     }

   if (in_count > children_count)
     efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILDREN_COUNT_CHANGED, &in_count);

   return EINA_TRUE;

error:
   if (prop_name)
     eina_stringshare_del(prop_name);
   if (cpd)
     free(cpd);
   if (arr)
     _values_free(arr, type);
   eina_error_set(EFL_MODEL_ERROR_UNKNOWN);
   return EINA_FALSE;
}

static const Eina_Array *
_efl_model_container_efl_model_properties_get(const Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd EINA_UNUSED)
{
   return sd->defined_properties;
}


Efl_Future *
_efl_model_container_efl_model_property_set(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd EINA_UNUSED, const char *property EINA_UNUSED, const Eina_Value *value EINA_UNUSED)
{
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future* future = efl_promise_future_get(promise);

   efl_promise_failed_set(promise, EFL_MODEL_ERROR_NOT_FOUND);
   return future;
}

Efl_Future *
_efl_model_container_efl_model_property_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd EINA_UNUSED, const char *property EINA_UNUSED)
{

   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future* future = efl_promise_future_get(promise);

   efl_promise_failed_set(promise, EFL_MODEL_ERROR_NOT_FOUND);
   return future;
}

static Efl_Future *
_efl_model_container_efl_model_children_slice_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd, unsigned int start, unsigned int count)
{
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future* future = efl_promise_future_get(promise);

   Eina_Accessor* accessor = efl_model_list_slice(sd->children, start, count);
   efl_promise_value_set(promise, accessor, (Eina_Free_Cb)&eina_accessor_free);

   return future;
}

static Efl_Future *
_efl_model_container_efl_model_children_count_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd)
{
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future* future = efl_promise_future_get(promise);

   unsigned int *count = calloc(1, sizeof(unsigned int));
   *count = eina_list_count(sd->children);
   efl_promise_value_set(promise, count, &free);

   return future;
}

static Eo *
_efl_model_container_efl_model_child_add(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd EINA_UNUSED)
{
   EINA_LOG_WARN("child_add not supported by Efl.Model.Container");
   eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);

   return NULL;
}

static void
_efl_model_container_efl_model_child_del(Eo *obj EINA_UNUSED, Efl_Model_Container_Data *sd EINA_UNUSED, Eo *child EINA_UNUSED)
{
   EINA_LOG_WARN("child_del not supported by Efl.Model.Container");
   eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);
}

#include "efl_model_container.eo.c"