2 * bluetooth-ag-phonebook.c
4 * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Sangki Park <sangki79.park@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
9 * Rakesh MK <rakesh.mk@samsung.com>
10 * Anurag B <biradar.a@samsung.com>
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
26 #include "bluetooth-ag-phonebook.h"
27 #include "bluetooth-ag-agent.h"
28 #include "bluetooth-ag-handler.h"
31 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void);
33 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void);
35 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void);
37 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void);
39 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
40 static gchar *_bluetooth_pb_number_from_person_id(gint person_id);
42 static gchar *__bluetooth_pb_number_from_contact(contacts_record_h contact);
44 static gchar *__bluetooth_pb_name_from_contact(contacts_record_h contact);
47 PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name)
53 if (g_strcmp0(name, "\"ME\"") == 0)
56 if (g_strcmp0(name, "\"RC\"") == 0)
59 if (g_strcmp0(name, "\"DC\"") == 0)
62 if (g_strcmp0(name, "\"MC\"") == 0)
64 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
65 if (g_strcmp0(name, "\"SM\"") == 0)
73 static bool _bt_is_sim_addressbook(const char *addressbook)
75 return g_str_has_prefix(addressbook, SIM_ADDRESSBOOK_PREFIX);
78 static bool __bt_is_matching_addressbook(const char *addressbook_name,
81 bool is_sim_addressbook = _bt_is_sim_addressbook(addressbook_name);
83 if ((is_sim_addressbook == false
84 && addressbook == PBAP_ADDRESSBOOK_PHONE) ||
85 (is_sim_addressbook == true
86 && addressbook == PBAP_ADDRESSBOOK_SIM))
92 static contacts_query_h __bluetooth_pb_query_person(int addressbook)
95 contacts_query_h query = NULL;
96 contacts_filter_h filter = NULL;
97 contacts_list_h recordList = NULL;
98 contacts_record_h record = NULL;
100 char *addressbook_name = NULL;
101 int address_book_id = 0;
105 bool is_first_condition = true;
106 DBG("Addressbook [%d]", addressbook);
108 status = contacts_query_create(_contacts_person_contact._uri, &query);
110 ERR("Could not create query");
114 /* Create addressbook Filter*/
115 contacts_db_get_all_records(_contacts_address_book._uri, 0, 0,
117 contacts_filter_create(_contacts_person_contact._uri, &filter);
118 contacts_list_get_count(recordList, &count);
119 INFO("COUNT %d", count);
120 for (i = 0; i < count; i++) {
121 status = contacts_list_get_current_record_p(recordList, &record);
122 if (status != CONTACTS_ERROR_NONE) {
123 ERR("Contact list get api failed %d", status);
126 status = contacts_record_get_str_p(record, _contacts_address_book.name,
128 if (status != CONTACTS_ERROR_NONE) {
129 ERR("Contact record get api failed %d", status);
132 status = contacts_record_get_int(record, _contacts_address_book.id,
134 if (status != CONTACTS_ERROR_NONE) {
135 ERR("contacts record get int api failed %d", status);
139 DBG("Addressbook ID: [%d] Addressbook Name: [%s]",
140 address_book_id, addressbook_name);
142 if (__bt_is_matching_addressbook(addressbook_name,
144 if (is_first_condition)
145 is_first_condition = false;
147 contacts_filter_add_operator(filter,
148 CONTACTS_FILTER_OPERATOR_OR);
149 DBG("SELECTED Addressbook ID: [%d] Addressbook Name: [%s]",
150 address_book_id, addressbook_name);
151 status = contacts_filter_add_int(filter,
152 _contacts_person_contact.address_book_id,
153 CONTACTS_MATCH_EQUAL, address_book_id);
154 if (status != CONTACTS_ERROR_NONE)
155 ERR("Contact filter add failed %d", status);
158 if (contacts_list_next(recordList) != CONTACTS_ERROR_NONE)
162 contacts_list_destroy(recordList, true);
164 status = contacts_query_set_filter(query, filter);
165 if (status != CONTACTS_ERROR_NONE)
166 ERR("Could not Apply Filter");
168 contacts_filter_destroy(filter);
173 gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
177 contacts_query_h query = NULL;
181 INFO("Pb type %d", pb_type);
184 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
187 query = __bluetooth_pb_query_phone_log_incoming();
190 query = __bluetooth_pb_query_phone_log_outgoing();
193 query = __bluetooth_pb_query_phone_log_missed();
196 query = __bluetooth_pb_query_phone_log_combined();
198 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
200 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
210 status = contacts_db_get_count_with_query(query, &signed_count);
212 if (status != CONTACTS_ERROR_NONE) {
213 contacts_query_destroy(query);
217 contacts_query_destroy(query);
219 if (signed_count < 0)
222 *count = (gint) signed_count;
228 void __bluetooth_pb_list_ptr_array_add(const gchar *name,
233 gchar *temp_name = g_strdup(name);
234 gchar *temp_number = NULL;
236 temp_number = g_strdup("");
238 temp_number = g_strdup(number);
240 _bt_read_phonebook_entries_indicator(temp_name,
241 temp_number, handle);
248 void __bluetooth_pb_list_ptr_array_add_by_name(const gchar *name,
253 gchar *temp_name = g_strdup(name);
254 gchar *temp_number = NULL;
256 temp_number = g_strdup("");
258 temp_number = g_strdup(number);
260 _bt_read_phonebook_entries_indicator_by_name(temp_name,
261 temp_number, handle);
268 contacts_query_h __bluetooth_pb_query_person_number(void)
271 contacts_query_h query = NULL;
275 status = contacts_query_create(_contacts_person_number._uri,
278 if (status != CONTACTS_ERROR_NONE)
285 void __bluetooth_pb_get_contact_list_number(
286 contacts_query_h query,
292 contacts_list_h record_list = NULL;
308 offset = to - from + 1;
314 status = contacts_db_get_records_with_query(query,
318 if (status != CONTACTS_ERROR_NONE) {
319 contacts_list_destroy(record_list, TRUE);
323 status = contacts_list_first(record_list);
325 if (status != CONTACTS_ERROR_NONE) {
326 contacts_list_destroy(record_list, TRUE);
331 contacts_record_h record;
337 status = contacts_list_get_current_record_p(record_list,
340 if (status != CONTACTS_ERROR_NONE)
346 contacts_record_get_str_p(record,
347 _contacts_person_number.display_name,
349 contacts_record_get_str_p(record,
350 _contacts_person_number.number,
353 __bluetooth_pb_list_ptr_array_add(display_name, number, i);
356 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
359 contacts_list_destroy(record_list, TRUE);
363 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
364 static gchar *__bluetooth_pb_number_from_contact(contacts_record_h contact)
374 status = contacts_record_get_child_record_count(contact,
375 _contacts_contact.number,
378 if (status != CONTACTS_ERROR_NONE)
381 for (i = 0; i < count; i++) {
382 contacts_record_h number = NULL;
386 bool is_default = false;
388 status = contacts_record_get_child_record_at_p(contact,
389 _contacts_contact.number, i, &number);
391 if (status != CONTACTS_ERROR_NONE)
394 status = contacts_record_get_bool(number,
395 _contacts_number.is_default,
398 if (status != CONTACTS_ERROR_NONE)
401 if (is_default == FALSE)
404 status = contacts_record_get_str_p(number,
405 _contacts_number.number,
408 if (status != CONTACTS_ERROR_NONE)
417 /* get first number */
421 contacts_record_h number = NULL;
423 status = contacts_record_get_child_record_at_p(contact,
424 _contacts_contact.number, 0, &number);
426 if (status != CONTACTS_ERROR_NONE)
429 status = contacts_record_get_str_p(number,
430 _contacts_number.number,
433 if (status != CONTACTS_ERROR_NONE)
443 gchar *_bluetooth_pb_number_from_person_id(gint person_id)
446 contacts_record_h person = NULL;
447 contacts_record_h contact = NULL;
454 status = contacts_db_get_record(_contacts_person._uri,
458 if (status != CONTACTS_ERROR_NONE)
462 status = contacts_record_get_int(person,
463 _contacts_person.display_contact_id,
466 if (status != CONTACTS_ERROR_NONE) {
467 contacts_record_destroy(person, TRUE);
471 status = contacts_db_get_record(_contacts_contact._uri,
475 if (status != CONTACTS_ERROR_NONE) {
476 contacts_record_destroy(person, TRUE);
480 str = __bluetooth_pb_number_from_contact(contact);
482 contacts_record_destroy(contact, TRUE);
483 contacts_record_destroy(person, TRUE);
489 gchar *_bluetooth_pb_name_from_person_id(gint person_id)
492 contacts_record_h person = NULL;
493 contacts_record_h contact = NULL;
500 status = contacts_db_get_record(_contacts_person._uri,
504 if (status != CONTACTS_ERROR_NONE)
507 status = contacts_record_get_int(person,
508 _contacts_person.display_contact_id,
511 if (status != CONTACTS_ERROR_NONE) {
512 contacts_record_destroy(person, TRUE);
516 status = contacts_db_get_record(_contacts_contact._uri,
520 if (status != CONTACTS_ERROR_NONE) {
521 contacts_record_destroy(person, TRUE);
525 str = __bluetooth_pb_name_from_contact(contact);
527 contacts_record_destroy(contact, TRUE);
528 contacts_record_destroy(person, TRUE);
534 static gchar *__bluetooth_pb_vcard_escape(const gchar *str)
545 escaped = g_string_new(NULL);
550 while (*pos != '\0') {
552 g_string_append_len(escaped, st, (pos - st));
553 g_string_append(escaped, "\\;");
562 g_string_append_len(escaped, st, (pos - st));
564 return g_string_free(escaped, FALSE);
567 static gchar *__bluetooth_pb_name_from_contact(contacts_record_h contact)
570 contacts_record_h name = NULL;
578 gint name_val[] = { _contacts_name.last,
579 _contacts_name.first,
580 _contacts_name.addition,
581 _contacts_name.prefix,
582 _contacts_name.suffix };
585 status = contacts_record_get_child_record_at_p(contact,
586 _contacts_contact.name, 0, &name);
588 if (status != CONTACTS_ERROR_NONE)
591 str = g_string_new(NULL);
593 for (i = 0; i < name_size; i++) {
595 gchar *escape = NULL;
597 status = contacts_record_get_str_p(name, name_val[i], &tmp);
599 if (status != CONTACTS_ERROR_NONE)
602 escape = __bluetooth_pb_vcard_escape(tmp);
604 g_string_append(str, escape);
610 return g_string_free(str, FALSE);
613 static void __bluetooth_pb_get_contact_list(
614 contacts_query_h query,
620 contacts_list_h record_list = NULL;
625 status = contacts_db_get_records_with_query(query,
626 start_index, end_index, &record_list);
628 if (status != CONTACTS_ERROR_NONE) {
629 contacts_list_destroy(record_list, TRUE);
633 status = contacts_list_first(record_list);
635 if (status != CONTACTS_ERROR_NONE) {
636 contacts_list_destroy(record_list, TRUE);
641 contacts_record_h record;
646 status = contacts_list_get_current_record_p(record_list,
649 if (status != CONTACTS_ERROR_NONE)
653 status = contacts_record_get_int(record,
654 _contacts_person_contact.person_id,
657 if (status != CONTACTS_ERROR_NONE)
663 name = _bluetooth_pb_name_from_person_id(id);
664 number = _bluetooth_pb_number_from_person_id(id);
666 __bluetooth_pb_list_ptr_array_add(name, number, id);
672 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
675 contacts_list_destroy(record_list, TRUE);
679 static void __bluetooth_pb_get_contact_list_by_name(
680 contacts_query_h query,
687 contacts_list_h record_list = NULL;
689 contacts_record_h record;
696 status = contacts_db_get_records_with_query(query,
697 start_index, end_index, &record_list);
699 if (status != CONTACTS_ERROR_NONE) {
700 contacts_list_destroy(record_list, TRUE);
704 status = contacts_list_first(record_list);
706 if (status != CONTACTS_ERROR_NONE) {
707 contacts_list_destroy(record_list, TRUE);
713 status = contacts_list_get_current_record_p(record_list,
716 if (status != CONTACTS_ERROR_NONE)
720 status = contacts_record_get_int(record,
721 _contacts_person_contact.person_id,
724 if (status != CONTACTS_ERROR_NONE)
727 name = _bluetooth_pb_name_from_person_id(id);
728 number = _bluetooth_pb_number_from_person_id(id);
729 pb_name_up = g_ascii_strup(name, strlen(name));
731 if (g_strstr_len(pb_name_up, -1, str) != NULL)
732 __bluetooth_pb_list_ptr_array_add_by_name(name, number, id);
740 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
743 contacts_list_destroy(record_list, TRUE);
748 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void)
753 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
754 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
755 CONTACTS_PLOG_TYPE_VOICE_REJECT,
756 CONTACTS_PLOG_TYPE_VIDEO_REJECT
760 return __bluetooth_pb_query_phone_log(match, size);
763 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void)
768 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
769 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING
773 return __bluetooth_pb_query_phone_log(match, size);
776 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void)
781 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
782 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
783 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
784 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN
788 return __bluetooth_pb_query_phone_log(match, size);
791 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void)
796 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
797 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
798 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
799 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING,
800 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
801 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
802 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
803 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN,
804 CONTACTS_PLOG_TYPE_VOICE_REJECT,
805 CONTACTS_PLOG_TYPE_VIDEO_REJECT
809 return __bluetooth_pb_query_phone_log(match, size);
813 gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
821 for (i = 0; i < size; i++) {
824 status = contacts_filter_add_operator(filter,
825 CONTACTS_FILTER_OPERATOR_OR);
827 if (status != CONTACTS_ERROR_NONE)
831 status = contacts_filter_add_int(filter,
832 _contacts_phone_log.log_type,
833 CONTACTS_MATCH_EQUAL,
836 if (status != CONTACTS_ERROR_NONE)
841 return CONTACTS_ERROR_NONE;
844 contacts_query_h __bluetooth_pb_query_phone_log(gint *match,
848 contacts_query_h query = NULL;
849 contacts_filter_h filter = NULL;
853 status = contacts_query_create(_contacts_phone_log._uri,
856 if (status != CONTACTS_ERROR_NONE)
859 status = contacts_filter_create(_contacts_phone_log._uri, &filter);
861 if (status != CONTACTS_ERROR_NONE) {
862 contacts_query_destroy(query);
866 status = __bluetooth_pb_phone_log_filter_append(filter, match, size);
868 if (status != CONTACTS_ERROR_NONE) {
869 contacts_filter_destroy(filter);
870 contacts_query_destroy(query);
874 status = contacts_query_set_filter(query, filter);
876 if (status != CONTACTS_ERROR_NONE) {
877 contacts_filter_destroy(filter);
878 contacts_query_destroy(query);
882 status = contacts_query_set_sort(query,
883 _contacts_phone_log.log_time,
886 if (status != CONTACTS_ERROR_NONE) {
887 contacts_filter_destroy(filter);
888 contacts_query_destroy(query);
892 contacts_filter_destroy(filter);
898 static gint __bluetooth_pb_person_id_from_phonelog_id(gint phonelog_id)
901 contacts_query_h query = NULL;
902 contacts_filter_h filter = NULL;
903 contacts_list_h record_list = NULL;
905 contacts_record_h phone_log = NULL;
906 contacts_record_h record = NULL;
911 status = contacts_db_get_record(_contacts_phone_log._uri,
915 if (status != CONTACTS_ERROR_NONE)
918 status = contacts_record_get_int(phone_log,
919 _contacts_phone_log.person_id,
922 if (status != CONTACTS_ERROR_NONE) {
923 contacts_record_destroy(phone_log, TRUE);
927 contacts_record_destroy(phone_log, TRUE);
932 status = contacts_filter_create(_contacts_person_phone_log._uri,
935 if (status != CONTACTS_ERROR_NONE)
939 status = contacts_filter_add_int(filter,
940 _contacts_person_phone_log.log_id,
941 CONTACTS_MATCH_EQUAL,
944 if (status != CONTACTS_ERROR_NONE)
947 status = contacts_query_create(_contacts_person_phone_log._uri, &query);
949 if (status != CONTACTS_ERROR_NONE)
952 status = contacts_query_set_filter(query, filter);
954 if (status != CONTACTS_ERROR_NONE)
957 status = contacts_db_get_records_with_query(query, -1, -1,
960 if (status != CONTACTS_ERROR_NONE)
963 status = contacts_list_first(record_list);
965 if (status != CONTACTS_ERROR_NONE)
968 status = contacts_list_get_current_record_p(record_list, &record);
970 if (status != CONTACTS_ERROR_NONE)
973 status = contacts_record_get_int(record,
974 _contacts_person_phone_log.person_id,
977 if (status != CONTACTS_ERROR_NONE)
981 if (record_list != NULL)
982 contacts_list_destroy(record_list, TRUE);
984 contacts_filter_destroy(filter);
987 contacts_query_destroy(query);
993 gchar *_bluetooth_pb_fn_from_person_id(gint person_id)
996 contacts_record_h person = NULL;
1002 status = contacts_db_get_record(_contacts_person._uri,
1006 if (status != CONTACTS_ERROR_NONE)
1009 status = contacts_record_get_str(person,
1010 _contacts_person.display_name,
1013 if (status != CONTACTS_ERROR_NONE)
1016 contacts_record_destroy(person, TRUE);
1022 gchar *_bluetooth_pb_fn_from_phonelog_id(gint phonelog_id)
1028 person_id = __bluetooth_pb_person_id_from_phonelog_id(phonelog_id);
1031 str = _bluetooth_pb_fn_from_person_id(person_id);
1040 static void __bluetooth_pb_get_phone_log_list_number(contacts_query_h query,
1046 contacts_list_h record_list = NULL;
1065 offset = to - from + 1;
1071 status = contacts_db_get_records_with_query(query,
1075 if (status != CONTACTS_ERROR_NONE) {
1076 contacts_list_destroy(record_list, TRUE);
1080 status = contacts_list_first(record_list);
1081 if (status != CONTACTS_ERROR_NONE) {
1082 contacts_list_destroy(record_list, TRUE);
1087 contacts_record_h record = NULL;
1091 gchar *display_name;
1095 status = contacts_list_get_current_record_p(record_list,
1098 if (status != CONTACTS_ERROR_NONE)
1102 status = contacts_record_get_int(record,
1103 _contacts_phone_log.id,
1105 if (status != CONTACTS_ERROR_NONE) {
1106 ERR("contact_record_get_int api failed %d", status);
1110 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1113 contacts_record_get_str_p(record,
1114 _contacts_phone_log.address,
1118 __bluetooth_pb_list_ptr_array_add(display_name, number, i);
1122 g_free(display_name);
1124 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1127 contacts_list_destroy(record_list, TRUE);
1131 void __bluetooth_pb_get_list_entries_by_name(
1132 PhoneBookType pb_type,
1139 contacts_query_h query;
1143 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1144 __bluetooth_pb_get_contact_list_by_name(query,
1145 start_index, end_index, str, count);
1149 query = __bluetooth_pb_query_phone_log_incoming();
1150 __bluetooth_pb_get_contact_list_by_name(query,
1151 start_index, end_index, str, count);
1154 query = __bluetooth_pb_query_phone_log_outgoing();
1155 __bluetooth_pb_get_contact_list_by_name(query,
1156 start_index, end_index, str, count);
1159 query = __bluetooth_pb_query_phone_log_missed();
1160 __bluetooth_pb_get_contact_list_by_name(query,
1161 start_index, end_index, str, count);
1164 query = __bluetooth_pb_query_phone_log_combined();
1165 __bluetooth_pb_get_contact_list_by_name(query,
1166 start_index, end_index, str, count);
1168 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1170 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1171 __bluetooth_pb_get_contact_list_by_name(query,
1172 start_index, end_index, str, count);
1180 contacts_query_destroy(query);
1184 void __bluetooth_pb_get_list_number(
1185 PhoneBookType pb_type,
1191 contacts_query_h query;
1195 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1196 __bluetooth_pb_get_contact_list(query,
1197 start_index, end_index, count);
1201 query = __bluetooth_pb_query_phone_log_incoming();
1202 __bluetooth_pb_get_phone_log_list_number(query,
1203 start_index, end_index, count);
1206 query = __bluetooth_pb_query_phone_log_outgoing();
1207 __bluetooth_pb_get_phone_log_list_number(query,
1208 start_index, end_index, count);
1211 query = __bluetooth_pb_query_phone_log_missed();
1212 __bluetooth_pb_get_phone_log_list_number(query,
1213 start_index, end_index, count);
1216 query = __bluetooth_pb_query_phone_log_combined();
1217 __bluetooth_pb_get_phone_log_list_number(query,
1218 start_index, end_index, count);
1220 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1222 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1223 __bluetooth_pb_get_contact_list(query,
1224 start_index, end_index, count);
1232 contacts_query_destroy(query);