| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* Copyright (C) <2010> <Alf> |
| 4 |
* |
| 5 |
* Contact: Alf <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
/* @CPPFILE ApvlvHtm.cc |
| 23 |
* |
| 24 |
* Author: Alf <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <QDomDocument> |
| 28 |
#include <QXmlStreamReader> |
| 29 |
#include <filesystem> |
| 30 |
#pragma GCC diagnostic push |
| 31 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 32 |
#include <quazipfile.h> |
| 33 |
#pragma GCC diagnostic pop |
| 34 |
|
| 35 |
#include "ApvlvEpub.h" |
| 36 |
#include "ApvlvUtil.h" |
| 37 |
#include "ApvlvWebViewWidget.h" |
| 38 |
|
| 39 |
namespace apvlv |
| 40 |
{ |
| 41 |
FILE_TYPE_DEFINITION ("Web", ApvlvEPUB, { ".epub" }); |
| 42 |
|
| 43 |
using namespace std; |
| 44 |
|
| 45 |
bool |
| 46 |
ApvlvEPUB::load (const string &filename) |
| 47 |
{ |
| 48 |
mQuaZip = make_unique<QuaZip> (QString::fromLocal8Bit (filename)); |
| 49 |
if (mQuaZip->open (QuaZip::mdUnzip) == false) |
| 50 |
{ |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
auto filenames = mQuaZip->getFileNameList (); |
| 55 |
if (!filenames.contains ("META-INF/container.xml")) |
| 56 |
{ |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
auto optcontainer = getZipFileContents ("META-INF/container.xml"); |
| 61 |
if (!optcontainer) |
| 62 |
{ |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
string contentfile = containerGetContentfile (optcontainer->constData (), |
| 67 |
optcontainer->length ()); |
| 68 |
if (contentfile.empty ()) |
| 69 |
{ |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
if (contentGetMedia (contentfile) == false) |
| 74 |
{ |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
ncxSetIndex (idSrcs["ncx"]); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
ApvlvEPUB::~ApvlvEPUB () { mQuaZip->close (); } |
| 83 |
|
| 84 |
int |
| 85 |
ApvlvEPUB::sum () |
| 86 |
{ |
| 87 |
return int (mPages.size ()); |
| 88 |
} |
| 89 |
|
| 90 |
bool |
| 91 |
ApvlvEPUB::pageRenderToWebView (int pn, double zm, int rot, WebView *webview) |
| 92 |
{ |
| 93 |
webview->setZoomFactor (zm); |
| 94 |
QUrl epuburi = QString ("apvlv:///") + QString::fromLocal8Bit (mPages[pn]); |
| 95 |
webview->load (epuburi); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
unique_ptr<WordListRectangle> |
| 100 |
ApvlvEPUB::pageSearch (int pn, const char *s) |
| 101 |
{ |
| 102 |
auto qpath = QString::fromLocal8Bit (mPages[pn]); |
| 103 |
auto content = getZipFileContents (qpath); |
| 104 |
auto html = content->toStdString (); |
| 105 |
auto pos = html.find (s); |
| 106 |
if (pos == string::npos) |
| 107 |
return nullptr; |
| 108 |
|
| 109 |
auto wordlist = make_unique<WordListRectangle> (); |
| 110 |
do |
| 111 |
{ |
| 112 |
WordRectangle word{}; |
| 113 |
word.word = s; |
| 114 |
wordlist->push_back (word); |
| 115 |
pos = html.find (s, pos + 1); |
| 116 |
} |
| 117 |
while (pos != string::npos); |
| 118 |
|
| 119 |
return wordlist; |
| 120 |
} |
| 121 |
|
| 122 |
optional<QByteArray> |
| 123 |
ApvlvEPUB::pathContent (const string &path) |
| 124 |
{ |
| 125 |
auto optcontent = getZipFileContents (QString::fromLocal8Bit (path)); |
| 126 |
return optcontent; |
| 127 |
} |
| 128 |
|
| 129 |
optional<QByteArray> |
| 130 |
ApvlvEPUB::getZipFileContents (const QString &name) |
| 131 |
{ |
| 132 |
if (mQuaZip->setCurrentFile (name) == false) |
| 133 |
return nullopt; |
| 134 |
|
| 135 |
auto zipfile = make_unique<QuaZipFile> (mQuaZip.get ()); |
| 136 |
zipfile->open (QIODevice::ReadOnly); |
| 137 |
auto qarray = zipfile->readAll (); |
| 138 |
zipfile->close (); |
| 139 |
return qarray; |
| 140 |
} |
| 141 |
|
| 142 |
string |
| 143 |
ApvlvEPUB::containerGetContentfile (const char *container, int len) |
| 144 |
{ |
| 145 |
vector<string> names{ "container", "rootfiles", "rootfile" }; |
| 146 |
return xmlContentGetAttributeValue (container, len, names, "full-path"); |
| 147 |
} |
| 148 |
|
| 149 |
bool |
| 150 |
ApvlvEPUB::contentGetMedia (const string &contentfile) |
| 151 |
{ |
| 152 |
string cover_id = "cover"; |
| 153 |
|
| 154 |
auto optcontent = getZipFileContents (QString::fromLocal8Bit (contentfile)); |
| 155 |
if (!optcontent) |
| 156 |
{ |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
vector<string> metas{ "package", "metadata" }; |
| 161 |
auto optcover = xmlContentGetElement (optcontent->constData (), |
| 162 |
optcontent->length (), metas); |
| 163 |
if (optcover) |
| 164 |
{ |
| 165 |
auto xml = optcover->get (); |
| 166 |
while ( |
| 167 |
!xml->atEnd () |
| 168 |
&& !(xml->isEndElement () && xml->name ().toString () == "metadata")) |
| 169 |
{ |
| 170 |
xml->readNext (); |
| 171 |
if (xml->isStartElement () && xml->name ().toString () == "meta") |
| 172 |
{ |
| 173 |
auto attrs = xml->attributes (); |
| 174 |
for (auto const &attr : attrs) |
| 175 |
{ |
| 176 |
if (attr.name ().toString () == "name" |
| 177 |
&& attr.value ().toString () == "cover") |
| 178 |
{ |
| 179 |
cover_id = xml->attributes () |
| 180 |
.value ("content") |
| 181 |
.toString () |
| 182 |
.toStdString (); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
vector<string> items = { "package", "manifest", "item" }; |
| 190 |
auto optxml = xmlContentGetElement (optcontent->constData (), |
| 191 |
optcontent->length (), items); |
| 192 |
if (!optxml) |
| 193 |
return false; |
| 194 |
|
| 195 |
auto xml = optxml->get (); |
| 196 |
while (!xml->atEnd () && xml->name ().toString () == "item") |
| 197 |
{ |
| 198 |
if (xml->isStartElement ()) |
| 199 |
{ |
| 200 |
string href = xmlStreamGetAttributeValue (xml, "href"); |
| 201 |
if (href.empty ()) |
| 202 |
{ |
| 203 |
xml->readNextStartElement (); |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
if (contentfile.rfind ('/') != string::npos) |
| 208 |
{ |
| 209 |
string dirname = contentfile.substr (0, contentfile.rfind ('/')); |
| 210 |
href = dirname + "/" + href; |
| 211 |
} |
| 212 |
|
| 213 |
string id = xmlStreamGetAttributeValue (xml, "id"); |
| 214 |
string type = xmlStreamGetAttributeValue (xml, "media-type"); |
| 215 |
if (id == cover_id) |
| 216 |
{ |
| 217 |
idSrcs["cover"] = href; |
| 218 |
srcMimeTypes[href] = type; |
| 219 |
} |
| 220 |
else |
| 221 |
{ |
| 222 |
idSrcs[id] = href; |
| 223 |
srcMimeTypes[href] = type; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
xml->readNextStartElement (); |
| 228 |
} |
| 229 |
|
| 230 |
vector<string> names{ "package", "spine", "itemref" }; |
| 231 |
optxml = xmlContentGetElement (optcontent->constData (), |
| 232 |
optcontent->length (), names); |
| 233 |
if (!optxml) |
| 234 |
return false; |
| 235 |
|
| 236 |
xml = optxml->get (); |
| 237 |
while (!xml->atEnd () && xml->name ().toString () == "itemref") |
| 238 |
{ |
| 239 |
if (xml->isStartElement ()) |
| 240 |
{ |
| 241 |
string id = xmlStreamGetAttributeValue (xml, "idref"); |
| 242 |
mPages.push_back (idSrcs[id]); |
| 243 |
srcPages[idSrcs[id]] = static_cast<int> (mPages.size () - 1); |
| 244 |
} |
| 245 |
xml->readNextStartElement (); |
| 246 |
} |
| 247 |
|
| 248 |
return true; |
| 249 |
} |
| 250 |
|
| 251 |
bool |
| 252 |
ApvlvEPUB::ncxSetIndex (const string &ncxfile) |
| 253 |
{ |
| 254 |
auto opttoc = getZipFileContents (QString::fromLocal8Bit (ncxfile)); |
| 255 |
if (!opttoc) |
| 256 |
{ |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
vector<string> names{ "ncx", "navMap" }; |
| 261 |
auto optxml |
| 262 |
= xmlContentGetElement (opttoc->constData (), opttoc->length (), names); |
| 263 |
|
| 264 |
if (!optxml) |
| 265 |
{ |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
mIndex = { "__cover__", 0, getFilename (), FileIndexType::FILE }; |
| 270 |
|
| 271 |
auto xml = optxml->get (); |
| 272 |
ncxNodeSetIndex (xml, "navMap", ncxfile, mIndex); |
| 273 |
|
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
void |
| 278 |
ApvlvEPUB::ncxNodeSetIndex (QXmlStreamReader *xml, const string &element_name, |
| 279 |
const string &ncxfile, FileIndex &index) |
| 280 |
{ |
| 281 |
while (!xml->atEnd () |
| 282 |
&& !(xml->isEndElement () |
| 283 |
&& xml->name ().toString ().toStdString () == element_name)) |
| 284 |
{ |
| 285 |
if (xml->isStartElement ()) |
| 286 |
{ |
| 287 |
if (xml->name ().toString () == "navLabel") |
| 288 |
{ |
| 289 |
while (!(xml->isEndElement () |
| 290 |
&& xml->name ().toString () == "navLabel")) |
| 291 |
{ |
| 292 |
xml->readNextStartElement (); |
| 293 |
if (xml->name ().toString () == "text") |
| 294 |
{ |
| 295 |
auto text = xml->readElementText ( |
| 296 |
QXmlStreamReader::ReadElementTextBehaviour:: |
| 297 |
SkipChildElements); |
| 298 |
index.title = text.toStdString (); |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
xml->readNextStartElement (); |
| 304 |
} |
| 305 |
|
| 306 |
if (xml->name ().toString () == "content") |
| 307 |
{ |
| 308 |
string srcstr = xmlStreamGetAttributeValue (xml, "src"); |
| 309 |
if (srcstr.empty ()) |
| 310 |
continue; |
| 311 |
|
| 312 |
if (ncxfile.find ('/') != string::npos) |
| 313 |
{ |
| 314 |
auto ncxdir |
| 315 |
= filesystem::path (ncxfile).parent_path ().string (); |
| 316 |
srcstr = string (ncxdir) + '/' + srcstr; |
| 317 |
} |
| 318 |
|
| 319 |
index.path = srcstr; |
| 320 |
|
| 321 |
auto href = srcstr; |
| 322 |
if (srcstr.find ('#') != string::npos) |
| 323 |
{ |
| 324 |
index.anchor = srcstr.substr (srcstr.find ('#')); |
| 325 |
href = srcstr.substr (0, srcstr.find ('#')); |
| 326 |
} |
| 327 |
|
| 328 |
for (decltype (mPages.size ()) ind = 0; ind < mPages.size (); |
| 329 |
++ind) |
| 330 |
{ |
| 331 |
if (mPages[ind] == href) |
| 332 |
{ |
| 333 |
index.page = int (ind); |
| 334 |
break; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
xml->readNextStartElement (); |
| 339 |
} |
| 340 |
|
| 341 |
if (xml->name ().toString () == "navPoint") |
| 342 |
{ |
| 343 |
xml->readNextStartElement (); |
| 344 |
auto childindex = FileIndex{}; |
| 345 |
ncxNodeSetIndex (xml, "navPoint", ncxfile, childindex); |
| 346 |
index.mChildrenIndex.emplace_back (childindex); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
xml->readNextStartElement (); |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// Local Variables: |
| 356 |
// mode: c++ |
| 357 |
// End: |