Bug fix of getting location information in Exif
[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 <aul/aul.h>
33 #include <mm_file.h>
34 #include <mm_error.h>
35 #include <libexif/exif-data.h>
36 #include <media-thumbnail.h>
37 #include <media-util.h>
38 #include <uuid/uuid.h>
39 #include <img-codec-parser.h>
40 #include <grp.h>
41 #include <pwd.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-utils.h"
50 #include "media-svc-localize_ch.h"
51 #include "media-svc-localize_tw.h"
52
53 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**<  Maximum file ext lenth*/
54
55 /* Define data structures for media type and mime type */
56 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
57 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
58 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
59 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
60 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
61 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
62
63 #define CONTENT_TYPE_NUM 5
64 #define MUSIC_MIME_NUM 29
65 #define SOUND_MIME_NUM 1
66 #define MIME_TYPE_LENGTH 255
67 #define MIME_LENGTH 50
68 #define _3GP_FILE ".3gp"
69 #define _MP4_FILE ".mp4"
70 #define _ASF_FILE ".asf"
71
72 typedef struct {
73         char content_type[15];
74         int category_by_mime;
75 } _media_svc_content_table_s;
76
77 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
78         {"audio", MEDIA_SVC_CATEGORY_SOUND},
79         {"image", MEDIA_SVC_CATEGORY_IMAGE},
80         {"video", MEDIA_SVC_CATEGORY_VIDEO},
81         {"application", MEDIA_SVC_CATEGORY_ETC},
82         {"text", MEDIA_SVC_CATEGORY_ETC},
83 };
84
85 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
86         /*known mime types of normal files*/
87         "mpeg",
88         "ogg",
89         "x-ms-wma",
90         "x-flac",
91         "mp4",
92         /* known mime types of drm files*/
93         "mp3",
94         "x-mp3", /*alias of audio/mpeg*/
95         "x-mpeg", /*alias of audio/mpeg*/
96         "3gpp",
97         "x-ogg", /*alias of  audio/ogg*/
98         "vnd.ms-playready.media.pya:*.pya", /*playready*/
99         "wma",
100         "aac",
101         "x-m4a", /*alias of audio/mp4*/
102         /* below mimes are rare*/
103         "x-vorbis+ogg",
104         "x-flac+ogg",
105         "x-matroska",
106         "ac3",
107         "mp2",
108         "x-ape",
109         "x-ms-asx",
110         "vnd.rn-realaudio",
111
112         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
113         "vorbis", /*alias of audio/x-vorbis+ogg*/
114         "x-oggflac",
115         "x-mp2", /*alias of audio/mp2*/
116         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
117         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
118         "x-wav",
119 };
120
121 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
122         "x-smaf",
123 };
124
125 typedef enum {
126         MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
127         MEDIA_SVC_EXTRACTED_FIELD_TITLE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
128         MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
129         MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
130         MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
131         MEDIA_SVC_EXTRACTED_FIELD_ARTIST                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
132         MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
133         MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
134         MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
135         MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
136         MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
137         MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST          = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
138 } media_svc_extracted_field_e;
139
140 static char *_media_svc_get_thumb_path(uid_t uid);
141
142 char *_media_info_generate_uuid(void)
143 {
144         uuid_t uuid_value;
145         static char uuid_unparsed[50];
146
147         uuid_generate(uuid_value);
148         uuid_unparse(uuid_value, uuid_unparsed);
149
150         /*media_svc_debug("UUID : %s", uuid_unparsed); */
151         return uuid_unparsed;
152 }
153
154 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
155 {
156         if (!x_src || strlen(x_src) == 0) {
157                 media_svc_error("x_src is NULL");
158                 return;
159         }
160
161         if (max_len < 1) {
162                 media_svc_error("length is Wrong");
163                 return;
164         }
165
166         strncpy(x_dst, x_src, max_len - 1);
167         x_dst[max_len - 1] = '\0';
168 }
169
170 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
171 {
172         int len = 0;
173
174         if (!STRING_VALID(src)) {
175                 media_svc_error("invalid src");
176                 return MS_MEDIA_ERR_INVALID_PARAMETER;
177         }
178
179         SAFE_FREE(*dst);
180
181         len = strlen(src) + 1;
182         *dst = malloc(len);
183
184         if (*dst == NULL) {
185                 media_svc_error("malloc failed");
186                 return MS_MEDIA_ERR_INTERNAL;
187         }
188
189         strncpy(*dst, src, len);
190         char *p = *dst;
191         p[len - 1] = '\0';
192
193         return MS_MEDIA_ERR_NONE;
194 }
195
196 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
197 {
198         if (!STRING_VALID(src)) {
199                 media_svc_error("invalid src");
200                 return MS_MEDIA_ERR_INVALID_PARAMETER;
201         }
202
203         SAFE_FREE(*dst);
204
205         *dst = malloc(copysize + 1);
206
207         if (*dst == NULL) {
208                 media_svc_error("malloc failed");
209                 return MS_MEDIA_ERR_INTERNAL;
210         }
211
212         strncpy(*dst, src, copysize);
213         char *p = *dst;
214         p[copysize] = '\0';
215
216         return MS_MEDIA_ERR_NONE;
217 }
218
219 static int __media_svc_split_to_double(char *input, double *arr)
220 {
221         char tmp_arr[255] = {0, };
222         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
223
224         if (!STRING_VALID(input)) {
225                 media_svc_error("Invalid parameter");
226                 return MS_MEDIA_ERR_INVALID_PARAMETER;
227         }
228         memset(tmp_arr, 0x0, sizeof(tmp_arr));
229
230         /*media_svc_debug("input: [%s]", input); */
231
232         len = strlen(input);
233
234         for (idx = 0; idx < (len + 1); idx++) {
235                 if (input[idx] == ' ') {
236                         continue;
237                 } else if ((input[idx] == ',') || (idx == len)) {
238                         arr[arr_idx] = atof(tmp_arr);
239                         arr_idx++;
240                         str_idx = 0;
241                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
242                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
243                 } else {
244                         tmp_arr[str_idx] = input[idx];
245                         str_idx++;
246                 }
247         }
248
249         if (arr_idx != 3) {
250                 media_svc_error("Error when parsing GPS [%d]", arr_idx);
251                 return MS_MEDIA_ERR_INTERNAL;
252         }
253
254         return MS_MEDIA_ERR_NONE;
255 }
256
257 static int __media_svc_get_exif_info(ExifData *ed,
258                                      char *buf,
259                                      int *i_value,
260                                      double *d_value,
261                                      int ifdtype,
262                                      long tagtype)
263 {
264         ExifEntry *entry;
265         ExifTag tag;
266
267         if (ed == NULL) {
268                 /*media_svc_debug("ExifData is NULL"); */
269                 return MS_MEDIA_ERR_INVALID_PARAMETER;
270         }
271
272         tag = tagtype;
273
274         entry = exif_data_get_entry(ed, tag);
275         if (entry) {
276                 /* Get the contents of the tag in human-readable form */
277                 if (tag == EXIF_TAG_ORIENTATION ||
278                     tag == EXIF_TAG_PIXEL_X_DIMENSION ||
279                     tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
280                     tag == EXIF_TAG_ISO_SPEED_RATINGS) {
281
282                         if (i_value == NULL) {
283                                 media_svc_error("i_value is NULL");
284                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
285                         }
286
287                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
288                         short exif_value = exif_get_short(entry->data, mByteOrder);
289                         /*media_svc_debug("%s : %d", exif_tag_get_name_in_ifd(tag, ifd), exif_value); */
290                         *i_value = (int)exif_value;
291
292                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
293
294                         if (d_value == NULL) {
295                                 media_svc_error("d_value is NULL");
296                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
297                         }
298
299                         /* Get the contents of the tag in human-readable form */
300                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
301                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
302                         gps_buf[strlen(gps_buf)] = '\0';
303                         int ret = MS_MEDIA_ERR_NONE;
304                         /*media_svc_debug("%s: [%s]", exif_tag_get_name_in_ifd(tag, ifd), gps_buf); */
305
306                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
307
308                         ret = __media_svc_split_to_double(gps_buf, tmp_arr);
309                         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
310
311                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
312                         /*media_svc_debug("GPS value is [%f], %f, %f, %f", *d_value, tmp_arr[0], tmp_arr[1], tmp_arr[2]); */
313                 }  else if (tag == EXIF_TAG_EXPOSURE_TIME) {
314
315                         if (buf == NULL) {
316                                 media_svc_error("buf is NULL");
317                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
318                         }
319
320                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
321                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
322                         long numerator = mRational.numerator;
323                         long denominator = mRational.denominator;
324                         snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
325
326                 } else if (tag == EXIF_TAG_FNUMBER) {
327
328                         if (d_value == NULL) {
329                                 media_svc_error("d_value is NULL");
330                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
331                         }
332
333                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
334                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
335                         long numerator = mRational.numerator;
336                         long denominator = mRational.denominator;
337
338                         *d_value = ((numerator*1.0)/(denominator*1.0));
339
340                 } else {
341
342                         if (buf == NULL) {
343                                 media_svc_error("buf is NULL");
344                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
345                         }
346
347                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
348                         buf[strlen(buf)] = '\0';
349                 }
350         }
351
352         return MS_MEDIA_ERR_NONE;
353 }
354
355 time_t __media_svc_get_timeline_from_str(const char *timstr)
356 {
357         struct tm t;
358         time_t modified_t = 0;
359         time_t rawtime;
360         struct tm timeinfo;
361
362         if (!STRING_VALID(timstr)) {
363                 media_svc_error("Invalid Parameter");
364                 return 0;
365         }
366
367         /*Exif Format : %Y:%m:%d %H:%M:%S
368         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
369         memset(&t, 0x00, sizeof(struct tm));
370
371         tzset();
372         time(&rawtime);
373         localtime_r(&rawtime, &timeinfo);
374
375         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
376                 t.tm_isdst = timeinfo.tm_isdst;
377                 if (t.tm_isdst != 0) {
378                         media_svc_error("DST %d", t.tm_isdst);
379                 }
380
381                 modified_t = mktime(&t);
382                 if (modified_t > 0) {
383                         return modified_t;
384                 } else {
385                         media_svc_error("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
386                 }
387         } else {
388                 media_svc_error("Failed to get timeline : [%s]", timstr);
389         }
390
391         return 0;
392 }
393
394 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
395 {
396         int idx = 0;
397
398         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
399
400         /*categorize from mimetype */
401         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
402                 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
403                         *category = (*category | content_category[idx].category_by_mime);
404                         break;
405                 }
406         }
407
408         /*in application type, exitst sound file ex) x-smafs */
409         if (*category & MEDIA_SVC_CATEGORY_ETC) {
410                 int prefix_len = strlen(content_category[0].content_type);
411
412                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
413                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
414                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
415                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
416                                 break;
417                         }
418                 }
419
420                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
421                         *category ^= MEDIA_SVC_CATEGORY_ETC;
422                         *category |= MEDIA_SVC_CATEGORY_SOUND;
423                 }
424         }
425
426         /*check music file in soun files. */
427         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
428                 int prefix_len = strlen(content_category[0].content_type) + 1;
429
430                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
431                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
432                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
433                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
434                                 break;
435                         }
436                 }
437
438                 /*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*/
439                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
440                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
441                         *category |= MEDIA_SVC_CATEGORY_ETC;
442                 }
443         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
444                 /*some video files don't have video stream. in this case it is categorize as music. */
445                 char *ext = NULL;
446                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
447                 ext = strrchr(path, '.');
448                 if (ext != NULL) {
449                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0) || (strncasecmp(ext, _ASF_FILE, 5) == 0)) {
450                                 int audio = 0;
451                                 int video = 0;
452                                 int err = 0;
453
454                                 err = mm_file_get_stream_info(path, &audio, &video);
455                                 if (err == 0) {
456                                         if (audio > 0 && video == 0) {
457                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
458                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
459                                         }
460                                 }
461                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
462                         }
463                 }
464         }
465
466         return MS_MEDIA_ERR_NONE;
467 }
468
469 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
470 {
471         int ret = MS_MEDIA_ERR_NONE;
472         int category = 0;
473
474         media_svc_media_type_e type;
475
476         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
477         if (ret != MS_MEDIA_ERR_NONE) {
478                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
479         }
480
481         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
482         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
483         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
484         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
485         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
486
487         *media_type = type;
488
489         return ret;
490 }
491
492 /*
493 drm_contentifo is not NULL, if the file is OMA DRM.
494 If the file is not OMA DRM, drm_contentinfo must be NULL.
495 */
496 static int __media_svc_get_mime_type(const char *path, char *mimetype)
497 {
498         if (path == NULL)
499                 return MS_MEDIA_ERR_INVALID_PARAMETER;
500
501         /*in case of normal files or failure to get mime in drm */
502         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
503                 media_svc_error("aul_get_mime_from_file fail");
504                 return MS_MEDIA_ERR_INTERNAL;
505         }
506
507         return MS_MEDIA_ERR_NONE;
508 }
509
510 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
511 {
512         int i = 0;
513
514         for (i = strlen(file_path); i >= 0; i--) {
515                 if (file_path[i] == '.') {
516                         _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
517                         return true;
518                 }
519
520                 if (file_path[i] == '/') {
521                         return false;
522                 }
523         }
524         return false;
525 }
526
527 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
528 {
529         char *err_attr_name = NULL;
530         double gps_value = 0.0;
531         int mmf_error = MM_ERROR_NONE;
532
533         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
534         if (mmf_error == MM_ERROR_NONE) {
535                 if (longitude != NULL) {
536                         *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
537                 }
538         } else {
539                 SAFE_FREE(err_attr_name);
540         }
541
542         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
543         if (mmf_error == MM_ERROR_NONE) {
544                 if (latitude != NULL) {
545                         *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
546                 }
547         } else {
548                 SAFE_FREE(err_attr_name);
549         }
550
551         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
552         if (mmf_error == MM_ERROR_NONE) {
553                 if (altitude != NULL) {
554                         *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
555                 }
556         } else {
557                 SAFE_FREE(err_attr_name);
558         }
559
560         return MS_MEDIA_ERR_NONE;
561 }
562
563 static int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
564 {
565         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
566
567         if (!image) {
568                 media_svc_error("invalid image..");
569                 return MS_MEDIA_ERR_INVALID_PARAMETER;
570         }
571
572         struct statfs fs;
573         if (-1 == statfs(_media_svc_get_thumb_path(uid), &fs)) {
574                 media_svc_error("error in statfs");
575                 return MS_MEDIA_ERR_INTERNAL;
576         }
577
578         long bsize_kbytes = fs.f_bsize >> 10;
579
580         if ((bsize_kbytes * fs.f_bavail) < 1024) {
581                 media_svc_error("not enought space...");
582                 return MS_MEDIA_ERR_INTERNAL;
583         }
584
585         FILE *fp = NULL;
586         int nwrite = -1;
587         if (image != NULL && size > 0) {
588                 fp = fopen(image_path, "w");
589
590                 if (fp == NULL) {
591                         media_svc_error("failed to open file");
592                         return MS_MEDIA_ERR_INTERNAL;
593                 }
594
595                 nwrite = fwrite(image, 1, size, fp);
596                 if (nwrite != size) {
597                         media_svc_error("failed to write thumbnail");
598                         fclose(fp);
599                         return MS_MEDIA_ERR_INTERNAL;
600                 }
601                 fclose(fp);
602         }
603
604         return MS_MEDIA_ERR_NONE;
605 }
606
607 static char *_media_svc_get_title_from_filepath(const char *path)
608 {
609         char *filename = NULL;
610         char *title = NULL;
611         char    *ext = NULL;
612         int filename_len = -1;
613         int new_title_len = -1;
614
615         if (!path) {
616                 media_svc_error("path is NULL");
617                 return NULL;
618         }
619
620         filename = g_path_get_basename(path);
621         if (!STRING_VALID(filename)) {
622                 media_svc_error("wrong file name");
623                 SAFE_FREE(filename);
624                 return NULL;
625         }
626
627         filename_len = strlen(filename);
628
629         ext = g_strrstr(filename, ".");
630         if (!ext) {
631                 media_svc_error("there is no file extention");
632                 return filename;
633         }
634
635         new_title_len = filename_len - strlen(ext);
636         if (new_title_len < 1) {
637                 media_svc_error("title length is zero");
638                 SAFE_FREE(filename);
639                 return NULL;
640         }
641
642         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
643
644         SAFE_FREE(filename);
645
646         media_svc_debug("extract title is [%s]", title);
647
648         return title;
649 }
650
651 int _media_svc_rename_file(const char *old_name, const char *new_name)
652 {
653         if ((old_name == NULL) || (new_name == NULL)) {
654                 media_svc_error("invalid file name");
655                 return MS_MEDIA_ERR_INVALID_PARAMETER;
656         }
657
658         if (rename(old_name, new_name) < 0) {
659                 media_svc_stderror(" ");
660                 return MS_MEDIA_ERR_INTERNAL;
661         }
662
663         return MS_MEDIA_ERR_NONE;
664 }
665
666 int _media_svc_remove_file(const char *path)
667 {
668         int result = -1;
669
670         result = remove(path);
671         if (result == 0) {
672                 media_svc_debug("success to remove file");
673                 return MS_MEDIA_ERR_NONE;
674         } else {
675                 media_svc_stderror("fail to remove file result");
676                 return MS_MEDIA_ERR_INTERNAL;
677         }
678 }
679
680 int _media_svc_remove_all_files_in_dir(const char *dir_path)
681 {
682         struct dirent entry;
683         struct dirent *result;
684         struct stat st;
685         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0};
686         DIR *dir = NULL;
687
688         dir = opendir(dir_path);
689         if (dir == NULL) {
690                 media_svc_error("%s is not exist", dir_path);
691                 return MS_MEDIA_ERR_INVALID_PARAMETER;
692         }
693
694         while (!readdir_r(dir, &entry, &result)) {
695                 if (result == NULL)
696                         break;
697
698                 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
699                         continue;
700                 }
701                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
702
703                 if (stat(filename, &st) != 0) {
704                         continue;
705                 }
706                 if (S_ISDIR(st.st_mode)) {
707                         continue;
708                 }
709                 if (unlink(filename) != 0) {
710                         media_svc_stderror("failed to remove");
711                         closedir(dir);
712                         return MS_MEDIA_ERR_INTERNAL;
713                 }
714         }
715
716         closedir(dir);
717         return MS_MEDIA_ERR_NONE;
718 }
719
720 char *_media_svc_get_thumb_internal_path(uid_t uid)
721 {
722         char *result_psswd = NULL;
723         struct group *grpinfo = NULL;
724         if (uid == getuid()) {
725                 result_psswd = strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
726                 grpinfo = getgrnam("users");
727                 if (grpinfo == NULL) {
728                         media_svc_error("getgrnam(users) returns NULL !");
729                         if(result_psswd)
730                                 free(result_psswd);
731                         return NULL;
732                 }
733         } else {
734                 struct passwd *userinfo = getpwuid(uid);
735                 if (userinfo == NULL) {
736                         media_svc_error("getpwuid(%d) returns NULL !", uid);
737                         return NULL;
738                 }
739                 grpinfo = getgrnam("users");
740                 if (grpinfo == NULL) {
741                         media_svc_error("getgrnam(users) returns NULL !");
742                         return NULL;
743                 }
744                 /* Compare git_t type and not group name */
745                 if (grpinfo->gr_gid != userinfo->pw_gid) {
746                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
747                         return NULL;
748                 }
749                 asprintf(&result_psswd, "%s/share/media/.thumb/phone", userinfo->pw_dir);
750         }
751
752         return result_psswd;
753 }
754
755 char *_media_svc_get_thumb_external_path(uid_t uid)
756 {
757         char *result_psswd = NULL;
758         struct group *grpinfo = NULL;
759         if (uid == getuid()) {
760                 result_psswd = strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
761                 grpinfo = getgrnam("users");
762                 if (grpinfo == NULL) {
763                         media_svc_error("getgrnam(users) returns NULL !");
764                         if(result_psswd)
765                                 free(result_psswd);
766                         return NULL;
767                 }
768         } else {
769                 struct passwd *userinfo = getpwuid(uid);
770                 if (userinfo == NULL) {
771                         media_svc_error("getpwuid(%d) returns NULL !", uid);
772                         return NULL;
773                 }
774                 grpinfo = getgrnam("users");
775                 if (grpinfo == NULL) {
776                         media_svc_error("getgrnam(users) returns NULL !");
777                         return NULL;
778                 }
779                 /* Compare git_t type and not group name */
780                 if (grpinfo->gr_gid != userinfo->pw_gid) {
781                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
782                         return NULL;
783                 }
784                 asprintf(&result_psswd, "%s/share/media/.thumb/mmc", userinfo->pw_dir);
785         }
786
787         return result_psswd;
788 }
789
790 static int __media_svc_check_thumb_dir(const char *thumb_dir)
791 {
792         int ret = 0;
793         DIR *dir = NULL;
794
795         dir = opendir(thumb_dir);
796         if (dir != NULL) {
797                 closedir(dir);
798         } else {
799                 media_svc_stderror("opendir fail");
800                 if (errno == ENOENT) {
801                         media_svc_error("[%s] is not exit. So, make it", thumb_dir);
802                         ret = mkdir(thumb_dir, 0777);
803                         if (ret < 0) {
804                                 media_svc_error("make fail");
805                                 goto ERROR;
806                         }
807                 } else {
808                         goto ERROR;
809                 }
810
811                 ret = chmod(thumb_dir, 0777);
812                 if (ret != 0) {
813                         media_svc_stderror("chmod failed");
814                 }
815                 ret = chown(thumb_dir, 5000, 5000);
816                 if (ret != 0) {
817                         media_svc_stderror("chown failed");
818                 }
819         }
820
821         return MS_MEDIA_ERR_NONE;
822
823 ERROR:
824         return -1;
825 }
826
827 static char *_media_svc_get_thumb_path(uid_t uid)
828 {
829         char *result_psswd = NULL;
830         struct group *grpinfo = NULL;
831         if (uid == getuid()) {
832                 result_psswd = strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
833                 grpinfo = getgrnam("users");
834                 if (grpinfo == NULL) {
835                         media_svc_error("getgrnam(users) returns NULL !");
836                         if(result_psswd)
837                                 free(result_psswd);
838                         return NULL;
839                 }
840         } else {
841                 struct passwd *userinfo = getpwuid(uid);
842                 if (userinfo == NULL) {
843                         media_svc_error("getpwuid(%d) returns NULL !", uid);
844                         return NULL;
845                 }
846                 grpinfo = getgrnam("users");
847                 if (grpinfo == NULL) {
848                         media_svc_error("getgrnam(users) returns NULL !");
849                         return NULL;
850                 }
851                 /* Compare git_t type and not group name */
852                 if (grpinfo->gr_gid != userinfo->pw_gid) {
853                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
854                         return NULL;
855                 }
856                 asprintf(&result_psswd, "%s/share/media/.thumb", userinfo->pw_dir);
857         }
858
859         return result_psswd;
860 }
861
862 int _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)
863 {
864         int ret = MS_MEDIA_ERR_NONE;
865         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0};
866         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0};
867         char *thumb_dir = NULL;
868         char hash[255 + 1];
869         char *thumbfile_ext = NULL;
870
871         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? _media_svc_get_thumb_internal_path(uid) : _media_svc_get_thumb_external_path(uid);
872
873         ret = __media_svc_check_thumb_dir(thumb_dir);
874         if (ret != MS_MEDIA_ERR_NONE) {
875                 media_svc_error("__media_svc_check_thumb_dir");
876                 return MS_MEDIA_ERR_INTERNAL;
877         }
878
879         memset(file_ext, 0, sizeof(file_ext));
880         if (!__media_svc_get_file_ext(pathname, file_ext)) {
881                 media_svc_error("get file ext fail");
882         }
883
884         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
885         if (ret != MS_MEDIA_ERR_NONE) {
886                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
887                 return MS_MEDIA_ERR_INTERNAL;
888         }
889
890         /*media_svc_debug("img format is [%s]", img_format); */
891
892         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
893                 thumbfile_ext = (char *)"jpg";
894         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
895                 thumbfile_ext = (char *)"png";
896         } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
897                 thumbfile_ext = (char *)"gif";
898         } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
899                 thumbfile_ext = (char *)"bmp";
900         } else {
901                 media_svc_error("Not proper img format");
902                 return MS_MEDIA_ERR_INTERNAL;
903         }
904
905         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
906         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
907         /*media_svc_debug("thumb_path is [%s]", thumb_path); */
908
909         return MS_MEDIA_ERR_NONE;
910 }
911
912 int _media_svc_get_file_time(const char *full_path)
913 {
914         struct stat statbuf;
915         int fd = 0;
916
917         memset(&statbuf, 0, sizeof(struct stat));
918         fd = stat(full_path, &statbuf);
919         if (fd == -1) {
920                 media_svc_error("stat(%s) fails.", full_path);
921                 return MS_MEDIA_ERR_INTERNAL;
922         }
923
924         return statbuf.st_mtime;
925 }
926
927 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
928 {
929         int ret = MS_MEDIA_ERR_NONE;
930
931         /* Set default GPS value before extracting meta information */
932         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
933         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
934         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
935
936         /* Set filename to title for all 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");
946                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
947                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
948         }
949
950         /* Set default value before extracting meta information */
951         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
952         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
953
954         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
955         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
956
957         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
958         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
959
960         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
961         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
962
963         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
964         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
965
966         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
967         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
968
969         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
970         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
971
972         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
973         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
974
975         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
976         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
977
978         if(refresh) {
979                 media_svc_debug("refresh");
980                 return MS_MEDIA_ERR_NONE;
981         }
982
983         content_info->played_count = 0;
984         content_info->last_played_time= 0;
985         content_info->last_played_position= 0;
986         content_info->favourate= 0;
987         content_info->media_meta.rating = 0;
988
989         return MS_MEDIA_ERR_NONE;
990 }
991
992 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
993                           const char *path, media_svc_media_type_e *media_type, bool refresh)
994 {
995         int ret = MS_MEDIA_ERR_NONE;
996         char * media_uuid = NULL;
997         char * file_name = NULL;
998         struct stat st;
999         bool drm_type = false;
1000         char mime_type[256] = {0};
1001
1002         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1003         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1004
1005         memset(&st, 0, sizeof(struct stat));
1006         if (stat(path, &st) == 0) {
1007                 content_info->modified_time = st.st_mtime;
1008                 content_info->timeline = content_info->modified_time;
1009                 content_info->size = st.st_size;
1010                 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1011         } else {
1012                 media_svc_stderror("stat failed");
1013         }
1014
1015         _media_svc_set_default_value(content_info, refresh);
1016
1017         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1018         if (refresh) {
1019                 media_svc_debug("refresh");
1020                 return MS_MEDIA_ERR_NONE;
1021         }
1022
1023         ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1024         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1025
1026         content_info->storage_type = storage_type;
1027         time(&content_info->added_time);
1028
1029         media_uuid = _media_info_generate_uuid();
1030         if (media_uuid == NULL) {
1031                 _media_svc_destroy_content_info(content_info);
1032                 return MS_MEDIA_ERR_INTERNAL;
1033         }
1034
1035         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1036         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1037
1038         file_name = g_path_get_basename(path);
1039         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1040         SAFE_FREE(file_name);
1041         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1042
1043         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1044         if drm_contentinfo is not NULL, the file is OMA DRM.*/
1045         ret = __media_svc_get_mime_type(path, mime_type);
1046         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1047
1048         media_svc_debug("mime [%s]", mime_type);
1049         content_info->is_drm = drm_type;
1050
1051         ret = __media_svc_get_media_type(path, mime_type, media_type);
1052         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1053
1054         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1055                 media_svc_error("invalid media_type condition[%d]", *media_type);
1056                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1057         }
1058
1059         ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1060         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1061
1062         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1063
1064         content_info->media_type = *media_type;
1065
1066         return MS_MEDIA_ERR_NONE;
1067 }
1068
1069 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1070 {
1071         int ret = MS_MEDIA_ERR_NONE;
1072         double value = 0.0;
1073         int orient_value = 0;
1074         int exif_width = 0;
1075         int exif_height = 0;
1076         ExifData *ed = NULL;
1077         int has_datetaken = FALSE;
1078         int datetaken_size = 19;
1079         double fnumber = 0.0;
1080         int iso = 0;
1081         char *path = NULL;
1082
1083         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1084         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1085         char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1086         char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1087
1088         memset(buf, 0x00, sizeof(buf));
1089         memset(description_buf, 0x00, sizeof(description_buf));
1090         memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1091         memset(model_buf, 0x00, sizeof(model_buf));
1092
1093         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1094                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1095                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1096         }
1097
1098         path = content_info->path;
1099         if (!STRING_VALID(path)) {
1100                 media_svc_error("Invalid Path");
1101                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1102         }
1103
1104         /* Load an ExifData object from an EXIF file */
1105         ed = exif_data_new_from_file(path);
1106
1107         if (!ed) {
1108                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1109                 goto GET_WIDTH_HEIGHT;
1110         }
1111
1112         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1113                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1114                         if (strlen(buf) > 0) {
1115                                 if (strcmp(buf, "S") == 0) {
1116                                         value = -1 * value;
1117                                 }
1118                         }
1119                         content_info->media_meta.latitude = value;
1120                 } else {
1121                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1122                 }
1123         } else {
1124                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1125         }
1126
1127         memset(buf, 0x00, sizeof(buf));
1128
1129         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1130                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1131                         if (strlen(buf) > 0) {
1132                                 if (strcmp(buf, "W") == 0) {
1133                                         value = -1 * value;
1134                                 }
1135                         }
1136                         content_info->media_meta.longitude = value;
1137                 } else {
1138                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1139                 }
1140         } else {
1141                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1142         }
1143
1144         memset(buf, 0x00, sizeof(buf));
1145
1146         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1147                 if (strlen(description_buf) == 0) {
1148                         /*media_svc_debug("Use 'No description'"); */
1149                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1150                         if (ret != MS_MEDIA_ERR_NONE)
1151                                 media_svc_error("strcpy error");
1152                 } else {
1153                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1154                         if (ret != MS_MEDIA_ERR_NONE)
1155                                 media_svc_error("strcpy error");
1156                 }
1157         } else {
1158                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1159                 if (ret != MS_MEDIA_ERR_NONE)
1160                         media_svc_error("strcpy error");
1161         }
1162
1163         memset(buf, 0x00, sizeof(buf));
1164
1165         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1166                 if (strlen(buf) == 0) {
1167                         /*media_svc_debug("time  is NULL"); */
1168                 } else {
1169                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1170                         if (ret != MS_MEDIA_ERR_NONE) {
1171                                 media_svc_error("strcpy error");
1172                         } else {
1173                                 has_datetaken = TRUE;
1174                                 /* This is same as recorded_date */
1175                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1176                                 if (ret != MS_MEDIA_ERR_NONE)
1177                                         media_svc_error("strcpy error");
1178                         }
1179                 }
1180         }
1181
1182         memset(buf, 0x00, sizeof(buf));
1183
1184         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1185                 if (strlen(buf) == 0) {
1186                         /*media_svc_debug("time  is NULL"); */
1187                 } else {
1188                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1189                         if (ret != MS_MEDIA_ERR_NONE) {
1190                                 media_svc_error("strcpy error");
1191                         } else {
1192                                 has_datetaken = TRUE;
1193                                 /* This is same as recorded_date */
1194                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1195                                 if (ret != MS_MEDIA_ERR_NONE)
1196                                         media_svc_error("strcpy error");
1197                         }
1198                 }
1199         }
1200
1201         if (has_datetaken) {
1202                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1203                 if (content_info->timeline == 0) {
1204                         content_info->timeline = content_info->modified_time;
1205                 } else {
1206                         media_svc_debug("Timeline : %ld", content_info->timeline);
1207                 }
1208         }
1209
1210         /* Get exposure_time value from exif. */
1211         if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1212                 if (strlen(exposure_time_buf) == 0) {
1213                         //media_svc_debug("exposure_time_buf is NULL");
1214                 } else {
1215                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1216                         if (ret != MS_MEDIA_ERR_NONE)
1217                                 media_svc_error("strcpy error");
1218                 }
1219         }
1220
1221         /* Get fnumber value from exif. */
1222         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_IFD_0, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1223                 if (fnumber > 0.0) {
1224                         content_info->media_meta.fnumber = fnumber;
1225                 } else {
1226                         content_info->media_meta.fnumber = 0.0;
1227                 }
1228         } else {
1229                 content_info->media_meta.fnumber = 0.0;
1230         }
1231
1232         /* Get iso value from exif. */
1233         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_IFD_0, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1234                 if (iso > 0) {
1235                         content_info->media_meta.iso = iso;
1236                 } else {
1237                         content_info->media_meta.iso = 0;
1238                 }
1239         } else {
1240                 content_info->media_meta.iso = 0;
1241         }
1242
1243         /* Get model value from exif. */
1244         if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1245                 if (strlen(model_buf) == 0) {
1246                         //media_svc_debug("model_buf is NULL");
1247                 } else {
1248                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1249                         if (ret != MS_MEDIA_ERR_NONE)
1250                                 media_svc_error("strcpy error");
1251                 }
1252         }
1253
1254         /* Get orientation value from exif. */
1255         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_IFD_0, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1256                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1257                         content_info->media_meta.orientation = orient_value;
1258                 } else {
1259                         content_info->media_meta.orientation = 0;
1260                 }
1261         } else {
1262                 content_info->media_meta.orientation = 0;
1263         }
1264
1265         /* Get width value from exif. */
1266         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1267                 if (exif_width > 0) {
1268                         content_info->media_meta.width = exif_width;
1269                 } else {
1270                         content_info->media_meta.width = 0;
1271                 }
1272         } else {
1273                 content_info->media_meta.width = 0;
1274         }
1275
1276         /* Get height value from exif. */
1277         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1278                 if (exif_height > 0) {
1279                         content_info->media_meta.height = exif_height;
1280                 } else {
1281                         content_info->media_meta.height = 0;
1282                 }
1283         } else {
1284                 content_info->media_meta.height = 0;
1285         }
1286
1287         if (ed != NULL) exif_data_unref(ed);
1288
1289 GET_WIDTH_HEIGHT:
1290
1291         if(content_info->media_meta.width == 0 ||
1292                 content_info->media_meta.height == 0) {
1293                 /*Get image width, height*/
1294                 unsigned int img_width = 0;
1295                 unsigned int img_height = 0;
1296                 ImgCodecType img_type = IMG_CODEC_NONE;
1297
1298                 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1299
1300                 if(content_info->media_meta.width == 0)
1301                         content_info->media_meta.width = img_width;
1302
1303                 if(content_info->media_meta.height == 0)
1304                         content_info->media_meta.height = img_height;
1305         }
1306
1307         return MS_MEDIA_ERR_NONE;
1308 }
1309
1310 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1311 {
1312         MMHandleType tag = 0;
1313         char *p = NULL;
1314         int size = -1;
1315         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1316         int mmf_error = MM_ERROR_NONE;
1317         char *err_attr_name = NULL;
1318         char *title = NULL;
1319         int album_id = 0;
1320         int ret = MS_MEDIA_ERR_NONE;
1321
1322         /*Get Content Tag attribute ===========*/
1323         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1324
1325         if (mmf_error == MM_ERROR_NONE) {
1326                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1327                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1328                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1329                         if (ret != MS_MEDIA_ERR_NONE)
1330                                 media_svc_error("strcpy error");
1331
1332                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1333                 } else {
1334                         SAFE_FREE(err_attr_name);
1335                         /*media_svc_debug("album - unknown"); */
1336                 }
1337
1338                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1339                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1340                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1341                         if (ret != MS_MEDIA_ERR_NONE)
1342                                 media_svc_error("strcpy error");
1343                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1344                 } else {
1345                         SAFE_FREE(err_attr_name);
1346                         /*media_svc_debug("artist - unknown"); */
1347                 }
1348
1349                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1350                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1351                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1352                         if (ret != MS_MEDIA_ERR_NONE)
1353                                 media_svc_error("strcpy error");
1354                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1355                 } else {
1356                         SAFE_FREE(err_attr_name);
1357                         /*media_svc_debug("album_artist - unknown"); */
1358                 }
1359
1360                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1361                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1362                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1363                         if (ret != MS_MEDIA_ERR_NONE)
1364                                 media_svc_error("strcpy error");
1365
1366                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1367                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1368                         /*
1369                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1370                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1371                         }
1372                         */
1373                 } else {
1374                         SAFE_FREE(err_attr_name);
1375                         /*media_svc_debug("genre - unknown"); */
1376                 }
1377
1378                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1379                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1380                         if (!isspace(*p)) {
1381                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1382                                 if (ret != MS_MEDIA_ERR_NONE)
1383                                         media_svc_error("strcpy error");
1384
1385                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1386                         } else {
1387                                 int idx = 0;
1388
1389                                 for (idx = 0; idx < size; idx++) {
1390                                         if (isspace(*p)) {
1391                                                 media_svc_debug("SPACE [%s]", p);
1392                                                 p++;
1393                                                 continue;
1394                                         } else {
1395                                                 media_svc_debug("Not SPACE [%s]", p);
1396                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1397                                                 if (ret != MS_MEDIA_ERR_NONE)
1398                                                         media_svc_error("strcpy error");
1399                                                 break;
1400                                         }
1401                                 }
1402
1403                                 if (idx == size) {
1404                                         media_svc_debug("Can't extract title. All string is space");
1405                                         title = _media_svc_get_title_from_filepath(content_info->path);
1406                                         if (title) {
1407                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1408                                                 if (ret != MS_MEDIA_ERR_NONE)
1409                                                         media_svc_error("strcpy error");
1410                                                 SAFE_FREE(title);
1411                                         } else {
1412                                                 media_svc_error("Can't extract title");
1413                                         }
1414                                 }
1415                         }
1416                 } else {
1417                         SAFE_FREE(err_attr_name);
1418                         title = _media_svc_get_title_from_filepath(content_info->path);
1419                         if (title) {
1420                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1421                                 if (ret != MS_MEDIA_ERR_NONE)
1422                                         media_svc_error("strcpy error");
1423                                 SAFE_FREE(title);
1424                         } else {
1425                                 media_svc_error("Can't extract title");
1426                         }
1427                 }
1428
1429                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1430                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1431                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1432                         if (ret != MS_MEDIA_ERR_NONE)
1433                                 media_svc_error("strcpy error");
1434                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1435                 } else {
1436                         SAFE_FREE(err_attr_name);
1437                 }
1438
1439                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1440                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1441                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1442                         if (ret != MS_MEDIA_ERR_NONE)
1443                                 media_svc_error("strcpy error");
1444                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1445                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1446                 } else {
1447                         /*media_svc_debug("composer - unknown"); */
1448                         SAFE_FREE(err_attr_name);
1449                 }
1450
1451                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1452                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1453                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1454                         if (ret != MS_MEDIA_ERR_NONE)
1455                                 media_svc_error("strcpy error");
1456                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1457                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1458                 } else {
1459                         /*media_svc_debug("copyright - unknown"); */
1460                         SAFE_FREE(err_attr_name);
1461                 }
1462
1463                 mmf_error = mm_file_destroy_tag_attrs(tag);
1464                 if (mmf_error != MM_ERROR_NONE) {
1465                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1466                 }
1467         }       else {
1468                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1469                 char *no_tag_title = NULL;
1470                 media_svc_error("no tag information");
1471
1472                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1473                 if (no_tag_title) {
1474                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1475                         if(ret != MS_MEDIA_ERR_NONE)
1476                                 media_svc_error("strcpy error");
1477                         SAFE_FREE(no_tag_title);
1478                 } else {
1479                         media_svc_error("Can't extract title");
1480                 }
1481
1482                 content_info->album_id = album_id;
1483         }
1484
1485         return MS_MEDIA_ERR_NONE;
1486 }
1487
1488 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1489 {
1490         MMHandleType content = 0;
1491         MMHandleType tag = 0;
1492         char *p = NULL;
1493         void *image = NULL;
1494         int size = -1;
1495         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1496         int mmf_error = MM_ERROR_NONE;
1497         char *err_attr_name = NULL;
1498         char *title = NULL;
1499         bool extract_thumbnail = FALSE;
1500         bool append_album = FALSE;
1501         int album_id = 0;
1502         int ret = MS_MEDIA_ERR_NONE;
1503         int cdis_value = 0;
1504
1505         /*Get Content Tag attribute ===========*/
1506         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1507
1508         if (mmf_error == MM_ERROR_NONE) {
1509                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1510                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1511                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1512                         if (ret != MS_MEDIA_ERR_NONE)
1513                                 media_svc_error("strcpy error");
1514
1515                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1516                 } else {
1517                         SAFE_FREE(err_attr_name);
1518                         /*media_svc_debug("album - unknown"); */
1519                 }
1520
1521                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1522                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1523                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1524                         if (ret != MS_MEDIA_ERR_NONE)
1525                                 media_svc_error("strcpy error");
1526                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1527                 } else {
1528                         SAFE_FREE(err_attr_name);
1529                         /*media_svc_debug("artist - unknown"); */
1530                 }
1531
1532                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1533                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1534                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1535                         if (ret != MS_MEDIA_ERR_NONE)
1536                                 media_svc_error("strcpy error");
1537                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1538                 } else {
1539                         SAFE_FREE(err_attr_name);
1540                         /*media_svc_debug("album_artist - unknown"); */
1541                 }
1542
1543                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1544                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1545                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1546                         if (ret != MS_MEDIA_ERR_NONE)
1547                                 media_svc_error("strcpy error");
1548
1549                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1550                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1551                         /*
1552                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1553                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1554                         }
1555                         */
1556                 } else {
1557                         SAFE_FREE(err_attr_name);
1558                         /*media_svc_debug("genre - unknown"); */
1559                 }
1560
1561                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1562                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1563                         if (!isspace(*p)) {
1564                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1565                                 if (ret != MS_MEDIA_ERR_NONE)
1566                                         media_svc_error("strcpy error");
1567
1568                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1569                         } else {
1570                                 int idx = 0;
1571
1572                                 for (idx = 0; idx < size; idx++) {
1573                                         if (isspace(*p)) {
1574                                                 media_svc_debug("SPACE [%s]", p);
1575                                                 p++;
1576                                                 continue;
1577                                         } else {
1578                                                 media_svc_debug("Not SPACE [%s]", p);
1579                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1580                                                 if (ret != MS_MEDIA_ERR_NONE)
1581                                                         media_svc_error("strcpy error");
1582                                                 break;
1583                                         }
1584                                 }
1585
1586                                 if (idx == size) {
1587                                         media_svc_debug("Can't extract title. All string is space");
1588                                         title = _media_svc_get_title_from_filepath(content_info->path);
1589                                         if (title) {
1590                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1591                                                 if (ret != MS_MEDIA_ERR_NONE)
1592                                                         media_svc_error("strcpy error");
1593                                                 SAFE_FREE(title);
1594                                         } else {
1595                                                 media_svc_error("Can't extract title");
1596                                         }
1597                                 }
1598                         }
1599                 } else {
1600                         SAFE_FREE(err_attr_name);
1601                         title = _media_svc_get_title_from_filepath(content_info->path);
1602                         if (title) {
1603                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1604                                 if (ret != MS_MEDIA_ERR_NONE)
1605                                         media_svc_error("strcpy error");
1606                                 SAFE_FREE(title);
1607                         } else {
1608                                 media_svc_error("Can't extract title");
1609                         }
1610                 }
1611
1612                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1613                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1614                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1615                         if (ret != MS_MEDIA_ERR_NONE)
1616                                 media_svc_error("strcpy error");
1617                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1618                 } else {
1619                         SAFE_FREE(err_attr_name);
1620                 }
1621
1622                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1623                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1624                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1625                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1626                                 char time_info[64] = {0, };
1627                                 char p_value[64] = {0, };
1628                                 int idx = 0;
1629                                 memset(time_info, 0x00, sizeof(time_info));
1630                                 memset(p_value, 0x00, sizeof(p_value));
1631                                 strncpy(p_value, p, size);
1632                                 for (idx = 0; idx < size; idx++) {
1633                                         if (p_value[idx] == '-') {
1634                                                 time_info[idx] = ':';
1635                                         } else if (p_value[idx] != '\0') {
1636                                                 time_info[idx] = p_value[idx];
1637                                         } else {
1638                                                 media_svc_error("strcpy error");
1639                                                 break;
1640                                         }
1641                                 }
1642                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1643                         } else {
1644                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1645                         }
1646
1647                         if (ret != MS_MEDIA_ERR_NONE) {
1648                                 media_svc_error("strcpy error");
1649                         } else {
1650                                 /* This is same as datetaken */
1651 #if 0
1652                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1653 #else
1654                                 int datetaken_size = 19;
1655                                 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1656 #endif
1657                                 if (ret != MS_MEDIA_ERR_NONE)
1658                                         media_svc_error("strcpy error");
1659
1660                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1661                                 if (content_info->timeline == 0) {
1662                                         content_info->timeline = content_info->modified_time;
1663                                 } else {
1664                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1665                                 }
1666                         }
1667                         /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1668                 } else {
1669                         SAFE_FREE(err_attr_name);
1670                 }
1671
1672                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1673                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1674                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1675                         if (ret != MS_MEDIA_ERR_NONE)
1676                                 media_svc_error("strcpy error");
1677                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1678                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1679                 } else {
1680                         /*media_svc_debug("composer - unknown"); */
1681                         SAFE_FREE(err_attr_name);
1682                 }
1683
1684                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1685                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1686                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1687                         if (ret != MS_MEDIA_ERR_NONE)
1688                                 media_svc_error("strcpy error");
1689                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1690                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1691                 } else {
1692                         /*media_svc_debug("copyright - unknown"); */
1693                         SAFE_FREE(err_attr_name);
1694                 }
1695
1696                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1697                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1698                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1699                         if (ret != MS_MEDIA_ERR_NONE)
1700                                 media_svc_error("strcpy error");
1701                 } else {
1702                         SAFE_FREE(err_attr_name);
1703                 }
1704
1705                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1706                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == MM_ERROR_NONE) && (size == 4)) {
1707                         int year = 0;
1708                         if ((p != NULL) && (sscanf(p, "%d", &year))) {
1709                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1710                                 if (ret != MS_MEDIA_ERR_NONE)
1711                                         media_svc_error("strcpy error");
1712                         } else {
1713                                 media_svc_debug("Wrong Year Information [%s]", p);
1714                         }
1715                 } else {
1716                         SAFE_FREE(err_attr_name);
1717                 }
1718
1719                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1720                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1721                         content_info->media_meta.rating = atoi(p);
1722                 } else {
1723                         SAFE_FREE(err_attr_name);
1724                         content_info->media_meta.rating = 0;
1725                 }
1726
1727                 /*Initialize album_id to 0. below code will set the album_id*/
1728                 content_info->album_id = album_id;
1729                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1730
1731                 if (ret != MS_MEDIA_ERR_NONE) {
1732                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1733                                 media_svc_debug("album does not exist. So start to make album art");
1734                                 extract_thumbnail = TRUE;
1735                                 append_album = TRUE;
1736                         } else {
1737                                 extract_thumbnail = TRUE;
1738                                 append_album = FALSE;
1739                         }
1740                 } else {
1741                         content_info->album_id = album_id;
1742                         append_album = FALSE;
1743
1744                         if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1745                             (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1746
1747                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1748                                 extract_thumbnail = TRUE;
1749                         } else {
1750                                 media_svc_debug("album already exists. don't need to make album art");
1751                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1752                                 extract_thumbnail = TRUE;
1753                         }
1754                 }
1755
1756                 /*Do not extract artwork for the USB Storage content*/
1757                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1758                         extract_thumbnail = FALSE;
1759
1760                 if (extract_thumbnail == TRUE) {
1761                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1762                         if (mmf_error != MM_ERROR_NONE) {
1763                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1764                                 SAFE_FREE(err_attr_name);
1765                         } else {
1766                                 /*media_svc_debug("artwork size1 [%d]", size); */
1767                         }
1768
1769                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1770                         if (mmf_error != MM_ERROR_NONE) {
1771                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1772                                 SAFE_FREE(err_attr_name);
1773                         } else {
1774                                 /*media_svc_debug("artwork size2 [%d]", size); */
1775                         }
1776                         if (image != NULL && size > 0) {
1777                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1778                                 int artwork_mime_size = -1;
1779
1780                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1781                                 if ((mmf_error == MM_ERROR_NONE) && (artwork_mime_size > 0)) {
1782                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1783                                         if (ret != MS_MEDIA_ERR_NONE) {
1784                                                 media_svc_error("Fail to Get Thumbnail Path");
1785                                         }
1786                                 } else {
1787                                         SAFE_FREE(err_attr_name);
1788                                 }
1789
1790                                 if (strlen(thumb_path) > 0) {
1791                                         ret = _media_svc_save_image(image, size, thumb_path, uid);
1792                                         if (ret != MS_MEDIA_ERR_NONE) {
1793                                                 media_svc_error("Fail to Save Thumbnail Image");
1794                                         } else {
1795                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1796                                                 if (ret != MS_MEDIA_ERR_NONE)
1797                                                         media_svc_error("strcpy error");
1798                                         }
1799                                 }
1800                         }
1801                 }
1802
1803                 if (append_album == TRUE) {
1804                         if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1805                             (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1806                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1807                         else
1808                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1809
1810                         content_info->album_id = album_id;
1811                 }
1812
1813                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1814                         double longitude = 0.0;
1815                         double latitude = 0.0;
1816                         double altitude = 0.0;
1817
1818                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1819                         content_info->media_meta.longitude = longitude;
1820                         content_info->media_meta.latitude = latitude;
1821                         content_info->media_meta.altitude = altitude;
1822
1823                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1824                         if (mmf_error != MM_ERROR_NONE) {
1825                                 cdis_value = 0;
1826                                 SAFE_FREE(err_attr_name);
1827                         }
1828
1829                         media_svc_debug("CDIS : %d", cdis_value);
1830                 }
1831
1832                 mmf_error = mm_file_destroy_tag_attrs(tag);
1833                 if (mmf_error != MM_ERROR_NONE) {
1834                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1835                 }
1836         }       else {
1837                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1838                 char *no_tag_title = NULL;
1839                 media_svc_error("no tag information");
1840
1841                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1842                 if (no_tag_title) {
1843                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1844                         if(ret != MS_MEDIA_ERR_NONE)
1845                                 media_svc_error("strcpy error");
1846                         SAFE_FREE(no_tag_title);
1847                 } else {
1848                         media_svc_error("Can't extract title");
1849                 }
1850
1851                 content_info->album_id = album_id;
1852         }
1853
1854         /*Get Content attribute ===========*/
1855         if (cdis_value == 1) {
1856                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1857         } else {
1858                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1859         }
1860
1861         if (mmf_error == MM_ERROR_NONE) {
1862                 /*Common attribute*/
1863                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1864                 if (mmf_error != MM_ERROR_NONE) {
1865                         SAFE_FREE(err_attr_name);
1866                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1867                 } else {
1868                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1869                 }
1870
1871                 /*Sound/Music attribute*/
1872                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1873
1874                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1875                         if (mmf_error != MM_ERROR_NONE) {
1876                                 SAFE_FREE(err_attr_name);
1877                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1878                         } else {
1879                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1880                         }
1881
1882                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1883                         if (mmf_error != MM_ERROR_NONE) {
1884                                 SAFE_FREE(err_attr_name);
1885                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1886                         } else {
1887                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1888                         }
1889
1890                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1891                         if (mmf_error != MM_ERROR_NONE) {
1892                                 SAFE_FREE(err_attr_name);
1893                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1894                         } else {
1895                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1896                         }
1897
1898                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1899                         if (mmf_error != MM_ERROR_NONE) {
1900                                 SAFE_FREE(err_attr_name);
1901                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1902                         } else {
1903                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1904                         }
1905                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1906                         int audio_bitrate = 0;
1907                         int video_bitrate = 0;
1908
1909                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1910                         if (mmf_error != MM_ERROR_NONE) {
1911                                 SAFE_FREE(err_attr_name);
1912                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1913                         } else {
1914                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1915                         }
1916
1917                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1918                         if (mmf_error != MM_ERROR_NONE) {
1919                                 SAFE_FREE(err_attr_name);
1920                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1921                         } else {
1922                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1923                         }
1924
1925                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1926
1927                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1928                         if (mmf_error != MM_ERROR_NONE) {
1929                                 SAFE_FREE(err_attr_name);
1930                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1931                         } else {
1932                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1933                         }
1934
1935                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1936                         if (mmf_error != MM_ERROR_NONE) {
1937                                 SAFE_FREE(err_attr_name);
1938                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1939                         } else {
1940                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1941                         }
1942
1943                 } else {
1944                         media_svc_error("Not support type");
1945                         mmf_error = mm_file_destroy_content_attrs(content);
1946                         if (mmf_error != MM_ERROR_NONE) {
1947                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1948                         }
1949                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1950                 }
1951
1952                 mmf_error = mm_file_destroy_content_attrs(content);
1953                 if (mmf_error != MM_ERROR_NONE) {
1954                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1955                 }
1956         } else {
1957                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1958         }
1959
1960         return MS_MEDIA_ERR_NONE;
1961 }
1962
1963 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1964 {
1965         media_svc_retm_if(content_info == NULL, "content info is NULL");
1966
1967         /* Delete media_svc_content_info_s */
1968         SAFE_FREE(content_info->media_uuid);
1969         SAFE_FREE(content_info->path);
1970         SAFE_FREE(content_info->file_name);
1971         SAFE_FREE(content_info->mime_type);
1972         SAFE_FREE(content_info->folder_uuid);
1973         SAFE_FREE(content_info->thumbnail_path);
1974         SAFE_FREE(content_info->storage_uuid);
1975
1976         /* Delete media_svc_content_meta_s */
1977         SAFE_FREE(content_info->media_meta.title);
1978         SAFE_FREE(content_info->media_meta.album);
1979         SAFE_FREE(content_info->media_meta.artist);
1980         SAFE_FREE(content_info->media_meta.album_artist);
1981         SAFE_FREE(content_info->media_meta.genre);
1982         SAFE_FREE(content_info->media_meta.composer);
1983         SAFE_FREE(content_info->media_meta.year);
1984         SAFE_FREE(content_info->media_meta.recorded_date);
1985         SAFE_FREE(content_info->media_meta.copyright);
1986         SAFE_FREE(content_info->media_meta.track_num);
1987         SAFE_FREE(content_info->media_meta.description);
1988         SAFE_FREE(content_info->media_meta.datetaken);
1989         SAFE_FREE(content_info->media_meta.exposure_time);
1990         SAFE_FREE(content_info->media_meta.model);
1991         SAFE_FREE(content_info->media_meta.weather);
1992
1993         SAFE_FREE(content_info->media_meta.title_pinyin);
1994         SAFE_FREE(content_info->media_meta.album_pinyin);
1995         SAFE_FREE(content_info->media_meta.artist_pinyin);
1996         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1997         SAFE_FREE(content_info->media_meta.genre_pinyin);
1998         SAFE_FREE(content_info->media_meta.composer_pinyin);
1999         SAFE_FREE(content_info->media_meta.copyright_pinyin);
2000         SAFE_FREE(content_info->media_meta.description_pinyin);
2001
2002         return;
2003 }
2004
2005 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2006 {
2007         if (STRING_VALID(path)) {
2008                 if (strncmp(path, _media_svc_get_path(uid), strlen(_media_svc_get_path(uid))) == 0) {
2009                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2010                 } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
2011                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2012                 } else if (strncmp (path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
2013                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2014                 } else {
2015                         media_svc_error("Invalid Path");
2016                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2017                 }
2018         } else {
2019                 media_svc_error("INVALID parameter");
2020                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2021         }
2022
2023         return MS_MEDIA_ERR_NONE;
2024 }
2025
2026 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2027 {
2028         char *result, *sr;
2029         size_t i, count = 0;
2030         size_t oldlen = strlen(olds);
2031         if (oldlen < 1) return s;
2032         size_t newlen = strlen(news);
2033
2034         if (newlen != oldlen) {
2035                 for (i = 0; s[i] != '\0';) {
2036                         if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2037                         else i++;
2038                 }
2039         } else i = strlen(s);
2040
2041
2042         result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2043         if (result == NULL) return NULL;
2044
2045         sr = result;
2046         while (*s) {
2047                 if (memcmp(s, olds, oldlen) == 0) {
2048                         memcpy(sr, news, newlen);
2049                         sr += newlen;
2050                         s  += oldlen;
2051                 } else *sr++ = *s++;
2052         }
2053
2054         *sr = '\0';
2055
2056         return result;
2057 }
2058
2059 bool _media_svc_is_drm_file(const char *path)
2060 {
2061         return FALSE;
2062 }
2063
2064 int _media_svc_request_thumbnail_with_origin_size(const char *path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
2065 {
2066         int ret = MS_MEDIA_ERR_NONE;
2067
2068         ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2069
2070         if (ret != MS_MEDIA_ERR_NONE) {
2071                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2072                 ret = MS_MEDIA_ERR_INTERNAL;
2073         } else {
2074                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2075         }
2076
2077         return ret;
2078 }
2079
2080 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2081 {
2082         int ret = MS_MEDIA_ERR_NONE;
2083         int size = 0;
2084         pinyin_name_s *pinyinname = NULL;
2085
2086         *pinyin_str = NULL;
2087
2088         if (!STRING_VALID(src_str)) {
2089                 media_svc_debug("String is invalid");
2090                 return ret;
2091         }
2092
2093         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2094         if (ret == MS_MEDIA_ERR_NONE) {
2095                 if(STRING_VALID(pinyinname[0].pinyin_name))
2096                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
2097                 else
2098                         *pinyin_str = strdup(src_str);  //Return Original Non China Character
2099         }
2100
2101         _media_svc_pinyin_free(pinyinname, size);
2102
2103         return ret;
2104 }
2105
2106 bool _media_svc_check_pinyin_support(void)
2107 {
2108         /*Check CSC*/
2109         return TRUE;
2110 }
2111
2112 char* _media_svc_get_title_from_path(const char *path)
2113 {
2114         char *filename = NULL;
2115         char *title = NULL;
2116         char    *ext = NULL;
2117         int filename_len = -1;
2118         int new_title_len = -1;
2119
2120         if (!path) {
2121                 media_svc_error("path is NULL");
2122                 return NULL;
2123         }
2124
2125         filename = g_path_get_basename(path);
2126         if (!STRING_VALID(filename)) {
2127                 media_svc_error("wrong file name");
2128                 SAFE_FREE(filename);
2129                 return NULL;
2130         }
2131
2132         filename_len = strlen(filename);
2133
2134         ext = g_strrstr(filename, ".");
2135         if (!ext) {
2136                 media_svc_error("there is no file extention");
2137                 return filename;
2138         }
2139
2140         new_title_len = filename_len - strlen(ext);
2141         if (new_title_len < 1) {
2142                 media_svc_error("title length is zero");
2143                 SAFE_FREE(filename);
2144                 return NULL;
2145         }
2146
2147         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2148
2149         SAFE_FREE(filename);
2150
2151         media_svc_debug("extract title is [%s]", title);
2152
2153         return title;
2154 }
2155
2156 #define BUF_LENGHT 256
2157
2158 void _media_svc_print_stderror(void)
2159 {
2160         char buf[BUF_LENGHT] = {0,};
2161
2162         media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
2163 }