External: Fix bugs that decryption doesn't work properly by ui-app
[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         /* for temporary start */
124         if (popup_data->mode == INTERNAL_ENCRYPT_PASSWORD || popup_data->mode == INTERNAL_DECRYPT_PASSWORD || popup_data->mode == EXTERNAL_DECRYPT_PASSWORD) {
125                 snprintf(popup_data->entry_data, PATH_MAX, "%s", elm_entry_entry_get(confirm_entry));
126                 evas_object_del(popup_data->popup);
127                 password_result_callback(popup_data, "success");
128                 return;
129         }
130         /* for temporary end */
131         ret = ode_external_encryption_verify_password(elm_entry_entry_get(confirm_entry), &result);
132         if (ret != ODE_ERROR_NONE) {
133                 dlog_print(DLOG_ERROR, LOG_TAG, "failed to verify password");
134                 return;
135         }
136
137         if (!result) {
138                 dlog_print(DLOG_ERROR, LOG_TAG, "password not matched");
139                 limit_count++;
140                 if (limit_count == 15) {
141                         evas_object_del(popup_data->popup);
142                         create_reset_sdcard_popup(popup_data);
143                 }
144                 update_content_text();
145                 return;
146         }
147
148         if (popup_data->mode == CHANGE_PASSWORD) {
149                 snprintf(popup_data->entry_data_old, PATH_MAX, "%s", elm_entry_entry_get(confirm_entry));
150                 evas_object_del(popup_data->popup);
151                 create_password_message_popup(popup_data);
152         } else {
153                 if (popup_data->entry_data)
154                         snprintf(popup_data->entry_data, PATH_MAX, "%s", elm_entry_entry_get(confirm_entry));
155                 evas_object_del(popup_data->popup);
156                 password_result_callback(popup_data, "success");
157         }
158
159         return;
160 }
161
162 void create_confirm_password_popup(popup_data_s *data)
163 {
164         Evas_Object *cancel_button = NULL;
165
166         limit_count = 0;
167         limit_time = 0;
168
169         if (data->mode == INSERT_SD_CARD)
170                 data->popup = ode_create_popup(data->parent, __("IDS_ST_HEADER_USE_ENCRYPTED_SD_CARD"), NULL);
171         else
172                 data->popup = ode_create_popup(data->parent, __("IDS_ST_BODY_PASSWORD"), NULL);
173
174         confirm_layout = ode_create_layout(data->popup, EDJ_PATH, "entry_popup");
175         elm_object_part_content_set(data->popup, "elm.swallow.content", confirm_layout);
176
177         update_title_text();
178
179         confirm_entry = ode_create_entry(confirm_layout);
180         elm_object_part_content_set(confirm_layout, "entry", confirm_entry);
181
182         cancel_button = ode_create_popup_button(data->popup, __("IDS_ST_BUTTON_CANCEL"),
183                                                         confirm_password_popup_cancel_cb, data);
184         next_button = ode_create_popup_button(data->popup, __("IDS_ST_BUTTON_DONE"),
185                                                         confirm_password_popup_confirm_cb, data);
186         elm_object_part_content_set(data->popup, "button1", cancel_button);
187         elm_object_part_content_set(data->popup, "button2", next_button);
188
189         evas_object_event_callback_add(data->popup, EVAS_CALLBACK_KEY_DOWN, key_event_cb, data);
190         evas_object_show(data->popup);
191         return;
192 }