124d5b5280852ea404d53a9fdbfa9405eb19b4e7
[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 Evas_Object *_add_layout(struct _priv *priv)
336 {
337         Evas_Object *ly, *photo, *name, *pin, *account, *lock;
338
339         ly = utils_add_layout(priv->base, GRP_USER_EDIT_CONTENTS, false,
340                         PART_USER_EDIT_CONTENTS);
341         if (!ly) {
342                 _ERR("failed to add layout");
343                 return NULL;
344         }
345
346         photo = utils_add_layout(ly, GRP_USER_EDIT_PHOTO, true,
347                         PART_USER_EDIT_CONTENTS_PHOTO);
348         if (!photo)
349                 goto err;
350
351         name = utils_add_entry(ly, MESSAGE_ENTRY_NAME, false,
352                         PART_USER_EDIT_CONTENTS_NAME);
353         if (!name)
354                 goto err;
355
356         pin = utils_add_entry(ly, MESSAGE_ENTRY_PIN, false,
357                         PART_USER_EDIT_CONTENTS_PIN);
358         if (!pin)
359                 goto err;
360
361         account = utils_add_entry(ly, MESSAGE_ENTRY_ACCOUNT, false,
362                         PART_USER_EDIT_CONTENTS_ACCOUNT);
363         if (!account)
364                 goto err;
365
366         lock = utils_add_layout(ly, GRP_USER_EDIT_SWITCH, true,
367                         PART_USER_EDIT_CONTENTS_LOCK);
368         if (!lock)
369                 goto err;
370
371         inputmgr_add_callback(photo, 0, &photo_handler, priv);
372
373         priv->photo = photo;
374         priv->name = name;
375         priv->account = account;
376         priv->pin = pin;
377         priv->lock = lock;
378
379         return ly;
380 err:
381         _ERR("failed to add layout");
382         evas_object_del(ly);
383         return NULL;
384 }
385
386 static bool _add_user_edit(struct _priv *priv)
387 {
388         Evas_Object *ly;
389
390         ly = _add_layout(priv);
391         if (!ly) {
392                 _ERR("failed to add edit layout");
393                 return false;
394         }
395
396         if (!_add_btns(priv)) {
397                 _ERR("failed to add buttons");
398                 evas_object_del(ly);
399                 return false;
400         }
401
402         /* It should be implemented later about function btn */
403
404         priv->ly = ly;
405
406         return true;
407 }
408
409 static Evas_Object *_create(Evas_Object *win, void *data)
410 {
411         struct _priv *priv;
412         Evas_Object *base;
413
414         if (!win || !data) {
415                 _ERR("Invalid argument");
416                 return NULL;
417         }
418
419         priv = calloc(1, sizeof(*priv));
420         if (!priv) {
421                 _ERR("failed to calloc priv");
422                 return NULL;
423         }
424
425         base = utils_add_layout(win, GRP_USER_EDIT, false, NULL);
426         if (!base) {
427                 _ERR("failed to create base");
428                 free(priv);
429                 return NULL;
430         }
431         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
432                         EVAS_HINT_EXPAND);
433         elm_win_resize_object_add(win, base);
434
435         priv->win = win;
436         priv->base = base;
437         priv->dm = data;
438
439         if (!_add_user_edit(priv)) {
440                 _ERR("failed to add user edit layout");
441                 evas_object_del(base);
442                 free(priv);
443                 return NULL;
444         }
445
446         viewmgr_set_view_data(VIEW_USER_EDIT, priv);
447         viewmgr_add_view(view_photo_get_vclass(), NULL);
448
449         return base;
450 }
451
452 static void _show(void *data)
453 {
454         struct _priv *priv;
455
456         if (!data) {
457                 _ERR("Invalid argument");
458                 return;
459         }
460
461         priv = data;
462
463         evas_object_show(priv->base);
464         elm_object_part_text_set(priv->base, PART_USER_EDIT_TITLE,
465                         MESSAGE_ADD_USER);
466         _load_user_icon(priv, IMAGE_USER_CURRENT_DEFAULT,
467                         IMAGE_USER_CURRENT_DEFAULT_FOCUS);
468 }
469
470 static void _hide(void *data)
471 {
472         struct _priv *priv;
473
474         if (!data) {
475                 _ERR("Invalid argument");
476                 return;
477         }
478
479         priv = data;
480
481         evas_object_hide(priv->base);
482 }
483
484 static void _destroy(void *data)
485 {
486         struct _priv *priv;
487
488         if (!data) {
489                 _ERR("Invalid argument");
490                 return;
491         }
492
493         priv = data;
494
495         inputmgr_remove_callback(priv->photo, &photo_handler);
496         inputmgr_remove_callback(priv->done, &done_handler);
497         inputmgr_remove_callback(priv->cancel, &cancel_handler);
498         viewmgr_remove_view(VIEW_PHOTO);
499         evas_object_del(priv->base);
500         free(priv);
501 }
502
503 static view_class vclass = {
504         .view_id = VIEW_USER_EDIT,
505         .create = _create,
506         .show = _show,
507         .hide = _hide,
508         .destroy = _destroy
509 };
510
511 view_class *view_user_edit_get_vclass(void)
512 {
513         return &vclass;
514 }
515