[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_sip_helper.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
20 #include "contacts.h"
21 #include "ctsvc_internal.h"
22 #include "ctsvc_db_schema.h"
23 #include "ctsvc_db_sqlite.h"
24 #include "ctsvc_db_init.h"
25 #include "ctsvc_db_query.h"
26 #include "ctsvc_db_plugin_sip_helper.h"
27 #include "ctsvc_record.h"
28 #include "ctsvc_notification.h"
29
30 /* in sip
31  * data1 : type
32  * data2 : label
33  * data3 : address
34  */
35 int ctsvc_db_sip_insert(contacts_record_h record, int contact_id, bool is_my_profile, int *id)
36 {
37         int ret;
38         cts_stmt stmt = NULL;
39         char query[CTS_SQL_MAX_LEN] = {0};
40         ctsvc_sip_s *sip = (ctsvc_sip_s *)record;
41
42         /* These check should be done in client side */
43         RETVM_IF(contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,
44                                 "Invalid parameter : contact_id(%d) is mandatory field to insert sip record", sip->contact_id);
45         RETVM_IF(0 < sip->id, CONTACTS_ERROR_INVALID_PARAMETER,
46                                 "Invalid parameter : id(%d), This record is already inserted", sip->id);
47
48         snprintf(query, sizeof(query),
49                 "INSERT INTO "CTS_TABLE_DATA"(contact_id, is_my_profile, datatype, data1, data2, data3) "
50                                                                         "VALUES(%d, %d, %d, %d, ?, ?)",
51                         contact_id, is_my_profile, CONTACTS_DATA_TYPE_SIP, sip->type);
52         ret = ctsvc_query_prepare(query, &stmt);
53         RETVM_IF(NULL == stmt, ret, "DB error : ctsvc_query_prepare() Fail(%d)", ret);
54
55         if (sip->label)
56                 ctsvc_stmt_bind_text(stmt, 1, sip->label);
57         if (sip->address)
58                 ctsvc_stmt_bind_text(stmt, 2, sip->address);
59
60         ret = ctsvc_stmt_step(stmt);
61         if (CONTACTS_ERROR_NONE != ret) {
62                 /* LCOV_EXCL_START */
63                 ERR("DB error : ctsvc_stmt_step() Fail(%d)", ret);
64                 ctsvc_stmt_finalize(stmt);
65                 return ret;
66                 /* LCOV_EXCL_STOP */
67         }
68
69         /* sip->id = ctsvc_db_get_last_insert_id(); */
70         if (id)
71                 *id = ctsvc_db_get_last_insert_id();
72
73         ctsvc_stmt_finalize(stmt);
74
75         if (false == is_my_profile)
76                 ctsvc_set_sip_noti();
77
78         return CONTACTS_ERROR_NONE;
79 }
80
81 int ctsvc_db_sip_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record, int start_count)
82 {
83         int ret;
84         ctsvc_sip_s *sip;
85         char *temp;
86
87         ret = contacts_record_create(_contacts_sip._uri, (contacts_record_h *)&sip);
88         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create Fail(%d)", ret);
89
90         sip->id = ctsvc_stmt_get_int(stmt, start_count++);
91         sip->contact_id = ctsvc_stmt_get_int(stmt, start_count++);
92         start_count++; /* is_default */
93         sip->type = ctsvc_stmt_get_int(stmt, start_count++);
94         temp = ctsvc_stmt_get_text(stmt, start_count++);
95         sip->label = SAFE_STRDUP(temp);
96         temp = ctsvc_stmt_get_text(stmt, start_count++);
97         sip->address = SAFE_STRDUP(temp);
98
99         *record = (contacts_record_h)sip;
100
101         return CONTACTS_ERROR_NONE;
102 }
103
104 int ctsvc_db_sip_update(contacts_record_h record, bool is_my_profile)
105 {
106         int id;
107         int ret = CONTACTS_ERROR_NONE;
108         char* set = NULL;
109         GSList *bind_text = NULL;
110         GSList *cursor = NULL;
111         ctsvc_sip_s *sip = (ctsvc_sip_s*)record;
112         char query[CTS_SQL_MAX_LEN] = {0};
113
114         RETVM_IF(0 == sip->id, CONTACTS_ERROR_INVALID_PARAMETER, "sip of contact has no ID.");
115         RETVM_IF(CTSVC_PROPERTY_FLAG_DIRTY != (sip->base.property_flag & CTSVC_PROPERTY_FLAG_DIRTY), CONTACTS_ERROR_NONE, "No update");
116
117         snprintf(query, sizeof(query),
118                         "SELECT id FROM "CTS_TABLE_DATA" WHERE id = %d", sip->id);
119         ret = ctsvc_query_get_first_int_result(query, &id);
120         RETV_IF(ret != CONTACTS_ERROR_NONE, ret);
121
122         do {
123                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_create_set_query(record, &set, &bind_text))) break;
124                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_update_record_with_set_query(set, bind_text, CTS_TABLE_DATA, sip->id))) break;
125                 if (false == is_my_profile)
126                         ctsvc_set_sip_noti();
127         } while (0);
128
129         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s *)record);
130         free(set);
131         if (bind_text) {
132                 for (cursor = bind_text; cursor; cursor = cursor->next)
133                         free(cursor->data);
134                 g_slist_free(bind_text);
135         }
136
137         return ret;
138 }
139
140 int ctsvc_db_sip_delete(int id, bool is_my_profile)
141 {
142         int ret;
143         char query[CTS_SQL_MIN_LEN] = {0};
144
145         snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_DATA" WHERE id = %d AND datatype = %d",
146                         id, CONTACTS_DATA_TYPE_SIP);
147
148         ret = ctsvc_query_exec(query);
149         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
150
151         if (false == is_my_profile)
152                 ctsvc_set_sip_noti();
153
154         return CONTACTS_ERROR_NONE;
155 }
156