4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <dbus/dbus-glib.h>
29 #include <sys/types.h>
35 #include <TapiUtility.h>
38 #include "bluetooth_pb_agent.h"
39 #include "bluetooth_pb_vcard.h"
41 #define BLUETOOTH_PB_AGENT_TIMEOUT 600
43 static gchar *bluetooth_pb_agent_folder_list[] = {
67 TapiHandle *tapi_handle;
70 GHashTable *contact_list;
74 PhoneBookType pb_type;
80 void (*clear) (BluetoothPbAgent *agent);
81 } BluetoothPbAgentClass;
88 GType bluetooth_pb_agent_get_type(void);
90 #define BLUETOOTH_PB_TYPE_AGENT (bluetooth_pb_agent_get_type())
92 #define BLUETOOTH_PB_AGENT(object) \
93 (G_TYPE_CHECK_INSTANCE_CAST((object), \
94 BLUETOOTH_PB_TYPE_AGENT , BluetoothPbAgent))
95 #define BLUETOOTH_PB_AGENT_CLASS(klass) \
96 (G_TYPE_CHECK_CLASS_CAST((klass), \
97 BLUETOOTH_PB_TYPE_AGENT , BluetoothPbAgentClass))
98 #define BLUETOOTH_IS_PB_AGENT(object) \
99 (G_TYPE_CHECK_INSTANCE_TYPE((object), \
100 BLUETOOTH_PB_TYPE_AGENT))
101 #define BLUETOOTH_IS_PB_AGENT_CLASS(klass) \
102 (G_TYPE_CHECK_CLASS_TYPE((klass), \
103 BLUETOOTH_PB_TYPE_AGENT))
104 #define BLUETOOTH_PB_AGENT_GET_CLASS(obj) \
105 (G_TYPE_INSTANCE_GET_CLASS((obj), \
106 BLUETOOTH_PB_TYPE_AGENT , BluetoothPbAgentClass))
108 G_DEFINE_TYPE(BluetoothPbAgent, bluetooth_pb_agent, G_TYPE_OBJECT)
110 #define DBUS_STRUCT_STRING_STRING_UINT (dbus_g_type_get_struct("GValueArray", G_TYPE_STRING, \
111 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID))
113 static guint signals[LAST_SIGNAL] = { 0 };
115 static GMainLoop *mainloop = NULL;
117 static void bluetooth_pb_agent_finalize(GObject *obj);
119 static void bluetooth_pb_agent_clear(BluetoothPbAgent *agent);
122 static gboolean bluetooth_pb_get_phonebook_folder_list(BluetoothPbAgent *agent,
123 const gchar ***folder_list,
126 static gboolean bluetooth_pb_get_phonebook(BluetoothPbAgent *agent,
130 guint16 max_list_count,
131 guint16 list_start_offset,
132 DBusGMethodInvocation *context);
134 static gboolean bluetooth_pb_get_phonebook_size(BluetoothPbAgent *agent,
136 DBusGMethodInvocation *context);
138 static gboolean bluetooth_pb_get_phonebook_list(BluetoothPbAgent *agent,
140 DBusGMethodInvocation *context);
142 static gboolean bluetooth_pb_get_phonebook_entry(BluetoothPbAgent *agent,
147 DBusGMethodInvocation *context);
149 static gboolean bluetooth_pb_get_phonebook_size_at(BluetoothPbAgent *agent,
150 const gchar *command,
151 DBusGMethodInvocation *context);
153 static gboolean bluetooth_pb_get_phonebook_entries_at(BluetoothPbAgent *agent,
154 const gchar *command,
157 DBusGMethodInvocation *context);
159 static gboolean bluetooth_pb_get_phonebook_entries_find_at(BluetoothPbAgent *agent,
160 const gchar *command,
161 const gchar *find_text,
162 DBusGMethodInvocation *context);
164 static gboolean bluetooth_pb_get_total_object_count(BluetoothPbAgent *agent,
166 DBusGMethodInvocation *context);
168 static gboolean bluetooth_pb_add_contact (BluetoothPbAgent *agent,
169 const char *filename,
172 static gboolean bluetooth_pb_destroy_agent(BluetoothPbAgent *agent,
173 DBusGMethodInvocation *context);
175 static void __bluetooth_pb_dbus_return_error(DBusGMethodInvocation *context,
177 const gchar *message);
179 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name);
181 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name);
183 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
187 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match,
190 static contacts_query_h __bluetooth_pb_query_person(void);
192 static contacts_query_h __bluetooth_pb_query_person_number(void);
194 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void);
196 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void);
198 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void);
200 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void);
202 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
205 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count);
207 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record);
209 static void __bluetooth_pb_get_vcards(BluetoothPbAgent *agent,
210 PhoneBookType pb_type,
213 guint16 max_list_count,
214 guint16 list_start_offset,
217 static void __bluetooth_pb_get_contact_list(BluetoothPbAgent *agent,
218 contacts_query_h query,
219 GPtrArray *ptr_array);
221 static void __bluetooth_pb_get_phone_log_list(BluetoothPbAgent *agent,
222 contacts_query_h query,
223 GPtrArray *ptr_array);
225 static void __bluetooth_pb_get_list(BluetoothPbAgent *agent,
226 PhoneBookType pb_type,
227 GPtrArray *ptr_array);
229 static void __bluetooth_pb_get_contact_list_number(BluetoothPbAgent *agent,
230 contacts_query_h query,
233 GPtrArray *ptr_array);
235 static void __bluetooth_pb_get_phone_log_list_number(BluetoothPbAgent *agent,
236 contacts_query_h query,
239 GPtrArray *ptr_array);
241 static void __bluetooth_pb_get_list_number(BluetoothPbAgent *agent,
242 PhoneBookType pb_type,
245 GPtrArray *ptr_array);
247 static void __bluetooth_pb_get_contact_list_name(BluetoothPbAgent *agent,
248 contacts_query_h query,
249 const gchar *find_text,
250 GPtrArray *ptr_array);
252 static void __bluetooth_pb_get_phone_log_list_name(BluetoothPbAgent *agent,
253 contacts_query_h query,
254 const gchar *find_text,
255 GPtrArray *ptr_array);
257 static void __bluetooth_pb_get_list_name(BluetoothPbAgent *agent,
258 PhoneBookType pb_type,
259 const gchar *find_text,
260 GPtrArray *ptr_array);
262 static void __bluetooth_pb_list_hash_reset(BluetoothPbAgent *agent);
264 static gboolean __bluetooth_pb_list_hash_insert(BluetoothPbAgent *agent,
268 static gint __bluetooth_pb_list_hash_lookup_id(BluetoothPbAgent *agent,
271 static void __bluetooth_pb_list_ptr_array_add(GPtrArray *ptr_array,
276 static void __bluetooth_pb_list_ptr_array_free(gpointer data);
278 static void __bluetooth_pb_agent_signal_handler(int signum);
280 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
283 static void __bluetooth_pb_agent_timeout_add_seconds(BluetoothPbAgent *agent);
285 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data);
287 static void __bluetooth_pb_tel_callback(TapiHandle *handle,
292 static void __bluetooth_pb_agent_dbus_init(BluetoothPbAgent *agent);
294 #include "bluetooth_pb_agent_glue.h"
296 static void bluetooth_pb_agent_init(BluetoothPbAgent *agent)
301 agent->tapi_handle = NULL;
302 agent->tel_number = NULL;
304 agent->contact_list = NULL;
305 agent->timeout_id = 0;
307 agent->pb_type = TELECOM_NONE;
310 static void bluetooth_pb_agent_class_init(BluetoothPbAgentClass *klass)
312 GObjectClass *object_class = (GObjectClass *) klass;
314 klass->clear = bluetooth_pb_agent_clear;
316 object_class->finalize = bluetooth_pb_agent_finalize;
318 signals[CLEAR] = g_signal_new("clear",
319 G_TYPE_FROM_CLASS(klass),
321 G_STRUCT_OFFSET(BluetoothPbAgentClass, clear),
323 g_cclosure_marshal_VOID__VOID,
326 dbus_g_object_type_install_info(BLUETOOTH_PB_TYPE_AGENT,
327 &dbus_glib_bluetooth_pb_object_info);
330 static void bluetooth_pb_agent_finalize(GObject *obj)
332 BluetoothPbAgent *agent = BLUETOOTH_PB_AGENT(obj);
336 if (agent->tapi_handle) {
337 tel_deinit(agent->tapi_handle);
338 agent->tapi_handle = NULL;
341 if (agent->tel_number) {
342 g_free(agent->tel_number);
343 agent->tel_number = NULL;
346 if(agent->timeout_id) {
347 g_source_remove(agent->timeout_id);
348 agent->timeout_id = 0;
351 if (agent->contact_list) {
352 g_hash_table_destroy(agent->contact_list);
353 agent->contact_list = NULL;
357 g_object_unref(agent->proxy);
362 dbus_g_connection_unref(agent->bus);
367 G_OBJECT_CLASS(bluetooth_pb_agent_parent_class)->finalize(obj);
370 static void bluetooth_pb_agent_clear(BluetoothPbAgent *agent)
374 if (agent->contact_list) {
375 g_hash_table_destroy(agent->contact_list);
376 agent->contact_list = NULL;
379 agent->pb_type = TELECOM_NONE;
382 static gboolean bluetooth_pb_get_phonebook_folder_list(BluetoothPbAgent *agent,
383 const gchar ***folder_list,
390 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list);
391 folder = g_new0(gchar *, size);
393 for (i = 0; i < size; i++)
394 folder[i] = g_strdup(bluetooth_pb_agent_folder_list[i]);
396 *folder_list = (const gchar **)folder;
401 static gboolean bluetooth_pb_get_phonebook(BluetoothPbAgent *agent,
405 guint16 max_list_count,
406 guint16 list_start_offset,
407 DBusGMethodInvocation *context)
409 PhoneBookType pb_type = TELECOM_NONE;
410 GPtrArray *vcards = NULL;
411 gchar **vcards_str = NULL;
413 guint new_missed_call = 0;
415 DBG("name: %s filter: %lld format: %d max_list_count: %d list_start_offset: %d\n",
416 name, filter, format, max_list_count, list_start_offset);
418 __bluetooth_pb_agent_timeout_add_seconds(agent);
420 pb_type = __bluetooth_pb_get_pb_type(name);
422 if (pb_type == TELECOM_NONE) {
423 __bluetooth_pb_dbus_return_error(context,
425 "unsupported name defined");
429 vcards = g_ptr_array_new();
431 if (max_list_count > 0) {
432 __bluetooth_pb_get_vcards(agent, pb_type,
434 max_list_count, list_start_offset,
439 g_ptr_array_add(vcards, NULL);
441 vcards_str = (gchar **) g_ptr_array_free(vcards, FALSE);
443 dbus_g_method_return(context, vcards_str, new_missed_call);
445 g_strfreev(vcards_str);
450 static gboolean bluetooth_pb_get_phonebook_size(BluetoothPbAgent *agent,
452 DBusGMethodInvocation *context)
454 PhoneBookType pb_type = TELECOM_NONE;
456 guint new_missed_call = 0;
459 DBG("name: %s\n", name);
461 __bluetooth_pb_agent_timeout_add_seconds(agent);
463 pb_type = __bluetooth_pb_get_pb_type(name);
465 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
466 __bluetooth_pb_dbus_return_error(context,
468 "unsupported name defined");
473 if (pb_type == TELECOM_PB)
476 __bluetooth_pb_get_count_new_missed_call(&new_missed_call);
478 dbus_g_method_return(context, count, new_missed_call);
484 static gboolean bluetooth_pb_get_phonebook_list(BluetoothPbAgent *agent,
486 DBusGMethodInvocation *context)
488 PhoneBookType pb_type = TELECOM_NONE;
490 GPtrArray *ptr_array;
492 DBG("name: %s\n", name);
494 __bluetooth_pb_agent_timeout_add_seconds(agent);
496 pb_type = __bluetooth_pb_get_pb_type(name);
498 if (pb_type == TELECOM_NONE) {
499 __bluetooth_pb_dbus_return_error(context,
501 "unsupported name defined");
505 ptr_array = g_ptr_array_new_with_free_func(__bluetooth_pb_list_ptr_array_free);
507 __bluetooth_pb_get_list(agent, pb_type, ptr_array);
509 dbus_g_method_return(context, ptr_array);
512 g_ptr_array_free(ptr_array, TRUE);
518 static gboolean bluetooth_pb_get_phonebook_entry(BluetoothPbAgent *agent,
523 DBusGMethodInvocation *context)
525 PhoneBookType pb_type = TELECOM_NONE;
531 const gchar *attr = NULL;
533 DBG("folder: %s id: %s filter: %ld format: %d\n",
534 folder, id, filter, format);
536 __bluetooth_pb_agent_timeout_add_seconds(agent);
538 if (!g_str_has_suffix(id, ".vcf")) {
539 __bluetooth_pb_dbus_return_error(context,
545 handle = (gint)g_ascii_strtoll(id, NULL, 10);
547 pb_type = __bluetooth_pb_get_pb_type(folder);
549 if (pb_type == TELECOM_NONE) {
550 __bluetooth_pb_dbus_return_error(context,
552 "unsupported name defined");
556 /* create index cache */
557 __bluetooth_pb_get_list(agent, pb_type, NULL);
559 cid = __bluetooth_pb_list_hash_lookup_id(agent, handle);
564 str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
567 str = _bluetooth_pb_vcard_contact(cid, filter, format);
571 str = _bluetooth_pb_vcard_call(cid, filter, format, "RECEIVED");
574 str = _bluetooth_pb_vcard_call(cid, filter, format, "DIALED");
577 str = _bluetooth_pb_vcard_call(cid, filter, format, "MISSED");
580 contacts_record_h record = NULL;
584 status = contacts_db_get_record(_contacts_phone_log._uri,
587 if (status != CONTACTS_ERROR_NONE)
590 attr = __bluetooth_pb_phone_log_get_log_type(record);
591 str = _bluetooth_pb_vcard_call(cid, filter, format, attr);
593 contacts_record_destroy(record, TRUE);
597 __bluetooth_pb_dbus_return_error(context,
599 "unsupported name defined");
603 dbus_g_method_return(context, str);
609 static gboolean bluetooth_pb_get_phonebook_size_at(BluetoothPbAgent *agent,
610 const gchar *command,
611 DBusGMethodInvocation *context)
613 PhoneBookType pb_type = TELECOM_NONE;
616 DBG("command: %s\n", command);
618 __bluetooth_pb_agent_timeout_add_seconds(agent);
620 pb_type = __bluetooth_pb_get_storage_pb_type(command);
622 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
623 __bluetooth_pb_dbus_return_error(context,
625 "unsupported name defined");
629 dbus_g_method_return(context, count);
634 static gboolean bluetooth_pb_get_phonebook_entries_at(BluetoothPbAgent *agent,
635 const gchar *command,
638 DBusGMethodInvocation *context)
640 PhoneBookType pb_type = TELECOM_NONE;
642 GPtrArray *ptr_array = NULL;
644 DBG("command: %s, start_index: %d, end_index: %d\n",
645 command, start_index, end_index);
647 __bluetooth_pb_agent_timeout_add_seconds(agent);
649 pb_type = __bluetooth_pb_get_storage_pb_type(command);
651 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
652 __bluetooth_pb_dbus_return_error(context,
654 "unsupported name defined");
658 ptr_array = g_ptr_array_new_with_free_func(__bluetooth_pb_list_ptr_array_free);
660 __bluetooth_pb_get_list_number(agent, pb_type,
661 start_index, end_index,
664 dbus_g_method_return(context, ptr_array);
667 g_ptr_array_free(ptr_array, TRUE);
672 static gboolean bluetooth_pb_get_phonebook_entries_find_at(BluetoothPbAgent *agent,
673 const gchar *command,
674 const gchar *find_text,
675 DBusGMethodInvocation *context)
677 PhoneBookType pb_type = TELECOM_NONE;
679 GPtrArray *ptr_array = NULL;
681 DBG("command: %s, find text: %s\n", command, find_text);
683 __bluetooth_pb_agent_timeout_add_seconds(agent);
685 pb_type = __bluetooth_pb_get_storage_pb_type(command);
687 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
688 __bluetooth_pb_dbus_return_error(context,
690 "unsupported name defined");
694 ptr_array = g_ptr_array_new_with_free_func(__bluetooth_pb_list_ptr_array_free);
696 __bluetooth_pb_get_list_name(agent, pb_type,
697 find_text, ptr_array);
699 dbus_g_method_return(context, ptr_array);
702 g_ptr_array_free(ptr_array, TRUE);
707 static gboolean bluetooth_pb_get_total_object_count(BluetoothPbAgent *agent,
708 gchar *path, DBusGMethodInvocation *context)
711 PhoneBookType pb_type = TELECOM_NONE;
713 DBG("%s() %d\n", __FUNCTION__, __LINE__);
715 __bluetooth_pb_agent_timeout_add_seconds(agent);
717 pb_type = __bluetooth_pb_get_storage_pb_type(path);
719 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
720 __bluetooth_pb_dbus_return_error(context,
722 "unsupported name defined");
726 dbus_g_method_return(context, count);
728 DBG("%s() %d\n", __FUNCTION__, __LINE__);
735 static int __bluetooth_pb_agent_read_file(const char *file_path, char **stream)
739 int received_file_size = 0;
740 struct stat file_attr;
742 if (file_path == NULL || stream == NULL) {
743 DBG("Invalid data \n");
747 DBG("file_path = %s\n", file_path);
749 if ((fp = fopen(file_path, "r+")) == NULL) {
750 DBG("Cannot open %s\n", file_path);
754 if (fstat(fileno(fp), &file_attr) == 0) {
755 received_file_size = file_attr.st_size;
756 DBG("file_attr.st_size = %d, size = %d\n", file_attr.st_size, received_file_size);
758 if (received_file_size <= 0) {
759 DBG("Some problem in the file size [%s] \n", file_path);
765 *stream = (char *)malloc(sizeof(char) * received_file_size);
766 if (NULL == *stream) {
772 DBG("Some problem in the file [%s] \n", file_path);
778 read_len = fread(*stream, 1, received_file_size, fp);
785 DBG("Cannot open %s\n", file_path);
797 static gboolean bluetooth_pb_add_contact(BluetoothPbAgent *agent, const char *filename,
800 /* Contact API is changed, Temporary blocked */
802 CTSstruct *contact_record = NULL;
803 GSList *numbers_list = NULL, *cursor;
805 int is_duplicated = 0;
809 DBG("file_path = %s\n", filename);
811 err = contacts_svc_connect();
812 DBG("contact_db_service_connect fucntion call [error] = %d \n", err);
814 err = __bluetooth_pb_agent_read_file(filename, &stream);
817 contacts_svc_disconnect();
818 DBG("contacts_svc_disconnect fucntion call [error] = %d \n", err);
820 if (NULL != stream) {
827 is_success = contacts_svc_get_contact_from_vcard((const void *)stream, &contact_record);
829 DBG("contacts_svc_get_contact_from_vcard fucntion call [is_success] = %d \n", is_success);
831 if (0 == is_success) {
832 contacts_svc_struct_get_list(contact_record, CTS_CF_NUMBER_LIST, &numbers_list);
833 cursor = numbers_list;
835 for (; cursor; cursor = g_slist_next(cursor)) {
836 if (contacts_svc_find_contact_by(CTS_FIND_BY_NUMBER,
837 contacts_svc_value_get_str(cursor->data,
838 CTS_NUM_VAL_NUMBER_STR)) > 0) {
839 DBG("is_duplicated\n");
840 is_duplicated = TRUE;
844 if (is_duplicated == FALSE) {
845 contacts_svc_insert_contact(0, contact_record);
851 err = contacts_svc_disconnect();
852 DBG("contacts_svc_disconnect fucntion call [error] = %d \n", err);
854 if (NULL != stream) {
863 static void __bluetooth_pb_dbus_return_error(DBusGMethodInvocation *context,
865 const gchar *message)
868 GError *error = NULL;
870 quark = g_type_qname(bluetooth_pb_agent_get_type());
871 error = g_error_new_literal(quark, code, message);
873 DBG("%s\n", message);
875 dbus_g_method_return_error(context, error);
879 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name)
881 gchar *suffix = ".vcf";
891 if (g_str_has_suffix(name, suffix))
892 len -= strlen(suffix);
894 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list) - 1;
895 for (i = 0; i < size; i++) {
896 if (strncmp(name, bluetooth_pb_agent_folder_list[i], len) == 0)
903 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name)
908 if (g_strcmp0(name, "\"ME\"") == 0 )
911 if (g_strcmp0(name, "\"RC\"") == 0)
914 if (g_strcmp0(name, "\"DC\"") == 0)
917 if (g_strcmp0(name, "\"MC\"") == 0)
923 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
930 for (i = 0; i < size; i++) {
933 status = contacts_filter_add_operator(filter,
934 CONTACTS_FILTER_OPERATOR_OR);
936 if (status != CONTACTS_ERROR_NONE)
940 status = contacts_filter_add_int(filter,
941 _contacts_phone_log.log_type,
942 CONTACTS_MATCH_EQUAL,
945 if (status != CONTACTS_ERROR_NONE)
949 return CONTACTS_ERROR_NONE;
952 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match,
955 contacts_query_h query = NULL;
956 contacts_filter_h filter = NULL;
960 status = contacts_query_create(_contacts_phone_log._uri,
963 if (status != CONTACTS_ERROR_NONE)
966 status = contacts_filter_create(_contacts_phone_log._uri, &filter);
968 if (status != CONTACTS_ERROR_NONE) {
969 contacts_query_destroy(query);
973 status = __bluetooth_pb_phone_log_filter_append(filter, match, size);
975 if (status != CONTACTS_ERROR_NONE) {
976 contacts_filter_destroy(filter);
977 contacts_query_destroy(query);
981 status = contacts_query_set_filter(query, filter);
983 if (status != CONTACTS_ERROR_NONE) {
984 contacts_filter_destroy(filter);
985 contacts_query_destroy(query);
989 status = contacts_query_set_sort(query,
990 _contacts_phone_log.log_time,
993 if (status != CONTACTS_ERROR_NONE) {
994 contacts_filter_destroy(filter);
995 contacts_query_destroy(query);
999 contacts_filter_destroy(filter);
1004 static contacts_query_h __bluetooth_pb_query_person(void)
1006 contacts_query_h query = NULL;
1010 status = contacts_query_create(_contacts_person._uri,
1013 if (status != CONTACTS_ERROR_NONE)
1019 static contacts_query_h __bluetooth_pb_query_person_number(void)
1021 contacts_query_h query = NULL;
1025 status = contacts_query_create(_contacts_person_number._uri,
1028 if (status != CONTACTS_ERROR_NONE)
1034 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void)
1038 CONTACTS_PLOG_TYPE_VOICE_INCOMMING,
1039 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING
1042 return __bluetooth_pb_query_phone_log(match, size);
1045 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void)
1049 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1050 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING
1053 return __bluetooth_pb_query_phone_log(match, size);
1057 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void)
1061 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN,
1062 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN,
1063 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN,
1064 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN
1067 return __bluetooth_pb_query_phone_log(match, size);
1070 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void)
1074 CONTACTS_PLOG_TYPE_VOICE_INCOMMING,
1075 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING,
1076 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1077 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING,
1078 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN,
1079 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN,
1080 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN,
1081 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN
1084 return __bluetooth_pb_query_phone_log(match, size);
1087 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
1090 contacts_query_h query = NULL;
1097 query = __bluetooth_pb_query_person();
1100 query = __bluetooth_pb_query_phone_log_incoming();
1103 query = __bluetooth_pb_query_phone_log_outgoing();
1106 query = __bluetooth_pb_query_phone_log_missed();
1109 query = __bluetooth_pb_query_phone_log_combined();
1118 status = contacts_db_get_count_with_query(query, &signed_count);
1120 if (status != CONTACTS_ERROR_NONE) {
1121 contacts_query_destroy(query);
1125 contacts_query_destroy(query);
1127 if (signed_count < 0)
1130 *count = (gint) signed_count;
1135 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count)
1137 contacts_query_h query = NULL;
1144 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN,
1145 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN
1148 query = __bluetooth_pb_query_phone_log(match, size);
1153 status = contacts_db_get_count_with_query(query, &signed_count);
1155 if (status != CONTACTS_ERROR_NONE) {
1156 contacts_query_destroy(query);
1160 contacts_query_destroy(query);
1162 if (signed_count < 0)
1165 *count = (guint)signed_count;
1170 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record)
1175 status = contacts_record_get_int(record,
1176 _contacts_phone_log.log_type,
1179 if (status != CONTACTS_ERROR_NONE)
1183 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING:
1184 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING:
1186 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
1187 case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
1189 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN:
1190 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN:
1191 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN:
1192 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN:
1199 static void __bluetooth_pb_get_vcards(BluetoothPbAgent *agent,
1200 PhoneBookType pb_type,
1203 guint16 max_list_count,
1204 guint16 list_start_offset,
1207 contacts_list_h record_list = NULL;
1208 contacts_query_h query = NULL;
1215 guint property_id = 0;
1217 const char *attr = NULL;
1219 gboolean get_log = FALSE;
1221 /* contact offset is n - 1 of PBAP */
1222 offset = (gint)list_start_offset - 1;
1224 if ( max_list_count >= 65535)
1225 limit = -1; /* contact limit -1 means unrestricted */
1227 limit = (gint)max_list_count;
1232 if (list_start_offset == 0) {
1235 vcard = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
1238 g_ptr_array_add(vcards, vcard);
1246 query = __bluetooth_pb_query_person();
1247 property_id = _contacts_person.id;
1250 query = __bluetooth_pb_query_phone_log_incoming();
1251 property_id = _contacts_phone_log.id;
1255 query = __bluetooth_pb_query_phone_log_outgoing();
1256 property_id = _contacts_phone_log.id;
1260 query = __bluetooth_pb_query_phone_log_missed();
1261 property_id = _contacts_phone_log.id;
1265 query = __bluetooth_pb_query_phone_log_combined();
1266 property_id = _contacts_phone_log.id;
1273 status = contacts_db_get_records_with_query(query, offset, limit, &record_list);
1275 if (status != CONTACTS_ERROR_NONE) {
1276 contacts_list_destroy(record_list, TRUE);
1277 contacts_query_destroy(query);
1281 status = contacts_list_first(record_list);
1283 if (status != CONTACTS_ERROR_NONE) {
1284 contacts_list_destroy(record_list, TRUE);
1285 contacts_query_destroy(query);
1290 contacts_record_h record;
1294 gchar *vcard = NULL;
1297 status = contacts_list_get_current_record_p(record_list, &record);
1299 if (status != CONTACTS_ERROR_NONE)
1302 status = contacts_record_get_int(record, property_id, &id);
1304 if (status != CONTACTS_ERROR_NONE)
1307 if (property_id == _contacts_person.id)
1308 vcard = _bluetooth_pb_vcard_contact(id, filter, format);
1311 attr = __bluetooth_pb_phone_log_get_log_type(record);
1313 vcard = _bluetooth_pb_vcard_call(id, filter, format, attr);
1317 g_ptr_array_add(vcards, vcard);
1319 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1321 contacts_list_destroy(record_list, TRUE);
1322 contacts_query_destroy(query);
1325 static void __bluetooth_pb_get_contact_list(BluetoothPbAgent *agent,
1326 contacts_query_h query,
1327 GPtrArray *ptr_array)
1329 contacts_list_h record_list = NULL;
1339 tmp = _bluetooth_pb_owner_name();
1340 name = g_strdup_printf("%s;;;;", tmp);
1343 __bluetooth_pb_list_ptr_array_add(ptr_array,
1344 name, agent->tel_number, 0);
1349 status = contacts_db_get_records_with_query(query,
1350 -1, -1, &record_list);
1352 if (status != CONTACTS_ERROR_NONE) {
1353 contacts_list_destroy(record_list, TRUE);
1357 status = contacts_list_first(record_list);
1359 if (status != CONTACTS_ERROR_NONE) {
1360 contacts_list_destroy(record_list, TRUE);
1361 contacts_query_destroy(query);
1365 __bluetooth_pb_list_hash_reset(agent);
1368 contacts_record_h record;
1373 status = contacts_list_get_current_record_p(record_list,
1376 if (status != CONTACTS_ERROR_NONE)
1380 status = contacts_record_get_int(record,
1381 _contacts_person.id,
1384 if (status != CONTACTS_ERROR_NONE)
1387 __bluetooth_pb_list_hash_insert(agent, i, id);
1394 name = _bluetooth_pb_name_from_person_id(id);
1395 number = _bluetooth_pb_number_from_person_id(id);
1397 __bluetooth_pb_list_ptr_array_add(ptr_array,
1406 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1408 contacts_list_destroy(record_list, TRUE);
1411 static void __bluetooth_pb_get_phone_log_list(BluetoothPbAgent *agent,
1412 contacts_query_h query,
1413 GPtrArray *ptr_array)
1415 contacts_list_h record_list = NULL;
1420 status = contacts_db_get_records_with_query(query,
1421 -1, -1, &record_list);
1423 if (status != CONTACTS_ERROR_NONE) {
1424 contacts_list_destroy(record_list, TRUE);
1428 status = contacts_list_first(record_list);
1430 if (status != CONTACTS_ERROR_NONE) {
1431 contacts_list_destroy(record_list, TRUE);
1432 contacts_query_destroy(query);
1436 __bluetooth_pb_list_hash_reset(agent);
1439 contacts_record_h record;
1444 status = contacts_list_get_current_record_p(record_list,
1447 if (status != CONTACTS_ERROR_NONE)
1451 status = contacts_record_get_int(record,
1452 _contacts_phone_log.id,
1455 if (status != CONTACTS_ERROR_NONE)
1458 __bluetooth_pb_list_hash_insert(agent, i, id);
1465 name = _bluetooth_pb_name_from_phonelog_id(id);
1468 contacts_record_get_str_p(record,
1469 _contacts_phone_log.address,
1472 __bluetooth_pb_list_ptr_array_add(ptr_array,
1480 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1482 contacts_list_destroy(record_list, TRUE);
1486 static void __bluetooth_pb_get_list(BluetoothPbAgent *agent,
1487 PhoneBookType pb_type,
1488 GPtrArray *ptr_array)
1490 contacts_query_h query;
1492 /* no requires refresh cache */
1493 if (ptr_array == NULL && agent->pb_type == pb_type)
1498 query = __bluetooth_pb_query_person();
1499 __bluetooth_pb_get_contact_list(agent, query, ptr_array);
1502 query = __bluetooth_pb_query_phone_log_incoming();
1503 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1506 query = __bluetooth_pb_query_phone_log_outgoing();
1507 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1510 query = __bluetooth_pb_query_phone_log_missed();
1511 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1514 query = __bluetooth_pb_query_phone_log_combined();
1515 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1521 agent->pb_type = pb_type;
1524 contacts_query_destroy(query);
1527 static void __bluetooth_pb_get_contact_list_number(BluetoothPbAgent *agent,
1528 contacts_query_h query,
1531 GPtrArray *ptr_array)
1533 contacts_list_h record_list = NULL;
1552 offset = to - from + 1;
1558 status = contacts_db_get_records_with_query(query,
1562 if (status != CONTACTS_ERROR_NONE) {
1563 contacts_list_destroy(record_list, TRUE);
1567 status = contacts_list_first(record_list);
1569 if (status != CONTACTS_ERROR_NONE) {
1570 contacts_list_destroy(record_list, TRUE);
1575 contacts_record_h record;
1577 gchar *display_name;
1581 status = contacts_list_get_current_record_p(record_list,
1584 if (status != CONTACTS_ERROR_NONE)
1587 display_name = NULL;
1590 contacts_record_get_str_p(record,
1591 _contacts_person_number.display_name,
1593 contacts_record_get_str_p(record,
1594 _contacts_person_number.number,
1597 __bluetooth_pb_list_ptr_array_add(ptr_array,
1598 display_name, number, i);
1601 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1603 contacts_list_destroy(record_list, TRUE);
1606 static void __bluetooth_pb_get_phone_log_list_number(BluetoothPbAgent *agent,
1607 contacts_query_h query,
1610 GPtrArray *ptr_array)
1612 contacts_list_h record_list = NULL;
1631 offset = to - from + 1;
1637 status = contacts_db_get_records_with_query(query,
1641 if (status != CONTACTS_ERROR_NONE) {
1642 contacts_list_destroy(record_list, TRUE);
1646 status = contacts_list_first(record_list);
1647 if (status != CONTACTS_ERROR_NONE) {
1648 contacts_list_destroy(record_list, TRUE);
1653 contacts_record_h record = NULL;
1657 gchar *display_name;
1661 status = contacts_list_get_current_record_p(record_list,
1664 if (status != CONTACTS_ERROR_NONE)
1668 status = contacts_record_get_int(record,
1669 _contacts_phone_log.id,
1672 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1675 contacts_record_get_str_p(record,
1676 _contacts_phone_log.address,
1680 __bluetooth_pb_list_ptr_array_add(ptr_array,
1681 display_name, number, i);
1685 g_free(display_name);
1687 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1689 contacts_list_destroy(record_list, TRUE);
1692 static void __bluetooth_pb_get_list_number(BluetoothPbAgent *agent,
1693 PhoneBookType pb_type,
1696 GPtrArray *ptr_array)
1698 contacts_query_h query;
1702 query = __bluetooth_pb_query_person_number();
1703 __bluetooth_pb_get_contact_list_number(agent, query,
1704 start_index, end_index, ptr_array);
1707 query = __bluetooth_pb_query_phone_log_incoming();
1708 __bluetooth_pb_get_phone_log_list_number(agent, query,
1709 start_index, end_index, ptr_array);
1712 query = __bluetooth_pb_query_phone_log_outgoing();
1713 __bluetooth_pb_get_phone_log_list_number(agent, query,
1714 start_index, end_index, ptr_array);
1717 query = __bluetooth_pb_query_phone_log_missed();
1718 __bluetooth_pb_get_phone_log_list_number(agent, query,
1719 start_index, end_index, ptr_array);
1722 query = __bluetooth_pb_query_phone_log_combined();
1723 __bluetooth_pb_get_phone_log_list_number(agent, query,
1724 start_index, end_index, ptr_array);
1731 contacts_query_destroy(query);
1734 static void __bluetooth_pb_get_contact_list_name(BluetoothPbAgent *agent,
1735 contacts_query_h query,
1736 const gchar *find_text,
1737 GPtrArray *ptr_array)
1739 contacts_list_h record_list = NULL;
1744 status = contacts_db_get_records_with_query(query,
1745 -1, -1, &record_list);
1747 if (status != CONTACTS_ERROR_NONE) {
1748 contacts_list_destroy(record_list, TRUE);
1752 status = contacts_list_first(record_list);
1754 if (status != CONTACTS_ERROR_NONE) {
1755 contacts_list_destroy(record_list, TRUE);
1760 contacts_record_h record;
1762 gchar *display_name;
1765 status = contacts_list_get_current_record_p(record_list,
1768 if (status != CONTACTS_ERROR_NONE)
1771 display_name = NULL;
1772 contacts_record_get_str_p(record,
1773 _contacts_person_number.display_name,
1776 if (g_str_has_prefix(display_name, find_text)) {
1780 contacts_record_get_str_p(record,
1781 _contacts_person_number.number,
1784 __bluetooth_pb_list_ptr_array_add(ptr_array,
1785 display_name, number, i);
1789 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1790 contacts_list_destroy(record_list, TRUE);
1793 static void __bluetooth_pb_get_phone_log_list_name(BluetoothPbAgent *agent,
1794 contacts_query_h query,
1795 const gchar *find_text,
1796 GPtrArray *ptr_array)
1798 contacts_list_h record_list = NULL;
1804 status = contacts_db_get_records_with_query(query,
1808 if (status != CONTACTS_ERROR_NONE) {
1809 contacts_list_destroy(record_list, TRUE);
1813 status = contacts_list_first(record_list);
1815 if (status != CONTACTS_ERROR_NONE) {
1816 contacts_list_destroy(record_list, TRUE);
1821 contacts_record_h record = NULL;
1825 gchar *display_name;
1828 status = contacts_list_get_current_record_p(record_list,
1831 if (status != CONTACTS_ERROR_NONE)
1835 status = contacts_record_get_int(record,
1836 _contacts_phone_log.id,
1839 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1841 if (g_str_has_prefix(display_name, find_text)) {
1842 gchar *number = NULL;
1845 contacts_record_get_str_p(record,
1846 _contacts_phone_log.address,
1849 __bluetooth_pb_list_ptr_array_add(ptr_array,
1850 display_name, number, i);
1855 g_free(display_name);
1857 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1859 contacts_list_destroy(record_list, TRUE);
1862 static void __bluetooth_pb_get_list_name(BluetoothPbAgent *agent,
1863 PhoneBookType pb_type,
1864 const gchar *find_text,
1865 GPtrArray *ptr_array)
1867 contacts_query_h query;
1871 query = __bluetooth_pb_query_person_number();
1872 __bluetooth_pb_get_contact_list_name(agent, query,
1873 find_text, ptr_array);
1876 query = __bluetooth_pb_query_phone_log_incoming();
1877 __bluetooth_pb_get_phone_log_list_name(agent, query,
1878 find_text, ptr_array);
1881 query = __bluetooth_pb_query_phone_log_outgoing();
1882 __bluetooth_pb_get_phone_log_list_name(agent, query,
1883 find_text, ptr_array);
1886 query = __bluetooth_pb_query_phone_log_missed();
1887 __bluetooth_pb_get_phone_log_list_name(agent, query,
1888 find_text, ptr_array);
1891 query = __bluetooth_pb_query_phone_log_combined();
1892 __bluetooth_pb_get_phone_log_list_name(agent, query,
1893 find_text, ptr_array);
1900 contacts_query_destroy(query);
1903 static void __bluetooth_pb_list_hash_reset(BluetoothPbAgent *agent)
1905 if(agent->contact_list)
1906 g_hash_table_destroy(agent->contact_list);
1908 agent->contact_list = g_hash_table_new(g_direct_hash, g_direct_equal);
1911 static gboolean __bluetooth_pb_list_hash_insert(BluetoothPbAgent *agent,
1915 if (agent->contact_list == NULL)
1918 g_hash_table_insert(agent->contact_list,
1919 GINT_TO_POINTER(handle), GINT_TO_POINTER(id));
1924 static gint __bluetooth_pb_list_hash_lookup_id(BluetoothPbAgent *agent,
1929 if (agent->contact_list == NULL)
1932 id = GPOINTER_TO_INT(g_hash_table_lookup(agent->contact_list,
1933 GINT_TO_POINTER(handle)));
1938 static void __bluetooth_pb_list_ptr_array_add(GPtrArray *ptr_array,
1940 const gchar *number,
1943 GValue value = { 0, };
1945 g_value_init(&value, DBUS_STRUCT_STRING_STRING_UINT);
1946 g_value_take_boxed(&value,
1947 dbus_g_type_specialized_construct(DBUS_STRUCT_STRING_STRING_UINT));
1949 dbus_g_type_struct_set(&value,
1951 1, g_strdup(number),
1955 g_ptr_array_add(ptr_array, g_value_get_boxed(&value));
1958 static void __bluetooth_pb_list_ptr_array_free(gpointer data)
1960 GValue value = { 0, };
1963 gchar *number = NULL;
1968 g_value_init(&value, DBUS_STRUCT_STRING_STRING_UINT);
1969 g_value_set_boxed(&value, data);
1971 dbus_g_type_struct_get(&value,
1980 static void __bluetooth_pb_agent_signal_handler(int signum)
1983 g_main_loop_quit(mainloop);
1985 DBG("Terminate Bluetooth PBAP agent");
1991 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
1994 BluetoothPbAgent *agent;
1996 g_return_if_fail(BLUETOOTH_IS_PB_AGENT(user_data));
1997 agent = BLUETOOTH_PB_AGENT(user_data);
1999 g_signal_emit(agent, signals[CLEAR], 0);
2000 g_object_unref(agent);
2003 static void __bluetooth_pb_agent_timeout_add_seconds(BluetoothPbAgent *agent)
2005 g_return_if_fail(BLUETOOTH_IS_PB_AGENT(agent));
2007 if(agent->timeout_id)
2008 g_source_remove(agent->timeout_id);
2010 agent->timeout_id = g_timeout_add_seconds(BLUETOOTH_PB_AGENT_TIMEOUT,
2011 __bluetooth_pb_agent_timeout_calback,
2015 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data)
2017 BluetoothPbAgent *agent;
2019 g_return_val_if_fail(BLUETOOTH_IS_PB_AGENT(user_data), FALSE);
2021 agent = BLUETOOTH_PB_AGENT(user_data);
2022 agent->timeout_id = 0;
2025 g_main_loop_quit(mainloop);
2030 static void __bluetooth_pb_tel_callback(TapiHandle *handle,
2035 BluetoothPbAgent *agent;
2036 TelSimMsisdnList_t *number;
2038 g_return_if_fail(BLUETOOTH_IS_PB_AGENT(user_data));
2040 agent = BLUETOOTH_PB_AGENT(user_data);
2042 __bluetooth_pb_agent_dbus_init(agent);
2044 number = (TelSimMsisdnList_t *)data;
2045 agent->tel_number = g_strdup(number->list[0].num);
2047 tel_deinit(agent->tapi_handle);
2048 agent->tapi_handle = NULL;
2051 static void __bluetooth_pb_agent_dbus_init(BluetoothPbAgent *agent)
2054 GError *error = NULL;
2056 agent->bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
2058 if (error != NULL) {
2059 DBG("Couldn't connect to system bus[%s]\n", error->message);
2060 g_error_free(error);
2064 agent->proxy = dbus_g_proxy_new_for_name(agent->bus,
2067 DBUS_INTERFACE_DBUS);
2069 if (agent->proxy == NULL) {
2070 DBG("Failed to get a proxy for D-Bus\n");
2074 if (!dbus_g_proxy_call(agent->proxy,
2075 "RequestName", &error,
2076 G_TYPE_STRING, BT_PB_SERVICE_NAME,
2079 G_TYPE_UINT, &result,
2081 if (error != NULL) {
2082 DBG("RequestName RPC failed[%s]\n", error->message);
2083 g_error_free(error);
2086 g_object_unref(agent->proxy);
2087 agent->proxy = NULL;
2091 DBG("result : %d %d\n", result, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
2092 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
2093 DBG("Failed to get the primary well-known name.\n");
2095 g_object_unref(agent->proxy);
2096 agent->proxy = NULL;
2101 g_object_unref(agent->proxy);
2102 agent->proxy = NULL;
2104 dbus_g_connection_register_g_object(agent->bus,
2105 BT_PB_SERVICE_OBJECT_PATH,
2109 static gboolean bluetooth_pb_destroy_agent(BluetoothPbAgent *agent,
2110 DBusGMethodInvocation *context)
2112 g_main_loop_quit(mainloop);
2117 BluetoothPbAgent *agent;
2119 gint ret = EXIT_SUCCESS;
2122 struct sigaction sa;
2123 DBG("Starting Bluetooth PBAP agent");
2127 mainloop = g_main_loop_new(NULL, FALSE);
2128 if (mainloop == NULL) {
2129 DBG("Couldn't create GMainLoop\n");
2130 return EXIT_FAILURE;
2133 agent = g_object_new(BLUETOOTH_PB_TYPE_AGENT, NULL);
2135 /* connect contact */
2136 if (contacts_connect2() != CONTACTS_ERROR_NONE) {
2137 DBG("Can not connect contacts server\n");
2138 g_object_unref(agent);
2139 return EXIT_FAILURE;
2142 if (contacts_db_add_changed_cb(_contacts_event._uri,
2143 __bluetooth_pb_contact_changed,
2144 g_object_ref(agent)) != CONTACTS_ERROR_NONE) {
2145 DBG("Can not add changed callback");
2149 memset(&sa, 0, sizeof(sa));
2150 sa.sa_handler = __bluetooth_pb_agent_signal_handler;
2151 sigaction(SIGTERM, &sa, NULL);
2152 sigaction(SIGINT, &sa, NULL);
2155 agent->tapi_handle = tel_init(NULL);
2156 tapi_result = tel_get_sim_msisdn(agent->tapi_handle,
2157 __bluetooth_pb_tel_callback, agent);
2159 if (tapi_result != TAPI_API_SUCCESS) {
2160 __bluetooth_pb_agent_dbus_init(agent);
2164 __bluetooth_pb_agent_timeout_add_seconds(agent);
2166 g_main_loop_run(mainloop);
2168 if (contacts_db_remove_changed_cb(_contacts_event._uri,
2169 __bluetooth_pb_contact_changed,
2170 g_object_ref(agent)) != CONTACTS_ERROR_NONE) {
2171 DBG("Cannot remove changed callback");
2174 g_object_unref(agent);
2176 if (contacts_disconnect2() != CONTACTS_ERROR_NONE)
2177 DBG("contacts_disconnect2 failed \n");
2179 g_signal_emit(agent, signals[CLEAR], 0);
2181 g_object_unref(agent);
2183 DBG("Terminate Bluetooth PBAP agent");