204 lines
6.6 KiB
C++
204 lines
6.6 KiB
C++
#include "CbzReader.h"
|
|
|
|
#include "backend/ZipUtils.h"
|
|
|
|
#include <QBuffer>
|
|
#include <QCryptographicHash>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QImageReader>
|
|
#include <QSet>
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <minizip/unzip.h>
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// ComicInfo.xml parsing
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static void parseComicInfo(const QByteArray &data,
|
|
QString &outTitle, QString &outAuthor, int &outCoverIndex)
|
|
{
|
|
QXmlStreamReader xml(data);
|
|
QString title, series, number, writer;
|
|
outCoverIndex = -1;
|
|
|
|
while (!xml.atEnd()) {
|
|
xml.readNext();
|
|
if (!xml.isStartElement())
|
|
continue;
|
|
const auto name = xml.name();
|
|
if (name == QLatin1String("Title"))
|
|
title = xml.readElementText().trimmed();
|
|
else if (name == QLatin1String("Series"))
|
|
series = xml.readElementText().trimmed();
|
|
else if (name == QLatin1String("Number"))
|
|
number = xml.readElementText().trimmed();
|
|
else if (name == QLatin1String("Writer"))
|
|
writer = xml.readElementText().trimmed();
|
|
else if (name == QLatin1String("Page")) {
|
|
const auto attrs = xml.attributes();
|
|
if (attrs.value(QLatin1String("Type")) == QLatin1String("FrontCover")) {
|
|
bool ok;
|
|
const int idx = attrs.value(QLatin1String("Image")).toInt(&ok);
|
|
if (ok && outCoverIndex < 0)
|
|
outCoverIndex = idx;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!title.isEmpty()) {
|
|
outTitle = title;
|
|
} else if (!series.isEmpty()) {
|
|
outTitle = series;
|
|
if (!number.isEmpty())
|
|
outTitle += QStringLiteral(" #") + number;
|
|
}
|
|
outAuthor = writer;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// CbzReader
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static const QSet<QString> kImageExtensions = {
|
|
QStringLiteral("jpg"), QStringLiteral("jpeg"),
|
|
QStringLiteral("png"), QStringLiteral("gif"),
|
|
QStringLiteral("webp")
|
|
};
|
|
|
|
bool CbzReader::parseArchive(const QString &filePath)
|
|
{
|
|
unzFile zf = unzOpen(filePath.toUtf8().constData());
|
|
if (!zf) {
|
|
m_error = QStringLiteral("Cannot open as ZIP archive: ") + filePath;
|
|
return false;
|
|
}
|
|
auto closeGuard = [](void *p) { unzClose(static_cast<unzFile>(p)); };
|
|
std::unique_ptr<void, decltype(closeGuard)> guard(zf, closeGuard);
|
|
|
|
const QStringList allEntries = ZipUtils::listZipEntries(zf);
|
|
|
|
QStringList imageEntries;
|
|
for (const QString &entry : allEntries) {
|
|
if (entry.startsWith(QLatin1String("__MACOSX/")))
|
|
continue;
|
|
const QString baseName = entry.contains(QLatin1Char('/'))
|
|
? entry.section(QLatin1Char('/'), -1) : entry;
|
|
if (baseName.startsWith(QLatin1Char('.')))
|
|
continue;
|
|
if (kImageExtensions.contains(QFileInfo(entry).suffix().toLower()))
|
|
imageEntries.append(entry);
|
|
}
|
|
imageEntries.sort();
|
|
|
|
// Read image header for each page to get natural dimensions (header-only, no pixel decode).
|
|
m_pageEntries.reserve(imageEntries.size());
|
|
for (const QString &entry : imageEntries) {
|
|
QSize naturalSize;
|
|
const QByteArray data = ZipUtils::readZipEntry(zf, entry.toUtf8().constData());
|
|
if (!data.isEmpty()) {
|
|
QBuffer buf;
|
|
buf.setData(data);
|
|
buf.open(QIODevice::ReadOnly);
|
|
QImageReader reader(&buf);
|
|
naturalSize = reader.size();
|
|
}
|
|
m_pageEntries.append({entry, naturalSize});
|
|
}
|
|
|
|
if (m_pageEntries.isEmpty()) {
|
|
m_error = QStringLiteral("No images found in archive: ") + filePath;
|
|
return false;
|
|
}
|
|
|
|
m_coverEntry = m_pageEntries.first().entry;
|
|
|
|
// Look for ComicInfo.xml (case-insensitive, archive-root only)
|
|
for (const QString &entry : allEntries) {
|
|
if (entry.compare(QLatin1String("ComicInfo.xml"), Qt::CaseInsensitive) == 0) {
|
|
const QByteArray comicInfoData = ZipUtils::readZipEntry(zf, entry.toUtf8().constData());
|
|
if (!comicInfoData.isEmpty()) {
|
|
int coverIndex = -1;
|
|
parseComicInfo(comicInfoData, m_title, m_author, coverIndex);
|
|
if (coverIndex >= 0 && coverIndex < m_pageEntries.size())
|
|
m_coverEntry = m_pageEntries[coverIndex].entry;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (m_title.isEmpty())
|
|
m_title = QFileInfo(filePath).completeBaseName();
|
|
|
|
m_filePath = filePath;
|
|
return true;
|
|
}
|
|
|
|
bool CbzReader::open(const QString &filePath)
|
|
{
|
|
m_title.clear();
|
|
m_author.clear();
|
|
m_error.clear();
|
|
m_pageEntries.clear();
|
|
m_coverEntry.clear();
|
|
m_contentHash.clear();
|
|
m_filePath.clear();
|
|
|
|
QFile f(filePath);
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
m_contentHash = QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
|
|
f.close();
|
|
}
|
|
|
|
return parseArchive(filePath);
|
|
}
|
|
|
|
bool CbzReader::loadMetadata(const QString &filePath)
|
|
{
|
|
m_title.clear();
|
|
m_author.clear();
|
|
m_error.clear();
|
|
m_pageEntries.clear();
|
|
m_coverEntry.clear();
|
|
m_filePath.clear();
|
|
|
|
return parseArchive(filePath);
|
|
}
|
|
|
|
Chapter CbzReader::loadChapter(int index)
|
|
{
|
|
if (index != 0 || m_pageEntries.isEmpty())
|
|
return Chapter{index, {}, {}};
|
|
|
|
QStringList chunks;
|
|
chunks.reserve(m_pageEntries.size());
|
|
for (const PageEntry &page : m_pageEntries) {
|
|
chunks.append(QStringLiteral("<p style=\"text-align:center;margin:0\">"
|
|
"<img src=\"image://cbz/%1\"/></p>").arg(page.entry));
|
|
}
|
|
return Chapter{0, m_title, chunks};
|
|
}
|
|
|
|
QVector<QSize> CbzReader::chapterPageSizes(int chapterIndex) const
|
|
{
|
|
if (chapterIndex != 0 || m_pageEntries.isEmpty())
|
|
return {};
|
|
QVector<QSize> sizes;
|
|
sizes.reserve(m_pageEntries.size());
|
|
for (const PageEntry &page : m_pageEntries)
|
|
sizes.append(page.naturalSize);
|
|
return sizes;
|
|
}
|
|
|
|
QString CbzReader::lastError() { return m_error; }
|
|
QString CbzReader::title() { return m_title; }
|
|
QString CbzReader::author() { return m_author; }
|
|
QString CbzReader::coverImagePath() { return m_coverEntry; }
|
|
int CbzReader::chapterCount() { return m_pageEntries.isEmpty() ? 0 : 1; }
|
|
QString CbzReader::filePath() { return m_filePath; }
|
|
QByteArray CbzReader::contentHash() { return m_contentHash; }
|
|
QString CbzReader::fileType() const { return QStringLiteral("cbz"); }
|
|
|
|
QList<TocEntry> CbzReader::tableOfContents() { return {}; }
|