a2ac8569b89ca2cfbe032d4fa0beb2d52d4dc07b
[platform/core/multimedia/libmedia-service.git] / src / common / 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 <unistd.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <string.h>
26 #include <sys/vfs.h>
27 #include <glib/gstdio.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <dirent.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <aul/aul.h>
34 #include <mm_file.h>
35 #include <mm_error.h>
36 #include <libexif/exif-data.h>
37 #include <media-thumbnail.h>
38 #include <media-util.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include "uuid.h"
42 #include "media-util-err.h"
43 #include "media-svc-util.h"
44 #include "media-svc-db-utils.h"
45 #include "media-svc-debug.h"
46 #include "media-svc-env.h"
47 #include "media-svc-hash.h"
48 #include "media-svc-album.h"
49 #include "media-svc-localize_ch.h"
50
51 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**<  Maximum file ext lenth*/
52 #define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
53
54 typedef enum {
55         MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
56         MEDIA_SVC_EXTRACTED_FIELD_TITLE                         = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
57         MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
58         MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
59         MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
60         MEDIA_SVC_EXTRACTED_FIELD_ARTIST                        = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
61         MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
62         MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
63         MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
64         MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
65         MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
66 } media_svc_extracted_field_e;
67
68
69 char *_media_info_generate_uuid(void)
70 {
71         uuid_t uuid_value;
72         static char uuid_unparsed[50];
73
74         uuid_generate(uuid_value);
75         uuid_unparse(uuid_value, uuid_unparsed);
76
77         //media_svc_debug("UUID : %s", uuid_unparsed);
78         return uuid_unparsed;
79 }
80
81 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
82 {
83         if (!x_src || strlen(x_src) == 0) {
84                 media_svc_error("x_src is NULL");
85                 return;
86         }
87
88         if (max_len < 1) {
89                 media_svc_error("length is Wrong");
90                 return;
91         }
92
93     strncpy(x_dst, x_src, max_len-1);
94         x_dst[max_len-1] = '\0';
95 }
96
97 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
98 {
99         int len = 0;
100
101         if (!STRING_VALID(src)) {
102                 media_svc_error("invalid src");
103                 return MS_MEDIA_ERR_INVALID_PARAMETER;
104         }
105
106         SAFE_FREE(*dst);
107
108         len = strlen(src) + 1;
109         *dst = malloc(len);
110
111         if (*dst == NULL) {
112                 media_svc_error("malloc failed");
113                 return MS_MEDIA_ERR_INTERNAL;
114         }
115
116         strncpy(*dst, src, len);
117         char *p = *dst;
118         p[len - 1] = '\0';
119
120         return MS_MEDIA_ERR_NONE;
121 }
122
123 static void __media_svc_split_to_double(char *input, double *arr, int *num)
124 {
125         char tmp_arr[255] = { 0, };
126         int len = strlen(input);
127         int i = 0, idx = 0, tmp_idx = 0;
128         int is_prev_space = 0;
129
130         for (;;) {
131                 if (input[len - 1] == ' ') {
132                         len--;
133                 } else {
134                         break;
135                 }
136         }
137
138         for (i = 0; i < len; i++) {
139                 if (idx > 2) {
140                         break;
141                 }
142
143                 if (input[i] == ' ') {
144                         if (is_prev_space == 1) {
145                                 continue;
146                         }
147                         if (idx <= 2) {
148                                 arr[idx++] = atof(tmp_arr);
149                         }
150                         tmp_idx = 0;
151                         is_prev_space = 1;
152                         continue;
153                 }
154
155                 tmp_arr[tmp_idx] = input[i];
156                 tmp_arr[++tmp_idx] = '\0';
157                 is_prev_space = 0;
158         }
159
160         if (i == len) {
161                 if (idx <= 2) {
162                         arr[idx++] = atof(tmp_arr);
163                 }
164                 *num = idx;
165                 return;
166         } else {
167                 *num = idx--;
168                 return;
169         }
170 }
171
172 static int __media_svc_get_exif_info(ExifData *ed,
173                                                                                 char *buf,
174                                                                                 int *i_value,
175                                                                                 double *d_value,
176                                                                                 int ifdtype,
177                                                                                 long tagtype)
178 {
179         ExifEntry *entry;
180         ExifIfd ifd;
181         ExifTag tag;
182
183         if (ed == NULL) {
184                 //media_svc_debug("ExifData is NULL");
185                 return MS_MEDIA_ERR_INVALID_PARAMETER;
186         }
187
188         ifd = ifdtype;
189         tag = tagtype;
190
191         entry = exif_data_get_entry(ed, tag);
192         if (entry) {
193                 /* Get the contents of the tag in human-readable form */
194                 if (tag == EXIF_TAG_ORIENTATION ||
195                                 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
196                                 tag == EXIF_TAG_PIXEL_Y_DIMENSION) {
197
198                         if (i_value == NULL) {
199                                 media_svc_error("i_value is NULL");
200                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
201                         }
202
203                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
204                         short exif_value = exif_get_short(entry->data, mByteOrder);
205                         //media_svc_debug("%s : %d", exif_tag_get_name_in_ifd(tag,ifd), exif_value);
206                         *i_value = (int)exif_value;
207
208                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
209
210                         if (d_value == NULL) {
211                                 media_svc_error("d_value is NULL");
212                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
213                         }
214
215                         /* Get the contents of the tag in human-readable form */
216                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
217                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
218                         gps_buf[strlen(gps_buf)] = '\0';
219
220                         //media_svc_debug("%s: %s\n", exif_tag_get_name_in_ifd(tag, ifd), gps_buf);
221
222                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
223                         int count = 0;
224
225                         __media_svc_split_to_double(gps_buf, tmp_arr, &count);
226                         if (count != 3) {
227                                 media_svc_error("Value is invalid");
228                                 return MS_MEDIA_ERR_INTERNAL;
229                         }
230
231                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
232                         //media_svc_debug("GPS value is %f", *d_value);
233                 } else {
234
235                         if (buf == NULL) {
236                                 media_svc_error("buf is NULL");
237                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
238                         }
239
240                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
241                         buf[strlen(buf)] = '\0';
242
243                         if (*buf) {
244                                 media_svc_debug("%s: %s\n", exif_tag_get_name_in_ifd(tag, ifd), buf);
245                         }
246                 }
247         }
248
249         return MS_MEDIA_ERR_NONE;
250 }
251
252 unsigned int _media_svc_get_current_time(void)
253 {
254         struct timeval  t;
255         unsigned int tval = 0;
256         gettimeofday(&t, NULL);
257
258         tval = t.tv_sec*1000000L + t.tv_usec;
259
260         return tval/1000;
261 }
262
263 int _media_svc_check_escape_char(char ch)
264 {
265         int i;
266         char escape_char[3] = {'%', '_' ,'#'};
267
268         for (i = 0; i < 3; i++) {
269                 if (ch == escape_char[i]) {
270                         return 1;
271                 }
272         }
273
274         return 0;
275 }
276
277 char *_media_svc_escape_str(char *input, int len)
278 {
279         int i = 0;
280         int j = 0;
281         char *result = NULL;
282
283         result = (char*)malloc(len * 2 * sizeof(char) + 1);
284         if (result == NULL) {
285                 return NULL;
286         }
287
288         for (i = 0; i < len; i++, j++) {
289                 if (input[i] == '\0') break;
290
291                 if (_media_svc_check_escape_char(input[i])) {
292                         result[j] = '#';
293                         result[++j] = input[i];
294                 } else {
295                         result[j] = input[i];
296                 }
297         }
298
299         result[j] = '\0';
300
301         return result;
302 }
303
304 int _media_svc_rename_file( const char *old_name, const char *new_name)
305 {
306         if((old_name == NULL) || (new_name == NULL))
307         {
308                 media_svc_error("invalid file name");
309                 return MS_MEDIA_ERR_INVALID_PARAMETER;
310         }
311
312         if (rename(old_name, new_name) < 0) {
313                 media_svc_error("Old : [%s] New : [%s] errno : [%s]", old_name, new_name, strerror(errno));
314                 return MS_MEDIA_ERR_INTERNAL;
315         }
316
317         return MS_MEDIA_ERR_NONE;
318 }
319
320 bool _media_svc_remove_file(const char *path)
321 {
322         int result = -1;
323
324         result = remove(path);
325         if (result == 0) {
326                 media_svc_debug("success to remove file");
327                 return TRUE;
328         } else {
329                 media_svc_error("fail to remove file result errno = %s", strerror(errno));
330                 return FALSE;
331         }
332 }
333
334 int _media_svc_remove_all_files_in_dir(const char *dir_path)
335 {
336         struct dirent *entry = NULL;
337         struct stat st;
338         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0};
339         DIR *dir = NULL;
340
341         dir = opendir(dir_path);
342         if (dir == NULL) {
343                 media_svc_error("%s is not exist", dir_path);
344                 return MS_MEDIA_ERR_INVALID_PARAMETER;
345         }
346
347         while ((entry = readdir(dir)) != NULL) {
348                 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
349                         continue;
350                 }
351                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry->d_name);
352
353                 if (stat(filename, &st) != 0) {
354                         continue;
355                 }
356                 if (S_ISDIR(st.st_mode)) {
357                         continue;
358                 }
359                 if (unlink(filename) != 0) {
360                         media_svc_error("failed to remove : %s", filename);
361                         closedir(dir);
362                         return MS_MEDIA_ERR_INTERNAL;
363                 }
364         }
365
366         closedir(dir);
367         return MS_MEDIA_ERR_NONE;
368 }
369
370 char *_media_svc_get_title_from_filepath (const char *path)
371 {
372         char *filename = NULL;
373         char *title = NULL;
374         char    *ext = NULL;
375         int filename_len = -1;
376         int new_title_len = -1;
377
378         if (!path) {
379                 media_svc_error("path is NULL");
380                 return NULL;
381         }
382
383         filename = g_path_get_basename(path);
384         if (!STRING_VALID(filename)) {
385                 media_svc_error("wrong file name");
386                 SAFE_FREE(filename);
387                 return NULL;
388         }
389
390         filename_len = strlen(filename);
391
392         ext = g_strrstr(filename, ".");
393         if (!ext) {
394                 media_svc_error("there is no file extention");
395                 return filename;
396         }
397
398         new_title_len = filename_len - strlen(ext);
399         if (new_title_len < 1) {
400                 media_svc_error("title length is zero");
401                 SAFE_FREE(filename);
402                 return NULL;
403         }
404
405         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
406
407         SAFE_FREE(filename);
408
409         media_svc_debug("extract title is [%s]", title);
410
411         return title;
412 }
413
414 static int _mkdir(const char *dir, mode_t mode) {
415         char tmp[256];
416         char *p = NULL;
417         size_t len;
418
419         snprintf(tmp, sizeof(tmp),"%s",dir);
420         len = strlen(tmp);
421         if(tmp[len - 1] == '/')
422                 tmp[len - 1] = 0;
423         for(p = tmp + 1; *p; p++)
424                 if(*p == '/') {
425                         *p = 0;
426                         mkdir(tmp, mode);
427                         *p = '/';
428                 }
429         return mkdir(tmp, mode);
430 }
431
432 static char* _media_svc_get_thumb_path(uid_t uid)
433 {
434         char *result_psswd = NULL;
435         struct group *grpinfo = NULL;
436         if(uid == getuid())
437         {
438                 result_psswd = strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
439                 grpinfo = getgrnam("users");
440                 if(grpinfo == NULL) {
441                         media_svc_error("getgrnam(users) returns NULL !");
442                         return NULL;
443                 }
444         }
445         else
446         {
447                 struct passwd *userinfo = getpwuid(uid);
448                 if(userinfo == NULL) {
449                         media_svc_error("getpwuid(%d) returns NULL !", uid);
450                         return NULL;
451                 }
452                 grpinfo = getgrnam("users");
453                 if(grpinfo == NULL) {
454                         media_svc_error("getgrnam(users) returns NULL !");
455                         return NULL;
456                 }
457                 // Compare git_t type and not group name
458                 if (grpinfo->gr_gid != userinfo->pw_gid) {
459                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
460                         return NULL;
461                 }
462                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb", userinfo->pw_dir);
463         }
464
465         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
466
467         return result_psswd;
468 }
469
470 char* _media_svc_get_thumb_internal_path(uid_t uid)
471 {
472         char *result_psswd = NULL;
473         struct group *grpinfo = NULL;
474         if(uid == getuid())
475         {
476                 result_psswd = strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
477                 grpinfo = getgrnam("users");
478                 if(grpinfo == NULL) {
479                         media_svc_error("getgrnam(users) returns NULL !");
480                         return NULL;
481                 }
482         }
483         else
484         {
485                 struct passwd *userinfo = getpwuid(uid);
486                 if(userinfo == NULL) {
487                         media_svc_error("getpwuid(%d) returns NULL !", uid);
488                         return NULL;
489                 }
490                 grpinfo = getgrnam("users");
491                 if(grpinfo == NULL) {
492                         media_svc_error("getgrnam(users) returns NULL !");
493                         return NULL;
494                 }
495                 // Compare git_t type and not group name
496                 if (grpinfo->gr_gid != userinfo->pw_gid) {
497                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
498                         return NULL;
499                 }
500                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
501         }
502
503         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
504
505         return result_psswd;
506 }
507
508 char* _media_svc_get_thumb_external_path(uid_t uid)
509 {
510         char *result_psswd = NULL;
511         struct group *grpinfo = NULL;   
512         if(uid == getuid())
513         {
514                 result_psswd = strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
515                 grpinfo = getgrnam("users");
516                 if(grpinfo == NULL) {
517                         media_svc_error("getgrnam(users) returns NULL !");
518                         return NULL;
519                 }
520         }
521         else
522         {
523                 struct passwd *userinfo = getpwuid(uid);
524                 if(userinfo == NULL) {
525                         media_svc_error("getpwuid(%d) returns NULL !", uid);
526                         return NULL;
527                 }
528                 grpinfo = getgrnam("users");
529                 if(grpinfo == NULL) {
530                         media_svc_error("getgrnam(users) returns NULL !");
531                         return NULL;
532                 }
533                 // Compare git_t type and not group name
534                 if (grpinfo->gr_gid != userinfo->pw_gid) {
535                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
536                         return NULL;
537                 }
538                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/mmc", userinfo->pw_dir);
539         }
540
541         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
542
543         return result_psswd;
544 }
545
546 int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
547 {
548         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
549
550         if (!image) {
551                 media_svc_error("invalid image..");
552                 return MS_MEDIA_ERR_INVALID_PARAMETER;
553         }
554
555         struct statfs fs;
556         if (-1 == statfs(_media_svc_get_thumb_path(uid), &fs)) {
557                 media_svc_error("error in statfs");
558                 return MS_MEDIA_ERR_INTERNAL;
559         }
560
561         long bsize_kbytes = fs.f_bsize >> 10;
562
563         if ((bsize_kbytes * fs.f_bavail) < 1024) {
564                 media_svc_error("not enought space...");
565                 return MS_MEDIA_ERR_INTERNAL;
566         }
567
568         FILE *fp = NULL;
569         int nwrite = -1;
570         if (image != NULL && size > 0) {
571                 fp = fopen(image_path, "w");
572
573                 if (fp == NULL) {
574                         media_svc_error("failed to open file");
575                         return MS_MEDIA_ERR_INTERNAL;
576                 }
577
578                 nwrite = fwrite(image, 1, size, fp);
579                 if (nwrite != size) {
580                         media_svc_error("failed to write thumbnail");
581                         fclose(fp);
582                         return MS_MEDIA_ERR_INTERNAL;
583                 }
584                 fclose(fp);
585         }
586
587         return MS_MEDIA_ERR_NONE;
588 }
589
590
591 bool _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
592 {
593         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0};
594         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0};
595         char *thumb_dir = NULL;
596         char hash[255 + 1];
597         char *thumbfile_ext = NULL;
598
599         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? _media_svc_get_thumb_internal_path(uid) : _media_svc_get_thumb_external_path(uid);
600
601         memset(file_ext, 0, sizeof(file_ext));
602         if (!_media_svc_get_file_ext(pathname, file_ext)) {
603                 media_svc_error("get file ext fail");
604         }
605
606         int err = -1;
607         err = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
608         if (err < 0) {
609                 media_svc_error("mb_svc_generate_hash_code failed : %d", err);
610                 return FALSE;
611         }
612
613         //media_svc_debug("img format is [%s]", img_format);
614
615         if((strstr(img_format, "jpeg") != NULL) ||(strstr(img_format, "jpg") != NULL) ||(strstr(img_format, "JPG") != NULL)) {
616                 thumbfile_ext = "jpg";
617         } else if((strstr(img_format, "png") != NULL) ||(strstr(img_format, "PNG") != NULL)) {
618                 thumbfile_ext = "png";
619         } else if((strstr(img_format, "gif") != NULL) ||(strstr(img_format, "GIF") != NULL)) {
620                 thumbfile_ext = "gif";
621         } else if((strstr(img_format, "bmp") != NULL) ||(strstr(img_format, "BMP") != NULL)) {
622                 thumbfile_ext = "bmp";
623         } else {
624                 media_svc_error("Not proper img format");
625                 return FALSE;
626         }
627
628         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
629         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
630         //media_svc_debug("thumb_path is [%s]", thumb_path);
631
632         return TRUE;
633 }
634
635 bool _media_svc_get_file_ext(const char *file_path, char *file_ext)
636 {
637         int i = 0;
638
639         for (i = strlen(file_path); i >= 0; i--) {
640                 if (file_path[i] == '.') {
641                         _strncpy_safe(file_ext, &file_path[i+1], MEDIA_SVC_FILE_EXT_LEN_MAX);
642                         return true;
643                 }
644
645                 if (file_path[i] == '/') {
646                         return false;
647                 }
648         }
649         return false;
650 }
651
652 int _media_svc_get_file_time(const char *full_path)
653 {
654         struct stat statbuf;
655         int fd = 0;
656
657         memset(&statbuf, 0, sizeof(struct stat));
658         fd = stat(full_path, &statbuf);
659         if (fd == -1) {
660                  media_svc_error("stat(%s) fails.", full_path);
661                  return MS_MEDIA_ERR_INTERNAL;
662          }
663
664          return statbuf.st_mtime;
665 }
666
667 int _media_svc_set_media_info(media_svc_content_info_s *content_info, media_svc_storage_type_e storage_type,
668                           const char *path, media_svc_media_type_e *media_type, bool refresh, drm_content_info_s **drm_contentInfo)
669 {
670         int ret = MS_MEDIA_ERR_NONE;
671         char * media_uuid = NULL;
672         char * file_name = NULL;
673         struct stat st;
674         drm_bool_type_e drm_type = DRM_FALSE;
675         char mime_type[256] = {0};
676
677         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
678         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
679
680         memset(&st, 0, sizeof(struct stat));
681         if (stat(path, &st) == 0) {
682                 content_info->modified_time = st.st_mtime;
683                 content_info->timeline = content_info->modified_time;
684                 content_info->size = st.st_size;
685                 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
686         } else {
687                 media_svc_error("stat failed : %s", strerror(errno));
688         }
689
690         /* Set default GPS value before extracting meta information */
691         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
692         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
693         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
694
695         /* Set filename to title for all media */
696         char *title = NULL;
697         title = _media_svc_get_title_from_filepath(content_info->path);
698         if (title) {
699                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
700                 if(ret != MS_MEDIA_ERR_NONE)
701                         media_svc_error("strcpy error");
702                 SAFE_FREE(title);
703         } else {
704                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
705                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
706                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
707         }
708
709         /* Set default value before extracting meta information */
710         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
711         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
712
713         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
714         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
715
716         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
717         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
718
719         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
720         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
721
722         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
723         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
724
725         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
726         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
727
728         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
729         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
730
731         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
732         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
733
734         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
735         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
736
737         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
738         if(refresh) {
739                 media_svc_debug("refresh");
740                 return MS_MEDIA_ERR_NONE;
741         }
742
743         content_info->storage_type = storage_type;
744         time(&content_info->added_time);
745
746         media_uuid = _media_info_generate_uuid();
747         media_svc_retvm_if(media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Invalid UUID");
748
749         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
750         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
751
752         file_name = g_path_get_basename(path);
753         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
754         SAFE_FREE(file_name);
755         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
756
757         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
758         if drm_contentinfo is not NULL, the file is OMA DRM.*/
759         ret = _media_svc_get_mime_type(path, mime_type, &drm_type, drm_contentInfo);
760         if (ret < 0) {
761                 media_svc_error("media_svc_get_mime_type failed : %d (%s)", ret, path);
762                 return MS_MEDIA_ERR_INVALID_PARAMETER;
763         }
764
765         media_svc_error("mime [%s]", mime_type);
766         content_info->is_drm = drm_type;
767
768         ret = _media_svc_get_media_type(path, mime_type, media_type);
769         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
770         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
771                 media_svc_error("invalid media_type condition[%d]", *media_type);
772                 return MS_MEDIA_ERR_INVALID_PARAMETER;
773         }
774
775         ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
776         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
777
778         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
779
780         content_info->media_type = *media_type;
781
782         content_info->played_count = 0;
783         content_info->last_played_time= 0;
784         content_info->last_played_position= 0;
785         content_info->favourate= 0;
786         content_info->media_meta.rating = 0;
787
788         return MS_MEDIA_ERR_NONE;
789 }
790
791 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
792 {
793         if (content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
794                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
795                 return MS_MEDIA_ERR_INVALID_PARAMETER;
796         }
797
798         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
799         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
800         memset(buf, 0x00, sizeof(buf));
801         memset(description_buf, 0x00, sizeof(description_buf));
802
803         int ret = MS_MEDIA_ERR_NONE;
804         double value = 0.0;
805         int orient_value = 0;
806         int exif_width = 0;
807         int exif_height = 0;
808         ExifData *ed = NULL;
809
810         char *path = content_info->path;
811         if (path == NULL) {
812                 media_svc_error("path is NULL");
813                 return MS_MEDIA_ERR_INVALID_PARAMETER;
814         }
815
816         /* Load an ExifData object from an EXIF file */
817         ed = exif_data_new_from_file(path);
818
819         if (!ed) {
820                 media_svc_debug("There is no exif data in [ %s ]", path);
821         }
822
823         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
824                 if (strlen(buf) != 0) {
825                         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
826
827                                 if (strcmp(buf, "S") == 0) {
828                                         value = -1 * value;
829                                 }
830
831                                 content_info->media_meta.latitude = value;
832                         } else {
833                                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
834                         }
835                 } else {
836                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
837                 }
838         } else {
839                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
840         }
841
842         memset(buf, 0x00, sizeof(buf));
843
844         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
845                 if (strlen(buf) != 0) {
846                         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
847                                 if (strcmp(buf, "W") == 0) {
848                                         value = -1 * value;
849                                 }
850                                 content_info->media_meta.longitude = value;
851                         } else {
852                                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
853                         }
854                 } else {
855                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
856                 }
857         } else {
858                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
859         }
860
861         memset(buf, 0x00, sizeof(buf));
862
863         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
864                 if (strlen(description_buf) == 0) {
865                         //media_svc_debug("Use 'No description'");
866                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
867                         if(ret != MS_MEDIA_ERR_NONE)
868                                 media_svc_error("strcpy error");
869                 } else {
870                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
871                         if(ret != MS_MEDIA_ERR_NONE)
872                                 media_svc_error("strcpy error");
873                 }
874         } else {
875                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
876                 if(ret != MS_MEDIA_ERR_NONE)
877                         media_svc_error("strcpy error");
878         }
879
880         memset(buf, 0x00, sizeof(buf));
881
882         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
883                 if (strlen(buf) == 0) {
884                         //media_svc_debug("time  is NULL");
885                 } else {
886                         //media_svc_debug("time  is %s", buf);
887                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, buf);
888                         if(ret != MS_MEDIA_ERR_NONE) {
889                                 media_svc_error("strcpy error");
890                         } else {
891                                 /* This is same as recorded_date */
892                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
893                                 if(ret != MS_MEDIA_ERR_NONE)
894                                         media_svc_error("strcpy error");
895                         }
896                 }
897         }
898
899         /* Get orientation value from exif. */
900         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_IFD_0, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
901                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
902                         content_info->media_meta.orientation = orient_value;
903                 } else {
904                         content_info->media_meta.orientation = 0;
905                 }
906         } else {
907                 content_info->media_meta.orientation = 0;
908         }
909
910         /* Get width value from exif. */
911         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
912                 if (exif_width > 0) {
913                         content_info->media_meta.width = exif_width;
914                 } else {
915                         content_info->media_meta.width = 0;
916                 }
917         } else {
918                 content_info->media_meta.width = 0;
919         }
920
921         /* Get height value from exif. */
922         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
923                 if (exif_height > 0) {
924                         content_info->media_meta.height = exif_height;
925                 } else {
926                         content_info->media_meta.height = 0;
927                 }
928         } else {
929                 content_info->media_meta.height = 0;
930         }
931
932         content_info->media_meta.weather = NULL;
933
934         if (ed != NULL) exif_data_unref(ed);
935
936         /* Set filename to title for image media */
937         char *title = NULL;
938         title = _media_svc_get_title_from_filepath(content_info->path);
939         if (title) {
940                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
941                 if(ret != MS_MEDIA_ERR_NONE)
942                         media_svc_error("strcpy error");
943                 SAFE_FREE(title);
944         } else {
945                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
946         }
947
948 #if 0
949         /* Extracting thumbnail */
950         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
951         int width = 0;
952         int height = 0;
953
954         ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
955         if (ret < 0) {
956                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
957         } else {
958                 //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
959         }
960
961         content_info->media_meta.width = width;
962         content_info->media_meta.height = height;
963
964         if (STRING_VALID(thumb_path))
965                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
966         else
967                 content_info->thumbnail_path = NULL;
968
969         if(ret != MS_MEDIA_ERR_NONE)
970                 media_svc_error("strcpy error");
971 #endif
972         return MS_MEDIA_ERR_NONE;
973 }
974
975 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type, drm_content_info_s *drm_contentInfo, uid_t uid)
976 {
977         MMHandleType content = 0;
978         MMHandleType tag = 0;
979         char *p = NULL;
980         void *image = NULL;
981         int size = -1;
982         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
983         int mmf_error = MM_ERROR_NONE;
984         bool thumb_extracted_from_drm = FALSE;
985         char *err_attr_name = NULL;
986         char *title = NULL;
987         bool extract_thumbnail = FALSE;
988         bool append_album = FALSE;
989         int album_id = 0;
990         double gps_value = 0.0;
991         int ret = MS_MEDIA_ERR_NONE;
992         char *path = content_info->path;
993
994 #ifdef __SUPPORT_DRM
995         /*To do - code for DRM content*/
996         if (content_info->is_drm) {
997                 drm_file_type_e drm_file_type;
998
999                 ret = drm_get_file_type(path, &drm_file_type);
1000                 if (ret < 0) {
1001                         media_svc_error("drm_get_file_type falied : %d", ret);
1002                         drm_file_type = DRM_TYPE_UNDEFINED;
1003                 }
1004
1005                 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
1006                 if (drm_contentInfo != NULL) {
1007                         if (drm_file_type == DRM_TYPE_OMA_V1) {
1008                                 if (strlen(drm_contentInfo->title) > 0) {
1009                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
1010                                         if(ret != MS_MEDIA_ERR_NONE)
1011                                                 media_svc_error("strcpy error");
1012                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1013                                 }
1014
1015                                 if (strlen(drm_contentInfo->description) > 0) {
1016                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
1017                                         if(ret != MS_MEDIA_ERR_NONE)
1018                                                 media_svc_error("strcpy error");
1019                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
1020                                 }
1021                         } else if (drm_file_type == DRM_TYPE_OMA_V2) {
1022                                 if (strlen(drm_contentInfo->title) > 0) {
1023                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
1024                                         if(ret != MS_MEDIA_ERR_NONE)
1025                                                 media_svc_error("strcpy error");
1026                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1027                                 }
1028
1029                                 if (strlen(drm_contentInfo->description) > 0) {
1030                                         ret =  __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
1031                                         if(ret != MS_MEDIA_ERR_NONE)
1032                                                 media_svc_error("strcpy error");
1033                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
1034                                 }
1035
1036                                 if (strlen(drm_contentInfo->copyright) > 0) {
1037                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, drm_contentInfo->copyright);
1038                                         if(ret != MS_MEDIA_ERR_NONE)
1039                                                 media_svc_error("strcpy error");
1040                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT;
1041                                 }
1042
1043                                 if (strlen(drm_contentInfo->author) > 0) {
1044                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, drm_contentInfo->author);
1045                                         if(ret != MS_MEDIA_ERR_NONE)
1046                                                 media_svc_error("strcpy error");
1047                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, drm_contentInfo->author);
1048                                         if(ret != MS_MEDIA_ERR_NONE)
1049                                                 media_svc_error("strcpy error");
1050
1051                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1052                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_ARTIST;
1053                                 }
1054                         }
1055
1056                         if (!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) {
1057                                 title = _media_svc_get_title_from_filepath(path);
1058                                 if (title) {
1059                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1060                                         SAFE_FREE(title);
1061                                         if(ret != MS_MEDIA_ERR_NONE)
1062                                                 media_svc_error("strcpy error");
1063                                 } else {
1064                                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1065                                 }
1066                         }
1067
1068                         return MS_MEDIA_ERR_NONE;
1069                 } else {
1070                         media_svc_debug("Some Not OMA Content's metadata is not incrypted so fileinfo can extracted metadata");
1071                 }
1072         }
1073 #endif
1074
1075         /*Get Content attribute ===========*/
1076         mmf_error = mm_file_create_content_attrs(&content, content_info->path);
1077
1078         if (mmf_error == MM_ERROR_NONE) {
1079                 /*Common attribute*/
1080                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1081                 if (mmf_error != MM_ERROR_NONE) {
1082                         SAFE_FREE(err_attr_name);
1083                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1084                 } else {
1085                         //media_svc_debug("duration : %d", content_info->media_meta.duration);
1086                 }
1087
1088                 /*Sound/Music attribute*/
1089                 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1090
1091                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1092                         if (mmf_error != MM_ERROR_NONE) {
1093                                 SAFE_FREE(err_attr_name);
1094                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1095                         } else {
1096                                 //media_svc_debug("bit rate : %d", content_info->media_meta.bitrate);
1097                         }
1098
1099                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1100                         if (mmf_error != MM_ERROR_NONE) {
1101                                 SAFE_FREE(err_attr_name);
1102                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1103                         } else {
1104                                 //media_svc_debug("sample rate : %d", content_info->media_meta.samplerate);
1105                         }
1106
1107                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1108                         if (mmf_error != MM_ERROR_NONE) {
1109                                 SAFE_FREE(err_attr_name);
1110                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1111                         } else {
1112                                 //media_svc_debug("channel : %d", content_info->media_meta.channel);
1113                         }
1114
1115                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1116                         if (mmf_error != MM_ERROR_NONE) {
1117                                 SAFE_FREE(err_attr_name);
1118                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1119                         } else {
1120                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1121                         }
1122                 }else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1123                         int audio_bitrate = 0;
1124                         int video_bitrate = 0;
1125
1126                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1127                         if (mmf_error != MM_ERROR_NONE) {
1128                                 SAFE_FREE(err_attr_name);
1129                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1130                         } else {
1131                                 //media_svc_debug("audio bit rate : %d", audio_bitrate);
1132                         }
1133
1134                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1135                         if (mmf_error != MM_ERROR_NONE) {
1136                                 SAFE_FREE(err_attr_name);
1137                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1138                         } else {
1139                                 //media_svc_debug("video bit rate : %d", video_bitrate);
1140                         }
1141
1142                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1143
1144                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1145                         if (mmf_error != MM_ERROR_NONE) {
1146                                 SAFE_FREE(err_attr_name);
1147                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1148                         } else {
1149                                 //media_svc_debug("width : %d", content_info->media_meta.width);
1150                         }
1151
1152                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1153                         if (mmf_error != MM_ERROR_NONE) {
1154                                 SAFE_FREE(err_attr_name);
1155                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1156                         } else {
1157                                 //media_svc_debug("height : %d", content_info->media_meta.height);
1158                         }
1159
1160                 } else {
1161                         media_svc_error("Not support type");
1162                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1163                 }
1164
1165                 mmf_error = mm_file_destroy_content_attrs(content);
1166                 if (mmf_error != MM_ERROR_NONE) {
1167                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1168                 }
1169         } else {
1170                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1171         }
1172
1173         /*Get Content Tag attribute ===========*/
1174         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1175
1176         if (mmf_error == MM_ERROR_NONE) {
1177                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1178                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1179                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1180                         if(ret != MS_MEDIA_ERR_NONE)
1181                                 media_svc_error("strcpy error");
1182
1183                         //media_svc_debug("album[%d] : %s", size, content_info->media_meta.album);
1184                 } else {
1185                         SAFE_FREE(err_attr_name);
1186                         //media_svc_debug("album - unknown");
1187                 }
1188
1189                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1190                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1191                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1192                         if(ret != MS_MEDIA_ERR_NONE)
1193                                 media_svc_error("strcpy error");
1194                         //media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist);
1195                 } else {
1196                         SAFE_FREE(err_attr_name);
1197                         //media_svc_debug("artist - unknown");
1198                 }
1199
1200                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1201                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1202                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1203                         if(ret != MS_MEDIA_ERR_NONE)
1204                                 media_svc_error("strcpy error");
1205
1206                         //media_svc_debug("genre : %s", content_info->media_meta.genre);
1207                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1208                         /*
1209                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1210                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1211                         }
1212                         */
1213                 } else {
1214                         SAFE_FREE(err_attr_name);
1215                         //media_svc_debug("genre - unknown");
1216                 }
1217
1218                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1219                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1220                         if(!isspace(*p)) {
1221                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1222                                 if(ret != MS_MEDIA_ERR_NONE)
1223                                         media_svc_error("strcpy error");
1224
1225                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1226                         }
1227                         else {
1228                                 int idx = 0;
1229
1230                                 for(idx = 0; idx < size; idx++) {
1231                                         if(isspace(*p)) {
1232                                                 media_svc_debug("SPACE [%s]", p);
1233                                                 p++;
1234                                                 continue;
1235                                         } else {
1236                                                 media_svc_debug("Not SPACE [%s]", p);
1237                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1238                                                 if(ret != MS_MEDIA_ERR_NONE)
1239                                                         media_svc_error("strcpy error");
1240                                                 break;
1241                                         }
1242                                 }
1243
1244                                 if(idx == size)
1245                                 {
1246                                         media_svc_debug("Can't extract title. All string is space");
1247                                         title = _media_svc_get_title_from_filepath(content_info->path);
1248                                         if (title) {
1249                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1250                                                 if(ret != MS_MEDIA_ERR_NONE)
1251                                                         media_svc_error("strcpy error");
1252                                                 SAFE_FREE(title);
1253                                         } else {
1254                                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1255                                         }
1256                                 }
1257                         }
1258                 } else {
1259                         SAFE_FREE(err_attr_name);
1260                         title = _media_svc_get_title_from_filepath(content_info->path);
1261                         if (title) {
1262                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1263                                 if(ret != MS_MEDIA_ERR_NONE)
1264                                         media_svc_error("strcpy error");
1265                                 SAFE_FREE(title);
1266                         } else {
1267                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1268                         }
1269                 }
1270
1271                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1272                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1273                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1274                         if(ret != MS_MEDIA_ERR_NONE)
1275                                 media_svc_error("strcpy error");
1276                         //media_svc_debug("desc : %s", content_info->media_meta.description);
1277                 } else {
1278                         SAFE_FREE(err_attr_name);
1279                 }
1280
1281                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1282                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1283                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1284                         if(ret != MS_MEDIA_ERR_NONE) {
1285                                 media_svc_error("strcpy error");
1286                         } else {
1287                                 /* This is same as datetaken */
1288                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, p);
1289                                 if(ret != MS_MEDIA_ERR_NONE)
1290                                         media_svc_error("strcpy error");
1291                         }
1292                         //media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date);
1293                 } else {
1294                         SAFE_FREE(err_attr_name);
1295                 }
1296
1297                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1298                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1299                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1300                         if(ret != MS_MEDIA_ERR_NONE)
1301                                 media_svc_error("strcpy error");
1302                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1303                         //media_svc_debug("extract composer from content : %s", content_info->media_meta.composer);
1304                 } else {
1305                         //media_svc_debug("composer - unknown");
1306                         SAFE_FREE(err_attr_name);
1307                 }
1308
1309                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1310                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1311                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1312                         if(ret != MS_MEDIA_ERR_NONE)
1313                                 media_svc_error("strcpy error");
1314                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1315                         //media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright);
1316                 } else {
1317                         //media_svc_debug("copyright - unknown");
1318                         SAFE_FREE(err_attr_name);
1319                 }
1320
1321                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1322                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1323                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1324                         if(ret != MS_MEDIA_ERR_NONE)
1325                                 media_svc_error("strcpy error");
1326                 } else {
1327                         SAFE_FREE(err_attr_name);
1328                 }
1329
1330                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1331                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == MM_ERROR_NONE) && (size == 4)) {
1332                         int year = 0;
1333                         if((p != NULL) && (sscanf( p, "%d", &year))) {
1334                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1335                                 if(ret != MS_MEDIA_ERR_NONE)
1336                                         media_svc_error("strcpy error");
1337                         } else {
1338                                 media_svc_debug("Wrong Year Information [%s]", p);
1339                         }
1340                 } else {
1341                         SAFE_FREE(err_attr_name);
1342                 }
1343
1344                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1345                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1346                         content_info->media_meta.rating = atoi(p);
1347                 } else {
1348                         SAFE_FREE(err_attr_name);
1349                         content_info->media_meta.rating = 0;
1350                 }
1351
1352                 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1353                         /*Initialize album_id to 0. below code will set the album_id*/
1354                         content_info->album_id = album_id;
1355                         ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1356         
1357                         if (ret != MS_MEDIA_ERR_NONE) {
1358                                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1359                                         media_svc_debug("album does not exist. So start to make album art");
1360                                         extract_thumbnail = TRUE;
1361                                         append_album = TRUE;
1362                                 } else {
1363                                         extract_thumbnail = FALSE;
1364                                         append_album = FALSE;
1365                                 }
1366                         } else {
1367                                 content_info->album_id = album_id;
1368                                 append_album = FALSE;
1369         
1370                                 if((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1371                                         (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1372         
1373                                         media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1374                                         extract_thumbnail = TRUE;
1375                                 } else {
1376         
1377                                         media_svc_debug("album already exists. don't need to make album art");
1378                                         ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1379                                         media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, content_info);
1380                                         extract_thumbnail = FALSE;
1381                                 }
1382                         }
1383         
1384                         if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE)) {
1385                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1386                                 if (mmf_error != MM_ERROR_NONE) {
1387                                         media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1388                                         SAFE_FREE(err_attr_name);
1389                                 } else {
1390                                         //media_svc_debug("artwork size1 [%d]", size);
1391                                 }
1392         
1393                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1394                                 if (mmf_error != MM_ERROR_NONE) {
1395                                         media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1396                                         SAFE_FREE(err_attr_name);
1397                                 } else {
1398                                         //media_svc_debug("artwork size2 [%d]", size);
1399                                 }
1400                                 if (image != NULL && size > 0) {
1401                                         bool result = FALSE;
1402                                         int ret = MS_MEDIA_ERR_NONE;
1403                                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1404                                         int artwork_mime_size = -1;
1405         
1406                                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1407                                         if ((mmf_error == MM_ERROR_NONE) && (artwork_mime_size > 0)) {
1408                                                 result = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1409                                                 if (result == FALSE) {
1410                                                         media_svc_error("Fail to Get Thumbnail Path");
1411                                                 }
1412                                         } else {
1413                                                 SAFE_FREE(err_attr_name);
1414                                         }
1415         
1416                                         if(strlen(thumb_path) > 0)
1417                                         {
1418                                                 ret = _media_svc_save_image(image, size, thumb_path, uid);
1419                                                 if (ret != MS_MEDIA_ERR_NONE) {
1420                                                         media_svc_error("Fail to Save Thumbnail Image");
1421                                                 }
1422                                                 else {
1423                                                         ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1424                                                         if(ret != MS_MEDIA_ERR_NONE)
1425                                                                 media_svc_error("strcpy error");
1426                                                 }
1427                                         }
1428                                 }
1429                         }
1430         
1431                         if(append_album == TRUE) {
1432         
1433                                 if((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1434                                         (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1435                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1436                                 else
1437                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1438         
1439                                 content_info->album_id = album_id;
1440                         }
1441                 } else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1442                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
1443                         if (mmf_error == MM_ERROR_NONE) {
1444                                 if (gps_value == 0.0)
1445                                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1446                                 else
1447                                         content_info->media_meta.longitude = gps_value;
1448                         } else {
1449                                 SAFE_FREE(err_attr_name);
1450                         }
1451
1452                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
1453                         if (mmf_error == MM_ERROR_NONE) {
1454                                 if (gps_value == 0.0)
1455                                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1456                                 else
1457                                         content_info->media_meta.latitude = gps_value;
1458                         } else {
1459                                 SAFE_FREE(err_attr_name);
1460                         }
1461
1462                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
1463                         if (mmf_error == MM_ERROR_NONE) {
1464                                 if (gps_value == 0.0)
1465                                         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1466                                 else
1467                                         content_info->media_meta.altitude = gps_value;
1468                         } else {
1469                                 SAFE_FREE(err_attr_name);
1470                         }
1471 #if 0
1472                         //if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE))
1473                         {
1474                                 /* Extracting thumbnail */
1475                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1476                                 int width = 0;
1477                                 int height = 0;
1478
1479                                 ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
1480                                 if (ret < 0) {
1481                                         media_svc_error("thumbnail_request_from_db failed: %d", ret);
1482                                 } else {
1483                                         //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
1484                                 }
1485
1486                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1487                                 if(ret != MS_MEDIA_ERR_NONE)
1488                                         media_svc_error("strcpy error");
1489
1490                                 if (content_info->media_meta.width <= 0) content_info->media_meta.width = width;
1491                                 if (content_info->media_meta.height <= 0) content_info->media_meta.height = height;
1492                         }
1493 #endif
1494                 }
1495
1496                 mmf_error = mm_file_destroy_tag_attrs(tag);
1497                 if (mmf_error != MM_ERROR_NONE) {
1498                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1499                 }
1500         }       else {
1501                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1502                 char *title = NULL;
1503                 media_svc_error("no tag information");
1504
1505                 title = _media_svc_get_title_from_filepath(content_info->path);
1506                 if (title) {
1507                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1508                         if(ret != MS_MEDIA_ERR_NONE)
1509                                 media_svc_error("strcpy error");
1510                         SAFE_FREE(title);
1511                 } else {
1512                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1513                 }
1514
1515                 content_info->album_id = album_id;
1516         }
1517
1518         return MS_MEDIA_ERR_NONE;
1519 }
1520
1521 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1522 {
1523         media_svc_retm_if(content_info == NULL, "content info is NULL");
1524
1525         /* Delete media_svc_content_info_s */
1526         SAFE_FREE(content_info->media_uuid);
1527         SAFE_FREE(content_info->path);
1528         SAFE_FREE(content_info->file_name);
1529         SAFE_FREE(content_info->mime_type);
1530         SAFE_FREE(content_info->folder_uuid);
1531         SAFE_FREE(content_info->thumbnail_path);
1532
1533         /* Delete media_svc_content_meta_s */
1534         SAFE_FREE(content_info->media_meta.title);
1535         SAFE_FREE(content_info->media_meta.album);
1536         SAFE_FREE(content_info->media_meta.artist);
1537         SAFE_FREE(content_info->media_meta.album_artist);
1538         SAFE_FREE(content_info->media_meta.genre);
1539         SAFE_FREE(content_info->media_meta.composer);
1540         SAFE_FREE(content_info->media_meta.year);
1541         SAFE_FREE(content_info->media_meta.recorded_date);
1542         SAFE_FREE(content_info->media_meta.copyright);
1543         SAFE_FREE(content_info->media_meta.track_num);
1544         SAFE_FREE(content_info->media_meta.description);
1545         SAFE_FREE(content_info->media_meta.datetaken);
1546         SAFE_FREE(content_info->media_meta.weather);
1547
1548         SAFE_FREE(content_info->media_meta.title_pinyin);
1549         SAFE_FREE(content_info->media_meta.album_pinyin);
1550         SAFE_FREE(content_info->media_meta.artist_pinyin);
1551         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1552         SAFE_FREE(content_info->media_meta.genre_pinyin);
1553         SAFE_FREE(content_info->media_meta.composer_pinyin);
1554         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1555         SAFE_FREE(content_info->media_meta.description_pinyin);
1556
1557         return;
1558 }
1559
1560 int _media_svc_get_store_type_by_path(const char *path, media_svc_storage_type_e *storage_type)
1561 {
1562         if(STRING_VALID(path))
1563         {
1564                 if(strncmp(path, MEDIA_SVC_PATH_PHONE, strlen(MEDIA_SVC_PATH_PHONE)) == 0)
1565                 {
1566                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
1567                 }
1568                 else if(strncmp (path, MEDIA_SVC_PATH_MMC, strlen(MEDIA_SVC_PATH_MMC)) == 0)
1569                 {
1570                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
1571                 }
1572         }
1573         else
1574         {
1575                 media_svc_error("INVALID parameter");
1576                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1577         }
1578
1579         return MS_MEDIA_ERR_NONE;
1580 }
1581
1582 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
1583 {
1584   char *result, *sr;
1585   size_t i, count = 0;
1586   size_t oldlen = strlen(olds); if (oldlen < 1) return s;
1587   size_t newlen = strlen(news);
1588
1589   if (newlen != oldlen) {
1590     for (i = 0; s[i] != '\0';) {
1591       if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
1592       else i++;
1593     }   
1594   } else i = strlen(s);
1595
1596
1597   result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
1598   if (result == NULL) return NULL;
1599
1600   sr = result;
1601   while (*s) {
1602     if (memcmp(s, olds, oldlen) == 0) {
1603       memcpy(sr, news, newlen);
1604       sr += newlen;
1605       s  += oldlen;
1606     } else *sr++ = *s++;
1607   }
1608
1609   *sr = '\0';
1610
1611   return result;
1612 }
1613
1614 bool _media_svc_is_drm_file(const char *path)
1615 {
1616 #ifdef __SUPPORT_DRM
1617         int ret;
1618         drm_bool_type_e is_drm_file = DRM_UNKNOWN;
1619
1620         ret = drm_is_drm_file(path,&is_drm_file);
1621         if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_drm_file)
1622                 return TRUE;
1623 #endif
1624         return FALSE;
1625 }
1626
1627 int _media_svc_get_mime_in_drm_info(const char *path, char *mime, drm_content_info_s **drm_contentInfo)
1628 {
1629 #ifdef __SUPPORT_DRM
1630         int ret = MS_MEDIA_ERR_NONE;
1631         drm_file_type_e file_type = DRM_TYPE_UNDEFINED;
1632
1633         if (path == NULL || mime == NULL)
1634                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1635
1636         ret = drm_get_file_type(path, &file_type);
1637         if (ret != DRM_RETURN_SUCCESS) {
1638                 media_svc_error("drm_get_file_type() failed : %d", ret);
1639                 return MS_MEDIA_ERR_INTERNAL;
1640         } else {
1641                 if (file_type == DRM_TYPE_OMA_V1
1642                 || file_type == DRM_TYPE_OMA_V2
1643                 || file_type == DRM_TYPE_OMA_PD) {
1644                         *drm_contentInfo = malloc(sizeof(drm_content_info_s));
1645                         memset(*drm_contentInfo,0x0,sizeof(drm_content_info_s));
1646                         ret = drm_get_content_info(path, *drm_contentInfo);
1647                         if (ret != DRM_RETURN_SUCCESS) {
1648                                 media_svc_error("drm_svc_get_content_info() fails :%d ", ret);
1649                                 free(*drm_contentInfo);
1650                                 *drm_contentInfo = NULL;
1651                                 return MS_MEDIA_ERR_INTERNAL;
1652                         }
1653
1654                         if (STRING_VALID((*drm_contentInfo)->mime_type)) {
1655                                 strncpy(mime,(*drm_contentInfo)->mime_type, MEDIA_SVC_METADATA_LEN_MAX);
1656                                 media_svc_debug("DRM contentType : %s",(*drm_contentInfo)->mime_type);
1657                         } else {
1658                                 free(*drm_contentInfo);
1659                                 *drm_contentInfo = NULL;
1660                                 return MS_MEDIA_ERR_INTERNAL;
1661                         }
1662                 } else {
1663                         media_svc_error("THIS IS DRM BUT YOU SHOULD USE API OF AUL LIBRARY");
1664                         *drm_contentInfo = NULL;
1665                         return MS_MEDIA_ERR_INTERNAL;
1666                 }
1667         }
1668
1669         return MS_MEDIA_ERR_NONE;
1670 #else
1671         *drm_contentInfo = NULL;
1672         return MS_MEDIA_ERR_INTERNAL;
1673 #endif
1674 }
1675
1676 int _media_svc_get_content_type_from_mime(const char * path, const char * mimetype, int * category)
1677 {
1678         int i = 0;
1679         int err = 0;
1680
1681         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
1682
1683         //media_svc_debug("mime type : %s", mimetype);
1684
1685         /*categorize from mimetype */
1686         for (i = 0; i < CONTENT_TYPE_NUM; i++) {
1687                 if (strstr(mimetype, content_category[i].content_type) != NULL) {
1688                         *category = (*category | content_category[i].category_by_mime);
1689                         break;
1690                 }
1691         }
1692
1693         /*in application type, exitst sound file ex) x-smafs */
1694         if (*category & MEDIA_SVC_CATEGORY_ETC) {
1695                 int prefix_len = strlen(content_category[0].content_type);
1696
1697                 for (i = 0; i < SOUND_MIME_NUM; i++) {
1698                         if (strstr(mimetype + prefix_len, sound_mime_table[i]) != NULL) {
1699                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
1700                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
1701                                 break;
1702                         }
1703                 }
1704         }
1705
1706         /*check music file in soun files. */
1707         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
1708                 int prefix_len = strlen(content_category[0].content_type) + 1;
1709
1710                 //media_svc_error("mime_type : %s", mimetype + prefix_len);
1711
1712                 for (i = 0; i < MUSIC_MIME_NUM; i++) {
1713                         if (strcmp(mimetype + prefix_len, music_mime_table[i]) == 0) {
1714                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
1715                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1716                                 break;
1717                         }
1718                 }
1719
1720                 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
1721                 if(strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
1722                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
1723                         *category |= MEDIA_SVC_CATEGORY_ETC;
1724                 }
1725         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
1726                 /*some video files don't have video stream. in this case it is categorize as music. */
1727                 char *ext;
1728                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
1729                 ext = strrchr(path, '.');
1730                 if (ext != NULL) {
1731                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
1732                                 int audio = 0;
1733                                 int video = 0;
1734
1735                                 err = mm_file_get_stream_info(path, &audio, &video);
1736                                 if (err == 0) {
1737                                         if (audio > 0 && video == 0) {
1738                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
1739                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1740                                         }
1741                                 }
1742                         }
1743                 }
1744         }
1745
1746         //media_svc_debug("category_from_ext : %d", *category);
1747
1748         return err;
1749 }
1750
1751 /*
1752 drm_contentifo is not NULL, if the file is OMA DRM.
1753 If the file is not OMA DRM, drm_contentinfo must be NULL.
1754 */
1755 int _media_svc_get_mime_type(const char *path, char *mimetype, drm_bool_type_e *is_drm, drm_content_info_s **drm_contentInfo)
1756 {
1757         int ret = MS_MEDIA_ERR_NONE;
1758
1759         if (path == NULL)
1760                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1761
1762 #ifdef __SUPPORT_DRM
1763         /* In case of drm file. */
1764         if (_media_svc_is_drm_file(path)) {
1765                 *is_drm = DRM_TRUE;
1766                 ret =  _media_svc_get_mime_in_drm_info(path, mimetype, drm_contentInfo);
1767                 if (ret == MS_MEDIA_ERR_NONE) {
1768                         return ret;
1769                 }
1770         }
1771 #else
1772         *is_drm = DRM_FALSE;
1773         *drm_contentInfo = NULL;
1774 #endif
1775
1776         /*in case of normal files or failure to get mime in drm */
1777         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
1778                 media_svc_error("aul_get_mime_from_file fail");
1779                 return MS_MEDIA_ERR_INTERNAL;
1780         }
1781
1782         return MS_MEDIA_ERR_NONE;
1783 }
1784
1785 int _media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
1786 {
1787         int ret = MS_MEDIA_ERR_NONE;
1788         int category = 0;
1789
1790         media_svc_media_type_e type;
1791
1792         ret = _media_svc_get_content_type_from_mime(path, mime_type, &category);
1793         if (ret < 0) {
1794                 media_svc_error("_media_svc_get_content_type_from_mime failed : %d", ret);
1795         }
1796
1797         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1798         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
1799         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
1800         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
1801         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1802
1803         *media_type = type;
1804
1805         return ret;
1806 }
1807
1808 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1809 {
1810         int ret = MS_MEDIA_ERR_NONE;
1811         int size = 0;
1812         pinyin_name_s *pinyinname = NULL;
1813         *pinyin_str = NULL;
1814
1815         if(!STRING_VALID(src_str))
1816         {
1817                 media_svc_debug("String is invalid");
1818                 return ret;
1819         }
1820
1821         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1822         if (ret == MS_MEDIA_ERR_NONE)
1823         {
1824                 if(STRING_VALID(pinyinname[0].pinyin_name))
1825                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1826                 else
1827                         *pinyin_str = strdup(src_str);  //Return Original Non China Character
1828         }
1829
1830         _media_svc_pinyin_free(pinyinname, size);
1831
1832         return ret;
1833 }
1834
1835 bool _media_svc_check_pinyin_support(void)
1836 {
1837         /*Check CSC*/
1838         return TRUE;
1839 }