aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/efl/interfaces/efl_model_common.c
blob: 3b32205a79a08fd00e763da841ae56ba0c1e9b04 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "Efl.h"
#include "Efl_Model_Common.h"

EAPI Eina_Error EFL_MODEL_ERROR_UNKNOWN = 0;
EAPI Eina_Error EFL_MODEL_ERROR_NOT_SUPPORTED = 0;
EAPI Eina_Error EFL_MODEL_ERROR_NOT_FOUND = 0;
EAPI Eina_Error EFL_MODEL_ERROR_READ_ONLY = 0;
EAPI Eina_Error EFL_MODEL_ERROR_INIT_FAILED = 0;
EAPI Eina_Error EFL_MODEL_ERROR_PERMISSION_DENIED = 0;
EAPI Eina_Error EFL_MODEL_ERROR_INCORRECT_VALUE = 0;

static const char EFL_MODEL_ERROR_UNKNOWN_STR[]           = "Unknown Error";
static const char EFL_MODEL_ERROR_NOT_SUPPORTED_STR[]     = "Operation not supported";
static const char EFL_MODEL_ERROR_NOT_FOUND_STR[]         = "Value not found";
static const char EFL_MODEL_ERROR_READ_ONLY_STR[]         = "Value read only";
static const char EFL_MODEL_ERROR_INIT_FAILED_STR[]       = "Init failed";
static const char EFL_MODEL_ERROR_PERMISSION_DENIED_STR[] = "Permission denied";
static const char EFL_MODEL_ERROR_INCORRECT_VALUE_STR[]   = "Incorrect value";

EAPI int
efl_model_init()
{
   EFL_MODEL_ERROR_INCORRECT_VALUE = eina_error_msg_static_register(
                   EFL_MODEL_ERROR_INCORRECT_VALUE_STR);

   EFL_MODEL_ERROR_UNKNOWN = eina_error_msg_static_register(
                   EFL_MODEL_ERROR_UNKNOWN_STR);

   EFL_MODEL_ERROR_NOT_SUPPORTED = eina_error_msg_static_register(
                   EFL_MODEL_ERROR_NOT_SUPPORTED_STR);

   EFL_MODEL_ERROR_NOT_FOUND = eina_error_msg_static_register(
                   EFL_MODEL_ERROR_NOT_FOUND_STR);

   EFL_MODEL_ERROR_READ_ONLY = eina_error_msg_static_register(
                   EFL_MODEL_ERROR_READ_ONLY_STR);

   EFL_MODEL_ERROR_INIT_FAILED = eina_error_msg_static_register(
                   EFL_MODEL_ERROR_INIT_FAILED_STR);

   EFL_MODEL_ERROR_PERMISSION_DENIED = eina_error_msg_static_register(
                   EFL_MODEL_ERROR_PERMISSION_DENIED_STR);

   return EINA_TRUE;
}

EAPI Eina_Accessor*
efl_model_list_slice(Eina_List *list, unsigned start, unsigned count)
{
   if (!list) return NULL;

   if ((start == 0) && (count == 0)) /* this is full data */
     {
        /*
         * children_accessor will be set to NULL by
         * eina_list_accessor_new if the later fails.
         */
       return eina_list_accessor_new(list);
     }

   Eo *child;
   Eina_List *l, *ln, *lr = NULL;
   ln = eina_list_nth_list(list, (start-1));
   if (!ln)
     {
        return NULL;
     }

   EINA_LIST_FOREACH(ln, l, child)
     {
        efl_ref(child);
        lr = eina_list_append(lr, child);
        if (eina_list_count(lr) == count)
          break;
     }

   if (!lr) return NULL;

   // This may leak the children Eina_List.
   return eina_list_accessor_new(lr);
}

EAPI void
efl_model_property_changed_notify(Efl_Model *model, const char *property)
{
   Eina_Array *changed_properties = eina_array_new(1);
   EINA_SAFETY_ON_NULL_RETURN(changed_properties);

   Eina_Bool ret = eina_array_push(changed_properties, property);
   EINA_SAFETY_ON_FALSE_GOTO(ret, on_error);

   Efl_Model_Property_Event evt = {.changed_properties = changed_properties};
   efl_event_callback_call(model, EFL_MODEL_EVENT_PROPERTIES_CHANGED, &evt);

on_error:
   eina_array_free(changed_properties);
}

EAPI void
efl_model_property_invalidated_notify(Efl_Model *model, const char *property)
{
   Eina_Array *invalidated_properties = eina_array_new(1);
   EINA_SAFETY_ON_NULL_RETURN(invalidated_properties);

   Eina_Bool ret = eina_array_push(invalidated_properties, property);
   EINA_SAFETY_ON_FALSE_GOTO(ret, on_error);

   Efl_Model_Property_Event evt = {.invalidated_properties = invalidated_properties};
   efl_event_callback_call(model, EFL_MODEL_EVENT_PROPERTIES_CHANGED, &evt);

on_error:
   eina_array_free(invalidated_properties);
}

typedef struct _Efl_Model_Value_Struct_Desc Efl_Model_Value_Struct_Desc;

struct _Efl_Model_Value_Struct_Desc
{
   Eina_Value_Struct_Desc base;
   void *data;
   Eina_Value_Struct_Member members[];
};

EAPI Eina_Value_Struct_Desc *
efl_model_value_struct_description_new(unsigned int member_count, Efl_Model_Value_Struct_Member_Setup_Cb setup_cb, void *data)
{
   Efl_Model_Value_Struct_Desc *desc;
   unsigned int offset = 0;
   size_t i;

   EINA_SAFETY_ON_FALSE_RETURN_VAL(member_count > 0, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(setup_cb, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(data, NULL);

   desc = malloc(sizeof(Efl_Model_Value_Struct_Desc) + member_count * sizeof(Eina_Value_Struct_Member));
   EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);

   desc->base.version = EINA_VALUE_STRUCT_DESC_VERSION;
   desc->base.ops = EINA_VALUE_STRUCT_OPERATIONS_STRINGSHARE;
   desc->base.members = desc->members;
   desc->base.member_count = member_count;
   desc->base.size = 0;
   desc->data = data;

   for (i = 0; i < member_count; ++i)
     {
        Eina_Value_Struct_Member *m = (Eina_Value_Struct_Member *)desc->members + i;
        unsigned int size;

        m->offset = offset;
        setup_cb(data, i, m);

        size = m->type->value_size;
        if (size % sizeof(void *) != 0)
          size += size - (size % sizeof(void *));

        offset += size;
     }

   desc->base.size = offset;
   return &desc->base;
}

EAPI void
efl_model_value_struct_description_free(Eina_Value_Struct_Desc *desc)
{
   size_t i;

   if (!desc) return;

   for (i = 0; i < desc->member_count; i++)
     eina_stringshare_del(desc->members[i].name);
   free(desc);
}