Tizen release 1.0
[pkgs/p/phone-lock.git] / phone-lock-ug-efl / src / phone-lock-efl.c
1 /*
2  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved 
3  *
4  * This file is part of <phone-lock>
5  * Written by <Seungtaek Chung> <seungtaek.chung@samsung.com>, <Mi-Ju Lee> <miju52.lee@samsung.com>, <Xi Zhichan> <zhichan.xi@samsung.com>
6  *
7  * PROPRIETARY/CONFIDENTIAL
8  *
9  * This software is the confidential and proprietary information of SAMSUNG ELECTRONICS ("Confidential Information").
10  * You shall not disclose such Confidential Information and shall use it only in accordance
11  * with the terms of the license agreement you entered into with SAMSUNG ELECTRONICS.
12  * SAMSUNG make no representations or warranties about the suitability of the software,
13  * either express or implied, including but not limited to the implied warranties of merchantability,
14  * fitness for a particular purpose, or non-infringement.
15  * SAMSUNG shall not be liable for any damages suffered by licensee as a result of using,
16  * modifying or distributing this software or its derivatives.
17  *
18  */
19
20 #ifndef UG_MODULE_API
21 #define UG_MODULE_API __attribute__ ((visibility("default")))
22 #endif
23
24 #include <Elementary.h>
25 #include <ui-gadget-module.h>
26 #include <bundle.h>
27 #include <Ecore_X.h>
28 #include <utilX.h>
29 #include <vconf.h>
30 #include <vconf-keys.h>
31
32 #include "phone-lock-util.h"
33 #include "phone-lock-view.h"
34 #include "phone-lock-string.h"
35 #include "phone-lock-verification.h"
36 #include "phone-lock-gadget.h"
37
38 #include "phone-lock-efl.h"
39
40 struct phone_lock_ug_data {
41         Evas_Object *base;
42         struct ui_gadget *ug;
43
44         phone_lock_view_data_t *phone_lock_view_data;
45
46         struct ui_gadget *ug_dialer;
47         Ecore_Event_Handler *event_handler;
48 };
49
50 void
51 _phone_lock_ug_send_result(struct phone_lock_ug_data *phone_lock_ugdata,
52                            char *name, char *result)
53 {
54         bundle *b;
55
56         b = bundle_create();
57         bundle_add(b, "name", name);
58         bundle_add(b, "result", result);
59
60         ug_send_result(phone_lock_ugdata->ug, b);
61
62         bundle_free(b);
63 }
64
65 static void _phone_lock_ug_gadget_destroy_cb(void *user_data)
66 {
67         PHONE_LOCK_DBG("");
68         phone_lock_view_data_t *phone_lock_view_data = NULL;
69         struct phone_lock_ug_data *phone_lock_ugdata =
70             (struct phone_lock_ug_data *)user_data;
71         if (!phone_lock_ugdata) {
72                 return;
73         }
74         phone_lock_ugdata->ug_dialer = NULL;
75         phone_lock_view_data = phone_lock_ugdata->phone_lock_view_data;
76
77         if (phone_lock_view_data) {
78                 phone_lock_view_reset_callug_launch_flag(phone_lock_view_data);
79                 phone_lock_view_show_imf_context_input_panel
80                     (phone_lock_view_data);
81         }
82 }
83
84 static void _phone_lock_ug_view_cb(int type, char *str, void *user_data)
85 {
86         struct phone_lock_ug_data *phone_lock_ugdata =
87             (struct phone_lock_ug_data *)user_data;
88         phone_lock_view_type_t phone_lock_view_type;
89
90         char buf[512];
91         int ret;
92
93         phone_lock_view_type =
94             phone_lock_view_get_type(phone_lock_ugdata->phone_lock_view_data);
95
96         if (phone_lock_view_type == PHONE_LOCK_VIEW_TYPE_PHONE_LOCK) {
97                 switch (type) {
98                 case 1:
99                         phone_lock_ugdata->ug_dialer = (struct ui_gadget *)
100                             phone_lock_gadget_show_dialer(ug_get_window(),
101                                                           phone_lock_ugdata->ug,
102                                                           _phone_lock_ug_gadget_destroy_cb,
103                                                           phone_lock_ugdata);
104                         break;
105                 case 2:
106                         PHONE_LOCK_DBG("%s, str : %s", __FUNCTION__, str);
107                         ret = phone_lock_verification_check_length(str, 4, 16);
108                         if (ret == 0) {
109                                 if (phone_lock_verification_check_phone_password
110                                     (str)) {
111                                         phone_lock_view_reset_attempts_num
112                                             (phone_lock_ugdata->phone_lock_view_data);
113                                         phone_lock_view_set_right_password_flag
114                                             (phone_lock_ugdata->phone_lock_view_data);
115                                         _phone_lock_ug_send_result(phone_lock_ugdata, "phonelock-ug", "success");
116                                         PHONE_LOCK_DBG("password verified. Unlock!\n");
117                                         ug_destroy_me(phone_lock_ugdata->ug);
118                                 } else {
119                                         phone_lock_view_check_attempts_left(phone_lock_ugdata->phone_lock_view_data);   /*new request */
120                                 }
121                         } else if (ret ==
122                                    IDS_IDLE_BODY_PD_TO_PD_DIGITS_OR_LETTERS_REQUIRED) {
123                                 snprintf(buf, sizeof(buf),
124                                          phone_lock_get_string
125                                          (IDS_IDLE_BODY_PD_TO_PD_DIGITS_OR_LETTERS_REQUIRED),
126                                          4, 16);
127                                 phone_lock_view_show_msg(phone_lock_ugdata->
128                                                          phone_lock_view_data,
129                                      buf);
130                         } else if (ret == IDS_IDLE_BODY_PASSWORD_EMPTY) {
131                                 phone_lock_view_show_msg
132                                     (phone_lock_ugdata->phone_lock_view_data,
133                                      phone_lock_get_string
134                                      (IDS_IDLE_BODY_PASSWORD_EMPTY));
135                         }
136                         break;
137                 case 3:
138                         _phone_lock_ug_send_result(phone_lock_ugdata,
139                                                    "phone-lock-ug-alpha", str);
140                         break;
141                 default:
142                         PHONE_LOCK_DBG("invalid type\n");
143                         break;
144                 }
145         } else if (phone_lock_view_type == PHONE_LOCK_VIEW_TYPE_PIN_LOCK) {
146         } else {
147                 PHONE_LOCK_DBG("invalid view type\n");
148         }
149
150 }
151
152 static Eina_Bool _phone_lock_ug_keydown_cb(void *data, int type, void *event)
153 {
154         PHONE_LOCK_DBG("");
155
156         Ecore_Event_Key *ev = event;
157         struct phone_lock_ug_data *phone_lock_ugdata =
158             (struct phone_lock_ug_data *)data;
159
160         if (!strcmp(ev->keyname, KEY_END) || !strcmp(ev->keyname, KEY_SELECT)) {
161                 if (phone_lock_ugdata != NULL) {
162                         if (phone_lock_ugdata->ug_dialer != NULL) {
163                                 ug_destroy(phone_lock_ugdata->ug_dialer);
164                                 phone_lock_ugdata->ug_dialer = NULL;
165                         }
166                 }
167         }
168
169         return ECORE_CALLBACK_PASS_ON;
170 }
171
172 static void _phone_lock_ug_key_grab(struct phone_lock_ug_data
173                                     *phone_lock_ugdata)
174 {
175         Ecore_X_Window xwin;
176         Ecore_X_Display *disp = NULL;
177         int r = 0;
178
179         if (phone_lock_ugdata == NULL) {
180                 return;
181         }
182         disp = ecore_x_display_get();
183         xwin = elm_win_xwindow_get((Evas_Object *) ug_get_window());
184
185         r = utilx_grab_key(disp, xwin, KEY_SELECT, TOP_POSITION_GRAB);
186         if (r) {
187                 return;
188         }
189         phone_lock_ugdata->event_handler =
190             ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
191                                     _phone_lock_ug_keydown_cb,
192                                     phone_lock_ugdata);
193 }
194
195 static void _phone_lock_ug_key_ungrab(struct phone_lock_ug_data
196                                       *phone_lock_ugdata)
197 {
198         Ecore_X_Window xwin;
199         Ecore_X_Display *disp = NULL;
200         int r = 0;
201
202         if (phone_lock_ugdata == NULL) {
203                 return;
204         }
205
206         disp = ecore_x_display_get();
207         xwin = elm_win_xwindow_get((Evas_Object *) ug_get_window());
208
209         r = utilx_ungrab_key(disp, xwin, KEY_SELECT);
210         if (r) {
211                 return;
212         }
213
214         ecore_event_handler_del(phone_lock_ugdata->event_handler);
215 }
216
217 static void _phone_lock_ug_pm_state_cb(keynode_t * node, void *data)
218 {
219         struct phone_lock_ug_data *phone_lock_ugdata =
220             (struct phone_lock_ug_data *)data;
221         int val;
222
223         if (phone_lock_ugdata == NULL) {
224                 PHONE_LOCK_WARN("");
225                 return;
226         }
227
228         if (vconf_get_int(VCONFKEY_PM_STATE, &val) < 0) {
229                 PHONE_LOCK_WARN("Fail get vconf : VCONFKEY_PM_STATE");
230                 return;
231         }
232
233         if (val == VCONFKEY_PM_STATE_LCDOFF) {
234                 if (phone_lock_ugdata->ug_dialer != NULL) {
235                         ug_destroy(phone_lock_ugdata->ug_dialer);
236                         phone_lock_ugdata->ug_dialer = NULL;
237                 }
238         }
239 }
240
241 static void _phone_lock_ug_init_vconf(struct phone_lock_ug_data
242                                       *phone_lock_ugdata)
243 {
244         if (vconf_notify_key_changed
245             (VCONFKEY_PM_STATE, _phone_lock_ug_pm_state_cb,
246              phone_lock_ugdata) != 0) {
247                 PHONE_LOCK_ERR
248                     ("Fail vconf_notify_key_changed : VCONFKEY_PM_STATE");
249         }
250 }
251
252 static void _phone_lock_ug_fini_vconf(struct phone_lock_ug_data
253                                       *phone_lock_ugdata)
254 {
255         if (vconf_ignore_key_changed
256             (VCONFKEY_PM_STATE, _phone_lock_ug_pm_state_cb)
257             != 0) {
258                 PHONE_LOCK_ERR
259                     ("Fail vconf_ignore_key_changed : VCONFKEY_PM_STATE");
260         }
261 }
262
263 static void *phone_lock_ug_on_create(struct ui_gadget *ug, enum ug_mode mode,
264                                      bundle * data, void *priv)
265 {
266         PHONE_LOCK_DBG("%s", __FUNCTION__);
267
268         struct phone_lock_ug_data *phone_lock_ugdata;
269         phone_lock_view_type_t view_type;
270         phone_lock_window_type_t window_type;
271         const char *val1;
272         const char *val2;
273
274         if (!ug || !priv)
275                 return NULL;
276
277         bindtextdomain(PACKAGE, LOCALEDIR);
278
279         phone_lock_ugdata = priv;
280         phone_lock_ugdata->ug = ug;
281
282         val1 = bundle_get_val(data, "phone-lock-type");
283         PHONE_LOCK_DBG("phone lock type : %s\n", val1);
284
285         if (val1 == NULL)
286                 return NULL;
287
288         if (!strcmp(val1, "phone-lock")) {
289                 view_type = PHONE_LOCK_VIEW_TYPE_PHONE_LOCK;
290         } else if (!strcmp(val1, "pin-lock")) {
291                 view_type = PHONE_LOCK_VIEW_TYPE_PIN_LOCK;
292         } else {
293                 PHONE_LOCK_DBG("Error. Invalied phone lock type.\n");
294                 return NULL;
295         }
296
297         val2 = bundle_get_val(data, "window-type");
298         PHONE_LOCK_DBG("window type : %s\n", val2);
299
300         if (val2 == NULL)
301                 return NULL;
302
303         if (!strcmp(val2, "normal")) {
304                 window_type = PHONE_LOCK_WINDOW_TYPE_NORMAL;
305         } else if (!strcmp(val2, "alpha")) {
306                 window_type = PHONE_LOCK_WINDOW_TYPE_ALPHA;
307         } else {
308                 PHONE_LOCK_DBG("Error. Invalied window type.\n");
309                 window_type = PHONE_LOCK_WINDOW_TYPE_NORMAL;
310         }
311
312         _phone_lock_ug_key_grab(phone_lock_ugdata);
313
314         _phone_lock_ug_init_vconf(phone_lock_ugdata);
315
316         phone_lock_ugdata->phone_lock_view_data =
317             phone_lock_view_create(view_type, window_type,
318                                    (Evas_Object *) ug_get_window(),
319                                    (void *)_phone_lock_ug_view_cb,
320                                    phone_lock_ugdata);
321
322         phone_lock_ugdata->base =
323             phone_lock_view_get_layoutmain
324             (phone_lock_ugdata->phone_lock_view_data);
325
326         return phone_lock_ugdata->base;
327 }
328
329 static void phone_lock_ug_on_start(struct ui_gadget *ug, bundle * data,
330                                    void *priv)
331 {
332         PHONE_LOCK_DBG("%s", __FUNCTION__);
333 }
334
335 static void phone_lock_ug_on_pause(struct ui_gadget *ug, bundle * data,
336                                    void *priv)
337 {
338         PHONE_LOCK_DBG("%s", __FUNCTION__);
339 }
340
341 static void phone_lock_ug_on_resume(struct ui_gadget *ug, bundle * data,
342                                     void *priv)
343 {
344         PHONE_LOCK_DBG("%s", __FUNCTION__);
345 }
346
347 static void phone_lock_ug_on_destroy(struct ui_gadget *ug, bundle * data,
348                                      void *priv)
349 {
350         PHONE_LOCK_DBG("%s", __FUNCTION__);
351
352         struct phone_lock_ug_data *phone_lock_ugdata;
353         phone_lock_view_data_t *phone_lock_view_data;
354
355         if (!ug || !priv)
356                 return;
357
358         phone_lock_ugdata = priv;
359         phone_lock_view_data = phone_lock_ugdata->phone_lock_view_data;
360
361         if (!phone_lock_view_data) {
362                 return;
363         }
364
365         _phone_lock_ug_fini_vconf(phone_lock_ugdata);
366         _phone_lock_ug_key_ungrab(phone_lock_ugdata);
367
368         phone_lock_view_destroy(phone_lock_view_data);
369
370         if (phone_lock_ugdata->base) {
371                 evas_object_del(phone_lock_ugdata->base);
372                 phone_lock_ugdata->base = NULL;
373         }
374 }
375
376 static void phone_lock_ug_on_message(struct ui_gadget *ug, bundle * msg,
377                                      bundle * data, void *priv)
378 {
379         PHONE_LOCK_DBG("%s", __FUNCTION__);
380 }
381
382 static void phone_lock_ug_on_event(struct ui_gadget *ug, enum ug_event event,
383                                    bundle * data, void *priv)
384 {
385         PHONE_LOCK_DBG("%s, event : %d", __FUNCTION__, event);
386
387         switch (event) {
388         case UG_EVENT_LOW_MEMORY:
389                 break;
390         case UG_EVENT_LOW_BATTERY:
391                 break;
392         case UG_EVENT_LANG_CHANGE:
393                 break;
394         case UG_EVENT_ROTATE_PORTRAIT:
395                 break;
396         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
397                 break;
398         case UG_EVENT_ROTATE_LANDSCAPE:
399                 break;
400         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
401                 break;
402         default:
403                 break;
404         }
405 }
406
407 static void phone_lock_ug_on_key_event(struct ui_gadget *ug,
408                                        enum ug_key_event event, bundle * data,
409                                        void *priv)
410 {
411         PHONE_LOCK_DBG("%s, event : %d", __FUNCTION__, event);
412
413         if (!ug)
414                 return;
415
416         switch (event) {
417         case UG_KEY_EVENT_END:
418                 ug_destroy_me(ug);
419                 break;
420         default:
421                 break;
422         }
423 }
424
425 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
426 {
427         PHONE_LOCK_DBG("%s", __FUNCTION__);
428
429         struct phone_lock_ug_data *phone_lock_ugdata;
430
431         if (!ops)
432                 return -1;
433
434         phone_lock_ugdata = calloc(1, sizeof(struct phone_lock_ug_data));
435
436         if (!phone_lock_ugdata)
437                 return -1;
438
439         ops->create = phone_lock_ug_on_create;
440         ops->start = phone_lock_ug_on_start;
441         ops->pause = phone_lock_ug_on_pause;
442         ops->resume = phone_lock_ug_on_resume;
443         ops->destroy = phone_lock_ug_on_destroy;
444         ops->message = phone_lock_ug_on_message;
445         ops->event = phone_lock_ug_on_event;
446         ops->key_event = phone_lock_ug_on_key_event;
447         ops->priv = phone_lock_ugdata;
448         ops->opt = UG_OPT_INDICATOR_ENABLE;
449
450         return 0;
451 }
452
453 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
454 {
455         PHONE_LOCK_DBG("%s", __FUNCTION__);
456
457         struct phone_lock_ug_data *phone_lock_ugdata;
458
459         if (!ops)
460                 return;
461
462         phone_lock_ugdata = ops->priv;
463
464         if (phone_lock_ugdata) {
465                 free(phone_lock_ugdata);
466                 phone_lock_ugdata = NULL;
467         }
468 }