check condition just when sotrage type is cloud
[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         bool drm_type = false;
1007         char mime_type[256] = {0, };
1008
1009         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1010         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1011
1012         if(storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1013                 struct stat st;
1014                 memset(&st, 0, sizeof(struct stat));
1015                 if (stat(path, &st) == 0) {
1016                         content_info->modified_time = st.st_mtime;
1017                         content_info->timeline = content_info->modified_time;
1018                         content_info->size = st.st_size;
1019                         //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1020                 } else {
1021                         media_svc_stderror("stat failed");
1022                 }
1023         }
1024
1025         _media_svc_set_default_value(content_info, refresh);
1026
1027         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1028         if (refresh) {
1029                 media_svc_debug("refresh");
1030                 return MS_MEDIA_ERR_NONE;
1031         }
1032
1033         ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1034         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1035
1036         content_info->storage_type = storage_type;
1037         time(&content_info->added_time);
1038
1039         media_uuid = _media_info_generate_uuid();
1040         if (media_uuid == NULL) {
1041                 _media_svc_destroy_content_info(content_info);
1042                 return MS_MEDIA_ERR_INTERNAL;
1043         }
1044
1045         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1046         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1047
1048         file_name = g_path_get_basename(path);
1049         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1050         SAFE_FREE(file_name);
1051         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1052
1053         if(storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1054                 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1055                 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1056                 ret = __media_svc_get_mime_type(path, mime_type);
1057                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1058
1059                 media_svc_debug("mime [%s]", mime_type);
1060                 content_info->is_drm = drm_type;
1061
1062                 ret = __media_svc_get_media_type(path, mime_type, media_type);
1063                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1064
1065                 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1066                         media_svc_error("invalid media_type condition[%d]", *media_type);
1067                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1068                 }
1069
1070                 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1071                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1072
1073                 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1074
1075                 content_info->media_type = *media_type;
1076         }
1077
1078         return MS_MEDIA_ERR_NONE;
1079 }
1080
1081 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1082 {
1083         int ret = MS_MEDIA_ERR_NONE;
1084         double value = 0.0;
1085         int orient_value = 0;
1086         int exif_width = 0;
1087         int exif_height = 0;
1088         ExifData *ed = NULL;
1089         int has_datetaken = FALSE;
1090         int datetaken_size = 19;
1091         double fnumber = 0.0;
1092         int iso = 0;
1093         char *path = NULL;
1094
1095         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1096         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1097         char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1098         char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1099
1100         memset(buf, 0x00, sizeof(buf));
1101         memset(description_buf, 0x00, sizeof(description_buf));
1102         memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1103         memset(model_buf, 0x00, sizeof(model_buf));
1104
1105         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1106                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1107                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1108         }
1109
1110         path = content_info->path;
1111         if (!STRING_VALID(path)) {
1112                 media_svc_error("Invalid Path");
1113                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1114         }
1115
1116         /* Load an ExifData object from an EXIF file */
1117         ed = exif_data_new_from_file(path);
1118
1119         if (!ed) {
1120                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1121                 goto GET_WIDTH_HEIGHT;
1122         }
1123
1124         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1125                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1126                         if (strlen(buf) > 0) {
1127                                 if (strcmp(buf, "S") == 0) {
1128                                         value = -1 * value;
1129                                 }
1130                         }
1131                         content_info->media_meta.latitude = value;
1132                 } else {
1133                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1134                 }
1135         } else {
1136                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1137         }
1138
1139         memset(buf, 0x00, sizeof(buf));
1140
1141         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1142                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1143                         if (strlen(buf) > 0) {
1144                                 if (strcmp(buf, "W") == 0) {
1145                                         value = -1 * value;
1146                                 }
1147                         }
1148                         content_info->media_meta.longitude = value;
1149                 } else {
1150                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1151                 }
1152         } else {
1153                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1154         }
1155
1156         memset(buf, 0x00, sizeof(buf));
1157
1158         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1159                 if (strlen(description_buf) == 0) {
1160                         /*media_svc_debug("Use 'No description'"); */
1161                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1162                         if (ret != MS_MEDIA_ERR_NONE)
1163                                 media_svc_error("strcpy error");
1164                 } else {
1165                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1166                         if (ret != MS_MEDIA_ERR_NONE)
1167                                 media_svc_error("strcpy error");
1168                 }
1169         } else {
1170                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1171                 if (ret != MS_MEDIA_ERR_NONE)
1172                         media_svc_error("strcpy error");
1173         }
1174
1175         memset(buf, 0x00, sizeof(buf));
1176
1177         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1178                 if (strlen(buf) == 0) {
1179                         /*media_svc_debug("time  is NULL"); */
1180                 } else {
1181                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1182                         if (ret != MS_MEDIA_ERR_NONE) {
1183                                 media_svc_error("strcpy error");
1184                         } else {
1185                                 has_datetaken = TRUE;
1186                                 /* This is same as recorded_date */
1187                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1188                                 if (ret != MS_MEDIA_ERR_NONE)
1189                                         media_svc_error("strcpy error");
1190                         }
1191                 }
1192         }
1193
1194         memset(buf, 0x00, sizeof(buf));
1195
1196         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1197                 if (strlen(buf) == 0) {
1198                         /*media_svc_debug("time  is NULL"); */
1199                 } else {
1200                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1201                         if (ret != MS_MEDIA_ERR_NONE) {
1202                                 media_svc_error("strcpy error");
1203                         } else {
1204                                 has_datetaken = TRUE;
1205                                 /* This is same as recorded_date */
1206                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1207                                 if (ret != MS_MEDIA_ERR_NONE)
1208                                         media_svc_error("strcpy error");
1209                         }
1210                 }
1211         }
1212
1213         if (has_datetaken) {
1214                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1215                 if (content_info->timeline == 0) {
1216                         content_info->timeline = content_info->modified_time;
1217                 } else {
1218                         media_svc_debug("Timeline : %ld", content_info->timeline);
1219                 }
1220         }
1221
1222         /* Get exposure_time value from exif. */
1223         if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1224                 if (strlen(exposure_time_buf) == 0) {
1225                         //media_svc_debug("exposure_time_buf is NULL");
1226                 } else {
1227                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1228                         if (ret != MS_MEDIA_ERR_NONE)
1229                                 media_svc_error("strcpy error");
1230                 }
1231         }
1232
1233         /* Get fnumber value from exif. */
1234         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_IFD_0, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1235                 if (fnumber > 0.0) {
1236                         content_info->media_meta.fnumber = fnumber;
1237                 } else {
1238                         content_info->media_meta.fnumber = 0.0;
1239                 }
1240         } else {
1241                 content_info->media_meta.fnumber = 0.0;
1242         }
1243
1244         /* Get iso value from exif. */
1245         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_IFD_0, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1246                 if (iso > 0) {
1247                         content_info->media_meta.iso = iso;
1248                 } else {
1249                         content_info->media_meta.iso = 0;
1250                 }
1251         } else {
1252                 content_info->media_meta.iso = 0;
1253         }
1254
1255         /* Get model value from exif. */
1256         if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1257                 if (strlen(model_buf) == 0) {
1258                         //media_svc_debug("model_buf is NULL");
1259                 } else {
1260                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1261                         if (ret != MS_MEDIA_ERR_NONE)
1262                                 media_svc_error("strcpy error");
1263                 }
1264         }
1265
1266         /* Get orientation value from exif. */
1267         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_IFD_0, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1268                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1269                         content_info->media_meta.orientation = orient_value;
1270                 } else {
1271                         content_info->media_meta.orientation = 0;
1272                 }
1273         } else {
1274                 content_info->media_meta.orientation = 0;
1275         }
1276
1277         /* Get width value from exif. */
1278         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1279                 if (exif_width > 0) {
1280                         content_info->media_meta.width = exif_width;
1281                 } else {
1282                         content_info->media_meta.width = 0;
1283                 }
1284         } else {
1285                 content_info->media_meta.width = 0;
1286         }
1287
1288         /* Get height value from exif. */
1289         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1290                 if (exif_height > 0) {
1291                         content_info->media_meta.height = exif_height;
1292                 } else {
1293                         content_info->media_meta.height = 0;
1294                 }
1295         } else {
1296                 content_info->media_meta.height = 0;
1297         }
1298
1299         if (ed != NULL) exif_data_unref(ed);
1300
1301 GET_WIDTH_HEIGHT:
1302
1303         if(content_info->media_meta.width == 0 ||
1304                 content_info->media_meta.height == 0) {
1305                 /*Get image width, height*/
1306                 unsigned int img_width = 0;
1307                 unsigned int img_height = 0;
1308                 ImgCodecType img_type = IMG_CODEC_NONE;
1309
1310                 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1311
1312                 if(content_info->media_meta.width == 0)
1313                         content_info->media_meta.width = img_width;
1314
1315                 if(content_info->media_meta.height == 0)
1316                         content_info->media_meta.height = img_height;
1317         }
1318
1319         return MS_MEDIA_ERR_NONE;
1320 }
1321
1322 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1323 {
1324         MMHandleType tag = 0;
1325         char *p = NULL;
1326         int size = -1;
1327         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1328         int mmf_error = MM_ERROR_NONE;
1329         char *err_attr_name = NULL;
1330         char *title = NULL;
1331         int album_id = 0;
1332         int ret = MS_MEDIA_ERR_NONE;
1333
1334         /*Get Content Tag attribute ===========*/
1335         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1336
1337         if (mmf_error == MM_ERROR_NONE) {
1338                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1339                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1340                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1341                         if (ret != MS_MEDIA_ERR_NONE)
1342                                 media_svc_error("strcpy error");
1343
1344                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1345                 } else {
1346                         SAFE_FREE(err_attr_name);
1347                         /*media_svc_debug("album - unknown"); */
1348                 }
1349
1350                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1351                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1352                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1353                         if (ret != MS_MEDIA_ERR_NONE)
1354                                 media_svc_error("strcpy error");
1355                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1356                 } else {
1357                         SAFE_FREE(err_attr_name);
1358                         /*media_svc_debug("artist - unknown"); */
1359                 }
1360
1361                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1362                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1363                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1364                         if (ret != MS_MEDIA_ERR_NONE)
1365                                 media_svc_error("strcpy error");
1366                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1367                 } else {
1368                         SAFE_FREE(err_attr_name);
1369                         /*media_svc_debug("album_artist - unknown"); */
1370                 }
1371
1372                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1373                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1374                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1375                         if (ret != MS_MEDIA_ERR_NONE)
1376                                 media_svc_error("strcpy error");
1377
1378                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1379                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1380                         /*
1381                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1382                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1383                         }
1384                         */
1385                 } else {
1386                         SAFE_FREE(err_attr_name);
1387                         /*media_svc_debug("genre - unknown"); */
1388                 }
1389
1390                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1391                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1392                         if (!isspace(*p)) {
1393                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1394                                 if (ret != MS_MEDIA_ERR_NONE)
1395                                         media_svc_error("strcpy error");
1396
1397                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1398                         } else {
1399                                 int idx = 0;
1400
1401                                 for (idx = 0; idx < size; idx++) {
1402                                         if (isspace(*p)) {
1403                                                 media_svc_debug("SPACE [%s]", p);
1404                                                 p++;
1405                                                 continue;
1406                                         } else {
1407                                                 media_svc_debug("Not SPACE [%s]", p);
1408                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1409                                                 if (ret != MS_MEDIA_ERR_NONE)
1410                                                         media_svc_error("strcpy error");
1411                                                 break;
1412                                         }
1413                                 }
1414
1415                                 if (idx == size) {
1416                                         media_svc_debug("Can't extract title. All string is space");
1417                                         title = _media_svc_get_title_from_filepath(content_info->path);
1418                                         if (title) {
1419                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1420                                                 if (ret != MS_MEDIA_ERR_NONE)
1421                                                         media_svc_error("strcpy error");
1422                                                 SAFE_FREE(title);
1423                                         } else {
1424                                                 media_svc_error("Can't extract title");
1425                                         }
1426                                 }
1427                         }
1428                 } else {
1429                         SAFE_FREE(err_attr_name);
1430                         title = _media_svc_get_title_from_filepath(content_info->path);
1431                         if (title) {
1432                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1433                                 if (ret != MS_MEDIA_ERR_NONE)
1434                                         media_svc_error("strcpy error");
1435                                 SAFE_FREE(title);
1436                         } else {
1437                                 media_svc_error("Can't extract title");
1438                         }
1439                 }
1440
1441                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1442                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1443                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1444                         if (ret != MS_MEDIA_ERR_NONE)
1445                                 media_svc_error("strcpy error");
1446                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1447                 } else {
1448                         SAFE_FREE(err_attr_name);
1449                 }
1450
1451                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1452                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1453                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1454                         if (ret != MS_MEDIA_ERR_NONE)
1455                                 media_svc_error("strcpy error");
1456                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1457                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1458                 } else {
1459                         /*media_svc_debug("composer - unknown"); */
1460                         SAFE_FREE(err_attr_name);
1461                 }
1462
1463                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1464                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1465                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1466                         if (ret != MS_MEDIA_ERR_NONE)
1467                                 media_svc_error("strcpy error");
1468                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1469                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1470                 } else {
1471                         /*media_svc_debug("copyright - unknown"); */
1472                         SAFE_FREE(err_attr_name);
1473                 }
1474
1475                 mmf_error = mm_file_destroy_tag_attrs(tag);
1476                 if (mmf_error != MM_ERROR_NONE) {
1477                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1478                 }
1479         }       else {
1480                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1481                 char *no_tag_title = NULL;
1482                 media_svc_error("no tag information");
1483
1484                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1485                 if (no_tag_title) {
1486                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1487                         if(ret != MS_MEDIA_ERR_NONE)
1488                                 media_svc_error("strcpy error");
1489                         SAFE_FREE(no_tag_title);
1490                 } else {
1491                         media_svc_error("Can't extract title");
1492                 }
1493
1494                 content_info->album_id = album_id;
1495         }
1496
1497         return MS_MEDIA_ERR_NONE;
1498 }
1499
1500 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1501 {
1502         MMHandleType content = 0;
1503         MMHandleType tag = 0;
1504         char *p = NULL;
1505         void *image = NULL;
1506         int size = -1;
1507         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1508         int mmf_error = MM_ERROR_NONE;
1509         char *err_attr_name = NULL;
1510         char *title = NULL;
1511         bool extract_thumbnail = FALSE;
1512         bool append_album = FALSE;
1513         int album_id = 0;
1514         int ret = MS_MEDIA_ERR_NONE;
1515         int cdis_value = 0;
1516
1517         /*Get Content Tag attribute ===========*/
1518         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1519
1520         if (mmf_error == MM_ERROR_NONE) {
1521                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1522                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1523                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1524                         if (ret != MS_MEDIA_ERR_NONE)
1525                                 media_svc_error("strcpy error");
1526
1527                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1528                 } else {
1529                         SAFE_FREE(err_attr_name);
1530                         /*media_svc_debug("album - unknown"); */
1531                 }
1532
1533                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1534                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1535                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1536                         if (ret != MS_MEDIA_ERR_NONE)
1537                                 media_svc_error("strcpy error");
1538                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1539                 } else {
1540                         SAFE_FREE(err_attr_name);
1541                         /*media_svc_debug("artist - unknown"); */
1542                 }
1543
1544                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1545                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1546                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1547                         if (ret != MS_MEDIA_ERR_NONE)
1548                                 media_svc_error("strcpy error");
1549                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1550                 } else {
1551                         SAFE_FREE(err_attr_name);
1552                         /*media_svc_debug("album_artist - unknown"); */
1553                 }
1554
1555                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1556                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1557                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1558                         if (ret != MS_MEDIA_ERR_NONE)
1559                                 media_svc_error("strcpy error");
1560
1561                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1562                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1563                         /*
1564                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1565                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1566                         }
1567                         */
1568                 } else {
1569                         SAFE_FREE(err_attr_name);
1570                         /*media_svc_debug("genre - unknown"); */
1571                 }
1572
1573                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1574                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1575                         if (!isspace(*p)) {
1576                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1577                                 if (ret != MS_MEDIA_ERR_NONE)
1578                                         media_svc_error("strcpy error");
1579
1580                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1581                         } else {
1582                                 int idx = 0;
1583
1584                                 for (idx = 0; idx < size; idx++) {
1585                                         if (isspace(*p)) {
1586                                                 media_svc_debug("SPACE [%s]", p);
1587                                                 p++;
1588                                                 continue;
1589                                         } else {
1590                                                 media_svc_debug("Not SPACE [%s]", p);
1591                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1592                                                 if (ret != MS_MEDIA_ERR_NONE)
1593                                                         media_svc_error("strcpy error");
1594                                                 break;
1595                                         }
1596                                 }
1597
1598                                 if (idx == size) {
1599                                         media_svc_debug("Can't extract title. All string is space");
1600                                         title = _media_svc_get_title_from_filepath(content_info->path);
1601                                         if (title) {
1602                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1603                                                 if (ret != MS_MEDIA_ERR_NONE)
1604                                                         media_svc_error("strcpy error");
1605                                                 SAFE_FREE(title);
1606                                         } else {
1607                                                 media_svc_error("Can't extract title");
1608                                         }
1609                                 }
1610                         }
1611                 } else {
1612                         SAFE_FREE(err_attr_name);
1613                         title = _media_svc_get_title_from_filepath(content_info->path);
1614                         if (title) {
1615                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1616                                 if (ret != MS_MEDIA_ERR_NONE)
1617                                         media_svc_error("strcpy error");
1618                                 SAFE_FREE(title);
1619                         } else {
1620                                 media_svc_error("Can't extract title");
1621                         }
1622                 }
1623
1624                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1625                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1626                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1627                         if (ret != MS_MEDIA_ERR_NONE)
1628                                 media_svc_error("strcpy error");
1629                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1630                 } else {
1631                         SAFE_FREE(err_attr_name);
1632                 }
1633
1634                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1635                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1636                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1637                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1638                                 char time_info[64] = {0, };
1639                                 char p_value[64] = {0, };
1640                                 int idx = 0;
1641                                 memset(time_info, 0x00, sizeof(time_info));
1642                                 memset(p_value, 0x00, sizeof(p_value));
1643                                 strncpy(p_value, p, size);
1644                                 for (idx = 0; idx < size; idx++) {
1645                                         if (p_value[idx] == '-') {
1646                                                 time_info[idx] = ':';
1647                                         } else if (p_value[idx] != '\0') {
1648                                                 time_info[idx] = p_value[idx];
1649                                         } else {
1650                                                 media_svc_error("strcpy error");
1651                                                 break;
1652                                         }
1653                                 }
1654                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1655                         } else {
1656                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1657                         }
1658
1659                         if (ret != MS_MEDIA_ERR_NONE) {
1660                                 media_svc_error("strcpy error");
1661                         } else {
1662                                 /* This is same as datetaken */
1663 #if 0
1664                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1665 #else
1666                                 int datetaken_size = 19;
1667                                 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1668 #endif
1669                                 if (ret != MS_MEDIA_ERR_NONE)
1670                                         media_svc_error("strcpy error");
1671
1672                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1673                                 if (content_info->timeline == 0) {
1674                                         content_info->timeline = content_info->modified_time;
1675                                 } else {
1676                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1677                                 }
1678                         }
1679                         /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1680                 } else {
1681                         SAFE_FREE(err_attr_name);
1682                 }
1683
1684                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1685                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1686                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1687                         if (ret != MS_MEDIA_ERR_NONE)
1688                                 media_svc_error("strcpy error");
1689                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1690                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1691                 } else {
1692                         /*media_svc_debug("composer - unknown"); */
1693                         SAFE_FREE(err_attr_name);
1694                 }
1695
1696                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1697                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1698                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1699                         if (ret != MS_MEDIA_ERR_NONE)
1700                                 media_svc_error("strcpy error");
1701                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1702                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1703                 } else {
1704                         /*media_svc_debug("copyright - unknown"); */
1705                         SAFE_FREE(err_attr_name);
1706                 }
1707
1708                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1709                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1710                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1711                         if (ret != MS_MEDIA_ERR_NONE)
1712                                 media_svc_error("strcpy error");
1713                 } else {
1714                         SAFE_FREE(err_attr_name);
1715                 }
1716
1717                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1718                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == MM_ERROR_NONE) && (size == 4)) {
1719                         int year = 0;
1720                         if ((p != NULL) && (sscanf(p, "%d", &year))) {
1721                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1722                                 if (ret != MS_MEDIA_ERR_NONE)
1723                                         media_svc_error("strcpy error");
1724                         } else {
1725                                 media_svc_debug("Wrong Year Information [%s]", p);
1726                         }
1727                 } else {
1728                         SAFE_FREE(err_attr_name);
1729                 }
1730
1731                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1732                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1733                         content_info->media_meta.rating = atoi(p);
1734                 } else {
1735                         SAFE_FREE(err_attr_name);
1736                         content_info->media_meta.rating = 0;
1737                 }
1738
1739                 /*Initialize album_id to 0. below code will set the album_id*/
1740                 content_info->album_id = album_id;
1741                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1742
1743                 if (ret != MS_MEDIA_ERR_NONE) {
1744                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1745                                 media_svc_debug("album does not exist. So start to make album art");
1746                                 extract_thumbnail = TRUE;
1747                                 append_album = TRUE;
1748                         } else {
1749                                 extract_thumbnail = TRUE;
1750                                 append_album = FALSE;
1751                         }
1752                 } else {
1753                         content_info->album_id = album_id;
1754                         append_album = FALSE;
1755
1756                         if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1757                             (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1758
1759                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1760                                 extract_thumbnail = TRUE;
1761                         } else {
1762                                 media_svc_debug("album already exists. don't need to make album art");
1763                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1764                                 extract_thumbnail = TRUE;
1765                         }
1766                 }
1767
1768                 /*Do not extract artwork for the USB Storage content*/
1769                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1770                         extract_thumbnail = FALSE;
1771
1772                 if (extract_thumbnail == TRUE) {
1773                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1774                         if (mmf_error != MM_ERROR_NONE) {
1775                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1776                                 SAFE_FREE(err_attr_name);
1777                         } else {
1778                                 /*media_svc_debug("artwork size1 [%d]", size); */
1779                         }
1780
1781                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1782                         if (mmf_error != MM_ERROR_NONE) {
1783                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1784                                 SAFE_FREE(err_attr_name);
1785                         } else {
1786                                 /*media_svc_debug("artwork size2 [%d]", size); */
1787                         }
1788                         if (image != NULL && size > 0) {
1789                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1790                                 int artwork_mime_size = -1;
1791
1792                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1793                                 if ((mmf_error == MM_ERROR_NONE) && (artwork_mime_size > 0)) {
1794                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1795                                         if (ret != MS_MEDIA_ERR_NONE) {
1796                                                 media_svc_error("Fail to Get Thumbnail Path");
1797                                         }
1798                                 } else {
1799                                         SAFE_FREE(err_attr_name);
1800                                 }
1801
1802                                 if (strlen(thumb_path) > 0) {
1803                                         ret = _media_svc_save_image(image, size, thumb_path, uid);
1804                                         if (ret != MS_MEDIA_ERR_NONE) {
1805                                                 media_svc_error("Fail to Save Thumbnail Image");
1806                                         } else {
1807                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1808                                                 if (ret != MS_MEDIA_ERR_NONE)
1809                                                         media_svc_error("strcpy error");
1810                                         }
1811                                 }
1812                         }
1813                 }
1814
1815                 if (append_album == TRUE) {
1816                         if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1817                             (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1818                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1819                         else
1820                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1821
1822                         content_info->album_id = album_id;
1823                 }
1824
1825                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1826                         double longitude = 0.0;
1827                         double latitude = 0.0;
1828                         double altitude = 0.0;
1829
1830                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1831                         content_info->media_meta.longitude = longitude;
1832                         content_info->media_meta.latitude = latitude;
1833                         content_info->media_meta.altitude = altitude;
1834
1835                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1836                         if (mmf_error != MM_ERROR_NONE) {
1837                                 cdis_value = 0;
1838                                 SAFE_FREE(err_attr_name);
1839                         }
1840
1841                         media_svc_debug("CDIS : %d", cdis_value);
1842                 }
1843
1844                 mmf_error = mm_file_destroy_tag_attrs(tag);
1845                 if (mmf_error != MM_ERROR_NONE) {
1846                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1847                 }
1848         }       else {
1849                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1850                 char *no_tag_title = NULL;
1851                 media_svc_error("no tag information");
1852
1853                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1854                 if (no_tag_title) {
1855                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1856                         if(ret != MS_MEDIA_ERR_NONE)
1857                                 media_svc_error("strcpy error");
1858                         SAFE_FREE(no_tag_title);
1859                 } else {
1860                         media_svc_error("Can't extract title");
1861                 }
1862
1863                 content_info->album_id = album_id;
1864         }
1865
1866         /*Get Content attribute ===========*/
1867         if (cdis_value == 1) {
1868                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1869         } else {
1870                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1871         }
1872
1873         if (mmf_error == MM_ERROR_NONE) {
1874                 /*Common attribute*/
1875                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1876                 if (mmf_error != MM_ERROR_NONE) {
1877                         SAFE_FREE(err_attr_name);
1878                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1879                 } else {
1880                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1881                 }
1882
1883                 /*Sound/Music attribute*/
1884                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1885
1886                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1887                         if (mmf_error != MM_ERROR_NONE) {
1888                                 SAFE_FREE(err_attr_name);
1889                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1890                         } else {
1891                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1892                         }
1893
1894                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1895                         if (mmf_error != MM_ERROR_NONE) {
1896                                 SAFE_FREE(err_attr_name);
1897                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1898                         } else {
1899                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1900                         }
1901
1902                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1903                         if (mmf_error != MM_ERROR_NONE) {
1904                                 SAFE_FREE(err_attr_name);
1905                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1906                         } else {
1907                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1908                         }
1909
1910                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1911                         if (mmf_error != MM_ERROR_NONE) {
1912                                 SAFE_FREE(err_attr_name);
1913                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1914                         } else {
1915                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1916                         }
1917                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1918                         int audio_bitrate = 0;
1919                         int video_bitrate = 0;
1920
1921                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1922                         if (mmf_error != MM_ERROR_NONE) {
1923                                 SAFE_FREE(err_attr_name);
1924                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1925                         } else {
1926                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1927                         }
1928
1929                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1930                         if (mmf_error != MM_ERROR_NONE) {
1931                                 SAFE_FREE(err_attr_name);
1932                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1933                         } else {
1934                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1935                         }
1936
1937                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1938
1939                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1940                         if (mmf_error != MM_ERROR_NONE) {
1941                                 SAFE_FREE(err_attr_name);
1942                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1943                         } else {
1944                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1945                         }
1946
1947                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1948                         if (mmf_error != MM_ERROR_NONE) {
1949                                 SAFE_FREE(err_attr_name);
1950                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1951                         } else {
1952                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1953                         }
1954
1955                 } else {
1956                         media_svc_error("Not support type");
1957                         mmf_error = mm_file_destroy_content_attrs(content);
1958                         if (mmf_error != MM_ERROR_NONE) {
1959                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1960                         }
1961                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1962                 }
1963
1964                 mmf_error = mm_file_destroy_content_attrs(content);
1965                 if (mmf_error != MM_ERROR_NONE) {
1966                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1967                 }
1968         } else {
1969                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1970         }
1971
1972         return MS_MEDIA_ERR_NONE;
1973 }
1974
1975 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1976 {
1977         media_svc_retm_if(content_info == NULL, "content info is NULL");
1978
1979         /* Delete media_svc_content_info_s */
1980         SAFE_FREE(content_info->media_uuid);
1981         SAFE_FREE(content_info->path);
1982         SAFE_FREE(content_info->file_name);
1983         SAFE_FREE(content_info->mime_type);
1984         SAFE_FREE(content_info->folder_uuid);
1985         SAFE_FREE(content_info->thumbnail_path);
1986         SAFE_FREE(content_info->storage_uuid);
1987
1988         /* Delete media_svc_content_meta_s */
1989         SAFE_FREE(content_info->media_meta.title);
1990         SAFE_FREE(content_info->media_meta.album);
1991         SAFE_FREE(content_info->media_meta.artist);
1992         SAFE_FREE(content_info->media_meta.album_artist);
1993         SAFE_FREE(content_info->media_meta.genre);
1994         SAFE_FREE(content_info->media_meta.composer);
1995         SAFE_FREE(content_info->media_meta.year);
1996         SAFE_FREE(content_info->media_meta.recorded_date);
1997         SAFE_FREE(content_info->media_meta.copyright);
1998         SAFE_FREE(content_info->media_meta.track_num);
1999         SAFE_FREE(content_info->media_meta.description);
2000         SAFE_FREE(content_info->media_meta.datetaken);
2001         SAFE_FREE(content_info->media_meta.exposure_time);
2002         SAFE_FREE(content_info->media_meta.model);
2003         SAFE_FREE(content_info->media_meta.weather);
2004
2005         SAFE_FREE(content_info->media_meta.title_pinyin);
2006         SAFE_FREE(content_info->media_meta.album_pinyin);
2007         SAFE_FREE(content_info->media_meta.artist_pinyin);
2008         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2009         SAFE_FREE(content_info->media_meta.genre_pinyin);
2010         SAFE_FREE(content_info->media_meta.composer_pinyin);
2011         SAFE_FREE(content_info->media_meta.copyright_pinyin);
2012         SAFE_FREE(content_info->media_meta.description_pinyin);
2013
2014         return;
2015 }
2016
2017 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2018 {
2019         if (STRING_VALID(path)) {
2020                 if (strncmp(path, _media_svc_get_path(uid), strlen(_media_svc_get_path(uid))) == 0) {
2021                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2022                 } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
2023                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2024                 } else if (strncmp (path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
2025                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2026                 } else {
2027                         media_svc_error("Invalid Path");
2028                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2029                 }
2030         } else {
2031                 media_svc_error("INVALID parameter");
2032                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2033         }
2034
2035         return MS_MEDIA_ERR_NONE;
2036 }
2037
2038 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2039 {
2040         char *result, *sr;
2041         size_t i, count = 0;
2042         size_t oldlen = strlen(olds);
2043         if (oldlen < 1) return s;
2044         size_t newlen = strlen(news);
2045
2046         if (newlen != oldlen) {
2047                 for (i = 0; s[i] != '\0';) {
2048                         if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2049                         else i++;
2050                 }
2051         } else i = strlen(s);
2052
2053
2054         result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2055         if (result == NULL) return NULL;
2056
2057         sr = result;
2058         while (*s) {
2059                 if (memcmp(s, olds, oldlen) == 0) {
2060                         memcpy(sr, news, newlen);
2061                         sr += newlen;
2062                         s  += oldlen;
2063                 } else *sr++ = *s++;
2064         }
2065
2066         *sr = '\0';
2067
2068         return result;
2069 }
2070
2071 bool _media_svc_is_drm_file(const char *path)
2072 {
2073         return FALSE;
2074 }
2075
2076 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)
2077 {
2078         int ret = MS_MEDIA_ERR_NONE;
2079
2080         ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2081
2082         if (ret != MS_MEDIA_ERR_NONE) {
2083                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2084                 ret = MS_MEDIA_ERR_INTERNAL;
2085         } else {
2086                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2087         }
2088
2089         return ret;
2090 }
2091
2092 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2093 {
2094         int ret = MS_MEDIA_ERR_NONE;
2095         int size = 0;
2096         pinyin_name_s *pinyinname = NULL;
2097
2098         *pinyin_str = NULL;
2099
2100         if (!STRING_VALID(src_str)) {
2101                 media_svc_debug("String is invalid");
2102                 return ret;
2103         }
2104
2105         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2106         if (ret == MS_MEDIA_ERR_NONE) {
2107                 if(STRING_VALID(pinyinname[0].pinyin_name))
2108                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
2109                 else
2110                         *pinyin_str = strdup(src_str);  //Return Original Non China Character
2111         }
2112
2113         _media_svc_pinyin_free(pinyinname, size);
2114
2115         return ret;
2116 }
2117
2118 bool _media_svc_check_pinyin_support(void)
2119 {
2120         /*Check CSC*/
2121         return TRUE;
2122 }
2123
2124 int _media_svc_get_ini_value()
2125 {
2126         if(g_ini_value == -1) {
2127                 dictionary *dict = NULL;
2128
2129                 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2130                 if(!dict) {
2131                         media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2132                         return -1;
2133                 }
2134
2135                 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation",g_ini_value, 0);
2136                 iniparser_freedict(dict);
2137         }
2138         media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2139
2140         return g_ini_value;
2141 }
2142
2143 char* _media_svc_get_title_from_path(const char *path)
2144 {
2145         char *filename = NULL;
2146         char *title = NULL;
2147         char    *ext = NULL;
2148         int filename_len = -1;
2149         int new_title_len = -1;
2150
2151         if (!path) {
2152                 media_svc_error("path is NULL");
2153                 return NULL;
2154         }
2155
2156         filename = g_path_get_basename(path);
2157         if (!STRING_VALID(filename)) {
2158                 media_svc_error("wrong file name");
2159                 SAFE_FREE(filename);
2160                 return NULL;
2161         }
2162
2163         filename_len = strlen(filename);
2164
2165         ext = g_strrstr(filename, ".");
2166         if (!ext) {
2167                 media_svc_error("there is no file extention");
2168                 return filename;
2169         }
2170
2171         new_title_len = filename_len - strlen(ext);
2172         if (new_title_len < 1) {
2173                 media_svc_error("title length is zero");
2174                 SAFE_FREE(filename);
2175                 return NULL;
2176         }
2177
2178         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2179
2180         SAFE_FREE(filename);
2181
2182         media_svc_debug("extract title is [%s]", title);
2183
2184         return title;
2185 }
2186
2187 #define BUF_LENGHT 256
2188
2189 void _media_svc_print_stderror(void)
2190 {
2191         char buf[BUF_LENGHT] = {0,};
2192
2193         media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
2194 }