[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_company_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 <ctype.h>
20 #include <unistd.h>
21
22 #include "contacts.h"
23 #include "ctsvc_internal.h"
24 #include "ctsvc_db_schema.h"
25 #include "ctsvc_db_sqlite.h"
26 #include "ctsvc_db_utils.h"
27 #include "ctsvc_db_init.h"
28 #include "ctsvc_db_query.h"
29 #include "ctsvc_db_plugin_company_helper.h"
30 #include "ctsvc_record.h"
31 #include "ctsvc_notification.h"
32 #include "ctsvc_notify.h"
33 #include "ctsvc_db_access_control.h"
34
35 static int __ctsvc_company_bind_stmt(cts_stmt stmt, ctsvc_company_s *company, int start_cnt)
36 {
37         ctsvc_stmt_bind_int(stmt, start_cnt, company->is_default);
38         ctsvc_stmt_bind_int(stmt, start_cnt+1, company->type);
39         if (company->label)
40                 sqlite3_bind_text(stmt, start_cnt+2, company->label,
41                                 strlen(company->label), SQLITE_STATIC);
42         if (company->name)
43                 sqlite3_bind_text(stmt, start_cnt+3, company->name,
44                                 strlen(company->name), SQLITE_STATIC);
45         if (company->department)
46                 sqlite3_bind_text(stmt, start_cnt+4, company->department,
47                                 strlen(company->department), SQLITE_STATIC);
48         if (company->job_title)
49                 sqlite3_bind_text(stmt, start_cnt+5, company->job_title,
50                                 strlen(company->job_title), SQLITE_STATIC);
51         if (company->role)
52                 sqlite3_bind_text(stmt, start_cnt+6, company->role,
53                                 strlen(company->role), SQLITE_STATIC);
54         if (company->assistant_name)
55                 sqlite3_bind_text(stmt, start_cnt+7, company->assistant_name,
56                                 strlen(company->assistant_name), SQLITE_STATIC);
57
58         /* skip logo here */
59
60         if (company->location)
61                 sqlite3_bind_text(stmt, start_cnt+9, company->location,
62                                 strlen(company->location), SQLITE_STATIC);
63         if (company->description)
64                 sqlite3_bind_text(stmt, start_cnt+10, company->description,
65                                 strlen(company->description), SQLITE_STATIC);
66         if (company->phonetic_name)
67                 sqlite3_bind_text(stmt, start_cnt+11, company->phonetic_name,
68                                 strlen(company->phonetic_name), SQLITE_STATIC);
69
70         return CONTACTS_ERROR_NONE;
71 }
72
73 int ctsvc_db_company_insert(contacts_record_h record, int contact_id, bool is_my_profile, int *id)
74 {
75         int ret;
76         int company_id = 0;
77         cts_stmt stmt = NULL;
78         ctsvc_company_s *company = (ctsvc_company_s*)record;
79         char query[CTS_SQL_MAX_LEN] = {0};
80
81         RETVM_IF(contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,
82                         "contact_id(%d) is mandatory field to insert company record ", company->contact_id);
83         RETVM_IF(0 < company->id, CONTACTS_ERROR_INVALID_PARAMETER,
84                         "id(%d), This record is already inserted", company->id);
85
86         if (company->name || company->department || company->job_title || company->role
87                         || company->assistant_name || company->logo || company->location || company->description
88                         || company->phonetic_name) {
89
90                 ret = ctsvc_db_get_next_id(CTS_TABLE_DATA);
91                 if (ret < CONTACTS_ERROR_NONE) {
92                         /* LCOV_EXCL_START */
93                         ERR("ctsvc_db_get_next_id() Fail(%d)", ret);
94                         ctsvc_end_trans(false);
95                         return ret;
96                         /* LCOV_EXCL_STOP */
97                 }
98                 company_id = ret;
99
100                 snprintf(query, sizeof(query),
101                                 "INSERT INTO "CTS_TABLE_DATA"(id, contact_id, is_my_profile, datatype, is_default, data1, data2, data3, data4, "
102                                 "data5, data6, data7, data8, data9, data10, data11, data12) "
103                                 "VALUES(%d, %d, %d, %d, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
104                                 company_id, contact_id, is_my_profile, CONTACTS_DATA_TYPE_COMPANY);
105
106                 ret = ctsvc_query_prepare(query, &stmt);
107                 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
108
109                 __ctsvc_company_bind_stmt(stmt, company, 1);
110                 if (company->logo) {
111                         char image[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
112                         ret = ctsvc_have_file_read_permission(company->logo);
113                         if (ret != CONTACTS_ERROR_NONE) {
114                                 /* LCOV_EXCL_START */
115                                 ERR("ctsvc_have_file_read_permission Fail(%d)", ret);
116                                 ctsvc_stmt_finalize(stmt);
117                                 return ret;
118                                 /* LCOV_EXCL_STOP */
119                         }
120                         ctsvc_utils_make_image_file_name(contact_id, company_id, company->logo, image, sizeof(image));
121                         ret = ctsvc_utils_copy_image(CTS_LOGO_IMAGE_LOCATION, company->logo, image);
122                         if (CONTACTS_ERROR_NONE != ret) {
123                                 /* LCOV_EXCL_START */
124                                 ERR("ctsvc_utils_copy_image() Fail(%d)", ret);
125                                 ctsvc_stmt_finalize(stmt);
126                                 return ret;
127                                 /* LCOV_EXCL_STOP */
128                         }
129                         ctsvc_stmt_bind_text(stmt, 9, image);
130                 }
131
132                 ret = ctsvc_stmt_step(stmt);
133                 if (CONTACTS_ERROR_NONE != ret) {
134                         /* LCOV_EXCL_START */
135                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
136                         ctsvc_stmt_finalize(stmt);
137                         return ret;
138                         /* LCOV_EXCL_STOP */
139                 }
140                 ctsvc_stmt_finalize(stmt);
141
142                 company->id = company_id;
143                 if (id)
144                         *id = company_id;
145
146                 if (false == is_my_profile)
147                         ctsvc_set_company_noti();
148         }
149         return CONTACTS_ERROR_NONE;
150 }
151
152 int ctsvc_db_company_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record, int start_count)
153 {
154         int ret;
155         char *temp;
156         ctsvc_company_s *company;
157
158         ret = contacts_record_create(_contacts_company._uri, (contacts_record_h*)&company);
159         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create Fail(%d)", ret);
160
161         company->id = ctsvc_stmt_get_int(stmt, start_count++);
162         company->contact_id = ctsvc_stmt_get_int(stmt, start_count++);
163         company->is_default = ctsvc_stmt_get_int(stmt, start_count++);
164         company->type = ctsvc_stmt_get_int(stmt, start_count++);
165         temp = ctsvc_stmt_get_text(stmt, start_count++);
166         company->label = SAFE_STRDUP(temp);
167         temp = ctsvc_stmt_get_text(stmt, start_count++);
168         company->name = SAFE_STRDUP(temp);
169         temp = ctsvc_stmt_get_text(stmt, start_count++);
170         company->department = SAFE_STRDUP(temp);
171         temp = ctsvc_stmt_get_text(stmt, start_count++);
172         company->job_title = SAFE_STRDUP(temp);
173         temp = ctsvc_stmt_get_text(stmt, start_count++);
174         company->role = SAFE_STRDUP(temp);
175         temp = ctsvc_stmt_get_text(stmt, start_count++);
176         company->assistant_name = SAFE_STRDUP(temp);
177         temp = ctsvc_stmt_get_text(stmt, start_count++);
178         if (temp) {
179                 char tmp_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
180                 snprintf(tmp_path, sizeof(tmp_path), "%s/%s", CTS_LOGO_IMAGE_LOCATION, temp);
181                 company->logo = strdup(tmp_path);
182         }
183         temp = ctsvc_stmt_get_text(stmt, start_count++);
184         company->location = SAFE_STRDUP(temp);
185         temp = ctsvc_stmt_get_text(stmt, start_count++);
186         company->description = SAFE_STRDUP(temp);
187         temp = ctsvc_stmt_get_text(stmt, start_count++);
188         company->phonetic_name = SAFE_STRDUP(temp);
189
190         if (company->name || company->department || company->job_title || company->role
191                         || company->assistant_name || company->logo || company->location || company->description
192                         || company->phonetic_name) {
193                 *record = (contacts_record_h)company;
194         } else {
195                 contacts_record_destroy((contacts_record_h)company, true);
196                 *record = NULL;
197         }
198         return CONTACTS_ERROR_NONE;
199 }
200
201 int ctsvc_db_company_update(contacts_record_h record, int contact_id, bool is_my_profile)
202 {
203         int ret = CONTACTS_ERROR_NONE;
204         char *set = NULL;
205         GSList *bind_text = NULL;
206         GSList *cursor = NULL;
207         char query[CTS_SQL_MAX_LEN] = {0};
208         ctsvc_company_s *company = (ctsvc_company_s*)record;
209         cts_stmt stmt = NULL;
210
211         RETVM_IF(0 == company->id, CONTACTS_ERROR_INVALID_PARAMETER, "company of contact has no ID.");
212         RETVM_IF(CTSVC_PROPERTY_FLAG_DIRTY != (company->base.property_flag & CTSVC_PROPERTY_FLAG_DIRTY), CONTACTS_ERROR_NONE, "No update");
213
214         snprintf(query, sizeof(query),
215                         "SELECT id, data8 FROM "CTS_TABLE_DATA" WHERE id = %d", company->id);
216         ret = ctsvc_query_prepare(query, &stmt);
217         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
218
219         ret = ctsvc_stmt_step(stmt);
220         if (ret != 1) {
221                 ctsvc_stmt_finalize(stmt);
222                 if (ret == CONTACTS_ERROR_NONE)
223                         return CONTACTS_ERROR_NO_DATA;
224                 else
225                         return ret;
226         }
227
228         if (ctsvc_record_check_property_flag((ctsvc_record_s*)company, _contacts_company.logo, CTSVC_PROPERTY_FLAG_DIRTY)) {
229                 char *logo = ctsvc_stmt_get_text(stmt, 1);
230                 bool same = false;
231                 bool check_permission = false;
232
233                 /* delete current logo image */
234                 if (logo) {
235                         char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
236                         snprintf(full_path, sizeof(full_path), "%s/%s", CTS_LOGO_IMAGE_LOCATION, logo);
237                         if (company->logo && STRING_EQUAL == strcmp(company->logo, full_path)) {
238                                 int index = _contacts_company.logo & 0x000000FF;
239                                 ((ctsvc_record_s*)record)->properties_flags[index] = 0;
240                                 same = true;
241                         } else {
242                                 if (company->logo) {
243                                         ret = ctsvc_have_file_read_permission(company->logo);
244                                         if (ret != CONTACTS_ERROR_NONE) {
245                                                 /* LCOV_EXCL_START */
246                                                 ERR("ctsvc_have_file_read_permission Fail(%d)", ret);
247                                                 ctsvc_stmt_finalize(stmt);
248                                                 return ret;
249                                                 /* LCOV_EXCL_STOP */
250                                         }
251                                         check_permission = true;
252                                 }
253                                 ret = unlink(full_path);
254                                 if (ret < 0)
255                                         WARN("unlink() Fail(%d)", errno);
256                         }
257                 }
258
259                 /* add new logo file */
260                 if (false == same && company->logo) {
261                         char dest[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
262                         if (false == check_permission) {
263                                 ret = ctsvc_have_file_read_permission(company->logo);
264                                 if (ret != CONTACTS_ERROR_NONE) {
265                                         /* LCOV_EXCL_START */
266                                         ERR("ctsvc_have_file_read_permission Fail(%d)", ret);
267                                         ctsvc_stmt_finalize(stmt);
268                                         return ret;
269                                         /* LCOV_EXCL_STOP */
270                                 }
271                         }
272                         ctsvc_utils_make_image_file_name(contact_id, company->id, company->logo, dest, sizeof(dest));
273                         ret = ctsvc_utils_copy_image(CTS_LOGO_IMAGE_LOCATION, company->logo, dest);
274                         if (CONTACTS_ERROR_NONE != ret) {
275                                 /* LCOV_EXCL_START */
276                                 ERR("cts_copy_file() Fail(%d)", ret);
277                                 ctsvc_stmt_finalize(stmt);
278                                 return ret;
279                                 /* LCOV_EXCL_STOP */
280                         }
281                         free(company->logo);
282                         company->logo = strdup(dest);
283                 }
284         }
285         ctsvc_stmt_finalize(stmt);
286
287         do {
288                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_create_set_query(record, &set, &bind_text))) break;
289                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_update_record_with_set_query(set, bind_text, CTS_TABLE_DATA, company->id))) break;
290                 if (false == is_my_profile)
291                         ctsvc_set_company_noti();
292         } while (0);
293
294         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
295         free(set);
296
297         if (bind_text) {
298                 for (cursor = bind_text; cursor; cursor = cursor->next) {
299                         free(cursor->data);
300                         cursor->data = NULL;
301                 }
302                 g_slist_free(bind_text);
303         }
304
305         return ret;
306 }
307
308 int ctsvc_db_company_delete(int id, bool is_my_profile)
309 {
310         int ret;
311         char query[CTS_SQL_MIN_LEN] = {0};
312
313         snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_DATA" WHERE id = %d AND datatype = %d",
314                         id, CONTACTS_DATA_TYPE_COMPANY);
315
316         ret = ctsvc_query_exec(query);
317         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
318         if (false == is_my_profile)
319                 ctsvc_set_company_noti();
320
321         return CONTACTS_ERROR_NONE;
322 }
323
324 /*
325  * Whenever deleting company record in data table, this function will be called
326  * in order to delete company logo image file
327  */
328 void ctsvc_db_company_delete_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
329 {
330         int ret;
331         const unsigned char *logo_path;
332
333         if (1 < argc) {
334                 sqlite3_result_null(context);
335                 return;
336         }
337         logo_path = sqlite3_value_text(argv[0]);
338
339         if (logo_path) {
340                 char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
341                 snprintf(full_path, sizeof(full_path), "%s/%s", CTS_LOGO_IMAGE_LOCATION, logo_path);
342                 ret = unlink(full_path);
343                 if (ret < 0)
344                         WARN("unlink() Fail(%d)", errno);
345         }
346
347         return;
348 }
349
350