839fc37c4a0626778e8673cd102c99fe2e2d7098
[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->exposure_time);
38         SAFE_FREE(_image->model);
39         SAFE_FREE(_image);
40
41         return MEDIA_CONTENT_ERROR_NONE;
42 }
43
44 int image_meta_clone(image_meta_h *dst, image_meta_h src)
45 {
46         image_meta_s *_src = (image_meta_s*)src;
47         media_content_retvm_if(_src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Source handle is null");
48
49         image_meta_s *_dst = (image_meta_s*)calloc(1, sizeof(image_meta_s));
50         media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
51
52         if (STRING_VALID(_src->media_id)) {
53                 _dst->media_id = strdup(_src->media_id);
54                 media_content_retv_free_image_if(_dst->media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
55         }
56
57         if (STRING_VALID(_src->date_taken)) {
58                 _dst->date_taken = strdup(_src->date_taken);
59                 media_content_retv_free_image_if(_dst->date_taken == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
60         }
61
62         if (STRING_VALID(_src->title)) {
63                 _dst->title = strdup(_src->title);
64                 media_content_retv_free_image_if(_dst->title == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
65         }
66
67         if (STRING_VALID(_src->exposure_time)) {
68                 _dst->exposure_time = strdup(_src->exposure_time);
69                 media_content_retv_free_image_if(_dst->exposure_time == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
70         }
71
72         if (STRING_VALID(_src->model)) {
73                 _dst->model = strdup(_src->model);
74                 media_content_retv_free_image_if(_dst->model == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
75         }
76
77         _dst->fnumber = _src->fnumber;
78         _dst->iso = _src->iso;
79         _dst->width = _src->width;
80         _dst->height = _src->height;
81         _dst->orientation = _src->orientation;
82
83         *dst = (image_meta_h)_dst;
84
85         return MEDIA_CONTENT_ERROR_NONE;
86 }
87
88 int image_meta_get_media_id(image_meta_h image, char **media_id)
89 {
90         int ret = MEDIA_CONTENT_ERROR_NONE;
91         image_meta_s *_image = (image_meta_s*)image;
92
93         if (_image && media_id) {
94                 if (STRING_VALID(_image->media_id)) {
95                         char *new_string = strdup(_image->media_id);
96                         media_content_retvm_if(new_string == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
97
98                         *media_id = new_string;
99                 } else {
100                         *media_id = NULL;
101                 }
102                 ret = MEDIA_CONTENT_ERROR_NONE;
103
104         } else {
105                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
106                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
107         }
108
109         return ret;
110
111 }
112
113 int image_meta_get_width(image_meta_h image, int *width)
114 {
115         int ret = MEDIA_CONTENT_ERROR_NONE;
116         image_meta_s *_image = (image_meta_s*)image;
117
118         if (_image && width) {
119                 *width = _image->width;
120                 ret = MEDIA_CONTENT_ERROR_NONE;
121         } else {
122                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
123                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
124         }
125
126         return ret;
127 }
128 int image_meta_get_height(image_meta_h image, int *height)
129 {
130         int ret = MEDIA_CONTENT_ERROR_NONE;
131         image_meta_s *_image = (image_meta_s*)image;
132
133         if (_image && height) {
134                 *height = _image->height;
135                 ret = MEDIA_CONTENT_ERROR_NONE;
136         } else {
137                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
138                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
139         }
140
141         return ret;
142 }
143
144 int image_meta_get_orientation(image_meta_h image, media_content_orientation_e* orientation)
145 {
146         int ret = MEDIA_CONTENT_ERROR_NONE;
147         image_meta_s *_image = (image_meta_s*)image;
148
149         if (_image && orientation) {
150                 *orientation = _image->orientation;
151                 ret = MEDIA_CONTENT_ERROR_NONE;
152         } else {
153                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
154                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
155         }
156
157         return ret;
158 }
159
160 int image_meta_get_date_taken(image_meta_h image, char **date_taken)
161 {
162         int ret = MEDIA_CONTENT_ERROR_NONE;
163         image_meta_s *_image = (image_meta_s*)image;
164
165         if (_image && date_taken) {
166                 if (STRING_VALID(_image->date_taken)) {
167                         char *new_string = strdup(_image->date_taken);
168                         media_content_retvm_if(new_string == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
169
170                         *date_taken = new_string;
171                 } else {
172                         *date_taken = NULL;
173                 }
174
175                 ret = MEDIA_CONTENT_ERROR_NONE;
176         } else {
177                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
178                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
179         }
180
181         return ret;
182 }
183
184 int image_meta_get_exposure_time(image_meta_h image, char **exposure_time)
185 {
186         int ret = MEDIA_CONTENT_ERROR_NONE;
187         image_meta_s *_image = (image_meta_s*)image;
188
189         if (_image && exposure_time) {
190                 if (STRING_VALID(_image->exposure_time)) {
191                         *exposure_time = strdup(_image->exposure_time);
192                         media_content_retvm_if(*exposure_time == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
193                 } else {
194                         *exposure_time = NULL;
195                 }
196                 ret = MEDIA_CONTENT_ERROR_NONE;
197         } else {
198                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
199                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
200         }
201
202         return ret;
203 }
204
205 int image_meta_get_fnumber(image_meta_h image, double *fnumber)
206 {
207         int ret = MEDIA_CONTENT_ERROR_NONE;
208         image_meta_s *_image = (image_meta_s*)image;
209
210         if (_image && fnumber) {
211                 *fnumber = _image->fnumber;
212                 ret = MEDIA_CONTENT_ERROR_NONE;
213         } else {
214                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
215                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
216         }
217
218         return ret;
219 }
220
221 int image_meta_get_iso(image_meta_h image, int *iso)
222 {
223         int ret = MEDIA_CONTENT_ERROR_NONE;
224         image_meta_s *_image = (image_meta_s*)image;
225
226         if (_image && iso) {
227                 *iso = _image->iso;
228                 ret = MEDIA_CONTENT_ERROR_NONE;
229         } else {
230                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
231                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
232         }
233
234         return ret;
235 }
236
237 int image_meta_get_model(image_meta_h image, char **model)
238 {
239         int ret = MEDIA_CONTENT_ERROR_NONE;
240         image_meta_s *_image = (image_meta_s*)image;
241
242         if (_image && model) {
243                 if (STRING_VALID(_image->model)) {
244                         *model = strdup(_image->model);
245                         media_content_retvm_if(*model == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
246                 } else {
247                         *model = NULL;
248                 }
249                 ret = MEDIA_CONTENT_ERROR_NONE;
250         } else {
251                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
252                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
253         }
254
255         return ret;
256 }
257
258 int image_meta_set_orientation(image_meta_h image, media_content_orientation_e orientation)
259 {
260         int ret = MEDIA_CONTENT_ERROR_NONE;
261         media_content_warn("DEPRECATION WARNING: image_meta_set_orientation() is deprecated and will be removed from next release.");
262         image_meta_s *_image = (image_meta_s*)image;
263
264         if (_image == NULL) {
265                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
266                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
267         }
268
269         if ((orientation < MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE) || (orientation > MEDIA_CONTENT_ORIENTATION_ROT_270)) {
270                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
271                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
272         }
273
274         _image->orientation = orientation;
275
276         return ret;
277 }
278
279 int image_meta_update_to_db(image_meta_h image)
280 {
281         int ret = MEDIA_CONTENT_ERROR_NONE;
282         media_content_warn("DEPRECATION WARNING: image_meta_update_to_db() is deprecated and will be removed from next release.");
283         image_meta_s *_image = (image_meta_s*)image;
284         char *sql = NULL;
285         char *storage_id = NULL;
286
287         if (_image != NULL && STRING_VALID(_image->media_id)) {
288                 ret = _media_db_get_storage_id_by_media_id(_image->media_id, &storage_id);
289                 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
290
291                 sql = sqlite3_mprintf(UPDATE_IMAGE_META_FROM_MEDIA, storage_id, _image->orientation, _image->media_id);
292                 ret = _content_query_sql(sql);
293                 SQLITE3_SAFE_FREE(sql);
294                 SAFE_FREE(storage_id);
295         } else {
296                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
297                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
298         }
299
300         return ret;
301 }