summaryrefslogtreecommitdiff
path: root/src/lib/ecore/efl_model_item.c
blob: 1cdf87fd627bda61c0f8a0cd227cc7d0298eb6fe (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

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

#define MY_CLASS EFL_MODEL_ITEM_CLASS

typedef struct _Efl_Model_Item_Data Efl_Model_Item_Data;
struct _Efl_Model_Item_Data
{
   Eina_Hash                        *properties;
   Eina_Array                       *defined_properties;
   Eina_List                        *children;
};

static void
_item_value_free_cb(void *data)
{
   eina_value_free(data);
}

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 Efl_Object *
_efl_model_item_efl_object_constructor(Eo *obj, Efl_Model_Item_Data *sd)
{
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   if (!obj)
     return NULL;

   sd->properties = eina_hash_stringshared_new(_item_value_free_cb);
   sd->defined_properties = eina_array_new(8);

   return obj;
}

static void
_efl_model_item_efl_object_destructor(Eo *obj, Efl_Model_Item_Data *sd)
{
   Efl_Model *child;

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

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

   eina_array_free(sd->defined_properties);

   efl_destructor(efl_super(obj, MY_CLASS));
}

EOLIAN static const Eina_Array *
_efl_model_item_efl_model_properties_get(const Eo *obj EINA_UNUSED, Efl_Model_Item_Data *sd)
{
   return sd->defined_properties;
}


static Efl_Future*
_efl_model_item_efl_model_property_set(Eo *obj EINA_UNUSED, Efl_Model_Item_Data *sd, const char *property, const Eina_Value *value)
{
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future* future = efl_promise_future_get(promise);
   Efl_Model_Property_Event evt;

   Eina_Stringshare *sshared = eina_stringshare_add(property);
   Eina_Value *p_v = eina_hash_find(sd->properties, sshared);
   if (p_v)
     {
        eina_stringshare_del(sshared);
        if (!eina_value_copy(value, p_v))
          goto err1;
     }
   else
     {
        if (!eina_array_push(sd->defined_properties, sshared))
          goto err2;

        p_v = eina_value_new(eina_value_type_get(value));
        if (!p_v)
          goto err3;

        if (!eina_value_copy(value, p_v) ||
            !eina_hash_direct_add(sd->properties, sshared, p_v))
          goto err4;
     }

   efl_promise_value_set(promise, p_v, NULL);

   evt.changed_properties = eina_array_new(20);
   eina_array_push(evt.changed_properties, property);
   efl_event_callback_call(obj, EFL_MODEL_EVENT_PROPERTIES_CHANGED, &evt);
   eina_array_free(evt.changed_properties);

   return future;

err4:
   eina_value_free(p_v);
err3:
   eina_array_pop(sd->defined_properties);
err2:
   eina_stringshare_del(sshared);
err1:
   efl_promise_failed_set(promise, EFL_MODEL_ERROR_UNKNOWN);
   return future;
}

static Efl_Future *
_efl_model_item_efl_model_property_get(Eo *obj EINA_UNUSED, Efl_Model_Item_Data *sd, const char *property)
{
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future *rfuture = efl_promise_future_get(promise);

   Eina_Stringshare *sshare = eina_stringshare_add(property);
   Eina_Value *p_v = eina_hash_find(sd->properties, sshare);
   eina_stringshare_del(sshare);
   if (p_v)
     efl_promise_value_set(promise, p_v, NULL);
   else
     efl_promise_failed_set(promise, EFL_MODEL_ERROR_NOT_FOUND);

   return rfuture;
}

static Efl_Future *
_efl_model_item_efl_model_children_slice_get(Eo *obj EINA_UNUSED, Efl_Model_Item_Data *sd, unsigned int start, unsigned int count)
{
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future *rfuture = 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 rfuture;
}

static Efl_Future *
_efl_model_item_efl_model_children_count_get(Eo *obj EINA_UNUSED, Efl_Model_Item_Data *sd)
{
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future *rfuture = 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 rfuture;
}

static Eo *
_efl_model_item_efl_model_child_add(Eo *obj, Efl_Model_Item_Data *sd)
{
   Efl_Model_Children_Event cevt;
   Efl_Model *child = efl_add(EFL_MODEL_ITEM_CLASS, obj);
   if (!child)
     {
        EINA_LOG_ERR("Could not allocate Efl.Model.Item");
        eina_error_set(EFL_MODEL_ERROR_UNKNOWN);
        return NULL;
     }
   cevt.index = eina_list_count(sd->children);
   sd->children = eina_list_append(sd->children, child);
   cevt.child = child;
   efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILD_ADDED, &cevt);
   efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILDREN_COUNT_CHANGED, &cevt.index);
   return child;
}

static void
_efl_model_item_efl_model_child_del(Eo *obj, Efl_Model_Item_Data *sd, Eo *child)
{
   Eina_List *l;
   Efl_Model *data;
   unsigned int i = 0;
   EINA_LIST_FOREACH(sd->children, l, data)
     {
        if (data == child)
          {
             Efl_Model_Children_Event cevt;
             sd->children = eina_list_remove_list(sd->children, l);

             cevt.child = efl_ref(child);
             cevt.index = i;

             efl_parent_set(child, NULL);

             efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILD_REMOVED, &cevt);
             efl_unref(child);

             i = eina_list_count(sd->children);
             efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILDREN_COUNT_CHANGED, &i);
             break;
          }
        ++i;
     }
}

#include "efl_model_item.eo.c"