Initial FB2 support
This commit is contained in:
9 files changed
+1059
-4
No files matched your search
@@ -0,0 +1,204 @@
|
||||
#include "Fb2ImageProvider.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QImage>
|
||||
#include <QMutexLocker>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fb2ImageResponse
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Fb2ImageResponse::Fb2ImageResponse(const QString &fb2Path, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
: m_fb2Path(fb2Path)
|
||||
, m_id(id)
|
||||
, m_displayWidth(displayWidth)
|
||||
, m_requestedSize(requestedSize)
|
||||
{
|
||||
setAutoDelete(false);
|
||||
}
|
||||
|
||||
QQuickTextureFactory *Fb2ImageResponse::textureFactory() const
|
||||
{
|
||||
if (m_image.isNull())
|
||||
return nullptr;
|
||||
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
||||
}
|
||||
|
||||
void Fb2ImageResponse::cancel()
|
||||
{
|
||||
m_cancelled.storeRelaxed(1);
|
||||
}
|
||||
|
||||
void Fb2ImageResponse::run()
|
||||
{
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
m_image = Fb2ImageProvider::loadImageFromFb2(m_fb2Path, m_id, m_displayWidth, m_requestedSize);
|
||||
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
emit finished();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fb2ImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage Fb2ImageProvider::loadImageFromFb2(const QString &fb2Path, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
{
|
||||
if (fb2Path.isEmpty() || id.isEmpty())
|
||||
return {};
|
||||
|
||||
QFile f(fb2Path);
|
||||
if (!f.open(QIODevice::ReadOnly))
|
||||
return {};
|
||||
|
||||
QXmlStreamReader xml(&f);
|
||||
while (!xml.atEnd()) {
|
||||
xml.readNext();
|
||||
if (!xml.isStartElement() || xml.name() != QLatin1String("binary"))
|
||||
continue;
|
||||
if (xml.attributes().value(QLatin1String("id")) != id)
|
||||
continue;
|
||||
|
||||
// Accumulate text (base64 data may span multiple character events)
|
||||
QString base64;
|
||||
while (!xml.atEnd()) {
|
||||
xml.readNext();
|
||||
if (xml.isCharacters())
|
||||
base64 += xml.text();
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
const QByteArray data = QByteArray::fromBase64(base64.toLatin1());
|
||||
QImage image;
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
if (requestedSize.width() > 0 || requestedSize.height() > 0) {
|
||||
int targetW = requestedSize.width() > 0 ? requestedSize.width() : -1;
|
||||
int targetH = requestedSize.height() > 0 ? requestedSize.height() : -1;
|
||||
if (displayWidth > 0 && targetW > 0 && targetW > displayWidth)
|
||||
targetW = displayWidth;
|
||||
if (targetW > 0 && targetH > 0)
|
||||
image = image.scaled(QSize(targetW, targetH), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
else if (targetW > 0)
|
||||
image = image.scaledToWidth(targetW, Qt::SmoothTransformation);
|
||||
else
|
||||
image = image.scaledToHeight(targetH, Qt::SmoothTransformation);
|
||||
} else if (displayWidth > 0 && image.width() > displayWidth) {
|
||||
image = image.scaledToWidth(displayWidth, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
QQuickImageResponse *Fb2ImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString fb2Path;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
fb2Path = m_fb2Path;
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
// id may have "?fb2=<encoded-path>" appended
|
||||
QString binaryId = id;
|
||||
const int qmark = id.indexOf(QLatin1Char('?'));
|
||||
if (qmark >= 0) {
|
||||
binaryId = id.left(qmark);
|
||||
const QString query = id.mid(qmark + 1);
|
||||
const QLatin1String key("fb2=");
|
||||
if (query.startsWith(key))
|
||||
fb2Path = QUrl::fromPercentEncoding(query.mid(key.size()).toUtf8());
|
||||
}
|
||||
|
||||
auto *response = new Fb2ImageResponse(fb2Path, binaryId, displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage Fb2ImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
if (url.scheme() != QLatin1String("image"))
|
||||
return {};
|
||||
|
||||
QString binaryId = url.path();
|
||||
if (binaryId.startsWith(QLatin1Char('/')))
|
||||
binaryId = binaryId.mid(1);
|
||||
if (binaryId.isEmpty())
|
||||
return {};
|
||||
|
||||
QString fb2Path;
|
||||
if (url.hasQuery()) {
|
||||
const QString q = url.query();
|
||||
const QLatin1String key("fb2=");
|
||||
if (q.startsWith(key))
|
||||
fb2Path = QUrl::fromPercentEncoding(q.mid(key.size()).toUtf8());
|
||||
}
|
||||
if (fb2Path.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
fb2Path = m_fb2Path;
|
||||
}
|
||||
|
||||
return loadImageFromFb2(fb2Path, binaryId, maxWidth, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> Fb2ImageProvider::clone() const
|
||||
{
|
||||
QString fb2Path;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
fb2Path = m_fb2Path;
|
||||
}
|
||||
auto c = std::make_unique<Fb2ImageProvider>();
|
||||
c->setFilePath(fb2Path);
|
||||
return c;
|
||||
}
|
||||
|
||||
void Fb2ImageProvider::setFilePath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_fb2Path = filePath;
|
||||
}
|
||||
|
||||
QString Fb2ImageProvider::coverImageUrl(const QString &coverPath) const
|
||||
{
|
||||
QString fb2Path;
|
||||
{ QMutexLocker lock(&m_mutex); fb2Path = m_fb2Path; }
|
||||
return QStringLiteral("image://fb2/") + coverPath
|
||||
+ QStringLiteral("?fb2=")
|
||||
+ QString::fromUtf8(QUrl::toPercentEncoding(fb2Path));
|
||||
}
|
||||
|
||||
QString Fb2ImageProvider::scheme() const
|
||||
{
|
||||
return QStringLiteral("fb2");
|
||||
}
|
||||
|
||||
void Fb2ImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void Fb2ImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_fb2Path.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMutex>
|
||||
#include <QRunnable>
|
||||
#include <QString>
|
||||
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
class Fb2ImageResponse : public QQuickImageResponse, public QRunnable {
|
||||
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||
public:
|
||||
Fb2ImageResponse(const QString &fb2Path, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
QQuickTextureFactory *textureFactory() const override;
|
||||
void cancel() override;
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
const QString m_fb2Path;
|
||||
const QString m_id;
|
||||
const int m_displayWidth;
|
||||
const QSize m_requestedSize;
|
||||
QAtomicInt m_cancelled{0};
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
class Fb2ImageProvider : public IImageProvider, public IImageLoader {
|
||||
public:
|
||||
QQuickImageResponse *requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize) override;
|
||||
|
||||
void setFilePath(const QString &filePath) override;
|
||||
void setDisplayWidth(int width) override;
|
||||
void clear() override;
|
||||
QString coverImageUrl(const QString &coverPath) const override;
|
||||
QString scheme() const override;
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override;
|
||||
std::unique_ptr<IImageLoader> clone() const override;
|
||||
|
||||
static QImage loadImageFromFb2(const QString &fb2Path, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
private:
|
||||
mutable QMutex m_mutex;
|
||||
QString m_fb2Path;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
@@ -0,0 +1,355 @@
|
||||
#include "Fb2Reader.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QDomDocument>
|
||||
#include <QFileInfo>
|
||||
#include <QStringConverter>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <uchardet/uchardet.h>
|
||||
#include <iconv.h>
|
||||
#include <fstream>
|
||||
|
||||
static constexpr int kChunkSize = 4096;
|
||||
|
||||
static QString decodeWithIconv(const QByteArray &raw, const char *charset)
|
||||
{
|
||||
iconv_t cd = iconv_open("UTF-8", charset);
|
||||
if (cd == reinterpret_cast<iconv_t>(-1))
|
||||
return {};
|
||||
QByteArray out(raw.size() * 4, '\0');
|
||||
char *inBuf = const_cast<char *>(raw.constData());
|
||||
size_t inLeft = raw.size();
|
||||
char *outBuf = out.data();
|
||||
size_t outLeft = out.size();
|
||||
const size_t r = iconv(cd, &inBuf, &inLeft, &outBuf, &outLeft);
|
||||
iconv_close(cd);
|
||||
if (r == static_cast<size_t>(-1))
|
||||
return {};
|
||||
out.resize(out.size() - outLeft);
|
||||
return QString::fromUtf8(out);
|
||||
}
|
||||
|
||||
// Decode FB2 bytes to a Unicode string.
|
||||
// Uses the XML encoding declaration as the authoritative charset source
|
||||
// (uchardet is unreliable on files with large binary sections or sparse text).
|
||||
// QStringConverter / iconv handle the actual conversion.
|
||||
static QString decodeRawBytes(const QByteArray &raw)
|
||||
{
|
||||
// Extract encoding name from XML declaration (first ~200 bytes)
|
||||
QByteArray enc;
|
||||
const int pos = raw.indexOf("encoding=");
|
||||
if (pos >= 0 && pos < 200) {
|
||||
const int qs = pos + 9;
|
||||
if (qs < raw.size()) {
|
||||
const char q = raw[qs];
|
||||
if (q == '"' || q == '\'') {
|
||||
const int qe = raw.indexOf(q, qs + 1);
|
||||
if (qe > qs)
|
||||
enc = raw.mid(qs + 1, qe - qs - 1).toLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (enc.isEmpty() || enc == "utf-8" || enc == "utf8")
|
||||
return QString::fromUtf8(raw);
|
||||
|
||||
// Try Qt's built-in converters first, fall back to iconv for exotic charsets
|
||||
auto qtEncoding = QStringConverter::encodingForName(enc.constData());
|
||||
if (qtEncoding) {
|
||||
QTextStream stream(raw);
|
||||
stream.setEncoding(qtEncoding.value());
|
||||
const QString decoded = stream.readAll();
|
||||
if (!decoded.isEmpty())
|
||||
return decoded;
|
||||
}
|
||||
|
||||
const QString decoded = decodeWithIconv(raw, enc.constData());
|
||||
if (!decoded.isEmpty())
|
||||
return decoded;
|
||||
|
||||
// Last resort: uchardet-based detection
|
||||
uchardet_t ud = uchardet_new();
|
||||
if (uchardet_handle_data(ud, raw.constData(), raw.size()) == 0) {
|
||||
uchardet_data_end(ud);
|
||||
const char *charset = uchardet_get_charset(ud);
|
||||
if (charset && charset[0] != '\0') {
|
||||
const QString s = decodeWithIconv(raw, charset);
|
||||
uchardet_delete(ud);
|
||||
if (!s.isEmpty())
|
||||
return s;
|
||||
}
|
||||
}
|
||||
uchardet_delete(ud);
|
||||
return QString::fromUtf8(raw);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static QDomElement firstChildByTag(const QDomElement &parent, const QString &tag)
|
||||
{
|
||||
for (QDomNode n = parent.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
||||
QDomElement el = n.toElement();
|
||||
if (!el.isNull() && el.tagName() == tag)
|
||||
return el;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// Converts a single FB2 DOM node to an HTML string. Handles inline and block elements.
|
||||
static QString nodeToHtml(const QDomNode &node)
|
||||
{
|
||||
if (node.isText())
|
||||
return node.nodeValue().toHtmlEscaped();
|
||||
|
||||
if (!node.isElement())
|
||||
return {};
|
||||
|
||||
const QDomElement el = node.toElement();
|
||||
const QString tag = el.tagName();
|
||||
|
||||
// Build inner HTML from children
|
||||
QString inner;
|
||||
for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling())
|
||||
inner += nodeToHtml(child);
|
||||
|
||||
if (tag == QLatin1String("p") || tag == QLatin1String("v")) {
|
||||
if (inner.trimmed().isEmpty())
|
||||
return {};
|
||||
return QStringLiteral("<p>") + inner + QStringLiteral("</p>\n");
|
||||
}
|
||||
if (tag == QLatin1String("title"))
|
||||
return inner.trimmed().isEmpty() ? QString{} : QStringLiteral("<h2>") + inner + QStringLiteral("</h2>\n");
|
||||
if (tag == QLatin1String("subtitle"))
|
||||
return inner.trimmed().isEmpty() ? QString{} : QStringLiteral("<h3>") + inner + QStringLiteral("</h3>\n");
|
||||
if (tag == QLatin1String("emphasis"))
|
||||
return QStringLiteral("<em>") + inner + QStringLiteral("</em>");
|
||||
if (tag == QLatin1String("strong"))
|
||||
return QStringLiteral("<strong>") + inner + QStringLiteral("</strong>");
|
||||
if (tag == QLatin1String("epigraph") || tag == QLatin1String("poem") || tag == QLatin1String("cite"))
|
||||
return inner.trimmed().isEmpty() ? QString{} : QStringLiteral("<blockquote>") + inner + QStringLiteral("</blockquote>\n");
|
||||
if (tag == QLatin1String("section") || tag == QLatin1String("stanza"))
|
||||
return inner;
|
||||
if (tag == QLatin1String("image")) {
|
||||
QString href = el.attribute(QStringLiteral("l:href"));
|
||||
if (href.startsWith(QLatin1Char('#')))
|
||||
href = href.mid(1);
|
||||
if (!href.isEmpty())
|
||||
return QStringLiteral("<img src=\"image://fb2/") + href + QStringLiteral("\"/>\n");
|
||||
return {};
|
||||
}
|
||||
if (tag == QLatin1String("a")) {
|
||||
const QString href = el.attribute(QStringLiteral("l:href"));
|
||||
if (!href.isEmpty() && !href.startsWith(QLatin1Char('#')))
|
||||
return QStringLiteral("<a href=\"") + href.toHtmlEscaped() + QStringLiteral("\">") + inner + QStringLiteral("</a>");
|
||||
return inner;
|
||||
}
|
||||
return inner;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Metadata parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
struct Fb2Metadata {
|
||||
QString title, author, coverBinaryId;
|
||||
};
|
||||
|
||||
static Fb2Metadata parseDescription(const QDomElement &desc)
|
||||
{
|
||||
Fb2Metadata meta;
|
||||
const QDomElement titleInfo = firstChildByTag(desc, QStringLiteral("title-info"));
|
||||
if (titleInfo.isNull())
|
||||
return meta;
|
||||
|
||||
const QDomElement bookTitle = firstChildByTag(titleInfo, QStringLiteral("book-title"));
|
||||
if (!bookTitle.isNull())
|
||||
meta.title = bookTitle.text().simplified();
|
||||
|
||||
const QDomElement author = firstChildByTag(titleInfo, QStringLiteral("author"));
|
||||
if (!author.isNull()) {
|
||||
const QString first = firstChildByTag(author, QStringLiteral("first-name")).text().simplified();
|
||||
const QString last = firstChildByTag(author, QStringLiteral("last-name")).text().simplified();
|
||||
if (!first.isEmpty() && !last.isEmpty())
|
||||
meta.author = first + QLatin1Char(' ') + last;
|
||||
else
|
||||
meta.author = first + last;
|
||||
}
|
||||
|
||||
const QDomElement coverpage = firstChildByTag(titleInfo, QStringLiteral("coverpage"));
|
||||
if (!coverpage.isNull()) {
|
||||
const QDomElement img = firstChildByTag(coverpage, QStringLiteral("image"));
|
||||
if (!img.isNull()) {
|
||||
QString href = img.attribute(QStringLiteral("l:href"));
|
||||
if (href.startsWith(QLatin1Char('#')))
|
||||
href = href.mid(1);
|
||||
meta.coverBinaryId = href;
|
||||
}
|
||||
}
|
||||
|
||||
return meta;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// FormatReader interface
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool Fb2Reader::open(const QString &filePath)
|
||||
{
|
||||
m_title.clear();
|
||||
m_author.clear();
|
||||
m_error.clear();
|
||||
m_coverBinaryId.clear();
|
||||
m_chapterHtmlNodes.clear();
|
||||
m_chapterTitles.clear();
|
||||
m_toc.clear();
|
||||
m_contentHash.clear();
|
||||
|
||||
std::ifstream f(filePath.toUtf8().constData(), std::ios::binary | std::ios::ate);
|
||||
if (!f) {
|
||||
m_error = QStringLiteral("Cannot open file: ") + filePath;
|
||||
return false;
|
||||
}
|
||||
const auto size = f.tellg();
|
||||
f.seekg(0);
|
||||
QByteArray raw(static_cast<int>(size), '\0');
|
||||
f.read(raw.data(), size);
|
||||
|
||||
m_contentHash = QCryptographicHash::hash(raw, QCryptographicHash::Md5);
|
||||
|
||||
QDomDocument doc;
|
||||
const auto result = doc.setContent(decodeRawBytes(raw), QDomDocument::ParseOption::Default);
|
||||
if (!result) {
|
||||
m_error = QStringLiteral("Failed to parse FB2 XML: ") + result.errorMessage;
|
||||
return false;
|
||||
}
|
||||
|
||||
const QDomElement root = doc.documentElement();
|
||||
|
||||
// Metadata
|
||||
const QDomElement desc = firstChildByTag(root, QStringLiteral("description"));
|
||||
if (!desc.isNull()) {
|
||||
const Fb2Metadata meta = parseDescription(desc);
|
||||
m_title = meta.title;
|
||||
m_author = meta.author;
|
||||
m_coverBinaryId = meta.coverBinaryId;
|
||||
}
|
||||
|
||||
if (m_title.isEmpty())
|
||||
m_title = QFileInfo(filePath).completeBaseName();
|
||||
|
||||
// Body sections
|
||||
for (QDomNode bodyNode = root.firstChild(); !bodyNode.isNull(); bodyNode = bodyNode.nextSibling()) {
|
||||
const QDomElement body = bodyNode.toElement();
|
||||
if (body.isNull() || body.tagName() != QLatin1String("body"))
|
||||
continue;
|
||||
// Skip named bodies (e.g. notes)
|
||||
if (!body.attribute(QStringLiteral("name")).isEmpty())
|
||||
continue;
|
||||
|
||||
for (QDomNode secNode = body.firstChild(); !secNode.isNull(); secNode = secNode.nextSibling()) {
|
||||
const QDomElement section = secNode.toElement();
|
||||
if (section.isNull() || section.tagName() != QLatin1String("section"))
|
||||
continue;
|
||||
|
||||
// Extract section title
|
||||
const QDomElement titleEl = firstChildByTag(section, QStringLiteral("title"));
|
||||
QString sectionTitle = titleEl.isNull() ? QString{} : titleEl.text().simplified();
|
||||
if (sectionTitle.isEmpty())
|
||||
sectionTitle = QStringLiteral("Chapter %1").arg(m_chapterHtmlNodes.size() + 1);
|
||||
m_chapterTitles.append(sectionTitle);
|
||||
|
||||
// Convert section children to HTML node list
|
||||
QStringList htmlNodes;
|
||||
for (QDomNode child = section.firstChild(); !child.isNull(); child = child.nextSibling()) {
|
||||
const QString html = nodeToHtml(child);
|
||||
if (!html.isEmpty())
|
||||
htmlNodes.append(html);
|
||||
}
|
||||
m_chapterHtmlNodes.append(htmlNodes);
|
||||
|
||||
TocEntry entry;
|
||||
entry.title = sectionTitle;
|
||||
entry.chapterIndex = m_chapterHtmlNodes.size() - 1;
|
||||
entry.depth = 0;
|
||||
m_toc.append(entry);
|
||||
}
|
||||
break; // only process first unnamed body
|
||||
}
|
||||
|
||||
m_filePath = filePath;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Fb2Reader::loadMetadata(const QString &filePath)
|
||||
{
|
||||
m_title.clear();
|
||||
m_author.clear();
|
||||
m_error.clear();
|
||||
m_coverBinaryId.clear();
|
||||
|
||||
std::ifstream f(filePath.toUtf8().constData(), std::ios::binary | std::ios::ate);
|
||||
if (!f) {
|
||||
m_error = QStringLiteral("Cannot open file: ") + filePath;
|
||||
return false;
|
||||
}
|
||||
const auto size = f.tellg();
|
||||
f.seekg(0);
|
||||
QByteArray raw(static_cast<int>(size), '\0');
|
||||
f.read(raw.data(), size);
|
||||
|
||||
QDomDocument doc;
|
||||
const auto result = doc.setContent(decodeRawBytes(raw), QDomDocument::ParseOption::Default);
|
||||
if (!result) {
|
||||
m_error = QStringLiteral("Failed to parse FB2 XML: ") + result.errorMessage;
|
||||
return false;
|
||||
}
|
||||
|
||||
const QDomElement root = doc.documentElement();
|
||||
const QDomElement desc = firstChildByTag(root, QStringLiteral("description"));
|
||||
if (!desc.isNull()) {
|
||||
const Fb2Metadata meta = parseDescription(desc);
|
||||
m_title = meta.title;
|
||||
m_author = meta.author;
|
||||
m_coverBinaryId = meta.coverBinaryId;
|
||||
}
|
||||
|
||||
if (m_title.isEmpty())
|
||||
m_title = QFileInfo(filePath).completeBaseName();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString Fb2Reader::lastError() { return m_error; }
|
||||
QString Fb2Reader::title() { return m_title; }
|
||||
QString Fb2Reader::author() { return m_author; }
|
||||
QString Fb2Reader::coverImagePath() { return m_coverBinaryId; }
|
||||
int Fb2Reader::chapterCount() { return m_chapterHtmlNodes.size(); }
|
||||
QList<TocEntry> Fb2Reader::tableOfContents() { return m_toc; }
|
||||
QString Fb2Reader::filePath() { return m_filePath; }
|
||||
QByteArray Fb2Reader::contentHash() { return m_contentHash; }
|
||||
QString Fb2Reader::fileType() const { return QStringLiteral("fb2"); }
|
||||
|
||||
Chapter Fb2Reader::loadChapter(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_chapterHtmlNodes.size())
|
||||
return Chapter{index, {}, {}};
|
||||
|
||||
const QStringList &nodes = m_chapterHtmlNodes[index];
|
||||
QStringList chunks;
|
||||
QString current;
|
||||
|
||||
for (const QString &node : nodes) {
|
||||
if (!current.isEmpty() && current.size() + node.size() > kChunkSize) {
|
||||
chunks.append(current);
|
||||
current.clear();
|
||||
}
|
||||
current += node;
|
||||
}
|
||||
if (!current.isEmpty())
|
||||
chunks.append(current);
|
||||
|
||||
return Chapter{index, m_chapterTitles[index], chunks};
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef FB2READER_H
|
||||
#define FB2READER_H
|
||||
|
||||
#include "backend/FormatReader.h"
|
||||
#include <QByteArray>
|
||||
#include <QList>
|
||||
|
||||
class Fb2Reader : public FormatReader {
|
||||
public:
|
||||
bool open(const QString &filePath) override;
|
||||
bool loadMetadata(const QString &filePath) override;
|
||||
QString lastError() override;
|
||||
int chapterCount() override;
|
||||
Chapter loadChapter(int index) override;
|
||||
QList<TocEntry> tableOfContents() override;
|
||||
QString filePath() override;
|
||||
QString title() override;
|
||||
QString author() override;
|
||||
QString coverImagePath() override;
|
||||
QByteArray contentHash() override;
|
||||
QString fileType() const override;
|
||||
|
||||
private:
|
||||
QString m_filePath;
|
||||
QByteArray m_contentHash;
|
||||
QString m_title;
|
||||
QString m_author;
|
||||
QString m_error;
|
||||
QString m_coverBinaryId;
|
||||
QList<QStringList> m_chapterHtmlNodes;
|
||||
QStringList m_chapterTitles;
|
||||
QList<TocEntry> m_toc;
|
||||
};
|
||||
|
||||
#endif // FB2READER_H
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ImageLoaderFactory.h"
|
||||
#include "EpubImageProvider.h"
|
||||
#include "Fb2ImageProvider.h"
|
||||
#include "WebImageProvider.h"
|
||||
#include <QFileInfo>
|
||||
|
||||
@@ -47,5 +48,10 @@ std::unique_ptr<IImageLoader> ImageLoaderFactory::create(const QString &filePath
|
||||
|
||||
return composite;
|
||||
}
|
||||
if (ext == QLatin1String("fb2")) {
|
||||
auto loader = std::make_unique<Fb2ImageProvider>();
|
||||
loader->setFilePath(filePath);
|
||||
return loader;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ImageProviderFactory.h"
|
||||
#include "EpubImageProvider.h"
|
||||
#include "Fb2ImageProvider.h"
|
||||
#include <QFileInfo>
|
||||
|
||||
IImageProvider *ImageProviderFactory::create(const QString &filePath)
|
||||
@@ -7,5 +8,7 @@ IImageProvider *ImageProviderFactory::create(const QString &filePath)
|
||||
const QString ext = QFileInfo(filePath).suffix().toLower();
|
||||
if (ext == QLatin1String("epub"))
|
||||
return new EpubImageProvider();
|
||||
if (ext == QLatin1String("fb2"))
|
||||
return new Fb2ImageProvider();
|
||||
return nullptr;
|
||||
}
|
||||
Reference in new issue
Block a user