Modify security page of setup-wizard
[platform/core/security/krate.git] / tools / apps / setup-wizard / src / security.c
1 /*
2  * Tizen Krate Setup-Wizard application
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "krate-setup.h"
21 #include "widget.h"
22
23 #define PASSPHRASE_STATE_UNDEFINED      0
24 #define PASSPHRASE_STATE_SETUP          1
25 #define PASSPHRASE_STATE_VERIFY         2
26
27 #define ARRAY_SIZE(_array_) \
28         (sizeof(_array_) / sizeof(_array_[0]))
29
30 extern uidata_s ud;
31
32 struct security_lock_type {
33         const char* text;
34         int index;
35 };
36
37 struct security_lock_type security_lock_types[] = {
38         { "Password", 0 },
39 };
40
41 char* security_group_text[] = {
42         "Unlock method",
43 };
44
45 static char* security_password_setup_data = NULL;
46 static char* security_password_verify_data = NULL;
47
48 static unsigned int security_passphrase_mode = PASSPHRASE_STATE_UNDEFINED;
49 static int security_lock_type_selected = 0;
50
51 static void create_password_setup_view(appdata_s *ad);
52 static void security_password_entry_changed_cb(void *data, Evas_Object *obj, void *event_info);
53
54 static char *security_multiline_text_get(void *data, Evas_Object *obj, const char *part)
55 {
56         char text[PATH_MAX] = "\0";
57
58         if (!strcmp(part, "elm.text.multiline")) {
59                 snprintf(text, PATH_MAX, "Select a Krate unlock method and a timeout option.");
60                 return strdup(text);
61         }
62
63         return NULL;
64 }
65
66 static char *security_group_text_get(void *data, Evas_Object *obj, const char *part)
67 {
68         char *text = (char *)data;
69
70         if (!strcmp(part, "elm.text")) {
71                 return strdup(text);
72         }
73
74         return NULL;
75 }
76
77 #if 0
78 static char *security_double_label_text_get(void *data, Evas_Object *obj, const char *part)
79 {
80         char text[PATH_MAX] = "\0";
81         int timeout = 10; /*[TBD] get value of timeout */
82
83         if (!strcmp(part, "elm.text"))
84                 snprintf(text, PATH_MAX, "Security Timeout");
85         else if (!strcmp(part, "elm.text.sub"))
86                 snprintf(text, PATH_MAX, "After %d minuates of inactivity", timeout);
87
88         return strdup(text);
89 }
90 #endif
91
92 static char *security_lock_type_text_get(void *data, Evas_Object *obj, const char *part)
93 {
94         struct security_lock_type *locktype = (struct security_lock_type *)data;
95
96         if (!strcmp(part, "elm.text")) {
97                 return strdup(locktype->text);
98         }
99
100         return NULL;
101 }
102
103 static Evas_Object *security_lock_type_content_get(void *data, Evas_Object *obj, const char *part)
104 {
105         static Evas_Object *group = NULL;
106         struct security_lock_type* locktype = (struct security_lock_type *)data;
107
108         if (group == NULL) {
109                 group = elm_radio_add(obj);
110                 elm_radio_state_value_set(group, 0);
111                 elm_radio_value_set(group, 0);
112         }
113
114         if (!strcmp(part, "elm.swallow.icon")) {
115                 Evas_Object* radio = elm_radio_add(obj);
116                 elm_radio_state_value_set(radio, locktype->index);
117
118                 if (locktype->index == security_lock_type_selected) {
119                         elm_radio_value_set(radio, locktype->index);
120                 }
121
122                 elm_radio_group_add(radio, group);
123                 evas_object_propagate_events_set(radio, EINA_FALSE);
124                 evas_object_repeat_events_set(radio, EINA_TRUE);
125
126                 return radio;
127         }
128
129         return NULL;
130 }
131
132 static void security_lock_type_select_cb(void *data, Evas_Object *obj, void *event_info)
133 {
134         struct security_lock_type* locktype = (struct security_lock_type *)data;
135
136         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
137         elm_radio_value_set(obj, locktype->index);
138         security_lock_type_selected = locktype->index;
139 }
140
141 static Eina_Bool security_view_pop_cb(void *data, Elm_Object_Item *it)
142 {
143         elm_object_signal_emit(ud.conform, "elm,state,indicator,overlap", "elm");
144
145         return EINA_TRUE;
146 }
147
148 static Eina_Bool password_view_pop_cb(void *data, Elm_Object_Item *it)
149 {
150         if (security_passphrase_mode != PASSPHRASE_STATE_UNDEFINED) {
151                 Evas_Object *entry = (Evas_Object *)evas_object_data_get(ud.nf, "setup_entry");
152                 evas_object_smart_callback_add(entry, "changed", security_password_entry_changed_cb, NULL);
153                 security_passphrase_mode--;
154         }
155         return EINA_TRUE;
156 }
157
158 static void delete_object_data(void)
159 {
160         evas_object_data_del(ud.nf, "setup_entry");
161         evas_object_data_del(ud.nf, "setup_button");
162         evas_object_data_del(ud.nf, "verify_entry");
163         evas_object_data_del(ud.nf, "verify_button");
164 }
165
166 static void security_previous_view_cb(void *data, Evas_Object *obj, void *event_info)
167 {
168         elm_naviframe_item_pop(ud.nf);
169 }
170
171 static void security_password_setup_cb(void *data, Evas_Object *obj, void *event_info)
172 {
173         appdata_s *ad = (appdata_s *)data;
174         Evas_Object *entry;
175         if (security_passphrase_mode < PASSPHRASE_STATE_VERIFY) {
176                 security_passphrase_mode++;
177                 entry = (Evas_Object *)evas_object_data_get(ud.nf, "setup_entry");
178                 evas_object_smart_callback_del(entry, "changed", security_password_entry_changed_cb);
179                 create_password_setup_view(ad);
180                 return;
181         }
182
183         if (strcmp(security_password_setup_data, security_password_verify_data) != 0) {
184                 entry = (Evas_Object *)evas_object_data_get(ud.nf, "verify_entry");
185                 elm_entry_entry_set(entry, "");
186                 elm_entry_input_panel_hide(entry);
187
188                 Evas_Object *layout = (Evas_Object *)evas_object_data_get(ud.nf, "layout");
189                 elm_object_part_text_set(layout, "entry_info", "Password do not matched.");
190                 dlog_print(DLOG_ERROR, LOG_TAG, "Password not matched");
191                 return;
192         }
193                 delete_object_data();
194
195         ad->krate_password = security_password_setup_data;
196
197         if (_send_krate_create_request(ad) != 0) {
198                 ui_app_exit();
199         }
200
201         elm_object_signal_emit(ud.conform, "elm,state,indicator,overlap", "elm");
202         _create_setup_view(ad);
203 }
204
205 static void security_password_entry_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
206 {
207         char **password = (char **)data;
208         *password = strdup(elm_entry_entry_get(obj));
209 }
210
211 static void security_password_entry_changed_cb(void *data, Evas_Object *obj, void *event_info)
212 {
213         Evas_Object *button;
214         const char *entry_data = elm_entry_entry_get(obj);
215
216         if (entry_data == NULL)
217                 return;
218
219         if (security_passphrase_mode < PASSPHRASE_STATE_VERIFY)
220                 button = (Evas_Object *)evas_object_data_get(ud.nf, "setup_button");
221         else
222                 button = (Evas_Object *)evas_object_data_get(ud.nf, "verify_button");
223
224         if (strlen(entry_data) > 1)
225                 elm_object_disabled_set(button, EINA_FALSE);
226         else
227                 elm_object_disabled_set(button, EINA_TRUE);
228 }
229
230 static Evas_Object *security_password_setup_content_get(void *data, Evas_Object *obj, const char *part)
231 {
232         if (!strcmp(part, "elm.swallow.content")) {
233                 Evas_Object *layout, *entry;
234
235                 layout =  _create_layout(obj, ud.edj_path, "security_layout");
236
237                 entry = _create_entry(obj);
238                 elm_object_part_text_set(layout, "title", "Enter password");
239                 elm_object_part_content_set(layout, "entry", entry);
240                 evas_object_smart_callback_add(entry, "unfocused", security_password_entry_unfocused_cb, data);
241                 evas_object_smart_callback_add(entry, "changed", security_password_entry_changed_cb, NULL);
242                 elm_entry_input_panel_return_key_disabled_set(entry, EINA_TRUE);
243
244                 if (security_passphrase_mode < PASSPHRASE_STATE_VERIFY)
245                         evas_object_data_set(ud.nf, "setup_entry", entry);
246                 else
247                         evas_object_data_set(ud.nf, "verify_entry", entry);
248
249                 evas_object_data_set(ud.nf, "layout", layout);
250
251                 return layout;
252         }
253
254         return NULL;
255 }
256
257 static void create_password_setup_view(appdata_s *ad)
258 {
259         char** entry;
260         const char* title;
261         Elm_Object_Item *item;
262         Elm_Genlist_Item_Class *itc;
263         Evas_Object *genlist, *layout, *left_button, *right_button;
264
265         layout = _create_layout(ud.nf, ud.edj_path, "base_layout");
266         genlist = elm_genlist_add(layout);
267         elm_layout_content_set(layout, "content_layout", genlist);
268         elm_object_style_set(genlist, "solid/default");
269         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
270
271         left_button = _create_button(layout, PREV_BUTTON, security_previous_view_cb, NULL);
272         right_button = _create_button(layout, NEXT_BUTTON, security_password_setup_cb, ad);
273         elm_object_disabled_set(right_button, EINA_TRUE);
274
275         _create_two_button_layout(layout, left_button, right_button);
276
277         if (security_passphrase_mode == 1) {
278                 entry = &security_password_setup_data;
279                 title = "Setup Password";
280                 evas_object_data_set(ud.nf, "setup_button", right_button);
281         } else {
282                 entry = &security_password_verify_data;
283                 title = "Verify Password";
284                 evas_object_data_set(ud.nf, "verify_button", right_button);
285         }
286
287         itc = _create_genlist_item_class("full", NULL, security_password_setup_content_get);
288         item = _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, (void **)entry);
289
290         item = elm_naviframe_item_push(ud.nf, title, NULL, NULL, layout, NULL);
291         elm_naviframe_item_title_enabled_set(item, EINA_TRUE, EINA_TRUE);
292         elm_naviframe_item_pop_cb_set(item, password_view_pop_cb, NULL);
293 }
294
295 void _create_security_view(appdata_s *ad)
296 {
297         int  index;
298         Elm_Object_Item *item;
299         Elm_Genlist_Item_Class *itc;
300         Evas_Object *genlist, *layout, *left_button, *right_button;
301
302         elm_object_signal_emit(ud.conform, "elm,state,indicator,nooverlap", "elm");
303
304         layout = _create_layout(ud.nf, ud.edj_path, "base_layout");
305         genlist = elm_genlist_add(layout);
306         elm_object_style_set(genlist, "solid/default");
307         elm_layout_content_set(layout, "content_layout", genlist);
308         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
309
310         itc = _create_genlist_item_class("multiline", security_multiline_text_get, NULL);
311         _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, NULL);
312
313         itc =  _create_genlist_item_class("group_index", security_group_text_get, NULL);
314         _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, security_group_text[0]);
315
316         itc = _create_genlist_item_class("one_icon", security_lock_type_text_get, security_lock_type_content_get);
317         for (index = 0; index < ARRAY_SIZE(security_lock_types); index++) {
318                 item = elm_genlist_item_append(genlist,
319                                                                            itc,
320                                                                            &security_lock_types[index],
321                                                                            NULL,
322                                                                            ELM_GENLIST_ITEM_NONE,
323                                                                            security_lock_type_select_cb,
324                                                                            &security_lock_types[index]);
325                 if (index == 1) {
326                         elm_object_item_disabled_set(item, EINA_TRUE);
327                 }
328         }
329 #if 0
330         /* Timeout list group*/
331         itc = _create_genlist_item_class("group_index", security_group_text_get, NULL);
332         _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, security_group_text[1]);
333
334         itc = _create_genlist_item_class("double_label", security_double_label_text_get, NULL);
335         item = _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, NULL);
336         elm_object_item_disabled_set(item, EINA_TRUE); /* [TBD] enable timeout options */
337 #endif
338         left_button = _create_button(layout, PREV_BUTTON, security_previous_view_cb, NULL);
339         right_button = _create_button(layout, NEXT_BUTTON, security_password_setup_cb, ad);
340         _create_two_button_layout(layout, left_button, right_button);
341
342         item = elm_naviframe_item_push(ud.nf, "Krate Security", NULL, NULL, layout, NULL);
343         elm_naviframe_item_title_enabled_set(item, EINA_TRUE, EINA_TRUE);
344         elm_naviframe_item_pop_cb_set(item, security_view_pop_cb, NULL);
345 }