4 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "media-ebook-plugin-epub.h"
30 #define LOG_TAG "MEDIA_SERVICE"
32 using namespace EBook;
34 Epub::Epub(std::string path, std::unique_ptr<IRunnable> runner)
35 : runner(std::move(runner))
42 LOGD("%s", path.c_str());
45 z = zip_open(path.c_str(), ZIP_RDONLY, &err);
47 LOGE("zip_open failed");
63 int entry_len = zip_get_num_entries(z, ZIP_FL_UNCHANGED);
64 for (int i = 0; i < entry_len; i++) {
65 if (!g_str_has_suffix(zip_get_name(z, i, ZIP_FL_ENC_GUESS), "html"))
68 if (zip_stat_index(z, i, 0, &sb) != 0)
71 zip_file_t *file = zip_fopen_index(z, i, 0);
75 std::vector<char> file_buf(sb.size);
77 zip_int64_t readn = zip_fread(file, file_buf.data(), sb.size);
80 if ((readn == static_cast<zip_int64_t>(sb.size)) &&
81 htmlFind(file_buf.data(), sb.size))
88 bool Epub::htmlFind(const char* html_buf, int buf_size)
90 htmlDocPtr doc = htmlReadMemory(html_buf, buf_size, "/", NULL,
91 HTML_PARSE_NOBLANKS | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET);
94 LOGE("htmlReadMemory failed");
98 bool found = htmlNodeFindRecursive(xmlDocGetRootElement(doc));
105 bool Epub::htmlNodeFindRecursive(xmlNodePtr node)
107 for (xmlNodePtr cur = node; cur; cur = cur->next) {
108 if (cur->type == XML_TEXT_NODE &&
109 runner->run(reinterpret_cast<char*>(cur->content)))
112 if (htmlNodeFindRecursive(cur->children))
123 int entry_len = zip_get_num_entries(z, ZIP_FL_UNCHANGED);
124 for (int i = 0; i < entry_len; i++) {
125 if (!g_str_has_suffix(zip_get_name(z, i, ZIP_FL_ENC_GUESS), "html"))
128 if (zip_stat_index(z, i, 0, &sb) != 0)
131 zip_file_t *file = zip_fopen_index(z, i, 0);
135 std::vector<char> file_buf(sb.size);
137 zip_int64_t readn = zip_fread(file, file_buf.data(), sb.size);
140 if (readn == static_cast<zip_int64_t>(sb.size))
141 htmlInsert(file_buf.data(), sb.size);
145 void Epub::htmlInsert(const char* html_buf, int buf_size)
147 htmlDocPtr doc = htmlReadMemory(html_buf, buf_size, "/", NULL,
148 HTML_PARSE_NOBLANKS | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET);
151 LOGE("htmlReadMemory failed");
155 htmlNodeInsertRecursive(xmlDocGetRootElement(doc));
160 void Epub::htmlNodeInsertRecursive(xmlNodePtr node)
162 for (xmlNodePtr cur = node; cur; cur = cur->next) {
163 if (cur->type == XML_TEXT_NODE)
164 runner->run(reinterpret_cast<char*>(cur->content));
166 htmlNodeInsertRecursive(cur->children);