apply content_retip_if_fail() for invalid parameters
[platform/core/api/media-content.git] / src / media_face.c
1 /*
2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include <media_info_private.h>
19
20 #define MAX_SIZE 16
21
22 static int __media_face_check_media_id(const char *media_id)
23 {
24         int ret = MEDIA_CONTENT_ERROR_NONE;
25         char *query_str = NULL;
26         sqlite3_stmt *stmt = NULL;
27         int item_count = 0;
28
29         content_retip_if_fail(STRING_VALID(media_id));
30
31         /* Get image count */
32         query_str = sqlite3_mprintf(SELECT_IMAGE_COUNT_FROM_MEDIA_BY_ID, media_id);
33         ret = _content_get_result(query_str, &stmt);
34         SQLITE3_SAFE_FREE(query_str);
35         content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
36
37         if (sqlite3_step(stmt) == SQLITE_ROW)
38                 item_count = (int)sqlite3_column_int(stmt, 0);
39
40         SQLITE3_FINALIZE(stmt);
41
42         content_retvm_if(item_count == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
43
44         return MEDIA_CONTENT_ERROR_NONE;
45 }
46
47 int media_face_destroy(media_face_h face)
48 {
49         media_face_s *_face = (media_face_s*)face;
50
51         content_retip_if_fail(face);
52
53         g_free(_face->media_id);
54         g_free(_face->face_tag);
55         g_free(_face);
56
57         return MEDIA_CONTENT_ERROR_NONE;
58 }
59
60 int media_face_clone(media_face_h *dst, media_face_h src)
61 {
62         media_face_s *_src = (media_face_s*)src;
63
64         content_retip_if_fail(dst);
65         content_retip_if_fail(src);
66
67         media_face_s *_dst = g_new0(media_face_s, 1);
68
69         _dst->media_id = g_strdup(_src->media_id);
70         _dst->face_id = _src->face_id;
71         _dst->face_rect_x = _src->face_rect_x;
72         _dst->face_rect_y = _src->face_rect_y;
73         _dst->face_rect_w = _src->face_rect_w;
74         _dst->face_rect_h = _src->face_rect_h;
75         _dst->orientation = _src->orientation;
76         _dst->face_tag = g_strdup(_src->face_tag);
77
78         *dst = (media_face_h)_dst;
79
80         return MEDIA_CONTENT_ERROR_NONE;
81 }
82
83 static void __media_face_convert_itoa(int face_id, char **face_strid)
84 {
85         char buf[MAX_SIZE] = {0, };
86
87         snprintf(buf, MAX_SIZE, "%d", face_id);
88         *face_strid = g_strndup(buf, strlen(buf));
89 }
90
91 int media_face_get_face_id(media_face_h face, char **face_id)
92 {
93         media_face_s* _face = (media_face_s*)face;
94
95         content_retip_if_fail(face);
96         content_retip_if_fail(face_id);
97
98         if (_face->face_id > 0)
99                 __media_face_convert_itoa(_face->face_id, face_id);
100
101         return MEDIA_CONTENT_ERROR_NONE;
102 }
103
104 int media_face_get_media_id(media_face_h face, char **media_id)
105 {
106         media_face_s* _face = (media_face_s*)face;
107
108         content_retip_if_fail(face);
109         content_retip_if_fail(media_id);
110
111         *media_id = g_strdup(_face->media_id);
112
113         return MEDIA_CONTENT_ERROR_NONE;
114 }
115
116 int media_face_get_face_rect(media_face_h face, unsigned int *rect_x, unsigned int *rect_y, unsigned int *rect_w, unsigned int *rect_h)
117 {
118         media_face_s* _face = (media_face_s*)face;
119
120         content_retip_if_fail(face);
121         content_retip_if_fail(rect_x);
122         content_retip_if_fail(rect_y);
123         content_retip_if_fail(rect_w);
124         content_retip_if_fail(rect_h);
125
126         *rect_x = _face->face_rect_x;
127         *rect_y = _face->face_rect_y;
128         *rect_w = _face->face_rect_w;
129         *rect_h = _face->face_rect_h;
130
131         return MEDIA_CONTENT_ERROR_NONE;
132 }
133
134 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
135 {
136         media_face_s* _face = (media_face_s*)face;
137
138         content_retip_if_fail(face);
139         content_retip_if_fail(orientation);
140
141         *orientation = _face->orientation;
142
143         return MEDIA_CONTENT_ERROR_NONE;
144 }
145
146 int media_face_get_tag(media_face_h face, char **tag)
147 {
148         media_face_s* _face = (media_face_s*)face;
149
150         content_retip_if_fail(face);
151         content_retip_if_fail(tag);
152
153         *tag = g_strdup(_face->face_tag);
154
155         return MEDIA_CONTENT_ERROR_NONE;
156 }
157
158 int media_face_create(const char *media_id, media_face_h *face)
159 {
160         int ret = MEDIA_CONTENT_ERROR_NONE;
161
162         content_retip_if_fail(face);
163
164         ret = __media_face_check_media_id(media_id);
165         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "media_id does not exist or is not an image");
166
167         media_face_s* _face = g_new0(media_face_s, 1);
168
169         _face->media_id = g_strdup(media_id);
170
171         *face = (media_face_h)_face;
172
173         return MEDIA_CONTENT_ERROR_NONE;
174 }
175
176 int media_face_set_face_rect(media_face_h face, unsigned int rect_x, unsigned int rect_y, unsigned int rect_w, unsigned int rect_h)
177 {
178         media_face_s* _face = (media_face_s*)face;
179
180         content_retip_if_fail(face);
181         content_retip_if_fail(rect_w > 0);
182         content_retip_if_fail(rect_h > 0);
183
184         _face->face_rect_x = rect_x;
185         _face->face_rect_y = rect_y;
186         _face->face_rect_w = rect_w;
187         _face->face_rect_h = rect_h;
188
189         return MEDIA_CONTENT_ERROR_NONE;
190 }
191
192 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
193 {
194         media_face_s* _face = (media_face_s*)face;
195
196         content_retip_if_fail(face);
197
198         _face->orientation = orientation;
199
200         return MEDIA_CONTENT_ERROR_NONE;
201 }
202
203 int media_face_set_tag(media_face_h face, const char *tag)
204 {
205         media_face_s* _face = (media_face_s*)face;
206
207         content_retip_if_fail(face);
208
209         g_free(_face->face_tag);
210         _face->face_tag = g_strdup(tag);
211
212         return MEDIA_CONTENT_ERROR_NONE;
213 }
214
215 int media_face_insert_to_db(media_face_h face)
216 {
217         int ret = MEDIA_CONTENT_ERROR_NONE;
218         char *query_str = NULL;
219         sqlite3_stmt *stmt = NULL;
220
221         media_face_s* _face = (media_face_s*)face;
222
223         content_retip_if_fail(face);
224         content_retip_if_fail(_face->media_id);
225         content_retip_if_fail(_face->face_rect_w > 0);
226         content_retip_if_fail(_face->face_rect_h > 0);
227
228         query_str = sqlite3_mprintf(INSERT_FACE_TO_FACE, _face->media_id, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation, _face->face_tag);
229
230         ret = _content_query_sql(query_str);
231         SQLITE3_SAFE_FREE(query_str);
232
233         query_str = sqlite3_mprintf(SELECT_FACE_ID, _face->media_id, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation);
234         ret = _content_get_result(query_str, &stmt);
235         SQLITE3_SAFE_FREE(query_str);
236         content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
237
238         if (sqlite3_step(stmt) == SQLITE_ROW)
239                 _face->face_id = (int)sqlite3_column_int(stmt, 0);
240
241         SQLITE3_FINALIZE(stmt);
242
243
244         return ret;
245 }
246
247 int media_face_update_to_db(media_face_h face)
248 {
249         int ret = MEDIA_CONTENT_ERROR_NONE;
250         char *query_str = NULL;
251
252         media_face_s* _face = (media_face_s*)face;
253
254         content_retip_if_fail(face);
255         content_retip_if_fail(_face->media_id);
256         content_retip_if_fail(_face->face_id > 0);
257         content_retip_if_fail(_face->face_rect_w > 0);
258         content_retip_if_fail(_face->face_rect_h > 0);
259
260         query_str = sqlite3_mprintf(UPDATE_FACE_TO_FACE, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation, _face->face_tag, _face->face_id);
261
262         ret = _content_query_sql(query_str);
263         SQLITE3_SAFE_FREE(query_str);
264
265         return ret;
266 }
267
268 static int __media_face_safe_atoi(const char *buffer, int *si)
269 {
270         char *end = NULL;
271         errno = 0;
272
273         content_retip_if_fail(buffer);
274         content_retip_if_fail(si);
275
276         const long sl = strtol(buffer, &end, 10);
277
278         content_retvm_if(end == buffer, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "not a decimal number");
279         content_retvm_if('\0' != *end, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "extra characters at end of input: %s", end);
280         content_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "out of range of type long");
281         content_retvm_if(sl > INT_MAX, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "greater than INT_MAX");
282         content_retvm_if(sl < INT_MIN, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "less than INT_MIN");
283
284         *si = (int)sl;
285
286         return MEDIA_CONTENT_ERROR_NONE;
287 }
288
289 int media_face_delete_from_db(const char *face_id)
290 {
291         int ret = MEDIA_CONTENT_ERROR_NONE;
292         char *query_str = NULL;
293         int query_face_id = 0;
294
295         ret = __media_face_safe_atoi(face_id, &query_face_id);
296         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
297
298         /*Update modified_time to 0.. It will restore the deleted face when media_info_start_face_detection() is called */
299         query_str = sqlite3_mprintf(UPDATE_MEDIA_INFO_IN_FACE_SCAN_LIST, query_face_id);
300         ret = _content_query_sql(query_str);
301         SQLITE3_SAFE_FREE(query_str);
302         content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
303
304         query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, query_face_id);
305         ret = _content_query_sql(query_str);
306         SQLITE3_SAFE_FREE(query_str);
307
308         return ret;
309 }
310
311 int media_face_get_face_count_from_db(filter_h filter, int *face_count)
312 {
313         content_retip_if_fail(face_count);
314
315         return _media_db_get_group_count(filter, MEDIA_GROUP_FACE, face_count);
316 }
317
318 int media_face_foreach_face_from_db(filter_h filter, media_face_cb callback, void *user_data)
319 {
320         content_retip_if_fail(callback);
321
322         return _media_db_get_face(NULL, filter, callback, user_data);
323 }