Add to search ebooks with keywords
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
index bf20193..a58a7c2 100644 (file)
@@ -51,7 +51,9 @@
 #include "media-svc-hash.h"
 #include "media-svc-album.h"
 #include "media-svc-localize_ch.h"
-/*For ebook*/
+#include "media-svc-util-pdf.h"
+#include "media-svc-util-epub.h"
+/*For ebook metadata */
 #include <zip.h>
 #include <libxml/xmlmemory.h>
 #include <libxml/parser.h>
@@ -1243,27 +1245,19 @@ static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
 {
        gchar *buf = NULL;
        gchar *tmp_buf = NULL;
-       int len = 0;
        xmlDocPtr doc = NULL;
        xmlNodePtr node = NULL;
 
        media_svc_retvm_if(!z, FALSE, "z is NULL");
        media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
 
-       tmp_buf = __media_svc_get_zipfile_data(z, "META-INF/container.xml");
-       media_svc_retvm_if(!tmp_buf, FALSE, "tmp_buf is NULL");
+       buf = __media_svc_get_zipfile_data(z, "META-INF/container.xml");
+       media_svc_retvm_if(!buf, FALSE, "buf is NULL");
 
-       len = strlen(tmp_buf);
 
-       while (0 < len) {
-               len--;
-
-               if (tmp_buf[len] == '>')
-                       break;
-       }
-
-       buf = g_strndup(tmp_buf, len + 1);
-       g_free(tmp_buf);
+       tmp_buf = g_strrstr(buf, ">");
+       if (tmp_buf)
+               *(tmp_buf + 1) = '\0';
 
        doc = xmlParseDoc((const xmlChar *)buf);
        g_free(buf);
@@ -1402,19 +1396,15 @@ static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
 
                        meta_buf = g_malloc0(end_pos - start_pos + 1);
 
-                       if (read(fd, meta_buf, end_pos - start_pos) != end_pos - start_pos) {
-                               g_free(meta_buf);
-                               goto NEXT;
-                       }
-
-                       if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
-                               g_free(meta_buf);
-                               break;
+                       if (read(fd, meta_buf, end_pos - start_pos) == end_pos - start_pos) {
+                               if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
+                                       g_free(meta_buf);
+                                       break;
+                               }
                        }
 
                        g_free(meta_buf);
-                       meta_buf = NULL;
-NEXT:
+
                        start_pos = 0;
                        end_pos = 0;
                }
@@ -1586,3 +1576,14 @@ bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
                return false;
        }
 }
+
+bool _media_svc_is_keyword_included(const char *path, const char *keyword)
+{
+       media_svc_retvm_if(!path, false, "Invalid path");
+       media_svc_retvm_if(!keyword, false, "Invalid keyword");
+
+       if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
+               return _media_svc_epub_is_keyword_included(path, keyword);
+       else
+               return _media_svc_pdf_is_keyword_included(path, keyword);
+}