2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <media_info_private.h>
22 static int __media_face_check_media_id(const char *media_id)
24 int ret = MEDIA_CONTENT_ERROR_NONE;
25 char *query_str = NULL;
26 sqlite3_stmt *stmt = NULL;
29 content_retip_if_fail(STRING_VALID(media_id));
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);
37 if (sqlite3_step(stmt) == SQLITE_ROW)
38 item_count = (int)sqlite3_column_int(stmt, 0);
40 SQLITE3_FINALIZE(stmt);
42 content_retvm_if(item_count == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
44 return MEDIA_CONTENT_ERROR_NONE;
47 int media_face_destroy(media_face_h face)
49 media_face_s *_face = (media_face_s*)face;
51 content_retip_if_fail(face);
53 g_free(_face->media_id);
54 g_free(_face->face_tag);
57 return MEDIA_CONTENT_ERROR_NONE;
60 int media_face_clone(media_face_h *dst, media_face_h src)
62 media_face_s *_src = (media_face_s*)src;
64 content_retip_if_fail(dst);
65 content_retip_if_fail(src);
67 media_face_s *_dst = g_new0(media_face_s, 1);
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);
78 *dst = (media_face_h)_dst;
80 return MEDIA_CONTENT_ERROR_NONE;
83 static void __media_face_convert_itoa(int face_id, char **face_strid)
85 char buf[MAX_SIZE] = {0, };
87 snprintf(buf, MAX_SIZE, "%d", face_id);
88 *face_strid = g_strndup(buf, strlen(buf));
91 int media_face_get_face_id(media_face_h face, char **face_id)
93 media_face_s* _face = (media_face_s*)face;
95 content_retip_if_fail(face);
96 content_retip_if_fail(face_id);
98 if (_face->face_id > 0)
99 __media_face_convert_itoa(_face->face_id, face_id);
101 return MEDIA_CONTENT_ERROR_NONE;
104 int media_face_get_media_id(media_face_h face, char **media_id)
106 media_face_s* _face = (media_face_s*)face;
108 content_retip_if_fail(face);
109 content_retip_if_fail(media_id);
111 *media_id = g_strdup(_face->media_id);
113 return MEDIA_CONTENT_ERROR_NONE;
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)
118 media_face_s* _face = (media_face_s*)face;
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);
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;
131 return MEDIA_CONTENT_ERROR_NONE;
134 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
136 media_face_s* _face = (media_face_s*)face;
138 content_retip_if_fail(face);
139 content_retip_if_fail(orientation);
141 *orientation = _face->orientation;
143 return MEDIA_CONTENT_ERROR_NONE;
146 int media_face_get_tag(media_face_h face, char **tag)
148 media_face_s* _face = (media_face_s*)face;
150 content_retip_if_fail(face);
151 content_retip_if_fail(tag);
153 *tag = g_strdup(_face->face_tag);
155 return MEDIA_CONTENT_ERROR_NONE;
158 int media_face_create(const char *media_id, media_face_h *face)
160 int ret = MEDIA_CONTENT_ERROR_NONE;
162 content_retip_if_fail(face);
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");
167 media_face_s* _face = g_new0(media_face_s, 1);
169 _face->media_id = g_strdup(media_id);
171 *face = (media_face_h)_face;
173 return MEDIA_CONTENT_ERROR_NONE;
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)
178 media_face_s* _face = (media_face_s*)face;
180 content_retip_if_fail(face);
181 content_retip_if_fail(rect_w > 0);
182 content_retip_if_fail(rect_h > 0);
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;
189 return MEDIA_CONTENT_ERROR_NONE;
192 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
194 media_face_s* _face = (media_face_s*)face;
196 content_retip_if_fail(face);
198 _face->orientation = orientation;
200 return MEDIA_CONTENT_ERROR_NONE;
203 int media_face_set_tag(media_face_h face, const char *tag)
205 media_face_s* _face = (media_face_s*)face;
207 content_retip_if_fail(face);
209 g_free(_face->face_tag);
210 _face->face_tag = g_strdup(tag);
212 return MEDIA_CONTENT_ERROR_NONE;
215 int media_face_insert_to_db(media_face_h face)
217 int ret = MEDIA_CONTENT_ERROR_NONE;
218 char *query_str = NULL;
219 sqlite3_stmt *stmt = NULL;
221 media_face_s* _face = (media_face_s*)face;
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);
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);
230 ret = _content_query_sql(query_str);
231 SQLITE3_SAFE_FREE(query_str);
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);
238 if (sqlite3_step(stmt) == SQLITE_ROW)
239 _face->face_id = (int)sqlite3_column_int(stmt, 0);
241 SQLITE3_FINALIZE(stmt);
247 int media_face_update_to_db(media_face_h face)
249 int ret = MEDIA_CONTENT_ERROR_NONE;
250 char *query_str = NULL;
252 media_face_s* _face = (media_face_s*)face;
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);
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);
262 ret = _content_query_sql(query_str);
263 SQLITE3_SAFE_FREE(query_str);
268 static int __media_face_safe_atoi(const char *buffer, int *si)
273 content_retip_if_fail(buffer);
274 content_retip_if_fail(si);
276 const long sl = strtol(buffer, &end, 10);
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");
286 return MEDIA_CONTENT_ERROR_NONE;
289 int media_face_delete_from_db(const char *face_id)
291 int ret = MEDIA_CONTENT_ERROR_NONE;
292 char *query_str = NULL;
293 int query_face_id = 0;
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");
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);
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);
311 int media_face_get_face_count_from_db(filter_h filter, int *face_count)
313 content_retip_if_fail(face_count);
315 return _media_db_get_group_count(filter, MEDIA_GROUP_FACE, face_count);
318 int media_face_foreach_face_from_db(filter_h filter, media_face_cb callback, void *user_data)
320 content_retip_if_fail(callback);
322 return _media_db_get_face(NULL, filter, callback, user_data);