5ca26b617c81b9a7fb2006f2f238e21b40b30597
[platform/core/security/ode.git] / tools / apps / ode / src / password / confirm-popup.c
1 /*
2  *
3  * Copyright (c) 2017 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 <ode/external-encryption.h>
19 #include "ode-password.h"
20
21 #define EDJ_PATH "/usr/apps/" PACKAGE "/res/" PACKAGE ".edj"
22
23 static Ecore_Timer *timer = NULL;
24 static Evas_Object *confirm_layout = NULL;
25 static Evas_Object *next_button = NULL;
26 static Evas_Object *confirm_entry = NULL;
27 static int limit_count = 0;
28 static int limit_time = 0;
29
30 static void update_title_text(void)
31 {
32         char text[PATH_MAX] = "";
33         const char *time_text = NULL;
34
35         if (limit_time != 0) {
36                 time_text = __("IDS_ST_BODY_ENTER_PASSWORD_IN_PD_SECONDS");
37                 snprintf(text, PATH_MAX, time_text, 6-limit_time);
38                 elm_object_part_text_set(confirm_layout, "title", text);
39         } else {
40                 elm_object_part_text_set(confirm_layout, "title", __("IDS_LCKSCN_NPBODY_ENTER_PASSWORD"));
41         }
42         return;
43 }
44
45 static Eina_Bool limit_count_timer_cb(void *data)
46 {
47         limit_time++;
48         update_title_text();
49
50         if (limit_time == 6) {
51                 limit_time = 0;
52                 ecore_timer_del(timer);
53                 timer = NULL;
54                 update_title_text();
55                 elm_object_disabled_set(next_button, EINA_FALSE);
56                 return ECORE_CALLBACK_DONE;
57         }
58
59         return ECORE_CALLBACK_PASS_ON;
60 }
61
62 static void update_content_text(void)
63 {
64         char text[PATH_MAX] = "";
65         char count_text[PATH_MAX] = "";
66         const char *pd_text = NULL;
67         int count = 15-limit_count;
68
69         if (count == 0) {
70                 return;
71         }
72
73         if (limit_count < 10) {
74                 pd_text = __("IDS_ST_BODY_YOU_HAVE_PD_ATTEMPTS_LEFT");
75                 snprintf(count_text, PATH_MAX, pd_text, count);
76                 snprintf(text, PATH_MAX, "%s<br>%s", __("IDS_LCKSCN_NPBODY_INCORRECT_PASSWORD_ENTERED"), count_text);
77         } else {
78                 pd_text = __("IDS_ST_BODY_IF_AN_INCORRECT_PASSWORD_IS_ENTERED_PD_MORE_TIMES_YOU_WILL_NO_LONGER_BE_ABLE_MSG");
79                 snprintf(count_text, PATH_MAX, pd_text, count);
80                 snprintf(text, PATH_MAX, "%s<br>%s", __("IDS_LCKSCN_NPBODY_INCORRECT_PASSWORD_ENTERED"), count_text);
81         }
82
83         elm_object_part_text_set(confirm_layout, "content", text);
84
85         if (limit_count > 5) {
86                 elm_object_disabled_set(next_button, EINA_TRUE);
87                 timer = ecore_timer_add(1, limit_count_timer_cb, NULL);
88         }
89
90         return;
91 }
92
93
94 static void key_event_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
95 {
96         Evas_Event_Key_Down *ev = (Evas_Event_Key_Down *)event_info;
97         popup_data_s *popup_data = (popup_data_s *)data;
98
99         if (!strcmp(ev->keyname, "XF86Back")) {
100                 evas_object_del(popup_data->popup);
101                 if (popup_data->mode == INSERT_SD_CARD)
102                         create_insert_sdcard_popup(popup_data);
103         }
104         return;
105 }
106
107 static void confirm_password_popup_cancel_cb(void *data, Evas_Object *obj, void *event_info)
108 {
109         popup_data_s *popup_data = (popup_data_s *)data;
110
111         evas_object_del(popup_data->popup);
112         if (popup_data->mode == INSERT_SD_CARD)
113                 create_insert_sdcard_popup(popup_data);
114         return;
115 }
116
117 static void confirm_password_popup_confirm_cb(void *data, Evas_Object *obj, void *event_info)
118 {
119         int ret = 0;
120         bool result = 0;
121         popup_data_s *popup_data = (popup_data_s *)data;
122
123         ret = ode_external_encryption_verify_password(elm_entry_entry_get(confirm_entry), &result);
124         if (ret != ODE_ERROR_NONE) {
125                 dlog_print(DLOG_ERROR, LOG_TAG, "failed to verify password");
126                 return;
127         }
128
129         if (!result) {
130                 dlog_print(DLOG_ERROR, LOG_TAG, "password not matched");
131                 limit_count++;
132                 if (limit_count == 15) {
133                         evas_object_del(popup_data->popup);
134                         create_reset_sdcard_popup(popup_data);
135                 }
136                 update_content_text();
137                 return;
138         }
139
140         if (popup_data->mode == CHANGE_PASSWORD) {
141                 snprintf(popup_data->entry_data_old, PATH_MAX, "%s", elm_entry_entry_get(confirm_entry));
142                 evas_object_del(popup_data->popup);
143                 create_password_message_popup(popup_data);
144         } else {
145                 if (popup_data->entry_data)
146                         snprintf(popup_data->entry_data, PATH_MAX, "%s", elm_entry_entry_get(confirm_entry));
147                 evas_object_del(popup_data->popup);
148                 password_result_callback(popup_data, "success");
149         }
150
151         return;
152 }
153
154 void create_confirm_password_popup(popup_data_s *data)
155 {
156         Evas_Object *cancel_button = NULL;
157
158         limit_count = 0;
159         limit_time = 0;
160
161         if (data->mode == INSERT_SD_CARD)
162                 data->popup = ode_create_popup(data->parent, __("IDS_ST_HEADER_USE_ENCRYPTED_SD_CARD"), NULL);
163         else
164                 data->popup = ode_create_popup(data->parent, __("IDS_ST_BODY_PASSWORD"), NULL);
165
166         confirm_layout = ode_create_layout(data->popup, EDJ_PATH, "entry_popup");
167         elm_object_part_content_set(data->popup, "elm.swallow.content", confirm_layout);
168
169         update_title_text();
170
171         confirm_entry = ode_create_entry(confirm_layout);
172         elm_object_part_content_set(confirm_layout, "entry", confirm_entry);
173
174         cancel_button = ode_create_popup_button(data->popup, __("IDS_ST_BUTTON_CANCEL"),
175                                                         confirm_password_popup_cancel_cb, data);
176         next_button = ode_create_popup_button(data->popup, __("IDS_ST_BUTTON_DONE"),
177                                                         confirm_password_popup_confirm_cb, data);
178         elm_object_part_content_set(data->popup, "button1", cancel_button);
179         elm_object_part_content_set(data->popup, "button2", next_button);
180
181         evas_object_event_callback_add(data->popup, EVAS_CALLBACK_KEY_DOWN, key_event_cb, data);
182         evas_object_show(data->popup);
183         return;
184 }