Manga (CBZ) Support

This commit is contained in:
Agel committed 2026-05-12 16:37:03 +02:00
1 parent 20dfdb3745
commit 9167f10b1c
12 files changed
+937 -17

No files matched your search

+174
View File
@@ -0,0 +1,174 @@
#include "CbzReader.h"
#include "backend/ZipUtils.h"
#include <QCryptographicHash>
#include <QFile>
#include <QFileInfo>
#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);
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()))
m_pageEntries.append(entry);
}
m_pageEntries.sort();
if (m_pageEntries.isEmpty()) {
m_error = QStringLiteral("No images found in archive: ") + filePath;
return false;
}
m_coverEntry = m_pageEntries.first();
// 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];
}
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 QString &entry : m_pageEntries) {
chunks.append(QStringLiteral("<p style=\"text-align:center;margin:0\">"
"<img src=\"image://cbz/%1\"/></p>").arg(entry));
}
return Chapter{0, m_title, chunks};
}
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 {}; }