[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_my_profile.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "contacts.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"
40
41 #include "ctsvc_db_plugin_contact_helper.h"
42
43 #define CTSVC_MY_PROFILE_DISPLAY_NAME_MAX_LEN 1024
44
45
46 static int __ctsvc_db_get_my_profile_base_info(int id, ctsvc_my_profile_s *my_profile)
47 {
48         int ret;
49         int i;
50         cts_stmt stmt = NULL;
51         char query[CTS_SQL_MAX_LEN] = {0};
52         char *temp;
53         char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
54
55         snprintf(query, sizeof(query),
56                         "SELECT my_profile_id, addressbook_id, changed_time, %s, image_thumbnail_path, uid "
57                         "FROM "CTS_TABLE_MY_PROFILES" WHERE my_profile_id = %d AND deleted = 0",
58                         ctsvc_get_display_column(), id);
59
60         ret = ctsvc_query_prepare(query, &stmt);
61         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
62
63         ret = ctsvc_stmt_step(stmt);
64         if (1 /*CTS_TRUE*/ != ret) {
65                 /* LCOV_EXCL_START */
66                 ERR("ctsvc_stmt_step() Fail(%d)", ret);
67                 ctsvc_stmt_finalize(stmt);
68                 if (CONTACTS_ERROR_NONE == ret)
69                         return CONTACTS_ERROR_NO_DATA;
70                 else
71                         return ret;
72                 /* LCOV_EXCL_STOP */
73         }
74
75         i = 0;
76         my_profile->id = ctsvc_stmt_get_int(stmt, i++);
77         my_profile->addressbook_id = ctsvc_stmt_get_int(stmt, i++);
78         my_profile->changed_time = ctsvc_stmt_get_int(stmt, i++);
79         temp = ctsvc_stmt_get_text(stmt, i++);
80         my_profile->display_name = SAFE_STRDUP(temp);
81         temp = ctsvc_stmt_get_text(stmt, i++);
82         if (temp) {
83                 snprintf(full_path, sizeof(full_path), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, temp);
84                 my_profile->image_thumbnail_path = strdup(full_path);
85         }
86         temp = ctsvc_stmt_get_text(stmt, i++);
87         my_profile->uid = SAFE_STRDUP(temp);
88         ctsvc_stmt_finalize(stmt);
89
90         return CONTACTS_ERROR_NONE;
91 }
92
93 static int __ctsvc_db_my_profile_get_data(int id, ctsvc_my_profile_s *my_profile)
94 {
95         int ret;
96         int datatype;
97         cts_stmt stmt = NULL;
98         char query[CTS_SQL_MAX_LEN] = {0};
99
100         snprintf(query, sizeof(query),
101                         "SELECT datatype, id, contact_id, is_default, data1, data2, "
102                         "data3, data4, data5, data6, data7, data8, data9, data10, data11, data12 "
103                         "FROM "CTS_TABLE_DATA" WHERE contact_id = %d AND is_my_profile = 1", id);
104
105         ret = ctsvc_query_prepare(query, &stmt);
106         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
107
108         ret = ctsvc_stmt_step(stmt);
109         if (1 /*CTS_TRUE */!= ret) {
110                 /* LCOV_EXCL_START */
111                 ERR("ctsvc_stmt_step() Fail(%d)", ret);
112                 ctsvc_stmt_finalize(stmt);
113                 return ret;
114                 /* LCOV_EXCL_STOP */
115         }
116
117         do {
118                 datatype = ctsvc_stmt_get_int(stmt, 0);
119                 switch (datatype) {
120                 case CONTACTS_DATA_TYPE_NAME:
121                         ctsvc_get_data_info_name(stmt, (contacts_list_h)my_profile->name);
122                         break;
123                 case CONTACTS_DATA_TYPE_EVENT:
124                         ctsvc_get_data_info_event(stmt, (contacts_list_h)my_profile->events);
125                         break;
126                 case CONTACTS_DATA_TYPE_MESSENGER:
127                         ctsvc_get_data_info_messenger(stmt, (contacts_list_h)my_profile->messengers);
128                         break;
129                 case CONTACTS_DATA_TYPE_POSTAL:
130                         ctsvc_get_data_info_address(stmt, (contacts_list_h)my_profile->postal_addrs);
131                         break;
132                 case CONTACTS_DATA_TYPE_URL:
133                         ctsvc_get_data_info_url(stmt, (contacts_list_h)my_profile->urls);
134                         break;
135                 case CONTACTS_DATA_TYPE_NICKNAME:
136                         ctsvc_get_data_info_nickname(stmt, (contacts_list_h)my_profile->nicknames);
137                         break;
138                 case CONTACTS_DATA_TYPE_NUMBER:
139                         ctsvc_get_data_info_number(stmt, (contacts_list_h)my_profile->numbers);
140                         break;
141                 case CONTACTS_DATA_TYPE_EMAIL:
142                         ctsvc_get_data_info_email(stmt, (contacts_list_h)my_profile->emails);
143                         break;
144                 case CONTACTS_DATA_TYPE_PROFILE:
145                         ctsvc_get_data_info_profile(stmt, (contacts_list_h)my_profile->profiles);
146                         break;
147                 case CONTACTS_DATA_TYPE_RELATIONSHIP:
148                         ctsvc_get_data_info_relationship(stmt, (contacts_list_h)my_profile->relationships);
149                         break;
150                 case CONTACTS_DATA_TYPE_IMAGE:
151                         ctsvc_get_data_info_image(stmt, (contacts_list_h)my_profile->images);
152                         break;
153                 case CONTACTS_DATA_TYPE_COMPANY:
154                         ctsvc_get_data_info_company(stmt, (contacts_list_h)my_profile->company);
155                         break;
156                 case CONTACTS_DATA_TYPE_NOTE:
157                         ctsvc_get_data_info_note(stmt, (contacts_list_h)my_profile->note);
158                         break;
159                 case CONTACTS_DATA_TYPE_EXTENSION:
160                         ctsvc_get_data_info_extension(stmt, (contacts_list_h)my_profile->extensions);
161                         break;
162                 case CONTACTS_DATA_TYPE_SIP:
163                         ctsvc_get_data_info_sip(stmt, (contacts_list_h)my_profile->sips);
164                         break;
165                 default:
166                         /* LCOV_EXCL_START */
167                         ERR("Intenal : Not supported data type (%d)", datatype);
168                         break;
169                         /* LCOV_EXCL_STOP */
170                 }
171
172         } while (1 /*CTS_TRUE*/ == ctsvc_stmt_step(stmt));
173
174         ctsvc_stmt_finalize(stmt);
175
176         return CONTACTS_ERROR_NONE;
177
178 }
179
180 static int __ctsvc_db_my_profile_get_record(int id, contacts_record_h *out_record)
181 {
182         int ret;
183         contacts_record_h record;
184         ctsvc_my_profile_s *my_profile;
185
186         RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
187         *out_record = NULL;
188
189         contacts_record_create(_contacts_my_profile._uri, &record);
190         my_profile = (ctsvc_my_profile_s*)record;
191         ret = __ctsvc_db_get_my_profile_base_info(id, my_profile);
192         if (CONTACTS_ERROR_NONE != ret) {
193                 /* LCOV_EXCL_START */
194                 ERR("cts_get_main_contacts_info(ALL) Fail(%d)", ret);
195                 contacts_record_destroy(record, true);
196                 return ret;
197                 /* LCOV_EXCL_STOP */
198         }
199
200         ret = __ctsvc_db_my_profile_get_data(id, my_profile);
201         if (CONTACTS_ERROR_NONE != ret) {
202                 /* LCOV_EXCL_START */
203                 ERR("__ctsvc_db_my_profile_get_data Fail(%d)", ret);
204                 contacts_record_destroy(record, true);
205                 return ret;
206                 /* LCOV_EXCL_STOP */
207         }
208
209         *out_record = record;
210
211         return CONTACTS_ERROR_NONE;
212 }
213
214 static int __ctsvc_db_my_profile_delete_record(int id)
215 {
216         CTS_FN_CALL;
217         int ret;
218         int addressbook_id;
219         char query[CTS_SQL_MAX_LEN] = {0};
220
221         ret = ctsvc_begin_trans();
222         RETVM_IF(ret, ret, "ctsvc_begin_trans() Fail(%d)", ret);
223
224         snprintf(query, sizeof(query),
225                         "SELECT addressbook_id FROM "CTSVC_DB_VIEW_MY_PROFILE" WHERE my_profile_id = %d", id);
226         ret  = ctsvc_query_get_first_int_result(query, &addressbook_id);
227         if (CONTACTS_ERROR_NONE != ret) {
228                 /* LCOV_EXCL_START */
229                 ERR("ctsvc_query_get_first_int_result() Fail(%d)", ret);
230                 ctsvc_end_trans(false);
231                 return ret;
232                 /* LCOV_EXCL_STOP */
233         }
234
235         if (false == ctsvc_have_ab_write_permission(addressbook_id, false)) {
236                 /* LCOV_EXCL_START */
237                 ERR("No permission in this addresbook_id(%d)", addressbook_id);
238                 ctsvc_end_trans(false);
239                 return CONTACTS_ERROR_PERMISSION_DENIED;
240                 /* LCOV_EXCL_STOP */
241         }
242
243         snprintf(query, sizeof(query), "UPDATE "CTS_TABLE_MY_PROFILES" "
244                         "SET deleted = 1, changed_ver = %d WHERE my_profile_id = %d", ctsvc_get_next_ver(), id);
245         ret = ctsvc_query_exec(query);
246         if (CONTACTS_ERROR_NONE != ret) {
247                 /* LCOV_EXCL_START */
248                 ERR("ctsvc_query_exec() Fail(%d)", ret);
249                 ctsvc_end_trans(false);
250                 return ret;
251                 /* LCOV_EXCL_STOP */
252         }
253
254         ctsvc_set_my_profile_noti();
255
256         ret = ctsvc_end_trans(true);
257         if (ret < CONTACTS_ERROR_NONE) {
258                 /* LCOV_EXCL_START */
259                 ERR("ctsvc_end_trans() Fail(%d)", ret);
260                 return ret;
261                 /* LCOV_EXCL_STOP */
262         } else {
263                 return CONTACTS_ERROR_NONE;
264         }
265 }
266
267 static inline int __ctsvc_my_profile_update_data(ctsvc_my_profile_s *my_profile)
268 {
269         int ret;
270
271         if (my_profile->name) {
272                 ret = ctsvc_contact_update_data_name((contacts_list_h)my_profile->name, my_profile->id, true);
273                 if (CONTACTS_ERROR_NONE != ret) {
274                         /* LCOV_EXCL_START */
275                         ERR("ctsvc_contact_update_data_name() Fail(%d)", ret);
276                         return ret;
277                         /* LCOV_EXCL_STOP */
278                 }
279         }
280
281         if (my_profile->company) {
282                 ret = ctsvc_contact_update_data_company((contacts_list_h)my_profile->company, my_profile->id, true);
283                 if (CONTACTS_ERROR_NONE != ret) {
284                         /* LCOV_EXCL_START */
285                         ERR("ctsvc_contact_update_data_company() Fail(%d)", ret);
286                         return ret;
287                         /* LCOV_EXCL_STOP */
288                 }
289         }
290
291         if (my_profile->note) {
292                 ret = ctsvc_contact_update_data_note((contacts_list_h)my_profile->note, my_profile->id, true);
293                 if (CONTACTS_ERROR_NONE != ret) {
294                         /* LCOV_EXCL_START */
295                         ERR("ctsvc_contact_update_data_note() Fail(%d)", ret);
296                         return ret;
297                         /* LCOV_EXCL_STOP */
298                 }
299         }
300
301         if (my_profile->events) {
302                 ret = ctsvc_contact_update_data_event((contacts_list_h)my_profile->events, my_profile->id, true);
303                 if (CONTACTS_ERROR_NONE != ret) {
304                         /* LCOV_EXCL_START */
305                         ERR("ctsvc_contact_update_data_events() Fail(%d)", ret);
306                         return ret;
307                         /* LCOV_EXCL_STOP */
308                 }
309         }
310
311         if (my_profile->messengers) {
312                 ret = ctsvc_contact_update_data_messenger((contacts_list_h)my_profile->messengers, my_profile->id, true);
313                 if (CONTACTS_ERROR_NONE != ret) {
314                         /* LCOV_EXCL_START */
315                         ERR("ctsvc_contact_update_data_messengers() Fail(%d)", ret);
316                         return ret;
317                         /* LCOV_EXCL_STOP */
318                 }
319         }
320
321         if (my_profile->postal_addrs) {
322                 ret = ctsvc_contact_update_data_address((contacts_list_h)my_profile->postal_addrs, my_profile->id, true);
323                 if (CONTACTS_ERROR_NONE != ret) {
324                         /* LCOV_EXCL_START */
325                         ERR("ctsvc_contact_update_data_address() Fail(%d)", ret);
326                         return ret;
327                         /* LCOV_EXCL_STOP */
328                 }
329         }
330
331         if (my_profile->urls) {
332                 ret = ctsvc_contact_update_data_url((contacts_list_h)my_profile->urls, my_profile->id, true);
333                 if (CONTACTS_ERROR_NONE != ret) {
334                         /* LCOV_EXCL_START */
335                         ERR("ctsvc_contact_update_data_url() Fail(%d)", ret);
336                         return ret;
337                         /* LCOV_EXCL_STOP */
338                 }
339         }
340
341         if (my_profile->nicknames) {
342                 ret = ctsvc_contact_update_data_nickname((contacts_list_h)my_profile->nicknames, my_profile->id, true);
343                 if (CONTACTS_ERROR_NONE != ret) {
344                         /* LCOV_EXCL_START */
345                         ERR("ctsvc_contact_update_data_nickname() Fail(%d)", ret);
346                         return ret;
347                         /* LCOV_EXCL_STOP */
348                 }
349         }
350
351         if (my_profile->numbers) {
352                 bool had_phonenumber;
353                 ret = ctsvc_contact_update_data_number((contacts_list_h)my_profile->numbers, my_profile->id, true, &had_phonenumber);
354                 if (CONTACTS_ERROR_NONE != ret) {
355                         /* LCOV_EXCL_START */
356                         ERR("ctsvc_contact_update_data_number() Fail(%d)", ret);
357                         return ret;
358                         /* LCOV_EXCL_STOP */
359                 }
360         }
361
362         if (my_profile->emails) {
363                 bool had_email;
364                 ret = ctsvc_contact_update_data_email((contacts_list_h)my_profile->emails, my_profile->id, true, &had_email);
365                 if (CONTACTS_ERROR_NONE != ret) {
366                         /* LCOV_EXCL_START */
367                         ERR("ctsvc_contact_update_data_email() Fail(%d)", ret);
368                         return ret;
369                         /* LCOV_EXCL_STOP */
370                 }
371         }
372
373         if (my_profile->profiles) {
374                 ret = ctsvc_contact_update_data_profile((contacts_list_h)my_profile->profiles, my_profile->id, true);
375                 if (CONTACTS_ERROR_NONE != ret) {
376                         /* LCOV_EXCL_START */
377                         ERR("ctsvc_contact_update_data_profile() Fail(%d)", ret);
378                         return ret;
379                         /* LCOV_EXCL_STOP */
380                 }
381         }
382
383         if (my_profile->relationships) {
384                 ret = ctsvc_contact_update_data_relationship((contacts_list_h)my_profile->relationships, my_profile->id, true);
385                 if (CONTACTS_ERROR_NONE != ret) {
386                         /* LCOV_EXCL_START */
387                         ERR("ctsvc_contact_update_data_relationship() Fail(%d)", ret);
388                         return ret;
389                         /* LCOV_EXCL_STOP */
390                 }
391         }
392
393         if (my_profile->images) {
394                 ret = ctsvc_contact_update_data_image((contacts_list_h)my_profile->images, my_profile->id, true);
395                 if (CONTACTS_ERROR_NONE != ret) {
396                         /* LCOV_EXCL_START */
397                         ERR("ctsvc_contact_update_data_image() Fail(%d)", ret);
398                         return ret;
399                         /* LCOV_EXCL_STOP */
400                 }
401         }
402
403         if (my_profile->extensions) {
404                 ret = ctsvc_contact_update_data_extension((contacts_list_h)my_profile->extensions, my_profile->id, true);
405                 if (CONTACTS_ERROR_NONE != ret) {
406                         /* LCOV_EXCL_START */
407                         ERR("ctsvc_contact_update_data_extension() Fail(%d)", ret);
408                         return ret;
409                         /* LCOV_EXCL_STOP */
410                 }
411         }
412
413         return CONTACTS_ERROR_NONE;
414 }
415
416 static void __ctsvc_my_profile_check_default_data(ctsvc_my_profile_s *my_profile)
417 {
418         ctsvc_contact_check_default_number((contacts_list_h)my_profile->numbers);
419         ctsvc_contact_check_default_email((contacts_list_h)my_profile->emails);
420         ctsvc_contact_check_default_image((contacts_list_h)my_profile->images);
421         ctsvc_contact_check_default_address((contacts_list_h)my_profile->postal_addrs);
422 }
423
424 static void __ctsvc_make_my_profile_display_name(ctsvc_my_profile_s *my_profile)
425 {
426         ctsvc_name_s *name = NULL;
427
428         free(my_profile->display_name);
429         my_profile->display_name = NULL;
430
431         free(my_profile->reverse_display_name);
432         my_profile->reverse_display_name = NULL;
433
434         if (0 < my_profile->name->count && my_profile->name->records
435                         && my_profile->name->records->data) {
436                 name = (ctsvc_name_s*)my_profile->name->records->data;
437         }
438
439         if (name && (name->first || name->last  || name->prefix || name->addition
440                                 || name->suffix)) {
441                 char *display = NULL;
442                 int len, display_len;
443                 int reverse_lang_type = -1;
444                 int temp_display_len;
445                 char *temp_display = NULL;
446
447                 /*
448                  * Make reverse display name (Last name first)
449                  * Default         : Prefix Last, First Middle(addition), Suffix
450                  * Korean, Chinese : Prefix LastFirstMiddleSuffix
451                  * Japanese        : Prefix Last Middle First Suffix
452                  * reverse sort name does not include prefix
453                  *    But, if there is only prefix, reverse sort_name is prefix
454                  */
455                 temp_display_len = SAFE_STRLEN(name->first)
456                         + SAFE_STRLEN(name->addition)
457                         + SAFE_STRLEN(name->last)
458                         + SAFE_STRLEN(name->suffix);
459                 if (0 < temp_display_len) {
460                         temp_display_len += 7;
461                         temp_display = calloc(1, temp_display_len);
462                         if (NULL == temp_display) {
463                                 /* LCOV_EXCL_START */
464                                 ERR("calloc() Fail");
465                                 return;
466                                 /* LCOV_EXCL_STOP */
467                         }
468                         len = 0;
469
470                         if (name->last) {
471                                 len += snprintf(temp_display + len, temp_display_len - len, "%s",
472                                                 name->last);
473
474                                 if (reverse_lang_type < 0)
475                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
476
477                                 if (reverse_lang_type != CTSVC_LANG_KOREAN &&
478                                                 reverse_lang_type != CTSVC_LANG_CHINESE &&
479                                                 reverse_lang_type != CTSVC_LANG_JAPANESE) {
480                                         if (name->first || name->addition)
481                                                 len += snprintf(temp_display + len, temp_display_len - len, ",");
482                                 }
483                         }
484
485                         if (reverse_lang_type < 0) {
486                                 if (*temp_display)
487                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
488                                 else if (name->first)
489                                         reverse_lang_type = ctsvc_check_language_type(name->first);
490                                 else if (name->addition)
491                                         reverse_lang_type = ctsvc_check_language_type(name->addition);
492                         }
493
494                         if (reverse_lang_type == CTSVC_LANG_JAPANESE) {
495                                 /* make temp_display name Prefix - Last - Middle - First - Suffix */
496                                 if (name->addition) {
497                                         if (*temp_display)
498                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
499                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
500                                 }
501
502                                 if (name->first) {
503                                         if (*temp_display)
504                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
505                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
506                                 }
507                         } else {
508                                 if (name->first) {
509                                         if (*temp_display) {
510                                                 if (reverse_lang_type < 0)
511                                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
512
513                                                 if (reverse_lang_type != CTSVC_LANG_KOREAN &&
514                                                                 reverse_lang_type != CTSVC_LANG_CHINESE)
515                                                         len += snprintf(temp_display + len, temp_display_len - len, " ");
516                                         }
517                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
518                                 }
519
520                                 if (name->addition) {
521                                         if (*temp_display) {
522                                                 if (reverse_lang_type < 0)
523                                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
524
525                                                 if (reverse_lang_type != CTSVC_LANG_KOREAN &&
526                                                                 reverse_lang_type != CTSVC_LANG_CHINESE)
527                                                         len += snprintf(temp_display + len, temp_display_len - len, " ");
528                                         }
529                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
530                                 }
531                         }
532
533                         if (name->suffix) {
534                                 if (*temp_display) {
535                                         if (reverse_lang_type < 0)
536                                                 reverse_lang_type = ctsvc_check_language_type(temp_display);
537
538                                         if (reverse_lang_type == CTSVC_LANG_JAPANESE) {
539                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
540                                         } else if (reverse_lang_type != CTSVC_LANG_KOREAN &&
541                                                         reverse_lang_type != CTSVC_LANG_CHINESE) {
542                                                 len += snprintf(temp_display + len, temp_display_len - len, ", ");
543                                         }
544                                 }
545                                 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->suffix);
546                         }
547                 }
548
549                 if (name->prefix && temp_display) {
550                         display_len = SAFE_STRLEN(name->prefix) + temp_display_len + 2;
551                         display = calloc(1, display_len);
552                         if (NULL == display) {
553                                 /* LCOV_EXCL_START */
554                                 ERR("calloc() Fail");
555                                 free(temp_display);
556                                 return;
557                                 /* LCOV_EXCL_STOP */
558                         }
559                         snprintf(display, display_len, "%s %s", name->prefix, temp_display);
560                         my_profile->reverse_display_name = display;
561                         free(temp_display);
562                 } else if (temp_display) {
563                         my_profile->reverse_display_name = temp_display;
564                 } else if (name->prefix) {
565                         my_profile->reverse_display_name = strdup(name->prefix);
566                 }
567
568                 /*
569                  * Make display name (First name first)
570                  * Default         : Prefix First Middle Last, Suffix
571                  * Korean, Chinese : Prefix LastFirstMiddleSuffix (Same as reverse display name)
572                  * Japanese        : Prefix First Middle Last Suffix
573                  * sort name does not include prefix
574                  *    But, if there is only prefix, sort_name is prefix
575                  */
576                 if (reverse_lang_type == CTSVC_LANG_KOREAN ||
577                                 reverse_lang_type == CTSVC_LANG_CHINESE) {
578                         my_profile->display_name = SAFE_STRDUP(my_profile->reverse_display_name);
579                 } else {
580                         int lang_type = -1;
581                         temp_display = NULL;
582                         temp_display_len = SAFE_STRLEN(name->first)
583                                 + SAFE_STRLEN(name->addition)
584                                 + SAFE_STRLEN(name->last)
585                                 + SAFE_STRLEN(name->suffix);
586                         if (0 < temp_display_len) {
587                                 temp_display_len += 6;
588                                 /* make reverse_temp_display_name */
589                                 temp_display = calloc(1, temp_display_len);
590                                 if (NULL == temp_display) {
591                                         /* LCOV_EXCL_START */
592                                         ERR("calloc() Fail");
593                                         return;
594                                         /* LCOV_EXCL_STOP */
595                                 }
596                                 len = 0;
597
598                                 if (name->first) {
599                                         if (*temp_display)
600                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
601                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
602                                 }
603
604                                 if (name->addition) {
605                                         if (*temp_display)
606                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
607                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
608                                 }
609
610                                 if (name->last) {
611                                         if (*temp_display)
612                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
613                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->last);
614                                 }
615
616                                 if (name->suffix) {
617                                         if (*temp_display) {
618                                                 lang_type = ctsvc_check_language_type(temp_display);
619                                                 if (lang_type == CTSVC_LANG_JAPANESE)
620                                                         len += snprintf(temp_display + len, temp_display_len - len, " ");
621                                                 else
622                                                         len += snprintf(temp_display + len, temp_display_len - len, ", ");
623                                         }
624                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->suffix);
625                                 }
626
627                         }
628                         if (name->prefix && temp_display) {
629                                 display_len = SAFE_STRLEN(name->prefix) + temp_display_len + 2;
630                                 display = calloc(1, display_len);
631                                 if (NULL == display) {
632                                         /* LCOV_EXCL_START */
633                                         ERR("calloc() Fail");
634                                         free(temp_display);
635                                         return;
636                                         /* LCOV_EXCL_STOP */
637                                 }
638                                 snprintf(display, display_len, "%s %s", name->prefix, temp_display);
639                                 my_profile->display_name = display;
640                                 free(temp_display);
641                         } else if (temp_display) {
642                                 my_profile->display_name = temp_display;
643                         } else if (name->prefix) {
644                                 my_profile->display_name = strdup(name->prefix);
645                         }
646                 }
647
648                 ctsvc_record_set_property_flag((ctsvc_record_s*)my_profile,
649                                 _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
650         } else {
651                 GList *cur;
652                 if (my_profile->company && my_profile->company->records) {
653                         for (cur = my_profile->company->records; cur; cur = cur->next) {
654                                 ctsvc_company_s *company = cur->data;
655                                 if (company && company->name) {
656                                         ctsvc_record_set_property_flag((ctsvc_record_s*)my_profile,
657                                                         _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
658                                         my_profile->display_name = SAFE_STRDUP(company->name);
659                                         break;
660                                 }
661                         }
662                 }
663
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->nicknames && my_profile->nicknames->records) {
666                         for (cur = my_profile->nicknames->records; cur; cur = cur->next) {
667                                 ctsvc_nickname_s *nickname = cur->data;
668                                 if (nickname && nickname->nickname) {
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(nickname->nickname);
672                                         break;
673                                 }
674                         }
675                 }
676
677                 if (false == ctsvc_record_check_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY) &&
678                                 my_profile->numbers && my_profile->numbers->records) {
679                         for (cur = my_profile->numbers->records; cur; cur = cur->next) {
680                                 ctsvc_number_s *number = cur->data;
681                                 if (number && number->number) {
682                                         ctsvc_record_set_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
683                                         free(my_profile->display_name);
684                                         my_profile->display_name = SAFE_STRDUP(number->number);
685                                         break;
686                                 }
687                         }
688                 }
689
690                 if (false == ctsvc_record_check_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY) &&
691                                 my_profile->emails && my_profile->emails->records) {
692                         for (cur = my_profile->emails->records; cur; cur = cur->next) {
693                                 ctsvc_email_s *email = cur->data;
694                                 if (email && email->email_addr) {
695                                         ctsvc_record_set_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
696                                         free(my_profile->display_name);
697                                         my_profile->display_name = SAFE_STRDUP(email->email_addr);
698                                         break;
699                                 }
700                         }
701                 }
702
703                 if (ctsvc_record_check_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY)) {
704                         my_profile->reverse_display_name = SAFE_STRDUP(my_profile->display_name);
705                 } else {
706                         /* Update as NULL */
707                         ctsvc_record_set_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
708                 }
709         }
710         return;
711 }
712
713
714 static int __ctsvc_db_my_profile_update_record(contacts_record_h record)
715 {
716         int id;
717         int ret;
718         char *set = NULL;
719         char query[CTS_SQL_MAX_LEN] = {0};
720         ctsvc_my_profile_s *my_profile = (ctsvc_my_profile_s*)record;
721         GSList *bind_text = NULL;
722         GSList *cursor = NULL;
723
724         ret = ctsvc_begin_trans();
725         RETVM_IF(ret, ret, "ctsvc_begin_trans() Fail(%d)", ret);
726
727         snprintf(query, sizeof(query),
728                         "SELECT my_profile_id FROM "CTSVC_DB_VIEW_MY_PROFILE" WHERE my_profile_id = %d", my_profile->id);
729         ret = ctsvc_query_get_first_int_result(query, &id);
730         if (CONTACTS_ERROR_NONE != ret) {
731                 /* LCOV_EXCL_START */
732                 ERR("The index(%d) is Invalid. %d Record(s) is(are) found", my_profile->id, ret);
733                 ctsvc_end_trans(false);
734                 return ret;
735                 /* LCOV_EXCL_STOP */
736         }
737
738         if (false == ctsvc_have_ab_write_permission(my_profile->addressbook_id, false)) {
739                 /* LCOV_EXCL_START */
740                 ERR("No permission in this addresbook_id(%d)", my_profile->addressbook_id);
741                 ctsvc_end_trans(false);
742                 return CONTACTS_ERROR_PERMISSION_DENIED;
743                 /* LCOV_EXCL_STOP */
744         }
745
746         __ctsvc_make_my_profile_display_name(my_profile);
747         __ctsvc_my_profile_check_default_data(my_profile);
748
749         /* update data */
750         ret = __ctsvc_my_profile_update_data(my_profile);
751         if (CONTACTS_ERROR_NONE != ret) {
752                 /* LCOV_EXCL_START */
753                 ERR("__ctsvc_my_profile_update_data() Fail(%d)", ret);
754                 ctsvc_end_trans(false);
755                 return ret;
756                 /* LCOV_EXCL_STOP */
757         }
758
759         /* thumbnail */
760         if (my_profile->images) {
761                 int ret = CONTACTS_ERROR_NONE;
762                 int count = 0;
763                 ctsvc_image_s *image;
764
765                 contacts_list_get_count((contacts_list_h)my_profile->images, &count);
766                 if (count) {
767                         contacts_list_first((contacts_list_h)my_profile->images);
768                         do {
769                                 ret = contacts_list_get_current_record_p((contacts_list_h)my_profile->images, (contacts_record_h*)&image);
770                                 if (CONTACTS_ERROR_NONE != ret) {
771                                         /* LCOV_EXCL_START */
772                                         ERR("contacts_list_get_current_record_p() Fail(%d)", ret);
773                                         ctsvc_end_trans(false);
774                                         return CONTACTS_ERROR_DB;
775                                         /* LCOV_EXCL_STOP */
776                                 }
777
778                                 if (image->is_default)
779                                         break;
780                         } while (CONTACTS_ERROR_NONE == contacts_list_next((contacts_list_h)my_profile->images));
781
782                         if ((NULL == my_profile->image_thumbnail_path && image->path) ||
783                                         (my_profile->image_thumbnail_path && NULL == image->path) ||
784                                         (my_profile->image_thumbnail_path && image->path && STRING_EQUAL != strcmp(my_profile->image_thumbnail_path, image->path))) {
785                                 ctsvc_record_set_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.image_thumbnail_path, CTSVC_PROPERTY_FLAG_DIRTY);
786
787                                 if (ctsvc_contact_check_image_location(image->path))
788                                         my_profile->image_thumbnail_path = SAFE_STRDUP(image->path + strlen(CTSVC_CONTACT_IMG_FULL_LOCATION) + 1);
789                                 else
790                                         my_profile->image_thumbnail_path = SAFE_STRDUP(image->path);
791                         }
792                 } else if (my_profile->image_thumbnail_path) {
793                         free(my_profile->image_thumbnail_path);
794                         my_profile->image_thumbnail_path = NULL;
795                         ctsvc_record_set_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.image_thumbnail_path, CTSVC_PROPERTY_FLAG_DIRTY);
796                 }
797         }
798
799         do {
800                 int len = 0;
801                 int version;
802                 char query[CTS_SQL_MAX_LEN] = {0};
803                 char query_set[CTS_SQL_MIN_LEN] = {0, };
804                 cts_stmt stmt = NULL;
805
806                 version = ctsvc_get_next_ver();
807
808                 ret = ctsvc_db_create_set_query(record, &set, &bind_text);
809                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_db_create_set_query() Fail(%d)", ret);
810
811                 if (set && *set)
812                         len = snprintf(query_set, sizeof(query_set), "%s, ", set);
813                 len += snprintf(query_set+len, sizeof(query_set)-len, " changed_ver=%d, changed_time=%d", version, (int)time(NULL));
814
815                 if (ctsvc_record_check_property_flag((ctsvc_record_s*)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY)) {
816                         len += snprintf(query_set+len, sizeof(query_set)-len,
817                                         ", display_name=?, reverse_display_name=?");
818                         bind_text = g_slist_append(bind_text, strdup(SAFE_STR(my_profile->display_name)));
819                         bind_text = g_slist_append(bind_text, strdup(SAFE_STR(my_profile->reverse_display_name)));
820                 }
821
822                 snprintf(query, sizeof(query), "UPDATE %s SET %s WHERE my_profile_id = %d", CTS_TABLE_MY_PROFILES, query_set, my_profile->id);
823
824                 ret = ctsvc_query_prepare(query, &stmt);
825                 if (NULL == stmt) {
826                         /* LCOV_EXCL_START */
827                         ERR("ctsvc_query_prepare() Fail(%d)", ret);
828                         break;
829                         /* LCOV_EXCL_STOP */
830                 }
831
832                 if (bind_text) {
833                         int i = 0;
834                         for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++) {
835                                 const char *text = cursor->data;
836                                 if (*text)
837                                         ctsvc_stmt_bind_text(stmt, i, text);
838                         }
839                 }
840
841                 ret = ctsvc_stmt_step(stmt);
842                 if (CONTACTS_ERROR_NONE != ret) {
843                         /* LCOV_EXCL_START */
844                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
845                         ctsvc_stmt_finalize(stmt);
846                         break;
847                         /* LCOV_EXCL_STOP */
848                 }
849                 ctsvc_stmt_finalize(stmt);
850         } while (0);
851
852         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
853         free(set);
854
855         if (bind_text) {
856                 for (cursor = bind_text; cursor; cursor = cursor->next) {
857                         free(cursor->data);
858                         cursor->data = NULL;
859                 }
860                 g_slist_free(bind_text);
861         }
862
863         ctsvc_set_my_profile_noti();
864
865         ret = ctsvc_end_trans(true);
866         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
867
868         return CONTACTS_ERROR_NONE;
869 }
870
871 static int __ctsvc_db_my_profile_get_all_records(int offset, int limit, contacts_list_h *out_list)
872 {
873         int ret;
874         int len;
875         int my_profile_id;
876         cts_stmt stmt;
877         char query[CTS_SQL_MAX_LEN] = {0};
878         contacts_list_h list;
879
880         len = snprintf(query, sizeof(query),
881                         "SELECT my_profile_id FROM "CTSVC_DB_VIEW_MY_PROFILE);
882
883         if (0 != limit) {
884                 len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
885                 if (0 < offset)
886                         len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
887         }
888
889         ret = ctsvc_query_prepare(query, &stmt);
890         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
891
892         contacts_list_create(&list);
893         while ((ret = ctsvc_stmt_step(stmt))) {
894                 contacts_record_h record;
895                 if (1 != ret) {
896                         /* LCOV_EXCL_START */
897                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
898                         ctsvc_stmt_finalize(stmt);
899                         contacts_list_destroy(list, true);
900                         return ret;
901                         /* LCOV_EXCL_STOP */
902                 }
903                 my_profile_id = ctsvc_stmt_get_int(stmt, 0);
904                 ret = ctsvc_db_get_record(_contacts_my_profile._uri, my_profile_id, &record);
905                 if (CONTACTS_ERROR_NONE != ret) {
906                         /* LCOV_EXCL_START */
907                         ERR("contacts_db_get_record() Fail(%d)", ret);
908                         ctsvc_stmt_finalize(stmt);
909                         contacts_list_destroy(list, true);
910                         return ret;
911                         /* LCOV_EXCL_STOP */
912                 }
913                 ctsvc_list_prepend(list, record);
914         }
915         ctsvc_stmt_finalize(stmt);
916         ctsvc_list_reverse(list);
917
918         *out_list = (contacts_list_h)list;
919         return CONTACTS_ERROR_NONE;
920 }
921
922 static int __ctsvc_db_my_profile_get_records_with_query(contacts_query_h query, int offset, int limit, contacts_list_h *out_list)
923 {
924         int ret;
925         int i;
926         int field_count;
927         ctsvc_query_s *s_query;
928         cts_stmt stmt;
929         contacts_list_h list;
930         ctsvc_my_profile_s *my_profile;
931         char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
932         bool had_my_profile_id = false;
933         int my_profile_id = 0;
934
935         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
936         s_query = (ctsvc_query_s*)query;
937
938         if (s_query->projection) {
939                 for (i = 0; i < s_query->projection_count; i++) {
940                         if (s_query->projection[i] == CTSVC_PROPERTY_MY_PROFILE_ID) {
941                                 had_my_profile_id = true;
942                                 break;
943                         }
944                 }
945         } else {
946                 s_query->projection_count = 0;
947                 had_my_profile_id = true;
948         }
949
950         if (false == had_my_profile_id) {
951                 s_query->projection = realloc(s_query->projection, s_query->projection_count+1);
952                 if (NULL == s_query->projection) {
953                         /* LCOV_EXCL_START */
954                         ERR("realloc() Fail");
955                         return CONTACTS_ERROR_OUT_OF_MEMORY;
956                         /* LCOV_EXCL_STOP */
957                 }
958                 s_query->projection[s_query->projection_count] = CTSVC_PROPERTY_MY_PROFILE_ID;
959                 s_query->projection_count++;
960         }
961
962         ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
963         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
964
965         contacts_list_create(&list);
966         while ((ret = ctsvc_stmt_step(stmt))) {
967                 contacts_record_h record;
968                 if (1 != ret) {
969                         /* LCOV_EXCL_START */
970                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
971                         ctsvc_stmt_finalize(stmt);
972                         contacts_list_destroy(list, true);
973                         return ret;
974                         /* LCOV_EXCL_STOP */
975                 }
976
977                 contacts_record_create(_contacts_my_profile._uri, &record);
978                 my_profile = (ctsvc_my_profile_s*)record;
979                 if (0 == s_query->projection_count) {
980                         field_count = s_query->property_count;
981                 } else {
982                         field_count = s_query->projection_count;
983                         ret = ctsvc_record_set_projection_flags(record, s_query->projection,
984                                         s_query->projection_count, s_query->property_count);
985
986                         if (CONTACTS_ERROR_NONE != ret)
987                                 ASSERT_NOT_REACHED("To set projection is Fail.\n");
988                 }
989
990                 for (i = 0; i < field_count; i++) {
991                         char *temp;
992                         int property_id;
993                         if (0 == s_query->projection_count)
994                                 property_id = s_query->properties[i].property_id;
995                         else
996                                 property_id = s_query->projection[i];
997
998                         switch (property_id) {
999                         case CTSVC_PROPERTY_MY_PROFILE_ID:
1000                                 my_profile_id = ctsvc_stmt_get_int(stmt, i);
1001                                 if (had_my_profile_id)
1002                                         my_profile->id = my_profile_id;
1003                                 break;
1004                         case CTSVC_PROPERTY_MY_PROFILE_DISPLAY_NAME:
1005                                 temp = ctsvc_stmt_get_text(stmt, i);
1006                                 free(my_profile->display_name);
1007                                 my_profile->display_name = SAFE_STRDUP(temp);
1008                                 break;
1009                         case CTSVC_PROPERTY_MY_PROFILE_ADDRESSBOOK_ID:
1010                                 my_profile->addressbook_id = ctsvc_stmt_get_int(stmt, i);
1011                                 break;
1012                         case CTSVC_PROPERTY_MY_PROFILE_IMAGE_THUMBNAIL:
1013                                 temp = ctsvc_stmt_get_text(stmt, i);
1014                                 if (temp) {
1015                                         snprintf(full_path, sizeof(full_path), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, temp);
1016                                         free(my_profile->image_thumbnail_path);
1017                                         my_profile->image_thumbnail_path = strdup(full_path);
1018                                 }
1019                                 break;
1020                         case CTSVC_PROPERTY_MY_PROFILE_CHANGED_TIME:
1021                                 my_profile->changed_time = ctsvc_stmt_get_int(stmt, i);
1022                                 break;
1023                         case CTSVC_PROPERTY_MY_PROFILE_UID:
1024                                 temp = ctsvc_stmt_get_text(stmt, i);
1025                                 free(my_profile->uid);
1026                                 my_profile->uid = SAFE_STRDUP(temp);
1027                                 break;
1028                         default:
1029                                 break;
1030                         }
1031                 }
1032                 ret = __ctsvc_db_my_profile_get_data(my_profile_id, my_profile);
1033                 if (CONTACTS_ERROR_NONE != ret) {
1034                         /* LCOV_EXCL_START */
1035                         ERR("__ctsvc_db_my_profile_get_data Fail(%d)", ret);
1036                         ctsvc_stmt_finalize(stmt);
1037                         contacts_list_destroy(list, true);
1038                         return ret;
1039                         /* LCOV_EXCL_STOP */
1040                 }
1041
1042                 ctsvc_list_prepend(list, record);
1043         }
1044         ctsvc_stmt_finalize(stmt);
1045         ctsvc_list_reverse(list);
1046
1047         *out_list = (contacts_list_h)list;
1048
1049         return CONTACTS_ERROR_NONE;
1050 }
1051
1052 static int __ctsvc_my_profile_insert_data(ctsvc_my_profile_s *contact)
1053 {
1054         int ret;
1055
1056         /* Insert the name */
1057         if (contact->name) {
1058                 ret = ctsvc_contact_insert_data_name((contacts_list_h)contact->name, contact->id, true);
1059                 if (CONTACTS_ERROR_NONE != ret) {
1060                         /* LCOV_EXCL_START */
1061                         ERR("ctsvc_contact_insert_data_name() Fail(%d)", ret);
1062                         return ret;
1063                         /* LCOV_EXCL_STOP */
1064                 }
1065         }
1066
1067         /* Insert the company */
1068         if (contact->company) {
1069                 ret = ctsvc_contact_insert_data_company((contacts_list_h)contact->company, contact->id, true);
1070                 if (CONTACTS_ERROR_NONE != ret) {
1071                         /* LCOV_EXCL_START */
1072                         ERR("ctsvc_insert_my_profile_data_company() Fail(%d)", ret);
1073                         return ret;
1074                         /* LCOV_EXCL_STOP */
1075                 }
1076         }
1077
1078         /* Insert the events */
1079         if (contact->events) {
1080                 ret = ctsvc_contact_insert_data_event((contacts_list_h)contact->events, contact->id, true);
1081                 if (CONTACTS_ERROR_NONE != ret) {
1082                         /* LCOV_EXCL_START */
1083                         ERR("ctsvc_insert_my_profile_data_event() Fail(%d)", ret);
1084                         return ret;
1085                         /* LCOV_EXCL_STOP */
1086                 }
1087         }
1088
1089         /* Insert the messengers */
1090         if (contact->messengers) {
1091                 ret = ctsvc_contact_insert_data_messenger((contacts_list_h)contact->messengers, contact->id, true);
1092                 if (CONTACTS_ERROR_NONE != ret) {
1093                         /* LCOV_EXCL_START */
1094                         ERR("ctsvc_insert_my_profile_data_messenger() Fail(%d)", ret);
1095                         return ret;
1096                         /* LCOV_EXCL_STOP */
1097                 }
1098         }
1099
1100         /* Insert the postals */
1101         if (contact->postal_addrs) {
1102                 ret = ctsvc_contact_insert_data_address((contacts_list_h)contact->postal_addrs, contact->id, true);
1103                 if (CONTACTS_ERROR_NONE != ret) {
1104                         /* LCOV_EXCL_START */
1105                         ERR("ctsvc_insert_my_profile_data_postal() Fail(%d)", ret);
1106                         return ret;
1107                         /* LCOV_EXCL_STOP */
1108                 }
1109         }
1110
1111         /* Insert the Web addrs */
1112         if (contact->urls) {
1113                 ret = ctsvc_contact_insert_data_url((contacts_list_h)contact->urls, contact->id, true);
1114                 if (CONTACTS_ERROR_NONE != ret) {
1115                         /* LCOV_EXCL_START */
1116                         ERR("ctsvc_insert_my_profile_data_web() Fail(%d)", ret);
1117                         return ret;
1118                         /* LCOV_EXCL_STOP */
1119                 }
1120         }
1121
1122         /* Insert the Nick names */
1123         if (contact->nicknames) {
1124                 ret = ctsvc_contact_insert_data_nickname((contacts_list_h)contact->nicknames, contact->id, true);
1125                 if (CONTACTS_ERROR_NONE != ret) {
1126                         /* LCOV_EXCL_START */
1127                         ERR("ctsvc_insert_my_profile_data_nickname() Fail(%d)", ret);
1128                         return ret;
1129                         /* LCOV_EXCL_STOP */
1130                 }
1131         }
1132
1133         /* Insert the numbers */
1134         if (contact->numbers) {
1135                 ret = ctsvc_contact_insert_data_number((contacts_list_h)contact->numbers, contact->id, true);
1136                 if (ret < CONTACTS_ERROR_NONE) {
1137                         /* LCOV_EXCL_START */
1138                         ERR("ctsvc_contact_insert_data_number() Fail(%d)", ret);
1139                         return ret;
1140                         /* LCOV_EXCL_STOP */
1141                 }
1142         }
1143
1144         /* Insert the emails */
1145         if (contact->emails) {
1146                 ret = ctsvc_contact_insert_data_email((contacts_list_h)contact->emails, contact->id, true);
1147                 if (ret < CONTACTS_ERROR_NONE) {
1148                         /* LCOV_EXCL_START */
1149                         ERR("ctsvc_insert_my_profile_data_email() Fail(%d)", ret);
1150                         return ret;
1151                         /* LCOV_EXCL_STOP */
1152                 }
1153         }
1154
1155         /* Insert the profile values */
1156         if (contact->profiles) {
1157                 ret = ctsvc_contact_insert_data_profile((contacts_list_h)contact->profiles, contact->id, true);
1158                 if (CONTACTS_ERROR_NONE != ret) {
1159                         /* LCOV_EXCL_START */
1160                         ERR("ctsvc_insert_my_profile_data_profile() Fail(%d)", ret);
1161                         return ret;
1162                         /* LCOV_EXCL_STOP */
1163                 }
1164         }
1165
1166         /* Insert the relationship values */
1167         if (contact->relationships) {
1168                 ret = ctsvc_contact_insert_data_relationship((contacts_list_h)contact->relationships, contact->id, true);
1169                 if (CONTACTS_ERROR_NONE != ret) {
1170                         /* LCOV_EXCL_START */
1171                         ERR("ctsvc_contact_insert_data_relationship() Fail(%d)", ret);
1172                         return ret;
1173                         /* LCOV_EXCL_STOP */
1174                 }
1175         }
1176
1177         /* Insert the image values */
1178         if (contact->images) {
1179                 ret = ctsvc_contact_insert_data_image((contacts_list_h)contact->images, contact->id, true);
1180                 if (CONTACTS_ERROR_NONE != ret) {
1181                         /* LCOV_EXCL_START */
1182                         ERR("ctsvc_contact_insert_data_image() Fail(%d)", ret);
1183                         return ret;
1184                         /* LCOV_EXCL_STOP */
1185                 }
1186         }
1187
1188         /* Insert the note values */
1189         if (contact->note) {
1190                 ret = ctsvc_contact_insert_data_note((contacts_list_h)contact->note, contact->id, true);
1191                 if (CONTACTS_ERROR_NONE != ret) {
1192                         /* LCOV_EXCL_START */
1193                         ERR("ctsvc_contact_insert_data_note() Fail(%d)", ret);
1194                         return ret;
1195                         /* LCOV_EXCL_STOP */
1196                 }
1197         }
1198
1199         /* Insert the extensions values */
1200         if (contact->extensions) {
1201                 ret = ctsvc_contact_insert_data_extension((contacts_list_h)contact->extensions, contact->id, true);
1202                 if (CONTACTS_ERROR_NONE != ret) {
1203                         /* LCOV_EXCL_START */
1204                         ERR("ctsvc_contact_insert_data_extension() Fail(%d)", ret);
1205                         return ret;
1206                         /* LCOV_EXCL_STOP */
1207                 }
1208         }
1209
1210         return CONTACTS_ERROR_NONE;
1211 }
1212
1213 static int __ctsvc_db_my_profile_insert_record(contacts_record_h record, int *id)
1214 {
1215         CTS_FN_CALL;
1216         int ret;
1217         int version;
1218         char query[CTS_SQL_MAX_LEN] = {0};
1219
1220         ctsvc_my_profile_s *my_profile = (ctsvc_my_profile_s*)record;
1221         cts_stmt stmt;
1222
1223         /* These check should be done in client side */
1224         RETV_IF(NULL == my_profile, CONTACTS_ERROR_INVALID_PARAMETER);
1225         RETVM_IF(my_profile->addressbook_id < 0, CONTACTS_ERROR_INVALID_PARAMETER,
1226                         "addressbook_id(%d) is mandatory field to insert my_profile record ", my_profile->addressbook_id);
1227         RETVM_IF(0 < my_profile->id, CONTACTS_ERROR_INVALID_PARAMETER,
1228                         "id(%d), This record is already inserted", my_profile->id);
1229
1230         ret = ctsvc_begin_trans();
1231         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_begin_trans() Fail(%d)", ret);
1232
1233         if (false == ctsvc_have_ab_write_permission(my_profile->addressbook_id, false)) {
1234                 /* LCOV_EXCL_START */
1235                 ERR("No permission in this addresbook_id(%d)", my_profile->addressbook_id);
1236                 ctsvc_end_trans(false);
1237                 return CONTACTS_ERROR_PERMISSION_DENIED;
1238                 /* LCOV_EXCL_STOP */
1239         }
1240
1241         snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_MY_PROFILES" WHERE addressbook_id = %d AND deleted = 1", my_profile->addressbook_id);
1242         ret = ctsvc_query_exec(query);
1243         WARN_IF(CONTACTS_ERROR_NONE != ret, "Delete deleted my_profile of addressbook(%d) Fail", my_profile->addressbook_id);
1244
1245         ret = ctsvc_db_get_next_id(CTS_TABLE_MY_PROFILES);
1246         if (ret < CONTACTS_ERROR_NONE) {
1247                 /* LCOV_EXCL_START */
1248                 ERR("ctsvc_db_get_next_id() Fail(%d)", ret);
1249                 ctsvc_end_trans(false);
1250                 return ret;
1251                 /* LCOV_EXCL_STOP */
1252         }
1253         my_profile->id = ret;
1254         if (id)
1255                 *id = ret;
1256
1257         __ctsvc_make_my_profile_display_name(my_profile);
1258         __ctsvc_my_profile_check_default_data(my_profile);
1259
1260         /* Insert Data */
1261         ret = __ctsvc_my_profile_insert_data(my_profile);
1262         if (CONTACTS_ERROR_NONE != ret) {
1263                 /* LCOV_EXCL_START */
1264                 ERR("cts_insert_my_profile_data() Fail(%d)", ret);
1265                 ctsvc_end_trans(false);
1266                 return ret;
1267                 /* LCOV_EXCL_STOP */
1268         }
1269
1270         /* thumbnail */
1271         free(my_profile->image_thumbnail_path);
1272         my_profile->image_thumbnail_path = NULL;
1273         if (my_profile->images) {
1274                 int count = 0;
1275                 ctsvc_image_s *image;
1276
1277                 contacts_list_get_count((contacts_list_h)my_profile->images, &count);
1278                 if (count) {
1279                         contacts_list_first((contacts_list_h)my_profile->images);
1280                         do {
1281                                 ret = contacts_list_get_current_record_p((contacts_list_h)my_profile->images, (contacts_record_h*)&image);
1282                                 if (CONTACTS_ERROR_NONE != ret) {
1283                                         /* LCOV_EXCL_START */
1284                                         ERR("contacts_list_get_current_record_p() Fail(%d)", ret);
1285                                         ctsvc_end_trans(false);
1286                                         return CONTACTS_ERROR_DB;
1287                                         /* LCOV_EXCL_STOP */
1288                                 }
1289
1290                                 if (image->path && image->is_default) {
1291                                         my_profile->image_thumbnail_path = strdup(image->path);
1292                                         break;
1293                                 }
1294                         } while (CONTACTS_ERROR_NONE == contacts_list_next((contacts_list_h)my_profile->images));
1295                 }
1296         }
1297
1298         version = ctsvc_get_next_ver();
1299
1300         snprintf(query, sizeof(query),
1301                         "INSERT INTO "CTS_TABLE_MY_PROFILES"(my_profile_id, addressbook_id, "
1302                         "created_ver, changed_ver, changed_time, "
1303                         "display_name, reverse_display_name, uid, image_thumbnail_path) "
1304                         "VALUES(%d, %d, %d, %d, %d, ?, ?, ?, ?)",
1305                         my_profile->id, my_profile->addressbook_id, version, version, (int)time(NULL));
1306
1307         ret = ctsvc_query_prepare(query, &stmt);
1308         if (NULL == stmt) {
1309                 /* LCOV_EXCL_START */
1310                 ERR("ctsvc_query_prepare() Fail(%d)", ret);
1311                 ctsvc_end_trans(false);
1312                 return ret;
1313                 /* LCOV_EXCL_STOP */
1314         }
1315
1316         if (my_profile->display_name)
1317                 ctsvc_stmt_bind_text(stmt, 1, my_profile->display_name);
1318         if (my_profile->reverse_display_name)
1319                 ctsvc_stmt_bind_text(stmt, 2, my_profile->reverse_display_name);
1320         if (my_profile->uid)
1321                 ctsvc_stmt_bind_text(stmt, 3, my_profile->uid);
1322         if (my_profile->image_thumbnail_path)
1323                 ctsvc_stmt_bind_text(stmt, 4, my_profile->image_thumbnail_path);
1324
1325         ret = ctsvc_stmt_step(stmt);
1326         if (CONTACTS_ERROR_NONE != ret) {
1327                 /* LCOV_EXCL_START */
1328                 ERR("ctsvc_stmt_step() Fail(%d)", ret);
1329                 ctsvc_stmt_finalize(stmt);
1330                 ctsvc_end_trans(false);
1331                 return ret;
1332                 /* LCOV_EXCL_STOP */
1333         }
1334         ctsvc_stmt_finalize(stmt);
1335
1336         ctsvc_set_my_profile_noti();
1337
1338         ret = ctsvc_end_trans(true);
1339         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "contacts_svc_end_trans() Fail(%d)", ret);
1340
1341         return CONTACTS_ERROR_NONE;
1342 }
1343
1344 ctsvc_db_plugin_info_s ctsvc_db_plugin_my_profile = {
1345         .is_query_only = false,
1346         .insert_record = __ctsvc_db_my_profile_insert_record,
1347         .get_record = __ctsvc_db_my_profile_get_record,
1348         .update_record = __ctsvc_db_my_profile_update_record,
1349         .delete_record = __ctsvc_db_my_profile_delete_record,
1350         .get_all_records = __ctsvc_db_my_profile_get_all_records,
1351         .get_records_with_query = __ctsvc_db_my_profile_get_records_with_query,
1352         .insert_records = NULL,
1353         .update_records = NULL,
1354         .delete_records = NULL,
1355         .get_count = NULL,
1356         .get_count_with_query = NULL,
1357         .replace_record = NULL,
1358         .replace_records = NULL,
1359 };
1360