4 * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include <sys/types.h>
24 #include "ctsvc_internal.h"
25 #include "ctsvc_db_sqlite.h"
26 #include "ctsvc_db_schema.h"
27 #include "ctsvc_db_init.h"
28 #include "ctsvc_db_query.h"
29 #include "ctsvc_db_utils.h"
30 #include "ctsvc_record.h"
31 #include "ctsvc_normalize.h"
32 #include "ctsvc_list.h"
33 #include "ctsvc_server_setting.h"
34 #include "ctsvc_localize_ch.h"
35 #include "ctsvc_server_group.h"
36 #include "ctsvc_notification.h"
37 #include "ctsvc_localize_utils.h"
38 #include "ctsvc_db_access_control.h"
39 #include "ctsvc_notify.h"
41 #include "ctsvc_db_plugin_contact_helper.h"
43 #define CTSVC_MY_PROFILE_DISPLAY_NAME_MAX_LEN 1024
45 static int __ctsvc_db_my_profile_insert_record(contacts_record_h record, int *id);
46 static int __ctsvc_db_my_profile_get_record(int id, contacts_record_h* out_record);
47 static int __ctsvc_db_my_profile_update_record(contacts_record_h record);
48 static int __ctsvc_db_my_profile_delete_record(int id);
50 static int __ctsvc_db_my_profile_get_all_records(int offset, int limit, contacts_list_h* out_list);
51 static int __ctsvc_db_my_profile_get_records_with_query(contacts_query_h query, int offset, int limit, contacts_list_h* out_list);
53 ctsvc_db_plugin_info_s ctsvc_db_plugin_my_profile = {
54 .is_query_only = false,
55 .insert_record = __ctsvc_db_my_profile_insert_record,
56 .get_record = __ctsvc_db_my_profile_get_record,
57 .update_record = __ctsvc_db_my_profile_update_record,
58 .delete_record = __ctsvc_db_my_profile_delete_record,
59 .get_all_records = __ctsvc_db_my_profile_get_all_records,
60 .get_records_with_query = __ctsvc_db_my_profile_get_records_with_query,
61 .insert_records = NULL,
62 .update_records = NULL,
63 .delete_records = NULL,
65 .get_count_with_query = NULL,
66 .replace_record = NULL,
67 .replace_records = NULL,
70 static int __ctsvc_db_get_my_profile_base_info(int id, ctsvc_my_profile_s *my_profile)
75 char query[CTS_SQL_MAX_LEN] = {0};
77 char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
79 snprintf(query, sizeof(query),
80 "SELECT my_profile_id, addressbook_id, changed_time, %s, image_thumbnail_path, uid "
81 "FROM "CTS_TABLE_MY_PROFILES" WHERE my_profile_id = %d AND deleted = 0",
82 ctsvc_get_display_column(), id);
84 ret = ctsvc_query_prepare(query, &stmt);
85 RETVM_IF(NULL == stmt, ret, "DB error : ctsvc_query_prepare() Fail(%d)", ret);
87 ret = ctsvc_stmt_step(stmt);
88 if (1 /*CTS_TRUE*/ != ret) {
89 CTS_ERR("ctsvc_stmt_step() Fail(%d)", ret);
90 ctsvc_stmt_finalize(stmt);
91 if (CONTACTS_ERROR_NONE == ret)
92 return CONTACTS_ERROR_NO_DATA;
98 my_profile->id = ctsvc_stmt_get_int(stmt, i++);
99 my_profile->addressbook_id = ctsvc_stmt_get_int(stmt, i++);
100 my_profile->changed_time = ctsvc_stmt_get_int(stmt, i++);
101 temp = ctsvc_stmt_get_text(stmt, i++);
102 my_profile->display_name = SAFE_STRDUP(temp);
103 temp = ctsvc_stmt_get_text(stmt, i++);
105 snprintf(full_path, sizeof(full_path), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, temp);
106 my_profile->image_thumbnail_path = strdup(full_path);
108 temp = ctsvc_stmt_get_text(stmt, i++);
109 my_profile->uid = SAFE_STRDUP(temp);
110 ctsvc_stmt_finalize(stmt);
112 return CONTACTS_ERROR_NONE;
115 static int __ctsvc_db_my_profile_get_data(int id, ctsvc_my_profile_s *my_profile)
119 cts_stmt stmt = NULL;
120 char query[CTS_SQL_MAX_LEN] = {0};
122 snprintf(query, sizeof(query),
123 "SELECT datatype, id, contact_id, is_default, data1, data2, "
124 "data3, data4, data5, data6, data7, data8, data9, data10, data11, data12 "
125 "FROM "CTS_TABLE_DATA" WHERE contact_id = %d AND is_my_profile = 1", id);
127 ret = ctsvc_query_prepare(query, &stmt);
128 RETVM_IF(NULL == stmt, ret, "DB error : ctsvc_query_prepare() Fail(%d)", ret);
130 ret = ctsvc_stmt_step(stmt);
131 if (1 /*CTS_TRUE */!= ret) {
132 CTS_ERR("ctsvc_stmt_step() Fail(%d)", ret);
133 ctsvc_stmt_finalize(stmt);
138 datatype = ctsvc_stmt_get_int(stmt, 0);
140 case CTSVC_DATA_NAME:
141 ctsvc_get_data_info_name(stmt, (contacts_list_h)my_profile->name);
143 case CTSVC_DATA_EVENT:
144 ctsvc_get_data_info_event(stmt, (contacts_list_h)my_profile->events);
146 case CTSVC_DATA_MESSENGER:
147 ctsvc_get_data_info_messenger(stmt, (contacts_list_h)my_profile->messengers);
149 case CTSVC_DATA_POSTAL:
150 ctsvc_get_data_info_address(stmt, (contacts_list_h)my_profile->postal_addrs);
153 ctsvc_get_data_info_url(stmt, (contacts_list_h)my_profile->urls);
155 case CTSVC_DATA_NICKNAME:
156 ctsvc_get_data_info_nickname(stmt, (contacts_list_h)my_profile->nicknames);
158 case CTSVC_DATA_NUMBER:
159 ctsvc_get_data_info_number(stmt, (contacts_list_h)my_profile->numbers);
161 case CTSVC_DATA_EMAIL:
162 ctsvc_get_data_info_email(stmt, (contacts_list_h)my_profile->emails);
164 case CTSVC_DATA_PROFILE:
165 ctsvc_get_data_info_profile(stmt, (contacts_list_h)my_profile->profiles);
167 case CTSVC_DATA_RELATIONSHIP:
168 ctsvc_get_data_info_relationship(stmt, (contacts_list_h)my_profile->relationships);
170 case CTSVC_DATA_IMAGE:
171 ctsvc_get_data_info_image(stmt, (contacts_list_h)my_profile->images);
173 case CTSVC_DATA_COMPANY:
174 ctsvc_get_data_info_company(stmt, (contacts_list_h)my_profile->company);
176 case CTSVC_DATA_NOTE:
177 ctsvc_get_data_info_note(stmt, (contacts_list_h)my_profile->note);
179 case CTSVC_DATA_EXTENSION:
180 ctsvc_get_data_info_extension(stmt, (contacts_list_h)my_profile->extensions);
183 CTS_ERR("Intenal : Not supported data type (%d)", datatype);
187 } while (1 /*CTS_TRUE*/ == ctsvc_stmt_step(stmt));
189 ctsvc_stmt_finalize(stmt);
191 return CONTACTS_ERROR_NONE;
195 static int __ctsvc_db_my_profile_get_record(int id, contacts_record_h* out_record)
198 contacts_record_h record;
199 ctsvc_my_profile_s *my_profile;
201 RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
204 contacts_record_create(_contacts_my_profile._uri, &record);
205 my_profile = (ctsvc_my_profile_s *)record;
206 ret = __ctsvc_db_get_my_profile_base_info(id, my_profile);
207 if (CONTACTS_ERROR_NONE != ret) {
208 CTS_ERR("cts_get_main_contacts_info(ALL) Fail(%d)", ret);
209 contacts_record_destroy(record, true);
213 ret = __ctsvc_db_my_profile_get_data(id, my_profile);
214 if (CONTACTS_ERROR_NONE != ret) {
215 CTS_ERR("__ctsvc_db_my_profile_get_data Fail(%d)", ret);
216 contacts_record_destroy(record, true);
220 *out_record = record;
222 return CONTACTS_ERROR_NONE;
225 static int __ctsvc_db_my_profile_delete_record(int id)
230 char query[CTS_SQL_MAX_LEN] = {0};
232 ret = ctsvc_begin_trans();
233 RETVM_IF(ret, ret, "DB error : ctsvc_begin_trans() Fail(%d)", ret);
235 snprintf(query, sizeof(query),
236 "SELECT addressbook_id FROM "CTSVC_DB_VIEW_MY_PROFILE" WHERE my_profile_id = %d", id);
237 ret = ctsvc_query_get_first_int_result(query, &addressbook_id);
238 if (CONTACTS_ERROR_NONE != ret) {
239 CTS_ERR("ctsvc_query_get_first_int_result Fail(%d)", ret);
240 ctsvc_end_trans(false);
244 if (false == ctsvc_have_ab_write_permission(addressbook_id)) {
245 CTS_ERR("Does not have permission to delete this contact");
246 ctsvc_end_trans(false);
247 return CONTACTS_ERROR_PERMISSION_DENIED;
250 snprintf(query, sizeof(query), "UPDATE "CTS_TABLE_MY_PROFILES" "
251 "SET deleted = 1, changed_ver = %d WHERE my_profile_id = %d", ctsvc_get_next_ver(), id);
252 ret = ctsvc_query_exec(query);
253 if (CONTACTS_ERROR_NONE != ret) {
254 CTS_ERR("ctsvc_query_exec() Fail(%d)", ret);
255 ctsvc_end_trans(false);
259 ctsvc_set_my_profile_noti();
261 ret = ctsvc_end_trans(true);
262 if (ret < CONTACTS_ERROR_NONE) {
263 CTS_ERR("DB error : ctsvc_end_trans() Fail(%d)", ret);
267 return CONTACTS_ERROR_NONE;
270 static inline int __ctsvc_my_profile_update_data(ctsvc_my_profile_s *my_profile)
274 if (my_profile->name) {
275 ret = ctsvc_contact_update_data_name((contacts_list_h)my_profile->name, my_profile->id, true);
276 if (CONTACTS_ERROR_NONE != ret) {
277 CTS_ERR("ctsvc_contact_update_data_name() Fail(%d)", ret);
282 if (my_profile->company) {
283 ret = ctsvc_contact_update_data_company((contacts_list_h)my_profile->company, my_profile->id, true);
284 if (CONTACTS_ERROR_NONE != ret) {
285 CTS_ERR("ctsvc_contact_update_data_company() Fail(%d)", ret);
290 if (my_profile->note) {
291 ret = ctsvc_contact_update_data_note((contacts_list_h)my_profile->note, my_profile->id, true);
292 if (CONTACTS_ERROR_NONE != ret) {
293 CTS_ERR("ctsvc_contact_update_data_note() Fail(%d)", ret);
298 if (my_profile->events) {
299 ret = ctsvc_contact_update_data_event((contacts_list_h)my_profile->events, my_profile->id, true);
300 if (CONTACTS_ERROR_NONE != ret) {
301 CTS_ERR("ctsvc_contact_update_data_events() Fail(%d)", ret);
306 if (my_profile->messengers) {
307 ret = ctsvc_contact_update_data_messenger((contacts_list_h)my_profile->messengers, my_profile->id, true);
308 if (CONTACTS_ERROR_NONE != ret) {
309 CTS_ERR("ctsvc_contact_update_data_messengers() Fail(%d)", ret);
314 if (my_profile->postal_addrs) {
315 ret = ctsvc_contact_update_data_address((contacts_list_h)my_profile->postal_addrs, my_profile->id, true);
316 if (CONTACTS_ERROR_NONE != ret) {
317 CTS_ERR("ctsvc_contact_update_data_address() Fail(%d)", ret);
322 if (my_profile->urls) {
323 ret = ctsvc_contact_update_data_url((contacts_list_h)my_profile->urls, my_profile->id, true);
324 if (CONTACTS_ERROR_NONE != ret) {
325 CTS_ERR("ctsvc_contact_update_data_url() Fail(%d)", ret);
330 if (my_profile->nicknames) {
331 ret = ctsvc_contact_update_data_nickname((contacts_list_h)my_profile->nicknames, my_profile->id, true);
332 if (CONTACTS_ERROR_NONE != ret) {
333 CTS_ERR("ctsvc_contact_update_data_nickname() Fail(%d)", ret);
338 if (my_profile->numbers) {
339 bool had_phonenumber;
340 ret = ctsvc_contact_update_data_number((contacts_list_h)my_profile->numbers, my_profile->id, true, &had_phonenumber);
341 if (CONTACTS_ERROR_NONE != ret) {
342 CTS_ERR("ctsvc_contact_update_data_number() Fail(%d)", ret);
347 if (my_profile->emails) {
349 ret = ctsvc_contact_update_data_email((contacts_list_h)my_profile->emails, my_profile->id, true, &had_email);
350 if (CONTACTS_ERROR_NONE != ret) {
351 CTS_ERR("ctsvc_contact_update_data_email() Fail(%d)", ret);
356 if (my_profile->profiles) {
357 ret = ctsvc_contact_update_data_profile((contacts_list_h)my_profile->profiles, my_profile->id, true);
358 if (CONTACTS_ERROR_NONE != ret) {
359 CTS_ERR("ctsvc_contact_update_data_profile() Fail(%d)", ret);
364 if (my_profile->relationships) {
365 ret = ctsvc_contact_update_data_relationship((contacts_list_h)my_profile->relationships, my_profile->id, true);
366 if (CONTACTS_ERROR_NONE != ret) {
367 CTS_ERR("ctsvc_contact_update_data_relationship() Fail(%d)", ret);
372 if (my_profile->images) {
373 ret = ctsvc_contact_update_data_image((contacts_list_h)my_profile->images, my_profile->id, true);
374 if (CONTACTS_ERROR_NONE != ret) {
375 CTS_ERR("ctsvc_contact_update_data_image() Fail(%d)", ret);
380 if (my_profile->extensions) {
381 ret = ctsvc_contact_update_data_extension((contacts_list_h)my_profile->extensions, my_profile->id, true);
382 if (CONTACTS_ERROR_NONE != ret) {
383 CTS_ERR("ctsvc_contact_update_data_extension() Fail(%d)", ret);
388 return CONTACTS_ERROR_NONE;
391 static void __ctsvc_my_profile_check_default_data(ctsvc_my_profile_s *my_profile)
393 ctsvc_contact_check_default_number((contacts_list_h)my_profile->numbers);
394 ctsvc_contact_check_default_email((contacts_list_h)my_profile->emails);
395 ctsvc_contact_check_default_image((contacts_list_h)my_profile->images);
396 ctsvc_contact_check_default_address((contacts_list_h)my_profile->postal_addrs);
399 static void __ctsvc_make_my_profile_display_name(ctsvc_my_profile_s *my_profile)
401 ctsvc_name_s *name = NULL;
403 free(my_profile->display_name);
404 my_profile->display_name = NULL;
406 free(my_profile->reverse_display_name);
407 my_profile->reverse_display_name = NULL;
409 if (0 < my_profile->name->count && my_profile->name->records && my_profile->name->records->data) {
410 name = (ctsvc_name_s *)my_profile->name->records->data;
413 if (name && (name->first || name->last || name->prefix || name->addition || name->suffix)) {
414 char *display = NULL;
415 int len, display_len;
416 int reverse_lang_type = -1;
417 int temp_display_len;
418 char *temp_display = NULL;
421 * Make reverse display name (Last name first)
422 * Default : Prefix Last, First Middle(addition), Suffix
423 * Korean, Chinese : Prefix LastFirstMiddleSuffix
424 * Japanese : Prefix Last Middle First Suffix
425 * reverse sort name does not include prefix
426 * But, if there is only prefix, reverse sort_name is prefix
428 temp_display_len = SAFE_STRLEN(name->first)
429 + SAFE_STRLEN(name->addition)
430 + SAFE_STRLEN(name->last)
431 + SAFE_STRLEN(name->suffix);
432 if (0 < temp_display_len) {
433 temp_display_len += 7;
434 temp_display = calloc(1, temp_display_len);
435 if (NULL == temp_display) {
436 CTS_ERR("calloc() Fail");
442 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->last);
444 if (reverse_lang_type < 0) {
445 reverse_lang_type = ctsvc_check_language_type(temp_display);
448 if (reverse_lang_type != CTSVC_LANG_KOREAN &&
449 reverse_lang_type != CTSVC_LANG_CHINESE &&
450 reverse_lang_type != CTSVC_LANG_JAPANESE) {
451 if (name->first || name->addition)
452 len += snprintf(temp_display + len, temp_display_len - len, ",");
456 if (reverse_lang_type < 0) {
458 reverse_lang_type = ctsvc_check_language_type(temp_display);
460 else if (name->first) {
461 reverse_lang_type = ctsvc_check_language_type(name->first);
463 else if (name->addition) {
464 reverse_lang_type = ctsvc_check_language_type(name->addition);
468 if (reverse_lang_type == CTSVC_LANG_JAPANESE) {
469 /* make temp_display name Prefix - Last - Middle - First - Suffix */
470 if (name->addition) {
472 len += snprintf(temp_display + len, temp_display_len - len, " ");
473 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
478 len += snprintf(temp_display + len, temp_display_len - len, " ");
479 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
485 if (reverse_lang_type < 0) {
486 reverse_lang_type = ctsvc_check_language_type(temp_display);
489 if (reverse_lang_type != CTSVC_LANG_KOREAN &&
490 reverse_lang_type != CTSVC_LANG_CHINESE)
491 len += snprintf(temp_display + len, temp_display_len - len, " ");
493 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
496 if (name->addition) {
498 if (reverse_lang_type < 0) {
499 reverse_lang_type = ctsvc_check_language_type(temp_display);
502 if (reverse_lang_type != CTSVC_LANG_KOREAN &&
503 reverse_lang_type != CTSVC_LANG_CHINESE)
504 len += snprintf(temp_display + len, temp_display_len - len, " ");
506 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
512 if (reverse_lang_type < 0) {
513 reverse_lang_type = ctsvc_check_language_type(temp_display);
516 if (reverse_lang_type == CTSVC_LANG_JAPANESE)
517 len += snprintf(temp_display + len, temp_display_len - len, " ");
518 else if (reverse_lang_type != CTSVC_LANG_KOREAN &&
519 reverse_lang_type != CTSVC_LANG_CHINESE)
520 len += snprintf(temp_display + len, temp_display_len - len, ", ");
522 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->suffix);
526 if (name->prefix && temp_display) {
527 display_len = SAFE_STRLEN(name->prefix) + temp_display_len + 2;
528 display = calloc(1, display_len);
529 if (NULL == display) {
530 CTS_ERR("calloc() Fail");
534 snprintf(display, display_len, "%s %s", name->prefix, temp_display);
535 my_profile->reverse_display_name = display;
538 else if (temp_display) {
539 my_profile->reverse_display_name = temp_display;
541 else if (name->prefix) {
542 my_profile->reverse_display_name = strdup(name->prefix);
546 * Make display name (First name first)
547 * Default : Prefix First Middle Last, Suffix
548 * Korean, Chinese : Prefix LastFirstMiddleSuffix (Same as reverse display name)
549 * Japanese : Prefix First Middle Last Suffix
550 * sort name does not include prefix
551 * But, if there is only prefix, sort_name is prefix
553 if (reverse_lang_type == CTSVC_LANG_KOREAN ||
554 reverse_lang_type == CTSVC_LANG_CHINESE)
555 my_profile->display_name = SAFE_STRDUP(my_profile->reverse_display_name);
559 temp_display_len = SAFE_STRLEN(name->first)
560 + SAFE_STRLEN(name->addition)
561 + SAFE_STRLEN(name->last)
562 + SAFE_STRLEN(name->suffix);
563 if (0 < temp_display_len) {
564 temp_display_len += 6;
565 /* make reverse_temp_display_name */
566 temp_display = calloc(1, temp_display_len);
567 if (NULL == temp_display) {
568 CTS_ERR("calloc() Fail");
575 len += snprintf(temp_display + len, temp_display_len - len, " ");
576 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
579 if (name->addition) {
581 len += snprintf(temp_display + len, temp_display_len - len, " ");
582 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
587 len += snprintf(temp_display + len, temp_display_len - len, " ");
588 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->last);
593 lang_type = ctsvc_check_language_type(temp_display);
594 if (lang_type == CTSVC_LANG_JAPANESE)
595 len += snprintf(temp_display + len, temp_display_len - len, " ");
597 len += snprintf(temp_display + len, temp_display_len - len, ", ");
599 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->suffix);
603 if (name->prefix && temp_display) {
604 display_len = SAFE_STRLEN(name->prefix) + temp_display_len + 2;
605 display = calloc(1, display_len);
606 if (NULL == display) {
607 CTS_ERR("calloc() Fail");
611 snprintf(display, display_len, "%s %s", name->prefix, temp_display);
612 my_profile->display_name = display;
615 else if (temp_display) {
616 my_profile->display_name = temp_display;
618 else if (name->prefix) {
619 my_profile->display_name = strdup(name->prefix);
623 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
627 if (my_profile->company && my_profile->company->records) {
628 for (cur=my_profile->company->records;cur;cur=cur->next) {
629 ctsvc_company_s *company = (ctsvc_company_s *)cur->data;
630 if (company && company->name) {
631 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
632 my_profile->display_name = SAFE_STRDUP(company->name);
638 if (false == ctsvc_record_check_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY) &&
639 my_profile->nicknames && my_profile->nicknames->records) {
640 for (cur=my_profile->nicknames->records;cur;cur=cur->next) {
641 ctsvc_nickname_s *nickname = (ctsvc_nickname_s *)cur->data;
642 if (nickname && nickname->nickname) {
643 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
644 free(my_profile->display_name);
645 my_profile->display_name = SAFE_STRDUP(nickname->nickname);
651 if (false == ctsvc_record_check_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY) &&
652 my_profile->numbers && my_profile->numbers->records) {
653 for (cur=my_profile->numbers->records;cur;cur=cur->next) {
654 ctsvc_number_s *number = (ctsvc_number_s *)cur->data;
655 if (number && number->number) {
656 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
657 free(my_profile->display_name);
658 my_profile->display_name = SAFE_STRDUP(number->number);
664 if (false == ctsvc_record_check_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY) &&
665 my_profile->emails && my_profile->emails->records) {
666 for (cur=my_profile->emails->records;cur;cur=cur->next) {
667 ctsvc_email_s *email = (ctsvc_email_s *)cur->data;
668 if (email && email->email_addr) {
669 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
670 free(my_profile->display_name);
671 my_profile->display_name = SAFE_STRDUP(email->email_addr);
677 if (ctsvc_record_check_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY)) {
678 my_profile->reverse_display_name = SAFE_STRDUP(my_profile->display_name);
682 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
689 static int __ctsvc_db_my_profile_update_record(contacts_record_h record)
694 char query[CTS_SQL_MAX_LEN] = {0};
695 ctsvc_my_profile_s *my_profile = (ctsvc_my_profile_s*)record;
696 GSList *bind_text = NULL;
697 GSList *cursor = NULL;
699 ret = ctsvc_begin_trans();
700 RETVM_IF(ret, ret, "ctsvc_begin_trans() Fail(%d)", ret);
702 snprintf(query, sizeof(query),
703 "SELECT my_profile_id FROM "CTSVC_DB_VIEW_MY_PROFILE" WHERE my_profile_id = %d", my_profile->id);
704 ret = ctsvc_query_get_first_int_result(query, &id);
705 if (CONTACTS_ERROR_NONE != ret) {
706 CTS_ERR("The index(%d) is Invalid. %d Record(s) is(are) found", my_profile->id, ret);
707 ctsvc_end_trans(false);
711 if (false == ctsvc_have_ab_write_permission(my_profile->addressbook_id)) {
712 CTS_ERR("ctsvc_have_ab_write_permission fail : does not have permission(addressbook_id : %d)",
713 my_profile->addressbook_id);
714 ctsvc_end_trans(false);
715 return CONTACTS_ERROR_PERMISSION_DENIED;
718 __ctsvc_make_my_profile_display_name(my_profile);
719 __ctsvc_my_profile_check_default_data(my_profile);
722 ret = __ctsvc_my_profile_update_data(my_profile);
723 if (CONTACTS_ERROR_NONE != ret) {
724 CTS_ERR("__ctsvc_my_profile_update_data() Fail(%d)", ret);
725 ctsvc_end_trans(false);
729 /******************************/
730 /* this code will be removed. */
731 if (my_profile->images) {
732 int ret = CONTACTS_ERROR_NONE;
733 contacts_record_h record_image = NULL;
735 ctsvc_image_s *image;
737 contacts_list_get_count((contacts_list_h)my_profile->images, &count);
739 contacts_list_first((contacts_list_h)my_profile->images);
740 ret = contacts_list_get_current_record_p((contacts_list_h)my_profile->images, &record_image);
742 if (CONTACTS_ERROR_NONE != ret) {
743 CTS_ERR("contacts_list_get_current_record_p() Fail(%d)", ret);
744 ctsvc_end_trans(false);
745 return CONTACTS_ERROR_DB;
748 image = (ctsvc_image_s*)record_image;
749 if ((NULL == my_profile->image_thumbnail_path && image->path) ||
750 (my_profile->image_thumbnail_path && NULL == image->path) ||
751 (my_profile->image_thumbnail_path && image->path && STRING_EQUAL != strcmp(my_profile->image_thumbnail_path, image->path))) {
752 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.image_thumbnail_path, CTSVC_PROPERTY_FLAG_DIRTY);
754 if (ctsvc_contact_check_image_location(image->path))
755 my_profile->image_thumbnail_path = SAFE_STRDUP(image->path + strlen(CTSVC_CONTACT_IMG_FULL_LOCATION) + 1);
757 my_profile->image_thumbnail_path = SAFE_STRDUP(image->path);
760 else if (my_profile->image_thumbnail_path) {
761 free(my_profile->image_thumbnail_path);
762 my_profile->image_thumbnail_path = NULL;
763 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.image_thumbnail_path, CTSVC_PROPERTY_FLAG_DIRTY);
766 /* this code will be removed. */
767 /******************************/
772 char query[CTS_SQL_MAX_LEN] = {0};
773 char query_set[CTS_SQL_MIN_LEN] = {0, };
774 cts_stmt stmt = NULL;
776 version = ctsvc_get_next_ver();
778 ret = ctsvc_db_create_set_query(record, &set, &bind_text);
779 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_db_create_set_query() Fail(%d)", ret);
782 len = snprintf(query_set, sizeof(query_set), "%s, ", set);
783 len += snprintf(query_set+len, sizeof(query_set)-len, " changed_ver=%d, changed_time=%d", version, (int)time(NULL));
785 if (ctsvc_record_check_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY)) {
786 len += snprintf(query_set+len, sizeof(query_set)-len,
787 ", display_name=?, reverse_display_name=?");
788 bind_text = g_slist_append(bind_text, strdup(SAFE_STR(my_profile->display_name)));
789 bind_text = g_slist_append(bind_text, strdup(SAFE_STR(my_profile->reverse_display_name)));
792 snprintf(query, sizeof(query), "UPDATE %s SET %s WHERE my_profile_id = %d", CTS_TABLE_MY_PROFILES, query_set, my_profile->id);
794 ret = ctsvc_query_prepare(query, &stmt);
796 CTS_ERR("DB error : ctsvc_query_prepare() Fail(%d)", ret);
802 for (cursor=bind_text,i=1;cursor;cursor=cursor->next,i++) {
803 const char *text = cursor->data;
805 ctsvc_stmt_bind_text(stmt, i, text);
809 ret = ctsvc_stmt_step(stmt);
810 if (CONTACTS_ERROR_NONE != ret) {
811 CTS_ERR("ctsvc_stmt_step() Fail(%d)", ret);
812 ctsvc_stmt_finalize(stmt);
815 ctsvc_stmt_finalize(stmt);
818 CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s *)record);
821 for (cursor=bind_text;cursor;cursor=cursor->next)
822 CONTACTS_FREE(cursor->data);
823 g_slist_free(bind_text);
826 ctsvc_set_my_profile_noti();
828 ret = ctsvc_end_trans(true);
829 RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "DB error : ctsvc_end_trans() Fail(%d)", ret);
831 return CONTACTS_ERROR_NONE;
834 static int __ctsvc_db_my_profile_get_all_records(int offset, int limit, contacts_list_h* out_list)
840 char query[CTS_SQL_MAX_LEN] = {0};
841 contacts_list_h list;
843 len = snprintf(query, sizeof(query),
844 "SELECT my_profile_id FROM "CTSVC_DB_VIEW_MY_PROFILE);
847 len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
849 len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
852 ret = ctsvc_query_prepare(query, &stmt);
853 RETVM_IF(NULL == stmt, ret, "DB error : ctsvc_query_prepare() Fail(%d)", ret);
855 contacts_list_create(&list);
856 while ((ret = ctsvc_stmt_step(stmt))) {
857 contacts_record_h record;
859 CTS_ERR("DB error : ctsvc_stmt_step() Fail(%d)", ret);
860 ctsvc_stmt_finalize(stmt);
861 contacts_list_destroy(list, true);
864 my_profile_id = ctsvc_stmt_get_int(stmt, 0);
865 ret = ctsvc_db_get_record(_contacts_my_profile._uri, my_profile_id, &record);
866 if (CONTACTS_ERROR_NONE != ret) {
867 CTS_ERR("DB error : contacts_db_get_record() Fail(%d)", ret);
868 ctsvc_stmt_finalize(stmt);
869 contacts_list_destroy(list, true);
872 ctsvc_list_prepend(list, record);
874 ctsvc_stmt_finalize(stmt);
875 ctsvc_list_reverse(list);
877 *out_list = (contacts_list_h)list;
878 return CONTACTS_ERROR_NONE;
881 static int __ctsvc_db_my_profile_get_records_with_query(contacts_query_h query, int offset, int limit, contacts_list_h* out_list)
886 ctsvc_query_s *s_query;
888 contacts_list_h list;
889 ctsvc_my_profile_s *my_profile;
890 char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
891 bool had_my_profile_id = false;
892 int my_profile_id = 0;
894 RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
895 s_query = (ctsvc_query_s *)query;
897 if (s_query->projection) {
898 for (i=0;i<s_query->projection_count;i++) {
899 if (s_query->projection[i] == CTSVC_PROPERTY_MY_PROFILE_ID) {
900 had_my_profile_id = true;
906 s_query->projection_count = 0;
907 had_my_profile_id = true;
910 if (false == had_my_profile_id) {
911 s_query->projection = realloc(s_query->projection, s_query->projection_count+1);
912 if (NULL == s_query->projection) {
913 CTS_ERR("realloc() Fail");
914 return CONTACTS_ERROR_OUT_OF_MEMORY;
916 s_query->projection[s_query->projection_count] = CTSVC_PROPERTY_MY_PROFILE_ID;
917 s_query->projection_count++;
920 ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
921 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
923 contacts_list_create(&list);
924 while ((ret = ctsvc_stmt_step(stmt))) {
925 contacts_record_h record;
927 CTS_ERR("DB error : ctsvc_stmt_step() Fail(%d)", ret);
928 ctsvc_stmt_finalize(stmt);
929 contacts_list_destroy(list, true);
933 contacts_record_create(_contacts_my_profile._uri, &record);
934 my_profile = (ctsvc_my_profile_s*)record;
935 if (0 == s_query->projection_count)
936 field_count = s_query->property_count;
938 field_count = s_query->projection_count;
939 ret = ctsvc_record_set_projection_flags(record, s_query->projection,
940 s_query->projection_count, s_query->property_count);
942 if (CONTACTS_ERROR_NONE != ret)
943 ASSERT_NOT_REACHED("To set projection is Fail.\n");
946 for (i=0;i<field_count;i++) {
949 if (0 == s_query->projection_count)
950 property_id = s_query->properties[i].property_id;
952 property_id = s_query->projection[i];
954 switch(property_id) {
955 case CTSVC_PROPERTY_MY_PROFILE_ID:
956 my_profile_id = ctsvc_stmt_get_int(stmt, i);
957 if (had_my_profile_id)
958 my_profile->id = my_profile_id;
960 case CTSVC_PROPERTY_MY_PROFILE_DISPLAY_NAME:
961 temp = ctsvc_stmt_get_text(stmt, i);
962 free(my_profile->display_name);
963 my_profile->display_name = SAFE_STRDUP(temp);
965 case CTSVC_PROPERTY_MY_PROFILE_ADDRESSBOOK_ID:
966 my_profile->addressbook_id = ctsvc_stmt_get_int(stmt, i);
968 case CTSVC_PROPERTY_MY_PROFILE_IMAGE_THUMBNAIL:
969 temp = ctsvc_stmt_get_text(stmt, i);
971 snprintf(full_path, sizeof(full_path), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, temp);
972 free(my_profile->image_thumbnail_path);
973 my_profile->image_thumbnail_path = strdup(full_path);
976 case CTSVC_PROPERTY_MY_PROFILE_CHANGED_TIME:
977 my_profile->changed_time = ctsvc_stmt_get_int(stmt, i);
979 case CTSVC_PROPERTY_MY_PROFILE_UID:
980 temp = ctsvc_stmt_get_text(stmt, i);
981 free(my_profile->uid);
982 my_profile->uid = SAFE_STRDUP(temp);
988 ret = __ctsvc_db_my_profile_get_data(my_profile_id, my_profile);
989 if (CONTACTS_ERROR_NONE != ret) {
990 CTS_ERR("__ctsvc_db_my_profile_get_data Fail(%d)", ret);
991 ctsvc_stmt_finalize(stmt);
992 contacts_list_destroy(list, true);
996 ctsvc_list_prepend(list, record);
998 ctsvc_stmt_finalize(stmt);
999 ctsvc_list_reverse(list);
1001 *out_list = (contacts_list_h)list;
1003 return CONTACTS_ERROR_NONE;
1006 static int __ctsvc_my_profile_insert_data(ctsvc_my_profile_s *contact)
1010 /* Insert the name */
1011 if (contact->name) {
1012 ret = ctsvc_contact_insert_data_name((contacts_list_h)contact->name, contact->id, true);
1013 if (CONTACTS_ERROR_NONE != ret) {
1014 CTS_ERR("ctsvc_contact_insert_data_name() Fail(%d)", ret);
1019 /* Insert the company */
1020 if (contact->company) {
1021 ret = ctsvc_contact_insert_data_company((contacts_list_h)contact->company, contact->id, true);
1022 if (CONTACTS_ERROR_NONE != ret) {
1023 CTS_ERR("ctsvc_insert_my_profile_data_company() Fail(%d)", ret);
1028 /* Insert the events */
1029 if (contact->events) {
1030 ret = ctsvc_contact_insert_data_event((contacts_list_h)contact->events, contact->id, true);
1031 if (CONTACTS_ERROR_NONE != ret) {
1032 CTS_ERR("ctsvc_insert_my_profile_data_event() Fail(%d)", ret);
1037 /* Insert the messengers */
1038 if (contact->messengers) {
1039 ret = ctsvc_contact_insert_data_messenger((contacts_list_h)contact->messengers, contact->id, true);
1040 if (CONTACTS_ERROR_NONE != ret) {
1041 CTS_ERR("ctsvc_insert_my_profile_data_messenger() Fail(%d)", ret);
1046 /* Insert the postals */
1047 if (contact->postal_addrs) {
1048 ret = ctsvc_contact_insert_data_address((contacts_list_h)contact->postal_addrs, contact->id, true);
1049 if (CONTACTS_ERROR_NONE != ret) {
1050 CTS_ERR("ctsvc_insert_my_profile_data_postal() Fail(%d)", ret);
1055 /* Insert the Web addrs */
1056 if (contact->urls) {
1057 ret = ctsvc_contact_insert_data_url((contacts_list_h)contact->urls, contact->id, true);
1058 if (CONTACTS_ERROR_NONE != ret) {
1059 CTS_ERR("ctsvc_insert_my_profile_data_web() Fail(%d)", ret);
1064 /* Insert the Nick names */
1065 if (contact->nicknames) {
1066 ret = ctsvc_contact_insert_data_nickname((contacts_list_h)contact->nicknames, contact->id, true);
1067 if (CONTACTS_ERROR_NONE != ret) {
1068 CTS_ERR("ctsvc_insert_my_profile_data_nickname() Fail(%d)", ret);
1073 /* Insert the numbers */
1074 if (contact->numbers) {
1075 ret = ctsvc_contact_insert_data_number((contacts_list_h)contact->numbers, contact->id, true);
1076 if (ret < CONTACTS_ERROR_NONE) {
1077 CTS_ERR("ctsvc_contact_insert_data_number() Fail(%d)", ret);
1082 /* Insert the emails */
1083 if (contact->emails) {
1084 ret = ctsvc_contact_insert_data_email((contacts_list_h)contact->emails, contact->id, true);
1085 if (ret < CONTACTS_ERROR_NONE) {
1086 CTS_ERR("ctsvc_insert_my_profile_data_email() Fail(%d)", ret);
1091 /* Insert the profile values */
1092 if (contact->profiles) {
1093 ret = ctsvc_contact_insert_data_profile((contacts_list_h)contact->profiles, contact->id, true);
1094 if (CONTACTS_ERROR_NONE != ret) {
1095 CTS_ERR("ctsvc_insert_my_profile_data_profile() Fail(%d)", ret);
1100 /* Insert the relationship values */
1101 if (contact->relationships) {
1102 ret = ctsvc_contact_insert_data_relationship((contacts_list_h)contact->relationships, contact->id, true);
1103 if (CONTACTS_ERROR_NONE != ret) {
1104 CTS_ERR("ctsvc_contact_insert_data_relationship() Fail(%d)", ret);
1109 /* Insert the image values */
1110 if (contact->images) {
1111 ret = ctsvc_contact_insert_data_image((contacts_list_h)contact->images, contact->id, true);
1112 if (CONTACTS_ERROR_NONE != ret) {
1113 CTS_ERR("ctsvc_contact_insert_data_image() Fail(%d)", ret);
1118 /* Insert the note values */
1119 if (contact->note) {
1120 ret = ctsvc_contact_insert_data_note((contacts_list_h)contact->note, contact->id, true);
1121 if (CONTACTS_ERROR_NONE != ret) {
1122 CTS_ERR("ctsvc_contact_insert_data_note() Fail(%d)", ret);
1127 /* Insert the extensions values */
1128 if (contact->extensions) {
1129 ret = ctsvc_contact_insert_data_extension((contacts_list_h)contact->extensions, contact->id, true);
1130 if (CONTACTS_ERROR_NONE != ret) {
1131 CTS_ERR("ctsvc_contact_insert_data_extension() Fail(%d)", ret);
1136 return CONTACTS_ERROR_NONE;
1139 static int __ctsvc_db_my_profile_insert_record(contacts_record_h record, int *id)
1144 char query[CTS_SQL_MAX_LEN] = {0};
1146 ctsvc_my_profile_s *my_profile = (ctsvc_my_profile_s*)record;
1149 /* These check should be done in client side */
1150 RETVM_IF(NULL == my_profile, CONTACTS_ERROR_INVALID_PARAMETER,
1151 "Invalid parameter : my_profile is NULL");
1152 RETVM_IF(my_profile->addressbook_id < 0, CONTACTS_ERROR_INVALID_PARAMETER,
1153 "Invalid parameter : addressbook_id(%d) is mandatory field to insert my_profile record ", my_profile->addressbook_id);
1154 RETVM_IF(0 < my_profile->id, CONTACTS_ERROR_INVALID_PARAMETER,
1155 "Invalid parameter : id(%d), This record is already inserted", my_profile->id);
1157 ret = ctsvc_begin_trans();
1158 RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_begin_trans() Fail(%d)", ret);
1160 if (false == ctsvc_have_ab_write_permission(my_profile->addressbook_id)) {
1161 CTS_ERR("ctsvc_have_ab_write_permission fail : does not have permission(addressbook_id : %d)",
1162 my_profile->addressbook_id);
1163 ctsvc_end_trans(false);
1164 return CONTACTS_ERROR_PERMISSION_DENIED;
1167 snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_MY_PROFILES" WHERE addressbook_id = %d AND deleted = 1", my_profile->addressbook_id);
1168 ret = ctsvc_query_exec(query);
1169 WARN_IF(CONTACTS_ERROR_NONE != ret, "Delete deleted my_profile of addressbook(%d) Fail", my_profile->addressbook_id);
1171 ret = ctsvc_db_get_next_id(CTS_TABLE_MY_PROFILES);
1172 if (ret < CONTACTS_ERROR_NONE) {
1173 CTS_ERR("ctsvc_db_get_next_id() Fail(%d)", ret);
1174 ctsvc_end_trans(false);
1177 my_profile->id = ret;
1181 __ctsvc_make_my_profile_display_name(my_profile);
1182 __ctsvc_my_profile_check_default_data(my_profile);
1185 ret = __ctsvc_my_profile_insert_data(my_profile);
1186 if (CONTACTS_ERROR_NONE != ret) {
1187 CTS_ERR("cts_insert_my_profile_data() Fail(%d)", ret);
1188 ctsvc_end_trans(false);
1192 /******************************/
1193 /* this code will be removed. */
1194 free(my_profile->image_thumbnail_path);
1195 my_profile->image_thumbnail_path = NULL;
1197 if (my_profile->images) {
1198 ctsvc_image_s *image;
1201 contacts_list_get_count((contacts_list_h)my_profile->images, &count);
1204 contacts_list_first((contacts_list_h)my_profile->images);
1205 ret = contacts_list_get_current_record_p((contacts_list_h)my_profile->images, (contacts_record_h*)&image);
1206 if (CONTACTS_ERROR_NONE != ret) {
1207 CTS_ERR("contacts_list_get_current_record_p() Fail(%d)", ret);
1208 ctsvc_end_trans(false);
1209 return CONTACTS_ERROR_DB;
1212 if (image->path && image->is_default) {
1213 my_profile->image_thumbnail_path = strdup(image->path);
1219 /* this code will be removed. */
1220 /******************************/
1222 version = ctsvc_get_next_ver();
1224 snprintf(query, sizeof(query),
1225 "INSERT INTO "CTS_TABLE_MY_PROFILES"(my_profile_id, addressbook_id, "
1226 "created_ver, changed_ver, changed_time, "
1227 "display_name, reverse_display_name, uid, image_thumbnail_path) "
1228 "VALUES(%d, %d, %d, %d, %d, ?, ?, ?, ?)",
1229 my_profile->id, my_profile->addressbook_id, version, version, (int)time(NULL));
1231 ret = ctsvc_query_prepare(query, &stmt);
1233 CTS_ERR("ctsvc_query_prepare() Fail(%d)", ret);
1234 ctsvc_end_trans(false);
1238 if (my_profile->display_name)
1239 ctsvc_stmt_bind_text(stmt, 1, my_profile->display_name);
1240 if (my_profile->reverse_display_name)
1241 ctsvc_stmt_bind_text(stmt, 2, my_profile->reverse_display_name);
1242 if (my_profile->uid)
1243 ctsvc_stmt_bind_text(stmt, 3, my_profile->uid);
1244 if (my_profile->image_thumbnail_path)
1245 ctsvc_stmt_bind_text(stmt, 4, my_profile->image_thumbnail_path);
1247 ret = ctsvc_stmt_step(stmt);
1248 if (CONTACTS_ERROR_NONE != ret) {
1249 CTS_ERR("ctsvc_stmt_step() Fail(%d)", ret);
1250 ctsvc_stmt_finalize(stmt);
1251 ctsvc_end_trans(false);
1254 ctsvc_stmt_finalize(stmt);
1256 ctsvc_set_my_profile_noti();
1258 ret = ctsvc_end_trans(true);
1259 RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "contacts_svc_end_trans() Fail(%d)", ret);
1261 return CONTACTS_ERROR_NONE;