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.lee@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.
36 #include <image_util.h>
37 #include <image_util_internal.h>
38 #include <libexif/exif-data.h>
41 #include "bluetooth_pb_vcard.h"
42 #include "bluetooth_pb_agent.h"
44 #define VCARD_FORMAT_2_1 0x0
45 #define VCARD_FORMAT_3_0 0x1
47 #define VCARD_VERSION (0x1)
48 #define VCARD_FN (0x1 << 1)
49 #define VCARD_N (0x1 << 2)
50 #define VCARD_PHOTO (0x1 << 3)
51 #define VCARD_BDAY (0x1 << 4)
52 #define VCARD_ADR (0x1 << 5)
53 #define VCARD_LABEL (0x1 << 6) /* not supported */
54 #define VCARD_TEL (0x1 << 7)
55 #define VCARD_EMAIL (0x1 << 8)
56 #define VCARD_MAILER (0x1 << 9) /* not supported */
57 #define VCARD_TZ (0x1 << 10) /* not supported */
58 #define VCARD_GEO (0x1 << 11) /* not supported */
59 #define VCARD_TITLE (0x1 << 12)
60 #define VCARD_ROLE (0x1 << 13)
61 #define VCARD_LOGO (0x1 << 14) /* not supported */
62 #define VCARD_AGENT (0x1 << 15) /* not supported */
63 #define VCARD_ORG (0x1 << 16)
64 #define VCARD_NOTE (0x1 << 17)
65 #define VCARD_REV (0x1 << 18)
66 #define VCARD_SOUND (0x1 << 19) /* not supported */
67 #define VCARD_URL (0x1 << 20)
68 #define VCARD_UID (0x1 << 21)
69 #define VCARD_KEY (0x1 << 22) /* not supported */
70 #define VCARD_NICKNAME (0x1 << 23)
71 #define VCARD_CATEGORIES (0x1 << 24) /* not supported */
72 #define VCARD_PROID (0x1 << 25) /* not supported */
73 #define VCARD_CLASS (0x1 << 26) /* not supported */
74 #define VCARD_SORT_STRING (0x1 << 27) /* not supported */
75 #define VCARD_X_IRMC_CALL_DATETIME (0x1 << 28)
78 #define LINEBREAK_LEN 75
84 contacts_record_h person;
88 gchar dest_thumb_path[255];
89 #define PBAP_IMAGE_THUMB_SIZE 48
90 #define PBAP_THMB_PATH "_thumb"
91 #define PBAP_THUMB_FILE_SIZE 4096
92 #define PBAP_IMAGE_ENCODE_QUALITY 90
93 #define PBAP_SECURITY_FILE_GROUP 6005
94 #define PBAP_SECURITY_DEFAULT_PERMISSION 0660
95 #define PBAP_SECURITY_DIR_DEFAULT_PERMISSION 0770
96 #define PBAP_SECURITY_IMAGE_PERMISSION 0440
98 static gchar *__bluetooth_pb_vcard_escape(const gchar *str);
100 static gchar *__bluetooth_pb_vcard_strv_concat(gchar **strv,
101 const gchar *delimeter);
103 static gboolean __bluetooth_pb_vcard_qp_encode_check(const gchar *str);
105 static gint __bluetooth_pb_vcard_qp_encode_strlen(const gchar *str,
108 static void __bluetooth_pb_vcard_qp_encode_append_to_hex(GString *string,
113 static void __bluetooth_pb_vcard_qp_encode_append_printable_c(GString *string,
117 static void __bluetooth_pb_vcard_qp_encode_append(GString *string,
122 static gchar *__bluetooth_pb_vcard_qp_encode(const gchar *str);
124 static void __bluetooth_pb_vcard_append_param_v21(GString *string,
127 static void __bluetooth_pb_vcard_append_qp_encode_v21(GString *string,
132 static void __bluetooth_pb_vcard_append_base64_encode_v21(GString *string,
139 static void __bluetooth_pb_vcard_append_n_v21(GString *string,
140 contacts_record_h contact);
142 static void __bluetooth_pb_vcard_append_phonetic_first_v21(GString *string,
143 contacts_record_h contact);
145 static void __bluetooth_pb_vcard_append_tel_v21(GString *string,
146 contacts_record_h conatct);
148 static void __bluetooth_pb_vcard_append_fn_v21(GString *string,
149 contacts_record_h person);
151 static void __bluetooth_pb_vcard_append_photo_v21(GString *string,
152 contacts_record_h person);
154 static void __bluetooth_pb_vcard_append_bday_v21(GString *string,
155 contacts_record_h contact);
157 static void __bluetooth_pb_vcard_append_adr_v21(GString *string,
158 contacts_record_h contact);
160 static void __bluetooth_pb_vcard_append_email_v21(GString *string,
161 contacts_record_h contact);
163 static void __bluetooth_pb_vcard_append_title_v21(GString *string,
164 contacts_record_h contact);
166 static void __bluetooth_pb_vcard_append_role_v21(GString *string,
167 contacts_record_h contact);
169 static void __bluetooth_pb_vcard_append_org_v21(GString *string,
170 contacts_record_h contact);
172 static void __bluetooth_pb_vcard_append_note_v21(GString *string,
173 contacts_record_h contact);
175 static void __bluetooth_pb_vcard_append_rev_v21(GString *string,
176 contacts_record_h contact);
178 static void __bluetooth_pb_vcard_append_url_v21(GString *string,
179 contacts_record_h contact);
181 static void __bluetooth_pb_vcard_append_uid_v21(GString *string,
182 contacts_record_h contact);
184 static void __bluetooth_pb_vcard_append_v30(GString *string,
189 static void __bluetooth_pb_vcard_remove_v30(GString *string,
190 const gchar *property_name);
192 static gchar *__bluetooth_pb_vcard_filter_v30(const gchar *vcard,
195 static gchar *__bluetooth_pb_vcard_real_contact_valist_v21(gint person_id,
198 const gchar *first_name,
201 static gchar *__bluetooth_pb_vcard_real_contact_valist_v30(gint person_id,
204 const gchar *first_name,
207 static gchar *__bluetooth_pb_vcard_real_contact_with_properties(gint person_id,
211 const gchar *first_name,
214 static gchar *__bluetooth_pb_vcard_real_call_v21(gint phonelog_id,
218 static gchar *__bluetooth_pb_vcard_real_call_v30(gint phonelog_id,
222 static gchar *__bluetooth_pb_vcard_real_call(gint phonelog_id,
227 static gchar **__bluetooth_pb_contact_add_field_str(contacts_record_h record,
231 static gchar **__bluetooth_pb_contact_tel_param(contacts_record_h number);
233 static gchar *__bluetooth_pb_contact_photo_type(const gchar *filename);
235 static gchar **__bluetooth_pb_contact_addr(contacts_record_h address);
237 static gchar **__bluetooth_pb_contact_addr_param(contacts_record_h address);
239 static gchar *__bluetooth_pb_phonelog_datetime(gint phonelog_id);
241 static gchar *__bluetooth_pb_name_from_contact(contacts_record_h contact);
243 static gchar *__bluetooth_pb_phonetic_name_from_contact(contacts_record_h contact);
245 static gchar *__bluetooth_pb_number_from_contact(contacts_record_h contact);
247 static gint __bluetooth_pb_person_id_from_phonelog_id(gint phonelog_id);
250 static gchar *__bluetooth_pb_vcard_escape(const gchar *str)
261 escaped = g_string_new(NULL);
266 while (*pos != '\0') {
268 g_string_append_len(escaped, st, (pos - st));
269 g_string_append(escaped, "\\;");
278 g_string_append_len(escaped, st, (pos - st));
280 return g_string_free(escaped, FALSE);
283 static gchar *__bluetooth_pb_vcard_strv_concat(gchar **strv,
284 const gchar *separator)
287 GString *string = g_string_new(NULL);
290 for (i = 0; strv[i] != NULL; i++) {
292 g_string_append(string, ";");
294 g_string_append(string, strv[i]);
298 return g_string_free(string, FALSE);
301 static gboolean __bluetooth_pb_vcard_qp_encode_check(const gchar *str)
310 while (*pos != '\0') {
311 /* ascii code ' ' : 32, '~' : 126 */
312 if ((guchar)*pos < ' ' || (guchar)*pos > '~')
321 /* get string length, which convert to quoted-printable encoding */
322 static gint __bluetooth_pb_vcard_qp_encode_strlen(const gchar *str,
334 if (strlen(str) < len)
339 while (*pos != '\0' && (((pos - str) < length) || length < 0)) {
340 if ((guchar)*pos == '\t') {
346 if ((guchar)*pos < ' ' || (guchar)*pos == '=') {
352 /* check no-ascii utf-8 character */
353 if ((guchar)*pos > '~') {
357 next = g_utf8_next_char(pos);
359 count += QP_ENC_LEN * (next - pos);
372 /* convert to quoted printable code */
373 static void __bluetooth_pb_vcard_qp_encode_append_to_hex(GString *string,
381 if (str == NULL || len == 0)
384 /* add soft linebreak when it exceed */
385 if ((*line_pos + (QP_ENC_LEN * len) > LINEBREAK_LEN)) {
386 g_string_append(string, "=\r\n");
390 for (i = 0; i < len; i++) {
391 g_string_append_printf(string, "=%02X", (guchar)*(str+i));
392 *line_pos += QP_ENC_LEN;
397 /* append plain visiable ascii character */
398 static void __bluetooth_pb_vcard_qp_encode_append_printable_c(GString *string,
403 /* add soft linebreak when it exceed */
404 if (*line_pos + 1 > LINEBREAK_LEN) {
405 g_string_append(string, "=\r\n");
408 g_string_append_c(string, ch);
413 static void __bluetooth_pb_vcard_qp_encode_append(GString *string,
426 encode_len = __bluetooth_pb_vcard_qp_encode_strlen(str, len);
428 /* add soft linebreak when it exceed */
429 if (((*line_pos + encode_len) > LINEBREAK_LEN) && (*line_pos > 1)) {
430 g_string_append(string, "=\r\n");
437 pos = ((gchar *)str) + i;
439 /* converts invisiable character and escape character '='
440 to quoted-printable */
441 if ((guchar)*pos != '\t' &&
442 ((guchar)*pos < ' ' || (guchar)*pos == '=')) {
443 __bluetooth_pb_vcard_qp_encode_append_to_hex(string,
450 /* converts non-ascii utf-8 character to quoted-printable */
451 if ((guchar)*pos > '~') {
455 next = g_utf8_next_char(pos);
458 __bluetooth_pb_vcard_qp_encode_append_to_hex(string,
459 pos, ch_len, line_pos);
465 __bluetooth_pb_vcard_qp_encode_append_printable_c(string, *pos,
472 static gchar *__bluetooth_pb_vcard_qp_encode(const gchar *str)
485 enc = g_string_new(NULL);
487 st_pos = (gchar *)str;
490 while (*pos != '\0') {
491 /* split string with given delimeter ' ' or '\t' */
492 if (*pos == ' ' || *pos == '\t') {
493 __bluetooth_pb_vcard_qp_encode_append(enc, st_pos,
494 (pos - st_pos), &line_pos);
502 /* split string with given delimeter '\r', '\n' or
504 if (*pos == '\r' || *pos == '\n') {
505 __bluetooth_pb_vcard_qp_encode_append(enc, st_pos,
506 (pos - st_pos), &line_pos);
508 /* converts newline to qp_encode with soft linebreak
509 for example, converts \r\n to =0D=0A=\r\n */
510 __bluetooth_pb_vcard_qp_encode_append_to_hex(enc,
511 "\r\n", 2, &line_pos);
512 g_string_append(enc, "=\r\n ");
516 if (*pos == '\r' && *(pos + 1) == '\n')
529 __bluetooth_pb_vcard_qp_encode_append(enc, st_pos,
530 (pos - st_pos), &line_pos);
533 return g_string_free(enc, FALSE);
536 static void __bluetooth_pb_vcard_append_param_v21(GString *string,
545 pos = (gchar *)param;
547 /* trim ';' on first */
548 while (*pos != '\0') {
556 g_string_append_printf(string, ";%s", pos);
560 static void __bluetooth_pb_vcard_append_qp_encode_v21(GString *string,
566 GString *property = NULL;
571 property = g_string_new(name);
572 __bluetooth_pb_vcard_append_param_v21(property, param);
574 if (__bluetooth_pb_vcard_qp_encode_check(value)) {
577 __bluetooth_pb_vcard_append_param_v21(property,
579 __bluetooth_pb_vcard_append_param_v21(property,
580 "ENCODING=QUOTED-PRINTABLE");
581 g_string_append(property, ":");
583 enc = __bluetooth_pb_vcard_qp_encode(value);
586 g_string_append(property, enc);
590 g_string_append(property, ":");
592 g_string_append(property , value);
595 g_string_append_printf(string, "%s\r\n", property->str);
597 g_string_free(property, TRUE);
602 static void __bluetooth_pb_vcard_append_base64_encode_v21(GString *string,
614 DBG("base 64 encoding\n");
616 g_string_append(string, name);
618 __bluetooth_pb_vcard_append_param_v21(string, param);
619 __bluetooth_pb_vcard_append_param_v21(string, "ENCODING=BASE64");
621 g_string_append(string, ":");
626 enc = g_base64_encode((const guchar *)value, len);
628 if (folding == FALSE) {
629 g_string_append(string, enc);
631 gint enc_len = strlen(enc);
634 /* count ' ' size for folding */
635 gint fline_len = LINEBREAK_LEN - 1;
637 for (i = 0; (i * fline_len) < enc_len; i++) {
638 g_string_append(string, "\r\n ");
639 if ((i * fline_len) + fline_len > enc_len)
640 g_string_append(string, enc + (i * fline_len));
642 g_string_append_len(string, enc +
643 (i * fline_len), fline_len);
646 /* some application requires more \r\n */
647 g_string_append(string, "\r\n");
649 g_string_append(string, "\r\n");
650 DBG("base 64 encoding\n");
656 static void __bluetooth_pb_vcard_append_n_v21(GString *string,
657 contacts_record_h contact)
662 str = __bluetooth_pb_name_from_contact(contact);
663 __bluetooth_pb_vcard_append_qp_encode_v21(string, "N", NULL, str);
669 static void __bluetooth_pb_vcard_append_phonetic_first_v21(GString *string,
670 contacts_record_h contact)
675 str = __bluetooth_pb_phonetic_name_from_contact(contact);
678 __bluetooth_pb_vcard_append_qp_encode_v21(string, "SOUND", "X-IRMC-N", str);
684 static void __bluetooth_pb_vcard_append_tel_v21(GString *string,
685 contacts_record_h contact)
693 status = contacts_record_get_child_record_count(contact,
694 _contacts_contact.number,
697 if (status != CONTACTS_ERROR_NONE)
700 for (i = 0; i < count; i++) {
701 contacts_record_h number = NULL;
703 gchar **paramv = NULL;
707 gchar *escaped = NULL;
709 status = contacts_record_get_child_record_at_p(contact,
710 _contacts_contact.number, i, &number);
712 if (status != CONTACTS_ERROR_NONE)
715 status = contacts_record_get_str_p(number,
716 _contacts_number.number,
719 if (status != CONTACTS_ERROR_NONE)
722 escaped = __bluetooth_pb_vcard_escape(tel);
724 paramv = __bluetooth_pb_contact_tel_param(number);
726 param = __bluetooth_pb_vcard_strv_concat(paramv, ";");
728 __bluetooth_pb_vcard_append_qp_encode_v21(string, "TEL", param,
738 static void __bluetooth_pb_vcard_append_fn_v21(GString *string,
739 contacts_record_h person)
747 status = contacts_record_get_str_p(person,
748 _contacts_person.display_name,
751 if (status != CONTACTS_ERROR_NONE)
754 fn = __bluetooth_pb_vcard_escape(tmp);
756 __bluetooth_pb_vcard_append_qp_encode_v21(string, "FN", NULL, fn);
761 static image_util_rotation_e __bt_pbap_get_rotation_info(const char *path)
766 image_util_rotation_e rotation = IMAGE_UTIL_ROTATION_NONE;
769 ed = exif_data_new_from_file(path);
771 ERR("exif_data_new_from_file : ExifData is NULL");
772 return IMAGE_UTIL_ROTATION_NONE;
775 entry = exif_data_get_entry(ed, EXIF_TAG_ORIENTATION);
777 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
778 orientation = (int)exif_get_short(entry->data, mByteOrder);
779 if (orientation < 0 || orientation > 8)
785 switch (orientation) {
786 case 1: /* Top-left */
787 rotation = IMAGE_UTIL_ROTATION_NONE;
789 case 2: /* Top-right */
790 rotation = IMAGE_UTIL_ROTATION_FLIP_HORZ;
792 case 3: /* Bottom-right */
793 rotation = IMAGE_UTIL_ROTATION_180;
795 case 4: /* Bottom-left */
796 rotation = IMAGE_UTIL_ROTATION_FLIP_VERT;
798 case 6: /* Right-top */
799 rotation = IMAGE_UTIL_ROTATION_90;
801 case 8: /* Left-bottom */
802 rotation = IMAGE_UTIL_ROTATION_270;
804 case 5: /* Left-top */
805 case 7: /* Right-bottom */
816 static bool __bt_pbap_image_util_supported_jpeg_colorspace_cb(
817 image_util_colorspace_e colorspace, void *user_data)
820 unsigned char *img_target = 0;
821 unsigned char *img_source = 0;
824 gchar *contents = NULL;
827 int resized_width = 0;
828 int resized_height = 0;
830 unsigned int size_decode = 0;
831 image_util_rotation_e rotation;
832 image_util_error_e ret = 0;
835 bt_image_info_t *info = (bt_image_info_t *)user_data;
836 rotation = __bt_pbap_get_rotation_info(info->src);
837 ret = image_util_decode_jpeg(info->src, colorspace, &img_source, &width,
838 &height, &size_decode);
839 if (ret != IMAGE_UTIL_ERROR_NONE) {
840 ERR("Can not decode");
841 memset(info, 0x00, sizeof(bt_image_info_t));
845 DBG("decoding completed width = %d, height = %d, size = %d\n", width,
846 height, size_decode);
847 if (width > PBAP_IMAGE_THUMB_SIZE || height > PBAP_IMAGE_THUMB_SIZE) {
848 if (width <= 0 || height <= 0) {
850 ERR("image size error(%d)", PBAP_IMAGE_THUMB_SIZE);
851 memset(info, 0x00, sizeof(bt_image_info_t));
855 if (width > height) {
856 resized_width = PBAP_IMAGE_THUMB_SIZE ;
857 resized_height = height * PBAP_IMAGE_THUMB_SIZE / width;
859 resized_height = PBAP_IMAGE_THUMB_SIZE;
860 resized_width = width * PBAP_IMAGE_THUMB_SIZE / height;
863 if (resized_height % 8)
864 resized_height += 8 - (resized_height % 8);
865 if (resized_width % 8)
866 resized_width += 8 - (resized_width % 8);
868 DBG("original size[%d, %d] changed to resize[%d,%d]", width,
869 height, resized_width, resized_height);
871 ret = image_util_calculate_buffer_size(resized_width,
876 img_target = g_malloc0(size_decode);
879 ret = image_util_resize(img_target, &resized_width,
880 &resized_height, img_source, width,
882 if (ret != IMAGE_UTIL_ERROR_NONE) {
883 ERR("image_util_resize failed(%d)", ret);
886 memset(info, 0x00, sizeof(bt_image_info_t));
891 resized_width = width;
892 resized_height = height;
893 img_target = img_source;
895 DBG("Resized w = %d, ht = %d, size = %d\n", width, height, size_decode);
897 if (IMAGE_UTIL_ROTATION_NONE != rotation) {
898 int rotated_width, rotated_height;
899 unsigned char *img_rotate = 0;
900 img_rotate = g_malloc0(size_decode);
901 ret = image_util_rotate(img_rotate, &rotated_width, &rotated_height,
902 rotation, img_target, resized_width,
903 resized_height, colorspace);
905 ERR("image_util_rotate failed");
906 resized_width = rotated_width;
907 resized_height = rotated_height;
909 img_target = img_rotate;
913 ret = image_util_encode_jpeg(img_target, resized_width, resized_height,
914 colorspace, PBAP_IMAGE_ENCODE_QUALITY,
917 if (ret != IMAGE_UTIL_ERROR_NONE) {
918 ERR("image_util_encode_jpeg failed(%d)", ret);
919 info->ret = CONTACTS_ERROR_INTERNAL;
922 DBG("Encoding done\n");
924 dest_fd = open(info->dest, O_RDONLY);
926 ERR("System : Open Failed(%d)", errno);
927 ERR_SECURE("Open : dest path(%s)", info->dest);
931 ret = fchown(dest_fd, getuid(), PBAP_SECURITY_FILE_GROUP);
933 ERR("fchown Failed(%d)", errno);
934 DBG_SECURE("fchown : dest path(%s)", info->dest);
939 ret = fchmod(dest_fd, PBAP_SECURITY_IMAGE_PERMISSION);
941 ERR("fchmod Failed(%d)", errno);
942 ERR_SECURE("fchmod : dest path(%s)", info->dest);
948 info->ret = CONTACTS_ERROR_NONE;
949 type = __bluetooth_pb_contact_photo_type(info->dest);
950 DBG("Cintact image thumb type is = %s\n", type);
953 param = g_strdup_printf("TYPE=%s", type);
957 if (g_file_get_contents(info->dest, &contents, &len, NULL) == FALSE) {
958 ERR("can not read file contents:%s\n", info->dest);
962 __bluetooth_pb_vcard_append_base64_encode_v21(info->string,
963 "PHOTO", param, contents, len, TRUE);
969 memset(info, 0x00, sizeof(bt_image_info_t));
970 DBG("Cintact image thumb created successfuly\n");
975 static void __bluetooth_pb_vcard_append_photo_v21(GString *string,
976 contacts_record_h person)
980 gchar *filename = NULL;
982 bt_image_info_t img_info;
983 struct stat stat_info;
986 gchar *contents = NULL;
990 status = contacts_record_get_str_p(person,
991 _contacts_person.image_thumbnail_path,
994 if (status != CONTACTS_ERROR_NONE || NULL == filename)
997 stat_info.st_size = 0;
999 if (0 > stat(filename, &stat_info))
1000 ERR("fstat failed, file does not exist %s", filename);
1002 if (PBAP_THUMB_FILE_SIZE > stat_info.st_size) {
1003 DBG_SECURE("File size small, so use thubnail %s\n", filename);
1005 type = __bluetooth_pb_contact_photo_type(filename);
1007 param = g_strdup_printf("TYPE=%s", type);
1011 if (g_file_get_contents(filename, &contents, &len, NULL) ==
1013 ERR("can not read file contents:%s\n", filename);
1017 DBG("Retrieved the contents of the file \n");
1018 __bluetooth_pb_vcard_append_base64_encode_v21(string,
1019 "PHOTO", param, contents, len, TRUE);
1026 ptr = strrchr(filename, '.');
1028 memset(dest_thumb_path, 0x00, sizeof(dest_thumb_path));
1029 g_strlcpy(dest_thumb_path, filename, ptr - filename);
1030 g_strlcat(dest_thumb_path, PBAP_THMB_PATH,
1031 sizeof(dest_thumb_path));
1032 g_strlcat(dest_thumb_path, ptr, sizeof(dest_thumb_path));
1033 DBG("Thumbnail path is = %s", dest_thumb_path);
1035 DBG_SECURE("filename = %s Thumbnail path is = %s", filename, dest_thumb_path);
1036 img_info.src = filename;
1037 img_info.dest = dest_thumb_path;
1038 img_info.ret = CONTACTS_ERROR_INTERNAL;
1039 img_info.person = person;
1040 img_info.string = string;
1042 res = image_util_foreach_supported_jpeg_colorspace(
1043 __bt_pbap_image_util_supported_jpeg_colorspace_cb, &img_info);
1045 if (res != IMAGE_UTIL_ERROR_NONE)
1046 ERR("Image resizing is failed");
1051 static void __bluetooth_pb_vcard_append_bday_v21(GString *string,
1052 contacts_record_h contact)
1060 status = contacts_record_get_child_record_count(contact,
1061 _contacts_contact.event, &count);
1063 if (status != CONTACTS_ERROR_NONE)
1066 for (i = 0; i < count; i++) {
1067 contacts_record_h event = NULL;
1073 contacts_event_type_e type;
1075 status = contacts_record_get_child_record_at_p(contact,
1076 _contacts_contact.event, i, &event);
1078 if (status != CONTACTS_ERROR_NONE)
1081 status = contacts_record_get_int(event,
1082 _contacts_event.type,
1085 if (status != CONTACTS_ERROR_NONE)
1088 if (type != CONTACTS_EVENT_TYPE_BIRTH)
1091 status = contacts_record_get_int(event,
1092 _contacts_event.date,
1095 if (status != CONTACTS_ERROR_NONE)
1101 bday = g_strdup_printf("%04d-%02d-%02d",
1102 (date/10000), (date/100)%100, date%100);
1103 __bluetooth_pb_vcard_append_qp_encode_v21(string, "BDAY",
1110 static void __bluetooth_pb_vcard_append_adr_v21(GString *string,
1111 contacts_record_h contact)
1119 status = contacts_record_get_child_record_count(contact,
1120 _contacts_contact.address,
1123 if (status != CONTACTS_ERROR_NONE)
1126 for (i = 0; i < count; i++) {
1127 contacts_record_h address = NULL;
1135 status = contacts_record_get_child_record_at_p(contact,
1136 _contacts_contact.address, i, &address);
1138 if (status != CONTACTS_ERROR_NONE)
1141 addrv = __bluetooth_pb_contact_addr(address);
1146 addr = __bluetooth_pb_vcard_strv_concat(addrv, ";");
1149 paramv = __bluetooth_pb_contact_addr_param(address);
1151 param = __bluetooth_pb_vcard_strv_concat(paramv, ";");
1154 __bluetooth_pb_vcard_append_qp_encode_v21(string, "ADR",
1164 static void __bluetooth_pb_vcard_append_email_v21(GString *string,
1165 contacts_record_h contact)
1173 status = contacts_record_get_child_record_count(contact,
1174 _contacts_contact.email,
1177 if (status != CONTACTS_ERROR_NONE)
1180 for (i = 0; i < count; i++) {
1181 contacts_record_h email = NULL;
1186 status = contacts_record_get_child_record_at_p(contact,
1187 _contacts_contact.email, i, &email);
1189 if (status != CONTACTS_ERROR_NONE)
1192 status = contacts_record_get_str_p(email,
1193 _contacts_email.email,
1196 if (status != CONTACTS_ERROR_NONE)
1199 escaped = __bluetooth_pb_vcard_escape(tmp);
1200 __bluetooth_pb_vcard_append_qp_encode_v21(string, "EMAIL", NULL,
1208 static void __bluetooth_pb_vcard_append_title_v21(GString *string,
1209 contacts_record_h contact)
1217 status = contacts_record_get_child_record_count(contact,
1218 _contacts_contact.company,
1221 if (status != CONTACTS_ERROR_NONE)
1224 for (i = 0; i < count; i++) {
1225 contacts_record_h company = NULL;
1230 status = contacts_record_get_child_record_at_p(contact,
1231 _contacts_contact.company, i, &company);
1233 if (status != CONTACTS_ERROR_NONE)
1236 status = contacts_record_get_str_p(company,
1237 _contacts_company.job_title,
1240 if (status != CONTACTS_ERROR_NONE)
1243 escaped = __bluetooth_pb_vcard_escape(title);
1244 __bluetooth_pb_vcard_append_qp_encode_v21(string, "TITLE", NULL,
1252 static void __bluetooth_pb_vcard_append_role_v21(GString *string,
1253 contacts_record_h contact)
1261 status = contacts_record_get_child_record_count(contact,
1262 _contacts_contact.company,
1265 if (status != CONTACTS_ERROR_NONE)
1268 for (i = 0; i < count; i++) {
1269 contacts_record_h company = NULL;
1274 status = contacts_record_get_child_record_at_p(contact,
1275 _contacts_contact.company, i, &company);
1277 if (status != CONTACTS_ERROR_NONE)
1280 status = contacts_record_get_str_p(company,
1281 _contacts_company.role,
1284 if (status != CONTACTS_ERROR_NONE)
1287 escaped = __bluetooth_pb_vcard_escape(role);
1288 __bluetooth_pb_vcard_append_qp_encode_v21(string, "ROLE", NULL,
1296 static void __bluetooth_pb_vcard_append_org_v21(GString *string,
1297 contacts_record_h contact)
1305 status = contacts_record_get_child_record_count(contact,
1306 _contacts_contact.company,
1309 if (status != CONTACTS_ERROR_NONE)
1312 for (i = 0; i < count; i++) {
1313 contacts_record_h company = NULL;
1318 gchar *department = NULL;
1321 gint department_status;
1323 status = contacts_record_get_child_record_at_p(contact,
1324 _contacts_contact.company, i, &company);
1326 if (status != CONTACTS_ERROR_NONE)
1330 name_status = contacts_record_get_str_p(company,
1331 _contacts_company.name,
1334 department_status = contacts_record_get_str_p(company,
1335 _contacts_company.department,
1338 if ((name_status != CONTACTS_ERROR_NONE) &&
1339 (department_status != CONTACTS_ERROR_NONE))
1342 str = g_string_new(NULL);
1347 escaped = __bluetooth_pb_vcard_escape(name);
1348 g_string_append(str, escaped);
1352 g_string_append(str, ";");
1357 escaped = __bluetooth_pb_vcard_escape(department);
1358 g_string_append(str, escaped);
1362 __bluetooth_pb_vcard_append_qp_encode_v21(string, "ORG", NULL,
1365 g_string_free(str, TRUE);
1371 static void __bluetooth_pb_vcard_append_note_v21(GString *string,
1372 contacts_record_h contact)
1380 status = contacts_record_get_child_record_count(contact,
1381 _contacts_contact.note,
1384 if (status != CONTACTS_ERROR_NONE)
1387 for (i = 0; i < count; i++) {
1388 contacts_record_h note = NULL;
1393 status = contacts_record_get_child_record_at_p(contact,
1394 _contacts_contact.note, i, ¬e);
1396 if (status != CONTACTS_ERROR_NONE)
1399 status = contacts_record_get_str_p(note,
1400 _contacts_note.note,
1403 if (status != CONTACTS_ERROR_NONE)
1406 escaped = __bluetooth_pb_vcard_escape(tmp);
1407 __bluetooth_pb_vcard_append_qp_encode_v21(string, "NOTE", NULL,
1415 static void __bluetooth_pb_vcard_append_rev_v21(GString *string,
1416 contacts_record_h contact)
1425 status = contacts_record_get_int(contact,
1426 _contacts_contact.changed_time,
1429 if (status != CONTACTS_ERROR_NONE)
1435 gmtime_r((const time_t *)(&time), &result);
1437 rev = g_strdup_printf("%04d-%02d-%02dT%02d:%02d:%02dZ",
1438 (1900 + result.tm_year), (1 + result.tm_mon),
1439 result.tm_mday, result.tm_hour, result.tm_min,
1442 __bluetooth_pb_vcard_append_qp_encode_v21(string, "REV", NULL, rev);
1448 static void __bluetooth_pb_vcard_append_url_v21(GString *string,
1449 contacts_record_h contact)
1457 status = contacts_record_get_child_record_count(contact,
1458 _contacts_contact.url,
1461 if (status != CONTACTS_ERROR_NONE)
1464 for (i = 0; i < count; i++) {
1465 contacts_record_h url = NULL;
1470 status = contacts_record_get_child_record_at_p(contact,
1471 _contacts_contact.url, i, &url);
1473 if (status != CONTACTS_ERROR_NONE)
1479 status = contacts_record_get_str_p(url,
1483 if (status != CONTACTS_ERROR_NONE)
1486 escaped = __bluetooth_pb_vcard_escape(tmp);
1487 __bluetooth_pb_vcard_append_qp_encode_v21(string, "URL", NULL,
1495 static void __bluetooth_pb_vcard_append_uid_v21(GString *string,
1496 contacts_record_h contact)
1504 status = contacts_record_get_str_p(contact,
1505 _contacts_contact.uid,
1508 if (status != CONTACTS_ERROR_NONE)
1511 escaped = __bluetooth_pb_vcard_escape(uid);
1512 __bluetooth_pb_vcard_append_qp_encode_v21(string, "UID", NULL, escaped);
1518 static void __bluetooth_pb_vcard_append_v30(GString *string,
1529 g_string_append(string, name);
1532 g_string_append_printf(string, ";%s", param);
1534 g_string_append(string, ":");
1537 g_string_append(string, value);
1539 g_string_append(string, "\r\n");
1543 static void __bluetooth_pb_vcard_remove_v30(GString *string,
1544 const gchar *property_name)
1548 gchar *st_pos = NULL;
1550 gboolean matched = FALSE;
1552 if (string == NULL || property_name == NULL)
1557 while (*pos != '\0') {
1558 if (matched == FALSE) {
1559 if (g_ascii_strncasecmp(pos, "\r\n", 2) == 0) {
1565 attrlen = strlen(property_name);
1566 if (g_ascii_strncasecmp(pos, property_name,
1570 if (*pos == ':' || *pos == ';') {
1578 if (g_ascii_strncasecmp(pos, "\r\n", 2) == 0) {
1581 if (*pos != ' ' && *pos != '\t') {
1582 /* +2 means move over \r\n */
1583 g_string_erase(string,
1584 (st_pos+2)-(string->str),
1599 static gchar *__bluetooth_pb_vcard_filter_v30(const gchar *vcard,
1603 GString *string = NULL;
1608 string = g_string_new(vcard);
1610 if ((filter & VCARD_BDAY) == 0)
1611 __bluetooth_pb_vcard_remove_v30(string, "BDAY");
1613 if ((filter & VCARD_ADR) == 0)
1614 __bluetooth_pb_vcard_remove_v30(string, "ADR");
1616 if ((filter & VCARD_EMAIL) == 0)
1617 __bluetooth_pb_vcard_remove_v30(string, "EMAIL");
1619 if ((filter & VCARD_TITLE) == 0)
1620 __bluetooth_pb_vcard_remove_v30(string, "TITLE");
1622 if ((filter & VCARD_ROLE) == 0)
1623 __bluetooth_pb_vcard_remove_v30(string, "ROLE");
1625 if ((filter & VCARD_ORG) == 0)
1626 __bluetooth_pb_vcard_remove_v30(string, "ORG");
1628 if ((filter & VCARD_NOTE) == 0)
1629 __bluetooth_pb_vcard_remove_v30(string, "NOTE");
1631 if ((filter & VCARD_REV) == 0)
1632 __bluetooth_pb_vcard_remove_v30(string, "REV");
1634 if ((filter & VCARD_URL) == 0)
1635 __bluetooth_pb_vcard_remove_v30(string, "URL");
1637 if ((filter & VCARD_UID) == 0)
1638 __bluetooth_pb_vcard_remove_v30(string, "UID");
1640 if ((filter & VCARD_NICKNAME) == 0)
1641 __bluetooth_pb_vcard_remove_v30(string, "NICKNAME");
1644 return g_string_free(string, FALSE);
1647 static gchar *__bluetooth_pb_vcard_real_contact_valist_v21(gint person_id,
1650 const gchar *first_name,
1654 contacts_record_h person = NULL;
1655 contacts_record_h contact = NULL;
1657 GString *str = NULL;
1659 gint contact_id = 0;
1664 const gchar *name = first_name;
1666 status = contacts_db_get_record(_contacts_person._uri,
1670 if (status != CONTACTS_ERROR_NONE)
1673 status = contacts_record_get_int(person,
1674 _contacts_person.display_contact_id,
1677 if (status != CONTACTS_ERROR_NONE)
1680 status = contacts_db_get_record(_contacts_contact._uri,
1684 if (status != CONTACTS_ERROR_NONE) {
1685 contacts_record_destroy(person, TRUE);
1692 DBG("filter[%x]\n", filter);
1694 str = g_string_new("BEGIN:VCARD\r\nVERSION:2.1\r\n");
1696 /* N, TEL is default */
1697 __bluetooth_pb_vcard_append_n_v21(str, contact);
1699 if (phonelog_id > 0) {
1702 number = _bluetooth_pb_number_from_phonelog_id(phonelog_id);
1703 __bluetooth_pb_vcard_append_qp_encode_v21(str, "TEL", "X-0",
1709 __bluetooth_pb_vcard_append_tel_v21(str, contact);
1713 __bluetooth_pb_vcard_append_fn_v21(str, person);
1714 /* Need to check filter
1715 if (f & VCARD_SOUND)*/
1716 __bluetooth_pb_vcard_append_phonetic_first_v21(str, contact);
1717 if (f & VCARD_PHOTO)
1718 __bluetooth_pb_vcard_append_photo_v21(str, person);
1720 __bluetooth_pb_vcard_append_bday_v21(str, contact);
1722 __bluetooth_pb_vcard_append_adr_v21(str, contact);
1723 if (f & VCARD_EMAIL)
1724 __bluetooth_pb_vcard_append_email_v21(str, contact);
1725 if (f & VCARD_TITLE)
1726 __bluetooth_pb_vcard_append_title_v21(str, contact);
1728 __bluetooth_pb_vcard_append_role_v21(str, contact);
1730 __bluetooth_pb_vcard_append_org_v21(str, contact);
1732 __bluetooth_pb_vcard_append_note_v21(str, contact);
1734 __bluetooth_pb_vcard_append_rev_v21(str, contact);
1736 __bluetooth_pb_vcard_append_url_v21(str, contact);
1738 __bluetooth_pb_vcard_append_uid_v21(str, contact);
1741 const gchar *param = va_arg(args, const gchar *);
1742 const gchar *value = va_arg(args, const gchar *);
1745 gchar *escaped = NULL;
1747 escaped = __bluetooth_pb_vcard_escape(value);
1748 __bluetooth_pb_vcard_append_qp_encode_v21(str, name,
1754 name = va_arg(args, const gchar *);
1757 g_string_append(str, "END:VCARD\r\n");
1759 contacts_record_destroy(contact, TRUE);
1760 contacts_record_destroy(person, TRUE);
1763 return g_string_free(str, FALSE);
1767 static gchar *__bluetooth_pb_vcard_real_contact_valist_v30(gint person_id,
1770 const gchar *first_name,
1774 contacts_record_h person = NULL;
1776 GString *str = NULL;
1780 const gchar *name = first_name;
1781 gchar *vcard = NULL;
1783 status = contacts_db_get_record(_contacts_person._uri,
1787 if (status != CONTACTS_ERROR_NONE)
1790 status = contacts_vcard_make_from_person(person, &vcard);
1792 if (status != CONTACTS_ERROR_NONE)
1795 /* removing the END:VCARD\r\n" to append extra data */
1796 str = g_string_new_len(vcard, (strlen(vcard)-11));
1799 /* append contents on vcard */
1801 const gchar *param = va_arg(args, const gchar *);
1802 const gchar *value = va_arg(args, const gchar *);
1804 __bluetooth_pb_vcard_append_v30(str, name, param, value);
1806 name = va_arg(args, const gchar *);
1809 /* if phonelog_id exist, we shall show only the phone number, which was
1810 used for that phone log */
1811 if (phonelog_id > 0) {
1814 __bluetooth_pb_vcard_remove_v30(str, "TEL");
1816 number = _bluetooth_pb_number_from_phonelog_id(phonelog_id);
1817 __bluetooth_pb_vcard_append_v30(str, "TEL", NULL, number);
1821 /* Remove Full contact image and add thumbnail image */
1822 __bluetooth_pb_vcard_remove_v30(str, "PHOTO");
1823 if (filter & VCARD_PHOTO)
1824 __bluetooth_pb_vcard_append_photo_v21(str, person); /* Photo is same as vCard 2.1 */
1826 /* Destroy contact record */
1827 status = contacts_record_destroy(person, TRUE);
1828 if (status != CONTACTS_ERROR_NONE)
1829 ERR("Failed to destroy person");
1831 g_string_append(str, "END:VCARD\r\n");
1833 vcard = g_string_free(str, FALSE);
1835 /* temporary fixed for some application crash */
1837 filter = ~VCARD_NOTE;
1840 gchar *new_vcard = NULL;
1842 new_vcard = __bluetooth_pb_vcard_filter_v30(vcard, filter);
1855 static gchar *__bluetooth_pb_vcard_real_contact_with_properties(gint person_id,
1859 const gchar *first_name,
1863 DBG(" format [%d]\n", format);
1864 gchar *vcard = NULL;
1867 va_start(args, first_name);
1870 case VCARD_FORMAT_3_0:
1871 vcard = __bluetooth_pb_vcard_real_contact_valist_v30(person_id,
1872 phonelog_id, filter,
1875 case VCARD_FORMAT_2_1:
1877 vcard = __bluetooth_pb_vcard_real_contact_valist_v21(person_id,
1878 phonelog_id, filter,
1889 static gchar *__bluetooth_pb_vcard_real_call_v21(gint phonelog_id,
1897 str = g_string_new("BEGIN:VCARD\r\nVERSION:2.1\r\n");
1899 __bluetooth_pb_vcard_append_qp_encode_v21(str, "N", NULL, NULL);
1901 number = _bluetooth_pb_number_from_phonelog_id(phonelog_id);
1902 __bluetooth_pb_vcard_append_qp_encode_v21(str, "TEL", "X-0", number);
1905 if (((filter == 0) || (filter & VCARD_X_IRMC_CALL_DATETIME))
1907 gchar *datetime = NULL;
1909 datetime = __bluetooth_pb_phonelog_datetime(phonelog_id);
1910 __bluetooth_pb_vcard_append_qp_encode_v21(str,
1911 "X-IRMC-CALL-DATETIME",
1916 g_string_append(str, "END:VCARD\r\n");
1919 return g_string_free(str, FALSE);
1922 static gchar *__bluetooth_pb_vcard_real_call_v30(gint phonelog_id,
1930 str = g_string_new("BEGIN:VCARD\r\nVERSION:3.0\r\n");
1932 __bluetooth_pb_vcard_append_v30(str, "N", NULL, NULL);
1933 __bluetooth_pb_vcard_append_v30(str, "FN", NULL, NULL);
1935 number = _bluetooth_pb_number_from_phonelog_id(phonelog_id);
1936 __bluetooth_pb_vcard_append_v30(str, "TEL", NULL, number);
1939 if (((filter == 0) || (filter & VCARD_X_IRMC_CALL_DATETIME))
1941 gchar *datetime = NULL;
1943 datetime = __bluetooth_pb_phonelog_datetime(phonelog_id);
1944 __bluetooth_pb_vcard_append_v30(str,
1945 "X-IRMC-CALL-DATETIME", attr, datetime);
1949 g_string_append(str, "END:VCARD\r\n");
1952 return g_string_free(str, FALSE);
1955 static gchar *__bluetooth_pb_vcard_real_call(gint phonelog_id,
1961 gchar *vcard = NULL;
1964 case VCARD_FORMAT_3_0:
1965 vcard = __bluetooth_pb_vcard_real_call_v30(phonelog_id,
1968 case VCARD_FORMAT_2_1:
1970 vcard = __bluetooth_pb_vcard_real_call_v21(phonelog_id,
1979 static gchar **__bluetooth_pb_contact_add_field_str(contacts_record_h record,
1989 gboolean valid = FALSE;
1991 /* check empty field */
1992 for (i = 0; i < field_size; i++) {
1995 status = contacts_record_get_str_p(record, field[i], &tmp);
1997 if (status != CONTACTS_ERROR_NONE)
2009 strv = g_new0(gchar *, field_size + 1);
2011 for (i = 0; i < field_size; i++) {
2014 status = contacts_record_get_str_p(record, field[i], &tmp);
2016 if (status != CONTACTS_ERROR_NONE)
2020 strv[i] = g_strdup("");
2022 strv[i] = __bluetooth_pb_vcard_escape(tmp);
2029 static gchar **__bluetooth_pb_contact_tel_param(contacts_record_h number)
2032 gchar **strv = NULL;
2034 const gint TEL_PARAM_LEN = 13;
2039 contacts_number_type_e type;
2041 bool is_default = false;
2043 status = contacts_record_get_bool(number, _contacts_number.is_default,
2046 if (status != CONTACTS_ERROR_NONE)
2049 status = contacts_record_get_int(number,
2050 _contacts_number.type,
2053 if (status != CONTACTS_ERROR_NONE)
2056 strv = g_new0(char *, TEL_PARAM_LEN + 1);/* tel param max size is 13 */
2059 strv[i] = g_strdup("PREF");
2063 if (type & CONTACTS_NUMBER_TYPE_HOME) {
2064 strv[i] = g_strdup("HOME");
2068 if (type & CONTACTS_NUMBER_TYPE_WORK) {
2069 strv[i] = g_strdup("WORK");
2073 if (type & CONTACTS_NUMBER_TYPE_VOICE) {
2074 strv[i] = g_strdup("VOICE");
2078 if (type & CONTACTS_NUMBER_TYPE_FAX) {
2079 strv[i] = g_strdup("FAX");
2083 if (type & CONTACTS_NUMBER_TYPE_MSG) {
2084 strv[i] = g_strdup("MSG");
2088 if (type & CONTACTS_NUMBER_TYPE_CELL) {
2089 strv[i] = g_strdup("CELL");
2093 if (type & CONTACTS_NUMBER_TYPE_PAGER) {
2094 strv[i] = g_strdup("PAGER");
2098 if (type & CONTACTS_NUMBER_TYPE_BBS) {
2099 strv[i] = g_strdup("BBS");
2103 if (type & CONTACTS_NUMBER_TYPE_MODEM) {
2104 strv[i] = g_strdup("MODEM");
2108 if (type & CONTACTS_NUMBER_TYPE_CAR) {
2109 strv[i] = g_strdup("CAR");
2113 if (type & CONTACTS_NUMBER_TYPE_ISDN) {
2114 strv[i] = g_strdup("ISDN");
2118 if (type & CONTACTS_NUMBER_TYPE_VIDEO) {
2119 strv[i] = g_strdup("VIDEO");
2123 /* CTS_NUM_TYPE_PCS is not part of vcard2.1 */
2129 static gchar *__bluetooth_pb_contact_photo_type(const gchar *filename)
2132 gchar *filetype = NULL;
2135 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR) == FALSE) {
2136 ERR_SECURE("file does not regular:%s\n", filename);
2140 ext = strrchr(filename, '.');
2142 ERR("file doesn't have extension\n");
2148 if (g_ascii_strcasecmp(ext, "gif") == 0)
2150 else if (g_ascii_strcasecmp(ext, "cgm") == 0)
2152 else if (g_ascii_strcasecmp(ext, "wmf") == 0)
2154 else if (g_ascii_strcasecmp(ext, "bmp") == 0)
2156 else if (g_ascii_strcasecmp(ext, "met") == 0)
2158 else if (g_ascii_strcasecmp(ext, "dib") == 0)
2160 else if (g_ascii_strcasecmp(ext, "pict") == 0 ||
2161 g_ascii_strcasecmp(ext, "pct") == 0 ||
2162 g_ascii_strcasecmp(ext, "pic") == 0)
2164 else if (g_ascii_strcasecmp(ext, "tiff") == 0 ||
2165 g_ascii_strcasecmp(ext, "tif") == 0)
2167 else if (g_ascii_strcasecmp(ext, "ps") == 0)
2169 else if (g_ascii_strcasecmp(ext, "pdf") == 0)
2171 else if (g_ascii_strcasecmp(ext, "jpeg") == 0 ||
2172 g_ascii_strcasecmp(ext, "jpg") == 0 ||
2173 g_ascii_strcasecmp(ext, "jpe") == 0)
2175 else if (g_ascii_strcasecmp(ext, "mpeg") == 0 ||
2176 g_ascii_strcasecmp(ext, "mpg") == 0)
2178 else if (g_ascii_strcasecmp(ext, "m2v") == 0)
2180 else if (g_ascii_strcasecmp(ext, "avi") == 0)
2182 else if (g_ascii_strcasecmp(ext, "mov") == 0)
2184 else if (g_ascii_strcasecmp(ext, "png") == 0)
2188 return g_strdup(filetype);
2191 static gchar **__bluetooth_pb_contact_addr(contacts_record_h address)
2194 const gint ADDR_LEN = 7;
2196 gchar **strv = NULL;
2198 gint addr[] = { _contacts_address.postbox,
2199 _contacts_address.extended,
2200 _contacts_address.street,
2201 _contacts_address.locality,
2202 _contacts_address.region,
2203 _contacts_address.postal_code,
2204 _contacts_address.country };
2206 strv = __bluetooth_pb_contact_add_field_str(address,
2212 static gchar **__bluetooth_pb_contact_addr_param(contacts_record_h address)
2215 contacts_address_type_e type;
2220 gchar **strv = NULL;
2222 status = contacts_record_get_int(address,
2223 _contacts_address.type,
2226 if (status != CONTACTS_ERROR_NONE)
2229 strv = g_new0(gchar *, 7); /* ADDR param max size is 6 */
2231 if (type & CONTACTS_ADDRESS_TYPE_HOME) {
2232 strv[i] = g_strdup("HOME");
2235 if (type & CONTACTS_ADDRESS_TYPE_WORK) {
2236 strv[i] = g_strdup("WORK");
2239 if (type & CONTACTS_ADDRESS_TYPE_DOM) {
2240 strv[i] = g_strdup("DOM");
2243 if (type & CONTACTS_ADDRESS_TYPE_INTL) {
2244 strv[i] = g_strdup("INTL");
2247 if (type & CONTACTS_ADDRESS_TYPE_POSTAL) {
2248 strv[i] = g_strdup("POSTAL");
2251 if (type & CONTACTS_ADDRESS_TYPE_PARCEL) {
2252 strv[i] = g_strdup("PARCEL");
2259 static gchar *__bluetooth_pb_phonelog_datetime(gint phonelog_id)
2262 contacts_record_h phone_log;
2264 char time_str[32] = {0,};
2269 struct tm time_info;
2271 status = contacts_db_get_record(_contacts_phone_log._uri,
2275 if (status != CONTACTS_ERROR_NONE)
2278 status = contacts_record_get_int(phone_log,
2279 _contacts_phone_log.log_time,
2282 if (status != CONTACTS_ERROR_NONE)
2288 localtime_r((time_t *)&time, &time_info);
2290 strftime(time_str, sizeof(time_str),
2291 "%Y%m%dT%H%M%S", &time_info);
2293 contacts_record_destroy(phone_log, TRUE);
2296 return g_strdup(time_str);
2299 static gchar *__bluetooth_pb_name_from_contact(contacts_record_h contact)
2302 contacts_record_h name = NULL;
2309 gint name_val[] = { _contacts_name.last,
2310 _contacts_name.first,
2311 _contacts_name.addition,
2312 _contacts_name.prefix,
2313 _contacts_name.suffix };
2316 status = contacts_record_get_child_record_at_p(contact,
2317 _contacts_contact.name, 0, &name);
2319 if (status != CONTACTS_ERROR_NONE)
2322 str = g_string_new(NULL);
2324 for (i = 0; i < name_size; i++) {
2326 gchar *escape = NULL;
2329 g_string_append_c(str, ';');
2331 status = contacts_record_get_str_p(name, name_val[i], &tmp);
2333 if (status != CONTACTS_ERROR_NONE)
2336 escape = __bluetooth_pb_vcard_escape(tmp);
2337 g_string_append(str, escape);
2343 return g_string_free(str, FALSE);
2346 static gchar *__bluetooth_pb_phonetic_name_from_contact(contacts_record_h contact)
2349 contacts_record_h name = NULL;
2355 gchar *phonetic_first = NULL;
2356 gchar *phonetic_last = NULL;
2357 gchar *escape = NULL;
2359 status = contacts_record_get_child_record_at_p(contact,
2360 _contacts_contact.name, 0, &name);
2362 if (status != CONTACTS_ERROR_NONE)
2365 status = contacts_record_get_str_p(name, _contacts_name.phonetic_first, &phonetic_first);
2367 if (status != CONTACTS_ERROR_NONE)
2370 if (phonetic_first == NULL)
2373 str = g_string_new(NULL);
2375 status = contacts_record_get_str_p(name, _contacts_name.phonetic_last, &phonetic_last);
2377 if ((status == CONTACTS_ERROR_NONE) && (phonetic_last != NULL)) {
2378 escape = __bluetooth_pb_vcard_escape(phonetic_last);
2379 g_string_append(str, escape);
2381 g_string_append_c(str, ' ');
2384 escape = __bluetooth_pb_vcard_escape(phonetic_first);
2385 g_string_append(str, escape);
2388 g_string_append_c(str, ';');
2389 g_string_append_c(str, ';');
2390 g_string_append_c(str, ';');
2391 g_string_append_c(str, ';');
2394 return g_string_free(str, FALSE);
2398 static gchar *__bluetooth_pb_number_from_contact(contacts_record_h contact)
2408 status = contacts_record_get_child_record_count(contact,
2409 _contacts_contact.number,
2412 if (status != CONTACTS_ERROR_NONE)
2415 for (i = 0; i < count; i++) {
2416 contacts_record_h number = NULL;
2420 bool is_default = false;
2422 status = contacts_record_get_child_record_at_p(contact,
2423 _contacts_contact.number, i, &number);
2425 if (status != CONTACTS_ERROR_NONE)
2428 status = contacts_record_get_bool(number,
2429 _contacts_number.is_default,
2432 if (status != CONTACTS_ERROR_NONE)
2435 if (is_default == FALSE)
2438 status = contacts_record_get_str_p(number,
2439 _contacts_number.number,
2442 if (status != CONTACTS_ERROR_NONE)
2446 str = g_strdup(tmp);
2451 /* get first number */
2455 contacts_record_h number = NULL;
2457 status = contacts_record_get_child_record_at_p(contact,
2458 _contacts_contact.number, 0, &number);
2460 if (status != CONTACTS_ERROR_NONE)
2463 status = contacts_record_get_str_p(number,
2464 _contacts_number.number,
2467 if (status != CONTACTS_ERROR_NONE)
2470 str = g_strdup(tmp);
2477 static gint __bluetooth_pb_person_id_from_phonelog_id(gint phonelog_id)
2480 contacts_query_h query = NULL;
2481 contacts_filter_h filter = NULL;
2482 contacts_list_h record_list = NULL;
2484 contacts_record_h phone_log = NULL;
2485 contacts_record_h record = NULL;
2490 status = contacts_db_get_record(_contacts_phone_log._uri,
2494 if (status != CONTACTS_ERROR_NONE)
2497 status = contacts_record_get_int(phone_log,
2498 _contacts_phone_log.person_id,
2501 if (status != CONTACTS_ERROR_NONE) {
2502 contacts_record_destroy(phone_log, TRUE);
2506 contacts_record_destroy(phone_log, TRUE);
2511 status = contacts_filter_create(_contacts_person_phone_log._uri,
2514 if (status != CONTACTS_ERROR_NONE)
2518 status = contacts_filter_add_int(filter,
2519 _contacts_person_phone_log.log_id,
2520 CONTACTS_MATCH_EQUAL,
2523 if (status != CONTACTS_ERROR_NONE)
2526 status = contacts_query_create(_contacts_person_phone_log._uri, &query);
2528 if (status != CONTACTS_ERROR_NONE)
2531 status = contacts_query_set_filter(query, filter);
2533 if (status != CONTACTS_ERROR_NONE)
2536 status = contacts_db_get_records_with_query(query, -1, -1,
2539 if (status != CONTACTS_ERROR_NONE)
2542 status = contacts_list_first(record_list);
2544 if (status != CONTACTS_ERROR_NONE)
2547 status = contacts_list_get_current_record_p(record_list, &record);
2549 if (status != CONTACTS_ERROR_NONE)
2552 status = contacts_record_get_int(record,
2553 _contacts_person_phone_log.person_id,
2556 if (status != CONTACTS_ERROR_NONE)
2560 if (record_list != NULL)
2561 contacts_list_destroy(record_list, TRUE);
2563 contacts_filter_destroy(filter);
2566 contacts_query_destroy(query);
2572 int _bluetooth_get_contact_addressbook(gint person_id)
2574 contacts_record_h person = NULL;
2575 contacts_record_h contact = NULL;
2576 contacts_record_h addressbook = NULL;
2578 char* addressbook_name = NULL;
2579 gint contact_id = 0;
2580 gint address_book_id = 0;
2583 status = contacts_db_get_record(_contacts_person._uri,
2586 if (status != CONTACTS_ERROR_NONE)
2587 return PBAP_ADDRESSBOOK_PHONE; /* Default*/
2589 status = contacts_record_get_int(person,
2590 _contacts_person.display_contact_id,
2592 contacts_record_destroy(person, TRUE);
2594 if (status != CONTACTS_ERROR_NONE)
2595 return PBAP_ADDRESSBOOK_PHONE; /* Default*/
2597 status = contacts_db_get_record(_contacts_contact._uri,
2600 if (status != CONTACTS_ERROR_NONE)
2601 return PBAP_ADDRESSBOOK_PHONE; /* Default*/
2603 status = contacts_record_get_int(contact,
2604 _contacts_contact.address_book_id,
2606 contacts_record_destroy(contact, TRUE);
2608 if (status != CONTACTS_ERROR_NONE)
2609 return PBAP_ADDRESSBOOK_PHONE; /* Default*/
2611 status = contacts_db_get_record(_contacts_address_book._uri,
2614 if (status != CONTACTS_ERROR_NONE)
2615 return PBAP_ADDRESSBOOK_PHONE; /* Default*/
2617 status = contacts_record_get_str_p(addressbook, _contacts_address_book.name,
2619 contacts_record_destroy(addressbook, TRUE);
2621 if (status != CONTACTS_ERROR_NONE)
2622 return PBAP_ADDRESSBOOK_PHONE; /* Default*/
2624 if (address_book_id == 0 || _bt_is_sim_addressbook(addressbook_name) == false)
2625 return PBAP_ADDRESSBOOK_PHONE;
2627 return PBAP_ADDRESSBOOK_SIM;
2630 gchar *_bluetooth_pb_vcard_contact(gint person_id,
2640 str = __bluetooth_pb_vcard_real_contact_with_properties(person_id, 0,
2647 gchar *_bluetooth_pb_vcard_contact_owner(const gchar *number,
2652 GString *str = g_string_new("BEGIN:VCARD\r\n");
2656 fn = _bluetooth_pb_owner_name();
2657 name = g_strdup_printf("%s;;;;", fn);
2660 case VCARD_FORMAT_3_0:
2661 g_string_append(str, "VERSION:3.0\r\n");
2663 __bluetooth_pb_vcard_append_v30(str, "N", NULL, name);
2664 __bluetooth_pb_vcard_append_v30(str, "FN", NULL, fn);
2665 __bluetooth_pb_vcard_append_v30(str, "TEL", "TYPE=CELL",
2668 case VCARD_FORMAT_2_1:
2670 g_string_append(str, "VERSION:2.1\r\n");
2672 __bluetooth_pb_vcard_append_qp_encode_v21(str, "N", NULL, name);
2674 if (filter == 0 || (filter & VCARD_FN))
2675 __bluetooth_pb_vcard_append_qp_encode_v21(str, "FN",
2678 __bluetooth_pb_vcard_append_qp_encode_v21(str, "TEL", "CELL",
2684 g_string_append(str, "END:VCARD\r\n");
2690 return g_string_free(str, FALSE);
2693 gchar *_bluetooth_pb_vcard_call(gint phonelog_id,
2704 ERR("Unknown attribute type ignored\n");
2708 person_id = __bluetooth_pb_person_id_from_phonelog_id(phonelog_id);
2710 DBG("person_id %d\n", person_id);
2713 if (filter == 0 || (filter & VCARD_X_IRMC_CALL_DATETIME)) {
2714 gchar *datetime = NULL;
2716 datetime = __bluetooth_pb_phonelog_datetime(
2719 str = __bluetooth_pb_vcard_real_contact_with_properties(
2723 "X-IRMC-CALL-DATETIME", attr, datetime,
2729 str = __bluetooth_pb_vcard_real_contact_with_properties(
2736 str = __bluetooth_pb_vcard_real_call(phonelog_id, filter,
2743 gchar *_bluetooth_pb_fn_from_person_id(gint person_id)
2746 contacts_record_h person = NULL;
2752 status = contacts_db_get_record(_contacts_person._uri,
2756 if (status != CONTACTS_ERROR_NONE)
2759 status = contacts_record_get_str(person,
2760 _contacts_person.display_name,
2763 if (status != CONTACTS_ERROR_NONE)
2766 contacts_record_destroy(person, TRUE);
2772 gchar *_bluetooth_pb_name_from_person_id(gint person_id)
2775 contacts_record_h person = NULL;
2776 contacts_record_h contact = NULL;
2779 gint contact_id = 0;
2783 status = contacts_db_get_record(_contacts_person._uri,
2787 if (status != CONTACTS_ERROR_NONE)
2790 status = contacts_record_get_int(person,
2791 _contacts_person.display_contact_id,
2794 if (status != CONTACTS_ERROR_NONE) {
2795 contacts_record_destroy(person, TRUE);
2799 status = contacts_db_get_record(_contacts_contact._uri,
2803 if (status != CONTACTS_ERROR_NONE) {
2804 contacts_record_destroy(person, TRUE);
2808 str = __bluetooth_pb_name_from_contact(contact);
2810 contacts_record_destroy(contact, TRUE);
2811 contacts_record_destroy(person, TRUE);
2817 gchar *_bluetooth_pb_number_from_person_id(gint person_id)
2820 contacts_record_h person = NULL;
2821 contacts_record_h contact = NULL;
2824 gint contact_id = 0;
2828 status = contacts_db_get_record(_contacts_person._uri,
2832 if (status != CONTACTS_ERROR_NONE)
2836 status = contacts_record_get_int(person,
2837 _contacts_person.display_contact_id,
2840 if (status != CONTACTS_ERROR_NONE) {
2841 contacts_record_destroy(person, TRUE);
2845 status = contacts_db_get_record(_contacts_contact._uri,
2849 if (status != CONTACTS_ERROR_NONE) {
2850 contacts_record_destroy(person, TRUE);
2854 str = __bluetooth_pb_number_from_contact(contact);
2856 contacts_record_destroy(contact, TRUE);
2857 contacts_record_destroy(person, TRUE);
2863 gchar *_bluetooth_pb_fn_from_phonelog_id(gint phonelog_id)
2869 person_id = __bluetooth_pb_person_id_from_phonelog_id(phonelog_id);
2872 str = _bluetooth_pb_fn_from_person_id(person_id);
2880 gchar *_bluetooth_pb_name_from_phonelog_id(gint phonelog_id)
2886 person_id = __bluetooth_pb_person_id_from_phonelog_id(phonelog_id);
2889 str = _bluetooth_pb_name_from_person_id(person_id);
2893 tmp = _bluetooth_pb_number_from_phonelog_id(phonelog_id);
2894 str = g_strdup_printf("%s;;;;", tmp);
2903 gchar *_bluetooth_pb_number_from_phonelog_id(gint phonelog_id)
2906 contacts_record_h phone_log;
2913 status = contacts_db_get_record(_contacts_phone_log._uri,
2914 phonelog_id, &phone_log);
2916 if (status != CONTACTS_ERROR_NONE)
2919 status = contacts_record_get_str_p(phone_log,
2920 _contacts_phone_log.address,
2923 if (status != CONTACTS_ERROR_NONE) {
2924 contacts_record_destroy(phone_log, TRUE);
2928 str = g_strdup(tmp);
2930 contacts_record_destroy(phone_log, TRUE);
2936 gchar *_bluetooth_pb_owner_name(void)
2941 name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
2944 name = g_strdup("My Name");
2950 bool _bt_is_sim_addressbook(const char *addressbook)
2952 return g_str_has_prefix(addressbook, SIM_ADDRESSBOOK_PREFIX);