Fix sign-compare
[platform/core/multimedia/libmedia-service.git] / src / 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 #include <string.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/vfs.h>
26 #include <ctype.h>
27 #include <aul/aul.h>
28 #include <mm_file.h>
29 #include <libexif/exif-data.h>
30 #include <uuid/uuid.h>
31 #include <media-thumbnail.h>
32 #include <media-util-user.h>
33 #include "media-svc-util.h"
34 #include "media-svc-db-utils.h"
35 #include "media-svc-debug.h"
36 #include "media-svc-env.h"
37 #include "media-svc-album.h"
38 /*For ebook metadata */
39 #include <zip.h>
40 #include <libxml/xmlmemory.h>
41 #include <libxml/parser.h>
42 #include <libxml/HTMLparser.h>
43 #include <dlfcn.h>
44
45 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6
46
47 #define MUSIC_MIME_NUM 29
48 #define SOUND_MIME_NUM 2
49 #define MIME_LENGTH 50
50 #define IMAGE_PREFIX "image/"
51 #define AUDIO_PREFIX "audio/"
52 #define VIDEO_PREFIX "video/"
53 #define PREFIX_LEN 6
54
55 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
56 #define MEDIA_SVC_PDF_BUF_SIZE 256
57
58 #define MEDIA_SVC_THUMB_WIDTH 320
59 #define MEDIA_SVC_THUMB_HEIGHT 240
60
61 #define PATH_PLUGIN_LIB                         PATH_LIBDIR"/libmedia-ebook-plugin.so"
62
63 enum Exif_Orientation {
64         NOT_AVAILABLE = 0,
65         NORMAL = 1,
66         HFLIP = 2,
67         ROT_180 = 3,
68         VFLIP = 4,
69         TRANSPOSE = 5,
70         ROT_90 = 6,
71         TRANSVERSE = 7,
72         ROT_270 = 8
73 };
74
75 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
76         /*known mime types of normal files*/
77         "mpeg",
78         "ogg",
79         "x-ms-wma",
80         "x-flac",
81         "mp4",
82         "mp3",
83         "x-mp3", /*alias of audio/mpeg*/
84         "x-mpeg", /*alias of audio/mpeg*/
85         "3gpp",
86         "x-ogg", /*alias of audio/ogg*/
87         "vnd.ms-playready.media.pya:*.pya", /*playready*/
88         "wma",
89         "aac",
90         "x-m4a", /*alias of audio/mp4*/
91         /* below mimes are rare*/
92         "x-vorbis+ogg",
93         "x-flac+ogg",
94         "x-matroska",
95         "ac3",
96         "mp2",
97         "x-ape",
98         "x-ms-asx",
99         "vnd.rn-realaudio",
100         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
101         "vorbis", /*alias of audio/x-vorbis+ogg*/
102         "x-oggflac",
103         "x-mp2", /*alias of audio/mp2*/
104         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
105         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
106         "x-wav",
107 };
108
109 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
110         "application/x-smaf",
111         "text/x-iMelody"
112 };
113
114 static char *__media_info_generate_uuid(void)
115 {
116         uuid_t uuid_value;
117         char uuid_unparsed[37];
118
119 RETRY_GEN:
120         uuid_generate(uuid_value);
121         uuid_unparse(uuid_value, uuid_unparsed);
122
123         if (strlen(uuid_unparsed) < 36) {
124                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
125                 goto RETRY_GEN;
126         }
127
128         return g_strdup(uuid_unparsed);
129 }
130
131 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
132 {
133         int idx = 0;
134         int audio = 0;
135         int video = 0;
136
137         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
138         media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
139         media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
140
141         /* Image */
142         if (strncmp(mime_type, IMAGE_PREFIX, PREFIX_LEN) == 0) {
143                 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
144                 return MS_MEDIA_ERR_NONE;
145         }
146
147         /* Audio */
148         if (strncmp(mime_type, AUDIO_PREFIX, PREFIX_LEN) == 0) {
149                 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
150
151                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
152                         if (strcmp(mime_type + PREFIX_LEN, music_mime_table[idx]) == 0) {
153                                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
154                                 break;
155                         }
156                 }
157
158                 /* audio/x-mpegurl : .m3u file (playlist file) */
159                 if (strcmp(mime_type + PREFIX_LEN, "x-mpegurl") == 0)
160                         *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
161
162                 return MS_MEDIA_ERR_NONE;
163         }
164
165         /* Video */
166         if (strncmp(mime_type, VIDEO_PREFIX, PREFIX_LEN) == 0) {
167                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
168
169                 /*some video files don't have video stream. in this case it is categorize as music. */
170                 if (strcmp(mime_type + PREFIX_LEN, "3gpp") == 0 ||
171                         strcmp(mime_type + PREFIX_LEN, "mp4") == 0) {
172                         if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
173                                 if (audio > 0 && video == 0)
174                                         *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
175                         }
176                 }
177
178                 return MS_MEDIA_ERR_NONE;
179         }
180
181         /* ETC */
182         *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
183
184         for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
185                 if (strcmp(mime_type, sound_mime_table[idx]) == 0) {
186                         *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
187                         return MS_MEDIA_ERR_NONE;
188                 }
189         }
190
191         /*"asf" must check video stream and then categorize in directly. */
192         if (strcmp(mime_type, "application/vnd.ms-asf") == 0) {
193                 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
194                         if (audio > 0 && video == 0)
195                                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
196                         else
197                                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
198                 }
199
200                 return MS_MEDIA_ERR_NONE;
201         }
202
203         if (strcmp(mime_type, "application/epub+zip") == 0 || strcmp(mime_type, "application/pdf") == 0)
204                 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
205
206         return MS_MEDIA_ERR_NONE;
207 }
208
209 static int __media_svc_get_mime_type(const char *path, char *mimetype)
210 {
211         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
212
213         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
214                 media_svc_error("aul_get_mime_from_file fail");
215                 return MS_MEDIA_ERR_INTERNAL;
216         }
217
218         return MS_MEDIA_ERR_NONE;
219 }
220
221 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
222 {
223         int i = 0;
224
225         for (i = strlen(file_path); i >= 0; i--) {
226                 if (file_path[i] == '.') {
227                         g_strlcpy(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
228                         return true;
229                 }
230
231                 if (file_path[i] == '/')
232                         return false;
233         }
234         return false;
235 }
236
237 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
238 {
239         int ret = MS_MEDIA_ERR_NONE;
240         struct statfs fs;
241         char *thumb_path = NULL;
242         long bsize_kbytes = 0;
243         GError *error = NULL;
244
245         media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
246         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
247
248         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
249         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
250
251         ret = statfs(thumb_path, &fs);
252         g_free(thumb_path);
253         media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
254
255         bsize_kbytes = fs.f_bsize >> 10;
256         media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
257
258         if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
259                 media_svc_error("g_file_set_contents faild:%s", error->message);
260                 g_error_free(error);
261                 return MS_MEDIA_ERR_INTERNAL;
262         }
263
264         return MS_MEDIA_ERR_NONE;
265 }
266
267 static char *__media_svc_get_title_from_filename(const char *filename)
268 {
269         char *title = NULL;
270         char *last_dot = NULL;
271
272         media_svc_retvm_if(!STRING_VALID(filename), g_strdup(MEDIA_SVC_TAG_UNKNOWN), "Invalid path");
273
274         last_dot = strrchr(filename, '.');
275         if (last_dot) {
276                 title = g_strndup(filename, last_dot - filename);
277         } else {
278                 title = g_strdup(filename);
279         }
280
281         media_svc_debug("extract title is [%s]", title);
282
283         return title;
284 }
285
286 void _media_svc_remove_file(const char *path)
287 {
288         if (!STRING_VALID(path))
289                 return;
290
291         if (remove(path) != 0)
292                 media_svc_stderror("fail to remove file result");
293 }
294
295 static int __media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
296 {
297         int ret = MS_MEDIA_ERR_NONE;
298         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
299         g_autofree gchar *hash = NULL;
300         g_autofree gchar *thumb_dir = NULL;
301
302         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
303         media_svc_retvm_if(!STRING_VALID(thumb_dir), ret, "ms_user_get_root_thumb_store_path failed");
304         media_svc_retvm_if(!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR), MS_MEDIA_ERR_INTERNAL, "Not a directory");
305
306         memset(file_ext, 0, sizeof(file_ext));
307         if (!__media_svc_get_file_ext(pathname, file_ext))
308                 media_svc_error("get file ext fail");
309
310         hash = g_compute_checksum_for_string(G_CHECKSUM_MD5, pathname, -1);
311         media_svc_retvm_if(!hash, MS_MEDIA_ERR_INTERNAL, "Failed to create hashname");
312
313         if (img_format) {
314                 /* 'img_format' is mime-type */
315                 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == PREFIX_LEN) {
316                         media_svc_error("Not proper img format");
317                         return MS_MEDIA_ERR_INTERNAL;
318                 }
319
320                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + PREFIX_LEN);
321         } else {
322                 if (strcasecmp(file_ext, "PNG") == 0)
323                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
324                 else
325                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
326         }
327
328         return MS_MEDIA_ERR_NONE;
329 }
330
331 int _media_svc_get_file_time(const char *full_path)
332 {
333         struct stat statbuf = { 0, };
334
335         if (stat(full_path, &statbuf) == -1) {
336                 media_svc_stderror("stat fails.");
337                 return 0;
338         }
339
340         return statbuf.st_mtime;
341 }
342
343 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, const char *path, bool refresh)
344 {
345         int ret = MS_MEDIA_ERR_NONE;
346         char mime_type[256] = {0, };
347         media_svc_media_type_e media_type;
348         struct stat st = { 0, };
349
350         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
351
352         content_info->path = g_strdup(path);
353         content_info->file_name = g_path_get_basename(path);
354
355         if (stat(path, &st) == 0) {
356                 content_info->modified_time = st.st_mtime;
357                 content_info->size = st.st_size;
358         } else {
359                 media_svc_stderror("stat failed");
360         }
361
362         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
363         if (refresh) {
364                 media_svc_debug("refresh");
365                 return MS_MEDIA_ERR_NONE;
366         }
367
368         content_info->storage_uuid = g_strdup(storage_id);
369         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
370
371         content_info->media_uuid = __media_info_generate_uuid();
372         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
373
374         ret = __media_svc_get_mime_type(path, mime_type);
375         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
376
377         media_svc_debug("mime [%s]", mime_type);
378
379         ret = __media_svc_get_media_type(path, mime_type, &media_type);
380         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
381
382         content_info->mime_type = g_strdup(mime_type);
383         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
384
385         media_svc_sec_debug("path[%s], media_type[%d]", content_info->path, media_type);
386
387         content_info->media_type = media_type;
388
389         return MS_MEDIA_ERR_NONE;
390 }
391
392 static char * __media_svc_get_title(MMHandleType tag, const char *filename)
393 {
394         int ret = FILEINFO_ERROR_NONE;
395         char *p = NULL;
396         int size = 0;
397
398         if (tag) {
399                 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
400                 if (ret == FILEINFO_ERROR_NONE && size > 0) {
401                         while(p && isspace(*p))
402                                 p++;
403
404                         return g_strdup(p);
405                 }
406         }
407
408         return __media_svc_get_title_from_filename(filename);
409 }
410
411 char * _media_svc_get_title_from_filename(const char *filename)
412 {
413         return __media_svc_get_title_from_filename(filename);
414 }
415
416 static char * __media_svc_get_exif_datetaken(ExifData *ed)
417 {
418         ExifEntry *entry;
419         char tmp[MEDIA_SVC_METADATA_LEN_MAX + 1] = { 0, };
420
421         media_svc_retv_if(!ed, NULL);
422
423         entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME_ORIGINAL);
424         if (entry) {
425                 exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
426                 if (strlen(tmp) > 0)
427                         return g_strdup(tmp);
428         }
429
430         entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME);
431         if (entry) {
432                 exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
433                 if (strlen(tmp) > 0)
434                         return g_strdup(tmp);
435         }
436
437         return NULL;
438 }
439
440 static bool __media_svc_get_exif_short(ExifData *ed, ExifTag tagtype, unsigned short *value)
441 {
442         ExifEntry *entry;
443
444         media_svc_retv_if(!ed, false);
445         media_svc_retvm_if(!value, false, "value is NULL");
446
447         entry = exif_data_get_entry(ed, tagtype);
448         media_svc_retv_if(!entry, false);
449         *value = exif_get_short(entry->data, exif_data_get_byte_order(ed));
450
451         return true;
452 }
453
454 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
455 {
456         unsigned short orient_value = 0;
457         unsigned short exif_width = 0;
458         unsigned short exif_height = 0;
459         ExifData *ed = NULL;
460
461         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
462         media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type");
463         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
464
465         content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
466
467         /* Load an ExifData object from an EXIF file */
468         ed = exif_data_new_from_file(content_info->path);
469         if (!ed) {
470                 media_svc_sec_debug("There is no exif data in [ %s ]", content_info->path);
471                 goto GET_WIDTH_HEIGHT;
472         }
473
474         content_info->media_meta.datetaken = __media_svc_get_exif_datetaken(ed);
475         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
476         if (content_info->media_meta.recorded_date == NULL)
477                 content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
478
479         if (__media_svc_get_exif_short(ed, EXIF_TAG_ORIENTATION, &orient_value)) {
480                 if (orient_value <= ROT_270)
481                         content_info->media_meta.orientation = orient_value;
482         }
483
484         if (__media_svc_get_exif_short(ed, EXIF_TAG_PIXEL_X_DIMENSION, &exif_width))
485                 content_info->media_meta.width = (unsigned int)exif_width;
486
487         if (__media_svc_get_exif_short(ed, EXIF_TAG_PIXEL_Y_DIMENSION, &exif_height))
488                 content_info->media_meta.height = (unsigned int)exif_height;
489
490         exif_data_unref(ed);
491
492 GET_WIDTH_HEIGHT:
493         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
494                 /*Get image width, height*/
495                 unsigned int img_width = 0;
496                 unsigned int img_height = 0;
497
498                 if (get_image_info(content_info->path, &img_width, &img_height) != THUMB_OK)
499                         return MS_MEDIA_ERR_NONE;
500
501                 if (content_info->media_meta.width == 0)
502                         content_info->media_meta.width = img_width;
503
504                 if (content_info->media_meta.height == 0)
505                         content_info->media_meta.height = img_height;
506         }
507
508         return MS_MEDIA_ERR_NONE;
509 }
510
511 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
512 {
513         int ret = FILEINFO_ERROR_NONE;
514         char *p = NULL;
515         int size = 0;
516
517         ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
518         if (ret == FILEINFO_ERROR_NONE && size > 0)
519                 return g_strdup(p);
520
521         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
522 }
523
524 static char * __media_svc_extract_albumart(MMHandleType tag, const char *path, uid_t uid)
525 {
526         int ret = FILEINFO_ERROR_NONE;
527         unsigned char *image = NULL;
528         unsigned int size = 0;
529         char *mimetype = NULL;
530         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = { 0, };
531         unsigned int mime_size = 0;
532
533         ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
534         media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag artwork[%d]", ret);
535         media_svc_retvm_if(!image || size == 0, NULL, "Invalid artwork");
536
537         ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &mimetype, &mime_size, NULL);
538         media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag mime[%d]", ret);
539         media_svc_retvm_if(mime_size == 0, NULL, "Invalid mimetype");
540
541         ret = __media_svc_get_thumbnail_path(thumb_path, path, mimetype, uid);
542         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Failed to get thumbnail path");
543
544         ret = __media_svc_save_image(image, size, thumb_path, uid);
545         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Fail to save thumbnail");
546
547         return g_strdup(thumb_path);
548 }
549
550 void _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
551 {
552         MMHandleType tag = 0;
553         char *p = NULL;
554         unsigned int size = 0;
555         int mmf_error = FILEINFO_ERROR_NONE;
556         int album_id = 0;
557         int ret = MS_MEDIA_ERR_NONE;
558         bool support_albumart = ms_user_thumb_support(uid, content_info->path);
559
560         /*Get Content Tag attribute ===========*/
561         if (support_albumart)
562                 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
563         else
564                 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
565
566         if (mmf_error != FILEINFO_ERROR_NONE) {
567                 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
568                 content_info->album_id = 0;
569                 return;
570         }
571
572         content_info->media_meta.title = __media_svc_get_title(tag, content_info->file_name);
573         content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
574         content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
575         content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
576         content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
577         content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
578
579         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
580         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
581                 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
582                         /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
583                         char *p_value = g_strdelimit(g_strdup(p), "-", ':');
584                         content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
585                         g_free(p_value);
586                 } else {
587                         content_info->media_meta.recorded_date = g_strdup(p);
588                 }
589         }
590
591         if (content_info->media_meta.recorded_date == NULL)
592                 content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
593         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
594
595         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
596         if (mmf_error == FILEINFO_ERROR_NONE && size == 4)
597                 content_info->media_meta.year = g_strdup(p);
598         else
599                 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
600
601         /*Do not extract artwork for the USB Storage content*/
602         if (support_albumart)
603                 content_info->thumbnail_path = __media_svc_extract_albumart(tag, content_info->path, uid);
604
605         ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
606         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
607                 media_svc_debug("album does not exist. So start to make album art");
608                 if (strlen(content_info->media_meta.album) > 0 && strlen(content_info->media_meta.artist) > 0)
609                         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);
610                 else
611                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
612         }
613         content_info->album_id = album_id;
614
615         if (mm_file_destroy_tag_attrs(tag) != FILEINFO_ERROR_NONE)
616                 media_svc_error("destroy failed");
617 }
618
619 void _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
620 {
621         int mmf_error = FILEINFO_ERROR_NONE;
622         MMHandleType tag = 0;
623         char *p = NULL;
624         unsigned int size = 0;
625
626         mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
627         if (mmf_error == FILEINFO_ERROR_NONE) {
628                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
629                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
630                         if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
631                                 /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
632                                 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
633                                 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
634                                 g_free(p_value);
635                         } else {
636                                 content_info->media_meta.recorded_date = g_strdup(p);
637                         }
638                 }
639                 if (content_info->media_meta.recorded_date == NULL)
640                         content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
641                 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
642
643                 mmf_error = mm_file_destroy_tag_attrs(tag);
644                 if (mmf_error != FILEINFO_ERROR_NONE)
645                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
646         }
647         /* All metadata fields must be empty strings until media_video is deleted */
648         content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
649         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
650         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
651         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
652         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
653         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
654         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
655         content_info->album_id = 0;
656 }
657
658 static gchar * __media_svc_get_zipfile_string(zip_t *z, const char *fname)
659 {
660         int err = 0;
661         zip_int64_t index_num = 0;
662         zip_file_t *file = NULL;
663         zip_stat_t sb = {0, };
664         gchar *buf = NULL;
665
666         media_svc_retvm_if(!z, NULL, "z is NULL");
667         media_svc_retvm_if(!fname, NULL, "fname is NULL");
668
669         index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
670         media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
671
672         err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
673         media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
674
675         file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
676         media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
677
678         buf = g_malloc0(sb.size + 1);
679
680         err = zip_fread(file, buf, sb.size);
681         zip_fclose(file);
682
683         if (err == -1) {
684                 g_free(buf);
685                 buf = NULL;
686         }
687
688         return buf;
689 }
690
691 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
692 {
693         xmlNodePtr tmp = NULL;
694
695         media_svc_retvm_if(!node, NULL, "node is NULL");
696         media_svc_retvm_if(!key, NULL, "key is NULL");
697
698         for (tmp = node->children; tmp; tmp = tmp->next) {
699                 if (xmlIsBlankNode(tmp))
700                         continue;
701
702                 if (g_str_has_suffix((gchar *)tmp->name, key))
703                         return tmp;
704         }
705
706         return NULL;
707 }
708
709 static char * __media_svc_remove_escape_c(const char *value)
710 {
711         int start = -1;
712         int end = 0;
713         int len, i;
714
715         media_svc_retv_if(!value, NULL);
716
717         len = strlen(value);
718
719         for (i = 0; i < len; i++) {
720                 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
721                         if (start == -1)
722                                 start = i;
723
724                         end = i;
725                 }
726         }
727
728         end = end - start + 1;
729
730         return g_strndup(value + start, end);
731 }
732
733 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
734 {
735         xmlNodePtr tmp = NULL;
736         char *tmp_res = NULL;
737         char *res = NULL;
738
739         media_svc_retvm_if(!node, NULL, "node is NULL");
740         media_svc_retvm_if(!key, NULL, "key is NULL");
741
742         for (tmp = node->children; tmp; tmp = tmp->next) {
743                 if (xmlIsBlankNode(tmp))
744                         continue;
745
746                 if (tmp->children) {
747                         tmp_res = __media_svc_find_and_get_value(tmp, key);
748                         if (tmp_res) {
749                                 res = __media_svc_remove_escape_c(tmp_res);
750                                 xmlFree(tmp_res);
751                                 return res;
752                         }
753                 }
754
755                 if (g_str_has_suffix((gchar *)tmp->name, key))
756                         return (char *)xmlNodeGetContent(tmp);
757         }
758
759         return NULL;
760 }
761
762 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
763 {
764         gchar *buf = NULL;
765         gchar *tmp_buf = NULL;
766         xmlDocPtr doc = NULL;
767         xmlNodePtr node = NULL;
768
769         media_svc_retvm_if(!z, FALSE, "z is NULL");
770         media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
771
772         buf = __media_svc_get_zipfile_string(z, "META-INF/container.xml");
773         media_svc_retvm_if(!buf, FALSE, "buf is NULL");
774
775         tmp_buf = g_strrstr(buf, ">");
776         if (tmp_buf)
777                 *(tmp_buf + 1) = '\0';
778
779         doc = xmlParseDoc((const xmlChar *)buf);
780         g_free(buf);
781         media_svc_retvm_if(!doc, FALSE, "doc is NULL");
782
783         node = xmlDocGetRootElement(doc);
784         node = __media_svc_find_node(node, "rootfiles");
785         node = __media_svc_find_node(node, "rootfile");
786
787         *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
788         media_svc_sec_debug("OPF [%s]", *opf_file);
789         xmlFreeDoc(doc);
790
791         return TRUE;
792 }
793
794 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
795 {
796         xmlDocPtr doc = NULL;
797         xmlNodePtr root = NULL;
798
799         media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
800         media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
801
802         doc = xmlParseDoc(buffer);
803         media_svc_retv_if(!doc, FALSE);
804
805         root = xmlDocGetRootElement(doc);
806         if (!root) {
807                 xmlFreeDoc(doc);
808                 return FALSE;
809         }
810
811         content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
812         /* In the case of PDF, if there is no title, it may not be a metadata block. Search for the next xml block*/
813         if (is_pdf && !content_info->media_meta.title) {
814                 xmlFreeDoc(doc);
815                 return FALSE;
816         }
817
818         content_info->media_meta.artist = __media_svc_find_and_get_value(root, "creator");
819         if (!content_info->media_meta.artist)
820                 content_info->media_meta.artist = __media_svc_find_and_get_value(root, "author");
821         content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
822
823         xmlFreeDoc(doc);
824
825         return TRUE;
826 }
827
828 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
829 {
830         int err = 0;
831         zip_t *z = NULL;
832         gchar *buf = NULL;
833         char *opf_path = NULL;
834
835         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
836
837         //1. open epub
838         z = zip_open(content_info->path, ZIP_RDONLY, &err);
839         media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
840
841         //2. find and read opf file
842         if (!__media_svc_get_epub_root_file(z, &opf_path)) {
843                 media_svc_error("__media_svc_get_epub_root_file failed");
844                 zip_close(z);
845                 return MS_MEDIA_ERR_INTERNAL;
846         }
847
848         //3. get metadata
849         buf = __media_svc_get_zipfile_string(z, opf_path);
850         xmlFree(opf_path);
851         zip_close(z);
852         media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_string failed");
853
854         if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
855                 media_svc_error("__media_svc_get_xml_metadata failed");
856
857         g_free(buf);
858
859         return MS_MEDIA_ERR_NONE;
860 }
861
862 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
863 {
864     int fd = 0;
865     int start_pos = 0;
866     int end_pos = 0;
867     int cur_pos = 0;
868     int search_limit = 0;
869     char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
870     gchar *meta_buf = NULL;
871     char *found = NULL;
872
873         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
874         media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
875
876         fd = open(content_info->path, O_RDONLY);
877         media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
878
879         search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
880
881         while (cur_pos <= search_limit) {
882                 if (lseek(fd, cur_pos, SEEK_SET) == -1)
883                         break;
884
885                 memset(&tmp, 0x00, MEDIA_SVC_PDF_BUF_SIZE + 1);
886
887                 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
888                         media_svc_error("read failed");
889                         break;
890                 }
891
892                 //1.Find <x:xmpmeta .. </x:xmpmeta> block
893                 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
894                         start_pos = cur_pos + (found - tmp);
895 //                      media_svc_error("FIND START_POS[%d]", start_pos);
896                         found = NULL;
897                 }
898
899
900                 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
901                         end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
902 //                      media_svc_error("FIND END_POS[%d]", end_pos);
903                         found = NULL;
904                 }
905
906                 //2.get metadata using xml parser
907                 if (start_pos && end_pos) {
908                         if (lseek(fd, start_pos, SEEK_SET) == -1)
909                                 break;
910
911                         meta_buf = g_malloc0(end_pos - start_pos + 1);
912
913                         if (read(fd, meta_buf, end_pos - start_pos) == end_pos - start_pos) {
914                                 if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
915                                         g_free(meta_buf);
916                                         break;
917                                 }
918                         }
919
920                         g_free(meta_buf);
921
922                         start_pos = 0;
923                         end_pos = 0;
924                 }
925
926                 cur_pos += 240;
927
928         }
929
930         close(fd);
931
932         return MS_MEDIA_ERR_NONE;
933 }
934
935 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
936 {
937         int ret = MS_MEDIA_ERR_NONE;
938
939         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
940
941         if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
942                 ret = __media_svc_get_epub_metadata(content_info);
943         else
944                 ret = __media_svc_get_pdf_metadata(content_info);
945
946         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to extract metadata");
947         if (!content_info->media_meta.title || strlen(content_info->media_meta.title) == 0) {
948                 g_free(content_info->media_meta.title);
949                 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
950         }
951
952         return MS_MEDIA_ERR_NONE;
953 }
954
955 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
956 {
957         media_svc_retm_if(!content_info, "content info is NULL");
958
959         /* Delete media_svc_content_info_s */
960         g_free(content_info->media_uuid);
961         g_free(content_info->path);
962         g_free(content_info->file_name);
963         g_free(content_info->mime_type);
964         g_free(content_info->thumbnail_path);
965         g_free(content_info->storage_uuid);
966
967         /* Delete media_svc_content_meta_s */
968         g_free(content_info->media_meta.title);
969         g_free(content_info->media_meta.album);
970         g_free(content_info->media_meta.artist);
971         g_free(content_info->media_meta.album_artist);
972         g_free(content_info->media_meta.genre);
973         g_free(content_info->media_meta.year);
974         g_free(content_info->media_meta.recorded_date);
975         g_free(content_info->media_meta.track_num);
976         g_free(content_info->media_meta.datetaken);
977 }
978
979 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
980 {
981         int ret = MS_MEDIA_ERR_NONE;
982
983         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
984         media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
985         media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
986
987         if (!ms_user_thumb_support(uid, path)) {
988                 media_svc_sec_error("origin path(%s) is invalid", path);
989                 return MS_MEDIA_ERR_INVALID_PARAMETER;
990         }
991
992         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
993
994         //1. make thumb path
995         ret = __media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
996         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to create thumbnail path[%d]", ret);
997
998         //2. save thumbnail
999         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1000                 ret = create_image_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
1001         else
1002                 ret = create_video_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
1003
1004         return (ret == THUMB_OK) ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_INTERNAL;
1005 }
1006
1007 int _media_svc_get_media_type(const char *path, int *mediatype)
1008 {
1009         int ret = MS_MEDIA_ERR_NONE;
1010         char mime_type[256] = {0};
1011         media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1012
1013         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1014
1015         ret = __media_svc_get_mime_type(path, mime_type);
1016         if (ret == MS_MEDIA_ERR_NONE)
1017                 __media_svc_get_media_type(path, mime_type, &media_type);
1018         else
1019                 media_svc_error("__media_svc_get_mime_type failed");
1020
1021         *mediatype = media_type;
1022
1023         return ret;
1024 }
1025
1026 bool _media_svc_is_keyword_included(const char *path, const char *keyword)
1027 {
1028         bool ret = false;
1029         void *handle = NULL;
1030         bool (*svc_search) (const char *, const char *);
1031
1032         media_svc_retvm_if(!path, false, "Invalid path");
1033         media_svc_retvm_if(!keyword, false, "Invalid keyword");
1034
1035         handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
1036         media_svc_retvm_if(!handle, false, "dlopen failed");
1037
1038         if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1039                 svc_search = dlsym(handle, "media_svc_epub_is_keyword_included");
1040         else
1041                 svc_search = dlsym(handle, "media_svc_pdf_is_keyword_included");
1042
1043         if (!svc_search) {
1044                 media_svc_error("dlsym failed - %s", dlerror());
1045                 dlclose(handle);
1046                 return false;
1047         }
1048
1049         ret = svc_search(path, keyword);
1050         dlclose(handle);
1051
1052         return ret;
1053 }
1054
1055 static int __media_svc_create_wordbook_db(const char *path, sqlite3 **handle)
1056 {
1057         int ret = SQLITE_OK;
1058         sqlite3 *db_handle = NULL;
1059         char *err = NULL;
1060
1061         ret = sqlite3_open_v2(path, &db_handle, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
1062         media_svc_retvm_if(ret != SQLITE_OK, ret, "sqlite3_open_v2 failed : %d", ret);
1063
1064         ret = sqlite3_exec(db_handle, "PRAGMA journal_mode = OFF;", NULL, NULL, &err);
1065         if (ret != SQLITE_OK)
1066                 goto ERROR;
1067
1068         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);
1069         if (ret != SQLITE_OK)
1070                 goto ERROR;
1071
1072         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);
1073         if (ret != SQLITE_OK)
1074                 goto ERROR;
1075
1076         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);
1077         if (ret != SQLITE_OK)
1078                 goto ERROR;
1079
1080         *handle = db_handle;
1081
1082         return SQLITE_OK;
1083
1084 ERROR:
1085         media_svc_error("sqlite3_exec failed : %s", err);
1086         SQLITE3_SAFE_FREE(err);
1087         sqlite3_close_v2(db_handle);
1088
1089         return ret;
1090 }
1091
1092 static bool __media_svc_get_wordbook_handle(uid_t uid, sqlite3 **handle)
1093 {
1094         int ret = SQLITE_OK;
1095         char *db_path = NULL;
1096
1097         ms_user_get_wordbook_db_path(uid, &db_path);
1098         if (!db_path)
1099                 return false;
1100
1101         ret = sqlite3_open_v2(db_path, handle, SQLITE_OPEN_READWRITE, NULL);
1102         if (ret != SQLITE_OK) {
1103                 ret = __media_svc_create_wordbook_db(db_path, handle);
1104                 free(db_path);
1105                 media_svc_retvm_if(ret != SQLITE_OK, false, "__media_svc_create_wordbook_db failed : %d", ret);
1106         } else {
1107                 ret = sqlite3_exec(*handle, "PRAGMA journal_mode = OFF;", NULL, NULL, NULL);
1108                 if (ret != SQLITE_OK)
1109                         media_svc_error("Failed to change journal mode [%d]", ret);
1110         }
1111
1112         return true;
1113 }
1114
1115 static bool __media_svc_is_exist_in_wordbook(sqlite3 *db_handle, const char *path)
1116 {
1117         int ret = SQLITE_OK;
1118         char *err = NULL;
1119         char *query = NULL;
1120
1121         query = sqlite3_mprintf("UPDATE files SET validity=1 WHERE path = %Q", path);
1122
1123         ret = sqlite3_exec(db_handle, query, NULL, NULL, &err);
1124         SQLITE3_SAFE_FREE(query);
1125         if (ret != SQLITE_OK) {
1126                 media_svc_error("Query failed. [%s]", err);
1127                 SQLITE3_SAFE_FREE(err);
1128                 return false;
1129         }
1130
1131         return sqlite3_changes(db_handle) > 0 ? true : false;
1132 }
1133
1134 static void __media_svc_insert_to_wordbook(sqlite3 *db_handle, const char *path)
1135 {
1136         void *handle = NULL;
1137         void (*svc_update) (sqlite3 *, const char *);
1138         char *query = NULL;
1139
1140         query = sqlite3_mprintf("INSERT INTO files(path) VALUES(%Q);", path);
1141         sqlite3_exec(db_handle, query, NULL, NULL, NULL);
1142         sqlite3_free(query);
1143
1144         handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
1145         if (!handle) {
1146                 media_svc_error("dlopen failed");
1147                 return;
1148         }
1149
1150         if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1151                 svc_update = dlsym(handle, "media_svc_epub_insert_to_db");
1152         else
1153                 svc_update = dlsym(handle, "media_svc_pdf_insert_to_db");
1154
1155         if (!svc_update) {
1156                 media_svc_error("dlsym failed - %s", dlerror());
1157                 dlclose(handle);
1158                 return;
1159         }
1160
1161         svc_update(db_handle, path);
1162         dlclose(handle);
1163 }
1164
1165 void _media_svc_update_wordbook(const char *path, uid_t uid)
1166 {
1167         sqlite3 *db_handle = NULL;
1168
1169         if (!path) {
1170                 media_svc_error("Invalid path");
1171                 return;
1172         }
1173
1174         // check db..
1175         if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1176                 return;
1177
1178         if (__media_svc_is_exist_in_wordbook(db_handle, path)) {
1179                 sqlite3_close_v2(db_handle);
1180                 return;
1181         }
1182
1183         // if no item, insert to db..
1184         __media_svc_insert_to_wordbook(db_handle, path);
1185         sqlite3_close_v2(db_handle);
1186 }
1187
1188 void _media_svc_clean_wordbook(uid_t uid)
1189 {
1190         sqlite3 *db_handle = NULL;
1191
1192         if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1193                 return;
1194
1195         sqlite3_exec(db_handle, "DELETE FROM files where validity = 0;", NULL, NULL, NULL);
1196         sqlite3_exec(db_handle, "UPDATE files SET validity = 0;", NULL, NULL, NULL);
1197         sqlite3_close_v2(db_handle);
1198 }
1199
1200 bool _media_svc_get_matched_list(const char *keyword, uid_t uid, GList **list)
1201 {
1202         int ret = SQLITE_OK;
1203         sqlite3 *handle = NULL;
1204         sqlite3_stmt *stmt = NULL;
1205         char *query = NULL;
1206
1207         media_svc_retvm_if(!list, false, "list is NULL");
1208         media_svc_retvm_if(!keyword, false, "keyword is NULL");
1209         media_svc_retvm_if(!__media_svc_get_wordbook_handle(uid, &handle), false, "Failed to get handle");
1210
1211         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);
1212         ret = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1213         SQLITE3_SAFE_FREE(query);
1214
1215         if (ret != SQLITE_OK) {
1216                 media_svc_error("Query failed[%d]", ret);
1217                 sqlite3_close_v2(handle);
1218                 return false;
1219         }
1220
1221         while (sqlite3_step(stmt) == SQLITE_ROW)
1222                 *list = g_list_append(*list, g_strdup((char *)sqlite3_column_text(stmt, 0)));
1223
1224         sqlite3_finalize(stmt);
1225         sqlite3_close_v2(handle);
1226
1227         return true;
1228 }