Apply coding rule
[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         { "IDS_ST_BODY_PASSWORD", 0 },
39 };
40
41 char *security_group_text[] = {
42         "IDS_ST_MBODY_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_win_indicator_opacity_set(ud.win, ELM_WIN_INDICATOR_BG_TRANSPARENT);
144         elm_object_signal_emit(ud.conform, "elm,state,indicator,overlap", "elm");
145
146         return EINA_TRUE;
147 }
148
149 static Eina_Bool password_view_pop_cb(void *data, Elm_Object_Item *it)
150 {
151         if (security_passphrase_mode != PASSPHRASE_STATE_UNDEFINED) {
152                 Evas_Object *entry = (Evas_Object *)evas_object_data_get(ud.nf, "setup_entry");
153                 evas_object_smart_callback_add(entry, "changed", security_password_entry_changed_cb, NULL);
154                 security_passphrase_mode--;
155         }
156         return EINA_TRUE;
157 }
158
159 static void delete_object_data(void)
160 {
161         evas_object_data_del(ud.nf, "setup_entry");
162         evas_object_data_del(ud.nf, "setup_button");
163         evas_object_data_del(ud.nf, "verify_entry");
164         evas_object_data_del(ud.nf, "verify_button");
165 }
166
167 static void security_previous_view_cb(void *data, Evas_Object *obj, void *event_info)
168 {
169         elm_naviframe_item_pop(ud.nf);
170 }
171
172 static void security_password_setup_cb(void *data, Evas_Object *obj, void *event_info)
173 {
174         appdata_s *ad = (appdata_s *)data;
175         Evas_Object *entry, *entry_text;
176
177         if (security_passphrase_mode < PASSPHRASE_STATE_VERIFY) {
178                 security_passphrase_mode++;
179                 entry = (Evas_Object *)evas_object_data_get(ud.nf, "setup_entry");
180                 evas_object_smart_callback_del(entry, "changed", security_password_entry_changed_cb);
181                 create_password_setup_view(ad);
182                 return;
183         }
184
185         if (strcmp(security_password_setup_data, security_password_verify_data) != 0) {
186                 entry = (Evas_Object *)evas_object_data_get(ud.nf, "verify_entry");
187                 elm_entry_entry_set(entry, "");
188                 elm_entry_input_panel_hide(entry);
189
190                 Evas_Object *layout = (Evas_Object *)evas_object_data_get(ud.nf, "layout");
191                 entry_text = _create_textblock(layout, __("IDS_ST_POP_PASSWORD_DO_NOT_MATCH"), ENTRY_INFO_STYLE_G);
192                 elm_object_part_content_set(layout, "entry_info", entry_text);
193                 dlog_print(DLOG_ERROR, LOG_TAG, "Password not matched");
194                 return;
195         }
196         delete_object_data();
197
198         ad->krate_password = security_password_setup_data;
199
200         if (_send_krate_create_request(ad) != 0) {
201                 ui_app_exit();
202         }
203
204         elm_win_indicator_opacity_set(ud.win, ELM_WIN_INDICATOR_BG_TRANSPARENT);
205         elm_object_signal_emit(ud.conform, "elm,state,indicator,overlap", "elm");
206         _create_setup_view(ad);
207 }
208
209 static void security_password_entry_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
210 {
211         char **password = (char **)data;
212         *password = strdup(elm_entry_entry_get(obj));
213 }
214
215 static void security_password_entry_changed_cb(void *data, Evas_Object *obj, void *event_info)
216 {
217         Evas_Object *button = NULL;
218         const char *entry_data = NULL;
219
220         entry_data = elm_entry_entry_get(obj);
221         if (entry_data == NULL)
222                 return;
223
224         if (security_passphrase_mode < PASSPHRASE_STATE_VERIFY)
225                 button = (Evas_Object *)evas_object_data_get(ud.nf, "setup_button");
226         else
227                 button = (Evas_Object *)evas_object_data_get(ud.nf, "verify_button");
228
229         if (strlen(entry_data) > 1)
230                 elm_object_disabled_set(button, EINA_FALSE);
231         else
232                 elm_object_disabled_set(button, EINA_TRUE);
233 }
234
235 static void create_password_setup_view(appdata_s *ad)
236 {
237         char **entry_data;
238         const char *title;
239         Elm_Object_Item *item;
240         Evas_Object *layout, *left_button, *right_button;
241         Evas_Object *security_layout, *entry, *entry_title;
242
243         layout = _create_layout(ud.nf, ud.edj_path, "base_layout");
244
245         left_button = _create_button(layout, __("IDS_ST_SK_PREVIOUS"), security_previous_view_cb, NULL);
246         right_button = _create_button(layout, __("IDS_ST_BUTTON_NEXT"), security_password_setup_cb, ad);
247         elm_object_disabled_set(right_button, EINA_TRUE);
248
249         _create_two_button_layout(layout, left_button, right_button);
250
251         if (security_passphrase_mode == 1) {
252                 entry_data = &security_password_setup_data;
253                 title = "Setup Password";
254                 evas_object_data_set(ud.nf, "setup_button", right_button);
255         } else {
256                 entry_data = &security_password_verify_data;
257                 title = "Verify Password";
258                 evas_object_data_set(ud.nf, "verify_button", right_button);
259         }
260
261         security_layout =  _create_layout(layout, ud.edj_path, "security_layout");
262
263         entry = _create_entry(security_layout);
264         entry_title = _create_textblock(security_layout, __("IDS_ST_BODY_ENTER_PASSWORD"), ENTRY_TITLE_STYLE_B);
265         elm_object_part_content_set(security_layout, "title", entry_title);
266         elm_object_part_content_set(security_layout, "entry", entry);
267         evas_object_smart_callback_add(entry, "unfocused", security_password_entry_unfocused_cb, entry_data);
268         evas_object_smart_callback_add(entry, "changed", security_password_entry_changed_cb, NULL);
269
270         elm_entry_input_panel_return_key_disabled_set(entry, EINA_TRUE);
271         elm_object_focus_set(entry, EINA_TRUE);
272
273         if (security_passphrase_mode < PASSPHRASE_STATE_VERIFY)
274                 evas_object_data_set(ud.nf, "setup_entry", entry);
275         else
276                 evas_object_data_set(ud.nf, "verify_entry", entry);
277
278         evas_object_data_set(ud.nf, "layout", security_layout);
279
280         elm_object_part_content_set(layout, "content_layout", security_layout);
281
282         item = elm_naviframe_item_push(ud.nf, title, NULL, NULL, layout, NULL);
283         elm_naviframe_item_title_enabled_set(item, EINA_TRUE, EINA_TRUE);
284         elm_naviframe_item_pop_cb_set(item, password_view_pop_cb, NULL);
285 }
286
287 void _create_security_view(appdata_s *ad)
288 {
289         int  index;
290         Elm_Object_Item *item;
291         Elm_Genlist_Item_Class *itc;
292         Evas_Object *genlist, *layout, *left_button, *right_button;
293
294         elm_win_indicator_opacity_set(ud.win, ELM_WIN_INDICATOR_OPAQUE);
295         elm_object_signal_emit(ud.conform, "elm,state,indicator,nooverlap", "elm");
296
297         layout = _create_layout(ud.nf, ud.edj_path, "base_layout");
298         genlist = elm_genlist_add(layout);
299         elm_object_style_set(genlist, "solid/default");
300         elm_layout_content_set(layout, "content_layout", genlist);
301         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
302
303         itc = _create_genlist_item_class("multiline", security_multiline_text_get, NULL);
304         _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, NULL);
305
306         itc =  _create_genlist_item_class("group_index", security_group_text_get, NULL);
307         _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, security_group_text[0]);
308
309         itc = _create_genlist_item_class("one_icon", security_lock_type_text_get, security_lock_type_content_get);
310         for (index = 0; index < ARRAY_SIZE(security_lock_types); index++) {
311                 item = elm_genlist_item_append(genlist,
312                                                                            itc,
313                                                                            &security_lock_types[index],
314                                                                            NULL,
315                                                                            ELM_GENLIST_ITEM_NONE,
316                                                                            security_lock_type_select_cb,
317                                                                            &security_lock_types[index]);
318         }
319 #if 0
320         /* Timeout list group*/
321         itc = _create_genlist_item_class("group_index", security_group_text_get, NULL);
322         _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, security_group_text[1]);
323
324         itc = _create_genlist_item_class("double_label", security_double_label_text_get, NULL);
325         item = _append_genlist_item(genlist, itc, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY, NULL);
326         elm_object_item_disabled_set(item, EINA_TRUE); /* [TBD] enable timeout options */
327 #endif
328         left_button = _create_button(layout, __("IDS_ST_SK_PREVIOUS"), security_previous_view_cb, NULL);
329         right_button = _create_button(layout, __("IDS_ST_BUTTON_NEXT"), security_password_setup_cb, ad);
330         _create_two_button_layout(layout, left_button, right_button);
331
332         item = elm_naviframe_item_push(ud.nf, "Krate Security", NULL, NULL, layout, NULL);
333         elm_naviframe_item_title_enabled_set(item, EINA_TRUE, EINA_TRUE);
334         elm_naviframe_item_pop_cb_set(item, security_view_pop_cb, NULL);
335 }