c72a3d992eccfa54639a7972dfaeedd8e4d4f65c
[profile/tv/apps/native/air_home.git] / src / view / view_user_edit.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_edit.h"
25 #include "view_photo.h"
26 #include "datamgr.h"
27 #include "utils.h"
28
29 #define MESSAGE_BTN_DONE "Done"
30 #define MESSAGE_BTN_CANCEL "Cancel"
31 #define MESSAGE_ADD_USER "Add user"
32 #define MESSAGE_EDIT_USER "Edit user"
33 #define MESSAGE_ENTRY_NAME "User Name"
34 #define MESSAGE_ENTRY_ACCOUNT "sample@tizen.com"
35 #define MESSAGE_ENTRY_PIN "Pin code"
36 #define ICON_ADD "add"
37 #define KEY_ICON "icon"
38 #define KEY_FOCUS_ICON "focusicon"
39
40 #define CTXPOPUP_X 294
41 #define CTXPOPUP_Y 310
42
43 struct _icon_info {
44         const char *file;
45         const char *focus_file;
46         const char *message;
47         const char *focus_message;
48 };
49
50 struct _priv {
51         Evas_Object *win;
52         Evas_Object *base;
53         Evas_Object *ly;
54         Evas_Object *photo;
55         Evas_Object *name;
56         Evas_Object *account;
57         Evas_Object *pin;
58         Evas_Object *lock;
59         Evas_Object *icon;
60         Evas_Object *focus_icon;
61         Evas_Object *done;
62         Evas_Object *cancel;
63         Evas_Object *ctxpopup;
64
65         struct datamgr *dm;
66 };
67
68 static void _done_key_down(int id, void *data, Evas *e, Evas_Object *obj,
69                 Evas_Event_Key_Down *ev)
70 {
71         if (!strcmp(ev->keyname, KEY_ENTER)) {
72                 /* check state of user edit */
73                 viewmgr_pop_view();
74         }
75 }
76
77 static input_handler done_handler = {
78         .key_down = _done_key_down
79 };
80
81 static void _cancel_key_down(int id, void *data, Evas *e, Evas_Object *obj,
82                 Evas_Event_Key_Down *ev)
83 {
84         if (!strcmp(ev->keyname, KEY_ENTER)) {
85                 viewmgr_pop_view();
86         }
87 }
88
89 static input_handler cancel_handler = {
90         .key_down = _cancel_key_down
91 };
92
93 static bool _add_btns(struct _priv *priv)
94 {
95         Evas_Object *done, *cancel;
96
97         done = utils_add_button(priv->base, MESSAGE_BTN_DONE,
98                         PART_USER_EDIT_BTN_DONE);
99         if (!done) {
100                 _ERR("failed to add done btn");
101                 return false;
102         }
103         inputmgr_add_callback(done, 0, &done_handler, NULL);
104
105         cancel = utils_add_button(priv->base, MESSAGE_BTN_CANCEL,
106                         PART_USER_EDIT_BTN_CANCEL);
107         if (!cancel) {
108                 _ERR("failed to add cancel btn");
109                 evas_object_del(done);
110                 return false;
111         }
112         inputmgr_add_callback(cancel, 0, &cancel_handler, NULL);
113
114         priv->cancel = cancel;
115         priv->done = done;
116
117         return true;
118 }
119
120 static void _focused(int id, void *data, Evas_Object *obj,
121                 Elm_Object_Item *item)
122 {
123         elm_object_signal_emit(obj, SIG_FOCUS, SRC_PROG);
124 }
125
126 static void _unfocused(int id, void *data, Evas_Object *obj,
127                 Elm_Object_Item *item)
128 {
129         elm_object_signal_emit(obj, SIG_UNFOCUS, SRC_PROG);
130 }
131
132 static void _load_user_icon(struct _priv *priv, const char *file,
133                 const char *focus_file)
134 {
135         Evas_Object *icon, *focus_icon;
136
137         icon = utils_add_icon(priv->photo, file, PART_USER_EDIT_PHOTO);
138         if (!icon)
139                 _ERR("failed to add icon");
140
141         focus_icon = utils_add_icon(priv->photo, focus_file,
142                         PART_USER_EDIT_PHOTO_FOCUS);
143         if (!focus_icon)
144                 _ERR("failed to add focus icon");
145
146         priv->icon = icon;
147         priv->focus_icon = focus_icon;
148 }
149
150 static void _eo_key_down(int id, void *data, Evas *e, Evas_Object *obj,
151                 Evas_Event_Key_Down *ev)
152 {
153         struct _priv *priv;
154         const char *file, *focus_file;
155
156         if (!data)
157                 return;
158
159         priv = data;
160
161         if (!strcmp(ev->keyname, KEY_BACK) ||
162                         !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
163                 evas_object_del(priv->ctxpopup);
164                 priv->ctxpopup = NULL;
165         } else if (!strcmp(ev->keyname, KEY_ENTER) ||
166                         !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
167                 evas_object_del(priv->ctxpopup);
168                 priv->ctxpopup = NULL;
169
170                 file = evas_object_data_get(obj, KEY_ICON);
171                 focus_file = evas_object_data_get(obj, KEY_FOCUS_ICON);
172
173                 if (!strcmp(file, ICON_ADD) || !strcmp(focus_file, ICON_ADD))
174                         viewmgr_push_view(VIEW_PHOTO);
175                 else
176                         _load_user_icon(priv, file, focus_file);
177         }
178 }
179
180 static input_handler icon_handler = {
181         .focused = _focused,
182         .unfocused = _unfocused,
183         .key_down = _eo_key_down
184 };
185
186 static Evas_Object *_pack_icon(Evas_Object *table, struct _icon_info *icon_info, int i)
187 {
188         Evas_Object *eo, *ic, *focus_ic;
189
190         eo = utils_add_layout(table, GRP_USER_EDIT_ICON_LIST_ITEM, true, NULL);
191         if (!eo) {
192                 _ERR("failed to add layout");
193                 return NULL;
194         }
195         evas_object_show(eo);
196         elm_table_pack(table, eo, i % 4, i / 4, 1, 1);
197
198         ic = utils_add_icon(eo, icon_info->file, PART_USER_EDIT_ICON_LIST_ITEM);
199         if (!ic) {
200                 _ERR("failed to add icon");
201                 evas_object_del(eo);
202                 return NULL;
203         }
204
205         focus_ic = utils_add_icon(eo, icon_info->focus_file,
206                         PART_USER_EDIT_ICON_LIST_ITEM_FOCUS);
207         if (!focus_ic) {
208                 _ERR("failed to add focus icon");
209                 evas_object_del(eo);
210                 return NULL;
211         }
212         evas_object_data_set(eo, KEY_ICON, icon_info->message);
213         evas_object_data_set(eo, KEY_FOCUS_ICON, icon_info->focus_message);
214
215         return eo;
216 }
217
218 static void _add_icon_list(struct _priv *priv)
219 {
220         Evas_Coord x, y, w, h;
221         Evas_Object *ctxpopup, *ly, *table, *eo;
222         int i;
223         struct _icon_info icon_info[] = {
224                 {
225                         IMAGE_USER_DEFAULT,
226                         IMAGE_USER_DEFAULT_FOCUS,
227                         IMAGE_USER_CURRENT_DEFAULT,
228                         IMAGE_USER_CURRENT_DEFAULT_FOCUS
229                 },
230                 {
231                         IMAGE_USER_DEFAULT_02,
232                         IMAGE_USER_DEFAULT_02_FOCUS,
233                         IMAGE_USER_CURRENT_DEFAULT_02,
234                         IMAGE_USER_CURRENT_DEFAULT_02_FOCUS
235                 },
236                 {
237                         IMAGE_USER_DEFAULT_03,
238                         IMAGE_USER_DEFAULT_03_FOCUS,
239                         IMAGE_USER_CURRENT_DEFAULT_03,
240                         IMAGE_USER_CURRENT_DEFAULT_03_FOCUS
241                 },
242                 {
243                         IMAGE_USER_DEFAULT_04,
244                         IMAGE_USER_DEFAULT_04_FOCUS,
245                         IMAGE_USER_CURRENT_DEFAULT_04,
246                         IMAGE_USER_CURRENT_DEFAULT_04_FOCUS
247                 },
248                 {
249                         IMAGE_USER_DEFAULT_05,
250                         IMAGE_USER_DEFAULT_05_FOCUS,
251                         IMAGE_USER_CURRENT_DEFAULT_05,
252                         IMAGE_USER_CURRENT_DEFAULT_05_FOCUS
253                 },
254                 {
255                         IMAGE_USER_DEFAULT_06,
256                         IMAGE_USER_DEFAULT_06_FOCUS,
257                         IMAGE_USER_CURRENT_DEFAULT_06,
258                         IMAGE_USER_CURRENT_DEFAULT_06_FOCUS
259                 },
260                 {
261                         IMAGE_USER_DEFAULT_07,
262                         IMAGE_USER_DEFAULT_07_FOCUS,
263                         IMAGE_USER_CURRENT_DEFAULT_07,
264                         IMAGE_USER_CURRENT_DEFAULT_07_FOCUS
265                 },
266                 {
267                         IMAGE_USER_ADD,
268                         IMAGE_USER_ADD_FOCUS,
269                         ICON_ADD,
270                         ICON_ADD
271                 }
272         };
273
274         ctxpopup = elm_ctxpopup_add(priv->base);
275         if (!ctxpopup) {
276                 _ERR("failed to add ctxpopup");
277                 return;
278         }
279         elm_ctxpopup_horizontal_set(ctxpopup, EINA_TRUE);
280         elm_ctxpopup_auto_hide_disabled_set(ctxpopup, EINA_TRUE);
281
282         ly = utils_add_layout(ctxpopup, GRP_USER_EDIT_ICON_LIST, false, NULL);
283         if (!ly) {
284                 _ERR("failed to layout");
285                 evas_object_del(ctxpopup);
286                 return;
287         }
288         elm_object_content_set(ctxpopup, ly);
289         evas_object_show(ly);
290         evas_object_show(ctxpopup);
291
292         evas_object_geometry_get(priv->photo, &x, &y, &w, &h);
293         evas_object_move(ctxpopup,
294                         (x + w + CTXPOPUP_X) * elm_config_scale_get(),
295                         (y + CTXPOPUP_Y) * elm_config_scale_get());
296
297         table = utils_add_table(ly, true, PART_USER_EDIT_ICON_LIST);
298         if (!table) {
299                 _ERR("failed to add table");
300                 evas_object_del(ctxpopup);
301                 return;
302         }
303         elm_table_padding_set(table, elm_config_scale_get() * 1,
304                         elm_config_scale_get() * 1);
305         evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND,
306                         EVAS_HINT_EXPAND);
307
308         for (i = 0; i < sizeof(icon_info) / sizeof(*icon_info); i++) {
309                 eo = _pack_icon(table, &icon_info[i], i);
310                 if (!eo)
311                         continue;
312
313                 inputmgr_add_callback(eo, 0, &icon_handler, priv);
314                 if (i == 0)
315                         elm_object_focus_set(eo, EINA_TRUE);
316         }
317
318         priv->ctxpopup = ctxpopup;
319 }
320
321 static void _photo_key_down(int id, void *data, Evas *e, Evas_Object *obj,
322                 Evas_Event_Key_Down *ev)
323 {
324         if (!strcmp(ev->keyname, KEY_ENTER)) {
325                 _add_icon_list(data);
326         }
327 }
328
329 static input_handler photo_handler = {
330         .focused = _focused,
331         .unfocused = _unfocused,
332         .key_down = _photo_key_down
333 };
334
335 static void _load_lock_icon(struct _priv *priv, Evas_Object *ly)
336 {
337         Evas_Object *lock;
338         const char *file = NULL;
339
340         if (priv->lock) {
341                 elm_image_file_get(priv->lock, &file, NULL);
342                 elm_object_part_content_unset(priv->lock,
343                                 PART_USER_EDIT_SWITCH);
344                 evas_object_del(priv->lock);
345         }
346
347         if (!file || !strcmp(file, IMAGE_SWITCH_OFF))
348                 file = IMAGE_SWITCH_ON;
349         else
350                 file = IMAGE_SWITCH_OFF;
351         lock = utils_add_icon(ly, file, PART_USER_EDIT_SWITCH);
352
353         priv->lock = lock;
354 }
355
356 static void _lock_key_down(int id, void *data, Evas *e, Evas_Object *obj,
357                 Evas_Event_Key_Down *ev)
358 {
359         if (!strcmp(ev->keyname, KEY_ENTER))
360                 _load_lock_icon(data, obj);
361 }
362
363 static input_handler lock_handler = {
364         .focused = _focused,
365         .unfocused = _unfocused,
366         .key_down = _lock_key_down
367 };
368
369 static Evas_Object *_add_layout(struct _priv *priv)
370 {
371         Evas_Object *ly, *photo, *name, *pin, *account, *lock;
372
373         ly = utils_add_layout(priv->base, GRP_USER_EDIT_CONTENTS, false,
374                         PART_USER_EDIT_CONTENTS);
375         if (!ly) {
376                 _ERR("failed to add layout");
377                 return NULL;
378         }
379
380         photo = utils_add_layout(ly, GRP_USER_EDIT_PHOTO, true,
381                         PART_USER_EDIT_CONTENTS_PHOTO);
382         if (!photo)
383                 goto err;
384
385         name = utils_add_entry(ly, MESSAGE_ENTRY_NAME, false,
386                         PART_USER_EDIT_CONTENTS_NAME);
387         if (!name)
388                 goto err;
389
390         pin = utils_add_entry(ly, MESSAGE_ENTRY_PIN, false,
391                         PART_USER_EDIT_CONTENTS_PIN);
392         if (!pin)
393                 goto err;
394
395         account = utils_add_entry(ly, MESSAGE_ENTRY_ACCOUNT, false,
396                         PART_USER_EDIT_CONTENTS_ACCOUNT);
397         if (!account)
398                 goto err;
399
400         lock = utils_add_layout(ly, GRP_USER_EDIT_SWITCH, true,
401                         PART_USER_EDIT_CONTENTS_LOCK);
402         if (!lock)
403                 goto err;
404
405         _load_lock_icon(priv, lock);
406
407         inputmgr_add_callback(lock, 0, &lock_handler, priv);
408         inputmgr_add_callback(photo, 0, &photo_handler, priv);
409
410         priv->photo = photo;
411         priv->name = name;
412         priv->account = account;
413         priv->pin = pin;
414
415         return ly;
416 err:
417         _ERR("failed to add layout");
418         evas_object_del(ly);
419         return NULL;
420 }
421
422 static bool _add_user_edit(struct _priv *priv)
423 {
424         Evas_Object *ly;
425
426         ly = _add_layout(priv);
427         if (!ly) {
428                 _ERR("failed to add edit layout");
429                 return false;
430         }
431
432         if (!_add_btns(priv)) {
433                 _ERR("failed to add buttons");
434                 evas_object_del(ly);
435                 return false;
436         }
437
438         /* It should be implemented later about function btn */
439
440         priv->ly = ly;
441
442         return true;
443 }
444
445 static Evas_Object *_create(Evas_Object *win, void *data)
446 {
447         struct _priv *priv;
448         Evas_Object *base;
449
450         if (!win || !data) {
451                 _ERR("Invalid argument");
452                 return NULL;
453         }
454
455         priv = calloc(1, sizeof(*priv));
456         if (!priv) {
457                 _ERR("failed to calloc priv");
458                 return NULL;
459         }
460
461         base = utils_add_layout(win, GRP_USER_EDIT, false, NULL);
462         if (!base) {
463                 _ERR("failed to create base");
464                 free(priv);
465                 return NULL;
466         }
467         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
468                         EVAS_HINT_EXPAND);
469         elm_win_resize_object_add(win, base);
470
471         priv->win = win;
472         priv->base = base;
473         priv->dm = data;
474
475         if (!_add_user_edit(priv)) {
476                 _ERR("failed to add user edit layout");
477                 evas_object_del(base);
478                 free(priv);
479                 return NULL;
480         }
481
482         viewmgr_set_view_data(VIEW_USER_EDIT, priv);
483         viewmgr_add_view(view_photo_get_vclass(), NULL);
484
485         return base;
486 }
487
488 static void _show(void *data)
489 {
490         struct _priv *priv;
491
492         if (!data) {
493                 _ERR("Invalid argument");
494                 return;
495         }
496
497         priv = data;
498
499         evas_object_show(priv->base);
500         elm_object_part_text_set(priv->base, PART_USER_EDIT_TITLE,
501                         MESSAGE_ADD_USER);
502         _load_user_icon(priv, IMAGE_USER_CURRENT_DEFAULT,
503                         IMAGE_USER_CURRENT_DEFAULT_FOCUS);
504 }
505
506 static void _hide(void *data)
507 {
508         struct _priv *priv;
509
510         if (!data) {
511                 _ERR("Invalid argument");
512                 return;
513         }
514
515         priv = data;
516
517         evas_object_hide(priv->base);
518 }
519
520 static void _destroy(void *data)
521 {
522         struct _priv *priv;
523
524         if (!data) {
525                 _ERR("Invalid argument");
526                 return;
527         }
528
529         priv = data;
530
531         inputmgr_remove_callback(priv->photo, &photo_handler);
532         inputmgr_remove_callback(priv->photo, &lock_handler);
533         inputmgr_remove_callback(priv->done, &done_handler);
534         inputmgr_remove_callback(priv->cancel, &cancel_handler);
535         viewmgr_remove_view(VIEW_PHOTO);
536         evas_object_del(priv->base);
537         free(priv);
538 }
539
540 static view_class vclass = {
541         .view_id = VIEW_USER_EDIT,
542         .create = _create,
543         .show = _show,
544         .hide = _hide,
545         .destroy = _destroy
546 };
547
548 view_class *view_user_edit_get_vclass(void)
549 {
550         return &vclass;
551 }
552