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