[ACR-1819] Deprecate meaningless media group enum
[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 // LCOV_EXCL_START
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         content_warn("DEPRECATION WARNING: media_face_destroy() is deprecated and will be removed from next release.");
50         media_face_s *_face = (media_face_s*)face;
51
52         content_retip_if_fail(face);
53
54         g_free(_face->media_id);
55         g_free(_face->face_tag);
56         g_free(_face);
57
58         return MEDIA_CONTENT_ERROR_NONE;
59 }
60
61 int media_face_clone(media_face_h *dst, media_face_h src)
62 {
63         content_warn("DEPRECATION WARNING: media_face_clone() is deprecated and will be removed from next release.");
64         media_face_s *_src = (media_face_s*)src;
65
66         content_retip_if_fail(dst);
67         content_retip_if_fail(src);
68
69         media_face_s *_dst = g_new0(media_face_s, 1);
70
71         _dst->media_id = g_strdup(_src->media_id);
72         _dst->face_id = _src->face_id;
73         _dst->face_rect_x = _src->face_rect_x;
74         _dst->face_rect_y = _src->face_rect_y;
75         _dst->face_rect_w = _src->face_rect_w;
76         _dst->face_rect_h = _src->face_rect_h;
77         _dst->orientation = _src->orientation;
78         _dst->face_tag = g_strdup(_src->face_tag);
79
80         *dst = (media_face_h)_dst;
81
82         return MEDIA_CONTENT_ERROR_NONE;
83 }
84
85 static void __media_face_convert_itoa(int face_id, char **face_strid)
86 {
87         char buf[MAX_SIZE] = {0, };
88
89         snprintf(buf, MAX_SIZE, "%d", face_id);
90         *face_strid = g_strndup(buf, strlen(buf));
91 }
92
93 int media_face_get_face_id(media_face_h face, char **face_id)
94 {
95         content_warn("DEPRECATION WARNING: media_face_get_face_id() is deprecated and will be removed from next release.");
96         media_face_s* _face = (media_face_s*)face;
97
98         content_retip_if_fail(face);
99         content_retip_if_fail(face_id);
100
101         if (_face->face_id > 0)
102                 __media_face_convert_itoa(_face->face_id, face_id);
103
104         return MEDIA_CONTENT_ERROR_NONE;
105 }
106
107 int media_face_get_media_id(media_face_h face, char **media_id)
108 {
109         content_warn("DEPRECATION WARNING: media_face_get_media_id() is deprecated and will be removed from next release.");
110         media_face_s* _face = (media_face_s*)face;
111
112         content_retip_if_fail(face);
113         content_retip_if_fail(media_id);
114
115         *media_id = g_strdup(_face->media_id);
116
117         return MEDIA_CONTENT_ERROR_NONE;
118 }
119
120 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)
121 {
122         content_warn("DEPRECATION WARNING: media_face_get_face_rect() is deprecated and will be removed from next release.");
123         media_face_s* _face = (media_face_s*)face;
124
125         content_retip_if_fail(face);
126         content_retip_if_fail(rect_x);
127         content_retip_if_fail(rect_y);
128         content_retip_if_fail(rect_w);
129         content_retip_if_fail(rect_h);
130
131         *rect_x = _face->face_rect_x;
132         *rect_y = _face->face_rect_y;
133         *rect_w = _face->face_rect_w;
134         *rect_h = _face->face_rect_h;
135
136         return MEDIA_CONTENT_ERROR_NONE;
137 }
138
139 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
140 {
141         content_warn("DEPRECATION WARNING: media_face_get_orientation() is deprecated and will be removed from next release.");
142         media_face_s* _face = (media_face_s*)face;
143
144         content_retip_if_fail(face);
145         content_retip_if_fail(orientation);
146
147         *orientation = _face->orientation;
148
149         return MEDIA_CONTENT_ERROR_NONE;
150 }
151
152 int media_face_get_tag(media_face_h face, char **tag)
153 {
154         content_warn("DEPRECATION WARNING: media_face_get_tag() is deprecated and will be removed from next release.");
155         media_face_s* _face = (media_face_s*)face;
156
157         content_retip_if_fail(face);
158         content_retip_if_fail(tag);
159
160         *tag = g_strdup(_face->face_tag);
161
162         return MEDIA_CONTENT_ERROR_NONE;
163 }
164
165 int media_face_create(const char *media_id, media_face_h *face)
166 {
167         content_warn("DEPRECATION WARNING: media_face_create() is deprecated and will be removed from next release.");
168         int ret = MEDIA_CONTENT_ERROR_NONE;
169
170         content_retip_if_fail(face);
171
172         ret = __media_face_check_media_id(media_id);
173         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "media_id does not exist or is not an image");
174
175         media_face_s* _face = g_new0(media_face_s, 1);
176
177         _face->media_id = g_strdup(media_id);
178
179         *face = (media_face_h)_face;
180
181         return MEDIA_CONTENT_ERROR_NONE;
182 }
183
184 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)
185 {
186         content_warn("DEPRECATION WARNING: media_face_set_face_rect() is deprecated and will be removed from next release.");
187         media_face_s* _face = (media_face_s*)face;
188
189         content_retip_if_fail(face);
190         content_retip_if_fail(rect_w > 0);
191         content_retip_if_fail(rect_h > 0);
192
193         _face->face_rect_x = rect_x;
194         _face->face_rect_y = rect_y;
195         _face->face_rect_w = rect_w;
196         _face->face_rect_h = rect_h;
197
198         return MEDIA_CONTENT_ERROR_NONE;
199 }
200
201 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
202 {
203         content_warn("DEPRECATION WARNING: media_face_set_orientation() is deprecated and will be removed from next release.");
204         media_face_s* _face = (media_face_s*)face;
205
206         content_retip_if_fail(face);
207
208         _face->orientation = orientation;
209
210         return MEDIA_CONTENT_ERROR_NONE;
211 }
212
213 int media_face_set_tag(media_face_h face, const char *tag)
214 {
215         content_warn("DEPRECATION WARNING: media_face_set_tag() is deprecated and will be removed from next release.");
216         media_face_s* _face = (media_face_s*)face;
217
218         content_retip_if_fail(face);
219
220         g_free(_face->face_tag);
221         _face->face_tag = g_strdup(tag);
222
223         return MEDIA_CONTENT_ERROR_NONE;
224 }
225
226 int media_face_insert_to_db(media_face_h face)
227 {
228         content_warn("DEPRECATION WARNING: media_face_insert_to_db() is deprecated and will be removed from next release.");
229         int ret = MEDIA_CONTENT_ERROR_NONE;
230         char *query_str = NULL;
231         sqlite3_stmt *stmt = NULL;
232
233         media_face_s* _face = (media_face_s*)face;
234
235         content_retip_if_fail(face);
236         content_retip_if_fail(_face->media_id);
237         content_retip_if_fail(_face->face_rect_w > 0);
238         content_retip_if_fail(_face->face_rect_h > 0);
239
240         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);
241
242         ret = _content_query_sql(query_str);
243         SQLITE3_SAFE_FREE(query_str);
244
245         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);
246         ret = _content_get_result(query_str, &stmt);
247         SQLITE3_SAFE_FREE(query_str);
248         content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
249
250         if (sqlite3_step(stmt) == SQLITE_ROW)
251                 _face->face_id = (int)sqlite3_column_int(stmt, 0);
252
253         SQLITE3_FINALIZE(stmt);
254
255
256         return ret;
257 }
258
259 int media_face_update_to_db(media_face_h face)
260 {
261         content_warn("DEPRECATION WARNING: media_face_update_to_db() is deprecated and will be removed from next release.");
262         int ret = MEDIA_CONTENT_ERROR_NONE;
263         char *query_str = NULL;
264
265         media_face_s* _face = (media_face_s*)face;
266
267         content_retip_if_fail(face);
268         content_retip_if_fail(_face->media_id);
269         content_retip_if_fail(_face->face_id > 0);
270         content_retip_if_fail(_face->face_rect_w > 0);
271         content_retip_if_fail(_face->face_rect_h > 0);
272
273         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);
274
275         ret = _content_query_sql(query_str);
276         SQLITE3_SAFE_FREE(query_str);
277
278         return ret;
279 }
280
281 static int __media_face_safe_atoi(const char *buffer, int *si)
282 {
283         char *end = NULL;
284         errno = 0;
285
286         content_retip_if_fail(buffer);
287         content_retip_if_fail(si);
288
289         const long sl = strtol(buffer, &end, 10);
290
291         content_retvm_if(end == buffer, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "not a decimal number");
292         content_retvm_if('\0' != *end, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "extra characters at end of input: %s", end);
293         content_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "out of range of type long");
294         content_retvm_if(sl > INT_MAX, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "greater than INT_MAX");
295         content_retvm_if(sl < INT_MIN, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "less than INT_MIN");
296
297         *si = (int)sl;
298
299         return MEDIA_CONTENT_ERROR_NONE;
300 }
301
302 int media_face_delete_from_db(const char *face_id)
303 {
304         content_warn("DEPRECATION WARNING: media_face_delete_from_db() is deprecated and will be removed from next release.");
305         int ret = MEDIA_CONTENT_ERROR_NONE;
306         char *query_str = NULL;
307         int query_face_id = 0;
308
309         ret = __media_face_safe_atoi(face_id, &query_face_id);
310         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
311
312         /*Update modified_time to 0.. It will restore the deleted face when media_info_start_face_detection() is called */
313         query_str = sqlite3_mprintf(UPDATE_MEDIA_INFO_IN_FACE_SCAN_LIST, query_face_id);
314         ret = _content_query_sql(query_str);
315         SQLITE3_SAFE_FREE(query_str);
316         content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
317
318         query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, query_face_id);
319         ret = _content_query_sql(query_str);
320         SQLITE3_SAFE_FREE(query_str);
321
322         return ret;
323 }
324
325 int media_face_get_face_count_from_db(filter_h filter, int *face_count)
326 {
327         content_warn("DEPRECATION WARNING: media_face_get_face_count_from_db() is deprecated and will be removed from next release.");
328         content_retip_if_fail(face_count);
329
330         return _media_db_get_group_count(filter, MEDIA_GROUP_FACE, face_count);
331 }
332
333 int media_face_foreach_face_from_db(filter_h filter, media_face_cb callback, void *user_data)
334 {
335         content_warn("DEPRECATION WARNING: media_face_foreach_face_from_db() is deprecated and will be removed from next release.");
336         content_retip_if_fail(callback);
337
338         return _media_db_get_face(NULL, filter, callback, user_data);
339 }
340 // LCOV_EXCL_STOP