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

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

#include "efl_model_container_private.h"

#define MY_CLASS EFL_MODEL_CONTAINER_ITEM_CLASS

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

EOLIAN static void
_efl_model_container_item_define(Eo *obj EINA_UNUSED, Efl_Model_Container_Item_Data *sd, void *parent_data, unsigned int index)
{
   sd->parent_data = parent_data;
   sd->index = index;
}

EOLIAN static void
_efl_model_container_item_efl_object_invalidate(Eo *obj, Efl_Model_Container_Item_Data *sd)
{
   efl_invalidate(efl_super(obj, MY_CLASS));

   sd->parent_data = NULL;
   sd->index = 0;
}

EOLIAN static const Eina_Array *
_efl_model_container_item_efl_model_properties_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Item_Data *sd)
{
   if (!sd->parent_data)
     return NULL;

   return sd->parent_data->defined_properties;
}

Efl_Future *
_efl_model_container_item_efl_model_property_set(Eo *obj EINA_UNUSED, Efl_Model_Container_Item_Data *sd, const char *property, const Eina_Value *value)
{
   Eina_Stringshare *prop_name;
   Child_Property_Data *cpd;
   void *data, *new_data;
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future* future = efl_promise_future_get(promise);

   if (!sd->parent_data)
     {
        efl_promise_failed_set(promise, EFL_MODEL_ERROR_INVALID_OBJECT);
        return future;
     }

   prop_name = eina_stringshare_add(property);
   cpd = eina_hash_find(sd->parent_data->property_data, prop_name);
   eina_stringshare_del(prop_name);
   if (!cpd || !cpd->property_values ||
       sd->index >= eina_array_count_get(cpd->property_values))
     {
        efl_promise_failed_set(promise, EFL_MODEL_ERROR_NOT_FOUND);
        return future;
     }

   if (cpd->property_type != eina_value_type_get(value))
     {
        efl_promise_failed_set(promise, EFL_MODEL_ERROR_INCORRECT_VALUE);
        return future;
     }

   data = calloc(1, cpd->property_type->value_size);
   if (!data || !eina_value_pget(value, data))
     {
        if (data)
          free(data);
        efl_promise_failed_set(promise, EFL_MODEL_ERROR_UNKNOWN);
        return future;
     }

   new_data = _value_copy_alloc(data, cpd->property_type);
   free(data);

   _value_free(eina_array_data_get(cpd->property_values, sd->index), cpd->property_type);
   eina_array_data_set(cpd->property_values, sd->index, new_data);

   {
      Eina_Value *v = eina_value_new(cpd->property_type);
      if (v && eina_value_copy(value, v))
        efl_promise_value_set(promise, v, _item_value_free_cb);
      else
        {
           if (v)
             eina_value_free(v);
           efl_promise_failed_set(promise, EFL_MODEL_ERROR_UNKNOWN);
        }
   }

   return future;
}

Efl_Future *
_efl_model_container_item_efl_model_property_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Item_Data *sd, const char *property)
{
   Eina_Stringshare *prop_name;
   Child_Property_Data *cpd;
   Eina_Value *value;
   Efl_Promise *promise = efl_add(EFL_PROMISE_CLASS, efl_main_loop_get());
   Efl_Future* future = efl_promise_future_get(promise);

   if (!sd->parent_data)
     {
        efl_promise_failed_set(promise, EFL_MODEL_ERROR_INVALID_OBJECT);
        return future;
     }

   prop_name = eina_stringshare_add(property);
   cpd = eina_hash_find(sd->parent_data->property_data, prop_name);
   eina_stringshare_del(prop_name);
   if (!cpd || !cpd->property_values ||
       sd->index >= eina_array_count_get(cpd->property_values))
     {
        efl_promise_failed_set(promise, EFL_MODEL_ERROR_NOT_FOUND);
        return future;
     }

   value = eina_value_new(cpd->property_type);
   if (!value)
        efl_promise_failed_set(promise, EFL_MODEL_ERROR_UNKNOWN);
   else
     {
        Eina_Bool r = EINA_FALSE;
        void *data = eina_array_data_get(cpd->property_values, sd->index);

        if (cpd->property_type == EINA_VALUE_TYPE_STRINGSHARE ||
            cpd->property_type == EINA_VALUE_TYPE_STRING)
          r = eina_value_set(value, data);
        else
          r = eina_value_pset(value, data);

        if (r)
          efl_promise_value_set(promise, value, _item_value_free_cb);
        else
          {
             efl_promise_failed_set(promise, EFL_MODEL_ERROR_UNKNOWN);
             eina_value_free(value);
          }
     }
   return future;
}

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

   efl_promise_value_set(promise, NULL, NULL);

   return future;
}

EOLIAN static Efl_Future *
_efl_model_container_item_efl_model_children_count_get(Eo *obj EINA_UNUSED, Efl_Model_Container_Item_Data *sd EINA_UNUSED)
{
   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 = 0;
   efl_promise_value_set(promise, count, &free);

   return future;
}

EOLIAN static Eo *
_efl_model_container_item_efl_model_child_add(Eo *obj EINA_UNUSED, Efl_Model_Container_Item_Data *sd EINA_UNUSED)
{
   EINA_LOG_WARN("child_add not supported by Efl.Model.Container.Item");
   eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);

   return NULL;
}

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

#include "efl_model_container_item.eo.c"