59888cf79af5d2bd449c4de54da6398f09233059
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_my_profile.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2012 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 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);
49
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);
52
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,
64         .get_count = NULL,
65         .get_count_with_query = NULL,
66         .replace_record = NULL,
67         .replace_records = NULL,
68 };
69
70 static int __ctsvc_db_get_my_profile_base_info(int id, ctsvc_my_profile_s *my_profile)
71 {
72         int ret;
73         int i;
74         cts_stmt stmt = NULL;
75         char query[CTS_SQL_MAX_LEN] = {0};
76         char *temp;
77         char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
78
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);
83
84         ret = ctsvc_query_prepare(query, &stmt);
85         RETVM_IF(NULL == stmt, ret, "DB error : ctsvc_query_prepare() Fail(%d)", ret);
86
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;
93                 else
94                         return ret;
95         }
96
97         i = 0;
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++);
104         if (temp) {
105                 snprintf(full_path, sizeof(full_path), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, temp);
106                 my_profile->image_thumbnail_path = strdup(full_path);
107         }
108         temp = ctsvc_stmt_get_text(stmt, i++);
109         my_profile->uid = SAFE_STRDUP(temp);
110         ctsvc_stmt_finalize(stmt);
111
112         return CONTACTS_ERROR_NONE;
113 }
114
115 static int __ctsvc_db_my_profile_get_data(int id, ctsvc_my_profile_s *my_profile)
116 {
117         int ret;
118         int datatype;
119         cts_stmt stmt = NULL;
120         char query[CTS_SQL_MAX_LEN] = {0};
121
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);
126
127         ret = ctsvc_query_prepare(query, &stmt);
128         RETVM_IF(NULL == stmt, ret, "DB error : ctsvc_query_prepare() Fail(%d)", ret);
129
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);
134                 return ret;
135         }
136
137         do {
138                 datatype = ctsvc_stmt_get_int(stmt, 0);
139                 switch (datatype) {
140                 case CTSVC_DATA_NAME:
141                         ctsvc_get_data_info_name(stmt, (contacts_list_h)my_profile->name);
142                         break;
143                 case CTSVC_DATA_EVENT:
144                         ctsvc_get_data_info_event(stmt, (contacts_list_h)my_profile->events);
145                         break;
146                 case CTSVC_DATA_MESSENGER:
147                         ctsvc_get_data_info_messenger(stmt, (contacts_list_h)my_profile->messengers);
148                         break;
149                 case CTSVC_DATA_POSTAL:
150                         ctsvc_get_data_info_address(stmt, (contacts_list_h)my_profile->postal_addrs);
151                         break;
152                 case CTSVC_DATA_URL:
153                         ctsvc_get_data_info_url(stmt, (contacts_list_h)my_profile->urls);
154                         break;
155                 case CTSVC_DATA_NICKNAME:
156                         ctsvc_get_data_info_nickname(stmt, (contacts_list_h)my_profile->nicknames);
157                         break;
158                 case CTSVC_DATA_NUMBER:
159                         ctsvc_get_data_info_number(stmt, (contacts_list_h)my_profile->numbers);
160                         break;
161                 case CTSVC_DATA_EMAIL:
162                         ctsvc_get_data_info_email(stmt, (contacts_list_h)my_profile->emails);
163                         break;
164                 case CTSVC_DATA_PROFILE:
165                         ctsvc_get_data_info_profile(stmt, (contacts_list_h)my_profile->profiles);
166                         break;
167                 case CTSVC_DATA_RELATIONSHIP:
168                         ctsvc_get_data_info_relationship(stmt, (contacts_list_h)my_profile->relationships);
169                         break;
170                 case CTSVC_DATA_IMAGE:
171                         ctsvc_get_data_info_image(stmt, (contacts_list_h)my_profile->images);
172                         break;
173                 case CTSVC_DATA_COMPANY:
174                         ctsvc_get_data_info_company(stmt, (contacts_list_h)my_profile->company);
175                         break;
176                 case CTSVC_DATA_NOTE:
177                         ctsvc_get_data_info_note(stmt, (contacts_list_h)my_profile->note);
178                         break;
179                 case CTSVC_DATA_EXTENSION:
180                         ctsvc_get_data_info_extension(stmt, (contacts_list_h)my_profile->extensions);
181                         break;
182                 default:
183                         CTS_ERR("Intenal : Not supported data type (%d)", datatype);
184                         break;
185                 }
186
187         } while (1 /*CTS_TRUE*/ == ctsvc_stmt_step(stmt));
188
189         ctsvc_stmt_finalize(stmt);
190
191         return CONTACTS_ERROR_NONE;
192
193 }
194
195 static int __ctsvc_db_my_profile_get_record(int id, contacts_record_h* out_record)
196 {
197         int ret;
198         contacts_record_h record;
199         ctsvc_my_profile_s *my_profile;
200
201         RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
202         *out_record = NULL;
203
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);
210                 return ret;
211         }
212
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);
217                 return ret;
218         }
219
220         *out_record = record;
221
222         return CONTACTS_ERROR_NONE;
223 }
224
225 static int __ctsvc_db_my_profile_delete_record(int id)
226 {
227         CTS_FN_CALL;
228         int ret;
229         int addressbook_id;
230         char query[CTS_SQL_MAX_LEN] = {0};
231
232         ret = ctsvc_begin_trans();
233         RETVM_IF(ret, ret, "DB error : ctsvc_begin_trans() Fail(%d)", ret);
234
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);
241                 return ret;
242         }
243
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;
248         }
249
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);
256                 return ret;
257         }
258
259         ctsvc_set_my_profile_noti();
260
261         ret = ctsvc_end_trans(true);
262         if (ret < CONTACTS_ERROR_NONE) {
263                 CTS_ERR("DB error : ctsvc_end_trans() Fail(%d)", ret);
264                 return ret;
265         }
266         else
267                 return CONTACTS_ERROR_NONE;
268 }
269
270 static inline int __ctsvc_my_profile_update_data(ctsvc_my_profile_s *my_profile)
271 {
272         int ret;
273
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);
278                         return ret;
279                 }
280         }
281
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);
286                         return ret;
287                 }
288         }
289
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);
294                         return ret;
295                 }
296         }
297
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);
302                         return ret;
303                 }
304         }
305
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);
310                         return ret;
311                 }
312         }
313
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);
318                         return ret;
319                 }
320         }
321
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);
326                         return ret;
327                 }
328         }
329
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);
334                         return ret;
335                 }
336         }
337
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);
343                         return ret;
344                 }
345         }
346
347         if (my_profile->emails) {
348                 bool had_email;
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);
352                         return ret;
353                 }
354         }
355
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);
360                         return ret;
361                 }
362         }
363
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);
368                         return ret;
369                 }
370         }
371
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);
376                         return ret;
377                 }
378         }
379
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);
384                         return ret;
385                 }
386         }
387
388         return CONTACTS_ERROR_NONE;
389 }
390
391 static void __ctsvc_my_profile_check_default_data(ctsvc_my_profile_s *my_profile)
392 {
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);
397 }
398
399 static void __ctsvc_make_my_profile_display_name(ctsvc_my_profile_s *my_profile)
400 {
401         ctsvc_name_s *name = NULL;
402
403         free(my_profile->display_name);
404         my_profile->display_name = NULL;
405
406         free(my_profile->reverse_display_name);
407         my_profile->reverse_display_name = NULL;
408
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;
411         }
412
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;
419
420                 /*
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
427                  */
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");
437                                 return;
438                         }
439                         len=0;
440
441                         if (name->last) {
442                                 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->last);
443
444                                 if (reverse_lang_type < 0) {
445                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
446                                 }
447
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, ",");
453                                 }
454                         }
455
456                         if (reverse_lang_type < 0) {
457                                 if (*temp_display) {
458                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
459                                 }
460                                 else if (name->first) {
461                                         reverse_lang_type = ctsvc_check_language_type(name->first);
462                                 }
463                                 else if (name->addition) {
464                                         reverse_lang_type = ctsvc_check_language_type(name->addition);
465                                 }
466                         }
467
468                         if (reverse_lang_type == CTSVC_LANG_JAPANESE) {
469                                 /* make temp_display name Prefix - Last - Middle - First - Suffix */
470                                 if (name->addition) {
471                                         if (*temp_display)
472                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
473                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
474                                 }
475
476                                 if (name->first) {
477                                         if (*temp_display)
478                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
479                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
480                                 }
481                         }
482                         else {
483                                 if (name->first) {
484                                         if (*temp_display) {
485                                                 if (reverse_lang_type < 0) {
486                                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
487                                                 }
488
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, " ");
492                                         }
493                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
494                                 }
495
496                                 if (name->addition) {
497                                         if (*temp_display) {
498                                                 if (reverse_lang_type < 0) {
499                                                         reverse_lang_type = ctsvc_check_language_type(temp_display);
500                                                 }
501
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, " ");
505                                         }
506                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
507                                 }
508                         }
509
510                         if (name->suffix) {
511                                 if (*temp_display) {
512                                         if (reverse_lang_type < 0) {
513                                                 reverse_lang_type = ctsvc_check_language_type(temp_display);
514                                         }
515
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, ", ");
521                                 }
522                                 len += snprintf(temp_display + len, temp_display_len - len, "%s", name->suffix);
523                         }
524                 }
525
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");
531                                 free(temp_display);
532                                 return;
533                         }
534                         snprintf(display, display_len, "%s %s", name->prefix, temp_display);
535                         my_profile->reverse_display_name = display;
536                         free(temp_display);
537                 }
538                 else if (temp_display) {
539                         my_profile->reverse_display_name = temp_display;
540                 }
541                 else if (name->prefix) {
542                         my_profile->reverse_display_name = strdup(name->prefix);
543                 }
544
545                 /*
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
552                  */
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);
556                 else {
557                         int lang_type = -1;
558                         temp_display = NULL;
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");
569                                         return;
570                                 }
571                                 len = 0;
572
573                                 if (name->first) {
574                                         if (*temp_display)
575                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
576                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->first);
577                                 }
578
579                                 if (name->addition) {
580                                         if (*temp_display)
581                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
582                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->addition);
583                                 }
584
585                                 if (name->last) {
586                                         if (*temp_display)
587                                                 len += snprintf(temp_display + len, temp_display_len - len, " ");
588                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->last);
589                                 }
590
591                                 if (name->suffix) {
592                                         if (*temp_display) {
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, " ");
596                                                 else
597                                                         len += snprintf(temp_display + len, temp_display_len - len, ", ");
598                                         }
599                                         len += snprintf(temp_display + len, temp_display_len - len, "%s", name->suffix);
600                                 }
601
602                         }
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");
608                                         free(temp_display);
609                                         return;
610                                 }
611                                 snprintf(display, display_len, "%s %s", name->prefix, temp_display);
612                                 my_profile->display_name = display;
613                                 free(temp_display);
614                         }
615                         else if (temp_display) {
616                                 my_profile->display_name = temp_display;
617                         }
618                         else if (name->prefix) {
619                                 my_profile->display_name = strdup(name->prefix);
620                         }
621                 }
622
623                 ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
624         }
625         else {
626                 GList *cur;
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);
633                                         break;
634                                 }
635                         }
636                 }
637
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);
646                                         break;
647                                 }
648                         }
649                 }
650
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);
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->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);
672                                         break;
673                                 }
674                         }
675                 }
676
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);
679                 }
680                 else {
681                         /* Update as NULL */
682                         ctsvc_record_set_property_flag((ctsvc_record_s *)my_profile, _contacts_my_profile.display_name, CTSVC_PROPERTY_FLAG_DIRTY);
683                 }
684         }
685         return;
686 }
687
688
689 static int __ctsvc_db_my_profile_update_record(contacts_record_h record)
690 {
691         int id;
692         int ret;
693         char *set = NULL;
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;
698
699         ret = ctsvc_begin_trans();
700         RETVM_IF(ret, ret, "ctsvc_begin_trans() Fail(%d)", ret);
701
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);
708                 return ret;
709         }
710
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;
716         }
717
718         __ctsvc_make_my_profile_display_name(my_profile);
719         __ctsvc_my_profile_check_default_data(my_profile);
720
721         /* update data */
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);
726                 return ret;
727         }
728
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;
734                 int count = 0;
735                 ctsvc_image_s *image;
736
737                 contacts_list_get_count((contacts_list_h)my_profile->images, &count);
738                 if (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);
741
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;
746                         }
747
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);
753
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);
756                                 else
757                                         my_profile->image_thumbnail_path = SAFE_STRDUP(image->path);
758                         }
759                 }
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);
764                 }
765         }
766         /* this code will be removed. */
767         /******************************/
768
769         do {
770                 int len = 0;
771                 int version;
772                 char query[CTS_SQL_MAX_LEN] = {0};
773                 char query_set[CTS_SQL_MIN_LEN] = {0, };
774                 cts_stmt stmt = NULL;
775
776                 version = ctsvc_get_next_ver();
777
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);
780
781                 if (set && *set)
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));
784
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)));
790                 }
791
792                 snprintf(query, sizeof(query), "UPDATE %s SET %s WHERE my_profile_id = %d", CTS_TABLE_MY_PROFILES, query_set, my_profile->id);
793
794                 ret = ctsvc_query_prepare(query, &stmt);
795                 if (NULL == stmt) {
796                         CTS_ERR("DB error : ctsvc_query_prepare() Fail(%d)", ret);
797                         break;
798                 }
799
800                 if (bind_text) {
801                         int i = 0;
802                         for (cursor=bind_text,i=1;cursor;cursor=cursor->next,i++) {
803                                 const char *text = cursor->data;
804                                 if (*text)
805                                         ctsvc_stmt_bind_text(stmt, i, text);
806                         }
807                 }
808
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);
813                         break;
814                 }
815                 ctsvc_stmt_finalize(stmt);
816         } while (0);
817
818         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s *)record);
819         CONTACTS_FREE(set);
820         if (bind_text) {
821                 for (cursor=bind_text;cursor;cursor=cursor->next)
822                         CONTACTS_FREE(cursor->data);
823                 g_slist_free(bind_text);
824         }
825
826         ctsvc_set_my_profile_noti();
827
828         ret = ctsvc_end_trans(true);
829         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "DB error : ctsvc_end_trans() Fail(%d)", ret);
830
831         return CONTACTS_ERROR_NONE;
832 }
833
834 static int __ctsvc_db_my_profile_get_all_records(int offset, int limit, contacts_list_h* out_list)
835 {
836         int ret;
837         int len;
838         int my_profile_id;
839         cts_stmt stmt;
840         char query[CTS_SQL_MAX_LEN] = {0};
841         contacts_list_h list;
842
843         len = snprintf(query, sizeof(query),
844                         "SELECT my_profile_id FROM "CTSVC_DB_VIEW_MY_PROFILE);
845
846         if (0 != limit) {
847                 len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
848                 if (0 < offset)
849                         len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
850         }
851
852         ret = ctsvc_query_prepare(query, &stmt);
853         RETVM_IF(NULL == stmt, ret, "DB error : ctsvc_query_prepare() Fail(%d)", ret);
854
855         contacts_list_create(&list);
856         while ((ret = ctsvc_stmt_step(stmt))) {
857                 contacts_record_h record;
858                 if (1 != ret) {
859                         CTS_ERR("DB error : ctsvc_stmt_step() Fail(%d)", ret);
860                         ctsvc_stmt_finalize(stmt);
861                         contacts_list_destroy(list, true);
862                         return ret;
863                 }
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);
870                         return ret;
871                 }
872                 ctsvc_list_prepend(list, record);
873         }
874         ctsvc_stmt_finalize(stmt);
875         ctsvc_list_reverse(list);
876
877         *out_list = (contacts_list_h)list;
878         return CONTACTS_ERROR_NONE;
879 }
880
881 static int __ctsvc_db_my_profile_get_records_with_query(contacts_query_h query, int offset, int limit, contacts_list_h* out_list)
882 {
883         int ret;
884         int i;
885         int field_count;
886         ctsvc_query_s *s_query;
887         cts_stmt stmt;
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;
893
894         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
895         s_query = (ctsvc_query_s *)query;
896
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;
901                                 break;
902                         }
903                 }
904         }
905         else {
906                 s_query->projection_count = 0;
907                 had_my_profile_id = true;
908         }
909
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;
915                 }
916                 s_query->projection[s_query->projection_count] = CTSVC_PROPERTY_MY_PROFILE_ID;
917                 s_query->projection_count++;
918         }
919
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);
922
923         contacts_list_create(&list);
924         while ((ret = ctsvc_stmt_step(stmt))) {
925                 contacts_record_h record;
926                 if (1 != ret) {
927                         CTS_ERR("DB error : ctsvc_stmt_step() Fail(%d)", ret);
928                         ctsvc_stmt_finalize(stmt);
929                         contacts_list_destroy(list, true);
930                         return ret;
931                 }
932
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;
937                 else {
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);
941
942                         if (CONTACTS_ERROR_NONE != ret)
943                                 ASSERT_NOT_REACHED("To set projection is Fail.\n");
944                 }
945
946                 for (i=0;i<field_count;i++) {
947                         char *temp;
948                         int property_id;
949                         if (0 == s_query->projection_count)
950                                 property_id = s_query->properties[i].property_id;
951                         else
952                                 property_id = s_query->projection[i];
953
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;
959                                 break;
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);
964                                 break;
965                         case CTSVC_PROPERTY_MY_PROFILE_ADDRESSBOOK_ID:
966                                 my_profile->addressbook_id = ctsvc_stmt_get_int(stmt, i);
967                                 break;
968                         case CTSVC_PROPERTY_MY_PROFILE_IMAGE_THUMBNAIL:
969                                 temp = ctsvc_stmt_get_text(stmt, i);
970                                 if (temp) {
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);
974                                 }
975                                 break;
976                         case CTSVC_PROPERTY_MY_PROFILE_CHANGED_TIME:
977                                 my_profile->changed_time = ctsvc_stmt_get_int(stmt, i);
978                                 break;
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);
983                                 break;
984                         default:
985                                 break;
986                         }
987                 }
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);
993                         return ret;
994                 }
995
996                 ctsvc_list_prepend(list, record);
997         }
998         ctsvc_stmt_finalize(stmt);
999         ctsvc_list_reverse(list);
1000
1001         *out_list = (contacts_list_h)list;
1002
1003         return CONTACTS_ERROR_NONE;
1004 }
1005
1006 static int __ctsvc_my_profile_insert_data(ctsvc_my_profile_s *contact)
1007 {
1008         int ret;
1009
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);
1015                         return ret;
1016                 }
1017         }
1018
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);
1024                         return ret;
1025                 }
1026         }
1027
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);
1033                         return ret;
1034                 }
1035         }
1036
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);
1042                         return ret;
1043                 }
1044         }
1045
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);
1051                         return ret;
1052                 }
1053         }
1054
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);
1060                         return ret;
1061                 }
1062         }
1063
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);
1069                         return ret;
1070                 }
1071         }
1072
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);
1078                         return ret;
1079                 }
1080         }
1081
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);
1087                         return ret;
1088                 }
1089         }
1090
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);
1096                         return ret;
1097                 }
1098         }
1099
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);
1105                         return ret;
1106                 }
1107         }
1108
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);
1114                         return ret;
1115                 }
1116         }
1117
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);
1123                         return ret;
1124                 }
1125         }
1126
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);
1132                         return ret;
1133                 }
1134         }
1135
1136         return CONTACTS_ERROR_NONE;
1137 }
1138
1139 static int __ctsvc_db_my_profile_insert_record(contacts_record_h record, int *id)
1140 {
1141         CTS_FN_CALL;
1142         int ret;
1143         int version;
1144         char query[CTS_SQL_MAX_LEN] = {0};
1145
1146         ctsvc_my_profile_s *my_profile = (ctsvc_my_profile_s*)record;
1147         cts_stmt stmt;
1148
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);
1156
1157         ret = ctsvc_begin_trans();
1158         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_begin_trans() Fail(%d)", ret);
1159
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;
1165         }
1166
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);
1170
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);
1175                 return ret;
1176         }
1177         my_profile->id = ret;
1178         if (id)
1179                 *id = ret;
1180
1181         __ctsvc_make_my_profile_display_name(my_profile);
1182         __ctsvc_my_profile_check_default_data(my_profile);
1183
1184         /* Insert Data */
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);
1189                 return ret;
1190         }
1191
1192         /******************************/
1193         /* this code will be removed. */
1194         free(my_profile->image_thumbnail_path);
1195         my_profile->image_thumbnail_path = NULL;
1196
1197         if (my_profile->images) {
1198                 ctsvc_image_s *image;
1199                 int count = 0;
1200
1201                 contacts_list_get_count((contacts_list_h)my_profile->images, &count);
1202
1203                 while (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;
1210                         }
1211
1212                         if (image->path && image->is_default) {
1213                                 my_profile->image_thumbnail_path = strdup(image->path);
1214                                 break;
1215                         }
1216                         count--;
1217                 }
1218         }
1219         /* this code will be removed. */
1220         /******************************/
1221
1222         version = ctsvc_get_next_ver();
1223
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));
1230
1231         ret = ctsvc_query_prepare(query, &stmt);
1232         if (NULL == stmt) {
1233                 CTS_ERR("ctsvc_query_prepare() Fail(%d)", ret);
1234                 ctsvc_end_trans(false);
1235                 return ret;
1236         }
1237
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);
1246
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);
1252                 return ret;
1253         }
1254         ctsvc_stmt_finalize(stmt);
1255
1256         ctsvc_set_my_profile_noti();
1257
1258         ret = ctsvc_end_trans(true);
1259         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "contacts_svc_end_trans() Fail(%d)", ret);
1260
1261         return CONTACTS_ERROR_NONE;
1262 }
1263