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