2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "view_pwd_popup.h"
23 #include "settings_provider.h"
25 #define PWD_DATA_ID "change_passcode_data"
26 #define CTXPOPUP_WIDTH 688
27 #define CTXPOPUP_HEIGHT 411
30 #define BOX_HPADDING 10
31 #define BOX_VPADDING 0
33 #define MSGID_PWD_TITLE "Change passcode"
34 #define MSGID_UPTEXT "Enter your old passcode"
35 #define MSGID_DOWNTEXT "Default passcode is 0000"
36 #define MSGID_INVALID_PWD "Failed passcode attempt"
37 #define MSGID_NEW_PWD "Enter your new passcode"
38 #define MSGID_REENTER_PWD "Re-enter your new passcode"
39 #define MSGID_DIFFER_PWD "Not same passcode. Enter again"
40 #define MSGID_CANCEL "Cancel"
41 #define PWD_SYMBOL "*"
42 #define PWD_RANGE "0123456789"
44 #define SIG_CLICKED "clicked"
45 #define SIG_DISMISSED "dismissed"
59 Evas_Object *cancel_btn;
60 Evas_Object *ctxpopup;
66 char entered_pwd[PWD_SIZE];
67 char reentered_pwd[PWD_SIZE];
73 struct setting_mgr *mgr;
74 struct settingview_data *view;
77 static void _hide(Evas_Object *base);
82 * @param[in]: data : the user data of change passcode view.
86 static void _free_pwd_data(struct _pwd_data *data)
92 evas_object_del(data->bg);
95 evas_object_del(data->base);
98 evas_object_del(data->ctxpopup);
101 eina_array_free(data->array);
107 * Create background of change passcode view.
109 * @param[in]: win : the window this view created on.
111 * @return: the created background of type evas object.
113 static Evas_Object *_add_pwd_bg(Evas_Object *win)
120 bg = utils_add_base(win);
122 _ERR("elm layout add failed.");
126 elm_layout_file_set(bg, EDJ_FILE, GRP_PWD_BG);
132 * Create the base layout of change passcode view.
134 * @param[in]: ctxpopup : the ctxpopup which contains change passcode view.
136 * @return: the created base layout of change passcode view.
138 static Evas_Object *_add_pwd_base(Evas_Object *ctxpopup)
145 base = utils_add_base(ctxpopup);
147 _ERR("elm layout add failed.");
151 elm_layout_file_set(base, EDJ_FILE, GRP_PWD_VIEW);
153 elm_object_part_text_set(base, PART_TITLE,
154 utils_get_translation_str(MSGID_PWD_TITLE));
155 elm_object_part_text_set(base, PART_UPTEXT,
156 utils_get_translation_str(MSGID_UPTEXT));
157 elm_object_part_text_set(base, PART_DOWNTEXT,
158 utils_get_translation_str(MSGID_DOWNTEXT));
164 * Evas_Smart_Cb type callback for handling click event.
166 * @param[in]: priv : the user data.
167 * @param[in]: obj : the corresponding object which the click event occurred.
168 * @param[in]: ev : event info.
172 static void _cancelbtn_clicked_cb(void *priv, Evas_Object *obj, void *ev)
174 struct _pwd_data *data;
179 data = (struct _pwd_data *) priv;
181 settingmgr_view_pop(data->mgr);
185 * Create cancel button of change passcode view.
187 * @param[in]: data : the user data of change passcode view.
189 * @return: the created cancel button or null if error occurred.
191 static Evas_Object *_add_pwd_cancel_btn(struct _pwd_data *data)
195 if (!data || !data->base)
198 btn = utils_add_btn(data->base, PWD_BASIC_BTN,
199 utils_get_translation_str((const char *) MSGID_CANCEL), EINA_TRUE);
201 _ERR("utils add button failed.");
205 evas_object_smart_callback_add(btn, SIG_CLICKED,
206 _cancelbtn_clicked_cb, data);
208 elm_object_part_content_set(data->base, SWALLOW_CANCEL_BTN, btn);
210 elm_object_focus_allow_set(btn, EINA_TRUE);
216 * Set the move directions of focus between entries and cancel button.
218 * @param[in]: array : eina array containing entries.
219 * @param[in]: cancel_btn : the cancel button.
223 static void _set_entry_directions(Eina_Array *array, Evas_Object *cancel_btn)
229 if (!array || !cancel_btn)
232 for (i = ENTRY_0; i < ENTRY_3; i++) {
233 en = (Evas_Object *) eina_array_data_get(array, i);
237 elm_object_focus_next_object_set(en,
238 (Evas_Object *) eina_array_data_get(array, i + 1),
240 elm_object_focus_next_object_set(en, en, ELM_FOCUS_LEFT);
241 elm_object_focus_next_object_set(en, en, ELM_FOCUS_RIGHT);
242 elm_object_focus_next_object_set(en, en, ELM_FOCUS_UP);
243 elm_object_focus_next_object_set(en, cancel_btn,
247 lasten = (Evas_Object *) eina_array_data_get(array, ENTRY_3);
248 elm_object_focus_next_object_set(lasten, lasten, ELM_FOCUS_UP);
249 elm_object_focus_next_object_set(lasten, cancel_btn, ELM_FOCUS_DOWN);
250 elm_object_focus_next_object_set(lasten, lasten, ELM_FOCUS_LEFT);
251 elm_object_focus_next_object_set(lasten, lasten, ELM_FOCUS_RIGHT);
253 elm_object_focus_next_object_set(cancel_btn, cancel_btn,
255 elm_object_focus_next_object_set(cancel_btn, cancel_btn,
257 elm_object_focus_next_object_set(cancel_btn,
258 (Evas_Object *) eina_array_data_get(array, ENTRY_0), ELM_FOCUS_UP);
259 elm_object_focus_next_object_set(cancel_btn, cancel_btn,
266 * @param[in]: vdata : view data.
268 * @return: the passcode.
270 static char *_get_pwd(struct settingview_data *vdata)
272 struct settingitem *item;
277 item = viewdata_get_parentitem(vdata);
279 _ERR("get parent item failed.");
283 return provider_get_list_value(item);
287 * Check if the input passcode is right.
289 * @param[in]: data : the user data of change passcode view.
291 * @return: 0 - wrong passcode, 1 - right passcode.
293 static int _check_pwd(struct _pwd_data *data)
297 if (!data || !data->view)
300 pwd = _get_pwd(data->view);
302 _ERR("get passcode failed.");
306 if (strncmp(data->pwd, pwd, strlen(pwd))) {
319 * @param[in]: pwd : the passcode.
323 static void _clear_pwd(char *pwd)
340 * This function clears displayed passcode in entries when user
341 * inputs wrong passcode.
343 * @param[in]: data : the user data of change passcode view.
347 static void _reset_pwd(struct _pwd_data *data)
349 Evas_Object *btn, *first;
353 if (!data || !data->array || !data->base)
358 first = (Evas_Object *) eina_array_data_get(array, ENTRY_0);
362 elm_object_focus_set(first, EINA_TRUE);
363 elm_object_focus_next_object_set(data->cancel_btn, first, ELM_FOCUS_UP);
365 for (i = ENTRY_0; i < ENTRY_MAX; i++) {
366 btn = (Evas_Object *)eina_array_data_get(array, i);
370 elm_object_text_set(btn, "");
375 * Set passcode to system.
377 * @param[in]: data : the user data of change passcode view.
379 * @return: 0 - Success, -1 - Fail.
381 static int _set_pwd(struct _pwd_data *data)
384 struct settingitem *item;
386 if (!data || !data->view)
389 item = viewdata_get_parentitem(data->view);
391 _ERR("get parent item failed.");
395 ret = provider_set_list_value(item, data->pwd);
397 _ERR("set passcode failed.");
405 * Check if the reentered passcode is right.
407 * @param[in]: data : the user data of change passcode view.
409 * @return: 0 - Success, -1 - Fail.
411 static int _check_reentered_pwd(struct _pwd_data *data)
416 if (strncmp(data->entered_pwd, data->reentered_pwd,
424 * Update text of change passcode view.
426 * @param[in]: data : the user data of change passcode view.
427 * @param[in]: part : edje part of text.
428 * @param[in]: msgid : the message id of text.
432 static void _update_pwd_ui(struct _pwd_data *data,
433 const char *part, const char *msgid)
437 if (!data || !data->base || !msgid || !part)
442 text = utils_get_translation_str(msgid);
443 elm_object_part_text_set(data->base, part, text);
447 * Evas_Object_Event_Cb type callback for handling key press event.
449 * @param[in]: priv : the user data.
450 * @param[in]: e : the evas canvas.
451 * @param[in]: obj : the corresponding object which the key press event occurred.
452 * @param[in]: ei : event info.
456 static void _entry_keypress_cb(void *priv, Evas *e,
457 Evas_Object *obj, void *ei)
459 Evas_Event_Key_Down *event;
461 Evas_Object *next_entry;
462 struct _pwd_data *data;
466 if (!priv || !ei || !obj)
469 data = (struct _pwd_data *) priv;
474 event = (Evas_Event_Key_Down *) ei;
475 keyname = event->keyname;
480 for (i = ENTRY_0; i < ENTRY_MAX; i++) {
481 if (obj == eina_array_data_get(array, i)) {
487 if (!strncmp(keyname, KEY_BACK, strlen(keyname)) || !strncmp(keyname, KEY_BACK_REMOTE, strlen(keyname))) {
488 settingmgr_view_pop(data->mgr);
489 } else if (!strncmp(keyname, KEY_DOWN, strlen(keyname))) {
490 elm_object_focus_set(data->cancel_btn, EINA_TRUE);
491 } else if (!strncmp(keyname, KEY_0, strlen(keyname)) ||
492 !strncmp(keyname, KEY_1, strlen(keyname)) ||
493 !strncmp(keyname, KEY_2, strlen(keyname)) ||
494 !strncmp(keyname, KEY_3, strlen(keyname)) ||
495 !strncmp(keyname, KEY_4, strlen(keyname)) ||
496 !strncmp(keyname, KEY_5, strlen(keyname)) ||
497 !strncmp(keyname, KEY_6, strlen(keyname)) ||
498 !strncmp(keyname, KEY_7, strlen(keyname)) ||
499 !strncmp(keyname, KEY_8, strlen(keyname)) ||
500 !strncmp(keyname, KEY_9, strlen(keyname))) {
501 elm_entry_entry_set(obj, PWD_SYMBOL);
503 data->pwd[entry_idx] = keyname[0];
505 if (data->new_flag && !data->renew_flag)
506 data->entered_pwd[entry_idx] = keyname[0];
508 if (data->renew_flag)
509 data->reentered_pwd[entry_idx] = keyname[0];
511 if (entry_idx < ENTRY_3) {
512 elm_object_focus_next(obj, ELM_FOCUS_NEXT);
513 next_entry = elm_object_focus_next_object_get(obj,
516 _ERR("next entry is null.");
520 elm_object_focus_next_object_set(data->cancel_btn,
521 next_entry, ELM_FOCUS_UP);
523 if (!data->new_flag) {
524 if (_check_pwd(data)) {
525 _update_pwd_ui(data, PART_UPTEXT,
530 _update_pwd_ui(data, PART_UPTEXT,
533 } else if (!data->renew_flag) {
534 _update_pwd_ui(data, PART_UPTEXT,
537 data->renew_flag = 1;
539 if (_check_reentered_pwd(data)) {
540 if (_set_pwd(data) != 0) {
541 _ERR("set passcode failed.");
546 data->renew_flag = 0;
548 settingmgr_view_pop(data->mgr);
550 _update_pwd_ui(data, PART_UPTEXT,
553 data->renew_flag = 0;
564 * @param[in]: parent : object the entry will be created on.
566 * @return: the created entry or null if error occurred.
568 static Evas_Object *_add_entry(Evas_Object *parent)
571 Elm_Entry_Filter_Limit_Size length = {
574 Elm_Entry_Filter_Accept_Set type = {
575 .accepted = PWD_RANGE
581 entry = utils_add_entry(parent);
583 _ERR("utils add entry failed.");
587 elm_entry_markup_filter_append(entry,
588 elm_entry_filter_limit_size, &length);
589 elm_entry_markup_filter_append(entry,
590 elm_entry_filter_accept_set, &type);
592 elm_object_style_set(entry, STYLE_PWD_ENTRY);
594 elm_entry_text_style_user_push(entry, STYLE_ENTRY_TEXT);
595 elm_object_part_content_set(parent, SWALLOW_ENTRY, entry);
601 * Create entries for inputting passcode.
603 * @param[in]: data : the user data of change passcode view.
605 * @return: 0 - Success, -1 - Fail.
607 static int _add_pwd_entry(struct _pwd_data *data)
612 Evas_Object *entry_base;
615 if (!data || !data->base) {
616 _ERR("invalid arguments.");
620 box = utils_add_box(data->base);
622 _ERR("add box failed.");
626 elm_box_horizontal_set(box, EINA_TRUE);
627 elm_box_padding_set(box, BOX_HPADDING, BOX_VPADDING);
629 array = eina_array_new(1);
631 _ERR("new eina array failed.");
632 evas_object_del(box);
636 for (i = ENTRY_0; i < ENTRY_MAX; i++) {
637 entry_base = utils_add_base(data->base);
639 _ERR("entry base created failed.");
640 evas_object_del(box);
641 eina_array_free(array);
645 elm_layout_file_set(entry_base, EDJ_FILE, GRP_PWD_ENTRY);
647 entry = _add_entry(entry_base);
649 _ERR("add entry failed.");
650 evas_object_del(box);
651 evas_object_del(entry_base);
652 eina_array_free(array);
656 eina_array_push(array, entry);
657 elm_box_pack_end(box, entry_base);
659 evas_object_event_callback_add(entry, EVAS_CALLBACK_KEY_DOWN,
660 _entry_keypress_cb, data);
663 elm_object_part_content_set(data->base, SWALLOW_CONTENT_AREA, box);
665 elm_object_focus_allow_set(
666 (Evas_Object *) eina_array_data_get(array, ENTRY_0), EINA_TRUE);
667 elm_object_focus_set(
668 (Evas_Object *)eina_array_data_get(array, ENTRY_0), EINA_TRUE);
670 _set_entry_directions(array, data->cancel_btn);
679 * Evas_Smart_Cb type callback for handling ctxpopup dismissed event.
681 * @param[in]: priv : the user data.
682 * @param[in]: obj : the corresponding object which the dismissed event occurred.
683 * @param[in]: ev : event info.
687 static void _ctxpopup_dismissed(void *priv, Evas_Object *obj,
690 struct _pwd_data *data;
695 data = (struct _pwd_data *) priv;
697 if (!data->base || !data->mgr)
702 settingmgr_view_pop(data->mgr);
706 * Create all the UI components of change passcode view.
708 * @param[in]: mgr : view manager of settings views.
709 * @param[in]: view : data of view.
710 * @prev[in]: prev : the user data.
712 * @return: the base layout of change passcode view or null
715 static Evas_Object *_create(struct setting_mgr *mgr,
716 struct settingview_data *view, void *prev)
718 Evas_Object *base, *bg;
720 Evas_Object *cancel_btn;
721 Evas_Object *ctxpopup;
722 struct _pwd_data *data;
728 _ERR("invalid argument.");
732 win = settingmgr_get_win(mgr);
734 _ERR("settingmgr get window failed.");
738 data = (struct _pwd_data *)calloc(1, sizeof(*data));
740 _ERR("calloc _change_passcode_data failed.");
746 _clear_pwd(data->pwd);
748 bg = _add_pwd_bg(win);
750 _ERR("add change passcode background failed.");
754 ctxpopup = utils_add_ctxpopup(win, (char *) CHANGE_PWD_CTXPOPUP_STYLE);
756 _ERR("add ctxpopup failed.");
760 data->ctxpopup = ctxpopup;
762 base = _add_pwd_base(data->ctxpopup);
764 _ERR("add change passcode base layout failed.");
770 elm_object_content_set(ctxpopup, base);
773 ecore_x_window_size_get(ecore_x_window_root_first_get(),
776 xpos = (width - CTXPOPUP_WIDTH) / 2;
777 ypos = (height - CTXPOPUP_HEIGHT) / 2;
778 evas_object_move(data->ctxpopup, xpos * ELM_SCALE, ypos * ELM_SCALE);
780 cancel_btn = _add_pwd_cancel_btn(data);
782 _ERR("add change passcode cancel button failed.");
786 data->cancel_btn = cancel_btn;
788 ret = _add_pwd_entry(data);
790 _ERR("add change passcode input buttons failed.");
798 evas_object_smart_callback_add(data->ctxpopup,
799 SIG_DISMISSED, _ctxpopup_dismissed, data);
801 evas_object_data_set(base, PWD_DATA_ID, data);
806 _free_pwd_data(data);
811 * Show the change passcode view.
813 * @param[in]: base : the base layout of change passcode view.
817 static void _show(Evas_Object *base)
819 struct _pwd_data *data;
824 data = (struct _pwd_data *)evas_object_data_get(base, PWD_DATA_ID);
826 _ERR("get data from base failed. ID: %s.", PWD_DATA_ID);
830 evas_object_show(data->ctxpopup);
834 * Destroy the change passcode view.
836 * @param[in]: base : the base layout of change passcode view.
840 static void _destroy(Evas_Object *base)
842 struct _pwd_data *data;
847 data = (struct _pwd_data *)evas_object_data_get(base, PWD_DATA_ID);
849 _ERR("get data from base failed. ID: %s.", PWD_DATA_ID);
853 _free_pwd_data(data);
857 * Hide the change passcode view.
859 * @param[in]: base : the base layout of change passcode view.
863 static void _hide(Evas_Object *base)
865 struct _pwd_data *data;
870 data = (struct _pwd_data *)evas_object_data_get(base, PWD_DATA_ID);
872 _ERR("get data from base failed. ID: %s.",
877 evas_object_hide(data->bg);
878 evas_object_hide(data->ctxpopup);
882 * view class of change passcode view.
884 static struct setting_class _vclass = {
885 VCLASS_TITLE_CHANGE_PASSCODE,
901 * Return view class of change passcode view.
905 * @return: the view class of change passcode view.
907 struct setting_class *view_passcode_popup_get_vclass(void)