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