89bdc30cb690508a12b4371a636356a3d554dc83
[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
37 #define CTXPOPUP_X 294
38 #define CTXPOPUP_Y 310
39
40 static const char *icon_info[][2] = {
41         { IMAGE_USER_DEFAULT, IMAGE_USER_DEFAULT_FOCUS },
42         { IMAGE_USER_DEFAULT_02, IMAGE_USER_DEFAULT_02_FOCUS },
43         { IMAGE_USER_DEFAULT_03, IMAGE_USER_DEFAULT_03_FOCUS },
44         { IMAGE_USER_DEFAULT_04, IMAGE_USER_DEFAULT_04_FOCUS },
45         { IMAGE_USER_DEFAULT_05, IMAGE_USER_DEFAULT_05_FOCUS },
46         { IMAGE_USER_DEFAULT_06, IMAGE_USER_DEFAULT_06_FOCUS },
47         { IMAGE_USER_DEFAULT_07, IMAGE_USER_DEFAULT_07_FOCUS },
48         { IMAGE_USER_ADD, IMAGE_USER_ADD_FOCUS }
49 };
50
51 struct _priv {
52         Evas_Object *win;
53         Evas_Object *base;
54         Evas_Object *ly;
55         Evas_Object *photo;
56         Evas_Object *name;
57         Evas_Object *account;
58         Evas_Object *pin;
59         Evas_Object *lock;
60         Evas_Object *icon;
61         Evas_Object *focus_icon;
62         Evas_Object *done;
63         Evas_Object *cancel;
64         Evas_Object *ctxpopup;
65
66         struct datamgr *dm;
67 };
68
69 static void _done_key_down(int id, void *data, Evas *e, Evas_Object *obj,
70                 Evas_Event_Key_Down *ev)
71 {
72         if (!strcmp(ev->keyname, KEY_ENTER)) {
73                 /* check state of user edit */
74                 viewmgr_pop_view();
75         }
76 }
77
78 static input_handler done_handler = {
79         .key_down = _done_key_down
80 };
81
82 static void _cancel_key_down(int id, void *data, Evas *e, Evas_Object *obj,
83                 Evas_Event_Key_Down *ev)
84 {
85         if (!strcmp(ev->keyname, KEY_ENTER)) {
86                 viewmgr_pop_view();
87         }
88 }
89
90 static input_handler cancel_handler = {
91         .key_down = _cancel_key_down
92 };
93
94 static bool _add_btns(struct _priv *priv)
95 {
96         Evas_Object *done, *cancel;
97
98         done = utils_add_button(priv->base, MESSAGE_BTN_DONE,
99                         PART_USER_EDIT_BTN_DONE);
100         if (!done) {
101                 _ERR("failed to add done btn");
102                 return false;
103         }
104         inputmgr_add_callback(done, 0, &done_handler, NULL);
105
106         cancel = utils_add_button(priv->base, MESSAGE_BTN_CANCEL,
107                         PART_USER_EDIT_BTN_CANCEL);
108         if (!cancel) {
109                 _ERR("failed to add cancel btn");
110                 evas_object_del(done);
111                 return false;
112         }
113         inputmgr_add_callback(cancel, 0, &cancel_handler, NULL);
114
115         priv->cancel = cancel;
116         priv->done = done;
117
118         return true;
119 }
120
121 static void _focused(int id, void *data, Evas_Object *obj,
122                 Elm_Object_Item *item)
123 {
124         elm_object_signal_emit(obj, SIG_FOCUS, SRC_PROG);
125 }
126
127 static void _unfocused(int id, void *data, Evas_Object *obj,
128                 Elm_Object_Item *item)
129 {
130         elm_object_signal_emit(obj, SIG_UNFOCUS, SRC_PROG);
131 }
132
133 static void _eo_key_down(int id, void *data, Evas *e, Evas_Object *obj,
134                 Evas_Event_Key_Down *ev)
135 {
136         if (!strcmp(ev->keyname, KEY_BACK) ||
137                         !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
138                 evas_object_del(data);
139         } else if (!strcmp(ev->keyname, KEY_ENTER) ||
140                         !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
141                 /* It should be implemented later */
142                 evas_object_del(data);
143                 viewmgr_push_view(VIEW_PHOTO);
144         }
145 }
146
147 static input_handler icon_handler = {
148         .focused = _focused,
149         .unfocused = _unfocused,
150         .key_down = _eo_key_down
151 };
152
153 static Evas_Object *_pack_icon(Evas_Object *table, const char *icon_file,
154                 const char *focus_icon_file, int i)
155 {
156         Evas_Object *eo, *ic, *focus_ic;
157
158         eo = utils_add_layout(table, GRP_USER_EDIT_ICON_LIST_ITEM, true, NULL);
159         if (!eo) {
160                 _ERR("failed to add layout");
161                 return NULL;
162         }
163         evas_object_show(eo);
164         elm_table_pack(table, eo, i % 4, i / 4, 1, 1);
165
166         ic = utils_add_icon(eo, icon_file, PART_USER_EDIT_ICON_LIST_ITEM);
167         if (!ic) {
168                 _ERR("failed to add icon");
169                 evas_object_del(eo);
170                 return NULL;
171         }
172
173         focus_ic = utils_add_icon(eo, focus_icon_file,
174                         PART_USER_EDIT_ICON_LIST_ITEM_FOCUS);
175         if (!focus_ic) {
176                 _ERR("failed to add focus icon");
177                 evas_object_del(eo);
178                 return NULL;
179         }
180
181         return eo;
182 }
183
184 static void _add_icon_list(struct _priv *priv)
185 {
186         Evas_Coord x, y, w, h;
187         Evas_Object *ctxpopup, *ly, *table, *eo;
188         int i;
189
190         ctxpopup = elm_ctxpopup_add(priv->base);
191         if (!ctxpopup) {
192                 _ERR("failed to add ctxpopup");
193                 return;
194         }
195         elm_ctxpopup_horizontal_set(ctxpopup, EINA_TRUE);
196         elm_ctxpopup_auto_hide_disabled_set(ctxpopup, EINA_TRUE);
197
198         ly = utils_add_layout(ctxpopup, GRP_USER_EDIT_ICON_LIST, false, NULL);
199         if (!ly) {
200                 _ERR("failed to layout");
201                 evas_object_del(ctxpopup);
202                 return;
203         }
204         elm_object_content_set(ctxpopup, ly);
205         evas_object_show(ly);
206         evas_object_show(ctxpopup);
207
208         evas_object_geometry_get(priv->photo, &x, &y, &w, &h);
209         evas_object_move(ctxpopup,
210                         (x + w + CTXPOPUP_X) * elm_config_scale_get(),
211                         (y + CTXPOPUP_Y) * elm_config_scale_get());
212
213         table = utils_add_table(ly, true, PART_USER_EDIT_ICON_LIST);
214         if (!table) {
215                 _ERR("failed to add table");
216                 evas_object_del(ctxpopup);
217                 return;
218         }
219         elm_table_padding_set(table, elm_config_scale_get() * 1,
220                         elm_config_scale_get() * 1);
221         evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND,
222                         EVAS_HINT_EXPAND);
223
224         for (i = 0; i < sizeof(icon_info) / sizeof(*icon_info); i++) {
225                 eo = _pack_icon(table, icon_info[i][0], icon_info[i][1], i);
226                 if (!eo)
227                         continue;
228
229                 inputmgr_add_callback(eo, 0, &icon_handler, ctxpopup);
230                 if (i == 0)
231                         elm_object_focus_set(eo, EINA_TRUE);
232         }
233
234         priv->ctxpopup = ctxpopup;
235 }
236
237 static void _photo_key_down(int id, void *data, Evas *e, Evas_Object *obj,
238                 Evas_Event_Key_Down *ev)
239 {
240         if (!strcmp(ev->keyname, KEY_ENTER)) {
241                 _add_icon_list(data);
242         }
243 }
244
245 static input_handler photo_handler = {
246         .focused = _focused,
247         .unfocused = _unfocused,
248         .key_down = _photo_key_down
249 };
250
251 static Evas_Object *_add_layout(struct _priv *priv)
252 {
253         Evas_Object *ly, *photo, *name, *pin, *account, *lock;
254
255         ly = utils_add_layout(priv->base, GRP_USER_EDIT_CONTENTS, false,
256                         PART_USER_EDIT_CONTENTS);
257         if (!ly) {
258                 _ERR("failed to add layout");
259                 return NULL;
260         }
261
262         photo = utils_add_layout(ly, GRP_USER_EDIT_PHOTO, true,
263                         PART_USER_EDIT_CONTENTS_PHOTO);
264         if (!photo)
265                 goto err;
266
267         name = utils_add_entry(ly, MESSAGE_ENTRY_NAME, false,
268                         PART_USER_EDIT_CONTENTS_NAME);
269         if (!name)
270                 goto err;
271
272         pin = utils_add_entry(ly, MESSAGE_ENTRY_PIN, false,
273                         PART_USER_EDIT_CONTENTS_PIN);
274         if (!pin)
275                 goto err;
276
277         account = utils_add_entry(ly, MESSAGE_ENTRY_ACCOUNT, false,
278                         PART_USER_EDIT_CONTENTS_ACCOUNT);
279         if (!account)
280                 goto err;
281
282         lock = utils_add_layout(ly, GRP_USER_EDIT_SWITCH, true,
283                         PART_USER_EDIT_CONTENTS_LOCK);
284         if (!lock)
285                 goto err;
286
287         inputmgr_add_callback(photo, 0, &photo_handler, priv);
288
289         priv->photo = photo;
290         priv->name = name;
291         priv->account = account;
292         priv->pin = pin;
293         priv->lock = lock;
294
295         return ly;
296 err:
297         _ERR("failed to add layout");
298         evas_object_del(ly);
299         return NULL;
300 }
301
302 static bool _add_user_edit(struct _priv *priv)
303 {
304         Evas_Object *ly;
305
306         ly = _add_layout(priv);
307         if (!ly) {
308                 _ERR("failed to add edit layout");
309                 return false;
310         }
311
312         if (!_add_btns(priv)) {
313                 _ERR("failed to add buttons");
314                 evas_object_del(ly);
315                 return false;
316         }
317
318         /* It should be implemented later about function btn */
319
320         priv->ly = ly;
321
322         return true;
323 }
324
325 static Evas_Object *_create(Evas_Object *win, void *data)
326 {
327         struct _priv *priv;
328         Evas_Object *base;
329
330         if (!win || !data) {
331                 _ERR("Invalid argument");
332                 return NULL;
333         }
334
335         priv = calloc(1, sizeof(*priv));
336         if (!priv) {
337                 _ERR("failed to calloc priv");
338                 return NULL;
339         }
340
341         base = utils_add_layout(win, GRP_USER_EDIT, false, NULL);
342         if (!base) {
343                 _ERR("failed to create base");
344                 free(priv);
345                 return NULL;
346         }
347         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
348                         EVAS_HINT_EXPAND);
349         elm_win_resize_object_add(win, base);
350
351         priv->win = win;
352         priv->base = base;
353         priv->dm = data;
354
355         if (!_add_user_edit(priv)) {
356                 _ERR("failed to add user edit layout");
357                 evas_object_del(base);
358                 free(priv);
359                 return NULL;
360         }
361
362         viewmgr_set_view_data(VIEW_USER_EDIT, priv);
363         viewmgr_add_view(view_photo_get_vclass(), NULL);
364
365         return base;
366 }
367
368 static void _load_user_edit(struct _priv *priv)
369 {
370         Evas_Object *icon, *focus_icon;
371
372         /* check state of user edit */
373
374         elm_object_part_text_set(priv->base, PART_USER_EDIT_TITLE,
375                         MESSAGE_ADD_USER);
376         icon = utils_add_icon(priv->photo, IMAGE_USER_CURRENT_DEFAULT,
377                         PART_USER_EDIT_PHOTO);
378         if (!icon)
379                 _ERR("failed to add icon");
380
381         focus_icon = utils_add_icon(priv->photo,
382                         IMAGE_USER_CURRENT_DEFAULT_FOCUS,
383                         PART_USER_EDIT_PHOTO_FOCUS);
384         if (!focus_icon)
385                 _ERR("failed to add focus icon");
386
387         priv->icon = icon;
388         priv->focus_icon = focus_icon;
389 }
390
391 static void _show(void *data)
392 {
393         struct _priv *priv;
394
395         if (!data) {
396                 _ERR("Invalid argument");
397                 return;
398         }
399
400         priv = data;
401
402         evas_object_show(priv->base);
403
404         _load_user_edit(priv);
405 }
406
407 static void _hide(void *data)
408 {
409         struct _priv *priv;
410
411         if (!data) {
412                 _ERR("Invalid argument");
413                 return;
414         }
415
416         priv = data;
417
418         evas_object_hide(priv->base);
419 }
420
421 static void _destroy(void *data)
422 {
423         struct _priv *priv;
424
425         if (!data) {
426                 _ERR("Invalid argument");
427                 return;
428         }
429
430         priv = data;
431
432         inputmgr_remove_callback(priv->photo, &photo_handler);
433         inputmgr_remove_callback(priv->done, &done_handler);
434         inputmgr_remove_callback(priv->cancel, &cancel_handler);
435         viewmgr_remove_view(VIEW_PHOTO);
436         evas_object_del(priv->base);
437         free(priv);
438 }
439
440 static view_class vclass = {
441         .view_id = VIEW_USER_EDIT,
442         .create = _create,
443         .show = _show,
444         .hide = _hide,
445         .destroy = _destroy
446 };
447
448 view_class *view_user_edit_get_vclass(void)
449 {
450         return &vclass;
451 }
452