Code sync
[apps/native/starter.git] / lock-setting / lockapp-setting / src / openlock-setting-pw.c
1  /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.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
7   *
8   *     http://www.tizenopensource.org/license
9   *
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.
15   */
16
17
18
19 #include <appcore-efl.h>
20 #include <Ecore_X.h>
21 #include <vconf.h>
22 #include <vconf-keys.h>
23
24 #include "openlock-setting-pw.h"
25 #include "openlock-setting-debug.h"
26 #include "starter-vconf.h"
27
28 static Evas_Object *done_button = NULL;
29 static Elm_Gen_Item_Class itc_label, itc_entry;
30 static Evas_Object *_openlock_setting_pw_editfield_create(Evas_Object * parent,
31                                                           void *data);
32 static Evas_Object *_openlock_setting_pw_editfield_entry_get(Evas_Object *
33                                                              parent);
34
35 static Evas_Object *_openlock_setting_pw_create_conformant(Evas_Object * parent)
36 {
37         Evas_Object *conform = NULL;
38         elm_win_conformant_set(parent, 1);
39         conform = elm_conformant_add(parent);
40         elm_object_style_set(conform, "internal_layout");
41         evas_object_show(conform);
42
43         return conform;
44 }
45
46 static void _openlock_setting_pw_done_button_changed(void *data,
47                                                      Evas_Object * obj, void *e)
48 {
49         openlock_setting_appdata *openlock_setting_data =
50             (openlock_setting_appdata *) data;
51
52         if (!openlock_setting_data) {
53                 return;
54         }
55         int length = strlen(elm_entry_entry_get(obj));
56
57         if (length == 0) {
58                 elm_object_disabled_set(done_button, EINA_TRUE);
59         } else {
60                 elm_object_disabled_set(done_button, EINA_FALSE);
61         }
62
63         if (elm_object_focus_get(obj)) {
64                 if (elm_entry_is_empty(obj)) {
65                         elm_object_signal_emit
66                             (openlock_setting_data->editfield_layout,
67                              "elm,state,eraser,hide", "elm");
68                 } else {
69                         elm_object_signal_emit
70                             (openlock_setting_data->editfield_layout,
71                              "elm,state,eraser,show", "elm");
72                 }
73         }
74 }
75
76 static void _openlock_setting_pw_focused_cb(void *data, Evas_Object * obj,
77                                             void *event_info)
78 {
79         if (!elm_entry_is_empty(obj)) {
80                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
81         }
82         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
83 }
84
85 static void _openlock_setting_pw_unfocused_cb(void *data, Evas_Object * obj,
86                                               void *event_info)
87 {
88         if (elm_entry_is_empty(obj)) {
89                 elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
90         }
91         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
92 }
93
94 static void _openlock_setting_pw_eraser_clicked_cb(void *data,
95                                                    Evas_Object * obj,
96                                                    const char *emission,
97                                                    const char *source)
98 {
99         elm_entry_entry_set(data, "");
100 }
101
102 static Evas_Object *_openlock_setting_pw_editfield_create(Evas_Object * parent,
103                                                           void *data)
104 {
105         Evas_Object *layout = NULL;
106         Evas_Object *entry = NULL;
107         openlock_setting_appdata *openlock_setting_data =
108             (openlock_setting_appdata *) data;
109         static Elm_Entry_Filter_Limit_Size limit_filter_data;
110
111         limit_filter_data.max_char_count = 15;  /* hard code for demo */
112         limit_filter_data.max_byte_count = 0;
113
114         layout = elm_layout_add(parent);
115         elm_layout_theme_set(layout, "layout", "editfield", "title");
116
117         entry = elm_entry_add(parent);
118         elm_entry_scrollable_set(entry, EINA_TRUE);
119         elm_entry_single_line_set(entry, EINA_TRUE);
120         elm_entry_password_set(entry, EINA_TRUE);
121         elm_entry_input_panel_layout_set(entry,
122                                          ELM_INPUT_PANEL_LAYOUT_NUMBERONLY);
123         elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size,
124                                        &limit_filter_data);
125         elm_object_focus_set(entry, EINA_TRUE);
126         evas_object_show(entry);
127         evas_object_smart_callback_add(entry, "changed",
128                                        _openlock_setting_pw_done_button_changed,
129                                        openlock_setting_data);
130         evas_object_smart_callback_add(entry, "focused",
131                                        _openlock_setting_pw_focused_cb, layout);
132         evas_object_smart_callback_add(entry, "unfocused",
133                                        _openlock_setting_pw_unfocused_cb,
134                                        layout);
135
136         elm_object_part_content_set(layout, "elm.swallow.content", entry);
137         elm_object_part_text_set(layout, "elm.guidetext", "");
138         elm_object_signal_callback_add(layout, "elm,eraser,clicked", "elm",
139                                        _openlock_setting_pw_eraser_clicked_cb,
140                                        entry);
141
142         Ecore_IMF_Context *imf_context =
143                         (Ecore_IMF_Context *) elm_entry_imf_context_get(entry);
144         if (imf_context) {
145                 ecore_imf_context_input_panel_show(imf_context);
146         }
147         elm_object_focus_set(entry, EINA_TRUE);
148
149         return layout;
150 }
151
152 static Evas_Object *_openlock_setting_pw_editfield_entry_get(Evas_Object *
153                                                              parent)
154 {
155         Evas_Object *entry = NULL;
156
157         entry = elm_object_part_content_get(parent, "elm.swallow.content");
158
159         return entry;
160 }
161
162 static char *_openlock_setting_pw_gl_label_get_title(void *data,
163                                                      Evas_Object * obj,
164                                                      const char *part)
165 {
166         openlock_setting_appdata *openlock_setting_data =
167             (openlock_setting_appdata *) data;
168         char buf[50] = { 0, };
169
170         if (!openlock_setting_data || !part) {
171                 return NULL;
172         }
173
174         if (!strcmp(part, "elm.text")) {
175                 snprintf(buf, sizeof(buf), "%s", "Enter Password");     /* hard code for demo */
176                 return strdup(buf);
177         }
178         return NULL;
179 }
180
181 static Evas_Object *_openlock_setting_pw_gl_icon_get(void *data,
182                                                      Evas_Object * obj,
183                                                      const char *part)
184 {
185         Evas_Object *layout = NULL;
186         openlock_setting_appdata *openlock_setting_data =
187             (openlock_setting_appdata *) data;
188
189         if (!openlock_setting_data || !part) {
190                 return NULL;
191         }
192
193         if (!strcmp(part, "elm.icon")) {
194                 layout =
195                     _openlock_setting_pw_editfield_create(obj,
196                                                           openlock_setting_data);
197                 openlock_setting_data->editfield_layout = layout;
198
199                 return layout;
200
201         }
202         return NULL;
203 }
204
205 static void _openlock_setting_pw_list_set_styles()
206 {
207         itc_label.item_style = "dialogue/title";
208         itc_label.func.text_get = _openlock_setting_pw_gl_label_get_title;
209         itc_label.func.content_get = NULL;
210         itc_label.func.state_get = NULL;
211         itc_label.func.del = NULL;
212
213         itc_entry.item_style = "dialogue/1icon";
214         itc_entry.func.text_get = NULL;
215         itc_entry.func.content_get = _openlock_setting_pw_gl_icon_get;
216         itc_entry.func.state_get = NULL;
217         itc_entry.func.del = NULL;
218 }
219
220 static void _openlock_setting_pw_back_cb(void *data, Evas_Object * obj,
221                                          void *event_info)
222 {
223         openlock_setting_appdata *openlock_setting_data =
224             (openlock_setting_appdata *) data;
225         openlock_ug_data *openlock_data = NULL;
226
227         if (!openlock_setting_data) {
228                 return;
229         }
230
231         openlock_data = openlock_setting_data->openlock_data;
232         if (!openlock_data) {
233                 return;
234         }
235         OPENLOCKS_DBG("_openlock_setting_pw_back_cb\n");
236         openlock_setting_data->count = 0;
237
238         elm_naviframe_item_pop(openlock_data->navi_bar);
239         if (openlock_setting_data->editfield_layout) {
240                 evas_object_del(openlock_setting_data->editfield_layout);
241                 openlock_setting_data->editfield_layout = NULL;
242         }
243         if (openlock_setting_data->genlist) {
244                 evas_object_del(openlock_setting_data->genlist);
245                 openlock_setting_data->genlist = NULL;
246         }
247         if (openlock_setting_data->ly) {
248                 evas_object_del(openlock_setting_data->ly);
249                 openlock_setting_data->ly = NULL;
250         }
251 }
252
253 static Eina_Bool _openlock_setting_pw_input_panel_show_idler(void *data)
254 {
255         Evas_Object *entry = (Evas_Object *) data;
256
257         OPENLOCKS_DBG("_openlock_setting_pw_input_panel_show_idler");
258
259         elm_object_focus_set(entry, EINA_TRUE);
260
261         return ECORE_CALLBACK_CANCEL;
262 }
263
264 static void _openlock_setting_pw_imf_context_input_panel_show(void *data)
265 {
266         openlock_setting_appdata *openlock_setting_data =
267                         (openlock_setting_appdata *) data;
268         Evas_Object *entry = NULL;
269
270         if (!openlock_setting_data) {
271                 return;
272         }
273
274         OPENLOCKS_DBG("_openlock_setting_pw_imf_context_input_panel_show\n");
275
276         entry = _openlock_setting_pw_editfield_entry_get(
277                         openlock_setting_data->editfield_layout);
278         evas_object_show(entry);
279
280         ecore_idler_add(_openlock_setting_pw_input_panel_show_idler, entry);
281 }
282
283 static void _openlock_setting_pw_imf_context_input_panel_hide(void *data)
284 {
285         openlock_setting_appdata *openlock_setting_data =
286                         (openlock_setting_appdata *) data;
287         Evas_Object *entry = NULL;
288
289         if (!openlock_setting_data) {
290                 return;
291         }
292
293         OPENLOCKS_DBG("_openlock_setting_pw_imf_context_input_panel_hide\n");
294
295         entry = _openlock_setting_pw_editfield_entry_get(
296                         openlock_setting_data->editfield_layout);
297         evas_object_hide(entry);
298
299         elm_object_focus_set(entry, EINA_FALSE);
300 }
301
302 static void _openlock_setting_pw_destroy_popup_cb(void *data, Evas_Object * obj,
303                                                   void *e)
304 {
305         openlock_setting_appdata *openlock_setting_data =
306             (openlock_setting_appdata *) data;
307         Evas_Object *popup = NULL;
308
309         OPENLOCKS_DBG("_openlock_setting_pw_destroy_popup_cb\n");
310         _openlock_setting_pw_imf_context_input_panel_show
311             (openlock_setting_data);
312
313         if (!openlock_setting_data) {
314                 return;
315         }
316
317         popup = openlock_setting_data->popup;
318         if (popup) {
319                 evas_object_del(popup);
320                 popup = NULL;
321         }
322 }
323
324 static void _openlock_setting_pw_show_popup(void *data, const char *mesg)
325 {
326         openlock_setting_appdata *openlock_setting_data =
327             (openlock_setting_appdata *) data;
328         openlock_ug_data *openlock_data = NULL;
329         Evas_Object *popup = NULL;
330
331         if (!openlock_setting_data) {
332                 return;
333         }
334         openlock_data = openlock_setting_data->openlock_data;
335         if (!openlock_data) {
336                 return;
337         }
338
339         OPENLOCKS_DBG("_openlock_setting_pw_show_popup\n");
340
341         _openlock_setting_pw_imf_context_input_panel_hide
342             (openlock_setting_data);
343         popup = elm_popup_add(openlock_data->win_main);
344         openlock_setting_data->popup = popup;
345         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND,
346                                          EVAS_HINT_EXPAND);
347         elm_object_text_set(popup, mesg);
348         elm_popup_timeout_set(popup, 3);
349         evas_object_smart_callback_add(popup, "timeout",
350                                        _openlock_setting_pw_destroy_popup_cb,
351                                        openlock_setting_data);
352         evas_object_show(popup);
353 }
354
355 static void _openlock_setting_pw_set_str(char **s, const char *str)
356 {
357         if (s == NULL)
358                 return;
359
360         if (*s)
361                 free(*s);
362
363         if (str && str[0] != '\0')
364                 *s = strdup(str);
365         else
366                 *s = NULL;
367 }
368
369 static void _openlock_setting_pw_done_cb(void *data, Evas_Object * obj, void *e)
370 {
371         openlock_setting_appdata *openlock_setting_data =
372             (openlock_setting_appdata *) data;
373         openlock_ug_data *openlock_data = NULL;
374         Evas_Object *entry = NULL;
375         Evas_Object *editfield_layout = NULL;
376         char *str = NULL;
377
378         if (!openlock_setting_data) {
379                 return;
380         }
381
382         OPENLOCKS_DBG("_openlock_setting_pw_done_cb\n");
383         editfield_layout = openlock_setting_data->editfield_layout;
384         entry =
385             _openlock_setting_pw_editfield_entry_get
386             (openlock_setting_data->editfield_layout);
387
388         _openlock_setting_pw_set_str(&str, elm_entry_entry_get(entry));
389         OPENLOCKS_DBG("str = %s\n", str);
390         if (!str) {
391                 _openlock_setting_pw_show_popup(openlock_setting_data, "Wrong Password!");      /* hard code for demo */
392                 if (entry) {
393                         elm_object_part_text_set(editfield_layout,
394                                                  "elm.guidetext", "");
395                         elm_entry_entry_set(entry, "");
396                 }
397         } else {
398                 if (strcmp(str, "16777216") == 0) {     /* hard code for demo */
399                         OPENLOCKS_DBG("right pw\n");
400                         OPENLOCKS_DBG("openlock_setting_data->index: %d",
401                                       openlock_setting_data->index);
402                         if (openlock_setting_data != NULL
403                             && openlock_setting_data->pkg_name != NULL) {
404                                 vconf_set_str(VCONF_PRIVATE_LOCKSCREEN_PKGNAME,
405                                               openlock_setting_data->pkg_name);
406                                 OPENLOCKS_DBG("vconf pkgname set : %s",
407                                               openlock_setting_data->pkg_name);
408                         }
409                         openlock_data = openlock_setting_data->openlock_data;
410                         if (!openlock_data) {
411                                 return;
412                         }
413                         OPENLOCKS_DBG("_openlock_setting_pw_done_cb\n");
414                         openlock_setting_data->count = 0;       /* reset the count */
415
416                         elm_naviframe_item_pop(openlock_data->navi_bar);
417                         if (openlock_setting_data->editfield_layout) {
418                                 evas_object_del
419                                     (openlock_setting_data->editfield_layout);
420                                 openlock_setting_data->editfield_layout = NULL;
421                         }
422                         if (openlock_setting_data->genlist) {
423                                 evas_object_del(openlock_setting_data->genlist);
424                                 openlock_setting_data->genlist = NULL;
425                         }
426                         if (openlock_setting_data->ly) {
427                                 evas_object_del(openlock_setting_data->ly);
428                                 openlock_setting_data->ly = NULL;
429                         }
430                 } else {
431                         _openlock_setting_pw_show_popup(openlock_setting_data, "Wrong Password!");      /* hard code for demo */
432                         if (entry) {
433                                 elm_object_part_text_set(editfield_layout,
434                                                          "elm.guidetext", "");
435                                 elm_entry_entry_set(entry, "");
436                         }
437                 }
438         }
439 }
440
441 void
442 openlock_setting_pw_create_view(openlock_setting_appdata *
443                                 openlock_setting_data)
444 {
445         Evas_Object *navi_bar = NULL;
446         Evas_Object *win_main = NULL;
447         Evas_Object *genlist = NULL;
448         Evas_Object *ly = NULL;
449         Evas_Object *cancel_button = NULL;
450         Evas_Object *back_button = NULL;
451         Elm_Object_Item *genlist_item = NULL;
452         Elm_Object_Item *navi_it = NULL;
453         openlock_ug_data *openlock_data = NULL;
454
455         if (!openlock_setting_data) {
456                 return;
457         }
458
459         openlock_data = openlock_setting_data->openlock_data;
460         if (!openlock_data) {
461                 return;
462         }
463
464         OPENLOCKS_DBG("openlock_setting_pw_create_view begin\n");
465
466         win_main = openlock_data->win_main;
467         navi_bar = openlock_data->navi_bar;
468
469         ly = _openlock_setting_pw_create_conformant(win_main);
470         openlock_setting_data->ly = ly;
471
472         /* genlist */
473         genlist = elm_genlist_add(navi_bar);
474         _openlock_setting_pw_list_set_styles();
475         genlist_item =
476             elm_genlist_item_append(genlist, &itc_label,
477                                     (void *)openlock_setting_data, NULL,
478                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
479         elm_genlist_item_select_mode_set(genlist_item, EINA_TRUE);
480
481         genlist_item =
482             elm_genlist_item_append(genlist, &itc_entry,
483                                     (void *)openlock_setting_data, NULL,
484                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
485         elm_genlist_item_select_mode_set(genlist_item, EINA_TRUE);
486
487         evas_object_show(genlist);
488         elm_object_content_set(ly, genlist);
489         openlock_setting_data->genlist = genlist;
490
491         /* Done button */
492         done_button = elm_button_add(navi_bar);
493         elm_object_style_set(done_button, "naviframe/title/default");
494         elm_object_text_set(done_button, "Done");       /* hard code for demo */
495         elm_object_disabled_set(done_button, EINA_TRUE);
496         evas_object_smart_callback_add(done_button, "clicked",
497                                        _openlock_setting_pw_done_cb,
498                                        openlock_setting_data);
499
500         /* Cancel button */
501         cancel_button = elm_button_add(navi_bar);
502         elm_object_style_set(cancel_button, "naviframe/title/default");
503         elm_object_text_set(cancel_button, "Cancel"); /* hard code for demo */
504         elm_object_disabled_set(cancel_button, EINA_FALSE);
505         evas_object_smart_callback_add(cancel_button, "clicked",
506                         _openlock_setting_pw_back_cb, openlock_setting_data);
507
508         /* Set navigation objects and push */
509         navi_it = elm_naviframe_item_push(navi_bar, "Enter Password", NULL, NULL,
510                         ly, NULL); /* hard code for demo */
511         elm_object_item_part_content_set(navi_it, "title_right_btn", done_button);
512         elm_object_item_part_content_set(navi_it, "title_left_btn", cancel_button);
513
514         /* Remove <- button */
515         back_button = elm_object_item_part_content_get(navi_it, "prev_btn");
516         if (back_button != NULL) {
517                 elm_object_item_part_content_set(navi_it, "prev_btn", NULL);
518                 if (back_button != NULL) {
519                         evas_object_del(back_button);
520                 }
521         }
522 }