Remove OMA DRM related code
[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 #ifndef __USE_XOPEN
25 #define DEF_XOPEN
26 #define __USE_XOPEN /* needed for strptime */
27 #endif
28 #include <time.h>
29 #ifdef DEF_XOPEN
30 #undef __USE_XOPEN
31 #endif
32 #include <string.h>
33 #include <sys/vfs.h>
34 #include <glib/gstdio.h>
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <ctype.h>
38 #include <aul/aul.h>
39 #include <mm_file.h>
40 #include <libexif/exif-data.h>
41 #include <media-util.h>
42 #include <uuid/uuid.h>
43 #include <mm_util_magick.h>
44 #include <media-thumbnail.h>
45 #include "media-util-err.h"
46 #include "media-svc-util.h"
47 #include "media-svc-db-utils.h"
48 #include "media-svc-debug.h"
49 #include "media-svc-env.h"
50 #include "media-svc-hash.h"
51 #include "media-svc-album.h"
52 /*For ebook metadata */
53 #include <zip.h>
54 #include <libxml/xmlmemory.h>
55 #include <libxml/parser.h>
56 #include <libxml/HTMLparser.h>
57 #include <dlfcn.h>
58
59 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
60
61 #define MUSIC_MIME_NUM 29
62 #define SOUND_MIME_NUM 2
63 #define MIME_TYPE_LENGTH 255
64 #define THUMB_HASH_LEN 256
65 #define MIME_LENGTH 50
66 #define MEDIA_SVC_ARTWORK_SIZE 2000
67 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
68 #define IMAGE_PREFIX "image/"
69 #define IMAGE_PREFIX_LEN 6
70 #define AUDIO_PREFIX "audio/"
71 #define AUDIO_PREFIX_LEN 6
72 #define VIDEO_PREFIX "video/"
73 #define VIDEO_PREFIX_LEN 6
74
75 #define MEDIA_SVC_DEFAULT_GPS_VALUE                     -200                    /**< Default GPS Value*/
76
77 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
78 #define MEDIA_SVC_PDF_BUF_SIZE 256
79
80 #define MEDIA_SVC_THUMB_WIDTH 320
81 #define MEDIA_SVC_THUMB_HEIGHT 240
82
83 #define PATH_PLUGIN_LIB                         PATH_LIBDIR"/libmedia-ebook-plugin.so"
84
85 enum Exif_Orientation {
86         NOT_AVAILABLE = 0,
87         NORMAL = 1,
88         HFLIP = 2,
89         ROT_180 = 3,
90         VFLIP = 4,
91         TRANSPOSE = 5,
92         ROT_90 = 6,
93         TRANSVERSE = 7,
94         ROT_270 = 8
95 };
96
97 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
98         /*known mime types of normal files*/
99         "mpeg",
100         "ogg",
101         "x-ms-wma",
102         "x-flac",
103         "mp4",
104         "mp3",
105         "x-mp3", /*alias of audio/mpeg*/
106         "x-mpeg", /*alias of audio/mpeg*/
107         "3gpp",
108         "x-ogg", /*alias of audio/ogg*/
109         "vnd.ms-playready.media.pya:*.pya", /*playready*/
110         "wma",
111         "aac",
112         "x-m4a", /*alias of audio/mp4*/
113         /* below mimes are rare*/
114         "x-vorbis+ogg",
115         "x-flac+ogg",
116         "x-matroska",
117         "ac3",
118         "mp2",
119         "x-ape",
120         "x-ms-asx",
121         "vnd.rn-realaudio",
122
123         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
124         "vorbis", /*alias of audio/x-vorbis+ogg*/
125         "x-oggflac",
126         "x-mp2", /*alias of audio/mp2*/
127         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
128         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
129         "x-wav",
130 };
131
132 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
133         "application/x-smaf",
134         "text/x-iMelody"
135 };
136
137 static char *__media_info_generate_uuid(void)
138 {
139         uuid_t uuid_value;
140         char uuid_unparsed[37];
141
142 RETRY_GEN:
143         uuid_generate(uuid_value);
144         uuid_unparse(uuid_value, uuid_unparsed);
145
146         if (strlen(uuid_unparsed) < 36) {
147                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
148                 goto RETRY_GEN;
149         }
150
151         return g_strdup(uuid_unparsed);
152 }
153
154 /* GPS information is not ExifTag member */
155 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
156 {
157         ExifEntry *entry;
158         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
159         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
160         int i = 0;
161         char **tmp_split = NULL;
162
163         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
164         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
165
166         entry = exif_data_get_entry(ed, tagtype);
167         media_svc_retv_if(!entry, MS_MEDIA_ERR_INTERNAL);
168
169         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
170         gps_buf[strlen(gps_buf)] = '\0';
171
172         tmp_split = g_strsplit(gps_buf, ",", -1);
173         if (g_strv_length(tmp_split) != 3) {
174                 g_strfreev(tmp_split);
175                 media_svc_error("Wrong GPS format");
176                 return MS_MEDIA_ERR_INTERNAL;
177         }
178
179         for (i = 0; i < 3; i++)
180                 tmp_arr[i] = g_strtod(tmp_split[i], NULL);
181
182         g_strfreev(tmp_split);
183
184         *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
185
186         return MS_MEDIA_ERR_NONE;
187 }
188
189 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
190 {
191         ExifEntry *entry;
192
193         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
194         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
195
196         entry = exif_data_get_entry(ed, tagtype);
197         if (entry) {
198                 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
199                 value[strlen(value)] = '\0';
200         }
201
202         return MS_MEDIA_ERR_NONE;
203 }
204
205 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
206 {
207         ExifEntry *entry;
208         ExifByteOrder mByteOrder;
209         ExifRational mRational;
210         long numerator, denominator;
211
212         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
213
214         entry = exif_data_get_entry(ed, tagtype);
215         media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
216
217         switch (tagtype) {
218         case EXIF_TAG_ORIENTATION:
219         case EXIF_TAG_PIXEL_X_DIMENSION:
220         case EXIF_TAG_PIXEL_Y_DIMENSION:
221         case EXIF_TAG_ISO_SPEED_RATINGS:
222                 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
223
224                 mByteOrder = exif_data_get_byte_order(ed);
225                 short exif_value = exif_get_short(entry->data, mByteOrder);
226                 *i_value = (int)exif_value;
227                 break;
228         case EXIF_TAG_EXPOSURE_TIME:
229                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
230
231                 mByteOrder = exif_data_get_byte_order(ed);
232                 mRational = exif_get_rational(entry->data, mByteOrder);
233                 numerator = mRational.numerator;
234                 denominator = mRational.denominator;
235                 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
236                 break;
237         case EXIF_TAG_FNUMBER:
238                 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
239
240                 mByteOrder = exif_data_get_byte_order(ed);
241                 mRational = exif_get_rational(entry->data, mByteOrder);
242                 numerator = mRational.numerator;
243                 denominator = mRational.denominator;
244
245                 *d_value = ((numerator*1.0)/(denominator*1.0));
246                 break;
247         default:
248                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
249
250                 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
251                 buf[strlen(buf)] = '\0';
252         }
253
254         return MS_MEDIA_ERR_NONE;
255 }
256
257 static time_t __media_svc_get_timeline_from_str(const char *timstr)
258 {
259         struct tm t;
260         time_t modified_t = 0;
261         time_t rawtime;
262         struct tm timeinfo;
263
264         if (!STRING_VALID(timstr)) {
265                 media_svc_error("Invalid Parameter");
266                 return 0;
267         }
268
269         /*Exif Format : %Y:%m:%d %H:%M:%S
270         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
271         memset(&t, 0x00, sizeof(struct tm));
272
273         tzset();
274         time(&rawtime);
275         localtime_r(&rawtime, &timeinfo);
276
277         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
278                 t.tm_isdst = timeinfo.tm_isdst;
279                 if (t.tm_isdst != 0)
280                         media_svc_debug("DST %d", t.tm_isdst);
281
282                 /* If time string has timezone */
283                 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
284                         char tim_tmp_str[255] = { 0, };
285
286                         /* ISO8601 Time string format */
287                         strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
288                         GDateTime *pdatetime = g_date_time_new_from_iso8601(tim_tmp_str, NULL);
289                         if (pdatetime)
290                                 modified_t = g_date_time_to_unix(pdatetime);
291                         g_date_time_unref(pdatetime);
292                         media_svc_debug("Calibrated timeval : [%ld][%s]", modified_t, tim_tmp_str);
293                 } else {
294                         /* Just localtime */
295                         modified_t = mktime(&t);
296                 }
297
298                 if (modified_t > 0)
299                         return modified_t;
300                 else
301                         media_svc_debug("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);
302         } else {
303                 media_svc_error("Failed to get timeline : [%s]", timstr);
304         }
305
306         return 0;
307 }
308
309 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
310 {
311         int idx = 0;
312         int audio = 0;
313         int video = 0;
314
315         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
316         media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
317         media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
318
319         /* Image */
320         if (strncmp(mime_type, IMAGE_PREFIX, IMAGE_PREFIX_LEN) == 0) {
321                 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
322                 return MS_MEDIA_ERR_NONE;
323         }
324
325         /* Audio */
326         if (strncmp(mime_type, AUDIO_PREFIX, AUDIO_PREFIX_LEN) == 0) {
327                 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
328
329                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
330                         if (strcmp(mime_type + AUDIO_PREFIX_LEN, music_mime_table[idx]) == 0) {
331                                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
332                                 break;
333                         }
334                 }
335
336                 /* audio/x-mpegurl : .m3u file (playlist file) */
337                 if (strcmp(mime_type + AUDIO_PREFIX_LEN, "x-mpegurl") == 0)
338                         *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
339
340                 return MS_MEDIA_ERR_NONE;
341         }
342
343         /* Video */
344         if (strncmp(mime_type, VIDEO_PREFIX, VIDEO_PREFIX_LEN) == 0) {
345                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
346
347                 /*some video files don't have video stream. in this case it is categorize as music. */
348                 if (strcmp(mime_type + VIDEO_PREFIX_LEN, "3gpp") == 0 ||
349                         strcmp(mime_type + VIDEO_PREFIX_LEN, "mp4") == 0) {
350                         if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
351                                 if (audio > 0 && video == 0)
352                                         *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
353                         }
354                 }
355
356                 return MS_MEDIA_ERR_NONE;
357         }
358
359         /* ETC */
360         *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
361
362         for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
363                 if (strcmp(mime_type, sound_mime_table[idx]) == 0) {
364                         *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
365                         return MS_MEDIA_ERR_NONE;
366                 }
367         }
368
369         /*"asf" must check video stream and then categorize in directly. */
370         if (strcmp(mime_type, "application/vnd.ms-asf") == 0) {
371                 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
372                         if (audio > 0 && video == 0)
373                                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
374                         else
375                                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
376                 }
377
378                 return MS_MEDIA_ERR_NONE;
379         }
380
381         if (strcmp(mime_type, "application/epub+zip") == 0 || strcmp(mime_type, "application/pdf") == 0)
382                 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
383
384         return MS_MEDIA_ERR_NONE;
385 }
386
387 static int __media_svc_get_mime_type(const char *path, char *mimetype)
388 {
389         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
390
391         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
392                 media_svc_error("aul_get_mime_from_file fail");
393                 return MS_MEDIA_ERR_INTERNAL;
394         }
395
396         return MS_MEDIA_ERR_NONE;
397 }
398
399 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
400 {
401         int i = 0;
402
403         for (i = strlen(file_path); i >= 0; i--) {
404                 if (file_path[i] == '.') {
405                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
406                         return true;
407                 }
408
409                 if (file_path[i] == '/')
410                         return false;
411         }
412         return false;
413 }
414
415 static int __media_svc_safe_atoi(char *buffer, int *si)
416 {
417         char *end = NULL;
418         errno = 0;
419         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
420
421         const long sl = strtol(buffer, &end, 10);
422
423         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
424         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
425         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
426         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
427         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
428
429         *si = (int)sl;
430
431         return MS_MEDIA_ERR_NONE;
432 }
433
434 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
435 {
436         int ret = MS_MEDIA_ERR_NONE;
437         struct statfs fs;
438         char *thumb_path = NULL;
439         long bsize_kbytes = 0;
440         GError *error = NULL;
441
442         media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
443         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
444
445         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
446         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
447
448         ret = statfs(thumb_path, &fs);
449         g_free(thumb_path);
450         media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
451
452         bsize_kbytes = fs.f_bsize >> 10;
453         media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
454
455         if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
456                 media_svc_error("g_file_set_contents faild:%s", error->message);
457                 g_error_free(error);
458                 return MS_MEDIA_ERR_INTERNAL;
459         }
460
461         return MS_MEDIA_ERR_NONE;
462 }
463
464 static char *__media_svc_get_title_from_filepath(const char *path)
465 {
466         char *filename = NULL;
467         char *title = NULL;
468         char *last_dot = NULL;
469
470         media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
471
472         filename = g_path_get_basename(path);
473
474         last_dot = strrchr(filename, '.');
475         if (last_dot) {
476                 title = g_strndup(filename, last_dot - filename);
477                 g_free(filename);
478         } else {
479                 title = filename;
480         }
481
482         media_svc_debug("extract title is [%s]", title);
483
484         return title;
485 }
486
487 void _media_svc_remove_file(const char *path)
488 {
489         if (!STRING_VALID(path))
490                 return;
491
492         if (remove(path) != 0)
493                 media_svc_stderror("fail to remove file result");
494 }
495
496 int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
497 {
498         int ret = MS_MEDIA_ERR_NONE;
499         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
500         char hash[THUMB_HASH_LEN] = {0, };
501         char *thumb_dir = NULL;
502
503         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
504         media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
505
506         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
507                 media_svc_error("Wrong path[%s]", thumb_dir);
508                 g_free(thumb_dir);
509                 return MS_MEDIA_ERR_INTERNAL;
510         }
511
512         memset(file_ext, 0, sizeof(file_ext));
513         if (!__media_svc_get_file_ext(pathname, file_ext))
514                 media_svc_error("get file ext fail");
515
516         ret = mb_svc_generate_hash_code(pathname, hash, THUMB_HASH_LEN);
517         if (ret != MS_MEDIA_ERR_NONE) {
518                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
519                 g_free(thumb_dir);
520                 return MS_MEDIA_ERR_INTERNAL;
521         }
522
523         if (img_format) {
524                 /* 'img_format' is mime-type */
525                 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
526                         media_svc_error("Not proper img format");
527                         g_free(thumb_dir);
528                         return MS_MEDIA_ERR_INTERNAL;
529                 }
530
531                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
532         } else {
533                 if (strcasecmp(file_ext, "PNG") == 0)
534                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
535                 else
536                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
537         }
538
539         g_free(thumb_dir);
540
541         return MS_MEDIA_ERR_NONE;
542 }
543
544 int _media_svc_get_file_time(const char *full_path)
545 {
546         struct stat statbuf = { 0, };
547
548         if (stat(full_path, &statbuf) == -1) {
549                 media_svc_stderror("stat fails.");
550                 return 0;
551         }
552
553         return statbuf.st_mtime;
554 }
555
556 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh)
557 {
558         int ret = MS_MEDIA_ERR_NONE;
559         char mime_type[256] = {0, };
560         media_svc_media_type_e media_type;
561
562         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
563         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
564
565         content_info->path = g_strdup(path);
566         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
567
568         struct stat st;
569         memset(&st, 0, sizeof(struct stat));
570         if (stat(path, &st) == 0) {
571                 content_info->modified_time = st.st_mtime;
572                 content_info->timeline = content_info->modified_time;
573                 content_info->size = st.st_size;
574         } else {
575                 media_svc_stderror("stat failed");
576         }
577
578         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
579         if (refresh) {
580                 media_svc_debug("refresh");
581                 return MS_MEDIA_ERR_NONE;
582         }
583
584         content_info->storage_uuid = g_strdup(storage_id);
585         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
586
587         content_info->storage_type = storage_type;
588         time(&content_info->added_time);
589
590         content_info->media_uuid = __media_info_generate_uuid();
591         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
592
593         content_info->file_name = g_path_get_basename(path);
594         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
595
596         ret = __media_svc_get_mime_type(path, mime_type);
597         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
598
599         media_svc_debug("mime [%s]", mime_type);
600
601         ret = __media_svc_get_media_type(path, mime_type, &media_type);
602         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
603
604         content_info->mime_type = g_strdup(mime_type);
605         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
606
607         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
608
609         content_info->media_type = media_type;
610
611         return MS_MEDIA_ERR_NONE;
612 }
613
614 static int __image_360_check(const char *path)
615 {
616         FILE *fp = NULL;
617         size_t size = 0, app1_size = 0, exif_app1_xmp_size = 0;
618         unsigned char exif_header[4] = {0, };
619         unsigned char exif_app1[2] = {0, };
620         unsigned char exif_app1_xmp[2] = {0, };
621         unsigned char exif_app1_xmp_t[2] = {0, };
622         GString *xmp_data = NULL;
623         int fdata = 0;
624         int result = 0;
625
626         memset(exif_header, 0x00, sizeof(exif_header));
627         memset(exif_app1, 0x00, sizeof(exif_app1));
628         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
629         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
630
631         fp = fopen(path, "rb");
632         if (fp == NULL)
633                 goto ERROR;
634
635         size = fread(exif_header, 1, sizeof(exif_header), fp);
636         if (size != sizeof(exif_header))
637                 goto ERROR;
638
639         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
640                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
641                 if (size != sizeof(exif_app1))
642                         goto ERROR;
643
644                 if ((size_t)((exif_app1[0] << 8) | (exif_app1[1])) <= 2)
645                         goto ERROR;
646
647                 app1_size = (size_t)((exif_app1[0] << 8) | (exif_app1[1])) - 2 ;
648                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
649                         goto ERROR;
650
651                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
652                 if (size != sizeof(exif_app1_xmp))
653                         goto ERROR;
654
655                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
656                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
657                         if (size != sizeof(exif_app1_xmp_t))
658                                 goto ERROR;
659
660                         if ((size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) <= 2)
661                                 goto ERROR;
662
663                         exif_app1_xmp_size = (size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) - 2;
664
665                         xmp_data = g_string_sized_new(exif_app1_xmp_size);
666
667                         do {
668                                 exif_app1_xmp_size--;
669                                 fdata = fgetc(fp);
670                                 if (fdata == EOF)
671                                         continue;
672                                 if (fdata == '\0')
673                                         continue;
674
675                                 xmp_data = g_string_append_c(xmp_data, (gchar)fdata);
676                         } while (exif_app1_xmp_size > 0);
677
678                         if (strstr(xmp_data->str, "UsePanoramaViewer") &&
679                                 strstr(xmp_data->str, "True") &&
680                                 strstr(xmp_data->str, "ProjectionType") &&
681                                 strstr(xmp_data->str, "equirectangular"))
682                                 result = 1;
683
684                         g_string_free(xmp_data, TRUE);
685                 }
686         }
687
688 ERROR:
689         if (fp) {
690                 fclose(fp);
691                 fp = NULL;
692         }
693
694         return result;
695 }
696
697 static char * __media_svc_get_title(MMHandleType tag, const char *path)
698 {
699         int ret = FILEINFO_ERROR_NONE;
700         char *p = NULL;
701         int size = 0;
702         char *title = NULL;
703
704         if (tag) {
705                 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
706                 if (ret == FILEINFO_ERROR_NONE && size > 0) {
707                         while(p && isspace(*p))
708                                 p++;
709
710                         return g_strdup(p);
711                 }
712         }
713
714         title = __media_svc_get_title_from_filepath(path);
715         if (title)
716                 return title;
717
718         media_svc_error("Can't extract title");
719
720         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
721 }
722
723 char * _media_svc_get_title_by_path(const char *path)
724 {
725         /* No MMHandleType in media-svc.c */
726         return __media_svc_get_title(NULL, path);
727 }
728
729 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
730 {
731         double value = 0.0;
732         int orient_value = 0;
733         int exif_width = 0;
734         int exif_height = 0;
735         ExifData *ed = NULL;
736         bool has_datetaken = false;
737         double fnumber = 0.0;
738         int iso = 0;
739         char *path = NULL;
740
741         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
742
743         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
744         media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
745         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
746
747         path = content_info->path;
748         content_info->media_meta.title = __media_svc_get_title(NULL, path);
749
750         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
751         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
752         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
753
754         /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
755         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
756         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
757         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
758         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
759         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
760         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
761         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
762         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
763
764         /* Load an ExifData object from an EXIF file */
765         ed = exif_data_new_from_file(path);
766         if (!ed) {
767                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
768                 goto GET_WIDTH_HEIGHT;
769         }
770
771         content_info->media_meta.is_360 = __image_360_check(path);
772
773         memset(buf, 0x00, sizeof(buf));
774         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
775                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
776                         if (!g_strcmp0(buf, "S"))
777                                 value *= -1;
778                         content_info->media_meta.latitude = value;
779                 }
780         }
781
782         memset(buf, 0x00, sizeof(buf));
783         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
784                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
785                         if (!g_strcmp0(buf, "W"))
786                                 value *= -1;
787
788                         content_info->media_meta.longitude = value;
789                 }
790         }
791
792         memset(buf, 0x00, sizeof(buf));
793         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
794                 if (strlen(buf) > 0)
795                         content_info->media_meta.description = g_strdup(buf);
796                 else
797                         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
798         }
799
800         memset(buf, 0x00, sizeof(buf));
801         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
802                 if (strlen(buf) > 0) {
803                         has_datetaken = true;
804                         content_info->media_meta.datetaken = g_strdup(buf);
805
806                         /* This is same as recorded_date */
807                         content_info->media_meta.recorded_date = g_strdup(buf);
808                 }
809         }
810
811         memset(buf, 0x00, sizeof(buf));
812         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
813                 if (strlen(buf) > 0) {
814                         has_datetaken = true;
815                         content_info->media_meta.datetaken = g_strdup(buf);
816
817                         /* This is same as recorded_date */
818                         content_info->media_meta.recorded_date = g_strdup(buf);
819                 }
820         }
821
822         if (has_datetaken) {
823                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
824                 if (content_info->timeline == 0)
825                         content_info->timeline = content_info->modified_time;
826                 else
827                         media_svc_debug("Timeline : %ld", content_info->timeline);
828         }
829
830         memset(buf, 0x00, sizeof(buf));
831         /* Get exposure_time value from exif. */
832         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
833                 if (strlen(buf) > 0)
834                         content_info->media_meta.exposure_time = g_strdup(buf);
835         }
836
837         /* Get fnumber value from exif. */
838         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
839                 content_info->media_meta.fnumber = fnumber;
840
841         /* Get iso value from exif. */
842         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
843                 content_info->media_meta.iso = iso;
844
845         memset(buf, 0x00, sizeof(buf));
846         /* Get model value from exif. */
847         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
848                 if (strlen(buf) > 0)
849                         content_info->media_meta.model = g_strdup(buf);
850         }
851
852         /* Get orientation value from exif. */
853         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
854                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
855                         content_info->media_meta.orientation = orient_value;
856         }
857
858         /* Get width value from exif. */
859         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
860                 if (exif_width > 0)
861                         content_info->media_meta.width = exif_width;
862         }
863
864         /* Get height value from exif. */
865         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
866                 if (exif_height > 0)
867                         content_info->media_meta.height = exif_height;
868         }
869
870         if (ed)
871                 exif_data_unref(ed);
872
873 GET_WIDTH_HEIGHT:
874
875         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
876                 /*Get image width, height*/
877                 unsigned int img_width = 0;
878                 unsigned int img_height = 0;
879                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
880
881                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
882                 if (content_info->media_meta.width == 0)
883                         content_info->media_meta.width = img_width;
884
885                 if (content_info->media_meta.height == 0)
886                         content_info->media_meta.height = img_height;
887         }
888
889         return MS_MEDIA_ERR_NONE;
890 }
891
892 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
893 {
894         int ret = FILEINFO_ERROR_NONE;
895         char *p = NULL;
896         int size = 0;
897
898         ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
899         if (ret == FILEINFO_ERROR_NONE && size > 0)
900                 return g_strdup(p);
901
902         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
903 }
904
905 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
906 {
907         MMHandleType tag = 0;
908         int mmf_error = FILEINFO_ERROR_NONE;
909
910         content_info->path = g_strdup(path);
911
912         mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
913         if (mmf_error == FILEINFO_ERROR_NONE) {
914                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
915                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
916                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
917                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
918                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
919                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
920                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
921                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
922         } else {
923                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
924         }
925
926         mm_file_destroy_tag_attrs(tag);
927
928         return MS_MEDIA_ERR_NONE;
929 }
930
931 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
932 {
933         MMHandleType content = 0;
934         MMHandleType tag = 0;
935         char *p = NULL;
936         unsigned char *image = NULL;
937         unsigned int size = 0;
938         int mmf_error = FILEINFO_ERROR_NONE;
939         int album_id = 0;
940         int ret = MS_MEDIA_ERR_NONE;
941         int convert_value = 0;
942         int cdis_value = 0;
943
944         /*Get Content Tag attribute ===========*/
945         if (content_info->storage_type == MS_USER_STORAGE_EXTERNAL_USB)
946                 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
947         else
948                 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
949
950         if (mmf_error == FILEINFO_ERROR_NONE) {
951                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
952                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
953                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
954                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
955                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
956                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
957                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
958                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
959                 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
960
961                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
962                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
963                         if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
964                                 /*Creation time format is 2013-01-01 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
965                                 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
966                                 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
967                                 g_free(p_value);
968                         } else {
969                                 content_info->media_meta.recorded_date = g_strdup(p);
970                         }
971
972                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
973                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
974                                 if (content_info->timeline == 0)
975                                         content_info->timeline = content_info->modified_time;
976
977                                 /* This is same as datetaken */
978                                 /* Remove compensation string */
979                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
980                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
981                                         g_free(content_info->media_meta.recorded_date);
982                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
983                                 } else {
984                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
985                                 }
986                         }
987                 }
988
989                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
990                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
991                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
992                                 content_info->media_meta.year = g_strdup(p);
993                 }
994
995                 if (!content_info->media_meta.year)
996                                 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
997
998                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
999                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1000                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1001                                 content_info->media_meta.rating = convert_value;
1002                 } else {
1003                         content_info->media_meta.rating = 0;
1004                 }
1005
1006                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1007
1008                 /*Do not extract artwork for the USB Storage content*/
1009                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1010                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1011                         if (mmf_error != FILEINFO_ERROR_NONE)
1012                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1013
1014                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1015                         if (mmf_error != FILEINFO_ERROR_NONE)
1016                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1017
1018                         if (image != NULL && size > 0) {
1019                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1020                                 int artwork_mime_size = -1;
1021
1022                                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1023                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1024                                         ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
1025                                         if (ret != MS_MEDIA_ERR_NONE) {
1026                                                 media_svc_error("Fail to Get Thumbnail Path");
1027                                         } else {
1028                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1029                                                 if (ret != MS_MEDIA_ERR_NONE) {
1030                                                         media_svc_error("Fail to Save Image");
1031                                                 } else {
1032                                                         content_info->thumbnail_path = g_strdup(thumb_path);
1033                                                 }
1034                                         }
1035                                 }
1036                         }
1037                 }
1038
1039                 /*Initialize album_id to 0. below code will set the album_id*/
1040                 content_info->album_id = album_id;
1041                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1042                 if (ret != MS_MEDIA_ERR_NONE) {
1043                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1044                                 media_svc_debug("album does not exist. So start to make album art");
1045                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1046                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1047                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1048                                 else
1049                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1050
1051                                 content_info->album_id = album_id;
1052                         }
1053                 } else {
1054                         content_info->album_id = album_id;
1055                 }
1056
1057                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1058                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1059                 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1060
1061                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1062                         double longitude = 0.0;
1063                         double latitude = 0.0;
1064                         double altitude = 0.0;
1065
1066                         mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1067                                 MM_FILE_TAG_LATIDUE, &latitude,
1068                                 MM_FILE_TAG_ALTIDUE, &altitude,
1069                                 NULL);
1070
1071                         content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1072                         content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1073                         content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1074
1075                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1076                         if (mmf_error != FILEINFO_ERROR_NONE)
1077                                 cdis_value = 0;
1078
1079                         media_svc_debug("CDIS : %d", cdis_value);
1080
1081                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1082                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1083                                 content_info->media_meta.orientation = atoi(p);
1084                         } else {
1085                                 content_info->media_meta.orientation = 0;
1086                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1087                         }
1088                 }
1089
1090                 mmf_error = mm_file_destroy_tag_attrs(tag);
1091                 if (mmf_error != FILEINFO_ERROR_NONE)
1092                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1093         }       else {
1094                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1095                 content_info->album_id = album_id;
1096         }
1097
1098         /*Get Content attribute ===========*/
1099         if (cdis_value == 1)
1100                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1101         else
1102                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1103
1104         media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1105
1106         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1107                 int audio_bitrate = 0;
1108                 int video_bitrate = 0;
1109
1110                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1111                         MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1112                         MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1113                         MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1114                         MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1115                         NULL);
1116
1117                 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1118         } else {
1119                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1120                         MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1121                         MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1122                         MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1123                         MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1124                         NULL);
1125         }
1126
1127         mm_file_destroy_content_attrs(content);
1128
1129         return MS_MEDIA_ERR_NONE;
1130 }
1131
1132 static gchar * __media_svc_get_zipfile_string(zip_t *z, const char *fname)
1133 {
1134         int err = 0;
1135         zip_int64_t index_num = 0;
1136         zip_file_t *file = NULL;
1137         zip_stat_t sb = {0, };
1138         gchar *buf = NULL;
1139
1140         media_svc_retvm_if(!z, NULL, "z is NULL");
1141         media_svc_retvm_if(!fname, NULL, "fname is NULL");
1142
1143         index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
1144         media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
1145
1146         err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
1147         media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
1148
1149         file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
1150         media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
1151
1152         buf = g_malloc0(sb.size + 1);
1153
1154         err = zip_fread(file, buf, sb.size);
1155         zip_fclose(file);
1156
1157         if (err == -1) {
1158                 g_free(buf);
1159                 buf = NULL;
1160         }
1161
1162         return buf;
1163 }
1164
1165 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
1166 {
1167         xmlNodePtr tmp = NULL;
1168
1169         media_svc_retvm_if(!node, NULL, "node is NULL");
1170         media_svc_retvm_if(!key, NULL, "key is NULL");
1171
1172         for (tmp = node->children; tmp; tmp = tmp->next) {
1173                 if (xmlIsBlankNode(tmp))
1174                         continue;
1175
1176                 if (g_str_has_suffix((gchar *)tmp->name, key))
1177                         return tmp;
1178         }
1179
1180         return NULL;
1181 }
1182
1183 static char * __media_svc_remove_escape_c(const char *value)
1184 {
1185         int start = -1;
1186         int end = 0;
1187         int len, i;
1188
1189         media_svc_retv_if(!value, NULL);
1190
1191         len = strlen(value);
1192
1193         for (i = 0; i < len; i++) {
1194                 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
1195                         if (start == -1)
1196                                 start = i;
1197
1198                         end = i;
1199                 }
1200         }
1201
1202         end = end - start + 1;
1203
1204         return g_strndup(value + start, end);
1205 }
1206
1207 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
1208 {
1209         xmlNodePtr tmp = NULL;
1210         char *tmp_res = NULL;
1211         char *res = NULL;
1212
1213         media_svc_retvm_if(!node, NULL, "node is NULL");
1214         media_svc_retvm_if(!key, NULL, "key is NULL");
1215
1216         for (tmp = node->children; tmp; tmp = tmp->next) {
1217                 if (xmlIsBlankNode(tmp))
1218                         continue;
1219
1220                 if (tmp->children) {
1221                         tmp_res = __media_svc_find_and_get_value(tmp, key);
1222                         if (tmp_res) {
1223                                 res = __media_svc_remove_escape_c(tmp_res);
1224                                 xmlFree(tmp_res);
1225                                 return res;
1226                         }
1227                 }
1228
1229                 if (g_str_has_suffix((gchar *)tmp->name, key))
1230                         return (char *)xmlNodeGetContent(tmp);
1231         }
1232
1233         return NULL;
1234 }
1235
1236 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
1237 {
1238         gchar *buf = NULL;
1239         gchar *tmp_buf = NULL;
1240         xmlDocPtr doc = NULL;
1241         xmlNodePtr node = NULL;
1242
1243         media_svc_retvm_if(!z, FALSE, "z is NULL");
1244         media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
1245
1246         buf = __media_svc_get_zipfile_string(z, "META-INF/container.xml");
1247         media_svc_retvm_if(!buf, FALSE, "buf is NULL");
1248
1249         tmp_buf = g_strrstr(buf, ">");
1250         if (tmp_buf)
1251                 *(tmp_buf + 1) = '\0';
1252
1253         doc = xmlParseDoc((const xmlChar *)buf);
1254         g_free(buf);
1255         media_svc_retvm_if(!doc, FALSE, "doc is NULL");
1256
1257         node = xmlDocGetRootElement(doc);
1258         node = __media_svc_find_node(node, "rootfiles");
1259         node = __media_svc_find_node(node, "rootfile");
1260
1261         *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
1262         media_svc_sec_debug("OPF [%s]", *opf_file);
1263         xmlFreeDoc(doc);
1264
1265         return TRUE;
1266 }
1267
1268 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
1269 {
1270         xmlDocPtr doc = NULL;
1271         xmlNodePtr root = NULL;
1272
1273         media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
1274         media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
1275
1276         doc = xmlParseDoc(buffer);
1277         media_svc_retv_if(!doc, FALSE);
1278
1279         root = xmlDocGetRootElement(doc);
1280         if (!root) {
1281                 xmlFreeDoc(doc);
1282                 return FALSE;
1283         }
1284
1285         content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
1286         if (is_pdf && !content_info->media_meta.title) {
1287                 xmlFreeDoc(doc);
1288                 return FALSE;
1289         }
1290
1291         content_info->media_meta.composer = __media_svc_find_and_get_value(root, "creator");
1292         if (!content_info->media_meta.composer)
1293                 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "author");
1294
1295         content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
1296         content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
1297         content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
1298         content_info->media_meta.description = __media_svc_find_and_get_value(root, "description");
1299
1300         xmlFreeDoc(doc);
1301
1302         return TRUE;
1303 }
1304
1305 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
1306 {
1307         int err = 0;
1308         zip_t *z = NULL;
1309         gchar *buf = NULL;
1310         char *opf_path = NULL;
1311
1312         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1313
1314         //1. open epub
1315         z = zip_open(content_info->path, ZIP_RDONLY, &err);
1316         media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
1317
1318         //2. find and read opf file
1319         if (!__media_svc_get_epub_root_file(z, &opf_path)) {
1320                 media_svc_error("__media_svc_get_epub_root_file failed");
1321                 zip_close(z);
1322                 return MS_MEDIA_ERR_INTERNAL;
1323         }
1324
1325         //3. get metadata
1326         buf = __media_svc_get_zipfile_string(z, opf_path);
1327         xmlFree(opf_path);
1328         zip_close(z);
1329         media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_string failed");
1330
1331         if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
1332                 media_svc_error("__media_svc_get_xml_metadata failed");
1333
1334         g_free(buf);
1335
1336         return MS_MEDIA_ERR_NONE;
1337 }
1338
1339 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
1340 {
1341     int fd = 0;
1342     int start_pos = 0;
1343     int end_pos = 0;
1344     int cur_pos = 0;
1345     int search_limit = 0;
1346     char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
1347     gchar *meta_buf = NULL;
1348     char *found = NULL;
1349
1350         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1351         media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
1352
1353         fd = open(content_info->path, O_RDONLY);
1354         media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
1355
1356         search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
1357
1358         while (cur_pos <= search_limit) {
1359                 if (lseek(fd, cur_pos, SEEK_SET) == -1)
1360                         break;
1361
1362                 memset(&tmp, 0x00, MEDIA_SVC_PDF_BUF_SIZE + 1);
1363
1364                 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
1365                         media_svc_error("read failed");
1366                         break;
1367                 }
1368
1369                 //1.Find <x:xmpmeta .. </x:xmpmeta> block
1370                 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
1371                         start_pos = cur_pos + (found - tmp);
1372 //                      media_svc_error("FIND START_POS[%d]", start_pos);
1373                         found = NULL;
1374                 }
1375
1376
1377                 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
1378                         end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
1379 //                      media_svc_error("FIND END_POS[%d]", end_pos);
1380                         found = NULL;
1381                 }
1382
1383                 //2.get metadata using xml parser
1384                 if (start_pos && end_pos) {
1385                         if (lseek(fd, start_pos, SEEK_SET) == -1)
1386                                 break;
1387
1388                         meta_buf = g_malloc0(end_pos - start_pos + 1);
1389
1390                         if (read(fd, meta_buf, end_pos - start_pos) == end_pos - start_pos) {
1391                                 if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
1392                                         g_free(meta_buf);
1393                                         break;
1394                                 }
1395                         }
1396
1397                         g_free(meta_buf);
1398
1399                         start_pos = 0;
1400                         end_pos = 0;
1401                 }
1402
1403                 cur_pos += 240;
1404
1405         }
1406
1407         close(fd);
1408
1409         return MS_MEDIA_ERR_NONE;
1410 }
1411
1412 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
1413 {
1414         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
1415
1416         if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
1417                 return __media_svc_get_epub_metadata(content_info);
1418         else
1419                 return __media_svc_get_pdf_metadata(content_info);
1420 }
1421
1422 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1423 {
1424         media_svc_retm_if(!content_info, "content info is NULL");
1425
1426         /* Delete media_svc_content_info_s */
1427         g_free(content_info->media_uuid);
1428         g_free(content_info->path);
1429         g_free(content_info->file_name);
1430         g_free(content_info->mime_type);
1431         g_free(content_info->thumbnail_path);
1432         g_free(content_info->storage_uuid);
1433
1434         /* Delete media_svc_content_meta_s */
1435         g_free(content_info->media_meta.title);
1436         g_free(content_info->media_meta.album);
1437         g_free(content_info->media_meta.artist);
1438         g_free(content_info->media_meta.album_artist);
1439         g_free(content_info->media_meta.genre);
1440         g_free(content_info->media_meta.composer);
1441         g_free(content_info->media_meta.year);
1442         g_free(content_info->media_meta.recorded_date);
1443         g_free(content_info->media_meta.copyright);
1444         g_free(content_info->media_meta.track_num);
1445         g_free(content_info->media_meta.description);
1446         g_free(content_info->media_meta.datetaken);
1447         g_free(content_info->media_meta.exposure_time);
1448         g_free(content_info->media_meta.model);
1449 }
1450
1451 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1452 {
1453         int ret = MS_MEDIA_ERR_NONE;
1454
1455         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1456         media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1457         media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1458
1459         ms_user_storage_type_e store_type = -1;
1460         ret = ms_user_get_storage_type(uid, path, &store_type);
1461
1462         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1463                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1464                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1465         }
1466
1467         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1468
1469         //1. make hash path
1470         ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
1471         if (ret != MS_MEDIA_ERR_NONE) {
1472                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1473                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1474                 return ret;
1475         }
1476
1477         //2. save thumbnail
1478         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1479                 return create_image_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
1480         else
1481                 return create_video_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
1482
1483 }
1484
1485 int _media_svc_get_media_type(const char *path, int *mediatype)
1486 {
1487         int ret = MS_MEDIA_ERR_NONE;
1488         char mime_type[256] = {0};
1489         media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1490
1491         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1492
1493         ret = __media_svc_get_mime_type(path, mime_type);
1494         if (ret == MS_MEDIA_ERR_NONE)
1495                 __media_svc_get_media_type(path, mime_type, &media_type);
1496         else
1497                 media_svc_error("__media_svc_get_mime_type failed");
1498
1499         *mediatype = media_type;
1500
1501         return ret;
1502 }
1503
1504 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1505 {
1506         switch (storage_type) {
1507         case MS_USER_STORAGE_INTERNAL:
1508         case MS_USER_STORAGE_EXTERNAL:
1509         case MS_USER_STORAGE_EXTERNAL_USB:
1510                 return true;
1511         default:
1512                 media_svc_error("storage type is incorrect[%d]", storage_type);
1513                 return false;
1514         }
1515 }
1516
1517 bool _media_svc_is_keyword_included(const char *path, const char *keyword)
1518 {
1519         bool ret = false;
1520         void *handle = NULL;
1521         bool (*svc_search) (const char *, const char *);
1522
1523         media_svc_retvm_if(!path, false, "Invalid path");
1524         media_svc_retvm_if(!keyword, false, "Invalid keyword");
1525
1526         handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
1527         media_svc_retvm_if(!handle, false, "dlopen failed");
1528
1529         if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1530                 svc_search = dlsym(handle, "media_svc_epub_is_keyword_included");
1531         else
1532                 svc_search = dlsym(handle, "media_svc_pdf_is_keyword_included");
1533
1534         if (!svc_search) {
1535                 media_svc_error("dlsym failed - %s", dlerror());
1536                 dlclose(handle);
1537                 return false;
1538         }
1539
1540         ret = svc_search(path, keyword);
1541         dlclose(handle);
1542
1543         return ret;
1544 }
1545
1546 static int __media_svc_create_wordbook_db(const char *path, sqlite3 **handle)
1547 {
1548         int ret = SQLITE_OK;
1549         sqlite3 *db_handle = NULL;
1550         char *err = NULL;
1551
1552         ret = sqlite3_open_v2(path, &db_handle, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
1553         media_svc_retvm_if(ret != SQLITE_OK, ret, "sqlite3_open_v2 failed : %d", ret);
1554
1555         ret = sqlite3_exec(db_handle, "PRAGMA journal_mode = OFF;", NULL, NULL, &err);
1556         if (ret != SQLITE_OK)
1557                 goto ERROR;
1558
1559         ret = sqlite3_exec(db_handle, "CREATE TABLE IF NOT EXISTS files(id integer primary key autoincrement, path text unique, validity integer default 1);", NULL, NULL, &err);
1560         if (ret != SQLITE_OK)
1561                 goto ERROR;
1562
1563         ret = sqlite3_exec(db_handle, "CREATE TABLE IF NOT EXISTS words(file_id integer, word text, frequency integer default 1, unique(file_id, word));", NULL, NULL, &err);
1564         if (ret != SQLITE_OK)
1565                 goto ERROR;
1566
1567         ret = sqlite3_exec(db_handle, "CREATE TRIGGER IF NOT EXISTS TR_files_words DELETE ON files BEGIN DELETE FROM words WHERE file_id = old.id;END;", NULL, NULL, &err);
1568         if (ret != SQLITE_OK)
1569                 goto ERROR;
1570
1571         *handle = db_handle;
1572
1573         return SQLITE_OK;
1574
1575 ERROR:
1576         media_svc_error("sqlite3_exec failed : %s", err);
1577         SQLITE3_SAFE_FREE(err);
1578         sqlite3_close_v2(db_handle);
1579
1580         return ret;
1581 }
1582
1583 static bool __media_svc_get_wordbook_handle(uid_t uid, sqlite3 **handle)
1584 {
1585         int ret = SQLITE_OK;
1586         char *db_path = NULL;
1587
1588         ms_user_get_wordbook_db_path(uid, &db_path);
1589         if (!db_path)
1590                 return false;
1591
1592         ret = sqlite3_open_v2(db_path, handle, SQLITE_OPEN_READWRITE, NULL);
1593         if (ret != SQLITE_OK) {
1594                 ret = __media_svc_create_wordbook_db(db_path, handle);
1595                 free(db_path);
1596                 media_svc_retvm_if(ret != SQLITE_OK, false, "__media_svc_create_wordbook_db failed : %d", ret);
1597         } else {
1598                 ret = sqlite3_exec(*handle, "PRAGMA journal_mode = OFF;", NULL, NULL, NULL);
1599                 if (ret != SQLITE_OK)
1600                         media_svc_error("Failed to change journal mode [%d]", ret);
1601         }
1602
1603         return true;
1604 }
1605
1606 static bool __media_svc_is_exist_in_wordbook(sqlite3 *db_handle, const char *path)
1607 {
1608         int ret = SQLITE_OK;
1609         char *err = NULL;
1610         char *query = NULL;
1611
1612         query = sqlite3_mprintf("UPDATE files SET validity=1 WHERE path = %Q", path);
1613
1614         ret = sqlite3_exec(db_handle, query, NULL, NULL, &err);
1615         SQLITE3_SAFE_FREE(query);
1616         if (ret != SQLITE_OK) {
1617                 media_svc_error("Query failed. [%s]", err);
1618                 SQLITE3_SAFE_FREE(err);
1619                 return false;
1620         }
1621
1622         return sqlite3_changes(db_handle) > 0 ? true : false;
1623 }
1624
1625 static void __media_svc_insert_to_wordbook(sqlite3 *db_handle, const char *path)
1626 {
1627         void *handle = NULL;
1628         void (*svc_update) (sqlite3 *, const char *);
1629         char *query = NULL;
1630
1631         query = sqlite3_mprintf("INSERT INTO files(path) VALUES(%Q);", path);
1632         sqlite3_exec(db_handle, query, NULL, NULL, NULL);
1633         sqlite3_free(query);
1634
1635         handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
1636         if (!handle) {
1637                 media_svc_error("dlopen failed");
1638                 return;
1639         }
1640
1641         if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1642                 svc_update = dlsym(handle, "media_svc_epub_insert_to_db");
1643         else
1644                 svc_update = dlsym(handle, "media_svc_pdf_insert_to_db");
1645
1646         if (!svc_update) {
1647                 media_svc_error("dlsym failed - %s", dlerror());
1648                 dlclose(handle);
1649                 return;
1650         }
1651
1652         svc_update(db_handle, path);
1653         dlclose(handle);
1654 }
1655
1656 void _media_svc_update_wordbook(const char *path, uid_t uid)
1657 {
1658         sqlite3 *db_handle = NULL;
1659
1660         if (!path) {
1661                 media_svc_error("Invalid path");
1662                 return;
1663         }
1664
1665         // check db..
1666         if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1667                 return;
1668
1669         if (__media_svc_is_exist_in_wordbook(db_handle, path)) {
1670                 sqlite3_close_v2(db_handle);
1671                 return;
1672         }
1673
1674         // if no item, insert to db..
1675         __media_svc_insert_to_wordbook(db_handle, path);
1676         sqlite3_close_v2(db_handle);
1677 }
1678
1679 void _media_svc_clean_wordbook(uid_t uid)
1680 {
1681         sqlite3 *db_handle = NULL;
1682
1683         if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1684                 return;
1685
1686         sqlite3_exec(db_handle, "DELETE FROM files where validity = 0;", NULL, NULL, NULL);
1687         sqlite3_exec(db_handle, "UPDATE files SET validity = 0;", NULL, NULL, NULL);
1688         sqlite3_close_v2(db_handle);
1689 }
1690
1691 bool _media_svc_get_matched_list(const char *keyword, uid_t uid, GList **list)
1692 {
1693         int ret = SQLITE_OK;
1694         sqlite3 *handle = NULL;
1695         sqlite3_stmt *stmt = NULL;
1696         char *query = NULL;
1697
1698         media_svc_retvm_if(!list, false, "list is NULL");
1699         media_svc_retvm_if(!keyword, false, "keyword is NULL");
1700         media_svc_retvm_if(!__media_svc_get_wordbook_handle(uid, &handle), false, "Failed to get handle");
1701
1702         query = sqlite3_mprintf("SELECT files.path FROM files JOIN (SELECT file_id, sum(frequency) AS freq_sum FROM words WHERE word LIKE '%q%%' GROUP BY file_id ORDER BY freq_sum DESC) w ON files.id = w.file_id;", keyword);
1703         ret = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1704         SQLITE3_SAFE_FREE(query);
1705
1706         if (ret != SQLITE_OK) {
1707                 media_svc_error("Query failed[%d]", ret);
1708                 sqlite3_close_v2(handle);
1709                 return false;
1710         }
1711
1712         while (sqlite3_step(stmt) == SQLITE_ROW)
1713                 *list = g_list_append(*list, g_strdup((char *)sqlite3_column_text(stmt, 0)));
1714
1715         sqlite3_finalize(stmt);
1716         sqlite3_close_v2(handle);
1717
1718         return true;
1719 }