add exception handling popup for checking 8 users
[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 #define MESSAGE_POPUP_CONTENT "Can not add users more than 8.<br>If you want to add another user, delete one of users"
31
32 struct _priv {
33         Evas_Object *win;
34         Evas_Object *base;
35         Evas_Object *scr;
36         Evas_Object *box;
37         Eina_List *list;
38
39         struct datamgr *dm;
40         struct _bar_item *foc;
41 };
42
43 struct _bar_item {
44         Evas_Object *eo;
45
46         struct datamgr_item *di;
47         struct _priv *priv;
48 };
49
50 static void _btn_key_down(int id, void *data, Evas *e, Evas_Object *obj,
51                 Evas_Event_Key_Down *ev)
52 {
53         if (!strcmp(ev->keyname, KEY_ENTER) ||
54                         !strcmp(ev->keyname, KEY_ENTER_REMOTE) ||
55                         !strcmp(ev->keyname, KEY_BACK) ||
56                         !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
57                 evas_object_del(data);
58         }
59 }
60
61 static input_handler btn_handler = {
62         .key_down = _btn_key_down
63 };
64
65 static void _add_popup(struct _priv *priv)
66 {
67         Evas_Object *popup, *btn;
68
69         popup = utils_add_popup(priv->base, MESSAGE_ADD_USER,
70                         MESSAGE_POPUP_CONTENT);
71         if (!popup) {
72                 _ERR("failed to add popup");
73                 return;
74         }
75
76         btn = utils_add_button(popup, MESSAGE_OK, PART_POPUP_BUTTON_1);
77         if (!btn) {
78                 _ERR("failed to add button");
79                 evas_object_del(popup);
80                 return;
81         }
82         inputmgr_add_callback(btn, 0, &btn_handler, popup);
83         elm_object_focus_set(btn, EINA_TRUE);
84 }
85
86 static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
87                 Evas_Event_Key_Down *ev)
88 {
89         struct _priv *priv;
90
91         if (!data) {
92                 _ERR("Invalid argument");
93                 return;
94         }
95
96         priv = data;
97
98         if (!strcmp(ev->keyname, KEY_ENTER) ||
99                         !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
100                 if ((priv->foc->di->action == ITEM_SELECT_ACTION_PUSH) &&
101                                 (eina_list_count(priv->list) >= MAX_USER_COUNT)) {
102                         _add_popup(priv);
103                         return;
104                 }
105
106                 viewmgr_update_view(VIEW_USER_EDIT, 0, NULL);
107                 datamgr_select_item(priv->dm, priv->foc->di);
108         } else if (!strcmp(ev->keyname, KEY_BACK) ||
109                         !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
110                 viewmgr_pop_view();
111         } else if (!strcmp(ev->keyname, KEY_DOWN)) {
112                 if (priv->foc->di->action == ITEM_SELECT_ACTION_PUSH)
113                         return;
114
115                 viewmgr_update_view(VIEW_USER_EDIT, 0, priv->foc->di);
116                 viewmgr_push_view(VIEW_USER_EDIT);
117         }
118 }
119
120 static input_handler base_handler = {
121         .key_down = _key_down
122 };
123
124 static Evas_Object *_create(Evas_Object *win, void *data)
125 {
126         struct _priv *priv;
127         struct datamgr *dm;
128         Evas_Object *base;
129
130         if (!win) {
131                 _ERR("Invalid argument");
132                 return NULL;
133         }
134
135         priv = calloc(1, sizeof(*priv));
136         if (!priv) {
137                 _ERR("failed to calloc priv");
138                 return NULL;
139         }
140
141         dm = datamgr_init(datamgr_user_get_dclass(), VIEW_USER);
142         if (!dm) {
143                 _ERR("failed to initialize datamgr");
144                 free(priv);
145                 return NULL;
146         }
147
148         base = utils_add_layout(win, GRP_USER, false, NULL);
149         if (!base) {
150                 _ERR("failed to create base");
151                 datamgr_fini(dm);
152                 free(priv);
153                 return NULL;
154         }
155         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
156                         EVAS_HINT_EXPAND);
157         elm_win_resize_object_add(win, base);
158
159         priv->win = win;
160         priv->base = base;
161         priv->dm = dm;
162
163         viewmgr_set_view_data(VIEW_USER, priv);
164         viewmgr_add_view(view_user_edit_get_vclass(), dm);
165         inputmgr_add_callback(base, 0, &base_handler, priv);
166
167         return base;
168 }
169
170 static void _focused(int id, void *data, Evas_Object *obj,
171                 Elm_Object_Item *item)
172 {
173         struct _priv *priv;
174         struct _bar_item *bi;
175
176         if (!data) {
177                 _ERR("Invalid argument");
178                 return;
179         }
180
181         bi = data;
182         priv = bi->priv;
183         priv->foc = bi;
184
185         elm_object_signal_emit(obj, SIG_FOCUS, SRC_PROG);
186 }
187
188 static void _unfocused(int id, void *data, Evas_Object *obj,
189                 Elm_Object_Item *item)
190 {
191         elm_object_signal_emit(obj, SIG_UNFOCUS, SRC_PROG);
192 }
193
194 static input_handler eo_handler = {
195         .focused = _focused,
196         .unfocused = _unfocused
197 };
198
199 static struct _bar_item *_pack_bar_item(struct _priv *priv, Evas_Object *box,
200                 struct datamgr_item *di)
201 {
202         struct _bar_item *bi;
203         Evas_Object *eo, *ic, *focus_ic, *lbl, *focus_lbl, *bg;
204
205         bi = calloc(1, sizeof(*bi));
206         if (!bi) {
207                 _ERR("failed to calloc bar item");
208                 return NULL;
209         }
210
211         if (di->title) {
212                 eo = utils_add_layout(box, GRP_USER_ITEM, true, NULL);
213                 if (!eo) {
214                         _ERR("failed to add layout");
215                         free(bi);
216                         return NULL;
217                 }
218                 lbl = utils_add_label(eo, di->title, STYLE_LABEL_TITLE,
219                                 PART_BAR_ITEM_TITLE);
220                 if (!lbl)
221                         goto err;
222
223                 focus_lbl = utils_add_label(eo, di->title,
224                                 STYLE_LABEL_TITLE_FOCUS,
225                                 PART_BAR_ITEM_TITLE_FOCUS);
226                 if (!focus_lbl)
227                         goto err;
228         } else {
229                 eo = utils_add_layout(box, GRP_BAR_ITEM, true, NULL);
230                 if (!eo) {
231                         _ERR("failed to add layout");
232                         free(bi);
233                         return NULL;
234                 }
235         }
236
237         ic = utils_add_icon(eo, di->icon, PART_BAR_ITEM_ICON);
238         if (!ic)
239                 goto err;
240
241         focus_ic = utils_add_icon(eo,
242                         utils_get_focus_icon_from_icon(di->focus_icon),
243                         PART_BAR_ITEM_ICON_FOCUS);
244         if (!focus_ic)
245                 goto err;
246
247         bg = utils_add_bg(eo, COLOR_DEFAULT_R, COLOR_DEFAULT_G, COLOR_DEFAULT_B,
248                         COLOR_DEFAULT_A, PART_BAR_ITEM_BG);
249         if (!bg)
250                 goto err;
251
252         inputmgr_add_callback(eo, 0, &eo_handler, bi);
253         elm_box_pack_end(box, eo);
254         evas_object_show(eo);
255
256         bi->priv = priv;
257         bi->eo = eo;
258         bi->di = di;
259
260         return bi;
261 err:
262         _ERR("failed to add home item");
263         evas_object_del(eo);
264         free(bi);
265
266         return NULL;
267 }
268
269 static void _add_user(struct _priv *priv)
270 {
271         Eina_List *list, *l;
272         Evas_Object *scr, *box;
273         struct _bar_item *bi;
274         struct datamgr_item *di;
275
276         scr = utils_add_scroller(priv->base);
277         if (!scr) {
278                 _ERR("failed to add scroller");
279                 return;
280         }
281
282         box = utils_add_box(scr, true);
283         if (!box) {
284                 _ERR("failed to add box");
285                 evas_object_del(scr);
286                 return;
287         }
288         elm_object_content_set(scr, box);
289         elm_object_part_content_set(priv->base, PART_USER_BAR, scr);
290         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND,
291                         EVAS_HINT_EXPAND);
292
293         list = datamgr_get_items(priv->dm);
294         if (!list) {
295                 _ERR("failed to load list");
296                 evas_object_del(scr);
297                 return;
298         }
299
300         EINA_LIST_FOREACH(list, l, di) {
301                 bi = _pack_bar_item(priv, box, di);
302                 if (!bi)
303                         continue;
304
305                 priv->list = eina_list_append(priv->list, bi);
306         }
307
308         priv->scr = scr;
309         priv->box = box;
310 }
311
312 static void _show(void *data)
313 {
314         struct _priv *priv;
315
316         if (!data) {
317                 _ERR("Invalid argument");
318                 return;
319         }
320
321         priv = data;
322
323         evas_object_show(priv->base);
324
325         _add_user(priv);
326
327         if (!priv->foc)
328                 priv->foc = eina_list_data_get(priv->list);
329
330         elm_object_focus_set(priv->foc->eo, EINA_TRUE);
331 }
332
333 static void _unload_user(struct _priv *priv)
334 {
335         struct _bar_item *bi;
336
337         EINA_LIST_FREE(priv->list, bi) {
338                 inputmgr_remove_callback(bi->eo, &eo_handler);
339                 evas_object_del(bi->eo);
340                 free(bi);
341         }
342
343         elm_box_clear(priv->box);
344         evas_object_del(priv->scr);
345         priv->box = NULL;
346         priv->scr = NULL;
347         priv->list = NULL;
348         priv->foc = NULL;
349 }
350
351 static void _hide(void *data)
352 {
353         struct _priv *priv;
354
355         if (!data) {
356                 _ERR("Invalid argument");
357                 return;
358         }
359
360         priv = data;
361
362         _unload_user(priv);
363
364         evas_object_hide(priv->base);
365 }
366
367 static void _destroy(void *data)
368 {
369         struct _priv *priv;
370
371         if (!data) {
372                 _ERR("Invalid argument");
373                 return;
374         }
375
376         priv = data;
377
378         _unload_user(priv);
379
380         viewmgr_remove_view(VIEW_USER_EDIT);
381         datamgr_fini(priv->dm);
382         inputmgr_remove_callback(priv->base, &base_handler);
383         evas_object_del(priv->base);
384         free(priv);
385 }
386
387 static view_class vclass = {
388         .view_id = VIEW_USER,
389         .create = _create,
390         .show = _show,
391         .hide = _hide,
392         .destroy = _destroy
393 };
394
395 view_class *view_user_get_vclass(void)
396 {
397         return &vclass;
398 }
399