4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
9 * Jaekyun Lee <jkyun.leek@samsung.com>
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
31 #include <dbus/dbus-glib.h>
34 #include <sys/types.h>
40 #include <TapiUtility.h>
43 #include "bluetooth_pb_agent.h"
44 #include "bluetooth_pb_vcard.h"
46 #define BLUETOOTH_PB_AGENT_TIMEOUT 600
54 TapiHandle *tapi_handle;
58 PhoneBookType pb_type;
64 void (*clear) (BluetoothPbAgent *agent);
65 } BluetoothPbAgentClass;
72 GType bluetooth_pb_agent_get_type(void);
74 #define BLUETOOTH_PB_TYPE_AGENT (bluetooth_pb_agent_get_type())
76 #define BLUETOOTH_PB_AGENT(object) \
77 (G_TYPE_CHECK_INSTANCE_CAST((object), \
78 BLUETOOTH_PB_TYPE_AGENT , BluetoothPbAgent))
79 #define BLUETOOTH_PB_AGENT_CLASS(klass) \
80 (G_TYPE_CHECK_CLASS_CAST((klass), \
81 BLUETOOTH_PB_TYPE_AGENT , BluetoothPbAgentClass))
82 #define BLUETOOTH_IS_PB_AGENT(object) \
83 (G_TYPE_CHECK_INSTANCE_TYPE((object), \
84 BLUETOOTH_PB_TYPE_AGENT))
85 #define BLUETOOTH_IS_PB_AGENT_CLASS(klass) \
86 (G_TYPE_CHECK_CLASS_TYPE((klass), \
87 BLUETOOTH_PB_TYPE_AGENT))
88 #define BLUETOOTH_PB_AGENT_GET_CLASS(obj) \
89 (G_TYPE_INSTANCE_GET_CLASS((obj), \
90 BLUETOOTH_PB_TYPE_AGENT , BluetoothPbAgentClass))
92 G_DEFINE_TYPE(BluetoothPbAgent, bluetooth_pb_agent, G_TYPE_OBJECT)
94 #define DBUS_STRUCT_STRING_STRING_UINT (dbus_g_type_get_struct("GValueArray", G_TYPE_STRING, \
95 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID))
97 static guint signals[LAST_SIGNAL] = { 0 };
98 static guint total_missed_call_count = 0;
99 static guint unnotified_missed_call_count = 0;
101 static GMainLoop *mainloop = NULL;
103 static void bluetooth_pb_agent_finalize(GObject *obj);
105 static void bluetooth_pb_agent_clear(BluetoothPbAgent *agent);
108 static gboolean bluetooth_pb_get_phonebook_folder_list(BluetoothPbAgent *agent,
109 const gchar ***folder_list,
112 static gboolean bluetooth_pb_get_phonebook(BluetoothPbAgent *agent,
116 guint16 max_list_count,
117 guint16 list_start_offset,
118 DBusGMethodInvocation *context);
120 static gboolean bluetooth_pb_get_phonebook_size(BluetoothPbAgent *agent,
122 DBusGMethodInvocation *context);
124 static gboolean bluetooth_pb_get_phonebook_list(BluetoothPbAgent *agent,
126 DBusGMethodInvocation *context);
128 static gboolean bluetooth_pb_get_phonebook_entry(BluetoothPbAgent *agent,
133 DBusGMethodInvocation *context);
135 static gboolean bluetooth_pb_get_phonebook_size_at(BluetoothPbAgent *agent,
136 const gchar *command,
137 DBusGMethodInvocation *context);
139 static gboolean bluetooth_pb_get_phonebook_entries_at(BluetoothPbAgent *agent,
140 const gchar *command,
143 DBusGMethodInvocation *context);
145 static gboolean bluetooth_pb_get_phonebook_entries_find_at(BluetoothPbAgent *agent,
146 const gchar *command,
147 const gchar *find_text,
148 DBusGMethodInvocation *context);
150 static gboolean bluetooth_pb_get_total_object_count(BluetoothPbAgent *agent,
152 DBusGMethodInvocation *context);
154 static gboolean bluetooth_pb_add_contact (BluetoothPbAgent *agent,
155 const char *filename,
158 static gboolean bluetooth_pb_destroy_agent(BluetoothPbAgent *agent,
159 DBusGMethodInvocation *context);
161 static void __bluetooth_pb_dbus_return_error(DBusGMethodInvocation *context,
163 const gchar *message);
165 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name);
167 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name);
169 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
173 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match,
176 static contacts_query_h __bluetooth_pb_query_person(int addressbook);
178 static contacts_query_h __bluetooth_pb_query_person_number(void);
180 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void);
182 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void);
184 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void);
186 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void);
188 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
191 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count);
193 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record);
195 static void __bluetooth_pb_get_vcards(BluetoothPbAgent *agent,
196 PhoneBookType pb_type,
199 guint16 max_list_count,
200 guint16 list_start_offset,
203 static void __bluetooth_pb_get_contact_list(BluetoothPbAgent *agent,
204 contacts_query_h query,
205 GPtrArray *ptr_array);
207 static void __bluetooth_pb_get_phone_log_list(BluetoothPbAgent *agent,
208 contacts_query_h query,
209 GPtrArray *ptr_array);
211 static void __bluetooth_pb_get_list(BluetoothPbAgent *agent,
212 PhoneBookType pb_type,
213 GPtrArray *ptr_array);
215 static void __bluetooth_pb_get_contact_list_number(BluetoothPbAgent *agent,
216 contacts_query_h query,
219 GPtrArray *ptr_array);
221 static void __bluetooth_pb_get_phone_log_list_number(BluetoothPbAgent *agent,
222 contacts_query_h query,
225 GPtrArray *ptr_array);
227 static void __bluetooth_pb_get_list_number(BluetoothPbAgent *agent,
228 PhoneBookType pb_type,
231 GPtrArray *ptr_array);
233 static void __bluetooth_pb_get_contact_list_name(BluetoothPbAgent *agent,
234 contacts_query_h query,
235 const gchar *find_text,
236 GPtrArray *ptr_array);
238 static void __bluetooth_pb_get_phone_log_list_name(BluetoothPbAgent *agent,
239 contacts_query_h query,
240 const gchar *find_text,
241 GPtrArray *ptr_array);
243 static void __bluetooth_pb_get_list_name(BluetoothPbAgent *agent,
244 PhoneBookType pb_type,
245 const gchar *find_text,
246 GPtrArray *ptr_array);
248 static void __bluetooth_pb_list_ptr_array_add(GPtrArray *ptr_array,
253 static void __bluetooth_pb_list_ptr_array_free(gpointer data);
255 static void __bluetooth_pb_agent_signal_handler(int signum);
257 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
260 static void __bluetooth_pb_agent_timeout_add_seconds(BluetoothPbAgent *agent);
262 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data);
264 static void __bluetooth_pb_tel_callback(TapiHandle *handle,
269 static void __bluetooth_pb_agent_dbus_init(BluetoothPbAgent *agent);
271 #include "bluetooth_pb_agent_glue.h"
273 static void bluetooth_pb_agent_init(BluetoothPbAgent *agent)
278 agent->tapi_handle = NULL;
279 agent->tel_number = NULL;
280 agent->timeout_id = 0;
281 agent->pb_type = TELECOM_NONE;
285 static void bluetooth_pb_agent_class_init(BluetoothPbAgentClass *klass)
288 GObjectClass *object_class = (GObjectClass *) klass;
290 klass->clear = bluetooth_pb_agent_clear;
292 object_class->finalize = bluetooth_pb_agent_finalize;
294 signals[CLEAR] = g_signal_new("clear",
295 G_TYPE_FROM_CLASS(klass),
297 G_STRUCT_OFFSET(BluetoothPbAgentClass, clear),
299 g_cclosure_marshal_VOID__VOID,
302 dbus_g_object_type_install_info(BLUETOOTH_PB_TYPE_AGENT,
303 &dbus_glib_bluetooth_pb_object_info);
307 static void bluetooth_pb_agent_finalize(GObject *obj)
310 BluetoothPbAgent *agent = BLUETOOTH_PB_AGENT(obj);
312 if (agent->tapi_handle) {
313 tel_deinit(agent->tapi_handle);
314 agent->tapi_handle = NULL;
317 if (agent->tel_number) {
318 g_free(agent->tel_number);
319 agent->tel_number = NULL;
322 if(agent->timeout_id) {
323 g_source_remove(agent->timeout_id);
324 agent->timeout_id = 0;
328 g_object_unref(agent->proxy);
333 dbus_g_connection_unref(agent->bus);
338 G_OBJECT_CLASS(bluetooth_pb_agent_parent_class)->finalize(obj);
342 static void bluetooth_pb_agent_clear(BluetoothPbAgent *agent)
345 agent->pb_type = TELECOM_NONE;
349 static gboolean bluetooth_pb_get_phonebook_folder_list(BluetoothPbAgent *agent,
350 const gchar ***folder_list,
358 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list);
359 folder = g_new0(gchar *, size);
361 for (i = 0; i < size; i++)
362 folder[i] = g_strdup(bluetooth_pb_agent_folder_list[i]);
364 *folder_list = (const gchar **)folder;
370 static gboolean bluetooth_pb_get_phonebook(BluetoothPbAgent *agent,
374 guint16 max_list_count,
375 guint16 list_start_offset,
376 DBusGMethodInvocation *context)
379 PhoneBookType pb_type = TELECOM_NONE;
380 GPtrArray *vcards = NULL;
381 gchar **vcards_str = NULL;
383 INFO("name: %s filter: %lld format: %d max_list_count: %d list_start_offset: %d\n",
384 name, filter, format, max_list_count, list_start_offset);
386 __bluetooth_pb_agent_timeout_add_seconds(agent);
388 pb_type = __bluetooth_pb_get_pb_type(name);
390 if (pb_type == TELECOM_NONE) {
391 __bluetooth_pb_dbus_return_error(context,
393 "unsupported name defined");
397 vcards = g_ptr_array_new();
399 if (max_list_count > 0) {
400 __bluetooth_pb_get_vcards(agent, pb_type,
402 max_list_count, list_start_offset,
407 g_ptr_array_add(vcards, NULL);
409 vcards_str = (gchar **) g_ptr_array_free(vcards, FALSE);
411 if (pb_type == TELECOM_MCH) {
412 dbus_g_method_return(context, vcards_str, unnotified_missed_call_count);
413 INFO("Notified [%d] missed call count", unnotified_missed_call_count);
414 unnotified_missed_call_count = 0;
416 dbus_g_method_return(context, vcards_str, 0);
419 g_strfreev(vcards_str);
425 static gboolean bluetooth_pb_get_phonebook_size(BluetoothPbAgent *agent,
427 DBusGMethodInvocation *context)
430 PhoneBookType pb_type = TELECOM_NONE;
433 DBG_SECURE("name: %s\n", name);
435 __bluetooth_pb_agent_timeout_add_seconds(agent);
437 pb_type = __bluetooth_pb_get_pb_type(name);
439 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
440 __bluetooth_pb_dbus_return_error(context,
442 "unsupported name defined");
447 #ifdef PBAP_SIM_ENABLE
448 if (pb_type == TELECOM_PB || pb_type == SIM_PB)
451 if (pb_type == TELECOM_PB)
454 if (pb_type == TELECOM_MCH) {
455 dbus_g_method_return(context, count, unnotified_missed_call_count);
456 INFO("Notified [%d] missed call count", unnotified_missed_call_count);
457 unnotified_missed_call_count = 0;
459 dbus_g_method_return(context, count, 0);
467 static gboolean bluetooth_pb_get_phonebook_list(BluetoothPbAgent *agent,
469 DBusGMethodInvocation *context)
472 PhoneBookType pb_type = TELECOM_NONE;
474 GPtrArray *ptr_array;
476 DBG_SECURE("name: %s\n", name);
478 __bluetooth_pb_agent_timeout_add_seconds(agent);
480 pb_type = __bluetooth_pb_get_pb_type(name);
482 if (pb_type == TELECOM_NONE) {
483 __bluetooth_pb_dbus_return_error(context,
485 "unsupported name defined");
489 ptr_array = g_ptr_array_new_with_free_func(__bluetooth_pb_list_ptr_array_free);
491 __bluetooth_pb_get_list(agent, pb_type, ptr_array);
493 // __bluetooth_pb_get_count_new_missed_call(&new_missed_call);
494 INFO("pb_type[%d] / number of missed_call[%d]", pb_type, unnotified_missed_call_count);
496 if (pb_type == TELECOM_MCH) {
497 dbus_g_method_return(context, ptr_array, unnotified_missed_call_count);
498 INFO("Notified [%d] missed call count", unnotified_missed_call_count);
499 unnotified_missed_call_count = 0;
501 dbus_g_method_return(context, ptr_array, 0);
505 g_ptr_array_free(ptr_array, TRUE);
512 static gboolean bluetooth_pb_get_phonebook_entry(BluetoothPbAgent *agent,
517 DBusGMethodInvocation *context)
520 PhoneBookType pb_type = TELECOM_NONE;
525 const gchar *attr = NULL;
527 DBG_SECURE("folder: %s id: %s filter: %ld format: %d\n",
528 folder, id, filter, format);
530 __bluetooth_pb_agent_timeout_add_seconds(agent);
532 if (!g_str_has_suffix(id, ".vcf")) {
533 __bluetooth_pb_dbus_return_error(context,
539 handle = (gint)g_ascii_strtoll(id, NULL, 10);
541 pb_type = __bluetooth_pb_get_pb_type(folder);
543 if (pb_type == TELECOM_NONE) {
544 __bluetooth_pb_dbus_return_error(context,
546 "unsupported name defined");
550 /* create index cache */
551 __bluetooth_pb_get_list(agent, pb_type, NULL);
556 str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
559 if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_PHONE)
560 str = _bluetooth_pb_vcard_contact(handle, filter, format);
565 str = _bluetooth_pb_vcard_call(handle, filter, format, "RECEIVED");
568 str = _bluetooth_pb_vcard_call(handle, filter, format, "DIALED");
571 str = _bluetooth_pb_vcard_call(handle, filter, format, "MISSED");
574 contacts_record_h record = NULL;
578 status = contacts_db_get_record(_contacts_phone_log._uri,
581 if (status != CONTACTS_ERROR_NONE)
584 attr = __bluetooth_pb_phone_log_get_log_type(record);
585 str = _bluetooth_pb_vcard_call(handle, filter, format, attr);
587 contacts_record_destroy(record, TRUE);
590 #ifdef PBAP_SIM_ENABLE
593 str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
596 if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_SIM)
597 str = _bluetooth_pb_vcard_contact(handle, filter, format);
602 __bluetooth_pb_dbus_return_error(context,
604 "unsupported name defined");
608 dbus_g_method_return(context, str);
615 static gboolean bluetooth_pb_get_phonebook_size_at(BluetoothPbAgent *agent,
616 const gchar *command,
617 DBusGMethodInvocation *context)
620 PhoneBookType pb_type = TELECOM_NONE;
623 DBG("command: %s\n", command);
625 __bluetooth_pb_agent_timeout_add_seconds(agent);
627 pb_type = __bluetooth_pb_get_storage_pb_type(command);
629 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
630 __bluetooth_pb_dbus_return_error(context,
632 "unsupported name defined");
636 dbus_g_method_return(context, count);
642 static gboolean bluetooth_pb_get_phonebook_entries_at(BluetoothPbAgent *agent,
643 const gchar *command,
646 DBusGMethodInvocation *context)
649 PhoneBookType pb_type = TELECOM_NONE;
651 GPtrArray *ptr_array = NULL;
653 DBG("command: %s, start_index: %d, end_index: %d\n",
654 command, start_index, end_index);
656 __bluetooth_pb_agent_timeout_add_seconds(agent);
658 pb_type = __bluetooth_pb_get_storage_pb_type(command);
660 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
661 __bluetooth_pb_dbus_return_error(context,
663 "unsupported name defined");
667 ptr_array = g_ptr_array_new_with_free_func(__bluetooth_pb_list_ptr_array_free);
669 __bluetooth_pb_get_list_number(agent, pb_type,
670 start_index, end_index,
673 dbus_g_method_return(context, ptr_array);
676 g_ptr_array_free(ptr_array, TRUE);
682 static gboolean bluetooth_pb_get_phonebook_entries_find_at(BluetoothPbAgent *agent,
683 const gchar *command,
684 const gchar *find_text,
685 DBusGMethodInvocation *context)
688 PhoneBookType pb_type = TELECOM_NONE;
690 GPtrArray *ptr_array = NULL;
692 DBG("command: %s, find text: %s\n", command, find_text);
694 __bluetooth_pb_agent_timeout_add_seconds(agent);
696 pb_type = __bluetooth_pb_get_storage_pb_type(command);
698 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
699 __bluetooth_pb_dbus_return_error(context,
701 "unsupported name defined");
705 ptr_array = g_ptr_array_new_with_free_func(__bluetooth_pb_list_ptr_array_free);
707 __bluetooth_pb_get_list_name(agent, pb_type,
708 find_text, ptr_array);
710 dbus_g_method_return(context, ptr_array);
713 g_ptr_array_free(ptr_array, TRUE);
719 static gboolean bluetooth_pb_get_total_object_count(BluetoothPbAgent *agent,
720 gchar *path, DBusGMethodInvocation *context)
724 PhoneBookType pb_type = TELECOM_NONE;
726 DBG("%s() %d\n", __FUNCTION__, __LINE__);
728 __bluetooth_pb_agent_timeout_add_seconds(agent);
730 pb_type = __bluetooth_pb_get_storage_pb_type(path);
732 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
733 __bluetooth_pb_dbus_return_error(context,
735 "unsupported name defined");
739 dbus_g_method_return(context, count);
741 DBG("%s() %d\n", __FUNCTION__, __LINE__);
749 static int __bluetooth_pb_agent_read_file(const char *file_path, char **stream)
754 int received_file_size = 0;
755 struct stat file_attr;
757 if (file_path == NULL || stream == NULL) {
758 ERR("Invalid data \n");
762 DBG_SECURE("file_path = %s\n", file_path);
764 if ((fp = fopen(file_path, "r+")) == NULL) {
765 ERR_SECURE("Cannot open %s\n", file_path);
769 if (fstat(fileno(fp), &file_attr) == 0) {
770 received_file_size = file_attr.st_size;
771 DBG("file_attr.st_size = %d, size = %d\n", file_attr.st_size, received_file_size);
773 if (received_file_size <= 0) {
774 ERR_SECURE("Some problem in the file size [%s] \n", file_path);
780 *stream = (char *)malloc(sizeof(char) * received_file_size);
781 if (NULL == *stream) {
787 ERR_SECURE("Some problem in the file [%s] \n", file_path);
793 read_len = fread(*stream, 1, received_file_size, fp);
800 DBG_SECURE("Cannot open %s\n", file_path);
813 static gboolean bluetooth_pb_add_contact(BluetoothPbAgent *agent, const char *filename,
817 /* Contact API is changed, Temporary blocked */
819 CTSstruct *contact_record = NULL;
820 GSList *numbers_list = NULL, *cursor;
822 int is_duplicated = 0;
826 DBG_SECURE("file_path = %s\n", filename);
828 err = contacts_svc_connect();
829 ERR("contact_db_service_connect fucntion call [error] = %d \n", err);
831 err = __bluetooth_pb_agent_read_file(filename, &stream);
834 contacts_svc_disconnect();
835 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
837 if (NULL != stream) {
844 is_success = contacts_svc_get_contact_from_vcard((const void *)stream, &contact_record);
846 DBG("contacts_svc_get_contact_from_vcard fucntion call [is_success] = %d \n", is_success);
848 if (0 == is_success) {
849 contacts_svc_struct_get_list(contact_record, CTS_CF_NUMBER_LIST, &numbers_list);
850 cursor = numbers_list;
852 for (; cursor; cursor = g_slist_next(cursor)) {
853 if (contacts_svc_find_contact_by(CTS_FIND_BY_NUMBER,
854 contacts_svc_value_get_str(cursor->data,
855 CTS_NUM_VAL_NUMBER_STR)) > 0) {
856 DBG("is_duplicated\n");
857 is_duplicated = TRUE;
861 if (is_duplicated == FALSE) {
862 contacts_svc_insert_contact(0, contact_record);
868 err = contacts_svc_disconnect();
869 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
871 if (NULL != stream) {
880 static void __bluetooth_pb_dbus_return_error(DBusGMethodInvocation *context,
882 const gchar *message)
886 GError *error = NULL;
888 quark = g_type_qname(bluetooth_pb_agent_get_type());
889 error = g_error_new_literal(quark, code, message);
891 DBG("%s\n", message);
893 dbus_g_method_return_error(context, error);
898 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name)
901 gchar *suffix = ".vcf";
911 if (g_str_has_suffix(name, suffix))
912 len -= strlen(suffix);
914 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list) - 1;
915 for (i = 0; i < size; i++) {
916 if (strncmp(name, bluetooth_pb_agent_folder_list[i], len) == 0)
924 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name)
930 if (g_strcmp0(name, "\"ME\"") == 0 )
933 if (g_strcmp0(name, "\"RC\"") == 0)
936 if (g_strcmp0(name, "\"DC\"") == 0)
939 if (g_strcmp0(name, "\"MC\"") == 0)
946 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
954 for (i = 0; i < size; i++) {
957 status = contacts_filter_add_operator(filter,
958 CONTACTS_FILTER_OPERATOR_OR);
960 if (status != CONTACTS_ERROR_NONE)
964 status = contacts_filter_add_int(filter,
965 _contacts_phone_log.log_type,
966 CONTACTS_MATCH_EQUAL,
969 if (status != CONTACTS_ERROR_NONE)
974 return CONTACTS_ERROR_NONE;
977 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match,
981 contacts_query_h query = NULL;
982 contacts_filter_h filter = NULL;
986 status = contacts_query_create(_contacts_phone_log._uri,
989 if (status != CONTACTS_ERROR_NONE)
992 status = contacts_filter_create(_contacts_phone_log._uri, &filter);
994 if (status != CONTACTS_ERROR_NONE) {
995 contacts_query_destroy(query);
999 status = __bluetooth_pb_phone_log_filter_append(filter, match, size);
1001 if (status != CONTACTS_ERROR_NONE) {
1002 contacts_filter_destroy(filter);
1003 contacts_query_destroy(query);
1007 status = contacts_query_set_filter(query, filter);
1009 if (status != CONTACTS_ERROR_NONE) {
1010 contacts_filter_destroy(filter);
1011 contacts_query_destroy(query);
1015 status = contacts_query_set_sort(query,
1016 _contacts_phone_log.log_time,
1019 if (status != CONTACTS_ERROR_NONE) {
1020 contacts_filter_destroy(filter);
1021 contacts_query_destroy(query);
1025 contacts_filter_destroy(filter);
1031 bool __bt_is_matching_addressbook(const char *addressbook_name, int addressbook)
1033 bool is_sim_addressbook = _bt_is_sim_addressbook(addressbook_name);
1035 if ((is_sim_addressbook == false
1036 && addressbook == PBAP_ADDRESSBOOK_PHONE) ||
1037 (is_sim_addressbook == true
1038 && addressbook == PBAP_ADDRESSBOOK_SIM))
1044 static contacts_query_h __bluetooth_pb_query_person(int addressbook)
1047 contacts_query_h query = NULL;
1048 contacts_filter_h filter = NULL;
1049 contacts_list_h recordList = NULL;
1050 contacts_record_h record = NULL;
1052 char* addressbook_name = NULL;
1053 int address_book_id = 0;
1057 bool is_first_condition = true;
1058 DBG("Addressbook [%d]", addressbook);
1060 status = contacts_query_create(_contacts_person_contact._uri, &query);
1062 ERR("Could not create query");
1066 /* Create addressbook Filter*/
1067 contacts_db_get_all_records(_contacts_address_book._uri, 0, 0, &recordList);
1068 contacts_filter_create(_contacts_person_contact._uri, &filter);
1069 contacts_list_get_count(recordList, &count);
1071 for (i = 0; i < count; i++) {
1072 status = contacts_list_get_current_record_p(recordList, &record);
1073 if (status != CONTACTS_ERROR_NONE) {
1074 ERR("Contact list get api failed %d", status);
1077 status = contacts_record_get_str_p(record, _contacts_address_book.name,
1079 if (status != CONTACTS_ERROR_NONE) {
1080 ERR("Contact record get api failed %d", status);
1083 status = contacts_record_get_int(record, _contacts_address_book.id,
1085 if (status != CONTACTS_ERROR_NONE) {
1086 ERR("contacts record get int api failed %d", status);
1090 DBG("Addressbook ID: [%d] Addressbook Name: [%s]",
1091 address_book_id, addressbook_name);
1093 if (__bt_is_matching_addressbook(addressbook_name,
1095 if (is_first_condition)
1096 is_first_condition = false;
1098 contacts_filter_add_operator(filter,
1099 CONTACTS_FILTER_OPERATOR_OR);
1100 DBG("SELECTED Addressbook ID: [%d] Addressbook Name: [%s]",
1101 address_book_id, addressbook_name);
1102 status = contacts_filter_add_int(filter,
1103 _contacts_person_contact.address_book_id,
1104 CONTACTS_MATCH_EQUAL, address_book_id);
1105 if (status != CONTACTS_ERROR_NONE)
1106 ERR("Contact filter add failed %d", status);
1109 if (contacts_list_next(recordList) != CONTACTS_ERROR_NONE)
1113 contacts_list_destroy(recordList, true);
1115 status = contacts_query_set_filter(query, filter);
1116 if (status != CONTACTS_ERROR_NONE)
1117 ERR("Could not Apply Filter");
1119 contacts_filter_destroy(filter);
1124 static contacts_query_h __bluetooth_pb_query_person_number(void)
1127 contacts_query_h query = NULL;
1131 status = contacts_query_create(_contacts_person_number._uri,
1134 if (status != CONTACTS_ERROR_NONE)
1141 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void)
1146 CONTACTS_PLOG_TYPE_VOICE_INCOMMING,
1147 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING,
1148 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1149 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1153 return __bluetooth_pb_query_phone_log(match, size);
1156 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void)
1161 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1162 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING
1166 return __bluetooth_pb_query_phone_log(match, size);
1169 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void)
1174 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN,
1175 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN,
1176 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN,
1177 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN
1181 return __bluetooth_pb_query_phone_log(match, size);
1184 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void)
1189 CONTACTS_PLOG_TYPE_VOICE_INCOMMING,
1190 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING,
1191 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1192 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING,
1193 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN,
1194 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN,
1195 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN,
1196 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN,
1197 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1198 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1202 return __bluetooth_pb_query_phone_log(match, size);
1205 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
1209 contacts_query_h query = NULL;
1216 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1219 query = __bluetooth_pb_query_phone_log_incoming();
1222 query = __bluetooth_pb_query_phone_log_outgoing();
1225 query = __bluetooth_pb_query_phone_log_missed();
1228 query = __bluetooth_pb_query_phone_log_combined();
1230 #ifdef PBAP_SIM_ENABLE
1232 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1242 status = contacts_db_get_count_with_query(query, &signed_count);
1244 if (status != CONTACTS_ERROR_NONE) {
1245 contacts_query_destroy(query);
1249 contacts_query_destroy(query);
1251 if (signed_count < 0)
1254 *count = (gint) signed_count;
1260 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count)
1263 contacts_query_h query = NULL;
1270 CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN,
1271 CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN
1274 query = __bluetooth_pb_query_phone_log(match, size);
1279 status = contacts_db_get_count_with_query(query, &signed_count);
1281 if (status != CONTACTS_ERROR_NONE) {
1282 contacts_query_destroy(query);
1286 contacts_query_destroy(query);
1288 if (signed_count < 0)
1291 *count = (guint)signed_count;
1297 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record)
1303 status = contacts_record_get_int(record,
1304 _contacts_phone_log.log_type,
1307 if (status != CONTACTS_ERROR_NONE)
1311 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING:
1312 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING:
1313 case CONTACTS_PLOG_TYPE_VOICE_REJECT:
1314 case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
1316 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
1317 case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
1319 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN:
1320 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN:
1321 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN:
1322 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN:
1330 static void __bluetooth_pb_get_vcards(BluetoothPbAgent *agent,
1331 PhoneBookType pb_type,
1334 guint16 max_list_count,
1335 guint16 list_start_offset,
1339 contacts_list_h record_list = NULL;
1340 contacts_query_h query = NULL;
1347 guint property_id = 0;
1349 const char *attr = NULL;
1351 gboolean get_log = FALSE;
1353 /* contact offset is n - 1 of PBAP */
1354 offset = (gint)list_start_offset - 1;
1356 if ( max_list_count >= 65535)
1357 limit = -1; /* contact limit -1 means unrestricted */
1359 limit = (gint)max_list_count;
1363 #ifdef PBAP_SIM_ENABLE
1367 if (list_start_offset == 0) {
1370 vcard = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
1373 g_ptr_array_add(vcards, vcard);
1383 if (pb_type == TELECOM_PB)
1384 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1385 #ifdef PBAP_SIM_ENABLE
1386 else if(pb_type == SIM_PB)
1387 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1390 property_id = _contacts_person.id;
1393 query = __bluetooth_pb_query_phone_log_incoming();
1394 property_id = _contacts_phone_log.id;
1398 query = __bluetooth_pb_query_phone_log_outgoing();
1399 property_id = _contacts_phone_log.id;
1403 query = __bluetooth_pb_query_phone_log_missed();
1404 property_id = _contacts_phone_log.id;
1408 query = __bluetooth_pb_query_phone_log_combined();
1409 property_id = _contacts_phone_log.id;
1415 INFO("Limit is = %d and offset is =%d\n", limit, offset);
1417 /* When limit is passed as ZERO to contacts_db_get_records_with_query API
1418 * then this API will provide all available contacts in its database (unrestricted).
1419 * Now consider a case when client requests for maxlistcount of 1 and start offset as 0
1420 * then we have already read the owner card in above switch case and when it reads owner
1421 * card it decrements the limit by 1.
1425 status = contacts_db_get_records_with_query(query, offset, limit, &record_list);
1427 if (status != CONTACTS_ERROR_NONE) {
1428 contacts_list_destroy(record_list, TRUE);
1429 contacts_query_destroy(query);
1433 status = contacts_list_first(record_list);
1435 if (status != CONTACTS_ERROR_NONE) {
1436 contacts_list_destroy(record_list, TRUE);
1437 contacts_query_destroy(query);
1442 contacts_record_h record;
1446 gchar *vcard = NULL;
1449 status = contacts_list_get_current_record_p(record_list, &record);
1451 if (status != CONTACTS_ERROR_NONE)
1454 status = contacts_record_get_int(record, property_id, &id);
1456 if (status != CONTACTS_ERROR_NONE)
1459 if (property_id == _contacts_person.id)
1460 vcard = _bluetooth_pb_vcard_contact(id, filter, format);
1463 attr = __bluetooth_pb_phone_log_get_log_type(record);
1465 vcard = _bluetooth_pb_vcard_call(id, filter, format, attr);
1469 g_ptr_array_add(vcards, vcard);
1471 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1472 contacts_list_destroy(record_list, TRUE);
1475 contacts_query_destroy(query);
1480 static void __bluetooth_pb_get_contact_list(BluetoothPbAgent *agent,
1481 contacts_query_h query,
1482 GPtrArray *ptr_array)
1485 contacts_list_h record_list = NULL;
1494 tmp = _bluetooth_pb_owner_name();
1495 name = g_strdup_printf("%s;;;;", tmp);
1498 __bluetooth_pb_list_ptr_array_add(ptr_array,
1499 name, agent->tel_number, 0);
1504 status = contacts_db_get_records_with_query(query,
1505 -1, -1, &record_list);
1507 if (status != CONTACTS_ERROR_NONE) {
1508 contacts_list_destroy(record_list, TRUE);
1512 status = contacts_list_first(record_list);
1514 if (status != CONTACTS_ERROR_NONE) {
1515 contacts_list_destroy(record_list, TRUE);
1520 contacts_record_h record;
1525 status = contacts_list_get_current_record_p(record_list,
1528 if (status != CONTACTS_ERROR_NONE)
1532 status = contacts_record_get_int(record,
1533 _contacts_person_contact.person_id,
1536 if (status != CONTACTS_ERROR_NONE)
1544 name = _bluetooth_pb_name_from_person_id(id);
1545 number = _bluetooth_pb_number_from_person_id(id);
1547 __bluetooth_pb_list_ptr_array_add(ptr_array,
1554 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1556 contacts_list_destroy(record_list, TRUE);
1560 static void __bluetooth_pb_get_phone_log_list(BluetoothPbAgent *agent,
1561 contacts_query_h query,
1562 GPtrArray *ptr_array)
1565 contacts_list_h record_list = NULL;
1569 status = contacts_db_get_records_with_query(query,
1570 -1, -1, &record_list);
1572 if (status != CONTACTS_ERROR_NONE)
1575 status = contacts_list_first(record_list);
1577 if (status != CONTACTS_ERROR_NONE) {
1578 contacts_list_destroy(record_list, TRUE);
1583 contacts_record_h record;
1588 status = contacts_list_get_current_record_p(record_list,
1591 if (status != CONTACTS_ERROR_NONE)
1595 status = contacts_record_get_int(record,
1596 _contacts_phone_log.id,
1599 if (status != CONTACTS_ERROR_NONE)
1607 name = _bluetooth_pb_name_from_phonelog_id(id);
1610 contacts_record_get_str_p(record,
1611 _contacts_phone_log.address,
1614 __bluetooth_pb_list_ptr_array_add(ptr_array,
1620 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1622 contacts_list_destroy(record_list, TRUE);
1627 static void __bluetooth_pb_get_list(BluetoothPbAgent *agent,
1628 PhoneBookType pb_type,
1629 GPtrArray *ptr_array)
1632 contacts_query_h query;
1634 /* no requires refresh cache */
1635 if (ptr_array == NULL && agent->pb_type == pb_type)
1640 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1641 __bluetooth_pb_get_contact_list(agent, query, ptr_array);
1644 query = __bluetooth_pb_query_phone_log_incoming();
1645 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1648 query = __bluetooth_pb_query_phone_log_outgoing();
1649 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1652 query = __bluetooth_pb_query_phone_log_missed();
1653 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1656 query = __bluetooth_pb_query_phone_log_combined();
1657 __bluetooth_pb_get_phone_log_list(agent, query, ptr_array);
1659 #ifdef PBAP_SIM_ENABLE
1661 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1662 __bluetooth_pb_get_contact_list(agent, query, ptr_array);
1669 agent->pb_type = pb_type;
1672 contacts_query_destroy(query);
1676 static void __bluetooth_pb_get_contact_list_number(BluetoothPbAgent *agent,
1677 contacts_query_h query,
1680 GPtrArray *ptr_array)
1683 contacts_list_h record_list = NULL;
1699 offset = to - from + 1;
1705 status = contacts_db_get_records_with_query(query,
1709 if (status != CONTACTS_ERROR_NONE) {
1710 contacts_list_destroy(record_list, TRUE);
1714 status = contacts_list_first(record_list);
1716 if (status != CONTACTS_ERROR_NONE) {
1717 contacts_list_destroy(record_list, TRUE);
1722 contacts_record_h record;
1724 gchar *display_name;
1728 status = contacts_list_get_current_record_p(record_list,
1731 if (status != CONTACTS_ERROR_NONE)
1734 display_name = NULL;
1737 contacts_record_get_str_p(record,
1738 _contacts_person_number.display_name,
1740 contacts_record_get_str_p(record,
1741 _contacts_person_number.number,
1744 __bluetooth_pb_list_ptr_array_add(ptr_array,
1745 display_name, number, i);
1748 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1750 contacts_list_destroy(record_list, TRUE);
1754 static void __bluetooth_pb_get_phone_log_list_number(BluetoothPbAgent *agent,
1755 contacts_query_h query,
1758 GPtrArray *ptr_array)
1761 contacts_list_h record_list = NULL;
1780 offset = to - from + 1;
1786 status = contacts_db_get_records_with_query(query,
1790 if (status != CONTACTS_ERROR_NONE) {
1791 contacts_list_destroy(record_list, TRUE);
1795 status = contacts_list_first(record_list);
1796 if (status != CONTACTS_ERROR_NONE) {
1797 contacts_list_destroy(record_list, TRUE);
1802 contacts_record_h record = NULL;
1806 gchar *display_name;
1810 status = contacts_list_get_current_record_p(record_list,
1813 if (status != CONTACTS_ERROR_NONE)
1817 status = contacts_record_get_int(record,
1818 _contacts_phone_log.id,
1820 if (status != CONTACTS_ERROR_NONE) {
1821 ERR("contact_record_get_int api failed %d", status);
1825 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1828 contacts_record_get_str_p(record,
1829 _contacts_phone_log.address,
1833 __bluetooth_pb_list_ptr_array_add(ptr_array,
1834 display_name, number, i);
1838 g_free(display_name);
1840 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1842 contacts_list_destroy(record_list, TRUE);
1846 static void __bluetooth_pb_get_list_number(BluetoothPbAgent *agent,
1847 PhoneBookType pb_type,
1850 GPtrArray *ptr_array)
1853 contacts_query_h query;
1857 query = __bluetooth_pb_query_person_number();
1858 __bluetooth_pb_get_contact_list_number(agent, query,
1859 start_index, end_index, ptr_array);
1862 query = __bluetooth_pb_query_phone_log_incoming();
1863 __bluetooth_pb_get_phone_log_list_number(agent, query,
1864 start_index, end_index, ptr_array);
1867 query = __bluetooth_pb_query_phone_log_outgoing();
1868 __bluetooth_pb_get_phone_log_list_number(agent, query,
1869 start_index, end_index, ptr_array);
1872 query = __bluetooth_pb_query_phone_log_missed();
1873 __bluetooth_pb_get_phone_log_list_number(agent, query,
1874 start_index, end_index, ptr_array);
1877 query = __bluetooth_pb_query_phone_log_combined();
1878 __bluetooth_pb_get_phone_log_list_number(agent, query,
1879 start_index, end_index, ptr_array);
1886 contacts_query_destroy(query);
1890 static void __bluetooth_pb_get_contact_list_name(BluetoothPbAgent *agent,
1891 contacts_query_h query,
1892 const gchar *find_text,
1893 GPtrArray *ptr_array)
1896 contacts_list_h record_list = NULL;
1901 status = contacts_db_get_records_with_query(query,
1902 -1, -1, &record_list);
1904 if (status != CONTACTS_ERROR_NONE) {
1905 contacts_list_destroy(record_list, TRUE);
1909 status = contacts_list_first(record_list);
1911 if (status != CONTACTS_ERROR_NONE) {
1912 contacts_list_destroy(record_list, TRUE);
1917 contacts_record_h record;
1919 gchar *display_name;
1922 status = contacts_list_get_current_record_p(record_list,
1925 if (status != CONTACTS_ERROR_NONE)
1928 display_name = NULL;
1929 contacts_record_get_str_p(record,
1930 _contacts_person_number.display_name,
1933 if (g_str_has_prefix(display_name, find_text)) {
1937 contacts_record_get_str_p(record,
1938 _contacts_person_number.number,
1941 __bluetooth_pb_list_ptr_array_add(ptr_array,
1942 display_name, number, i);
1946 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1947 contacts_list_destroy(record_list, TRUE);
1951 static void __bluetooth_pb_get_phone_log_list_name(BluetoothPbAgent *agent,
1952 contacts_query_h query,
1953 const gchar *find_text,
1954 GPtrArray *ptr_array)
1957 contacts_list_h record_list = NULL;
1963 status = contacts_db_get_records_with_query(query,
1967 if (status != CONTACTS_ERROR_NONE) {
1968 contacts_list_destroy(record_list, TRUE);
1972 status = contacts_list_first(record_list);
1974 if (status != CONTACTS_ERROR_NONE) {
1975 contacts_list_destroy(record_list, TRUE);
1980 contacts_record_h record = NULL;
1984 gchar *display_name;
1987 status = contacts_list_get_current_record_p(record_list,
1990 if (status != CONTACTS_ERROR_NONE)
1994 status = contacts_record_get_int(record,
1995 _contacts_phone_log.id,
1997 if (status != CONTACTS_ERROR_NONE) {
1998 ERR("contacts_record_get_int failed %d", status);
2002 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
2004 if (g_str_has_prefix(display_name, find_text)) {
2005 gchar *number = NULL;
2008 contacts_record_get_str_p(record,
2009 _contacts_phone_log.address,
2012 __bluetooth_pb_list_ptr_array_add(ptr_array,
2013 display_name, number, i);
2018 g_free(display_name);
2020 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
2022 contacts_list_destroy(record_list, TRUE);
2026 static void __bluetooth_pb_get_list_name(BluetoothPbAgent *agent,
2027 PhoneBookType pb_type,
2028 const gchar *find_text,
2029 GPtrArray *ptr_array)
2032 contacts_query_h query;
2036 query = __bluetooth_pb_query_person_number();
2037 __bluetooth_pb_get_contact_list_name(agent, query,
2038 find_text, ptr_array);
2041 query = __bluetooth_pb_query_phone_log_incoming();
2042 __bluetooth_pb_get_phone_log_list_name(agent, query,
2043 find_text, ptr_array);
2046 query = __bluetooth_pb_query_phone_log_outgoing();
2047 __bluetooth_pb_get_phone_log_list_name(agent, query,
2048 find_text, ptr_array);
2051 query = __bluetooth_pb_query_phone_log_missed();
2052 __bluetooth_pb_get_phone_log_list_name(agent, query,
2053 find_text, ptr_array);
2056 query = __bluetooth_pb_query_phone_log_combined();
2057 __bluetooth_pb_get_phone_log_list_name(agent, query,
2058 find_text, ptr_array);
2065 contacts_query_destroy(query);
2069 static void __bluetooth_pb_list_ptr_array_add(GPtrArray *ptr_array,
2071 const gchar *number,
2075 GValue value = { 0, };
2076 gchar *temp_name = g_strdup(name);
2077 gchar *temp_number = g_strdup(number);
2079 g_value_init(&value, DBUS_STRUCT_STRING_STRING_UINT);
2080 g_value_take_boxed(&value,
2081 dbus_g_type_specialized_construct(DBUS_STRUCT_STRING_STRING_UINT));
2083 dbus_g_type_struct_set(&value,
2089 g_ptr_array_add(ptr_array, g_value_get_boxed(&value));
2095 static void __bluetooth_pb_list_ptr_array_free(gpointer data)
2098 GValue value = { 0, };
2101 gchar *number = NULL;
2106 g_value_init(&value, DBUS_STRUCT_STRING_STRING_UINT);
2107 g_value_set_boxed(&value, data);
2109 dbus_g_type_struct_get(&value,
2119 static void __bluetooth_pb_agent_signal_handler(int signum)
2123 g_main_loop_quit(mainloop);
2125 DBG("Terminate Bluetooth PBAP agent");
2131 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
2135 BluetoothPbAgent *agent;
2136 guint new_missed_call;
2138 DBG("Received contact changed cb");
2139 g_return_if_fail(BLUETOOTH_IS_PB_AGENT(user_data));
2140 agent = BLUETOOTH_PB_AGENT(user_data);
2142 g_object_ref(agent);
2143 g_signal_emit(agent, signals[CLEAR], 0);
2144 g_object_unref(agent);
2146 __bluetooth_pb_get_count_new_missed_call(&new_missed_call);
2148 if (new_missed_call > total_missed_call_count)
2149 unnotified_missed_call_count += new_missed_call - total_missed_call_count;
2151 INFO("Missed call count : #prev[%d], #current[%d], #unnotified[%d]",
2152 total_missed_call_count, new_missed_call, unnotified_missed_call_count);
2154 total_missed_call_count = new_missed_call;
2158 static void __bluetooth_pb_agent_timeout_add_seconds(BluetoothPbAgent *agent)
2161 g_return_if_fail(BLUETOOTH_IS_PB_AGENT(agent));
2163 if(agent->timeout_id)
2164 g_source_remove(agent->timeout_id);
2166 agent->timeout_id = g_timeout_add_seconds(BLUETOOTH_PB_AGENT_TIMEOUT,
2167 __bluetooth_pb_agent_timeout_calback,
2172 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data)
2175 BluetoothPbAgent *agent;
2177 g_return_val_if_fail(BLUETOOTH_IS_PB_AGENT(user_data), FALSE);
2179 agent = BLUETOOTH_PB_AGENT(user_data);
2180 agent->timeout_id = 0;
2183 g_main_loop_quit(mainloop);
2189 static void __bluetooth_pb_tel_callback(TapiHandle *handle,
2195 BluetoothPbAgent *agent;
2196 TelSimMsisdnList_t *number;
2198 g_return_if_fail(BLUETOOTH_IS_PB_AGENT(user_data));
2200 agent = BLUETOOTH_PB_AGENT(user_data);
2202 __bluetooth_pb_agent_dbus_init(agent);
2205 number = (TelSimMsisdnList_t *)data;
2206 agent->tel_number = g_strdup(number->list[0].num);
2209 tel_deinit(agent->tapi_handle);
2210 agent->tapi_handle = NULL;
2214 static void __bluetooth_pb_agent_dbus_init(BluetoothPbAgent *agent)
2218 GError *error = NULL;
2220 agent->bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
2222 if (error != NULL) {
2223 ERR("Couldn't connect to system bus[%s]\n", error->message);
2224 g_error_free(error);
2228 agent->proxy = dbus_g_proxy_new_for_name(agent->bus,
2231 DBUS_INTERFACE_DBUS);
2233 if (agent->proxy == NULL) {
2234 ERR("Failed to get a proxy for D-Bus\n");
2238 if (!dbus_g_proxy_call(agent->proxy,
2239 "RequestName", &error,
2240 G_TYPE_STRING, BT_PB_SERVICE_NAME,
2243 G_TYPE_UINT, &result,
2245 if (error != NULL) {
2246 ERR("RequestName RPC failed[%s]\n", error->message);
2247 g_error_free(error);
2250 g_object_unref(agent->proxy);
2251 agent->proxy = NULL;
2255 DBG("result : %d %d\n", result, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
2256 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
2257 ERR("Failed to get the primary well-known name.\n");
2259 g_object_unref(agent->proxy);
2260 agent->proxy = NULL;
2265 g_object_unref(agent->proxy);
2266 agent->proxy = NULL;
2268 dbus_g_connection_register_g_object(agent->bus,
2269 BT_PB_SERVICE_OBJECT_PATH,
2274 static gboolean bluetooth_pb_destroy_agent(BluetoothPbAgent *agent,
2275 DBusGMethodInvocation *context)
2278 g_main_loop_quit(mainloop);
2286 BluetoothPbAgent *agent;
2288 gint ret = EXIT_SUCCESS;
2291 struct sigaction sa;
2292 DBG("Starting Bluetooth PBAP agent");
2296 mainloop = g_main_loop_new(NULL, FALSE);
2297 if (mainloop == NULL) {
2298 ERR("Couldn't create GMainLoop\n");
2299 return EXIT_FAILURE;
2302 agent = g_object_new(BLUETOOTH_PB_TYPE_AGENT, NULL);
2304 /* connect contact */
2305 if (contacts_connect() != CONTACTS_ERROR_NONE) {
2306 ERR("Can not connect contacts server\n");
2307 g_object_unref(agent);
2308 return EXIT_FAILURE;
2311 __bluetooth_pb_get_count_new_missed_call(&total_missed_call_count);
2313 if (contacts_db_add_changed_cb(_contacts_contact._uri,
2314 __bluetooth_pb_contact_changed,
2315 (void *)agent) != CONTACTS_ERROR_NONE) {
2316 ERR("Can not add changed callback");
2319 if (contacts_db_add_changed_cb(_contacts_phone_log._uri,
2320 __bluetooth_pb_contact_changed,
2321 (void *)agent) != CONTACTS_ERROR_NONE) {
2322 ERR("Can not add changed callback");
2326 memset(&sa, 0, sizeof(sa));
2327 sa.sa_handler = __bluetooth_pb_agent_signal_handler;
2328 sigaction(SIGTERM, &sa, NULL);
2329 sigaction(SIGINT, &sa, NULL);
2332 agent->tapi_handle = tel_init(NULL);
2333 tapi_result = tel_get_sim_msisdn(agent->tapi_handle,
2334 __bluetooth_pb_tel_callback, agent);
2336 if (tapi_result != TAPI_API_SUCCESS) {
2337 __bluetooth_pb_agent_dbus_init(agent);
2341 __bluetooth_pb_agent_timeout_add_seconds(agent);
2343 g_main_loop_run(mainloop);
2345 if (contacts_db_remove_changed_cb(_contacts_phone_log._uri,
2346 __bluetooth_pb_contact_changed,
2347 (void *)agent) != CONTACTS_ERROR_NONE) {
2348 ERR("Cannot remove changed callback");
2351 if (contacts_db_remove_changed_cb(_contacts_contact._uri,
2352 __bluetooth_pb_contact_changed,
2353 (void *)agent) != CONTACTS_ERROR_NONE) {
2354 ERR("Cannot remove changed callback");
2357 if (contacts_disconnect() != CONTACTS_ERROR_NONE)
2358 ERR("contacts_disconnect failed \n");
2360 g_signal_emit(agent, signals[CLEAR], 0);
2362 g_object_unref(agent);
2364 DBG("Terminate Bluetooth PBAP agent");