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

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

#include "efl_container_model_private.h"

#define MY_CLASS EFL_CONTAINER_MODEL_ITEM_CLASS

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

EOLIAN static void
_efl_container_model_item_efl_object_invalidate(Eo *obj, Efl_Container_Model_Item_Data *sd)
{
   efl_invalidate(efl_super(obj, MY_CLASS));

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

static Eina_Iterator *
_efl_container_model_item_efl_model_properties_get(const Eo *obj EINA_UNUSED, Efl_Container_Model_Item_Data *sd)
{
   // FIXME: Not to sure here, shouldn't we extend a child of the parent actually ?
   return efl_model_properties_get(sd->parent_data->obj);
}

static Efl_Object *
_efl_container_model_item_efl_object_finalize(Eo *obj, Efl_Container_Model_Item_Data *pd)
{
   if (!pd->parent_data)
     return NULL;

   return obj;
}

static Eina_Future *
_efl_container_model_item_efl_model_property_set(Eo *obj,
                                                 Efl_Container_Model_Item_Data *sd,
                                                 const char *property, Eina_Value *value)
{
   Eina_Stringshare *name;
   Child_Property_Data *cpd;
   Eina_Value v = EINA_VALUE_EMPTY;
   void *data, *new_data;


   name = eina_stringshare_add(property);
   cpd = eina_hash_find(sd->parent_data->properties, name);
   eina_stringshare_del(name);

   if (!cpd || !cpd->values ||
       sd->index >= eina_array_count_get(cpd->values))
     return efl_loop_future_rejected(obj,
                                 EFL_MODEL_ERROR_NOT_FOUND);

   eina_value_setup(&v,cpd->type);
   if (!eina_value_convert(value, &v))
     return efl_loop_future_rejected(obj,
                                 EFL_MODEL_ERROR_INCORRECT_VALUE);

   // FIXME: This is trying to optimize and avoid the use of Eina_Value,
   // but this put restriction on the type that can be stored in this container.
   data = calloc(1, cpd->type->value_size);
   if (!data || !eina_value_pget(&v, data))
     goto on_error;

   new_data = _value_copy_alloc(data, cpd->type);

   _value_free(eina_array_data_get(cpd->values, sd->index), cpd->type);

   eina_array_data_set(cpd->values, sd->index, new_data);

   free(data);

   return efl_loop_future_resolved(obj, v);

 on_error:
   eina_value_flush(&v);
   free(data);

   return efl_loop_future_rejected(obj,
                               EFL_MODEL_ERROR_UNKNOWN);
}

static Eina_Value *
_efl_container_model_item_efl_model_property_get(const Eo *obj EINA_UNUSED,
                                                 Efl_Container_Model_Item_Data *sd,
                                                 const char *property)
{
   Eina_Stringshare *name;
   Child_Property_Data *cpd;
   Eina_Value *value;
   void *data;
   Eina_Bool r = EINA_FALSE;

   name = eina_stringshare_add(property);
   cpd = eina_hash_find(sd->parent_data->properties, name);
   eina_stringshare_del(name);

   if (!cpd || !cpd->values ||
       sd->index >= eina_array_count_get(cpd->values))
     return eina_value_error_new(EFL_MODEL_ERROR_NOT_FOUND);

   data = eina_array_data_get(cpd->values, sd->index);

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

   if (!r)
     {
        eina_value_free(value);
        value = eina_value_error_new(EFL_MODEL_ERROR_UNKNOWN);
     }
   return value;
}

EOLIAN static Eina_Future *
_efl_container_model_item_efl_model_children_slice_get(Eo *obj,
                                                       Efl_Container_Model_Item_Data *sd EINA_UNUSED,
                                                       unsigned int start EINA_UNUSED,
                                                       unsigned int count EINA_UNUSED)
{
   return efl_loop_future_resolved(obj, EINA_VALUE_EMPTY);
}

EOLIAN static unsigned int
_efl_container_model_item_efl_model_children_count_get(const Eo *obj EINA_UNUSED,
                                                       Efl_Container_Model_Item_Data *sd EINA_UNUSED)
{
   return 0;
}

EOLIAN static Eo *
_efl_container_model_item_efl_model_child_add(Eo *obj EINA_UNUSED,
                                              Efl_Container_Model_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_container_model_item_efl_model_child_del(Eo *obj EINA_UNUSED,
                                              Efl_Container_Model_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_container_model_item.eo.c"