89bc029c646fdcb0ba2ef8f228ff997757788cfa
[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_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid 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_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
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_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
121         content_retvm_if(!(rect_x && rect_y && rect_w && rect_h), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect");
122
123         *rect_x = _face->face_rect_x;
124         *rect_y = _face->face_rect_y;
125         *rect_w = _face->face_rect_w;
126         *rect_h = _face->face_rect_h;
127
128         return MEDIA_CONTENT_ERROR_NONE;
129 }
130
131 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
132 {
133         media_face_s* _face = (media_face_s*)face;
134
135         content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
136         content_retvm_if(orientation == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid orientation");
137
138         *orientation = _face->orientation;
139
140         return MEDIA_CONTENT_ERROR_NONE;
141 }
142
143 int media_face_get_tag(media_face_h face, char **tag)
144 {
145         media_face_s* _face = (media_face_s*)face;
146
147         content_retip_if_fail(face);
148         content_retip_if_fail(tag);
149
150         *tag = g_strdup(_face->face_tag);
151
152         return MEDIA_CONTENT_ERROR_NONE;
153 }
154
155 int media_face_create(const char *media_id, media_face_h *face)
156 {
157         int ret = MEDIA_CONTENT_ERROR_NONE;
158
159         content_retip_if_fail(face);
160
161         ret = __media_face_check_media_id(media_id);
162         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "media_id does not exist or is not an image");
163
164         media_face_s* _face = g_new0(media_face_s, 1);
165
166         _face->media_id = g_strdup(media_id);
167
168         *face = (media_face_h)_face;
169
170         return MEDIA_CONTENT_ERROR_NONE;
171 }
172
173 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)
174 {
175         media_face_s* _face = (media_face_s*)face;
176
177         content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
178         content_retvm_if(rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
179         content_retvm_if(rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
180
181         _face->face_rect_x = rect_x;
182         _face->face_rect_y = rect_y;
183         _face->face_rect_w = rect_w;
184         _face->face_rect_h = rect_h;
185
186         return MEDIA_CONTENT_ERROR_NONE;
187 }
188
189 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
190 {
191         media_face_s* _face = (media_face_s*)face;
192
193         content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
194
195         _face->orientation = orientation;
196
197         return MEDIA_CONTENT_ERROR_NONE;
198 }
199
200 int media_face_set_tag(media_face_h face, const char *tag)
201 {
202         media_face_s* _face = (media_face_s*)face;
203
204         content_retip_if_fail(face);
205
206         g_free(_face->face_tag);
207         _face->face_tag = g_strdup(tag);
208
209         return MEDIA_CONTENT_ERROR_NONE;
210 }
211
212 int media_face_insert_to_db(media_face_h face)
213 {
214         int ret = MEDIA_CONTENT_ERROR_NONE;
215         char *query_str = NULL;
216         sqlite3_stmt *stmt = NULL;
217
218         media_face_s* _face = (media_face_s*)face;
219
220         content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
221         content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
222         content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
223         content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
224
225         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);
226
227         ret = _content_query_sql(query_str);
228         SQLITE3_SAFE_FREE(query_str);
229
230         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);
231         ret = _content_get_result(query_str, &stmt);
232         SQLITE3_SAFE_FREE(query_str);
233         content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
234
235         if (sqlite3_step(stmt) == SQLITE_ROW)
236                 _face->face_id = (int)sqlite3_column_int(stmt, 0);
237
238         SQLITE3_FINALIZE(stmt);
239
240
241         return ret;
242 }
243
244 int media_face_update_to_db(media_face_h face)
245 {
246         int ret = MEDIA_CONTENT_ERROR_NONE;
247         char *query_str = NULL;
248
249         media_face_s* _face = (media_face_s*)face;
250
251         content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
252         content_retvm_if(_face->face_id == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid face_id");
253         content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
254         content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
255         content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
256
257         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);
258
259         ret = _content_query_sql(query_str);
260         SQLITE3_SAFE_FREE(query_str);
261
262         return ret;
263 }
264
265 static int __media_face_safe_atoi(const char *buffer, int *si)
266 {
267         char *end = NULL;
268         errno = 0;
269         content_retvm_if(buffer == NULL || si == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid parameter");
270
271         const long sl = strtol(buffer, &end, 10);
272
273         content_retvm_if(end == buffer, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "not a decimal number");
274         content_retvm_if('\0' != *end, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "extra characters at end of input: %s", end);
275         content_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "out of range of type long");
276         content_retvm_if(sl > INT_MAX, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "greater than INT_MAX");
277         content_retvm_if(sl < INT_MIN, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "less than INT_MIN");
278
279         *si = (int)sl;
280
281         return MEDIA_CONTENT_ERROR_NONE;
282 }
283
284 int media_face_delete_from_db(const char *face_id)
285 {
286         int ret = MEDIA_CONTENT_ERROR_NONE;
287         char *query_str = NULL;
288         int query_face_id = 0;
289         content_retvm_if(!STRING_VALID(face_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
290
291         ret = __media_face_safe_atoi(face_id, &query_face_id);
292         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
293
294         /*Update modified_time to 0.. It will restore the deleted face when media_info_start_face_detection() is called */
295         query_str = sqlite3_mprintf(UPDATE_MEDIA_INFO_IN_FACE_SCAN_LIST, query_face_id);
296         ret = _content_query_sql(query_str);
297         SQLITE3_SAFE_FREE(query_str);
298         content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
299
300         query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, query_face_id);
301         ret = _content_query_sql(query_str);
302         SQLITE3_SAFE_FREE(query_str);
303
304         return ret;
305 }
306
307 int media_face_get_face_count_from_db(filter_h filter, int *face_count)
308 {
309         int ret = MEDIA_CONTENT_ERROR_NONE;
310
311         if (face_count == NULL) {
312                 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
313                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
314         }
315
316         ret = _media_db_get_group_count(filter, MEDIA_GROUP_FACE, face_count);
317
318         return ret;
319 }
320
321 int media_face_foreach_face_from_db(filter_h filter, media_face_cb callback, void *user_data)
322 {
323         int ret = MEDIA_CONTENT_ERROR_NONE;
324
325         content_retvm_if(callback == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid paramter");
326
327         ret = _media_db_get_face(NULL, filter, callback, user_data);
328
329         return ret;
330 }