[Bt-syspopup] change bluez agent path
[platform/core/connectivity/bt-syspopup.git] / src / bt-syspopup.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software 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 #include <stdio.h>
18 #include <pmapi.h>
19 #include <appcore-efl.h>
20 #include <vconf.h>
21 #include <vconf-keys.h>
22 #include <syspopup.h>
23 #include <E_DBus.h>
24 #include <aul.h>
25 #include <bluetooth-api.h>
26 #include <feedback.h>
27 #include <linux/input.h>
28
29 #include "bt-syspopup.h"
30
31 static void __bluetooth_delete_input_view(struct bt_popup_appdata *ad);
32 static void __bluetooth_win_del(void *data);
33
34 static int __bluetooth_term(bundle *b, void *data)
35 {
36         BT_DBG("System-popup: terminate");
37         return 0;
38 }
39
40 static int __bluetooth_timeout(bundle *b, void *data)
41 {
42         BT_DBG("System-popup: timeout");
43         return 0;
44 }
45
46 syspopup_handler handler = {
47         .def_term_fn = __bluetooth_term,
48         .def_timeout_fn = __bluetooth_timeout
49 };
50
51 /* Cleanup objects to avoid mem-leak */
52 static void __bluetooth_cleanup(struct bt_popup_appdata *ad)
53 {
54         if (ad == NULL)
55                 return;
56
57         if (ad->popup) {
58                 evas_object_del(ad->popup);
59                 ad->popup = NULL;
60         }
61
62         if (ad->win_main) {
63                 evas_object_del(ad->win_main);
64                 ad->win_main = NULL;
65         }
66
67         if (ad->agent_proxy) {
68                 g_object_unref(ad->agent_proxy);
69                 ad->agent_proxy = NULL;
70         }
71 }
72
73 static void __bluetooth_notify_event(void)
74 {
75         int result;
76
77         BT_DBG("Notify event");
78
79         result = feedback_initialize();
80         if (result != FEEDBACK_ERROR_NONE) {
81                 BT_ERR("feedback_initialize error : %d", result);
82                 return;
83         }
84
85         result = feedback_play(FEEDBACK_PATTERN_GENERAL);
86         BT_DBG("ret value : %d", result);
87
88         result = feedback_deinitialize();
89         if (result != FEEDBACK_ERROR_NONE) {
90                 BT_DBG("feedback_initialize error : %d", result);
91                 return;
92         }
93 }
94
95 static void __bluetooth_parse_event(struct bt_popup_appdata *ad, const char *event_type)
96 {
97         if (!strcasecmp(event_type, "pin-request"))
98                 ad->event_type = BT_EVENT_PIN_REQUEST;
99         else if (!strcasecmp(event_type, "passkey-confirm-request"))
100                 ad->event_type = BT_EVENT_PASSKEY_CONFIRM_REQUEST;
101         else if (!strcasecmp(event_type, "passkey-request"))
102                 ad->event_type = BT_EVENT_PASSKEY_REQUEST;
103         else if (!strcasecmp(event_type, "authorize-request"))
104                 ad->event_type = BT_EVENT_AUTHORIZE_REQUEST;
105         else if (!strcasecmp(event_type, "app-confirm-request"))
106                 ad->event_type = BT_EVENT_APP_CONFIRM_REQUEST;
107         else if (!strcasecmp(event_type, "push-authorize-request"))
108                 ad->event_type = BT_EVENT_PUSH_AUTHORIZE_REQUEST;
109         else if (!strcasecmp(event_type, "confirm-overwrite-request"))
110                 ad->event_type = BT_EVENT_CONFIRM_OVERWRITE_REQUEST;
111         else if (!strcasecmp(event_type, "keyboard-passkey-request"))
112                 ad->event_type = BT_EVENT_KEYBOARD_PASSKEY_REQUEST;
113         else if (!strcasecmp(event_type, "bt-information"))
114                 ad->event_type = BT_EVENT_INFORMATION;
115         else if (!strcasecmp(event_type, "exchange-request"))
116                 ad->event_type = BT_EVENT_EXCHANGE_REQUEST;
117         else if (!strcasecmp(event_type, "phonebook-request"))
118                 ad->event_type = BT_EVENT_PHONEBOOK_REQUEST;
119         else if (!strcasecmp(event_type, "message-request"))
120                 ad->event_type = BT_EVENT_MESSAGE_REQUEST;
121         else
122                 ad->event_type = 0x0000;
123                 return;
124 }
125
126 static void __bluetooth_request_to_cancel(void)
127 {
128         bluetooth_cancel_bonding();
129 }
130
131 static void __bluetooth_remove_all_event(struct bt_popup_appdata *ad)
132 {
133         switch (ad->event_type) {
134         case BT_EVENT_PIN_REQUEST:
135
136                 dbus_g_proxy_call_no_reply(ad->agent_proxy,
137                                            "ReplyPinCode",
138                                            G_TYPE_UINT, BT_AGENT_CANCEL,
139                                            G_TYPE_STRING, "", G_TYPE_INVALID,
140                                            G_TYPE_INVALID);
141
142                 break;
143
144
145         case BT_EVENT_KEYBOARD_PASSKEY_REQUEST:
146
147                 __bluetooth_request_to_cancel();
148
149                 break;
150
151         case BT_EVENT_PASSKEY_CONFIRM_REQUEST:
152
153                 dbus_g_proxy_call_no_reply(ad->agent_proxy,
154                                            "ReplyConfirmation",
155                                            G_TYPE_UINT, BT_AGENT_CANCEL,
156                                            G_TYPE_INVALID, G_TYPE_INVALID);
157
158                 break;
159
160         case BT_EVENT_PASSKEY_REQUEST:
161
162                 dbus_g_proxy_call_no_reply(ad->agent_proxy,
163                                            "ReplyPasskey",
164                                            G_TYPE_UINT, BT_AGENT_CANCEL,
165                                            G_TYPE_STRING, "", G_TYPE_INVALID,
166                                            G_TYPE_INVALID);
167
168                 break;
169
170         case BT_EVENT_PASSKEY_DISPLAY_REQUEST:
171                 /* Nothing to do */
172                 break;
173
174         case BT_EVENT_AUTHORIZE_REQUEST:
175
176                 dbus_g_proxy_call_no_reply(ad->agent_proxy,
177                                            "ReplyAuthorize",
178                                            G_TYPE_UINT, BT_AGENT_CANCEL,
179                                            G_TYPE_INVALID, G_TYPE_INVALID);
180
181                 break;
182
183         case BT_EVENT_APP_CONFIRM_REQUEST:
184                 {
185                         DBusMessage *msg = NULL;
186                         int response = 2;
187
188                         msg = dbus_message_new_signal(
189                                         BT_SYS_POPUP_IPC_RESPONSE_OBJECT,
190                                         BT_SYS_POPUP_INTERFACE,
191                                         BT_SYS_POPUP_METHOD_RESPONSE);
192
193                         /* For timeout rejection is sent to  be handled in
194                            application */
195                         response = 1;
196
197                         dbus_message_append_args(msg,
198                                  DBUS_TYPE_INT32, &response,
199                                  DBUS_TYPE_INVALID);
200
201                         e_dbus_message_send(ad->EDBusHandle,
202                                 msg, NULL, -1, NULL);
203
204                         dbus_message_unref(msg);
205                 }
206                 break;
207
208         case BT_EVENT_PUSH_AUTHORIZE_REQUEST:
209         case BT_EVENT_EXCHANGE_REQUEST:
210
211                 dbus_g_proxy_call_no_reply(ad->obex_proxy,
212                                            "ReplyAuthorize",
213                                            G_TYPE_UINT, BT_AGENT_CANCEL,
214                                            G_TYPE_INVALID, G_TYPE_INVALID);
215
216                 break;
217
218         case BT_EVENT_CONFIRM_OVERWRITE_REQUEST:
219
220                 dbus_g_proxy_call_no_reply(ad->obex_proxy,
221                                            "ReplyOverwrite",
222                                            G_TYPE_UINT, BT_AGENT_CANCEL,
223                                            G_TYPE_INVALID, G_TYPE_INVALID);
224
225                 break;
226
227         default:
228                 break;
229         }
230
231         __bluetooth_win_del(ad);
232 }
233
234 static int __bluetooth_keydown_cb(void *data, int type, void *event_info)
235 {
236         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
237         Ecore_Event_Key *ev = event_info;
238
239         if (!g_strcmp0(ev->keyname, KEY_END) || !g_strcmp0(ev->keyname, KEY_SELECT)) {
240                 BT_DBG("Key [%s]", ev->keyname);
241                 /* remove_all_event(); */
242
243                 if (!g_strcmp0(ev->keyname, KEY_END)) {
244                         __bluetooth_remove_all_event(ad);
245                 }
246         }
247
248         return 0;
249 }
250
251 static int __bluetooth_request_timeout_cb(void *data)
252 {
253         struct bt_popup_appdata *ad;
254
255         if (data == NULL)
256                 return 0;
257
258         ad = (struct bt_popup_appdata *)data;
259
260         BT_DBG("Request time out, Canceling reqeust");
261
262         /* Destory UI and timer */
263         if (ad->timer) {
264                 ecore_timer_del(ad->timer);
265                 ad->timer = NULL;
266         }
267
268         __bluetooth_remove_all_event(ad);
269         return 0;
270 }
271
272 static void __bluetooth_input_request_cb(void *data,
273                                        Evas_Object *obj, void *event_info)
274 {
275         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
276         const char *event = elm_object_text_get(obj);
277         int response;
278         char *input_text = NULL;
279         char *convert_input_text = NULL;
280
281         if (ad == NULL)
282                 return;
283
284         /* BT_EVENT_PIN_REQUEST / BT_EVENT_PASSKEY_REQUEST */
285
286         input_text = (char *)elm_entry_entry_get(ad->entry);
287
288         if (input_text) {
289                 convert_input_text =
290                     elm_entry_markup_to_utf8(input_text);
291         }
292
293         if (!g_strcmp0(event, BT_STR_OK))
294                 response = 1;
295         else
296                 response = 0;
297
298         if (convert_input_text == NULL)
299                 return;
300
301         BT_DBG("PIN/Passkey[%s] event[%d] response[%d]",
302                      convert_input_text, ad->event_type, response);
303
304         if (response == 1) {
305                 BT_DBG("Done case");
306                 if (ad->event_type == BT_EVENT_PIN_REQUEST) {
307                         dbus_g_proxy_call_no_reply(ad->agent_proxy,
308                                                    "ReplyPinCode",
309                                                    G_TYPE_UINT, BT_AGENT_ACCEPT,
310                                                    G_TYPE_STRING,
311                                                    convert_input_text,
312                                                    G_TYPE_INVALID,
313                                                    G_TYPE_INVALID);
314                 } else {
315                         dbus_g_proxy_call_no_reply(ad->agent_proxy,
316                                                    "ReplyPasskey",
317                                                    G_TYPE_UINT, BT_AGENT_ACCEPT,
318                                                    G_TYPE_STRING,
319                                                    convert_input_text,
320                                                    G_TYPE_INVALID,
321                                                    G_TYPE_INVALID);
322                 }
323         } else {
324                 BT_DBG("Cancel case");
325                 if (ad->event_type == BT_EVENT_PIN_REQUEST) {
326                         dbus_g_proxy_call_no_reply(ad->agent_proxy,
327                                                    "ReplyPinCode",
328                                                    G_TYPE_UINT, BT_AGENT_CANCEL,
329                                                    G_TYPE_STRING, "",
330                                                    G_TYPE_INVALID,
331                                                    G_TYPE_INVALID);
332                 } else {
333                         dbus_g_proxy_call_no_reply(ad->agent_proxy,
334                                                    "ReplyPasskey",
335                                                    G_TYPE_UINT, BT_AGENT_CANCEL,
336                                                    G_TYPE_STRING, "",
337                                                    G_TYPE_INVALID,
338                                                    G_TYPE_INVALID);
339                 }
340         }
341
342         __bluetooth_delete_input_view(ad);
343
344         free(convert_input_text);
345
346         __bluetooth_win_del(ad);
347 }
348
349 static void __bluetooth_input_cancel_cb(void *data,
350                                        Evas_Object *obj, void *event_info)
351 {
352         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
353
354         bluetooth_cancel_bonding();
355
356         __bluetooth_win_del(ad);
357 }
358
359 static void __bluetooth_passkey_confirm_cb(void *data,
360                                          Evas_Object *obj, void *event_info)
361 {
362         if (obj == NULL || data == NULL)
363                 return;
364
365         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
366         const char *event = elm_object_text_get(obj);
367
368         if (!g_strcmp0(event, BT_STR_OK)) {
369                 dbus_g_proxy_call_no_reply(ad->agent_proxy, "ReplyConfirmation",
370                                            G_TYPE_UINT, BT_AGENT_ACCEPT,
371                                            G_TYPE_INVALID, G_TYPE_INVALID);
372         } else {
373                 dbus_g_proxy_call_no_reply(ad->agent_proxy, "ReplyConfirmation",
374                                            G_TYPE_UINT, BT_AGENT_CANCEL,
375                                            G_TYPE_INVALID, G_TYPE_INVALID);
376         }
377
378         evas_object_del(obj);
379
380         __bluetooth_win_del(ad);
381 }
382
383 static int __bluetooth_init_app_signal(struct bt_popup_appdata *ad)
384 {
385         if (NULL == ad)
386                 return FALSE;
387
388         e_dbus_init();
389         ad->EDBusHandle = e_dbus_bus_get(DBUS_BUS_SYSTEM);
390         if (!ad->EDBusHandle) {
391                 BT_ERR("e_dbus_bus_get failed  \n ");
392                 return FALSE;
393         } else {
394                 BT_DBG("e_dbus_bus_get success \n ");
395                 e_dbus_request_name(ad->EDBusHandle,
396                                     BT_SYS_POPUP_IPC_NAME, 0, NULL, NULL);
397         }
398         return TRUE;
399 }
400
401 static void __bluetooth_app_confirm_cb(void *data,
402                                      Evas_Object *obj, void *event_info)
403 {
404         BT_DBG("__bluetooth_app_confirm_cb ");
405         if (obj == NULL || data == NULL)
406                 return;
407
408         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
409         const char *event = elm_object_text_get(obj);
410
411         DBusMessage *msg = NULL;
412         int response;
413
414         msg = dbus_message_new_signal(BT_SYS_POPUP_IPC_RESPONSE_OBJECT,
415                                       BT_SYS_POPUP_INTERFACE,
416                                       BT_SYS_POPUP_METHOD_RESPONSE);
417
418         if (!g_strcmp0(event, BT_STR_YES) || !g_strcmp0(event, BT_STR_OK))
419                 response = 0;
420         else
421                 response = 1;
422
423         dbus_message_append_args(msg,
424                                  DBUS_TYPE_INT32, &response, DBUS_TYPE_INVALID);
425
426         e_dbus_message_send(ad->EDBusHandle, msg, NULL, -1, NULL);
427         dbus_message_unref(msg);
428
429         evas_object_del(obj);
430
431         __bluetooth_win_del(ad);
432 }
433
434 static void __bluetooth_authorization_request_cb(void *data,
435                                                Evas_Object *obj,
436                                                void *event_info)
437 {
438         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
439         if (obj == NULL || ad == NULL)
440                 return;
441
442         const char *event = elm_object_text_get(obj);
443
444         if (!g_strcmp0(event, BT_STR_YES)) {
445                 dbus_g_proxy_call_no_reply(ad->agent_proxy, "ReplyAuthorize",
446                                            G_TYPE_UINT, BT_AGENT_ACCEPT,
447                                            G_TYPE_INVALID, G_TYPE_INVALID);
448         } else {
449                 dbus_g_proxy_call_no_reply(ad->agent_proxy, "ReplyAuthorize",
450                                            G_TYPE_UINT, BT_AGENT_CANCEL,
451                                            G_TYPE_INVALID, G_TYPE_INVALID);
452         }
453
454         __bluetooth_win_del(ad);
455 }
456
457 static void __bluetooth_push_authorization_request_cb(void *data,
458                                                     Evas_Object *obj,
459                                                     void *event_info)
460 {
461         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
462         if (obj == NULL || ad == NULL)
463                 return;
464
465         const char *event = elm_object_text_get(obj);
466
467         if (!g_strcmp0(event, BT_STR_YES))
468                 dbus_g_proxy_call_no_reply(ad->obex_proxy, "ReplyAuthorize",
469                                            G_TYPE_UINT, BT_AGENT_ACCEPT,
470                                            G_TYPE_INVALID, G_TYPE_INVALID);
471         else
472                 dbus_g_proxy_call_no_reply(ad->obex_proxy, "ReplyAuthorize",
473                                            G_TYPE_UINT, BT_AGENT_CANCEL,
474                                            G_TYPE_INVALID, G_TYPE_INVALID);
475
476         __bluetooth_win_del(ad);
477 }
478
479 static void __bluetooth_ime_hide(void)
480 {
481         Ecore_IMF_Context *imf_context = NULL;
482         imf_context = ecore_imf_context_add(ecore_imf_context_default_id_get());
483         if (imf_context)
484                 ecore_imf_context_input_panel_hide(imf_context);
485 }
486
487 static void __bluetooth_entry_change_cb(void *data, Evas_Object *obj,
488                                       void *event_info)
489 {
490         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
491         char *input_text = NULL;
492         char *convert_input_text = NULL;
493         char *output_text = NULL;
494         int text_length = 0;
495
496         input_text = (char *)elm_entry_entry_get(obj);
497
498         if (input_text) {
499                 convert_input_text = elm_entry_markup_to_utf8(input_text);
500                 if (convert_input_text) {
501                         text_length = strlen(convert_input_text);
502
503                         if (text_length == 0) {
504                                 elm_object_disabled_set(ad->edit_field_save_btn,
505                                                         EINA_TRUE);
506                                 elm_object_signal_emit(ad->editfield,
507                                                         "elm,state,eraser,hide",
508                                                         "elm");
509                         } else {
510                                 elm_object_disabled_set(ad->edit_field_save_btn,
511                                                         EINA_FALSE);
512                                 elm_object_signal_emit(ad->editfield,
513                                                         "elm,state,eraser,show",
514                                                         "elm");
515                         }
516
517                         if (ad->event_type == BT_EVENT_PASSKEY_REQUEST) {
518                                 if (text_length > BT_PK_MLEN) {
519                                         text_length = BT_PK_MLEN;
520                                         convert_input_text[BT_PK_MLEN] = '\0';
521                                         output_text =
522                                             elm_entry_utf8_to_markup
523                                             (convert_input_text);
524
525                                         elm_entry_entry_set(obj, output_text);
526                                         elm_entry_cursor_end_set(obj);
527                                         free(output_text);
528                                 }
529                         } else {
530                                 if (text_length > BT_PIN_MLEN) {
531                                         text_length = BT_PIN_MLEN;
532                                         convert_input_text[BT_PIN_MLEN] = '\0';
533                                         output_text =
534                                             elm_entry_utf8_to_markup
535                                                 (convert_input_text);
536
537                                         elm_entry_entry_set(obj, output_text);
538                                         elm_entry_cursor_end_set(obj);
539                                         free(output_text);
540                                 }
541                         }
542                         free(convert_input_text);
543                 }
544         }
545 }
546
547 static void __bluetooth_entry_focused_cb(void *data, Evas_Object *obj,
548                                       void *event_info)
549 {
550         if (!elm_entry_is_empty(obj))
551                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
552
553         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
554 }
555
556 static void __bluetooth_entry_unfocused_cb(void *data, Evas_Object *obj,
557                                       void *event_info)
558 {
559         if (elm_entry_is_empty(obj))
560                 elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
561
562         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
563 }
564
565 static void __bluetooth_eraser_clicked_cb(void* data, Evas_Object* obj,
566                                 const char* emission, const char* source)
567 {
568         elm_entry_entry_set(data, "");
569 }
570
571 static void __bluetooth_check_chagned_cb(void *data, Evas_Object *obj,
572                                       void *event_info)
573 {
574         Eina_Bool state = EINA_FALSE;
575
576         if (obj == NULL)
577                 return;
578
579         state = elm_check_state_get(obj);
580         elm_entry_password_set(data, !state);
581 }
582
583 static void __bluetooth_draw_popup(struct bt_popup_appdata *ad,
584                         const char *title, char *btn1_text,
585                         char *btn2_text, void (*func) (void *data,
586                         Evas_Object *obj, void *event_info))
587 {
588         char temp_str[BT_TITLE_STR_MAX_LEN+BT_TEXT_EXTRA_LEN] = { 0 };
589         Evas_Object *btn1;
590         Evas_Object *btn2;
591
592         BT_DBG("__bluetooth_draw_popup");
593
594         ad->popup = elm_popup_add(ad->win_main);
595         evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
596                                          EVAS_HINT_EXPAND);
597
598         elm_object_style_set(ad->popup, "transparent");
599
600         if (title != NULL) {
601                 snprintf(temp_str, BT_TITLE_STR_MAX_LEN+BT_TEXT_EXTRA_LEN,
602                         "<align=center>%s</align>", title);
603                 elm_object_text_set(ad->popup, temp_str);
604         }
605
606         if ((btn1_text != NULL) && (btn2_text != NULL)) {
607                 btn1 = elm_button_add(ad->popup);
608                 elm_object_style_set(btn1, "popup_button/default");
609                 elm_object_text_set(btn1, btn1_text);
610                 elm_object_part_content_set(ad->popup, "button1", btn1);
611                 evas_object_smart_callback_add(btn1, "clicked", func, ad);
612
613                 btn2 = elm_button_add(ad->popup);
614                 elm_object_style_set(btn2, "popup_button/default");
615                 elm_object_text_set(btn2, btn2_text);
616                 elm_object_part_content_set(ad->popup, "button2", btn2);
617                 evas_object_smart_callback_add(btn2, "clicked", func, ad);
618         } else if (btn1_text != NULL) {
619                 btn1 = elm_button_add(ad->popup);
620                 elm_object_style_set(btn1, "popup_button/default");
621                 elm_object_text_set(btn1, btn1_text);
622                 elm_object_part_content_set(ad->popup, "button1", btn1);
623                 evas_object_smart_callback_add(btn1, "clicked", func, ad);
624         }
625
626         evas_object_show(ad->popup);
627         evas_object_show(ad->win_main);
628
629         BT_DBG("__bluetooth_draw_popup END");
630 }
631
632 static void __bluetooth_draw_input_view(struct bt_popup_appdata *ad,
633                         const char *title, const char *text,
634                         void (*func)
635                         (void *data, Evas_Object *obj, void *event_info))
636 {
637         Evas_Object *conformant = NULL;
638         Evas_Object *content = NULL;
639         Evas_Object *layout = NULL;
640         Evas_Object *passpopup = NULL;
641         Evas_Object *label = NULL;
642         Evas_Object *editfield = NULL;
643         Evas_Object *entry = NULL;
644         Evas_Object *check = NULL;
645         Evas_Object *l_button = NULL;
646         Evas_Object *r_button = NULL;
647
648         if (ad == NULL || ad->win_main == NULL) {
649                 BT_ERR("Invalid parameter");
650                 return;
651         }
652
653         evas_object_show(ad->win_main);
654
655         conformant = elm_conformant_add(ad->win_main);
656         if (conformant == NULL) {
657                 BT_ERR("conformant is NULL");
658                 return;
659         }
660         ad->popup = conformant;
661
662         elm_win_conformant_set(ad->win_main, EINA_TRUE);
663         elm_win_resize_object_add(ad->win_main, conformant);
664         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
665         evas_object_size_hint_align_set(conformant, EVAS_HINT_FILL, EVAS_HINT_FILL);
666         evas_object_show(conformant);
667
668         content = elm_layout_add(conformant);
669         elm_object_content_set(conformant, content);
670
671         passpopup = elm_popup_add(content);
672         elm_object_part_text_set(passpopup, "title,text", title);
673
674         elm_object_style_set(passpopup, "transparent");
675
676         layout = elm_layout_add(passpopup);
677         elm_layout_file_set(layout, CUSTOM_POPUP_PATH, "popup_checkview_image");
678         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
679
680         label = elm_label_add(passpopup);
681         elm_object_style_set(label, "popup/default");
682         elm_label_line_wrap_set(label, ELM_WRAP_CHAR);
683         elm_object_text_set(label, text);
684         evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
685         evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
686         evas_object_show(label);
687
688         editfield = elm_layout_add(passpopup);
689         elm_layout_theme_set(editfield, "layout", "editfield", "default");
690         evas_object_size_hint_weight_set(editfield, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
691         evas_object_size_hint_align_set(editfield, EVAS_HINT_FILL, EVAS_HINT_FILL);
692         ad->editfield = editfield;
693
694         entry = elm_entry_add(passpopup);
695         elm_entry_single_line_set(entry, EINA_TRUE);
696         elm_entry_scrollable_set(entry, EINA_TRUE);
697         elm_entry_prediction_allow_set(entry, EINA_FALSE);
698         elm_entry_password_set(entry, TRUE);
699         elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBERONLY);
700         elm_object_part_content_set(editfield, "elm.swallow.content", entry);
701         ad->entry = entry;
702
703         evas_object_smart_callback_add(entry, "changed",
704                                 __bluetooth_entry_change_cb,
705                                 ad);
706
707         evas_object_smart_callback_add(entry, "focused",
708                                 __bluetooth_entry_focused_cb,
709                                 editfield);
710
711         evas_object_smart_callback_add(entry, "unfocused",
712                                 __bluetooth_entry_unfocused_cb,
713                                 editfield);
714
715         elm_object_signal_callback_add(editfield, "elm,eraser,clicked", "elm",
716                                 (Edje_Signal_Cb)__bluetooth_eraser_clicked_cb,
717                                 entry);
718
719         evas_object_show(entry);
720         evas_object_show(editfield);
721
722         check = elm_check_add(passpopup);
723         elm_object_text_set(check, BT_STR_SHOW_PASSWORD);
724         elm_object_focus_allow_set(check, EINA_FALSE);
725         evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND,
726                                         EVAS_HINT_EXPAND);
727         evas_object_size_hint_align_set(check, EVAS_HINT_FILL,
728                                         EVAS_HINT_FILL);
729         evas_object_smart_callback_add(check, "changed",
730                                 __bluetooth_check_chagned_cb, entry);
731         evas_object_show(check);
732
733         l_button = elm_button_add(ad->win_main);
734         elm_object_style_set(l_button, "popup_button/default");
735         elm_object_text_set(l_button, BT_STR_OK);
736         elm_object_part_content_set(passpopup, "button1", l_button);
737         evas_object_smart_callback_add(l_button, "clicked", func, ad);
738         elm_object_disabled_set(l_button, EINA_TRUE);
739
740         ad->edit_field_save_btn = l_button;
741
742         r_button = elm_button_add(ad->win_main);
743         elm_object_style_set(r_button, "popup_button/default");
744         elm_object_text_set(r_button, BT_STR_CANCEL);
745         elm_object_part_content_set(passpopup, "button2", r_button);
746         evas_object_smart_callback_add(r_button, "clicked", func, ad);
747
748         elm_object_part_content_set(layout, "elm.swallow.content", label);
749         elm_object_part_content_set(layout, "elm.swallow.entry", editfield);
750         elm_object_part_content_set(layout, "elm.swallow.end", check);
751
752         evas_object_show(layout);
753         evas_object_show(content);
754         evas_object_show(passpopup);
755         elm_object_content_set(passpopup, layout);
756         elm_object_focus_set(entry, EINA_TRUE);
757 }
758
759 static void __bluetooth_delete_input_view(struct bt_popup_appdata *ad)
760 {
761         __bluetooth_ime_hide();
762 }
763
764 static DBusGProxy* __bluetooth_create_agent_proxy(DBusGConnection *conn,
765                                                                 const char *path)
766 {
767         return dbus_g_proxy_new_for_name(conn, "org.projectx.bt", path,
768                                                         "org.bluez.Agent1");
769 }
770
771 /* AUL bundle handler */
772 static int __bluetooth_launch_handler(struct bt_popup_appdata *ad,
773                              void *reset_data, const char *event_type)
774 {
775         bundle *kb = (bundle *) reset_data;
776         char view_title[BT_TITLE_STR_MAX_LEN] = { 0 };
777         char text[BT_GLOBALIZATION_STR_LENGTH] = { 0 };
778         int timeout = 0;
779         const char *device_name = NULL;
780         const char *passkey = NULL;
781         const char *file = NULL;
782         const char *agent_path;
783         char *conv_str = NULL;
784
785         BT_DBG("__bluetooth_launch_handler");
786
787         if (!reset_data || !event_type)
788                 return -1;
789
790         if (!strcasecmp(event_type, "pin-request")) {
791                 device_name = bundle_get_val(kb, "device-name");
792                 agent_path = bundle_get_val(kb, "agent-path");
793
794                 ad->agent_proxy = __bluetooth_create_agent_proxy(ad->conn, agent_path);
795                 if (!ad->agent_proxy)
796                         return -1;
797
798                 if (device_name)
799                         conv_str = elm_entry_utf8_to_markup(device_name);
800
801                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
802                          "%s", BT_STR_BLUETOOTH_PAIRING_REQUEST);
803
804                 snprintf(text, BT_GLOBALIZATION_STR_LENGTH,
805                          BT_STR_ENTER_PIN_TO_PAIR, conv_str);
806
807                 if (conv_str)
808                         free(conv_str);
809
810                 /* Request user inputted PIN for basic pairing */
811                 __bluetooth_draw_input_view(ad, view_title, text,
812                                           __bluetooth_input_request_cb);
813         } else if (!strcasecmp(event_type, "passkey-confirm-request")) {
814                 device_name = bundle_get_val(kb, "device-name");
815                 passkey = bundle_get_val(kb, "passkey");
816                 agent_path = bundle_get_val(kb, "agent-path");
817
818                 ad->agent_proxy = __bluetooth_create_agent_proxy(ad->conn, agent_path);
819                 if (!ad->agent_proxy)
820                         return -1;
821
822                 if (device_name && passkey) {
823                         conv_str = elm_entry_utf8_to_markup(device_name);
824
825                         snprintf(view_title, BT_TITLE_STR_MAX_LEN,
826                              BT_STR_CONFIRM_PASSKEY_PS_TO_PAIR_WITH_PS,
827                              conv_str, passkey);
828
829                         BT_DBG("title: %s", view_title);
830
831                         if (conv_str)
832                                 free(conv_str);
833
834                         __bluetooth_draw_popup(ad, view_title,
835                                         BT_STR_OK, BT_STR_CANCEL,
836                                         __bluetooth_passkey_confirm_cb);
837                 } else {
838                         timeout = BT_ERROR_TIMEOUT;
839                 }
840         } else if (!strcasecmp(event_type, "passkey-request")) {
841                 const char *device_name = NULL;
842
843                 device_name = bundle_get_val(kb, "device-name");
844                 agent_path = bundle_get_val(kb, "agent-path");
845
846                 ad->agent_proxy = __bluetooth_create_agent_proxy(ad->conn, agent_path);
847                 if (!ad->agent_proxy)
848                         return -1;
849
850                 if (device_name)
851                         conv_str = elm_entry_utf8_to_markup(device_name);
852
853                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
854                          "%s", BT_STR_BLUETOOTH_PAIRING_REQUEST);
855
856                 snprintf(text, BT_GLOBALIZATION_STR_LENGTH,
857                          BT_STR_ENTER_PIN_TO_PAIR, conv_str);
858
859                 if (conv_str)
860                         free(conv_str);
861
862                 /* Request user inputted Passkey for basic pairing */
863                 __bluetooth_draw_input_view(ad, view_title, text,
864                                           __bluetooth_input_request_cb);
865
866         } else if (!strcasecmp(event_type, "passkey-display-request")) {
867                 device_name = bundle_get_val(kb, "device-name");
868                 passkey = bundle_get_val(kb, "passkey");
869
870                 if (device_name && passkey) {
871                         conv_str = elm_entry_utf8_to_markup(device_name);
872
873                         snprintf(view_title, BT_TITLE_STR_MAX_LEN,
874                              BT_STR_ENTER_PS_ON_PS_TO_PAIR, passkey, conv_str);
875
876                         BT_DBG("title: %s", view_title);
877
878                         if (conv_str)
879                                 free(conv_str);
880
881                         __bluetooth_draw_popup(ad, view_title,
882                                                 BT_STR_CANCEL, NULL,
883                                                 __bluetooth_input_cancel_cb);
884                 } else {
885                         timeout = BT_ERROR_TIMEOUT;
886                 }
887         } else if (!strcasecmp(event_type, "authorize-request")) {
888                 timeout = BT_AUTHORIZATION_TIMEOUT;
889
890                 device_name = bundle_get_val(kb, "device-name");
891                 agent_path = bundle_get_val(kb, "agent-path");
892
893                 ad->agent_proxy = __bluetooth_create_agent_proxy(ad->conn, agent_path);
894                 if (!ad->agent_proxy)
895                         return -1;
896
897                 if (device_name)
898                         conv_str = elm_entry_utf8_to_markup(device_name);
899
900                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
901                          BT_STR_ALLOW_PS_TO_CONNECT_Q, conv_str);
902
903                 if (conv_str)
904                         free(conv_str);
905
906                 __bluetooth_draw_popup(ad, view_title, BT_STR_YES, BT_STR_NO,
907                                      __bluetooth_authorization_request_cb);
908         } else if (!strcasecmp(event_type, "app-confirm-request")) {
909                 BT_DBG("app-confirm-request");
910                 timeout = BT_AUTHORIZATION_TIMEOUT;
911
912                 const char *title = NULL;
913                 const char *type = NULL;
914
915                 title = bundle_get_val(kb, "title");
916                 type = bundle_get_val(kb, "type");
917
918                 if (!title)
919                         return -1;
920
921                 if (strcasecmp(type, "twobtn") == 0) {
922                         __bluetooth_draw_popup(ad, title, BT_STR_YES, BT_STR_NO,
923                                              __bluetooth_app_confirm_cb);
924                 } else if (strcasecmp(type, "onebtn") == 0) {
925                         timeout = BT_NOTIFICATION_TIMEOUT;
926                         __bluetooth_draw_popup(ad, title, BT_STR_YES, NULL,
927                                              __bluetooth_app_confirm_cb);
928                 } else if (strcasecmp(type, "none") == 0) {
929                         timeout = BT_NOTIFICATION_TIMEOUT;
930                         __bluetooth_draw_popup(ad, title, NULL, NULL,
931                                              __bluetooth_app_confirm_cb);
932                 }
933         } else if (!strcasecmp(event_type, "push-authorize-request")) {
934                 timeout = BT_AUTHORIZATION_TIMEOUT;
935
936                 device_name = bundle_get_val(kb, "device-name");
937                 file = bundle_get_val(kb, "file");
938
939                 if (device_name)
940                         conv_str = elm_entry_utf8_to_markup(device_name);
941
942                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
943                          BT_STR_RECEIVE_PS_FROM_PS_Q, file, conv_str);
944
945                 if (conv_str)
946                         free(conv_str);
947
948                 __bluetooth_draw_popup(ad, view_title, BT_STR_YES, BT_STR_NO,
949                                 __bluetooth_push_authorization_request_cb);
950         } else if (!strcasecmp(event_type, "confirm-overwrite-request")) {
951                 timeout = BT_AUTHORIZATION_TIMEOUT;
952
953                 file = bundle_get_val(kb, "file");
954
955                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
956                          BT_STR_OVERWRITE_FILE_Q, file);
957
958                 __bluetooth_draw_popup(ad, view_title, BT_STR_YES, BT_STR_NO,
959                                 __bluetooth_app_confirm_cb);
960         } else if (!strcasecmp(event_type, "keyboard-passkey-request")) {
961                 device_name = bundle_get_val(kb, "device-name");
962                 passkey = bundle_get_val(kb, "passkey");
963
964                 if (device_name && passkey) {
965                         conv_str = elm_entry_utf8_to_markup(device_name);
966
967                         snprintf(view_title, BT_TITLE_STR_MAX_LEN,
968                              BT_STR_ENTER_PS_ON_PS_TO_PAIR, passkey, conv_str);
969
970                         BT_DBG("title: %s", view_title);
971
972                         if (conv_str)
973                                 free(conv_str);
974
975                         __bluetooth_draw_popup(ad, view_title,
976                                                 BT_STR_CANCEL, NULL,
977                                                 __bluetooth_input_cancel_cb);
978                 } else {
979                         timeout = BT_ERROR_TIMEOUT;
980                 }
981         } else if (!strcasecmp(event_type, "bt-information")) {
982                 BT_DBG("bt-information");
983                 timeout = BT_NOTIFICATION_TIMEOUT;
984
985                 const char *title = NULL;
986                 const char *type = NULL;
987
988                 title = bundle_get_val(kb, "title");
989                 type = bundle_get_val(kb, "type");
990
991                 if (title != NULL) {
992                         if (strlen(title) > 255)
993                                 return -1;
994                 } else
995                         return -1;
996
997                 if (strcasecmp(type, "onebtn") == 0) {
998                         __bluetooth_draw_popup(ad, title, BT_STR_OK, NULL,
999                                              __bluetooth_app_confirm_cb);
1000                 } else if (strcasecmp(type, "none") == 0) {
1001                         __bluetooth_draw_popup(ad, title, NULL, NULL,
1002                                              __bluetooth_app_confirm_cb);
1003                 }
1004         } else if (!strcasecmp(event_type, "exchange-request")) {
1005                 timeout = BT_AUTHORIZATION_TIMEOUT;
1006
1007                 device_name = bundle_get_val(kb, "device-name");
1008                 agent_path = bundle_get_val(kb, "agent-path");
1009
1010                 ad->agent_proxy = __bluetooth_create_agent_proxy(ad->conn, agent_path);
1011                 if (!ad->agent_proxy)
1012                         return -1;
1013
1014                 if (device_name)
1015                         conv_str = elm_entry_utf8_to_markup(device_name);
1016
1017                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
1018                          BT_STR_EXCHANGE_OBJECT_WITH_PS_Q, conv_str);
1019
1020                 if (conv_str)
1021                         free(conv_str);
1022
1023                 __bluetooth_draw_popup(ad, view_title, BT_STR_YES, BT_STR_NO,
1024                                      __bluetooth_authorization_request_cb);
1025         } else if (!strcasecmp(event_type, "phonebook-request")) {
1026                 timeout = BT_AUTHORIZATION_TIMEOUT;
1027
1028                 device_name = bundle_get_val(kb, "device-name");
1029                 agent_path = bundle_get_val(kb, "agent-path");
1030
1031                 ad->agent_proxy = __bluetooth_create_agent_proxy(ad->conn, agent_path);
1032                 if (!ad->agent_proxy)
1033                         return -1;
1034
1035                 if (device_name)
1036                         conv_str = elm_entry_utf8_to_markup(device_name);
1037
1038                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
1039                          BT_STR_ALLOW_PS_PHONEBOOK_ACCESS_Q, conv_str);
1040
1041                 if (conv_str)
1042                         free(conv_str);
1043
1044                 __bluetooth_draw_popup(ad, view_title, BT_STR_YES, BT_STR_NO,
1045                                      __bluetooth_authorization_request_cb);
1046         } else if (!strcasecmp(event_type, "message-request")) {
1047                 timeout = BT_AUTHORIZATION_TIMEOUT;
1048
1049                 device_name = bundle_get_val(kb, "device-name");
1050                 agent_path = bundle_get_val(kb, "agent-path");
1051
1052                 ad->agent_proxy = __bluetooth_create_agent_proxy(ad->conn, agent_path);
1053                 if (!ad->agent_proxy)
1054                         return -1;
1055
1056                 if (device_name)
1057                         conv_str = elm_entry_utf8_to_markup(device_name);
1058
1059                 snprintf(view_title, BT_TITLE_STR_MAX_LEN,
1060                          BT_STR_ALLOW_PS_TO_ACCESS_MESSAGES_Q, conv_str);
1061
1062                 if (conv_str)
1063                         free(conv_str);
1064
1065                 __bluetooth_draw_popup(ad, view_title, BT_STR_YES, BT_STR_NO,
1066                                      __bluetooth_authorization_request_cb);
1067         } else {
1068                 return -1;
1069         }
1070
1071         if (ad->event_type != BT_EVENT_FILE_RECEIVED && timeout != 0) {
1072                 ad->timer = ecore_timer_add(timeout, (Ecore_Task_Cb)
1073                                         __bluetooth_request_timeout_cb,
1074                                         ad);
1075         }
1076
1077         return 0;
1078 }
1079
1080 static void __bluetooth_win_del(void *data)
1081 {
1082         struct bt_popup_appdata *ad = (struct bt_popup_appdata *)data;
1083
1084         __bluetooth_cleanup(ad);
1085
1086         elm_exit();
1087 }
1088
1089 static Evas_Object *__bluetooth_create_win(const char *name)
1090 {
1091         Evas_Object *eo;
1092
1093         eo = elm_win_add(NULL, name, ELM_WIN_DIALOG_BASIC);
1094         if (eo) {
1095                 elm_win_title_set(eo, name);
1096                 elm_win_borderless_set(eo, EINA_TRUE);
1097         }
1098
1099         return eo;
1100 }
1101
1102 static void __bluetooth_session_init(struct bt_popup_appdata *ad)
1103 {
1104         DBusGConnection *conn = NULL;
1105         GError *err = NULL;
1106
1107         g_type_init();
1108
1109         conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &err);
1110
1111         if (!conn) {
1112                 BT_ERR("ERROR: Can't get on system bus [%s]",
1113                              err->message);
1114                 g_error_free(err);
1115                 return;
1116         }
1117
1118         ad->conn = conn;
1119
1120         ad->obex_proxy = dbus_g_proxy_new_for_name(conn,
1121                                                    "org.bluez.frwk_agent",
1122                                                    "/org/obex/ops_agent",
1123                                                    "org.openobex.Agent");
1124         if (!ad->obex_proxy)
1125                 BT_ERR("Could not create obex dbus proxy");
1126
1127         if (!__bluetooth_init_app_signal(ad))
1128                 BT_ERR("__bt_syspopup_init_app_signal failed");
1129 }
1130
1131 static int __bluetooth_create(void *data)
1132 {
1133         struct bt_popup_appdata *ad = data;
1134         Evas_Object *win = NULL;
1135
1136         BT_DBG("__bluetooth_create() start.\n");
1137
1138         /* create window */
1139         win = __bluetooth_create_win(PACKAGE);
1140         if (win == NULL)
1141                 return -1;
1142         ad->win_main = win;
1143
1144         /* init internationalization */
1145         if (appcore_set_i18n(BT_COMMON_PKG, BT_COMMON_RES) < 0)
1146                 return -1;
1147
1148         ecore_imf_init();
1149         ad->event_handle = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
1150                                                    (Ecore_Event_Handler_Cb)
1151                                                    __bluetooth_keydown_cb,
1152                                                    ad);
1153
1154         __bluetooth_session_init(ad);
1155
1156         return 0;
1157 }
1158
1159 static int __bluetooth_terminate(void *data)
1160 {
1161         struct bt_popup_appdata *ad = data;
1162
1163         __bluetooth_ime_hide();
1164
1165         if (ad->conn) {
1166                 dbus_g_connection_unref(ad->conn);
1167                 ad->conn = NULL;
1168         }
1169
1170         if (ad->event_handle)
1171                 ecore_event_handler_del(ad->event_handle);
1172
1173         if (ad->popup)
1174                 evas_object_del(ad->popup);
1175
1176         if (ad->win_main)
1177                 evas_object_del(ad->win_main);
1178
1179         ad->popup = NULL;
1180         ad->win_main = NULL;
1181
1182         return 0;
1183 }
1184
1185 static int __bluetooth_pause(void *data)
1186 {
1187
1188         return 0;
1189 }
1190
1191 static int __bluetooth_resume(void *data)
1192 {
1193
1194         return 0;
1195 }
1196
1197 static int __bluetooth_reset(bundle *b, void *data)
1198 {
1199         struct bt_popup_appdata *ad = data;
1200         const char *event_type = NULL;
1201         int ret = 0;
1202
1203         BT_DBG("__bluetooth_reset()\n");
1204
1205         if (ad == NULL) {
1206                 BT_ERR("App data is NULL\n");
1207                 return -1;
1208         }
1209
1210         /* Start Main UI */
1211         event_type = bundle_get_val(b, "event-type");
1212
1213         if (event_type != NULL) {
1214                 if (!strcasecmp(event_type, "terminate")) {
1215                         __bluetooth_win_del(ad);
1216                         return 0;
1217                 }
1218
1219                 if (syspopup_has_popup(b)) {
1220                         /* Destroy the existing popup*/
1221                         __bluetooth_cleanup(ad);
1222                         /* create window */
1223                         ad->win_main = __bluetooth_create_win(PACKAGE);
1224                         if (ad->win_main == NULL)
1225                                 return -1;
1226                 }
1227
1228                 __bluetooth_parse_event(ad, event_type);
1229
1230                 elm_win_alpha_set(ad->win_main, EINA_TRUE);
1231
1232                 ret = syspopup_create(b, &handler, ad->win_main, ad);
1233                 if (ret == -1) {
1234                         BT_ERR("syspopup_create err");
1235                         __bluetooth_remove_all_event(ad);
1236                 } else {
1237                         ret = __bluetooth_launch_handler(ad,
1238                                                        b, event_type);
1239
1240                         if (ret != 0)
1241                                 __bluetooth_remove_all_event(ad);
1242
1243                         __bluetooth_notify_event();
1244
1245                         /* Change LCD brightness */
1246                         ret = pm_change_state(LCD_NORMAL);
1247                         if (ret != 0)
1248                                 BT_ERR("Fail to change LCD");
1249                 }
1250         } else {
1251                 BT_ERR("event type is NULL \n");
1252         }
1253
1254         return 0;
1255 }
1256
1257 EXPORT int main(int argc, char *argv[])
1258 {
1259         struct bt_popup_appdata ad;
1260         struct appcore_ops ops = {
1261                 .create = __bluetooth_create,
1262                 .terminate = __bluetooth_terminate,
1263                 .pause = __bluetooth_pause,
1264                 .resume = __bluetooth_resume,
1265                 .reset = __bluetooth_reset,
1266         };
1267
1268         memset(&ad, 0x0, sizeof(struct bt_popup_appdata));
1269         ops.data = &ad;
1270
1271         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1272 }
1273