enable to add/edit/delete user in view_user_edit
[profile/tv/apps/native/air_home.git] / src / view / view_user.c
1 /*
2
3  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <Elementary.h>
19 #include <app_debug.h>
20 #include <viewmgr.h>
21 #include <inputmgr.h>
22
23 #include "defs.h"
24 #include "view_user.h"
25 #include "view_user_edit.h"
26 #include "data_user.h"
27 #include "datamgr.h"
28 #include "utils.h"
29
30 struct _priv {
31         Evas_Object *win;
32         Evas_Object *base;
33         Evas_Object *scr;
34         Evas_Object *box;
35         Eina_List *list;
36
37         struct datamgr *dm;
38         struct _bar_item *foc;
39 };
40
41 struct _bar_item {
42         Evas_Object *eo;
43
44         struct datamgr_item *di;
45         struct _priv *priv;
46 };
47
48 static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
49                 Evas_Event_Key_Down *ev)
50 {
51         struct _priv *priv;
52
53         if (!data) {
54                 _ERR("Invalid argument");
55                 return;
56         }
57
58         priv = data;
59
60         if (!strcmp(ev->keyname, KEY_ENTER) ||
61                         !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
62                 viewmgr_update_view(VIEW_USER_EDIT, 0, NULL);
63                 datamgr_select_item(priv->dm, priv->foc->di);
64         } else if (!strcmp(ev->keyname, KEY_BACK) ||
65                         !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
66                 viewmgr_pop_view();
67         } else if (!strcmp(ev->keyname, KEY_DOWN)) {
68                 if (priv->foc->di->action == ITEM_SELECT_ACTION_PUSH)
69                         return;
70
71                 viewmgr_update_view(VIEW_USER_EDIT, 0, priv->foc->di);
72                 viewmgr_push_view(VIEW_USER_EDIT);
73         }
74 }
75
76 static input_handler base_handler = {
77         .key_down = _key_down
78 };
79
80 static Evas_Object *_create(Evas_Object *win, void *data)
81 {
82         struct _priv *priv;
83         struct datamgr *dm;
84         Evas_Object *base;
85
86         if (!win) {
87                 _ERR("Invalid argument");
88                 return NULL;
89         }
90
91         priv = calloc(1, sizeof(*priv));
92         if (!priv) {
93                 _ERR("failed to calloc priv");
94                 return NULL;
95         }
96
97         dm = datamgr_init(datamgr_user_get_dclass(), VIEW_USER);
98         if (!dm) {
99                 _ERR("failed to initialize datamgr");
100                 free(priv);
101                 return NULL;
102         }
103
104         base = utils_add_layout(win, GRP_USER, false, NULL);
105         if (!base) {
106                 _ERR("failed to create base");
107                 datamgr_fini(dm);
108                 free(priv);
109                 return NULL;
110         }
111         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
112                         EVAS_HINT_EXPAND);
113         elm_win_resize_object_add(win, base);
114
115         priv->win = win;
116         priv->base = base;
117         priv->dm = dm;
118
119         viewmgr_set_view_data(VIEW_USER, priv);
120         viewmgr_add_view(view_user_edit_get_vclass(), dm);
121         inputmgr_add_callback(base, 0, &base_handler, priv);
122
123         return base;
124 }
125
126 static void _focused(int id, void *data, Evas_Object *obj,
127                 Elm_Object_Item *item)
128 {
129         struct _priv *priv;
130         struct _bar_item *bi;
131
132         if (!data) {
133                 _ERR("Invalid argument");
134                 return;
135         }
136
137         bi = data;
138         priv = bi->priv;
139         priv->foc = bi;
140
141         elm_object_signal_emit(obj, SIG_FOCUS, SRC_PROG);
142 }
143
144 static void _unfocused(int id, void *data, Evas_Object *obj,
145                 Elm_Object_Item *item)
146 {
147         elm_object_signal_emit(obj, SIG_UNFOCUS, SRC_PROG);
148 }
149
150 static input_handler eo_handler = {
151         .focused = _focused,
152         .unfocused = _unfocused
153 };
154
155 static struct _bar_item *_pack_bar_item(struct _priv *priv, Evas_Object *box,
156                 struct datamgr_item *di)
157 {
158         struct _bar_item *bi;
159         Evas_Object *eo, *ic, *focus_ic, *lbl, *focus_lbl, *bg;
160
161         bi = calloc(1, sizeof(*bi));
162         if (!bi) {
163                 _ERR("failed to calloc bar item");
164                 return NULL;
165         }
166
167         if (di->title) {
168                 eo = utils_add_layout(box, GRP_USER_ITEM, true, NULL);
169                 if (!eo) {
170                         _ERR("failed to add layout");
171                         free(bi);
172                         return NULL;
173                 }
174                 lbl = utils_add_label(eo, di->title, STYLE_LABEL_TITLE,
175                                 PART_BAR_ITEM_TITLE);
176                 if (!lbl)
177                         goto err;
178
179                 focus_lbl = utils_add_label(eo, di->title,
180                                 STYLE_LABEL_TITLE_FOCUS,
181                                 PART_BAR_ITEM_TITLE_FOCUS);
182                 if (!focus_lbl)
183                         goto err;
184         } else {
185                 eo = utils_add_layout(box, GRP_BAR_ITEM, true, NULL);
186                 if (!eo) {
187                         _ERR("failed to add layout");
188                         free(bi);
189                         return NULL;
190                 }
191         }
192
193         ic = utils_add_icon(eo, di->icon, PART_BAR_ITEM_ICON);
194         if (!ic)
195                 goto err;
196
197         focus_ic = utils_add_icon(eo,
198                         utils_get_focus_icon_from_icon(di->focus_icon),
199                         PART_BAR_ITEM_ICON_FOCUS);
200         if (!focus_ic)
201                 goto err;
202
203         bg = utils_add_bg(eo, COLOR_DEFAULT_R, COLOR_DEFAULT_G, COLOR_DEFAULT_B,
204                         COLOR_DEFAULT_A, PART_BAR_ITEM_BG);
205         if (!bg)
206                 goto err;
207
208         inputmgr_add_callback(eo, 0, &eo_handler, bi);
209         elm_box_pack_end(box, eo);
210         evas_object_show(eo);
211
212         bi->priv = priv;
213         bi->eo = eo;
214         bi->di = di;
215
216         return bi;
217 err:
218         _ERR("failed to add home item");
219         evas_object_del(eo);
220         free(bi);
221
222         return NULL;
223 }
224
225 static void _add_user(struct _priv *priv)
226 {
227         Eina_List *list, *l;
228         Evas_Object *scr, *box;
229         struct _bar_item *bi;
230         struct datamgr_item *di;
231
232         scr = utils_add_scroller(priv->base);
233         if (!scr) {
234                 _ERR("failed to add scroller");
235                 return;
236         }
237
238         box = utils_add_box(scr, true);
239         if (!box) {
240                 _ERR("failed to add box");
241                 evas_object_del(scr);
242                 return;
243         }
244         elm_object_content_set(scr, box);
245         elm_object_part_content_set(priv->base, PART_USER_BAR, scr);
246         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND,
247                         EVAS_HINT_EXPAND);
248
249         list = datamgr_get_items(priv->dm);
250         if (!list) {
251                 _ERR("failed to load list");
252                 evas_object_del(scr);
253                 return;
254         }
255
256         EINA_LIST_FOREACH(list, l, di) {
257                 bi = _pack_bar_item(priv, box, di);
258                 if (!bi)
259                         continue;
260
261                 priv->list = eina_list_append(priv->list, bi);
262         }
263
264         priv->scr = scr;
265         priv->box = box;
266 }
267
268 static void _show(void *data)
269 {
270         struct _priv *priv;
271
272         if (!data) {
273                 _ERR("Invalid argument");
274                 return;
275         }
276
277         priv = data;
278
279         evas_object_show(priv->base);
280
281         _add_user(priv);
282
283         if (!priv->foc)
284                 priv->foc = eina_list_data_get(priv->list);
285
286         elm_object_focus_set(priv->foc->eo, EINA_TRUE);
287 }
288
289 static void _unload_user(struct _priv *priv)
290 {
291         struct _bar_item *bi;
292
293         EINA_LIST_FREE(priv->list, bi) {
294                 inputmgr_remove_callback(bi->eo, &eo_handler);
295                 evas_object_del(bi->eo);
296                 free(bi);
297         }
298
299         elm_box_clear(priv->box);
300         evas_object_del(priv->scr);
301         priv->box = NULL;
302         priv->scr = NULL;
303         priv->list = NULL;
304         priv->foc = NULL;
305 }
306
307 static void _hide(void *data)
308 {
309         struct _priv *priv;
310
311         if (!data) {
312                 _ERR("Invalid argument");
313                 return;
314         }
315
316         priv = data;
317
318         _unload_user(priv);
319
320         evas_object_hide(priv->base);
321 }
322
323 static void _destroy(void *data)
324 {
325         struct _priv *priv;
326
327         if (!data) {
328                 _ERR("Invalid argument");
329                 return;
330         }
331
332         priv = data;
333
334         _unload_user(priv);
335
336         viewmgr_remove_view(VIEW_USER_EDIT);
337         datamgr_fini(priv->dm);
338         inputmgr_remove_callback(priv->base, &base_handler);
339         evas_object_del(priv->base);
340         free(priv);
341 }
342
343 static view_class vclass = {
344         .view_id = VIEW_USER,
345         .create = _create,
346         .show = _show,
347         .hide = _hide,
348         .destroy = _destroy
349 };
350
351 view_class *view_user_get_vclass(void)
352 {
353         return &vclass;
354 }
355