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