6b0d1231e6f54aa6ea131c072a46b5849292efb9
[framework/multimedia/libmedia-service.git] / src / visual / media-svc-util.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-svc-util.h"
23 #include "media-svc-debug.h"
24 #include "media-svc-error.h"
25 #include "minfo-types.h"
26 #include "media-svc-structures.h"
27
28 #include <vconf.h>
29 #include <vconf-keys.h>
30 #include <drm-service.h>
31 #include <string.h>
32 #include <aul/aul.h>
33
34 bool _mb_svc_get_file_display_name(const char *file_path, char *file_name)
35 {
36         char *result = NULL;
37
38         if ((result = strrchr(file_path, '/'))) {
39                 strncpy(file_name, (result + 1), MB_SVC_FILE_NAME_LEN_MAX + 1);
40                 return TRUE;
41         }
42
43         strncpy(file_name, file_path, MB_SVC_FILE_NAME_LEN_MAX + 1);
44         file_name[MB_SVC_FILE_NAME_LEN_MAX] = '\0';
45
46         return TRUE;
47 }
48
49 bool _mb_svc_get_file_parent_path(const char *file_path, char *parent_path)
50 {
51         char file_name[MB_SVC_FILE_NAME_LEN_MAX + 1] = { 0 };
52
53         _mb_svc_get_file_display_name(file_path, file_name);
54         strncpy(parent_path, file_path, MB_SVC_FILE_PATH_LEN_MAX + 1);
55         parent_path[strlen(file_path) - strlen(file_name) - 1] = '\0';
56
57         if (strlen(parent_path) == 0) {
58                 strncpy(parent_path, "/", MB_SVC_FILE_PATH_LEN_MAX + 1);
59         }
60
61         return TRUE;
62 }
63
64 bool _mb_svc_get_dir_display_name(const char *dir_path, char *dir_name)
65 {
66         char path[MB_SVC_DIR_PATH_LEN_MAX + 1] = { 0 };
67         char *result = NULL;
68
69         strncpy(path, dir_path, sizeof(path));
70
71         if ((result = strrchr(path, '/'))) {
72                 if (*(result + 1) == '\0') {
73                         *result = '\0';
74                 }
75         }
76
77         if (strncmp(dir_path, MB_SVC_PATH_MMC, strlen(dir_path)) == 0
78             || strncmp(dir_path, MB_SVC_PATH_PHONE, strlen(dir_path)) == 0) {
79                 mb_svc_debug("dir path is empty because path is root");
80                 strncpy(dir_name, "", MB_SVC_FILE_NAME_LEN_MAX + 1);
81                 return true;
82         }
83
84         if ((result = strrchr(path, '/'))) {
85                 strncpy(dir_name, (result + 1), MB_SVC_FILE_NAME_LEN_MAX + 1);
86                 return TRUE;
87         }
88
89         strncpy(dir_name, path, MB_SVC_FILE_NAME_LEN_MAX + 1);
90         dir_name[MB_SVC_FILE_NAME_LEN_MAX] = '\0';
91
92         return TRUE;
93 }
94
95 /*
96 ** in this funtion, if dir_path equals "/01/02/03", parent path is "/01/02/", not "01/02"
97 ** if dir_path equals "/01", its parent path is "/"
98 */
99 bool _mb_svc_get_dir_parent_path(const char *dir_path, char *parent_path)
100 {
101         char dir_name[MB_SVC_DIR_PATH_LEN_MAX + 1] = { 0 };
102
103         _mb_svc_get_dir_display_name(dir_path, dir_name);
104         strncpy(parent_path, dir_path, MB_SVC_DIR_PATH_LEN_MAX + 1);
105         parent_path[strlen(parent_path) - strlen(dir_name)] = '\0';
106         mb_svc_debug("parent_path is %s", parent_path);
107         return TRUE;
108 }
109
110 int _mb_svc_get_file_dir_modified_date(const char *full_path)
111 {
112         struct stat statbuf = { 0 };
113         int fd = 0;
114         int err = 0;
115
116         fd = stat(full_path, &statbuf);
117         if (fd == -1) {
118                 err = errno;
119                 mb_svc_debug("stat(%s) fails. err[%d]", full_path, err);
120                 return MB_SVC_ERROR_INTERNAL;
121         }
122
123         return statbuf.st_mtime;
124 }
125
126 bool
127 _mb_svc_get_full_path(const char *path, minfo_store_type storage_type,
128                       char *full_path)
129 {
130         if (path == NULL || full_path == NULL) {
131                 mb_svc_debug("path == NULL || full_path == NULL ");
132                 return false;
133         }
134
135         switch (storage_type) {
136         case MINFO_MMC:
137                 strncpy(full_path, MB_SVC_PATH_MMC, MB_SVC_FILE_PATH_LEN_MAX + 1);
138                 break;
139         case MINFO_PHONE:
140         default:
141                 strncpy(full_path, MB_SVC_PATH_PHONE, MB_SVC_FILE_PATH_LEN_MAX + 1);
142                 break;
143         }
144         if (strncmp(path, "/", MB_SVC_FILE_PATH_LEN_MAX + 1) != 0) {
145                 strcat(full_path, path);
146         }
147
148         return true;
149 }
150
151 bool _mb_svc_is_valid_path(const char *full_path)
152 {
153         char phone_root_path[MB_SVC_DIR_PATH_LEN_MAX + 1] = { 0 };
154         char mmc_root_path[MB_SVC_DIR_PATH_LEN_MAX + 1] = { 0 };
155
156         if (strlen(full_path) == 0) {
157                 return FALSE;
158         }
159
160         _mb_svc_get_full_path("/", MINFO_PHONE, phone_root_path);
161         _mb_svc_get_full_path("/", MINFO_MMC, mmc_root_path);
162
163         if (strncmp(full_path, phone_root_path, strlen(phone_root_path)) == 0) {
164                 /* like "/mnt/ums/.message" isn't valid mesage, shoud filter */
165                 if (strlen(full_path) > strlen(phone_root_path) + 1 && full_path[strlen(phone_root_path) + 1] == '.') {
166                         return FALSE;
167                 } else {
168                         return TRUE;
169                 }
170         }
171
172         if (strncmp(full_path, mmc_root_path, strlen(mmc_root_path)) == 0) {
173                 return TRUE;
174         }
175
176         return FALSE;
177 }
178
179 int _mb_svc_get_store_type_by_full(const char *full_path)
180 {
181         if (full_path != NULL) {
182                 if (strncmp
183                     (full_path, MB_SVC_PATH_PHONE,
184                      strlen(MB_SVC_PATH_PHONE)) == 0) {
185                         return MINFO_PHONE;
186                 } else
187                     if (strncmp
188                         (full_path, MB_SVC_PATH_MMC,
189                          strlen(MB_SVC_PATH_MMC)) == 0) {
190                         return MINFO_MMC;
191                 }
192         }
193
194         return MB_SVC_ERROR_INTERNAL;
195 }
196
197 int _mb_svc_get_rel_path_by_full(const char *full_path, char *path)
198 {
199         int root_len = 0;
200         minfo_store_type store_type = 0;
201
202         store_type = _mb_svc_get_store_type_by_full(full_path);
203
204         switch (store_type) {
205         case MINFO_PHONE:
206                 root_len = strlen(MB_SVC_PATH_PHONE);
207                 break;
208         case MINFO_MMC:
209                 root_len = strlen(MB_SVC_PATH_MMC);
210                 break;
211         default:
212                 return MB_SVC_ERROR_INTERNAL;
213         }
214         if (*(full_path + root_len) != '\0') {
215                 strncpy(path, full_path + root_len, MB_SVC_FILE_PATH_LEN_MAX + 1);
216         } else {
217                 strncpy(path, "/", MB_SVC_FILE_PATH_LEN_MAX + 1);
218         }
219
220         return 0;
221 }
222
223 bool _mb_svc_get_file_ext(const char *file_path, char *file_ext)
224 {
225         int i = 0;
226
227         for (i = strlen(file_path); i >= 0; i--) {
228                 if ((file_path[i] == '.') && (i < MB_SVC_FILE_PATH_LEN_MAX)) {
229                         strncpy(file_ext, &file_path[i + 1],
230                                 MB_SVC_FILE_EXT_LEN_MAX + 1);
231                         return TRUE;
232                 }
233
234                 if (file_path[i] == '/') { /* meet the dir. no ext */
235                         return TRUE;
236                 }
237         }
238         return TRUE;
239 }
240
241 bool _mb_svc_glist_free(GList **glist, bool is_free_element)
242 {
243         int length = 0;
244         int i = 0;
245         void *p = NULL;
246
247         if (*glist == NULL) {
248                 return TRUE;
249         }
250
251         if (is_free_element) {
252                 length = g_list_length(*glist);
253                 for (i = 0; i < length; i++) {
254                         p = g_list_nth_data(*glist, i);
255                         free(p);
256                         p = NULL;
257                 }
258         }
259
260         if (*glist != NULL) {
261                 g_list_free(*glist);
262                 *glist = NULL;
263         }
264         return TRUE;
265 }
266
267 int _mb_svc_get_file_type(const char *file_full_path)
268 {
269         int ret = 0;
270         char mimetype[255];
271
272         if (file_full_path == NULL)
273                 return MB_SVC_ERROR_INVALID_PARAMETER;
274
275         if (drm_svc_is_drm_file(file_full_path) == DRM_TRUE) {
276                 DRM_FILE_TYPE drm_type = DRM_FILE_TYPE_NONE;
277                 drm_type = drm_svc_get_drm_type(file_full_path);
278                 if (drm_type == DRM_FILE_TYPE_NONE) {
279                         return MINFO_ITEM_NONE;
280                 } 
281                 else {
282                         drm_content_info_t contentInfo = { 0 };
283
284                         ret = drm_svc_get_content_info(file_full_path, &contentInfo);
285                         if (ret != DRM_RESULT_SUCCESS) {
286                                 mb_svc_debug("drm_svc_get_content_info() fails. ");
287                                 return MINFO_ITEM_NONE;
288                         }
289
290                         strncpy(mimetype, contentInfo.contentType, sizeof(mimetype));
291                 }
292         } else {
293                 /* get content type and mime type from file. */
294                 ret =
295                         aul_get_mime_from_file(file_full_path, mimetype, sizeof(mimetype));
296                 if (ret < 0) {
297                         mb_svc_debug
298                                 ("aul_get_mime_from_file fail.. Now trying to get type by extension");
299         
300                         char ext[MB_SVC_FILE_EXT_LEN_MAX + 1] = { 0 };
301                         _mb_svc_get_file_ext(file_full_path, ext);
302         
303                         if (strcasecmp(ext, "JPG") == 0 ||
304                                 strcasecmp(ext, "JPEG") == 0 ||
305                                 strcasecmp(ext, "PNG") == 0 ||
306                                 strcasecmp(ext, "GIF") == 0 ||
307                                 strcasecmp(ext, "AGIF") == 0 ||
308                                 strcasecmp(ext, "XWD") == 0 ||
309                                 strcasecmp(ext, "BMP") == 0 ||
310                                 strcasecmp(ext, "TIF") == 0 ||
311                                 strcasecmp(ext, "TIFF") == 0 ||
312                                 strcasecmp(ext, "WBMP") == 0) {
313                                 return MINFO_ITEM_IMAGE;
314                         } else if (strcasecmp(ext, "AVI") == 0 ||
315                                 strcasecmp(ext, "MPEG") == 0 ||
316                                 strcasecmp(ext, "MP4") == 0 ||
317                                 strcasecmp(ext, "DCF") == 0 ||
318                                 strcasecmp(ext, "WMV") == 0 ||
319                                 strcasecmp(ext, "ASF") == 0 ||
320                                 strcasecmp(ext, "DIVX") == 0 ||
321                                 strcasecmp(ext, "3GPP") == 0 ||
322                                 strcasecmp(ext, "3GP") == 0) {
323                                 return MINFO_ITEM_VIDEO;
324                         } else {
325                                 return MINFO_ITEM_NONE;
326                         }
327                 }
328         }
329
330         mb_svc_debug("mime type : %s", mimetype);
331
332         /* categorize from mimetype */
333         if (strstr(mimetype, "image") != NULL) {
334                 return MINFO_ITEM_IMAGE;
335         } else if (strstr(mimetype, "video") != NULL) {
336                 return MINFO_ITEM_VIDEO;
337         }
338
339         return MINFO_ITEM_NONE;
340 }
341