Reinforce code for coverage
[platform/core/api/media-content.git] / src / media_image.c
1 /*
2 * Copyright (c) 2011 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 media_content_retv_free_image_if(expr, val, p_str) do { \
21                                 if (expr) {     \
22                                         LOGE(FONT_COLOR_RED"Memory allocation failure"FONT_COLOR_RESET);        \
23                                         image_meta_destroy(p_str);      \
24                                         return (val);   \
25                                 }       \
26                         } while (0)
27
28
29 int image_meta_destroy(image_meta_h image)
30 {
31         image_meta_s *_image = (image_meta_s*)image;
32         media_content_retvm_if(_image == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Image handle is null");
33
34         SAFE_FREE(_image->media_id);
35         SAFE_FREE(_image->date_taken);
36         SAFE_FREE(_image->title);
37         SAFE_FREE(_image->burst_id);
38         SAFE_FREE(_image->exposure_time);
39         SAFE_FREE(_image->model);
40         SAFE_FREE(_image);
41
42         return MEDIA_CONTENT_ERROR_NONE;
43 }
44
45 int image_meta_clone(image_meta_h *dst, image_meta_h src)
46 {
47         image_meta_s *_src = (image_meta_s*)src;
48         media_content_retvm_if(_src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Source handle is null");
49
50         image_meta_s *_dst = (image_meta_s*)calloc(1, sizeof(image_meta_s));
51         media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
52
53         if (STRING_VALID(_src->media_id)) {
54                 _dst->media_id = strdup(_src->media_id);
55                 media_content_retv_free_image_if(_dst->media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
56         }
57
58         if (STRING_VALID(_src->date_taken)) {
59                 _dst->date_taken = strdup(_src->date_taken);
60                 media_content_retv_free_image_if(_dst->date_taken == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
61         }
62
63         if (STRING_VALID(_src->title)) {
64                 _dst->title = strdup(_src->title);
65                 media_content_retv_free_image_if(_dst->title == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
66         }
67
68         if (STRING_VALID(_src->burst_id)) {
69                 _dst->burst_id = strdup(_src->burst_id);
70                 media_content_retv_free_image_if(_dst->burst_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
71         }
72
73         if (STRING_VALID(_src->exposure_time)) {
74                 _dst->exposure_time = strdup(_src->exposure_time);
75                 media_content_retv_free_image_if(_dst->exposure_time == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
76         }
77
78         if (STRING_VALID(_src->model)) {
79                 _dst->model = strdup(_src->model);
80                 media_content_retv_free_image_if(_dst->model == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
81         }
82
83         _dst->fnumber = _src->fnumber;
84         _dst->iso = _src->iso;
85         _dst->width = _src->width;
86         _dst->height = _src->height;
87         _dst->orientation = _src->orientation;
88
89         *dst = (image_meta_h)_dst;
90
91         return MEDIA_CONTENT_ERROR_NONE;
92 }
93
94 int image_meta_get_media_id(image_meta_h image, char **media_id)
95 {
96         int ret = MEDIA_CONTENT_ERROR_NONE;
97         image_meta_s *_image = (image_meta_s*)image;
98
99         if (_image && media_id) {
100                 if (STRING_VALID(_image->media_id)) {
101                         char *new_string = strdup(_image->media_id);
102                         media_content_retvm_if(new_string == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
103
104                         *media_id = new_string;
105                 } else {
106                         *media_id = NULL;
107                 }
108                 ret = MEDIA_CONTENT_ERROR_NONE;
109
110         } else {
111                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
112                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
113         }
114
115         return ret;
116
117 }
118
119 int image_meta_get_width(image_meta_h image, int *width)
120 {
121         int ret = MEDIA_CONTENT_ERROR_NONE;
122         image_meta_s *_image = (image_meta_s*)image;
123
124         if (_image && width) {
125                 *width = _image->width;
126                 ret = MEDIA_CONTENT_ERROR_NONE;
127         } else {
128                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
129                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
130         }
131
132         return ret;
133 }
134 int image_meta_get_height(image_meta_h image, int *height)
135 {
136         int ret = MEDIA_CONTENT_ERROR_NONE;
137         image_meta_s *_image = (image_meta_s*)image;
138
139         if (_image && height) {
140                 *height = _image->height;
141                 ret = MEDIA_CONTENT_ERROR_NONE;
142         } else {
143                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
144                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
145         }
146
147         return ret;
148 }
149
150 int image_meta_get_orientation(image_meta_h image, media_content_orientation_e* orientation)
151 {
152         int ret = MEDIA_CONTENT_ERROR_NONE;
153         image_meta_s *_image = (image_meta_s*)image;
154
155         if (_image && orientation) {
156                 *orientation = _image->orientation;
157                 ret = MEDIA_CONTENT_ERROR_NONE;
158         } else {
159                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
160                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
161         }
162
163         return ret;
164 }
165
166 int image_meta_get_date_taken(image_meta_h image, char **date_taken)
167 {
168         int ret = MEDIA_CONTENT_ERROR_NONE;
169         image_meta_s *_image = (image_meta_s*)image;
170
171         if (_image && date_taken) {
172                 if (STRING_VALID(_image->date_taken)) {
173                         char *new_string = strdup(_image->date_taken);
174                         media_content_retvm_if(new_string == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
175
176                         *date_taken = new_string;
177                 } else {
178                         *date_taken = NULL;
179                 }
180
181                 ret = MEDIA_CONTENT_ERROR_NONE;
182         } else {
183                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
184                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
185         }
186
187         return ret;
188 }
189
190 int image_meta_get_burst_id(image_meta_h image, char **burst_id)
191 {
192         int ret = MEDIA_CONTENT_ERROR_NONE;
193         media_content_warn("DEPRECATION WARNING: image_meta_get_burst_id() is deprecated and will be removed from next release.");
194         image_meta_s *_image = (image_meta_s*)image;
195
196         if (_image && burst_id) {
197                 if (STRING_VALID(_image->burst_id)) {
198                         *burst_id = strdup(_image->burst_id);
199                         media_content_retvm_if(*burst_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
200                 } else {
201                         *burst_id = NULL;
202                 }
203                 ret = MEDIA_CONTENT_ERROR_NONE;
204         } else {
205                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
206                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
207         }
208
209         return ret;
210 }
211
212 int image_meta_get_exposure_time(image_meta_h image, char **exposure_time)
213 {
214         int ret = MEDIA_CONTENT_ERROR_NONE;
215         image_meta_s *_image = (image_meta_s*)image;
216
217         if (_image && exposure_time) {
218                 if (STRING_VALID(_image->exposure_time)) {
219                         *exposure_time = strdup(_image->exposure_time);
220                         media_content_retvm_if(*exposure_time == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
221                 } else {
222                         *exposure_time = NULL;
223                 }
224                 ret = MEDIA_CONTENT_ERROR_NONE;
225         } else {
226                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
227                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
228         }
229
230         return ret;
231 }
232
233 int image_meta_get_fnumber(image_meta_h image, double *fnumber)
234 {
235         int ret = MEDIA_CONTENT_ERROR_NONE;
236         image_meta_s *_image = (image_meta_s*)image;
237
238         if (_image && fnumber) {
239                 *fnumber = _image->fnumber;
240                 ret = MEDIA_CONTENT_ERROR_NONE;
241         } else {
242                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
243                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
244         }
245
246         return ret;
247 }
248
249 int image_meta_get_iso(image_meta_h image, int *iso)
250 {
251         int ret = MEDIA_CONTENT_ERROR_NONE;
252         image_meta_s *_image = (image_meta_s*)image;
253
254         if (_image && iso) {
255                 *iso = _image->iso;
256                 ret = MEDIA_CONTENT_ERROR_NONE;
257         } else {
258                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
259                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
260         }
261
262         return ret;
263 }
264
265 int image_meta_get_model(image_meta_h image, char **model)
266 {
267         int ret = MEDIA_CONTENT_ERROR_NONE;
268         image_meta_s *_image = (image_meta_s*)image;
269
270         if (_image && model) {
271                 if (STRING_VALID(_image->model)) {
272                         *model = strdup(_image->model);
273                         media_content_retvm_if(*model == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
274                 } else {
275                         *model = NULL;
276                 }
277                 ret = MEDIA_CONTENT_ERROR_NONE;
278         } else {
279                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
280                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
281         }
282
283         return ret;
284 }
285
286 int image_meta_is_burst_shot(image_meta_h image, bool *is_burst_shot)
287 {
288         int ret = MEDIA_CONTENT_ERROR_NONE;
289         media_content_warn("DEPRECATION WARNING: image_meta_is_burst_shot() is deprecated and will be removed from next release.");
290         image_meta_s *_image = (image_meta_s*)image;
291
292         if (_image && is_burst_shot) {
293                 if (STRING_VALID(_image->burst_id))
294                         *is_burst_shot = true;
295                 else
296                         *is_burst_shot = false;
297
298                 ret = MEDIA_CONTENT_ERROR_NONE;
299         } else {
300                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
301                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
302         }
303
304         return ret;
305 }
306
307 int image_meta_set_orientation(image_meta_h image, media_content_orientation_e orientation)
308 {
309         int ret = MEDIA_CONTENT_ERROR_NONE;
310         media_content_warn("DEPRECATION WARNING: image_meta_set_orientation() is deprecated and will be removed from next release.");
311         image_meta_s *_image = (image_meta_s*)image;
312
313         if (_image == NULL) {
314                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
315                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
316         }
317
318         if ((orientation < MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE) || (orientation > MEDIA_CONTENT_ORIENTATION_ROT_270)) {
319                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
320                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
321         }
322
323         _image->orientation = orientation;
324
325         return ret;
326 }
327
328 int image_meta_update_to_db(image_meta_h image)
329 {
330         int ret = MEDIA_CONTENT_ERROR_NONE;
331         media_content_warn("DEPRECATION WARNING: image_meta_update_to_db() is deprecated and will be removed from next release.");
332         image_meta_s *_image = (image_meta_s*)image;
333         char *sql = NULL;
334
335         if (_image != NULL && STRING_VALID(_image->media_id)) {
336                 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0,};
337                 memset(storage_id, 0x00, sizeof(storage_id));
338
339                 ret = _media_db_get_storage_id_by_media_id(_image->media_id, storage_id);
340                 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
341
342                 sql = sqlite3_mprintf(UPDATE_IMAGE_META_FROM_MEDIA, storage_id, _image->orientation, _image->media_id);
343                 ret = _content_query_sql(sql);
344                 SQLITE3_SAFE_FREE(sql);
345         } else {
346                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
347                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
348         }
349
350         return ret;
351 }