Add user path to system session path
[platform/core/api/thumbnail-util.git] / src / thumbnail_util.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 #include <media-thumbnail.h>
18 #include <media-util.h>
19 #include <thumbnail_util.h>
20 #include <thumbnail_util_private.h>
21 #include <storage.h>
22
23 #define MAX_SIZE 16
24 #define MAX_PATH_SIZE 4096
25
26 int __thumbnail_util_replace_path(const char *path, char *replace_path)
27 {
28         int ret = THUMBNAIL_UTIL_ERROR_NONE;
29         char *old_path = NULL;
30
31         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
32         if (ret != STORAGE_ERROR_NONE) {
33                 thumbnail_util_error("storage_get_directory failed");
34                 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
35         }
36
37         if (strncmp(path, old_path, strlen(old_path)) == 0) {
38                 thumbnail_util_sec_debug("Old path[%s]", path);
39                 snprintf(replace_path, MAX_PATH_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(old_path));
40         } else {
41                 snprintf(replace_path, MAX_PATH_SIZE, "%s", path);
42         }
43
44         SAFE_FREE(old_path);
45
46         if (!STRING_VALID(replace_path)) {
47                 thumbnail_util_error("replace failed");
48                 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
49         }
50
51         return THUMBNAIL_UTIL_ERROR_NONE;
52 }
53
54 int __thumbnail_util_error_capi(int content_error)
55 {
56         /*Error None*/
57         if (content_error == MS_MEDIA_ERR_NONE)
58                 return THUMBNAIL_UTIL_ERROR_NONE;
59
60         /* Internal operation error*/
61         else if ((content_error == MS_MEDIA_ERR_INVALID_PARAMETER) ||
62                 (content_error == MS_MEDIA_ERR_INVALID_PATH) ||
63                 (content_error == MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST))
64                 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
65
66         else if (content_error == MS_MEDIA_ERR_OUT_OF_MEMORY)
67                 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
68
69         /* IPC operation error*/
70         else if ((content_error <= MS_MEDIA_ERR_SOCKET_CONN) && (content_error >= MS_MEDIA_ERR_SOCKET_INTERNAL))
71                 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
72
73         /* MEDIA SERVER error*/
74         else if (content_error == MS_MEDIA_ERR_PERMISSION_DENIED)
75                 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
76
77         /*ETC*/
78         return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
79 }
80
81 void __thumbnail_util_convert_itoa(int request_id, char **req_str)
82 {
83         char buf[MAX_SIZE] = {0, };
84
85         snprintf(buf, MAX_SIZE, "%d", request_id);
86         *req_str = strndup(buf, strlen(buf));
87 }
88 void __thumbnail_util_extract_completed_cb(int error, int request_id, const char *path, int thumb_width, int thumb_height, unsigned char *thumb_data, int thumb_size, void *user_data)
89 {
90         thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)user_data;
91         char *request_id_str = NULL;
92
93         if (_thumb_cb != NULL) {
94                 __thumbnail_util_convert_itoa(request_id, &request_id_str);
95                 if (_thumb_cb->thumb_extract_cb)
96                         _thumb_cb->thumb_extract_cb(error, request_id_str, thumb_width, thumb_height, thumb_data, thumb_size, _thumb_cb->user_data);
97
98         }
99         SAFE_FREE(_thumb_cb);
100 }
101
102 int thumbnail_util_create(thumbnail_h *thumb)
103 {
104         int ret = THUMBNAIL_UTIL_ERROR_NONE;
105
106         if (thumb == NULL) {
107                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
108                 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
109         }
110
111         thumbnail_s *_thumb = (thumbnail_s *)malloc(sizeof(thumbnail_s));
112         if (_thumb == NULL) {
113                 thumbnail_util_error("OUT_OF_MEMORY(0x%08x)", THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY);
114                 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
115         }
116
117         _thumb->request_id = 0;
118         _thumb->file_path = NULL;
119         _thumb->dst_width = 0;
120         _thumb->dst_height = 0;
121         _thumb->thumb_size = 0;
122         _thumb->thumb_data = NULL;
123
124         *thumb = (thumbnail_h)_thumb;
125
126         return ret;
127 }
128
129 int thumbnail_util_extract(thumbnail_h thumb, thumbnail_extracted_cb callback, void *user_data, char **request_id)
130 {
131         int ret = THUMBNAIL_UTIL_ERROR_NONE;
132         int res = 0;
133         static int g_thumbnail_req_id = 0;
134         thumbnail_s *_thumb = (thumbnail_s *)thumb;
135
136         if (_thumb != NULL && STRING_VALID(_thumb->file_path)) {
137                 g_thumbnail_req_id++;
138                 _thumb->request_id = g_thumbnail_req_id;
139                 __thumbnail_util_convert_itoa(_thumb->request_id, request_id);
140                 thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)calloc(1, sizeof(thumbnail_extract_cb_s));
141
142                 if (_thumb_cb != NULL) {
143                         _thumb_cb->handle = _thumb;
144                         _thumb_cb->user_data = user_data;
145                         _thumb_cb->thumb_extract_cb = callback;
146                 }
147
148                 if (_thumb->dst_width == 0 || _thumb->dst_height == 0) {
149                         _thumb->dst_width = 320;
150                         _thumb->dst_height = 240;
151                 }
152
153                 res = thumbnail_request_extract_raw_data_async(_thumb->request_id, _thumb->file_path, _thumb->dst_width, _thumb->dst_height, (ThumbRawFunc)__thumbnail_util_extract_completed_cb, (void *)_thumb_cb, tzplatform_getuid(TZ_USER_NAME));
154                 ret = __thumbnail_util_error_capi(res);
155         } else {
156                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
157                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
158         }
159
160         return ret;
161 }
162
163 int thumbnail_util_set_path(thumbnail_h thumb, const char *path)
164 {
165         int ret = THUMBNAIL_UTIL_ERROR_NONE;
166         char repl_path[MAX_PATH_SIZE + 1] = {0, };
167         thumbnail_s *_thumb = (thumbnail_s *)thumb;
168
169         if (_thumb != NULL && path != NULL) {
170                 SAFE_FREE(_thumb->file_path);
171                 memset(repl_path, 0, sizeof(repl_path));
172                 ret = __thumbnail_util_replace_path(path, repl_path);
173                 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
174                         thumbnail_util_error("Convert path failed");
175                         _thumb->file_path = NULL;
176                 } else {
177                         _thumb->file_path = strndup(repl_path, strlen(repl_path));
178                 }
179         } else {
180                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
181                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
182         }
183
184         return ret;
185 }
186
187 int thumbnail_util_set_size(thumbnail_h thumb, int width, int height)
188 {
189         int ret = THUMBNAIL_UTIL_ERROR_NONE;
190         thumbnail_s *_thumb = (thumbnail_s *)thumb;
191
192         if (_thumb != NULL && width > 0 && height > 0) {
193                 _thumb->dst_width = width;
194                 _thumb->dst_height = height;
195         } else {
196                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
197                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
198         }
199
200         return ret;
201 }
202
203 int thumbnail_util_cancel(thumbnail_h thumb, const char *request_id)
204 {
205         int ret = THUMBNAIL_UTIL_ERROR_NONE;
206         thumbnail_s *_thumb = (thumbnail_s *)thumb;
207
208         if (_thumb != NULL && STRING_VALID(request_id)) {
209                 unsigned int request_id_integer = atoi(request_id);
210                 ret = thumbnail_request_cancel_raw_data(request_id_integer);
211         } else {
212                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
213                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
214         }
215
216         return ret;
217 }
218
219 int thumbnail_util_cancel_all(thumbnail_h thumb)
220 {
221         int ret = THUMBNAIL_UTIL_ERROR_NONE;
222         thumbnail_s *_thumb = (thumbnail_s *)thumb;
223
224         if (_thumb != NULL) {
225                 ret = thumbnail_request_cancel_all(true);
226         } else {
227                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
228                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
229         }
230
231         return ret;
232 }
233
234
235 int thumbnail_util_destroy(thumbnail_h thumb)
236 {
237         int ret = THUMBNAIL_UTIL_ERROR_NONE;
238         thumbnail_s *_thumb = (thumbnail_s *)thumb;
239
240         if (_thumb) {
241                 SAFE_FREE(_thumb->file_path);
242                 SAFE_FREE(_thumb->thumb_data);
243                 SAFE_FREE(_thumb);
244         } else {
245                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
246                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
247         }
248
249         return ret;
250 }