Tizen 2.1 base
[apps/native/ug-bluetooth-efl.git] / src / ui / bt-main-view.c
1 /*
2  * Copyright (c) 2012-2013 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://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 <Ecore.h>
18 #include <eina_list.h>
19 #include <aul.h>
20 #include <bluetooth.h>
21 #include <syspopup_caller.h>
22 #include <dbus/dbus.h>
23 #include <vconf.h>
24
25 #include "bt-main-ug.h"
26 #include "bt-string-define.h"
27 #include "bt-main-view.h"
28 #include "bt-profile-view.h"
29 #include "bt-ipc-handler.h"
30 #include "bt-debug.h"
31 #include "bt-util.h"
32 #include "bt-callback.h"
33 #include "bt-widget.h"
34 #include "bt-resource.h"
35 #include "bt-dbus-method.h"
36 #include "bt-net-connection.h"
37
38 /**********************************************************************
39 *                                      Static Functions declaration
40 ***********************************************************************/
41
42 static void __bt_main_onoff_btn_cb(void *data, Evas_Object *obj,
43                                    void *event_info);
44
45 static void __bt_main_set_controlbar_mode(void *data, int enable_mode);
46
47 static service_h __bt_main_get_visibility_result(bt_ug_data *ugd,
48                                                 gboolean result);
49
50 static service_h __bt_main_get_pick_result(bt_ug_data *ugd,
51                                                 gboolean result);
52
53 /**********************************************************************
54 *                                               Static Functions
55 ***********************************************************************/
56
57 static char *__bt_main_status_label_get(void *data, Evas_Object *obj,
58                                         const char *part)
59 {
60         FN_START;
61
62         bt_ug_data *ugd = NULL;
63         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0, };
64         char *dev_name = NULL;
65         char *ptr = NULL;
66
67         retv_if(data == NULL, NULL);
68
69         ugd = (bt_ug_data *)data;
70
71         if (!strcmp(part, "elm.text.1")) {
72                 switch (ugd->op_status) {
73                 case BT_ACTIVATING:
74                         g_strlcpy(buf, BT_STR_ACTIVATING_ING,
75                                 BT_GLOBALIZATION_STR_LENGTH);
76                         break;
77
78                 case BT_DEACTIVATING:
79                         g_strlcpy(buf, BT_STR_DEACTIVATING_ING,
80                                 BT_GLOBALIZATION_STR_LENGTH);
81                         break;
82
83                 default:
84                         g_strlcpy(buf, BT_STR_BLUETOOTH,
85                                 BT_GLOBALIZATION_STR_LENGTH);
86                         break;
87                 }
88         } else if (strcmp(part, "elm.text.2") == 0) {
89                 memset(ugd->phone_name, 0x00, BT_DEVICE_NAME_LENGTH_MAX + 1);
90
91                 if (bt_adapter_get_name(&dev_name) ==
92                                         BT_ERROR_NONE) {
93                         g_strlcpy(ugd->phone_name, dev_name,
94                                 BT_DEVICE_NAME_LENGTH_MAX);
95                         g_free(dev_name);
96                 } else {
97                         _bt_util_get_phone_name(ugd->phone_name,
98                                         sizeof(ugd->phone_name)-1);
99                 }
100
101                 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
102                 if (!g_utf8_validate(ugd->phone_name, -1, (const char **)&ptr))
103                         *ptr = '\0';
104
105                 g_strlcpy(buf, ugd->phone_name, BT_GLOBALIZATION_STR_LENGTH);
106         } else {
107
108                 BT_DBG("empty text for label. \n");
109                 return NULL;
110         }
111
112         FN_END;
113
114         return strdup(buf);
115 }
116
117 static Evas_Object *__bt_main_status_icon_get(void *data, Evas_Object *obj,
118                                               const char *part)
119 {
120         FN_START;
121
122         bt_ug_data *ugd = NULL;
123         Evas_Object *btn = NULL;
124         bool activated = FALSE;
125
126         retv_if(data == NULL, NULL);
127
128         ugd = (bt_ug_data *)data;
129
130         if (ugd->op_status == BT_ACTIVATING ||
131              ugd->op_status == BT_DEACTIVATING ||
132               ugd->op_status == BT_SEARCHING) {
133                 __bt_main_set_controlbar_mode(ugd, BT_CONTROL_BAR_DISABLE);
134
135                 if (ugd->op_status != BT_SEARCHING)
136                         return NULL;
137         }
138
139         if (!strcmp(part, "elm.icon")) {
140                 activated = (ugd->op_status == BT_DEACTIVATED) ? FALSE : TRUE;
141
142                 if (activated == TRUE)
143                         __bt_main_set_controlbar_mode(ugd,
144                                                       BT_CONTROL_BAR_ENABLE);
145                 else
146                         __bt_main_set_controlbar_mode(ugd,
147                                                       BT_CONTROL_BAR_DISABLE);
148
149                 btn = elm_check_add(obj);
150                 elm_object_style_set(btn, "on&off");
151                 evas_object_show(btn);
152                 evas_object_pass_events_set(btn, EINA_TRUE);
153                 evas_object_propagate_events_set(btn, EINA_FALSE);
154                 elm_check_state_set(btn, activated);    /* set on or off */
155
156                 /* add smart callback */
157                 evas_object_smart_callback_add(btn, "changed",
158                                                __bt_main_onoff_btn_cb, ugd);
159                 ugd->onoff_btn = btn;
160         }
161
162         FN_END;
163         return btn;
164 }
165
166 static char *__bt_main_visible_label_get(void *data, Evas_Object *obj,
167                                       const char *part)
168 {
169         FN_START;
170
171         char buf[BT_GLOBALIZATION_STR_LENGTH+BT_EXTRA_STR_LEN] = { 0 };
172         char remain_time[BT_EXTRA_STR_LEN] = { 0 };
173         bt_ug_data *ugd = NULL;
174
175         if (data == NULL)
176                 return NULL;
177
178         ugd = (bt_ug_data *)data;
179
180         if (!strcmp(part, "elm.text.1")) {
181                 g_strlcpy(buf, BT_STR_VISIBLE, BT_GLOBALIZATION_STR_LENGTH);
182         } else if (strcmp(part, "elm.text.2") == 0) {
183                 if (ugd->visibility_timeout <= 0) {
184                         _bt_util_get_timeout_string(ugd->visibility_timeout,
185                                                 buf, sizeof(buf));
186                 } else {
187                         /* Display remain timeout */
188                         _bt_util_convert_time_to_string(ugd->remain_time,
189                                                         remain_time,
190                                                         sizeof(remain_time));
191
192                         snprintf(buf, sizeof(buf), BT_STR_PS_REMAINING, remain_time);
193                 }
194         } else {
195                 BT_DBG("empty text for label. \n");
196                 return NULL;
197         }
198
199         FN_END;
200         return strdup(buf);
201 }
202
203 static char *__bt_main_timeout_value_label_get(void *data, Evas_Object *obj,
204                                       const char *part)
205 {
206         FN_START;
207
208         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0 };
209         int timeout = 0;
210         bt_ug_data *ugd = NULL;
211         bt_radio_item *item = NULL;
212
213         retv_if(data == NULL, NULL);
214
215         item = (bt_radio_item *)data;
216         retv_if(item->ugd == NULL, NULL);
217
218         ugd = (bt_ug_data *)item->ugd;
219
220         if (!strcmp(part, "elm.text")) {
221                 timeout = _bt_util_get_timeout_value(item->index);
222                 _bt_util_get_timeout_string(timeout, buf, sizeof(buf));
223         } else {
224                 BT_DBG("empty text for label. \n");
225                 return NULL;
226         }
227
228         FN_END;
229         return strdup(buf);
230 }
231
232 static Evas_Object *__bt_main_timeout_value_icon_get(void *data, Evas_Object *obj,
233                                       const char *part)
234 {
235         FN_START;
236
237         bt_ug_data *ugd = NULL;
238         bt_radio_item *item = NULL;
239         Evas_Object *btn = NULL;
240
241         retv_if(data == NULL, NULL);
242
243         item = (bt_radio_item *)data;
244         retv_if(item->ugd == NULL, NULL);
245
246         ugd = (bt_ug_data *)item->ugd;
247
248         if (!strcmp(part, "elm.icon")) {
249                 btn = elm_radio_add(obj);
250                 elm_radio_state_value_set(btn, item->index);
251                 elm_radio_group_add(btn, ugd->radio_main);
252                 elm_radio_value_set(btn, ugd->selected_radio);
253
254                 evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND,
255                                                 EVAS_HINT_EXPAND);
256
257                 evas_object_size_hint_align_set(btn, EVAS_HINT_FILL,
258                                                 EVAS_HINT_FILL);
259         }
260
261         FN_END;
262         return btn;
263 }
264
265 static gboolean __bt_main_visible_timeout_cb(gpointer user_data)
266 {
267         FN_START;
268
269         bt_ug_data *ugd = NULL;
270
271         ugd = (bt_ug_data *)user_data;
272
273         ugd->remain_time--;
274
275         if (ugd->remain_time <= 0) {
276                 g_source_remove(ugd->timeout_id);
277                 ugd->timeout_id = 0;
278                 ugd->visibility_timeout = 0;
279                 ugd->remain_time = 0;
280                 ugd->selected_radio = 0;
281
282                 elm_genlist_item_update(ugd->visible_item);
283
284                 return FALSE;
285         }
286
287         elm_genlist_item_update(ugd->visible_item);
288
289         FN_END;
290         return TRUE;
291 }
292
293 static int __bt_idle_destroy_ug(void *data)
294 {
295         FN_START;
296
297         bt_ug_data *ugd = data;
298         service_h service = NULL;
299
300         retv_if(ugd == NULL, BT_UG_FAIL);
301
302
303         if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY)
304                 service = __bt_main_get_visibility_result(ugd, TRUE);
305         else if (ugd->bt_launch_mode == BT_LAUNCH_PICK)
306                 service = __bt_main_get_pick_result(ugd, TRUE);
307
308         _bt_ug_destroy(data, (void *)service);
309
310         if (service)
311                 service_destroy(service);
312
313         FN_END;
314         return BT_UG_ERROR_NONE;
315 }
316
317 static void __bt_main_timeout_value_item_sel(void *data, Evas_Object *obj,
318                                     void *event_info)
319 {
320         FN_START;
321
322         bt_ug_data *ugd = NULL;
323         bt_radio_item *item = NULL;
324         int ret;
325         int timeout;
326
327         ret_if(data == NULL);
328
329         item = (bt_radio_item *)data;
330
331         ugd = (bt_ug_data *)item->ugd;
332
333         elm_genlist_item_selected_set((Elm_Object_Item *)event_info,
334                                       EINA_FALSE);
335
336         timeout = _bt_util_get_timeout_value(item->index);
337
338         if (timeout < 0) {
339                 ret = bt_adapter_set_visibility(
340                         BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE,
341                         0);
342
343                 if (ugd->timeout_id) {
344                         g_source_remove(ugd->timeout_id);
345                         ugd->timeout_id = 0;
346                 }
347         } else if (timeout == 0) {
348                 ret = bt_adapter_set_visibility(
349                         BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE,
350                         0);
351                 if (ugd->timeout_id) {
352                         g_source_remove(ugd->timeout_id);
353                         ugd->timeout_id = 0;
354                 }
355         } else {
356                 ret = bt_adapter_set_visibility(
357                         BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE,
358                         timeout);
359
360                 if (ret == BT_ERROR_NONE) {
361                         if (ugd->timeout_id) {
362                                 g_source_remove(ugd->timeout_id);
363                                 ugd->timeout_id = 0;
364                         }
365
366                         ugd->remain_time = timeout;
367
368                         ugd->timeout_id = g_timeout_add_seconds(1,
369                                         __bt_main_visible_timeout_cb, ugd);
370                 }
371         }
372
373         if (ret != BT_ERROR_NONE) {
374                 BT_DBG("bt_adapter_set_visibility() failed");
375                 return;
376         }
377
378         ugd->selected_radio = item->index;
379         ugd->visibility_timeout = timeout;
380
381         elm_genlist_item_update(ugd->visible_item);
382         elm_radio_value_set(ugd->radio_main, ugd->selected_radio);
383
384         if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY)
385                 g_idle_add((GSourceFunc)__bt_idle_destroy_ug, ugd);
386
387         FN_END;
388         return;
389 }
390
391 static void __bt_main_timeout_value_del(void *data, Evas_Object *obj)
392 {
393         FN_START;
394
395         bt_radio_item *item = (bt_radio_item *)data;
396         if (item)
397                 free(item);
398
399         FN_END;
400 }
401
402 static gboolean __bt_main_is_connectable_device(bt_dev_t *dev)
403 {
404         FN_START;
405
406         bt_ug_data *ugd = NULL;
407
408         retv_if(dev == NULL, FALSE);
409         retv_if(dev->ugd == NULL, FALSE);
410
411         ugd = (bt_ug_data *)dev->ugd;
412
413         if(ugd->bt_launch_mode != BT_LAUNCH_NORMAL) {
414                 /* In no normal mode,
415                     connectable devices is only shown in the list */
416                 BT_DBG("Not in the normal mode");
417                 return TRUE;
418         }
419
420         if (dev->service_list == 0) {
421                 BT_DBG("No service list");
422                 return FALSE;
423         }
424
425         if ((dev->service_list & BT_SC_HFP_SERVICE_MASK) ||
426              (dev->service_list & BT_SC_HSP_SERVICE_MASK) ||
427               (dev->service_list & BT_SC_A2DP_SERVICE_MASK) ||
428                (dev->service_list & BT_SC_HID_SERVICE_MASK) ||
429                 (dev->service_list & BT_SC_NAP_SERVICE_MASK)) {
430                 /* Connectable device */
431                 return TRUE;
432         }
433
434         FN_END;
435         return FALSE;
436 }
437
438 static char *__bt_main_list_label_get(void *data, Evas_Object *obj,
439                                       const char *part)
440 {
441         FN_START;
442
443         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0 };
444         bt_dev_t *dev = NULL;
445
446         retv_if(data == NULL, NULL);
447
448         dev = (bt_dev_t *)data;
449
450         if (!strcmp(part, "elm.text.1") || !strcmp(part, "elm.text")) {
451                 g_strlcpy(buf, dev->name, BT_GLOBALIZATION_STR_LENGTH);
452                 BT_DBG("label : %s", buf);
453         } else if (strcmp(part, "elm.text.2") == 0) {
454                 if (dev->status == BT_IDLE) {
455                         if (__bt_main_is_connectable_device(dev) == 0) {
456                                 snprintf(buf, BT_GLOBALIZATION_STR_LENGTH,
457                                                 BT_SET_FONT_SIZE,
458                                                 BT_GENLIST_FONT_32_INC,
459                                                 BT_STR_PAIRED);
460                         } else if (dev->connected_mask > 0) {
461                                 snprintf(buf, BT_GLOBALIZATION_STR_LENGTH,
462                                                 BT_SET_FONT_SIZE_COLOR,
463                                                 BT_GENLIST_FONT_32_INC,
464                                                 BT_GENLIST_SUBTEXT_COLOR,
465                                                 BT_STR_TAP_TO_DISCONNECT);
466                         } else {
467                                 snprintf(buf, BT_GLOBALIZATION_STR_LENGTH,
468                                                 BT_SET_FONT_SIZE,
469                                                 BT_GENLIST_FONT_32_INC,
470                                                 BT_STR_TAP_TO_CONNECT);
471                         }
472                 } else if (dev->status == BT_CONNECTING) {
473                         snprintf(buf, BT_GLOBALIZATION_STR_LENGTH,
474                                         BT_SET_FONT_SIZE,
475                                         BT_GENLIST_FONT_32_INC,
476                                         BT_STR_CONNECTING);
477                 } else if (dev->status == BT_SERVICE_SEARCHING) {
478                         snprintf(buf, BT_GLOBALIZATION_STR_LENGTH,
479                                         BT_SET_FONT_SIZE,
480                                         BT_GENLIST_FONT_32_INC,
481                                         BT_STR_SEARCHING_SERVICES);
482                 } else if (dev->status == BT_DISCONNECTING) {
483                         snprintf(buf, BT_GLOBALIZATION_STR_LENGTH,
484                                         BT_SET_FONT_SIZE_COLOR,
485                                         BT_GENLIST_FONT_32_INC,
486                                         BT_GENLIST_SUBTEXT_COLOR,
487                                         BT_STR_DISCONNECTING);
488                 }
489         } else {                /* for empty item */
490
491                 BT_DBG("empty text for label. \n");
492                 return NULL;
493         }
494
495         FN_END;
496         return strdup(buf);
497 }
498
499 static void __bt_main_list_select_cb(void *data, Evas_Object *obj, void *event_info)
500 {
501         FN_START;
502         int ret;
503         bt_dev_t *dev = NULL;
504         bt_ug_data *ugd = NULL;
505
506         ret_if(data == NULL);
507
508         dev = (bt_dev_t *)data;
509         ret_if(dev->ugd == NULL);
510
511         ugd = dev->ugd;
512
513         if (ugd->op_status == BT_SEARCHING) {
514                 ret = bt_adapter_stop_device_discovery();
515                 if(ret != BT_ERROR_NONE)
516                         BT_DBG("Fail to stop discovery: %d", ret);
517         }
518
519         if (ugd->connect_req == TRUE) {
520                 _bt_main_draw_selection_info(ugd, BT_STR_CONNECTION_FAILED);
521                 return;
522         }
523
524         /* Create the profile view */
525         _bt_profile_create_view(dev);
526
527         FN_END;
528 }
529
530 static Evas_Object *__bt_main_list_icon_get(void *data, Evas_Object *obj,
531                                             const char *part)
532 {
533         FN_START;
534
535         Evas_Object *icon = NULL;
536         char *dev_icon_file = NULL;
537         bt_dev_t *dev = NULL;
538
539         retv_if(data == NULL, NULL);
540
541         dev = (bt_dev_t *)data;
542
543         if (!strcmp(part, "elm.icon.1") || !strcmp(part, "elm.icon")) {
544                 dev_icon_file =
545                     _bt_main_get_device_icon(dev->major_class,
546                                              dev->minor_class,
547                                              dev->connected_mask);
548                 icon = _bt_create_icon(obj, dev_icon_file);
549         } else if (!strcmp(part, "elm.icon.2")) {
550                 BT_DBG("status : %d", dev->status);
551
552                 if (dev->status == BT_IDLE) {
553                         icon = elm_button_add(obj);
554                         elm_object_style_set(icon, "reveal");
555                         evas_object_smart_callback_add(icon, "clicked",
556                                         (Evas_Smart_Cb)__bt_main_list_select_cb, (void *)dev);
557                         evas_object_propagate_events_set(icon, EINA_FALSE);
558                 } else {
559                         icon = _bt_create_progressbar(obj, NULL);
560                 }
561         }
562
563         FN_END;
564         return icon;
565 }
566
567 static char *__bt_main_searched_label_get(void *data, Evas_Object *obj,
568                                       const char *part)
569 {
570         FN_START;
571
572         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0 };
573         bt_dev_t *dev = NULL;
574
575         if (data == NULL)
576                 return NULL;
577
578         dev = (bt_dev_t *)data;
579
580         if (!strcmp(part, "elm.text")) {
581                 g_strlcpy(buf, dev->name, BT_GLOBALIZATION_STR_LENGTH);
582                 BT_DBG("label : %s", buf);
583         }
584
585         FN_END;
586         return strdup(buf);
587 }
588
589
590 static Evas_Object *__bt_main_searched_icon_get(void *data,
591                                         Evas_Object *obj, const char *part)
592 {
593         FN_START;
594
595         Evas_Object *icon = NULL;
596         char *dev_icon_file = NULL;
597         bt_dev_t *dev = NULL;
598
599         retv_if(data == NULL, NULL);
600
601         dev = (bt_dev_t *)data;
602
603         if (!strcmp(part, "elm.icon.1") || !strcmp(part, "elm.icon")) {
604                 dev_icon_file =
605                     _bt_main_get_device_icon(dev->major_class,
606                                              dev->minor_class,
607                                              dev->connected_mask);
608                 icon = _bt_create_icon(obj, dev_icon_file);
609         } else if (!strcmp(part, "elm.icon.2")) {
610                 if (dev->status != BT_IDLE) {
611                         icon = _bt_create_progressbar(obj, NULL);
612                 }
613         }
614
615         FN_END;
616         return icon;
617 }
618
619 static char *__bt_main_no_device_label_get(void *data, Evas_Object *obj,
620                                       const char *part)
621 {
622         FN_START;
623
624         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0 };
625
626         if (!strcmp(part, "elm.text")) {
627                 g_strlcpy(buf, BT_STR_NO_DEVICE_FOUND,
628                                 BT_GLOBALIZATION_STR_LENGTH);
629         }
630
631         FN_END;
632         return strdup(buf);
633 }
634
635 static char *__bt_main_paired_title_label_get(void *data, Evas_Object *obj,
636                                               const char *part)
637 {
638         FN_START;
639
640         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0, };
641
642         if (strcmp(part, "elm.text") == 0) {
643                 /*Label */
644                 strncpy(buf, BT_STR_PAIRED_DEVICES,
645                         BT_GLOBALIZATION_STR_LENGTH);
646         } else {
647                 BT_DBG("This part name is not exist in style");
648                 return NULL;
649         }
650
651         FN_END;
652         return strdup(buf);
653 }
654
655 static char *__bt_main_searched_title_label_get(void *data, Evas_Object *obj,
656                                                 const char *part)
657 {
658         FN_START;
659
660         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0, };
661         bt_ug_data *ugd = NULL;
662
663         retv_if(data == NULL, NULL);
664
665         ugd = (bt_ug_data *)data;
666
667         if (strcmp(part, "elm.text") == 0) {
668                 /* Label */
669                 if (ugd->searched_device == NULL ||
670                      eina_list_count(ugd->searched_device) == 0) {
671                         strncpy(buf, BT_STR_BLUETOOTH_DEVICES,
672                                         BT_GLOBALIZATION_STR_LENGTH);
673                 } else {
674                         strncpy(buf, BT_STR_AVAILABLE_DEVICES,
675                                         BT_GLOBALIZATION_STR_LENGTH);
676                 }
677         } else {
678                 BT_DBG("This part name is not exist in style");
679                 return NULL;
680         }
681
682         FN_END;
683         return strdup(buf);
684 }
685
686 static Evas_Object *__bt_main_title_icon_get(void *data, Evas_Object *obj,
687                                              const char *part)
688 {
689         FN_START;
690
691         bt_ug_data *ugd = NULL;
692         Evas_Object *progressbar = NULL;
693
694         retv_if(data == NULL, NULL);
695         retv_if(obj == NULL, NULL);
696         retv_if(part == NULL, NULL);
697
698         ugd = (bt_ug_data *)data;
699
700         if (!strcmp(part, "elm.icon") && ugd->op_status == BT_SEARCHING) {
701                 progressbar = _bt_create_progressbar(obj, "list_process_small");
702         }
703
704         FN_END;
705         return progressbar;
706 }
707
708 static void __bt_main_status_item_sel(void *data, Evas_Object *obj,
709                                       void *event_info)
710 {
711         FN_START;
712
713         bt_ug_data *ugd = NULL;
714
715         ret_if(data == NULL);
716
717         ugd = (bt_ug_data *)data;
718
719         elm_genlist_item_selected_set((Elm_Object_Item *)event_info,
720                                       EINA_FALSE);
721
722         __bt_main_onoff_btn_cb(data, ugd->onoff_btn, NULL);
723
724         FN_END;
725         return;
726 }
727
728 static void __bt_main_visible_item_sel(void *data, Evas_Object *obj,
729                                     void *event_info)
730 {
731         FN_START;
732
733         bt_ug_data *ugd = NULL;
734         Eina_Bool expanded = EINA_FALSE;
735
736         ret_if(data == NULL);
737         ret_if(event_info == NULL);
738
739         ugd = (bt_ug_data *)data;
740
741         elm_genlist_item_selected_set((Elm_Object_Item *)event_info,
742                                       EINA_FALSE);
743
744         expanded = elm_genlist_item_expanded_get(event_info);
745         elm_genlist_item_expanded_set(event_info, !expanded);
746
747         FN_END;
748         return;
749 }
750
751 static service_h __bt_main_get_visibility_result(bt_ug_data *ugd,
752                                                 gboolean result)
753 {
754         service_h service = NULL;
755         char mode_str[BT_RESULT_STR_MAX] = { 0 };
756         const char *result_str;
757
758         retv_if(ugd == NULL, NULL);
759
760         service_create(&service);
761
762         retv_if(service == NULL, NULL);
763
764         if (result == TRUE)
765                 result_str = BT_RESULT_SUCCESS;
766         else
767                 result_str = BT_RESULT_FAIL;
768
769         if (service_add_extra_data(service, "result",
770                                         result_str) < 0) {
771                 BT_DBG("Fail to add extra data");
772         }
773
774         snprintf(mode_str, BT_RESULT_STR_MAX, "%d", (int)ugd->selected_radio);
775
776         if (service_add_extra_data(service, "visibility",
777                                         (const char *)mode_str) < 0) {
778                 BT_DBG("Fail to add extra data");
779         }
780
781         return service;
782 }
783
784 static service_h __bt_main_get_pick_result(bt_ug_data *ugd,
785                                                 gboolean result)
786 {
787         service_h service = NULL;
788         const char *result_str;
789         char address[BT_ADDRESS_STR_LEN] = { 0 };
790         char value_str[BT_RESULT_STR_MAX] = { 0 };
791         bt_dev_t *dev;
792
793         retv_if(ugd == NULL, NULL);
794         retv_if(ugd->pick_device == NULL, NULL);
795
796         dev = ugd->pick_device;
797
798         service_create(&service);
799
800         retv_if(service == NULL, NULL);
801
802         if (result == TRUE)
803                 result_str = BT_RESULT_SUCCESS;
804         else
805                 result_str = BT_RESULT_FAIL;
806
807         if (service_add_extra_data(service, "result",
808                                         result_str) < 0) {
809                 BT_DBG("Fail to add extra data");
810         }
811
812         _bt_util_addr_type_to_addr_result_string(address, dev->bd_addr);
813
814         if (service_add_extra_data(service, "address",
815                                         (const char *)address) < 0) {
816                 BT_DBG("Fail to add extra data");
817         }
818
819         if (service_add_extra_data(service, "name",
820                                         (const char *)dev->name) < 0) {
821                 BT_DBG("Fail to add extra data");
822         }
823
824         snprintf(value_str, BT_RESULT_STR_MAX, "%d", dev->rssi);
825
826         if (service_add_extra_data(service, "rssi",
827                                         (const char *)value_str) < 0) {
828                 BT_DBG("Fail to add extra data");
829         }
830
831         memset(value_str, 0x00, sizeof(value_str));
832         snprintf(value_str, BT_RESULT_STR_MAX, "%d", dev->is_bonded);
833
834         if (service_add_extra_data(service, "is_bonded",
835                                         (const char *)value_str) < 0) {
836                 BT_DBG("Fail to add extra data");
837         }
838
839         memset(value_str, 0x00, sizeof(value_str));
840         snprintf(value_str, BT_RESULT_STR_MAX, "%d", dev->major_class);
841
842         if (service_add_extra_data(service, "major_class",
843                                         (const char *)value_str) < 0) {
844                 BT_DBG("Fail to add extra data");
845         }
846
847         memset(value_str, 0x00, sizeof(value_str));
848         snprintf(value_str, BT_RESULT_STR_MAX, "%d", dev->minor_class);
849
850         if (service_add_extra_data(service, "minor_class",
851                                         (const char *)value_str) < 0) {
852                 BT_DBG("Fail to add extra data");
853         }
854
855         memset(value_str, 0x00, sizeof(value_str));
856         snprintf(value_str, BT_RESULT_STR_MAX, "%ld",
857                                 (long int)dev->service_class);
858
859         if (service_add_extra_data(service, "service_class",
860                                         (const char *)value_str) < 0) {
861                 BT_DBG("Fail to add extra data");
862         }
863
864         if (service_add_extra_data_array(service, "uuids",
865                                         (const char **)dev->uuids,
866                                         dev->uuid_count) < 0) {
867                 BT_DBG("Fail to add extra data");
868         }
869
870         return service;
871 }
872
873 static void __bt_main_quit_btn_cb(void *data, Evas_Object *obj,
874                                   void *event_info)
875 {
876         FN_START;
877
878         service_h service = NULL;
879         bt_ug_data *ugd = (bt_ug_data *)data;
880
881         ret_if(ugd == NULL);
882
883         if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY) {
884                 service = __bt_main_get_visibility_result(ugd, FALSE);
885
886                 _bt_ug_destroy(data, (void *)service);
887
888                 if (service)
889                         service_destroy(service);
890         } else if (ugd->bt_launch_mode == BT_LAUNCH_PICK) {
891                 service_create(&service);
892
893                 if (service == NULL) {
894                         _bt_ug_destroy(data, NULL);
895                         return;
896                 }
897
898                 if (service_add_extra_data(service, "result",
899                                                 BT_RESULT_FAIL) < 0) {
900                         BT_DBG("Fail to add extra data");
901                 }
902
903                 _bt_ug_destroy(data, (void *)service);
904
905                 service_destroy(service);
906         } else {
907                 _bt_ug_destroy(data, NULL);
908         }
909
910         FN_END;
911 }
912
913 static int __bt_main_enable_bt(void *data)
914 {
915         FN_START;
916         int ret;
917         retv_if(data == NULL, -1);
918         bt_ug_data *ugd = (bt_ug_data *)data;
919
920         if (_bt_util_is_battery_low() == TRUE) {
921                 /* Battery is critical low */
922                 _bt_main_create_information_popup(ugd,BT_STR_LOW_BATTERY);
923                 elm_genlist_item_update(ugd->status_item);
924                 return -1;
925         }
926
927         ret = bt_adapter_enable();
928         if (ret != BT_ERROR_NONE) {
929                 BT_ERR("Failed to enable bluetooth [%d]", ret);
930         } else {
931                 ugd->op_status = BT_ACTIVATING;
932                 elm_object_disabled_set(ugd->scan_btn, EINA_TRUE);
933         }
934
935         FN_END;
936         return 0;
937 }
938
939 static void __bt_main_onoff_btn_cb(void *data, Evas_Object *obj,
940                                    void *event_info)
941 {
942         FN_START;
943         ret_if(data == NULL);
944
945         int ret;
946         bt_ug_data *ugd = (bt_ug_data *)data;
947
948         if (ugd->op_status == BT_DEACTIVATED) {
949                 ret = __bt_main_enable_bt(data);
950         } else {
951                 ret = bt_adapter_disable();
952                 if (ret != BT_ERROR_NONE) {
953                         BT_ERR("Failed to disable bluetooth [%d]", ret);
954                 } else {
955                         ugd->op_status = BT_DEACTIVATING;
956                         elm_object_disabled_set(ugd->scan_btn, EINA_TRUE);
957                 }
958         }
959
960         elm_genlist_item_update(ugd->status_item);
961
962         FN_END;
963 }
964
965 static void __bt_main_controlbar_btn_cb(void *data, Evas_Object *obj,
966                                               void *event_info)
967 {
968         FN_START;
969
970         bt_ug_data *ugd = NULL;
971
972         retm_if(data == NULL, "Invalid argument: bt_ug_data is NULL\n");
973
974         ugd = (bt_ug_data *)data;
975
976         if (ugd->op_status == BT_SEARCHING) {
977                 if (bt_adapter_stop_device_discovery() == BT_ERROR_NONE) {
978                         elm_object_text_set(ugd->scan_btn, BT_STR_STOP);
979                 }
980         } else {
981                 _bt_main_scan_device(ugd);
982         }
983
984         FN_END;
985 }
986
987 static void __bt_main_disconnect_cb(void *data, Evas_Object *obj,
988                                     void *event_info)
989 {
990         FN_START;
991
992         bt_dev_t *dev = NULL;
993         bt_ug_data *ugd = NULL;
994
995         retm_if(data == NULL, "Invalid argument: data is NULL\n");
996
997         dev = (bt_dev_t *)data;
998         retm_if(dev->ugd == NULL, "ugd is NULL\n");
999
1000         ugd = dev->ugd;
1001
1002         if (ugd->popup) {
1003                 evas_object_del(ugd->popup);
1004                 ugd->popup = NULL;
1005         }
1006
1007         _bt_main_disconnect_device(ugd, dev);
1008
1009         FN_END;
1010 }
1011
1012 static void __bt_main_popup_menu_cb(void *data, Evas_Object *obj,
1013                                     void *event_info)
1014 {
1015         FN_START;
1016
1017         bt_ug_data *ugd = NULL;
1018
1019         retm_if(data == NULL, "Invalid argument: data is NULL\n");
1020
1021         ugd = (bt_ug_data *)data;
1022
1023         if (ugd->popup_menu)
1024                 evas_object_del(ugd->popup_menu);
1025
1026         ugd->popup_menu = NULL;
1027
1028         if (ugd->popup == NULL)
1029                 ugd->back_cb = NULL;
1030
1031         FN_END;
1032 }
1033
1034 static char *__bt_main_popup_menu_label_get(void *data, Evas_Object *obj,
1035                                             const char *part)
1036 {
1037         FN_START;
1038
1039         bt_dev_t *dev = NULL;
1040         char buf[BT_MAX_MENU_NAME_LEN] = { 0, };
1041         int index = 0;
1042
1043         retvm_if(data == NULL, NULL, "Invalid argument: data is NULL\n");
1044
1045         index = (int)data;
1046         dev = (bt_dev_t *)evas_object_data_get(obj, "dev_info");
1047
1048         retvm_if(dev == NULL, NULL, "dev is NULL\n");
1049
1050         if (strcmp(part, "elm.text") != 0) {
1051                 BT_DBG("It is not in elm.text part\n");
1052                 return NULL;
1053         }
1054
1055         switch (index) {
1056         case BT_CONNECT_MENU:
1057                 snprintf(buf, sizeof(buf), BT_STR_DISCONNECT);
1058                 break;
1059         default:
1060                 snprintf(buf, sizeof(buf), " ");
1061                 break;
1062         }
1063
1064         BT_DBG("popup_menu_label_get() end. %s", buf);
1065
1066         FN_END;
1067         return strdup(buf);
1068 }
1069
1070 /* change popup style for popup menu */
1071 static void __bt_main_select_menu_cb(void *data, Evas_Object *obj,
1072                                      void *event_info)
1073 {
1074         FN_START;
1075
1076         Elm_Object_Item *selected_menu = NULL;
1077         bt_dev_t *dev = NULL;
1078         bt_ug_data *ugd = NULL;
1079
1080         retm_if(data == NULL, "Invalid argument: data is NULL\n");
1081         retm_if(event_info == NULL, "Invalid argument: event_info is NULL\n");
1082
1083         ugd = (bt_ug_data *)data;
1084
1085         selected_menu = (Elm_Object_Item *)event_info;
1086
1087         elm_genlist_item_selected_set(selected_menu, EINA_FALSE);
1088
1089         dev = _bt_main_get_dev_info(ugd->paired_device, ugd->paired_item);
1090         retm_if(dev == NULL, "dev is NULL\n");
1091
1092         ugd->back_cb = NULL;
1093
1094         if (_bt_is_profile_connected(BT_HEADSET_CONNECTED, ugd->conn,
1095                                                 dev->bd_addr) == TRUE) {
1096                 BT_DBG("Disconnecting AG service \n");
1097                 if (bt_audio_disconnect(dev->addr_str,
1098                                 BT_AUDIO_PROFILE_TYPE_ALL) == BT_ERROR_NONE) {
1099                         ugd->connect_req = TRUE;
1100                         dev->status = BT_DISCONNECTING;
1101                 } else {
1102                         BT_DBG("Fail to connect Headset device");
1103                 }
1104         } else if (_bt_is_profile_connected(BT_STEREO_HEADSET_CONNECTED, ugd->conn,
1105                                                 dev->bd_addr) == TRUE) {
1106                 BT_DBG("Disconnecting AV service \n");
1107                 if (bt_audio_disconnect(dev->addr_str,
1108                                 BT_AUDIO_PROFILE_TYPE_A2DP) == BT_ERROR_NONE) {
1109                         ugd->connect_req = TRUE;
1110                         dev->status = BT_DISCONNECTING;
1111                 } else {
1112                         BT_DBG("Fail to connect Headset device");
1113                 }
1114         } else if (_bt_is_profile_connected(BT_HID_CONNECTED, ugd->conn,
1115                                                 dev->bd_addr) == TRUE) {
1116                 BT_DBG("Disconnecting HID service!!\n");
1117
1118                 if (bt_hid_host_disconnect(dev->addr_str) == BT_ERROR_NONE) {
1119                         dev->status = BT_DISCONNECTING;
1120                         elm_genlist_item_update((Elm_Object_Item *)dev->genlist_item);
1121                 } else {
1122                         BT_DBG("Fail to connect HID device");
1123                 }
1124                 return;
1125         }
1126
1127         elm_genlist_item_update((Elm_Object_Item *)dev->genlist_item);
1128         FN_END;
1129 }
1130
1131 static void __bt_main_set_controlbar_mode(void *data, int enable_mode)
1132 {
1133         FN_START;
1134
1135         bt_ug_data *ugd = NULL;
1136         bool mode;
1137         int count = 0;
1138
1139         ret_if(data == NULL);
1140
1141         ugd = (bt_ug_data *)data;
1142
1143         mode = enable_mode ? FALSE : TRUE;
1144
1145         if (ugd->paired_device)
1146                 count = eina_list_count(ugd->paired_device);
1147
1148         if (count == 0 || (ugd->op_status != BT_ACTIVATED)) {
1149                 BT_DBG("device count: %d, op_status: %d",
1150                        eina_list_count(ugd->paired_device), ugd->op_status);
1151                 mode = TRUE;
1152         }
1153
1154         FN_END;
1155 }
1156
1157 static int __bt_main_request_to_send(bt_ug_data *ugd, bt_dev_t *dev)
1158 {
1159         obex_ipc_param_t param;
1160         char *value = NULL;
1161
1162         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
1163                dev->bd_addr[0], dev->bd_addr[1],
1164                dev->bd_addr[2], dev->bd_addr[3],
1165                dev->bd_addr[4], dev->bd_addr[5]);
1166
1167         memset(&param, 0x00, sizeof(obex_ipc_param_t));
1168         memcpy(param.param2, dev->bd_addr, BT_ADDRESS_LENGTH_MAX);
1169
1170         if (service_get_extra_data(ugd->service, "filecount", &value) < 0)
1171                 BT_DBG("Get data error");
1172
1173         if (value == NULL)
1174                 goto fail;
1175
1176         param.param3 = atoi(value);
1177         free(value);
1178         value = NULL;
1179
1180         if (service_get_extra_data(ugd->service, "files", &value) < 0)
1181                 BT_DBG("Get data error");
1182
1183         if (value == NULL)
1184                 goto fail;
1185
1186         param.param4 = g_strdup(value);
1187         param.param5 = g_strdup("normal");
1188         param.param6 = g_strdup(dev->name);
1189         free(value);
1190
1191         if (_bt_ipc_send_obex_message(&param, ugd) != BT_UG_ERROR_NONE)
1192                 goto fail;
1193
1194         g_free(param.param4);
1195         g_free(param.param5);
1196         g_free(param.param6);
1197         return BT_UG_ERROR_NONE;
1198 fail:
1199         _bt_main_launch_syspopup(ugd, BT_SYSPOPUP_REQUEST_NAME,
1200                         BT_STR_UNABLE_TO_SEND,
1201                         BT_SYSPOPUP_ONE_BUTTON_TYPE);
1202
1203         g_free(param.param4);
1204         g_free(param.param5);
1205         g_free(param.param6);
1206
1207         return BT_UG_FAIL;
1208 }
1209
1210 static void __bt_main_paired_item_sel_cb(void *data, Evas_Object *obj,
1211                                          void *event_info)
1212 {
1213         FN_START;
1214
1215         bt_ug_data *ugd = NULL;
1216         bt_dev_t *dev = NULL;
1217         Elm_Object_Item *item = NULL;
1218         Evas_Object *btn = NULL;
1219         Evas_Object *popup = NULL;
1220         Evas_Object *popup_btn = NULL;
1221         char msg[BT_DISCONNECT_TEXT_LENGTH] = { 0 };
1222         int ret;
1223
1224         elm_genlist_item_selected_set((Elm_Object_Item *)event_info,
1225                                       EINA_FALSE);
1226
1227         retm_if(data == NULL, "Invalid argument: bt_ug_data is NULL\n");
1228
1229         ugd = (bt_ug_data *)data;
1230         item = (Elm_Object_Item *)event_info;
1231
1232         ret_if(ugd->waiting_service_response == TRUE);
1233         ret_if(ugd->op_status == BT_PAIRING);
1234
1235         if (ugd->op_status == BT_SEARCHING) {
1236                 ret = bt_adapter_stop_device_discovery();
1237                 if(ret != BT_ERROR_NONE)
1238                         BT_DBG("Fail to stop discovery");
1239                 return;
1240         }
1241
1242         dev = _bt_main_get_dev_info(ugd->paired_device, item);
1243         retm_if(dev == NULL, "Invalid argument: device info is NULL\n");
1244         retm_if(dev->status != BT_IDLE,
1245                         "Connecting / Disconnecting is in progress");
1246
1247         if ((ugd->auto_service_search || ugd->waiting_service_response) &&
1248                   (dev->service_list == 0)) {
1249                 ugd->paired_item = item;
1250
1251                 if (ugd->popup) {
1252                         evas_object_del(ugd->popup);
1253                         ugd->popup = NULL;
1254                 }
1255
1256                 ugd->popup =
1257                     _bt_create_popup(ugd->win_main, BT_STR_INFORMATION,
1258                                      BT_STR_GETTING_SERVICE_LIST,
1259                                      _bt_main_popup_del_cb, ugd, 2);
1260
1261                 btn = elm_button_add(ugd->popup);
1262                 elm_object_text_set(btn, BT_STR_OK);
1263                 elm_object_part_content_set(ugd->popup, "button1", btn);
1264                 evas_object_smart_callback_add(btn, "clicked",
1265                         (Evas_Smart_Cb)_bt_main_popup_del_cb, ugd);
1266
1267                 return;
1268         }
1269
1270         if (ugd->bt_launch_mode == BT_LAUNCH_NORMAL ||
1271              ugd->bt_launch_mode == BT_LAUNCH_USE_NFC) {
1272
1273                 ugd->paired_item = item;
1274
1275                 if (dev->service_list == 0 &&
1276                      ugd->auto_service_search == FALSE) {
1277
1278                         if(bt_device_start_service_search(
1279                                 (const char *)dev->addr_str) == BT_ERROR_NONE) {
1280
1281                                 dev->status = BT_SERVICE_SEARCHING;
1282                                 ugd->waiting_service_response = TRUE;
1283                                 ugd->request_timer =
1284                                     ecore_timer_add(BT_SEARCH_SERVICE_TIMEOUT,
1285                                             (Ecore_Task_Cb)
1286                                             _bt_main_service_request_cb,
1287                                             ugd);
1288
1289                                 elm_genlist_item_update(ugd->paired_item);
1290                                 return;
1291                         } else {
1292                                 BT_DBG("service search error");
1293                                 return;
1294                         }
1295                 }
1296
1297                 if (dev->connected_mask == 0) {
1298                         /* Not connected case */
1299                         _bt_main_connect_device(ugd, dev);
1300                 } else {
1301                         /* connected case */
1302                         snprintf(msg, sizeof(msg), "%s %s<br>%s", BT_STR_END_CONNECTION,
1303                                                         dev->name,
1304                                                         BT_STR_DISCONNECT_Q);
1305
1306                         if (ugd->popup) {
1307                                 evas_object_del(ugd->popup);
1308                                 ugd->popup = NULL;
1309                         }
1310
1311                         popup = _bt_create_popup(ugd->win_main, BT_STR_INFORMATION,
1312                                         msg,
1313                                         _bt_main_popup_del_cb, ugd, 0);
1314
1315                         if (popup == NULL)
1316                                 return;
1317
1318                         ugd->popup = popup;
1319
1320                         popup_btn = elm_button_add(popup);
1321                         elm_object_text_set(popup_btn, BT_STR_OK);
1322                         elm_object_part_content_set(popup, "button1", popup_btn);
1323                         evas_object_smart_callback_add(popup_btn, "clicked",
1324                                                 __bt_main_disconnect_cb, dev);
1325
1326                         popup_btn = elm_button_add(popup);
1327                         elm_object_text_set(popup_btn, BT_STR_CANCEL);
1328                         elm_object_part_content_set(popup, "button2", popup_btn);
1329                         evas_object_smart_callback_add(popup_btn, "clicked",
1330                                                 _bt_main_popup_del_cb, ugd);
1331                 }
1332         } else if (ugd->bt_launch_mode == BT_LAUNCH_SEND_FILE) {
1333                 if (_bt_util_is_battery_low() == TRUE) {
1334                         /* Battery is critical low */
1335                         _bt_main_create_information_popup(ugd, BT_STR_LOW_BATTERY);
1336                         return;
1337                 }
1338
1339                 if (__bt_main_request_to_send(ugd, dev) == BT_UG_ERROR_NONE)
1340                         BT_ERR("Fail to send");
1341
1342                 _bt_ug_destroy(ugd, NULL);
1343         } else if (ugd->bt_launch_mode == BT_LAUNCH_PICK) {
1344                 ugd->pick_device = dev;
1345                 g_idle_add((GSourceFunc)__bt_idle_destroy_ug, ugd);
1346         }
1347
1348         FN_END;
1349 }
1350
1351 static void __bt_main_searched_item_sel_cb(void *data, Evas_Object *obj,
1352                                            void *event_info)
1353 {
1354         FN_START;
1355
1356         bt_ug_data *ugd = NULL;
1357         bt_dev_t *dev = NULL;
1358         Elm_Object_Item *item = NULL;
1359         int ret;
1360
1361         elm_genlist_item_selected_set((Elm_Object_Item *)event_info,
1362                                       EINA_FALSE);
1363
1364         retm_if(data == NULL, "Invalid argument: bt_ug_data is NULL\n");
1365
1366         ugd = (bt_ug_data *)data;
1367
1368         ret_if(ugd->op_status == BT_PAIRING);
1369
1370         item = (Elm_Object_Item *)event_info;
1371
1372         dev = _bt_main_get_dev_info(ugd->searched_device,
1373                                   (Elm_Object_Item *)event_info);
1374         retm_if(dev == NULL, "Invalid argument: device info is NULL\n");
1375
1376         if (ugd->bt_launch_mode == BT_LAUNCH_USE_NFC) {
1377                 char address[18] = { 0 };
1378                 service_h service = NULL;
1379
1380                 service_create(&service);
1381
1382                 ret_if(service == NULL);
1383
1384                 BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", dev->bd_addr[0],
1385                        dev->bd_addr[1], dev->bd_addr[2], dev->bd_addr[3],
1386                        dev->bd_addr[4], dev->bd_addr[5]);
1387
1388                 _bt_util_addr_type_to_addr_string(address, dev->bd_addr);
1389
1390                 if (service_add_extra_data(service, "device_address",
1391                                                 (const char *)address) < 0) {
1392                         BT_DBG("Fail to add extra data");
1393                 }
1394
1395                 if (service_add_extra_data(service, "device_name",
1396                                                 (const char *)dev->name) < 0) {
1397                         BT_DBG("Fail to add extra data");
1398                 }
1399
1400                 _bt_ug_destroy(ugd, (void *)service);
1401
1402                 service_destroy(service);
1403
1404                 return;
1405         } else if (ugd->bt_launch_mode == BT_LAUNCH_PICK) {
1406                 ugd->pick_device = dev;
1407                 g_idle_add((GSourceFunc)__bt_idle_destroy_ug, ugd);
1408                 return;
1409         }
1410
1411         ugd->searched_item = item;
1412
1413         if (_bt_util_is_battery_low() == TRUE) {
1414                 /* Battery is critical low */
1415                 _bt_main_create_information_popup(ugd, BT_STR_LOW_BATTERY);
1416                 return;
1417         }
1418
1419         if (ugd->bt_launch_mode == BT_LAUNCH_SEND_FILE) {
1420                 if (ugd->op_status == BT_SEARCHING) {
1421                         ret = bt_adapter_stop_device_discovery();
1422                         if(ret != BT_ERROR_NONE)
1423                                 BT_DBG("Fail to stop discovery");
1424                 }
1425
1426                 if (__bt_main_request_to_send(ugd,
1427                                         dev) == BT_UG_ERROR_NONE) {
1428                         BT_ERR("Fail to send");
1429                 }
1430
1431                 _bt_ug_destroy(ugd, NULL);
1432                 return;
1433         }
1434
1435         if (_bt_main_request_pairing_with_effect(ugd, item) !=
1436             BT_UG_ERROR_NONE) {
1437                 ugd->searched_item = NULL;
1438         }
1439
1440         FN_END;
1441 }
1442
1443 static void __bt_main_item_expanded(void *data, Evas_Object *obj, void *event_info)
1444 {
1445         FN_START;
1446
1447         Elm_Object_Item *it = event_info;
1448         Elm_Object_Item *git = NULL;
1449         Evas_Object *gl = elm_object_item_widget_get(it);
1450         bt_radio_item *item = NULL;
1451         int i = 0;
1452         bt_ug_data *ugd = NULL;
1453
1454         ret_if(data == NULL);
1455         ret_if(gl == NULL);
1456
1457         ugd = (bt_ug_data *)data;
1458
1459         for (i = 0; i < BT_MAX_TIMEOUT_ITEMS; i++) {
1460                 item = calloc(1, sizeof(bt_radio_item));
1461                 ret_if(item == NULL);
1462
1463                 item->index = i;
1464                 item->ugd = ugd;
1465
1466                 git = elm_genlist_item_append(gl, ugd->timeout_value_itc,
1467                                         (void *)item, it,
1468                                         ELM_GENLIST_ITEM_NONE,
1469                                         __bt_main_timeout_value_item_sel,
1470                                         (void *)item);
1471
1472                 item->it = git;
1473         }
1474
1475         elm_genlist_realized_items_update(ugd->main_genlist);
1476
1477         FN_END;
1478 }
1479
1480 static void __bt_main_item_contracted(void *data, Evas_Object *obj, void *event_info)
1481 {
1482         FN_START;
1483
1484         bt_ug_data *ugd = (bt_ug_data *)data;;
1485         Elm_Object_Item *item = event_info;
1486
1487         ret_if(ugd == NULL);
1488         ret_if(item == NULL);
1489
1490         elm_genlist_item_subitems_clear(item);
1491         elm_genlist_item_update(ugd->visible_item);
1492
1493         FN_END;
1494 }
1495
1496 static int __bt_main_get_item_type(bt_ug_data *ugd, Elm_Object_Item *item)
1497 {
1498         int type = BT_ITEM_NO_TYPE;
1499         bt_dev_t *dev = NULL;
1500         Eina_List *l = NULL;
1501
1502         retv_if(ugd == NULL, BT_ITEM_NO_TYPE);
1503         retv_if(item == NULL, BT_ITEM_NO_TYPE);
1504
1505         if (item == ugd->paired_padding || item == ugd->searched_padding ||
1506              item == ugd->paired_title || item == ugd->searched_title) {
1507                 /* Nothing to do */
1508                 return BT_ITEM_NO_TYPE;
1509         } else if (item == ugd->status_item) {
1510                 type = (ugd->visible_item != NULL) ? BT_ITEM_TOP : BT_ITEM_NO_TYPE;
1511                 return type;
1512         } else if (item == ugd->visible_item) {
1513                 type = (elm_genlist_item_expanded_get(item) == EINA_TRUE) ?
1514                                                 BT_ITEM_CENTER : BT_ITEM_BOTTOM;
1515                 return type;
1516         }
1517
1518         EINA_LIST_FOREACH(ugd->paired_device, l, dev) {
1519                 if (dev && (dev->genlist_item == item)) {
1520                         return dev->item_type;
1521                 }
1522         }
1523
1524         EINA_LIST_FOREACH(ugd->searched_device, l, dev) {
1525                 if (dev && (dev->genlist_item == item)) {
1526                         return dev->item_type;
1527                 }
1528         }
1529
1530         return BT_ITEM_NO_TYPE;
1531 }
1532
1533 static int __bt_main_get_1depth_item_type(bt_ug_data *ugd, Elm_Object_Item *item)
1534 {
1535         Elm_Object_Item *current = NULL;
1536         Elm_Object_Item *next = NULL;
1537
1538         retv_if(ugd == NULL, BT_ITEM_NO_TYPE);
1539         retv_if(item == NULL, BT_ITEM_NO_TYPE);
1540
1541         current = elm_genlist_item_next_get(ugd->visible_item);
1542
1543         while (current != NULL) {
1544                 next = elm_genlist_item_next_get(current);
1545
1546                 if (current == item) {
1547                         if (next == NULL ||
1548                              next == ugd->paired_padding ||
1549                               next == ugd->searched_padding) {
1550                                 return BT_ITEM_BOTTOM;
1551                         } else {
1552                                 return BT_ITEM_CENTER;
1553                         }
1554                 }
1555                 current = next;
1556         }
1557
1558         return BT_ITEM_NO_TYPE;
1559 }
1560
1561 static void __bt_main_gl_realized(void *data, Evas_Object *obj, void *event_info)
1562 {
1563         FN_START;
1564
1565         int depth;
1566         int item_type;
1567         bt_ug_data *ugd;
1568         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
1569
1570         ret_if(data == NULL);
1571         ret_if(item == NULL);
1572
1573         ugd = (bt_ug_data *)data;
1574
1575         depth = elm_genlist_item_expanded_depth_get(item);
1576
1577         if (depth == 0) {
1578                 item_type = __bt_main_get_item_type(ugd, item);
1579         } else {
1580                 item_type = __bt_main_get_1depth_item_type(ugd, item);
1581         }
1582
1583         BT_DBG("type: %d", item_type);
1584
1585         switch (item_type) {
1586         case BT_ITEM_TOP:
1587                 elm_object_item_signal_emit(item, "elm,state,top", "");
1588                 break;
1589         case BT_ITEM_CENTER:
1590                 elm_object_item_signal_emit(item, "elm,state,center", "");
1591                 break;
1592         case BT_ITEM_BOTTOM:
1593                 elm_object_item_signal_emit(item, "elm,state,bottom", "");
1594                 break;
1595         default:
1596                 elm_object_item_signal_emit(item, "elm,state,default", "");
1597                 break;
1598         }
1599
1600         FN_END;
1601 }
1602
1603 static Evas_Object *__bt_main_add_genlist_dialogue(Evas_Object *parent,
1604                                                    bt_ug_data *ugd)
1605 {
1606         FN_START;
1607         retv_if(ugd == NULL, NULL);
1608
1609         Evas_Object *genlist = NULL;
1610         Elm_Object_Item *git = NULL;
1611
1612         genlist = elm_genlist_add(parent);
1613
1614         /* We shoud set the mode to compress
1615              for using dialogue/2text.2icon.3.tb */
1616         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
1617         evas_object_smart_callback_add(genlist, "realized",
1618                                 __bt_main_gl_realized, ugd);
1619
1620         ugd->radio_main = elm_radio_add(genlist);
1621         elm_radio_state_value_set(ugd->radio_main, 0);
1622         elm_radio_value_set(ugd->radio_main, 0);
1623
1624         ugd->selected_radio = _bt_util_get_timeout_index(ugd->visibility_timeout);
1625
1626         /* Set item class for dialogue seperator */
1627         ugd->sp_itc = elm_genlist_item_class_new();
1628         retv_if (ugd->sp_itc == NULL, NULL);
1629
1630         ugd->sp_itc->item_style = "dialogue/separator";
1631         ugd->sp_itc->func.content_get = NULL;
1632         ugd->sp_itc->func.text_get = NULL;
1633         ugd->sp_itc->func.state_get = NULL;
1634         ugd->sp_itc->func.del = NULL;
1635
1636         /* Set item class for bluetooth status and on/off button */
1637         ugd->status_itc = elm_genlist_item_class_new();
1638         retv_if (ugd->status_itc == NULL, NULL);
1639
1640         ugd->status_itc->item_style = "dialogue/2text.1icon.10";
1641         ugd->status_itc->func.text_get = __bt_main_status_label_get;
1642         ugd->status_itc->func.content_get = __bt_main_status_icon_get;
1643         ugd->status_itc->func.state_get = NULL;
1644         ugd->status_itc->func.del = NULL;
1645
1646         if (ugd->bt_launch_mode == BT_LAUNCH_NORMAL) {
1647                 /* Set item class for visibility */
1648                 ugd->visible_itc = elm_genlist_item_class_new();
1649                 retv_if (ugd->visible_itc == NULL, NULL);
1650
1651                 ugd->visible_itc->item_style = "dialogue/2text.3/expandable";
1652                 ugd->visible_itc->func.text_get = __bt_main_visible_label_get;
1653                 ugd->visible_itc->func.content_get = NULL;
1654                 ugd->visible_itc->func.state_get = NULL;
1655                 ugd->visible_itc->func.del = NULL;
1656         }
1657
1658         /* Set item class for paired dialogue title */
1659         ugd->paired_title_itc = elm_genlist_item_class_new();
1660         retv_if (ugd->paired_title_itc == NULL, NULL);
1661
1662         ugd->paired_title_itc->item_style = "dialogue/grouptitle";
1663         ugd->paired_title_itc->func.text_get = __bt_main_paired_title_label_get;
1664         ugd->paired_title_itc->func.content_get = NULL;
1665         ugd->paired_title_itc->func.state_get = NULL;
1666         ugd->paired_title_itc->func.del = NULL;
1667
1668         /* Set item class for searched dialogue title */
1669         ugd->searched_title_itc = elm_genlist_item_class_new();
1670         retv_if (ugd->searched_title_itc == NULL, NULL);
1671
1672         ugd->searched_title_itc->item_style = "dialogue/grouptitle";
1673         ugd->searched_title_itc->func.text_get =
1674             __bt_main_searched_title_label_get;
1675         ugd->searched_title_itc->func.content_get = __bt_main_title_icon_get;
1676         ugd->searched_title_itc->func.state_get = NULL;
1677         ugd->searched_title_itc->func.del = NULL;
1678
1679         /* Set item class for paired device */
1680         ugd->device_itc = elm_genlist_item_class_new();
1681         retv_if (ugd->device_itc == NULL, NULL);
1682
1683         if (ugd->bt_launch_mode == BT_LAUNCH_PICK)
1684                 ugd->device_itc->item_style = "dialogue/1text.2icon";
1685         else
1686                 ugd->device_itc->item_style = "dialogue/2text.2icon.3.tb";
1687
1688         ugd->device_itc->func.text_get = __bt_main_list_label_get;
1689         ugd->device_itc->func.content_get = __bt_main_list_icon_get;
1690         ugd->device_itc->func.state_get = NULL;
1691         ugd->device_itc->func.del = NULL;
1692
1693         /* Set item class for searched device */
1694         ugd->searched_itc = elm_genlist_item_class_new();
1695         retv_if (ugd->searched_itc == NULL, NULL);
1696
1697         ugd->searched_itc->item_style = "dialogue/1text.2icon";
1698         ugd->searched_itc->func.text_get = __bt_main_searched_label_get;
1699         ugd->searched_itc->func.content_get = __bt_main_searched_icon_get;
1700         ugd->searched_itc->func.state_get = NULL;
1701         ugd->searched_itc->func.del = NULL;
1702
1703         /* Set item class for no device */
1704         ugd->no_device_itc = elm_genlist_item_class_new();
1705         retv_if (ugd->no_device_itc == NULL, NULL);
1706
1707         ugd->no_device_itc->item_style = "dialogue/1text";
1708         ugd->no_device_itc->func.text_get = __bt_main_no_device_label_get;
1709         ugd->no_device_itc->func.content_get = NULL;
1710         ugd->no_device_itc->func.state_get = NULL;
1711         ugd->no_device_itc->func.del = NULL;
1712
1713         /* Set item class for timeout value */
1714         ugd->timeout_value_itc = elm_genlist_item_class_new();
1715         retv_if(ugd->timeout_value_itc == NULL, NULL);
1716
1717         ugd->timeout_value_itc->item_style = "dialogue/1text.1icon/expandable2";
1718         ugd->timeout_value_itc->func.text_get = __bt_main_timeout_value_label_get;
1719         ugd->timeout_value_itc->func.content_get = __bt_main_timeout_value_icon_get;
1720         ugd->timeout_value_itc->func.state_get = NULL;
1721         ugd->timeout_value_itc->func.del = __bt_main_timeout_value_del;
1722
1723         /* Seperator */
1724         git = elm_genlist_item_append(genlist, ugd->sp_itc, ugd, NULL,
1725                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
1726
1727         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
1728
1729         /* Status and on/off button */
1730         git = elm_genlist_item_append(genlist, ugd->status_itc, ugd, NULL,
1731                                     ELM_GENLIST_ITEM_NONE,
1732                                     __bt_main_status_item_sel, ugd);
1733         ugd->status_item = git;
1734
1735         if (ugd->bt_launch_mode == BT_LAUNCH_NORMAL) {
1736                 /* visibility */
1737                 git = elm_genlist_item_append(genlist, ugd->visible_itc, ugd,
1738                                         NULL, ELM_GENLIST_ITEM_TREE,
1739                                         __bt_main_visible_item_sel, ugd);
1740                 ugd->visible_item = git;
1741                 elm_object_item_disabled_set(ugd->visible_item, EINA_TRUE);
1742         }
1743
1744         evas_object_show(genlist);
1745
1746         evas_object_smart_callback_add(genlist, "expanded", __bt_main_item_expanded, ugd);
1747         evas_object_smart_callback_add(genlist, "contracted", __bt_main_item_contracted, ugd);
1748
1749         FN_END;
1750         return genlist;
1751 }
1752
1753 static Evas_Object *__bt_main_add_visibility_dialogue(Evas_Object *parent,
1754                                                    bt_ug_data *ugd)
1755 {
1756         FN_START;
1757         retv_if(ugd == NULL, NULL);
1758
1759         Evas_Object *genlist = NULL;
1760         Elm_Object_Item *git = NULL;
1761
1762         genlist = elm_genlist_add(parent);
1763
1764         evas_object_smart_callback_add(genlist, "realized",
1765                                 __bt_main_gl_realized, ugd);
1766
1767         ugd->radio_main = elm_radio_add(genlist);
1768         elm_radio_state_value_set(ugd->radio_main, 0);
1769         elm_radio_value_set(ugd->radio_main, 0);
1770
1771         ugd->selected_radio = _bt_util_get_timeout_index(ugd->visibility_timeout);
1772
1773         /* Set item class for dialogue seperator */
1774         ugd->sp_itc = elm_genlist_item_class_new();
1775         retv_if (ugd->sp_itc == NULL, NULL);
1776
1777         ugd->sp_itc->item_style =  "dialogue/separator";
1778         ugd->sp_itc->func.content_get = NULL;
1779         ugd->sp_itc->func.text_get = NULL;
1780         ugd->sp_itc->func.state_get = NULL;
1781         ugd->sp_itc->func.del = NULL;
1782
1783         /* Set item class for bluetooth status and on/off button */
1784         ugd->status_itc = elm_genlist_item_class_new();
1785         retv_if (ugd->status_itc == NULL, NULL);
1786
1787         ugd->status_itc->item_style = "dialogue/2text.1icon.10";
1788         ugd->status_itc->func.text_get = __bt_main_status_label_get;
1789         ugd->status_itc->func.content_get = __bt_main_status_icon_get;
1790         ugd->status_itc->func.state_get = NULL;
1791         ugd->status_itc->func.del = NULL;
1792
1793         /* Set item class for visibility */
1794         ugd->visible_itc = elm_genlist_item_class_new();
1795         retv_if (ugd->visible_itc == NULL, NULL);
1796
1797         ugd->visible_itc->item_style = "dialogue/2text.3/expandable";
1798         ugd->visible_itc->func.text_get = __bt_main_visible_label_get;
1799         ugd->visible_itc->func.content_get = NULL;
1800         ugd->visible_itc->func.state_get = NULL;
1801         ugd->visible_itc->func.del = NULL;
1802
1803         /* Set item class for timeout value */
1804         ugd->timeout_value_itc = elm_genlist_item_class_new();
1805         retv_if(ugd->timeout_value_itc == NULL, NULL);
1806
1807         ugd->timeout_value_itc->item_style = "dialogue/1text.1icon/expandable2";
1808         ugd->timeout_value_itc->func.text_get = __bt_main_timeout_value_label_get;
1809         ugd->timeout_value_itc->func.content_get = __bt_main_timeout_value_icon_get;
1810         ugd->timeout_value_itc->func.state_get = NULL;
1811         ugd->timeout_value_itc->func.del = __bt_main_timeout_value_del;
1812
1813         /* Seperator */
1814         git = elm_genlist_item_append(genlist, ugd->sp_itc, ugd, NULL,
1815                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
1816
1817         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
1818
1819         /* Status and on/off button */
1820         git = elm_genlist_item_append(genlist, ugd->status_itc, ugd, NULL,
1821                                     ELM_GENLIST_ITEM_NONE,
1822                                     __bt_main_status_item_sel, ugd);
1823         ugd->status_item = git;
1824
1825         /* visibility */
1826         git = elm_genlist_item_append(genlist, ugd->visible_itc, ugd,
1827                                 NULL, ELM_GENLIST_ITEM_TREE,
1828                                 __bt_main_visible_item_sel, ugd);
1829         ugd->visible_item = git;
1830         elm_object_item_disabled_set(ugd->visible_item, EINA_TRUE);
1831
1832         evas_object_show(genlist);
1833
1834         evas_object_smart_callback_add(genlist, "expanded", __bt_main_item_expanded, ugd);
1835         evas_object_smart_callback_add(genlist, "contracted", __bt_main_item_contracted, ugd);
1836
1837         FN_END;
1838         return genlist;
1839 }
1840
1841 static gboolean __bt_main_system_popup_timer_cb(gpointer user_data)
1842 {
1843         FN_START;
1844
1845         int ret = 0;
1846         bt_ug_data *ugd = NULL;
1847         bundle *b = NULL;
1848
1849         retv_if(user_data == NULL, FALSE);
1850
1851         ugd = (bt_ug_data *)user_data;
1852
1853         b = ugd->popup_bundle;
1854
1855         ugd->popup_bundle = NULL;
1856         ugd->popup_timer = 0;
1857
1858         if (NULL == b) {
1859                 BT_DBG("bundle is NULL\n");
1860                 return FALSE;
1861         }
1862
1863         ret = syspopup_launch("bt-syspopup", b);
1864         if (0 > ret) {
1865                 BT_DBG("Sorry Can not launch popup\n");
1866         } else {
1867                 BT_DBG("Finally Popup launched \n");
1868         }
1869
1870         bundle_free(b);
1871
1872         FN_END;
1873         return (0 > ret) ? TRUE : FALSE;
1874 }
1875
1876 static char *__bt_main_get_name(bt_dev_t *dev)
1877 {
1878         FN_START;
1879
1880         char *conv_str = NULL;
1881         char tmp_str[BT_DEVICE_NAME_LENGTH_MAX + 1] = { 0, };
1882         char buf[BT_FILE_NAME_LEN_MAX + 1] = { 0, };
1883
1884         retv_if(dev == NULL, NULL);
1885
1886         if (strlen(dev->name) > 0) {
1887                 strncpy(tmp_str, dev->name, BT_DEVICE_NAME_LENGTH_MAX);
1888                 tmp_str[BT_DEVICE_NAME_LENGTH_MAX] = '\0';
1889
1890                 conv_str = elm_entry_utf8_to_markup(tmp_str);
1891                 if (conv_str)
1892                         strncpy(buf, conv_str, BT_DEVICE_NAME_LENGTH_MAX);
1893                 else
1894                         strncpy(buf, tmp_str, BT_DEVICE_NAME_LENGTH_MAX);
1895         }
1896
1897         if (conv_str)
1898                 free(conv_str);
1899
1900         FN_END;
1901         return strdup(buf);
1902 }
1903
1904 static bool __bt_main_get_mime_type(char *file)
1905 {
1906         FN_START;
1907
1908         char mime_type[BT_FILE_NAME_LEN_MAX] = {0, };
1909         int len = strlen("image");
1910
1911         retv_if(file == NULL, FALSE);
1912
1913         if (aul_get_mime_from_file(file, mime_type,
1914                      BT_FILE_NAME_LEN_MAX) == AUL_R_OK) {
1915                 BT_DBG(" mime type    =%s \n \n \n", mime_type);
1916                 if (0 != strncmp(mime_type, "image", len))
1917                         return FALSE;
1918         } else {
1919                 BT_DBG("Error getting mime type");
1920                 return FALSE;
1921         }
1922
1923         FN_END;
1924         return TRUE;
1925 }
1926
1927 static bool __bt_main_is_image_file(service_h service)
1928 {
1929         FN_START;
1930
1931         char *value = NULL;
1932         int number_of_files = 0;
1933         char *token = NULL;
1934         char *param = NULL;
1935         int i = 0;
1936
1937         retvm_if(service == NULL, FALSE, "Invalid data bundle");
1938
1939         if (service_get_extra_data(service, "filecount", &value) < 0)
1940                 BT_DBG("Get data error");
1941
1942         retv_if(value == NULL, FALSE);
1943
1944         number_of_files = atoi(value);
1945         BT_DBG("[%d] files\n", number_of_files);
1946         free(value);
1947         value = NULL;
1948
1949         if (number_of_files <= 0) {
1950                 BT_DBG("No File\n");
1951                 return FALSE;
1952         }
1953
1954         if (service_get_extra_data(service, "files", &value) < 0)
1955                 BT_DBG("Get data error");
1956
1957         retv_if(value == NULL, FALSE);
1958
1959         param = value;
1960         while (((token = strstr(param, "?")) != NULL) &&
1961                         i < number_of_files) {
1962                 *token = '\0';
1963                 if (!__bt_main_get_mime_type(param)) {
1964                         *token = '?';
1965                         free(value);
1966                         return FALSE;
1967                 }
1968                 BT_DBG("File [%d] [%s]\n", i, param);
1969                 *token = '?';
1970                 param = token + 1;
1971                 i++;
1972         }
1973         if (i == (number_of_files - 1)) {
1974                 if (!__bt_main_get_mime_type(param)) {
1975                         free(value);
1976                         return FALSE;
1977                 }
1978                 BT_DBG("File [%d] [%s]\n", i, param);
1979         } else {
1980                 BT_DBG("Count not match : [%d] / [%d]\n",
1981                         number_of_files, i);
1982                 free(value);
1983                 return FALSE;
1984         }
1985
1986         free(value);
1987
1988         FN_END;
1989         return TRUE;
1990 }
1991
1992 /**********************************************************************
1993 *                                              Common Functions
1994 ***********************************************************************/
1995
1996 void _bt_main_scan_device(bt_ug_data *ugd)
1997 {
1998         FN_START;
1999         int ret;
2000
2001         ret_if(ugd == NULL);
2002
2003         if (ugd->op_status != BT_DEACTIVATED &&
2004              ugd->op_status != BT_ACTIVATED) {
2005                 BT_DBG("current bluetooth status [%d]", ugd->op_status);
2006                 return;
2007         }
2008
2009         if (_bt_util_is_battery_low() == TRUE) {
2010                 /* Battery is critical low */
2011                 _bt_main_create_information_popup(ugd, BT_STR_LOW_BATTERY);
2012                 return;
2013         }
2014
2015         if (ugd->op_status == BT_DEACTIVATED) {
2016                 ret = __bt_main_enable_bt((void *)ugd);
2017                 if(!ret) {
2018                         /* After activating, searching start by this flag. */
2019                         ugd->search_req = TRUE;
2020                         elm_object_disabled_set(ugd->scan_btn, EINA_TRUE);
2021                 }
2022         } else {
2023                 _bt_main_remove_all_searched_devices(ugd);
2024
2025                 ret = bt_adapter_start_device_discovery();
2026                 if (!ret) {
2027                         ugd->op_status = BT_SEARCHING;
2028                         elm_object_text_set(ugd->scan_btn, BT_STR_STOP);
2029
2030                         if (ugd->searched_title == NULL)
2031                                 _bt_main_add_searched_title(ugd);
2032                 } else {
2033                         BT_DBG("Operation failed : Error Cause[%d]", ret);
2034                 }
2035         }
2036
2037         elm_genlist_item_update(ugd->status_item);
2038
2039         FN_END;
2040 }
2041
2042 void _bt_main_draw_selection_info(bt_ug_data *ugd, char *message)
2043 {
2044         if (ugd->selectioninfo)
2045                 evas_object_del(ugd->selectioninfo);
2046
2047         ugd->selectioninfo = _bt_create_selectioninfo(ugd->win_main,
2048                                         message, ugd->rotation,
2049                                         _bt_main_selectioninfo_hide_cb,
2050                                         ugd, BT_DELETED_TIMEOUT);
2051 }
2052
2053 int _bt_main_service_request_cb(void *data)
2054 {
2055         FN_START;
2056
2057         bt_ug_data *ugd = NULL;
2058
2059         retvm_if(data == NULL, BT_UG_FAIL,
2060                  "Invalid argument: bt_ug_data is NULL\n");
2061
2062         ugd = (bt_ug_data *)data;
2063
2064         if (ugd->request_timer) {
2065                 ecore_timer_del(ugd->request_timer);
2066                 ugd->request_timer = NULL;
2067         }
2068
2069         ugd->auto_service_search = FALSE;
2070
2071         /* Need to modify API: Address parameter */
2072         if (ugd->waiting_service_response == TRUE) {
2073                 bt_dev_t *dev = NULL;
2074
2075                 ugd->waiting_service_response = FALSE;
2076                 bt_device_cancel_service_search();
2077
2078                 dev =
2079                     _bt_main_get_dev_info(ugd->paired_device, ugd->paired_item);
2080                 retvm_if(dev == NULL, BT_UG_FAIL, "dev is NULL\n");
2081
2082                 dev->status = BT_IDLE;
2083                 elm_genlist_item_update(ugd->paired_item);
2084
2085                 _bt_main_connect_device(ugd, dev);
2086         } else {
2087                 ugd->paired_item = NULL;
2088         }
2089
2090         FN_END;
2091         return BT_UG_ERROR_NONE;
2092 }
2093
2094 char *_bt_main_get_device_icon(int major_class, int minor_class, int connected)
2095 {
2096         FN_START;
2097         char *icon = BT_ICON_UNKNOWN;
2098
2099         BT_DBG("major_class: %d, minor_class: %d\n", major_class, minor_class);
2100
2101         switch (major_class) {
2102         case BT_MAJOR_DEV_CLS_COMPUTER:
2103                 icon = (connected == 0) ? BT_ICON_PC : BT_ICON_CONNECTED_PC;
2104                 break;
2105         case BT_MAJOR_DEV_CLS_PHONE:
2106                 icon = (connected == 0) ? BT_ICON_PHONE :
2107                                         BT_ICON_CONNECTED_PHONE;
2108                 break;
2109         case BT_MAJOR_DEV_CLS_AUDIO:
2110                 BT_DBG("minor_class: %x", minor_class);
2111
2112                 if (minor_class == BTAPP_MIN_DEV_CLS_HEADPHONES) {
2113                         icon = (connected == 0) ? BT_ICON_HEADPHONE :
2114                                                 BT_ICON_CONNECTED_HEADPHONE;
2115                 } else {
2116                         icon = (connected == 0) ? BT_ICON_HEADSET :
2117                                                 BT_ICON_CONNECTED_HEADSET;
2118                 }
2119                 break;
2120         case BT_MAJOR_DEV_CLS_LAN_ACCESS_POINT:
2121                 icon = (connected == 0) ? BT_ICON_NETWORK :
2122                                         BT_ICON_CONNECTED_NETWORK;
2123                 break;
2124         case BT_MAJOR_DEV_CLS_IMAGING:
2125                 if (minor_class == BTAPP_MIN_DEV_CLS_PRINTER) {
2126                         icon = (connected == 0) ? BT_ICON_PRINTER :
2127                                         BT_ICON_CONNECTED_PRINTER;
2128                 } else if (minor_class == BTAPP_MIN_DEV_CLS_CAMERA) {
2129                         icon = (connected == 0) ? BT_ICON_CAMERA :
2130                                         BT_ICON_CONNECTED_CAMERA;
2131                 } else if (minor_class == BTAPP_MIN_DEV_CLS_DISPLAY) {
2132                         icon = (connected == 0) ? BT_ICON_DISPLAY :
2133                                         BT_ICON_CONNECTED_DISPLAY;
2134                 }
2135                 break;
2136         case BT_MAJOR_DEV_CLS_PERIPHERAL:
2137                 if (minor_class == BTAPP_MIN_DEV_CLS_KEY_BOARD) {
2138                         icon = (connected == 0) ? BT_ICON_KEYBOARD :
2139                                         BT_ICON_CONNECTED_KEYBOARD;
2140                 } else if (minor_class == BTAPP_MIN_DEV_CLS_POINTING_DEVICE) {
2141                         icon = (connected == 0) ? BT_ICON_MOUSE :
2142                                         BT_ICON_CONNECTED_MOUSE;
2143                 } else if (minor_class == BTAPP_MIN_DEV_CLS_GAME_PAD) {
2144                         icon = (connected == 0) ? BT_ICON_GAMING :
2145                                         BT_ICON_CONNECTED_GAMING;
2146                 }
2147                 break;
2148         case BT_MAJOR_DEV_CLS_HEALTH:
2149                 icon = (connected == 0) ? BT_ICON_HEALTH :
2150                                         BT_ICON_CONNECTED_HEALTH;
2151                 break;
2152
2153                 /* end */
2154         default:
2155                 icon = (connected == 0) ? BT_ICON_UNKNOWN :
2156                                         BT_ICON_CONNECTED_UNKNOWN;
2157                 break;
2158         }
2159
2160         FN_END;
2161         return icon;
2162 }
2163
2164 void _bt_main_popup_del_cb(void *data, Evas_Object *obj,
2165                                     void *event_info)
2166 {
2167         FN_START;
2168         retm_if(data == NULL, "Invalid argument: struct bt_appdata is NULL\n");
2169
2170         bt_ug_data *ugd = (bt_ug_data *)data;
2171
2172         if (ugd->popup) {
2173                 BT_DBG("delete popup\n");
2174                 evas_object_del(ugd->popup);
2175                 ugd->popup = NULL;
2176         }
2177
2178         ugd->back_cb = NULL;
2179
2180         FN_END;
2181 }
2182
2183 void _bt_main_selectioninfo_hide_cb(void *data, Evas * e,
2184                                         Evas_Object *obj, void *event_info)
2185 {
2186         FN_START;
2187         retm_if(data == NULL, "Invalid argument: struct bt_appdata is NULL\n");
2188
2189         bt_ug_data *ugd = (bt_ug_data *)data;
2190
2191         evas_object_del(ugd->selectioninfo);
2192         ugd->selectioninfo = NULL;
2193
2194         FN_END;
2195 }
2196
2197 Elm_Object_Item *_bt_main_add_paired_device(bt_ug_data *ugd, bt_dev_t *dev)
2198 {
2199         FN_START;
2200
2201         Elm_Object_Item *git;
2202
2203         retvm_if(ugd == NULL, NULL, "Invalid argument: ugd is NULL\n");
2204         retvm_if(dev == NULL, NULL, "Invalid argument: dev is NULL\n");
2205
2206         /* Paired device Title */
2207         if (ugd->paired_title == NULL) {
2208                 if (ugd->searched_padding == NULL) {
2209                         git = elm_genlist_item_append(ugd->main_genlist,
2210                                                     ugd->sp_itc,
2211                                                     (void *)ugd, NULL,
2212                                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
2213                         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
2214                         ugd->paired_padding = git;
2215
2216                         git = elm_genlist_item_append(ugd->main_genlist,
2217                                                 ugd->paired_title_itc,
2218                                                 (void *)ugd, NULL,
2219                                                 ELM_GENLIST_ITEM_NONE,
2220                                                 NULL, NULL);
2221                         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
2222
2223                 } else {
2224                         git = elm_genlist_item_insert_before(ugd->main_genlist,
2225                                                 ugd->sp_itc,
2226                                                 (void *)ugd, NULL,
2227                                                 ugd->searched_padding,
2228                                                 ELM_GENLIST_ITEM_NONE,
2229                                                 NULL, NULL);
2230                         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
2231                         ugd->paired_padding = git;
2232
2233                         git = elm_genlist_item_insert_after(ugd->main_genlist,
2234                                                 ugd->paired_title_itc,
2235                                                 (void *)ugd, NULL,
2236                                                 ugd->paired_padding,
2237                                                 ELM_GENLIST_ITEM_NONE,
2238                                                 NULL, NULL);
2239                         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
2240                 }
2241                 ugd->paired_title = git;
2242
2243                 __bt_main_set_controlbar_mode(ugd, BT_CONTROL_BAR_ENABLE);
2244         }
2245
2246         dev->ugd = (void *)ugd;
2247         dev->is_bonded = TRUE;
2248         dev->status = BT_IDLE;
2249
2250         /* Add the device item in the list */
2251         git = elm_genlist_item_insert_after(ugd->main_genlist, ugd->device_itc,
2252                                           dev, NULL, ugd->paired_title,
2253                                           ELM_GENLIST_ITEM_NONE,
2254                                           __bt_main_paired_item_sel_cb, ugd);
2255
2256         dev->genlist_item = git;
2257
2258         FN_END;
2259         return git;
2260 }
2261
2262 Elm_Object_Item *_bt_main_add_searched_device(bt_ug_data *ugd, bt_dev_t *dev)
2263 {
2264         FN_START;
2265
2266         Elm_Object_Item *git = NULL;
2267
2268         retvm_if(ugd == NULL, NULL, "Invalid argument: ugd is NULL\n");
2269         retvm_if(dev == NULL, NULL, "Invalid argument: dev is NULL\n");
2270
2271         if (ugd->searched_title == NULL)
2272                 _bt_main_add_searched_title(ugd);
2273
2274         /* Searched device Item */
2275         if (ugd->searched_device == NULL) {
2276                 git = elm_genlist_item_insert_after(ugd->main_genlist, ugd->searched_itc,
2277                                         dev, NULL, ugd->searched_title, ELM_GENLIST_ITEM_NONE,
2278                                         __bt_main_searched_item_sel_cb, ugd);
2279         } else {
2280                 bt_dev_t *item_dev = NULL;
2281                 Elm_Object_Item *item = NULL;
2282                 Elm_Object_Item *next = NULL;
2283
2284                 item = elm_genlist_item_next_get(ugd->searched_title);
2285
2286                 /* check the RSSI value of searched device list add arrange its order */
2287                 while (item != NULL) {
2288                         item_dev = _bt_main_get_dev_info(ugd->searched_device, item);
2289                         retv_if(item_dev == NULL, NULL);
2290
2291                         if (item_dev->rssi > dev->rssi) {
2292                                 next = elm_genlist_item_next_get(item);
2293                                 if (next == NULL) {
2294                                         git = elm_genlist_item_insert_after(ugd->main_genlist,
2295                                                         ugd->searched_itc,
2296                                                         dev, NULL, item, ELM_GENLIST_ITEM_NONE,
2297                                                         __bt_main_searched_item_sel_cb, ugd);
2298                                         break;
2299                                 }
2300                                 item = next;
2301                         } else {
2302                                 git = elm_genlist_item_insert_before(ugd->main_genlist,
2303                                                         ugd->searched_itc,
2304                                                         dev, NULL, item, ELM_GENLIST_ITEM_NONE,
2305                                                         __bt_main_searched_item_sel_cb, ugd);
2306                                 break;
2307                         }
2308                 }
2309         }
2310
2311         dev->genlist_item = git;
2312         dev->status = BT_IDLE;
2313         dev->ugd = (void *)ugd;
2314         dev->is_bonded = FALSE;
2315
2316         FN_END;
2317         return git;
2318 }
2319
2320 Elm_Object_Item *_bt_main_add_no_device_found(bt_ug_data *ugd)
2321 {
2322         FN_START;
2323
2324         Elm_Object_Item *git = NULL;
2325
2326         retvm_if(ugd == NULL, NULL, "Invalid argument: ugd is NULL\n");
2327         retvm_if(ugd->searched_title == NULL, NULL, "title is NULL\n");
2328
2329         /* No device found Item */
2330         git = elm_genlist_item_insert_after(ugd->main_genlist, ugd->no_device_itc,
2331                                 NULL, NULL, ugd->searched_title, ELM_GENLIST_ITEM_NONE,
2332                                 NULL, ugd);
2333
2334         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
2335
2336         FN_END;
2337         return git;
2338 }
2339
2340 int _bt_main_draw_list_view(bt_ug_data *ugd)
2341 {
2342         FN_START;
2343
2344         int ret = 0;
2345         Evas_Object *navi;
2346         Evas_Object *btn;
2347         Evas_Object *layout;
2348         Evas_Object *genlist;
2349         Evas_Object *back_btn;
2350         Elm_Object_Item *navi_it;
2351
2352         retv_if(ugd == NULL, BT_UG_FAIL);
2353
2354         navi = _bt_create_naviframe(ugd->base);
2355
2356         /* create back button */
2357         back_btn = elm_button_add(navi);
2358
2359         _bt_create_bg(navi, "group_list");
2360
2361         layout = _bt_create_layout(navi, NULL, NULL);
2362
2363         genlist = __bt_main_add_genlist_dialogue(layout, ugd);
2364
2365         navi_it = elm_naviframe_item_push(navi, BT_STR_BLUETOOTH, back_btn, NULL, genlist,
2366                                NULL);
2367
2368         /* BT_STR_SCAN will be changed to icon */
2369         btn = _bt_create_button(navi, "naviframe/toolbar/default", BT_STR_SCAN,
2370                                 NULL, __bt_main_controlbar_btn_cb,
2371                                 (void *)ugd);
2372         ugd->scan_btn = btn;
2373
2374         elm_object_item_part_content_set(navi_it, "toolbar_button1", btn);
2375
2376         ugd->navi_it = navi_it;
2377
2378         /* Style set should be called after elm_naviframe_item_push(). */
2379         elm_object_style_set(back_btn, "naviframe/back_btn/default");
2380         evas_object_smart_callback_add(back_btn, "clicked",
2381                                        __bt_main_quit_btn_cb, (void *)ugd);
2382
2383         ugd->navi_bar = navi;
2384         ugd->main_layout = layout;
2385         ugd->main_genlist = genlist;
2386         ugd->confirm_req = BT_NONE_REQ;
2387
2388         /* In the NFC case, we don't need to display the paired device */
2389         if (ugd->op_status == BT_ACTIVATED &&
2390              ugd->bt_launch_mode != BT_LAUNCH_USE_NFC) {
2391                 _bt_main_draw_paired_devices(ugd);
2392
2393                 if (ugd->paired_device == NULL ||
2394                      eina_list_count(ugd->paired_device) == 0) {
2395                         if (_bt_util_is_battery_low() == FALSE) {
2396                                 ret = bt_adapter_start_device_discovery();
2397                                 if (!ret) {
2398                                         ugd->op_status = BT_SEARCHING;
2399                                         elm_object_text_set(ugd->scan_btn, BT_STR_STOP);
2400
2401                                         elm_genlist_item_update(ugd->status_item);
2402
2403                                         if (ugd->searched_title == NULL)
2404                                                 _bt_main_add_searched_title(ugd);
2405                                 } else
2406                                         BT_DBG("Operation failed : Error Cause[%d]", ret);
2407                         }
2408                         elm_genlist_item_update(ugd->status_item);
2409                 }
2410         } else if (ugd->op_status == BT_DEACTIVATED &&
2411              ugd->bt_launch_mode != BT_LAUNCH_NORMAL) {
2412                  __bt_main_enable_bt(ugd);
2413         }
2414
2415         if (ugd->op_status == BT_ACTIVATED || ugd->op_status == BT_SEARCHING)
2416                 elm_object_item_disabled_set(ugd->visible_item, EINA_FALSE);
2417
2418
2419         FN_END;
2420         return BT_UG_ERROR_NONE;
2421 }
2422
2423 int _bt_main_draw_visibility_view(bt_ug_data *ugd)
2424 {
2425         FN_START;
2426
2427         Evas_Object *navi = NULL;
2428         Evas_Object *layout = NULL;
2429         Evas_Object *genlist = NULL;
2430         Evas_Object *back_btn = NULL;
2431         Elm_Object_Item *navi_it;
2432
2433         retv_if(ugd == NULL, BT_UG_FAIL);
2434
2435         navi = _bt_create_naviframe(ugd->base);
2436
2437         /* create back button */
2438         back_btn = elm_button_add(navi);
2439
2440         _bt_create_bg(navi, "group_list");
2441
2442         layout = _bt_create_layout(navi, NULL, NULL);
2443
2444         genlist = __bt_main_add_visibility_dialogue(layout, ugd);
2445
2446         navi_it = elm_naviframe_item_push(navi, BT_STR_VISIBLE, back_btn, NULL, genlist,
2447                                NULL);
2448
2449         ugd->navi_it = navi_it;
2450
2451         /* create controlbar */
2452         _bt_create_controlbar(navi, "default");
2453
2454         /* Style set should be called after elm_naviframe_item_push(). */
2455         elm_object_style_set(back_btn, "naviframe/back_btn/default");
2456         evas_object_smart_callback_add(back_btn, "clicked",
2457                                        __bt_main_quit_btn_cb, (void *)ugd);
2458
2459         ugd->navi_bar = navi;
2460         ugd->main_layout = layout;
2461         ugd->main_genlist = genlist;
2462         ugd->confirm_req = BT_NONE_REQ;
2463
2464         if (ugd->op_status == BT_ACTIVATED)
2465                 elm_object_item_disabled_set(ugd->visible_item, EINA_FALSE);
2466
2467
2468         FN_END;
2469         return BT_UG_ERROR_NONE;
2470 }
2471
2472 static bool __bt_cb_adapter_bonded_device(bt_device_info_s *device_info,
2473                                                 void *user_data)
2474 {
2475         bt_dev_t *dev = NULL;
2476         bool connected = FALSE;
2477         bt_ug_data *ugd = NULL;
2478         void *profile_h = NULL;
2479
2480         retv_if(user_data == NULL, false);
2481
2482         ugd = (bt_ug_data *)user_data;
2483
2484         if (_bt_main_check_and_update_device(ugd->paired_device,
2485                                         device_info->remote_address,
2486                                         device_info->remote_name) >= 0) {
2487                         /* Update all realized items */
2488                         elm_genlist_realized_items_update(ugd->main_genlist);
2489
2490                         return true;
2491         }
2492
2493         dev = _bt_main_create_paired_device_item(device_info);
2494
2495         if (!dev)
2496                 return false;
2497
2498         dev->ugd = (void *)ugd;
2499
2500         if (dev->service_list & BT_SC_HFP_SERVICE_MASK ||
2501              dev->service_list & BT_SC_HSP_SERVICE_MASK ||
2502               dev->service_list & BT_SC_A2DP_SERVICE_MASK) {
2503                 connected = _bt_is_profile_connected(BT_HEADSET_CONNECTED,
2504                                                 ugd->conn, dev->bd_addr);
2505                 dev->connected_mask |= connected ? BT_HEADSET_CONNECTED : 0x00;
2506
2507                 connected = _bt_is_profile_connected(BT_STEREO_HEADSET_CONNECTED,
2508                                                 ugd->conn, dev->bd_addr);
2509                 dev->connected_mask |= connected ? BT_STEREO_HEADSET_CONNECTED : 0x00;
2510         } else if (dev->service_list & BT_SC_HID_SERVICE_MASK) {
2511                 connected = _bt_is_profile_connected(BT_HID_CONNECTED,
2512                                                 ugd->conn, dev->bd_addr);
2513                 dev->connected_mask |= connected ? BT_HID_CONNECTED : 0x00;
2514         }
2515
2516         profile_h = _bt_get_registered_net_profile(ugd->connection,
2517                                                 dev->bd_addr);
2518
2519         if (profile_h == NULL) {
2520                 profile_h = _bt_get_connected_net_profile(ugd->connection,
2521                                                         dev->bd_addr);
2522                 if (profile_h)
2523                         dev->connected_mask |= connected ? BT_NETWORK_CONNECTED : 0x00;
2524         }
2525
2526         if (profile_h)
2527                 _bt_set_profile_state_changed_cb(profile_h, dev);
2528
2529         BT_DBG("connected mask: %d", dev->connected_mask);
2530
2531         if (_bt_main_is_matched_profile(ugd->search_type,
2532                                 dev->major_class,
2533                                 dev->service_class,
2534                                 ugd->service) == TRUE) {
2535                 BT_DBG("Device count [%d]",
2536                        eina_list_count(ugd->paired_device));
2537
2538                 if (_bt_main_add_paired_device(ugd, dev) != NULL) {
2539                         ugd->paired_device =
2540                             eina_list_append(ugd->paired_device, dev);
2541
2542                         _bt_update_paired_item_style(ugd);
2543                 }
2544         } else {
2545                 BT_DBG("Device class and search type do not match \n");
2546                 free(dev);
2547         }
2548
2549         return true;
2550 }
2551
2552 void _bt_main_draw_paired_devices(bt_ug_data *ugd)
2553 {
2554         FN_START;
2555
2556         ret_if(ugd == NULL);
2557
2558         if (bt_adapter_foreach_bonded_device(__bt_cb_adapter_bonded_device,
2559                                                 (void *)ugd) != BT_ERROR_NONE) {
2560                 BT_DBG("bt_adapter_foreach_bonded_device() failed");
2561                 return;
2562         }
2563
2564         FN_END;
2565         return;
2566 }
2567
2568 void _bt_main_remove_paired_device(bt_ug_data *ugd, bt_dev_t *dev)
2569 {
2570         FN_START;
2571
2572         bt_dev_t *item = NULL;
2573         Eina_List *l = NULL;
2574         Eina_List *l_next = NULL;
2575
2576         retm_if(ugd == NULL, "Invalid argument: ugd is NULL\n");
2577         retm_if(dev == NULL, "Invalid argument: dev is NULL\n");
2578
2579         EINA_LIST_FOREACH_SAFE(ugd->paired_device, l, l_next, item) {
2580                 if (item && (item == dev)) {
2581                         elm_object_item_del(dev->genlist_item);
2582                         ugd->paired_device =
2583                             eina_list_remove_list(ugd->paired_device, l);
2584                         _bt_util_free_device_item(item);
2585                 }
2586         }
2587
2588         if (ugd->paired_device == NULL ||
2589              eina_list_count(ugd->paired_device) == 0) {
2590                 elm_object_item_del(ugd->paired_title);
2591                 ugd->paired_title = NULL;
2592
2593                 if (ugd->paired_padding) {
2594                         elm_object_item_del(ugd->paired_padding);
2595                         ugd->paired_padding = NULL;
2596                 }
2597         } else {
2598                 _bt_update_paired_item_style(ugd);
2599         }
2600
2601         FN_END;
2602         return;
2603 }
2604
2605 void _bt_main_remove_all_paired_devices(bt_ug_data *ugd)
2606 {
2607         FN_START;
2608
2609         bt_dev_t *dev = NULL;
2610         Eina_List *l = NULL;
2611         Eina_List *l_next = NULL;
2612         Elm_Object_Item *item;
2613         Elm_Object_Item *next;
2614
2615         ret_if(ugd == NULL);
2616
2617         if (ugd->paired_padding) {
2618                 item = elm_genlist_item_next_get(ugd->paired_padding);
2619
2620                 elm_object_item_del(ugd->paired_padding);
2621                 ugd->paired_padding = NULL;
2622
2623                 while (item != NULL && (item != ugd->searched_padding)) {
2624                         next = elm_genlist_item_next_get(item);
2625                         elm_object_item_del(item);
2626                         item = next;
2627                 }
2628
2629                 ugd->paired_title = NULL;
2630         }
2631
2632         EINA_LIST_FOREACH_SAFE(ugd->paired_device, l, l_next, dev) {
2633                 ugd->paired_device =
2634                     eina_list_remove_list(ugd->paired_device, l);
2635                 _bt_util_free_device_item(dev);
2636         }
2637
2638         FN_END;
2639         return;
2640 }
2641
2642 void _bt_main_remove_searched_device(bt_ug_data *ugd, bt_dev_t *dev)
2643 {
2644         FN_START;
2645
2646         bt_dev_t *item = NULL;
2647         Eina_List *l = NULL;
2648         Eina_List *l_next = NULL;
2649
2650         ret_if(ugd == NULL);
2651
2652         EINA_LIST_FOREACH_SAFE(ugd->searched_device, l, l_next, item) {
2653                 if (item && (item == dev)) {
2654                         elm_object_item_del(dev->genlist_item);
2655                         ugd->searched_device =
2656                             eina_list_remove_list(ugd->searched_device, l);
2657                         _bt_util_free_device_item(item);
2658                 }
2659         }
2660
2661         if (ugd->searched_device == NULL ||
2662              eina_list_count(ugd->searched_device) == 0) {
2663                 elm_object_item_del(ugd->searched_title);
2664                 ugd->searched_title = NULL;
2665
2666                 if (ugd->searched_padding) {
2667                         elm_object_item_del(ugd->searched_padding);
2668                         ugd->searched_padding = NULL;
2669                 }
2670         } else {
2671                 _bt_update_searched_item_style(ugd);
2672         }
2673
2674         FN_END;
2675         return;
2676 }
2677
2678 void _bt_main_remove_all_searched_devices(bt_ug_data *ugd)
2679 {
2680         FN_START;
2681
2682         bt_dev_t *dev = NULL;
2683         Eina_List *l = NULL;
2684         Eina_List *l_next = NULL;
2685         Elm_Object_Item *item;
2686         Elm_Object_Item *next;
2687
2688         ret_if(ugd == NULL);
2689
2690         if (ugd->searched_padding) {
2691                 item = elm_genlist_item_next_get(ugd->searched_padding);
2692                 elm_object_item_del(ugd->searched_padding);
2693                 ugd->searched_padding = NULL;
2694
2695                 while (item != NULL) {
2696                         next = elm_genlist_item_next_get(item);
2697                         elm_object_item_del(item);
2698                         item = next;
2699                 }
2700
2701                 ugd->searched_title = NULL;
2702                 ugd->no_device_item = NULL;
2703         }
2704
2705         EINA_LIST_FOREACH_SAFE(ugd->searched_device, l, l_next, dev) {
2706                 ugd->searched_device =
2707                     eina_list_remove_list(ugd->searched_device, l);
2708                 _bt_util_free_device_item(dev);
2709         }
2710
2711         FN_END;
2712         return;
2713 }
2714
2715 gboolean _bt_main_is_headset_connected(bt_ug_data *ugd)
2716 {
2717         gboolean connected = FALSE;
2718         Eina_List *l = NULL;
2719         bt_dev_t *item = NULL;
2720
2721         retv_if(ugd == NULL, FALSE);
2722
2723         EINA_LIST_FOREACH(ugd->paired_device, l, item) {
2724                 if (item) {
2725                         if ((item->service_list & BT_SC_HFP_SERVICE_MASK) ||
2726                              (item->service_list & BT_SC_HSP_SERVICE_MASK)) {
2727                                 connected = _bt_is_profile_connected(BT_HEADSET_CONNECTED,
2728                                                         ugd->conn, item->bd_addr);
2729
2730                                 if (connected)
2731                                         return TRUE;
2732                         }
2733                 }
2734         }
2735
2736         return connected;
2737 }
2738
2739 gboolean _bt_main_is_stereo_headset_connected(bt_ug_data *ugd)
2740 {
2741         gboolean connected = FALSE;
2742         Eina_List *l = NULL;
2743         bt_dev_t *item = NULL;
2744
2745         retv_if(ugd == NULL, FALSE);
2746
2747         EINA_LIST_FOREACH(ugd->paired_device, l, item) {
2748                 if (!item)
2749                         continue;
2750
2751                 if (item->service_list & BT_SC_A2DP_SERVICE_MASK) {
2752                         connected = _bt_is_profile_connected(BT_STEREO_HEADSET_CONNECTED,
2753                                                         ugd->conn, item->bd_addr);
2754
2755                         if (connected)
2756                                 return TRUE;
2757                 }
2758
2759         }
2760
2761         return connected;
2762 }
2763
2764 void _bt_main_retry_connection(void *data, int response)
2765 {
2766         FN_START;
2767
2768         bt_ug_data *ugd = NULL;
2769         bt_dev_t *dev = NULL;
2770
2771         ret_if(data == NULL);
2772
2773         ugd = (bt_ug_data *)data;
2774         dev = _bt_main_get_dev_info(ugd->paired_device, ugd->paired_item);
2775
2776         retm_if(dev == NULL, "dev is NULL\n");
2777
2778         if (response == 0) {
2779                 BT_DBG("Connect with the same device again\n");
2780                 _bt_main_connect_device(ugd, dev);
2781         } else {
2782                 dev->status = BT_IDLE;
2783                 ugd->confirm_req = BT_NONE_REQ;
2784                 elm_genlist_item_update(ugd->paired_item);
2785         }
2786
2787         FN_END;
2788 }
2789
2790 void _bt_main_connect_device(bt_ug_data *ugd, bt_dev_t *dev)
2791 {
2792         FN_START;
2793
2794         int headset_type = BT_AUDIO_PROFILE_TYPE_ALL;
2795
2796         ret_if(ugd == NULL);
2797         ret_if(dev == NULL);
2798
2799         if (ugd->connect_req == TRUE) {
2800                 _bt_main_draw_selection_info(ugd, BT_STR_CONNECTION_FAILED);
2801                 return;
2802         }
2803
2804         if ((dev->service_list & BT_SC_HFP_SERVICE_MASK) ||
2805              (dev->service_list & BT_SC_HSP_SERVICE_MASK)) {
2806                 /* Connect the  Headset */
2807
2808                 if (_bt_main_is_headset_connected(ugd) == TRUE) {
2809                         /* Check if A2DP is connected or not */
2810                         if ((dev->service_list & BT_SC_A2DP_SERVICE_MASK) &&
2811                                 _bt_main_is_stereo_headset_connected(ugd) == TRUE) {
2812                                 _bt_main_draw_selection_info(ugd, BT_STR_CONNECTION_EXISTS);
2813                                 return;
2814                         }
2815
2816                         headset_type = BT_AUDIO_PROFILE_TYPE_A2DP;
2817                 } else {
2818                         headset_type = BT_AUDIO_PROFILE_TYPE_ALL;
2819                 }
2820
2821                 if (bt_audio_connect(dev->addr_str,
2822                                         headset_type) == BT_ERROR_NONE) {
2823                         ugd->connect_req = TRUE;
2824                         dev->status = BT_CONNECTING;
2825                 } else {
2826                         BT_DBG("Fail to connect Headset device");
2827                 }
2828         } else if (dev->service_list & BT_SC_A2DP_SERVICE_MASK) {
2829                 /* Connect the  Stereo Headset */
2830                 if (_bt_main_is_stereo_headset_connected(ugd) == TRUE) {
2831                         _bt_main_draw_selection_info(ugd, BT_STR_CONNECTION_EXISTS);
2832                         return;
2833                 }
2834
2835                 if (bt_audio_connect(dev->addr_str,
2836                                 BT_AUDIO_PROFILE_TYPE_A2DP) == BT_ERROR_NONE) {
2837                         ugd->connect_req = TRUE;
2838                         dev->status = BT_CONNECTING;
2839                 } else {
2840                         BT_DBG("Fail to connect Headset device");
2841                 }
2842         } else if (dev->service_list & BT_SC_HID_SERVICE_MASK) {
2843                 BT_DBG("HID connect request\n");
2844
2845                 if (bt_hid_host_connect(dev->addr_str) == BT_ERROR_NONE) {
2846                         ugd->connect_req = TRUE;
2847                         dev->status = BT_CONNECTING;
2848                 } else {
2849                         BT_DBG("Fail to connect HID device");
2850                 }
2851         }
2852
2853         if (dev->net_profile) {
2854                 if (_bt_connect_net_profile(ugd->connection,
2855                                         dev->net_profile,
2856                                         dev) == BT_UG_ERROR_NONE) {
2857                         ugd->connect_req = TRUE;
2858                         dev->status = BT_CONNECTING;
2859                 } else {
2860                         BT_ERR("Fail to connect the net profile");
2861                 }
2862         }
2863
2864         elm_genlist_item_update((Elm_Object_Item *)dev->genlist_item);
2865
2866         FN_END;
2867 }
2868
2869 void _bt_main_disconnect_device(bt_ug_data *ugd, bt_dev_t *dev)
2870 {
2871         FN_START;
2872
2873         ret_if(ugd == NULL);
2874         ret_if(dev == NULL);
2875
2876         if (ugd->connect_req == TRUE) {
2877                 _bt_main_draw_selection_info(ugd, BT_STR_CONNECTION_FAILED);
2878                 return;
2879         }
2880
2881         if (_bt_is_profile_connected(BT_HEADSET_CONNECTED, ugd->conn,
2882                                                 dev->bd_addr) == TRUE) {
2883                 BT_DBG("Disconnecting AG service \n");
2884                 if (bt_audio_disconnect(dev->addr_str,
2885                                 BT_AUDIO_PROFILE_TYPE_ALL) == BT_ERROR_NONE) {
2886                         ugd->connect_req = TRUE;
2887                         dev->status = BT_DISCONNECTING;
2888                 } else {
2889                         BT_DBG("Fail to connect Headset device");
2890                 }
2891         } else if (_bt_is_profile_connected(BT_STEREO_HEADSET_CONNECTED, ugd->conn,
2892                                                 dev->bd_addr) == TRUE) {
2893                 BT_DBG("Disconnecting AV service \n");
2894                 if (bt_audio_disconnect(dev->addr_str,
2895                                 BT_AUDIO_PROFILE_TYPE_A2DP) == BT_ERROR_NONE) {
2896                         ugd->connect_req = TRUE;
2897                         dev->status = BT_DISCONNECTING;
2898                 } else {
2899                         BT_DBG("Fail to connect Headset device");
2900                 }
2901         } else if (_bt_is_profile_connected(BT_HID_CONNECTED, ugd->conn,
2902                                                 dev->bd_addr) == TRUE) {
2903                 BT_DBG("Disconnecting HID service!!\n");
2904
2905                 if (bt_hid_host_disconnect(dev->addr_str) == BT_ERROR_NONE) {
2906                         dev->status = BT_DISCONNECTING;
2907                 } else {
2908                         BT_DBG("Fail to disconnect HID device");
2909                 }
2910                 return;
2911         }
2912
2913         if (dev->net_profile) {
2914                 if (_bt_disconnect_net_profile(ugd->connection,
2915                                         dev->net_profile,
2916                                         dev) == BT_UG_ERROR_NONE) {
2917                         ugd->connect_req = TRUE;
2918                         dev->status = BT_CONNECTING;
2919                 } else {
2920                         BT_ERR("Fail to connect the net profile");
2921                 }
2922         }
2923
2924         elm_genlist_item_update((Elm_Object_Item *)dev->genlist_item);
2925
2926         FN_END;
2927 }
2928
2929 void _bt_main_change_rotate_mode(void *data)
2930 {
2931         FN_START;
2932
2933         bt_ug_data *ugd = NULL;
2934
2935         ret_if(data == NULL);
2936
2937         ugd = (bt_ug_data *)data;
2938
2939         BT_DBG("rotation: %d", ugd->rotation);
2940
2941         FN_END;
2942 }
2943
2944 void _bt_main_change_connection_status(bool connected, bt_ug_data *ugd,
2945                                        bt_dev_t *dev)
2946 {
2947         FN_START;
2948
2949         retm_if(ugd == NULL, "Invalid argument: ugd is NULL\n");
2950         retm_if(dev == NULL, "Invalid argument: dev is NULL\n");
2951         retm_if(dev->genlist_item == NULL,
2952                 "Invalid argument: genlist_item is NULL\n");
2953
2954         BT_DBG("Connected: %d", connected);
2955
2956         dev->status = BT_IDLE;
2957         dev->connected_mask = connected ? dev->connected_mask : 0x00;
2958
2959         elm_genlist_item_update((Elm_Object_Item *)dev->genlist_item);
2960
2961         FN_END;
2962         return;
2963 }
2964
2965 void _bt_main_draw_popup_menu(Evas_Object *parent, bt_dev_t *dev,
2966                               bt_ug_data *ugd)
2967 {
2968         FN_START;
2969
2970         Evas_Object *menu_list = NULL;
2971         Evas_Object *popup_menu = NULL;
2972         Evas_Object *btn = NULL;
2973         char *name = NULL;
2974
2975         retm_if(parent == NULL, "Invalid argument: parent is NULL\n");
2976         retm_if(dev == NULL, "Invalid argument: ugd is NULL\n");
2977         retm_if(ugd == NULL, "Invalid argument: ugd is NULL\n");
2978         retm_if(ugd->popup_menu != NULL, "Menu popup is displaying\n");
2979
2980         BT_DBG("dev name: %s", dev->name);
2981
2982         name = __bt_main_get_name(dev);
2983
2984         /* create normal popup */
2985         popup_menu =
2986             _bt_create_popup(parent, name, NULL, __bt_main_popup_menu_cb, ugd,
2987                              0);
2988
2989         elm_object_style_set(popup_menu, "menustyle");
2990         btn = elm_button_add(ugd->popup);
2991         elm_object_text_set(btn, BT_STR_BLUETOOTH_CLOSE);
2992         elm_object_part_content_set(ugd->popup, "button1", btn);
2993         evas_object_smart_callback_add(btn, "clicked", __bt_main_popup_menu_cb, ugd);
2994
2995         ugd->popup_menu = popup_menu;
2996
2997         if (name)
2998                 free(name);
2999
3000         /* create the popup menu using genlist */
3001         menu_list = _bt_create_genlist(popup_menu);
3002
3003         evas_object_data_set(menu_list, "dev_info", dev);
3004
3005         /* create inner list for menu */
3006         ugd->popup_menu_itc.item_style = "1text";
3007         ugd->popup_menu_itc.func.text_get = __bt_main_popup_menu_label_get;
3008         ugd->popup_menu_itc.func.content_get = NULL;
3009         ugd->popup_menu_itc.func.state_get = NULL;
3010         ugd->popup_menu_itc.func.del = NULL;
3011
3012         BT_DBG("service list: %x", dev->service_list);
3013         BT_DBG("major clase: %x", dev->major_class);
3014
3015         elm_genlist_item_append(menu_list, &ugd->popup_menu_itc,
3016                                     (void *)BT_CONNECT_MENU, NULL,
3017                                     ELM_GENLIST_ITEM_NONE,
3018                                     __bt_main_select_menu_cb, ugd);
3019
3020         /* set inner list to popup */
3021         elm_object_content_set(popup_menu, menu_list);
3022         ugd->back_cb = (bt_app_back_cb) __bt_main_popup_menu_cb;
3023
3024         FN_END;
3025 }
3026
3027 int _bt_main_request_pairing_with_effect(bt_ug_data *ugd,
3028                                          Elm_Object_Item *seleted_item)
3029 {
3030         FN_START;
3031
3032         bt_dev_t *dev;
3033
3034         retvm_if(ugd == NULL, BT_UG_FAIL, "Invalid argument: ugd is NULL\n");
3035         retvm_if(seleted_item == NULL, BT_UG_FAIL,
3036                  "Invalid argument: object is NULL\n");
3037
3038         dev = _bt_main_get_dev_info(ugd->searched_device, seleted_item);
3039         retvm_if(dev == NULL, BT_UG_FAIL, "Invalid argument: dev is NULL\n");
3040
3041         if (bt_device_create_bond(dev->addr_str) == BT_ERROR_NONE) {
3042                 dev->status = BT_DEV_PAIRING;
3043                 ugd->op_status = BT_PAIRING;
3044
3045                 elm_genlist_item_update(seleted_item);
3046                 elm_object_disabled_set(ugd->scan_btn, EINA_TRUE);
3047         } else {
3048                 return BT_UG_FAIL;
3049         }
3050
3051         FN_END;
3052         return BT_UG_ERROR_NONE;
3053 }
3054
3055 void _bt_main_retry_pairing(void *data, int response)
3056 {
3057         FN_START;
3058
3059         bt_ug_data *ugd = NULL;
3060         bt_dev_t *dev = NULL;
3061
3062         ret_if(data == NULL);
3063
3064         ugd = (bt_ug_data *)data;
3065         dev = _bt_main_get_dev_info(ugd->searched_device, ugd->searched_item);
3066         retm_if(dev == NULL, "dev is NULL\n");
3067
3068         if (response == 0) {
3069                 /* Retry pairing with same device */
3070                 dev->status = BT_DEV_PAIRING;
3071                 elm_genlist_item_update(ugd->searched_item);
3072
3073                 if (ugd->op_status != BT_PAIRING) {
3074                         if (_bt_main_request_pairing_with_effect
3075                             (ugd, ugd->searched_item) != BT_UG_ERROR_NONE)
3076                                 ugd->searched_item = NULL;
3077                 } else
3078                         ugd->searched_item = NULL;
3079         } else {
3080                 dev->status = BT_IDLE;
3081                 ugd->confirm_req = BT_NONE_REQ;
3082                 elm_genlist_item_update(ugd->searched_item);
3083         }
3084
3085         FN_END;
3086 }
3087
3088 void __bt_main_parse_service(bt_ug_data *ugd, service_h service)
3089 {
3090         char *launch_type = NULL;
3091         char *operation = NULL;
3092         const char *file_url = NULL;
3093         const char *file_path = NULL;
3094
3095         ret_if(ugd == NULL);
3096         ret_if(service == NULL);
3097
3098         if (service_get_operation(service, &operation) < 0)
3099                 BT_DBG("Get operation error");
3100
3101         BT_DBG("operation: %s", operation);
3102
3103         if (g_strcmp0(operation, SERVICE_OPERATION_SEND) == 0) {
3104                 launch_type = strdup("send");
3105
3106                 if (service_get_uri(service, (char **)&file_url) < 0)
3107                         BT_DBG("Get uri error");
3108
3109                 if (file_url == NULL)
3110                         goto done;
3111
3112                 file_path = g_filename_from_uri(file_url, NULL, NULL);
3113
3114                 if (file_path == NULL) {
3115                         BT_DBG("Not include URI info");
3116                         file_path = strdup(file_url);
3117                 }
3118
3119                 /* In now, we support only 1 file by AppControl */
3120                 if (service_add_extra_data(service, "filecount", "1") < 0)
3121                         BT_DBG("Fail to add extra data");
3122
3123                 if (service_add_extra_data(service, "files", file_path) < 0)
3124                         BT_DBG("Fail to add extra data");
3125         } else if (service_get_extra_data(service, "launch-type",
3126                                                 &launch_type) < 0) {
3127                 BT_DBG("Get data error");
3128         }
3129
3130 done:
3131         if (launch_type) {
3132                 BT_DBG("Launch with launch type [%s]\n", launch_type);
3133                 _bt_util_set_value(launch_type, &ugd->search_type,
3134                                    &ugd->bt_launch_mode);
3135         } else {
3136                 BT_DBG("launch type is NULL");
3137         }
3138
3139         if (file_url)
3140                 free((void *)file_url);
3141
3142         if (file_path)
3143                 free((void *)file_path);
3144
3145         if (launch_type)
3146                 free((void *)launch_type);
3147
3148 }
3149
3150 void _bt_main_init_status(bt_ug_data *ugd, void *data)
3151 {
3152         FN_START;
3153
3154         service_h service = NULL;
3155         int remain_time = 0;
3156         char *dev_name = NULL;
3157         char phone_name[BT_DEVICE_NAME_LENGTH_MAX + 1];
3158         bool status = false;
3159         bt_adapter_state_e bt_state = BT_ADAPTER_DISABLED;
3160         bt_adapter_visibility_mode_e mode =
3161                                 BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
3162
3163         ret_if(ugd == NULL);
3164
3165         service = data;
3166
3167         if (service != NULL) {
3168                 __bt_main_parse_service(ugd, service);
3169         } else {
3170                 ugd->search_type = MISCELLANEOUS_MAJOR_DEVICE_MASK;
3171                 ugd->bt_launch_mode = BT_LAUNCH_NORMAL;
3172         }
3173
3174         ugd->conn = (void *)dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
3175
3176         if (bt_initialize() != BT_ERROR_NONE)
3177                 BT_ERR("bt_initialize() failed");
3178
3179         if (bt_audio_initialize() != BT_ERROR_NONE)
3180                 BT_ERR("bt_initialize() failed");
3181
3182         if (bt_adapter_get_state(&bt_state) != BT_ERROR_NONE)
3183                 BT_ERR("bt_adapter_get_state() failed.");
3184
3185         if (_bt_create_net_connection(&ugd->connection) != BT_UG_ERROR_NONE)
3186                 BT_ERR("_bt_create_net_connection fail");
3187
3188         if (bt_state == BT_ADAPTER_DISABLED) {
3189                 ugd->op_status = BT_DEACTIVATED;
3190         } else {
3191                 if (bt_adapter_is_discovering(&status) != BT_ERROR_NONE)
3192                         BT_DBG("bt_adapter_get_state() failed.");
3193
3194                 if (status == true)
3195                         bt_adapter_stop_device_discovery();
3196
3197                 ugd->op_status = BT_ACTIVATED;
3198
3199                 /* Get adapter name from bluez */
3200                 bt_adapter_get_name(&dev_name);
3201
3202                 /* Get phone name from vconf */
3203                 _bt_util_get_phone_name(phone_name, BT_DEVICE_NAME_LENGTH_MAX);
3204
3205                 if (g_strcmp0(dev_name, phone_name) != 0) {
3206                         _bt_util_set_phone_name();
3207                 }
3208
3209                 g_free(dev_name);
3210         }
3211
3212         if(bt_adapter_get_visibility(&mode, &remain_time) != BT_ERROR_NONE)
3213                 BT_DBG("bt_adapter_get_visibility() failed.");
3214
3215         if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
3216                 ugd->visible = FALSE;
3217                 ugd->visibility_timeout = 0;
3218         } else if (mode == BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE){
3219                 ugd->visible = TRUE;
3220                 ugd->visibility_timeout = -1;
3221         } else {
3222                 /* BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE */
3223                 /* Need to add the code for getting timeout */
3224                 if (vconf_get_int(BT_VCONF_VISIBLE_TIME,
3225                                 &ugd->visibility_timeout)) {
3226                         BT_DBG("Get the timeout value");
3227                 }
3228
3229                 ugd->remain_time = remain_time;
3230
3231                 if (ugd->remain_time > 0) {
3232                         ugd->timeout_id = g_timeout_add_seconds(1,
3233                                         __bt_main_visible_timeout_cb, ugd);
3234                 } else {
3235                         ugd->visibility_timeout = 0;
3236                 }
3237         }
3238
3239         /* Set event callbacks */
3240         bt_adapter_set_state_changed_cb(_bt_cb_state_changed, (void *)ugd);
3241
3242         if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY) {
3243                 /* Don't need to register callback */
3244                 return;
3245         }
3246
3247         bt_audio_set_connection_state_changed_cb(
3248                                 _bt_cb_audio_state_changed,
3249                                 (void *)ugd);
3250
3251         bt_adapter_set_device_discovery_state_changed_cb(
3252                                         _bt_cb_discovery_state_changed,
3253                                         (void *)ugd);
3254
3255         bt_device_set_bond_created_cb(_bt_cb_bonding_created, (void *)ugd);
3256
3257         bt_device_set_bond_destroyed_cb(_bt_cb_bonding_destroyed, (void *)ugd);
3258
3259         bt_device_set_service_searched_cb(_bt_cb_service_searched, (void *)ugd);
3260
3261         bt_hid_host_initialize(_bt_cb_hid_state_changed, (void *)ugd);
3262
3263         FN_END;
3264 }
3265
3266 bt_dev_t *_bt_main_create_paired_device_item(void *data)
3267 {
3268         FN_START;
3269
3270         int i;
3271         unsigned char bd_addr[BT_ADDRESS_LENGTH_MAX];
3272         bt_dev_t *dev = NULL;
3273         bt_device_info_s *dev_info = NULL;
3274
3275         retv_if(data == NULL, NULL);
3276
3277         dev_info = (bt_device_info_s *)data;
3278
3279         if (strlen(dev_info->remote_name) == 0)
3280                 return NULL;
3281
3282         dev = malloc(sizeof(bt_dev_t));
3283         retv_if(dev == NULL, NULL);
3284
3285         memset(dev, 0, sizeof(bt_dev_t));
3286         strncpy(dev->name, dev_info->remote_name,
3287                 BT_DEVICE_NAME_LENGTH_MAX);
3288
3289         dev->major_class = dev_info->bt_class.major_device_class;
3290         dev->minor_class = dev_info->bt_class.minor_device_class;
3291         dev->service_class = dev_info->bt_class.major_service_class_mask;
3292
3293         if (dev_info->service_uuid != NULL && dev_info->service_count > 0) {
3294                 dev->uuids = g_new0(char *, dev_info->service_count + 1);
3295
3296                 for (i = 0; i < dev_info->service_count; i++) {
3297                         dev->uuids[i] = g_strdup(dev_info->service_uuid[i]);
3298                 }
3299
3300                 dev->uuid_count = dev_info->service_count;
3301         }
3302
3303         _bt_util_addr_string_to_addr_type(bd_addr, dev_info->remote_address);
3304
3305         memcpy(dev->addr_str, dev_info->remote_address,
3306                BT_ADDRESS_STR_LEN);
3307
3308         memcpy(dev->bd_addr, bd_addr, BT_ADDRESS_LENGTH_MAX);
3309
3310         _bt_util_get_service_mask_from_uuid_list(dev_info->service_uuid,
3311                                                  dev_info->service_count,
3312                                                  &dev->service_list);
3313
3314         BT_DBG("device name [%s]", dev->name);
3315         BT_DBG("device major class [%x]", dev->major_class);
3316         BT_DBG("device minor class [%x]", dev->minor_class);
3317         BT_DBG("device service class [%x]", dev->service_class);
3318         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", dev->bd_addr[0],
3319                dev->bd_addr[1], dev->bd_addr[2], dev->bd_addr[3],
3320                dev->bd_addr[4], dev->bd_addr[5]);
3321
3322         FN_END;
3323         return dev;
3324 }
3325
3326 bt_dev_t *_bt_main_create_searched_device_item(void *data)
3327 {
3328         FN_START;
3329
3330         int i;
3331         unsigned char bd_addr[BT_ADDRESS_LENGTH_MAX];
3332         bt_dev_t *dev = NULL;
3333         bt_adapter_device_discovery_info_s *dev_info = NULL;
3334
3335         retv_if(data == NULL, NULL);
3336
3337         dev_info = (bt_adapter_device_discovery_info_s *)data;
3338
3339         if (strlen(dev_info->remote_name) == 0)
3340                 return NULL;
3341
3342         dev = calloc(1, sizeof(bt_dev_t));
3343         retv_if(dev == NULL, NULL);
3344
3345         strncpy(dev->name, dev_info->remote_name,
3346                 BT_DEVICE_NAME_LENGTH_MAX);
3347
3348         dev->major_class = dev_info->bt_class.major_device_class;
3349         dev->minor_class = dev_info->bt_class.minor_device_class;
3350         dev->service_class = dev_info->bt_class.major_service_class_mask;
3351         dev->rssi = dev_info->rssi;
3352
3353         if (dev_info->service_uuid != NULL && dev_info->service_count > 0) {
3354                 dev->uuids = g_new0(char *, dev_info->service_count + 1);
3355
3356                 for (i = 0; i < dev_info->service_count; i++) {
3357                         dev->uuids[i] = g_strdup(dev_info->service_uuid[i]);
3358                 }
3359
3360                 dev->uuid_count = dev_info->service_count;
3361         }
3362
3363         _bt_util_addr_string_to_addr_type(bd_addr, dev_info->remote_address);
3364
3365         memcpy(dev->addr_str, dev_info->remote_address,
3366                BT_ADDRESS_STR_LEN);
3367
3368         memcpy(dev->bd_addr, bd_addr, BT_ADDRESS_LENGTH_MAX);
3369
3370         BT_DBG("device name [%s]", dev->name);
3371         BT_DBG("device major class [%x]", dev->major_class);
3372         BT_DBG("device minor class [%x]", dev->minor_class);
3373         BT_DBG("device service class [%x]", dev->service_class);
3374         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", dev->bd_addr[0],
3375                dev->bd_addr[1], dev->bd_addr[2], dev->bd_addr[3],
3376                dev->bd_addr[4], dev->bd_addr[5]);
3377
3378         FN_END;
3379         return dev;
3380 }
3381
3382 gboolean _bt_main_is_matched_profile(unsigned int search_type,
3383                                  unsigned int major_class,
3384                                  unsigned int service_class,
3385                                  service_h service)
3386 {
3387         FN_START;
3388
3389         bt_device_major_mask_t major_mask = BT_DEVICE_MAJOR_MASK_MISC;
3390
3391         if (search_type == 0x000000)
3392                 return TRUE;
3393
3394         BT_DBG("search_type: %x", search_type);
3395         BT_DBG("service_class: %x", service_class);
3396
3397         /* Check the service_class */
3398         if (service_class & search_type) {
3399                 if (search_type & OBJECT_TRANSFER_MAJOR_SERVICE_MASK &&
3400                      major_class == BT_MAJOR_DEV_CLS_IMAGING) {
3401                         if (__bt_main_is_image_file(service))
3402                                 return TRUE;
3403                 } else {
3404                         return TRUE;
3405                 }
3406         }
3407
3408         /* Check the major class */
3409         switch (major_class) {
3410                 case BT_MAJOR_DEV_CLS_COMPUTER:
3411                         major_mask = BT_DEVICE_MAJOR_MASK_COMPUTER;
3412                         break;
3413                 case BT_MAJOR_DEV_CLS_PHONE:
3414                         major_mask = BT_DEVICE_MAJOR_MASK_PHONE;
3415                         break;
3416                 case BT_MAJOR_DEV_CLS_LAN_ACCESS_POINT:
3417                         major_mask = BT_DEVICE_MAJOR_MASK_LAN_ACCESS_POINT;
3418                         break;
3419                 case BT_MAJOR_DEV_CLS_AUDIO:
3420                         major_mask = BT_DEVICE_MAJOR_MASK_AUDIO;
3421                         break;
3422                 case BT_MAJOR_DEV_CLS_PERIPHERAL:
3423                         major_mask = BT_DEVICE_MAJOR_MASK_PERIPHERAL;
3424                         break;
3425                 case BT_MAJOR_DEV_CLS_IMAGING:
3426                         major_mask = BT_DEVICE_MAJOR_MASK_IMAGING;
3427                         break;
3428                 case BT_MAJOR_DEV_CLS_WEARABLE:
3429                         major_mask = BT_DEVICE_MAJOR_MASK_WEARABLE;
3430                         break;
3431                 case BT_MAJOR_DEV_CLS_TOY:
3432                         major_mask = BT_DEVICE_MAJOR_MASK_TOY;
3433                         break;
3434                 case BT_MAJOR_DEV_CLS_HEALTH:
3435                         major_mask = BT_DEVICE_MAJOR_MASK_HEALTH;
3436                         break;
3437                 default:
3438                         major_mask = BT_DEVICE_MAJOR_MASK_MISC;
3439                         break;
3440         }
3441
3442         BT_DBG("major_mask: %x", major_mask);
3443
3444         if (search_type & major_mask)
3445                 return TRUE;
3446
3447         FN_END;
3448         return FALSE;
3449 }
3450
3451 bt_dev_t *_bt_main_get_dev_info(Eina_List *list,
3452                                 Elm_Object_Item *genlist_item)
3453 {
3454         FN_START;
3455
3456         bt_dev_t *item = NULL;
3457         Eina_List *l = NULL;
3458
3459         retvm_if(list == NULL, NULL, "Invalid argument: list is NULL\n");
3460         retvm_if(genlist_item == NULL, NULL, "Invalid argument: obj is NULL\n");
3461
3462         EINA_LIST_FOREACH(list, l, item) {
3463                 if (item) {
3464                         if (item->genlist_item == genlist_item)
3465                                 return item;
3466                 }
3467         }
3468
3469         FN_END;
3470         return NULL;
3471 }
3472
3473 bt_dev_t *_bt_main_get_dev_info_by_address(Eina_List *list,
3474                                                 char *address)
3475 {
3476         FN_START;
3477
3478         bt_dev_t *item = NULL;
3479         Eina_List *l = NULL;
3480
3481         retvm_if(list == NULL, NULL, "Invalid argument: list is NULL\n");
3482         retvm_if(address == NULL, NULL, "Invalid argument: addr is NULL\n");
3483
3484         EINA_LIST_FOREACH(list, l, item) {
3485                 if (item) {
3486                         if (memcmp(item->addr_str, address, BT_ADDRESS_STR_LEN) == 0)
3487                                 return item;
3488                 }
3489         }
3490
3491         FN_END;
3492         return NULL;
3493 }
3494
3495 int _bt_main_check_and_update_device(Eina_List *list, char *addr,
3496                                      char *name)
3497 {
3498         FN_START;
3499
3500         bt_dev_t *item = NULL;
3501         Eina_List *l = NULL;
3502
3503         retv_if(list == NULL, -1);
3504         retv_if(addr == NULL, -1);
3505         retv_if(name == NULL, -1);
3506
3507         EINA_LIST_FOREACH(list, l, item) {
3508                 if (item) {
3509                         if (memcmp(item->addr_str, addr, BT_ADDRESS_STR_LEN) == 0) {
3510                                 memset(item->name, 0x00,
3511                                        BT_DEVICE_NAME_LENGTH_MAX);
3512                                 memcpy(item->name, name,
3513                                        BT_DEVICE_NAME_LENGTH_MAX);
3514                                 return 0;
3515                         }
3516                 }
3517         }
3518
3519         FN_END;
3520
3521         return -1;
3522 }
3523
3524 void _bt_main_launch_syspopup(void *data, char *event_type, char *title,
3525                               char *type)
3526 {
3527         FN_START;
3528
3529         int ret = 0;
3530         bt_ug_data *ugd = NULL;
3531         bundle *b = NULL;
3532
3533         ret_if(event_type == NULL);
3534         ret_if(type == NULL);
3535
3536         ugd = (bt_ug_data *)data;
3537
3538         _bt_ipc_register_popup_event_signal(ugd->EDBusHandle, data);
3539
3540         b = bundle_create();
3541         ret_if(b == NULL);
3542
3543         bundle_add(b, "event-type", event_type);
3544         bundle_add(b, "title", title);
3545         bundle_add(b, "type", type);
3546
3547         ret = syspopup_launch("bt-syspopup", b);
3548         if (0 > ret) {
3549                 BT_DBG("Popup launch failed...retry %d \n", ret);
3550                 ugd->popup_bundle = b;
3551                 ugd->popup_timer = g_timeout_add(BT_UG_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS,
3552                               (GSourceFunc) __bt_main_system_popup_timer_cb, ugd);
3553         } else {
3554                 bundle_free(b);
3555         }
3556         FN_END;
3557 }
3558
3559 void _bt_main_create_information_popup(bt_ug_data *ugd, char *msg) {
3560         FN_START;
3561         ret_if(ugd == NULL);
3562
3563         if (ugd->popup) {
3564                 evas_object_del(ugd->popup);
3565                 ugd->popup = NULL;
3566         }
3567
3568         ugd->popup = _bt_create_popup(ugd->win_main,
3569                                 BT_STR_INFORMATION,
3570                                 msg,
3571                                 _bt_main_popup_del_cb,
3572                                 ugd, 2);
3573         FN_END;
3574 }
3575
3576 void _bt_main_add_searched_title(bt_ug_data *ugd)
3577 {
3578         Elm_Object_Item *git;
3579
3580         git = elm_genlist_item_append(ugd->main_genlist,
3581                                     ugd->sp_itc,
3582                                     (void *)ugd, NULL,
3583                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
3584         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
3585         ugd->searched_padding = git;
3586
3587         git = elm_genlist_item_append(ugd->main_genlist,
3588                         ugd->searched_title_itc,
3589                         (void *)ugd, NULL,
3590                         ELM_GENLIST_ITEM_NONE, NULL, NULL);
3591
3592         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
3593         ugd->searched_title = git;
3594 }
3595
3596 void _bt_update_paired_item_style(bt_ug_data *ugd)
3597 {
3598         int i = 0;
3599         bt_dev_t *item_dev = NULL;
3600         Elm_Object_Item *item = NULL;
3601         Elm_Object_Item *next = NULL;
3602
3603         ret_if(ugd == NULL);
3604
3605         item = elm_genlist_item_next_get(ugd->paired_title);
3606
3607         while (item != NULL && item != ugd->searched_padding) {
3608                 item_dev = _bt_main_get_dev_info(ugd->paired_device, item);
3609                 ret_if(item_dev == NULL);
3610
3611                 next = elm_genlist_item_next_get(item);
3612                 if (next == NULL || next == ugd->searched_padding) {
3613                         if (i == 0) {
3614                                 item_dev->item_type = BT_ITEM_NO_TYPE;
3615                         } else {
3616                                 item_dev->item_type = BT_ITEM_BOTTOM;
3617                         }
3618                         break;
3619                 }
3620
3621                 if (i == 0) {
3622                         item_dev->item_type = BT_ITEM_TOP;
3623                 } else {
3624                         item_dev->item_type = BT_ITEM_CENTER;
3625                 }
3626
3627                 item = next;
3628                 i++;
3629         }
3630
3631         elm_genlist_realized_items_update(ugd->main_genlist);
3632 }
3633
3634 void _bt_update_searched_item_style(bt_ug_data *ugd)
3635 {
3636         int i = 0;
3637         bt_dev_t *item_dev = NULL;
3638         Elm_Object_Item *item = NULL;
3639         Elm_Object_Item *next = NULL;
3640
3641         ret_if(ugd == NULL);
3642
3643         item = elm_genlist_item_next_get(ugd->searched_title);
3644
3645         while (item != NULL) {
3646                 item_dev = _bt_main_get_dev_info(ugd->searched_device, item);
3647                 ret_if(item_dev == NULL);
3648
3649                 next = elm_genlist_item_next_get(item);
3650                 if (next == NULL) {
3651                         if (i == 0) {
3652                                 item_dev->item_type = BT_ITEM_NO_TYPE;
3653                         } else {
3654                                 item_dev->item_type = BT_ITEM_BOTTOM;
3655                         }
3656                         break;
3657                 }
3658
3659                 if (i == 0) {
3660                         item_dev->item_type = BT_ITEM_TOP;
3661                 } else {
3662                         item_dev->item_type = BT_ITEM_CENTER;
3663                 }
3664
3665                 item = next;
3666                 i++;
3667         }
3668
3669         elm_genlist_realized_items_update(ugd->main_genlist);
3670 }
3671