Manga (CBZ) Support
This commit is contained in:
12 files changed
+937
-17
No files matched your search
@@ -0,0 +1,194 @@
|
||||
#include "CbzImageProvider.h"
|
||||
|
||||
#include "backend/ZipUtils.h"
|
||||
|
||||
#include <QImage>
|
||||
#include <QMutexLocker>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CbzImageResponse
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
CbzImageResponse::CbzImageResponse(const QString &cbzPath, const QString &entryName,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
: m_cbzPath(cbzPath)
|
||||
, m_entryName(entryName)
|
||||
, m_displayWidth(displayWidth)
|
||||
, m_requestedSize(requestedSize)
|
||||
{
|
||||
setAutoDelete(false);
|
||||
}
|
||||
|
||||
QQuickTextureFactory *CbzImageResponse::textureFactory() const
|
||||
{
|
||||
if (m_image.isNull())
|
||||
return nullptr;
|
||||
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
||||
}
|
||||
|
||||
void CbzImageResponse::cancel()
|
||||
{
|
||||
m_cancelled.storeRelaxed(1);
|
||||
}
|
||||
|
||||
void CbzImageResponse::run()
|
||||
{
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
m_image = CbzImageProvider::loadImageFromCbz(m_cbzPath, m_entryName,
|
||||
m_displayWidth, m_requestedSize);
|
||||
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
emit finished();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CbzImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage CbzImageProvider::loadImageFromCbz(const QString &cbzPath, const QString &entryName,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
{
|
||||
if (cbzPath.isEmpty() || entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
unzFile zf = unzOpen(cbzPath.toUtf8().constData());
|
||||
if (!zf)
|
||||
return {};
|
||||
auto closeGuard = [](void *p) { unzClose(static_cast<unzFile>(p)); };
|
||||
std::unique_ptr<void, decltype(closeGuard)> guard(zf, closeGuard);
|
||||
|
||||
const QByteArray data = ZipUtils::readZipEntry(zf, entryName.toUtf8().constData());
|
||||
if (data.isEmpty())
|
||||
return {};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
QQuickImageResponse *CbzImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString cbzPath;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
cbzPath = m_cbzPath;
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
// id may be "entry/name.jpg?cbz=<encoded-path>"
|
||||
// Qt Quick passes the path portion of the URL in percent-encoded form
|
||||
// (e.g. "{HQ}" becomes "%7BHQ%7D"), so decode it back to get the real entry name.
|
||||
QString entryName = id;
|
||||
const int qmark = id.indexOf(QLatin1Char('?'));
|
||||
if (qmark >= 0) {
|
||||
entryName = id.left(qmark);
|
||||
const QString query = id.mid(qmark + 1);
|
||||
const QLatin1String key("cbz=");
|
||||
if (query.startsWith(key))
|
||||
cbzPath = QUrl::fromPercentEncoding(query.mid(key.size()).toUtf8());
|
||||
}
|
||||
entryName = QUrl::fromPercentEncoding(entryName.toUtf8());
|
||||
|
||||
auto *response = new CbzImageResponse(cbzPath, entryName, displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage CbzImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
if (url.scheme() != QLatin1String("image"))
|
||||
return {};
|
||||
|
||||
QString entryName = url.path();
|
||||
if (entryName.startsWith(QLatin1Char('/')))
|
||||
entryName = entryName.mid(1);
|
||||
if (entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
QString cbzPath;
|
||||
if (url.hasQuery()) {
|
||||
const QString q = url.query();
|
||||
const QLatin1String key("cbz=");
|
||||
if (q.startsWith(key))
|
||||
cbzPath = QUrl::fromPercentEncoding(q.mid(key.size()).toUtf8());
|
||||
}
|
||||
if (cbzPath.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
cbzPath = m_cbzPath;
|
||||
}
|
||||
|
||||
return loadImageFromCbz(cbzPath, entryName, maxWidth, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> CbzImageProvider::clone() const
|
||||
{
|
||||
QString cbzPath;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
cbzPath = m_cbzPath;
|
||||
}
|
||||
auto c = std::make_unique<CbzImageProvider>();
|
||||
c->setFilePath(cbzPath);
|
||||
return c;
|
||||
}
|
||||
|
||||
void CbzImageProvider::setFilePath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_cbzPath = filePath;
|
||||
}
|
||||
|
||||
QString CbzImageProvider::coverImageUrl(const QString &coverPath) const
|
||||
{
|
||||
QString cbzPath;
|
||||
{ QMutexLocker lock(&m_mutex); cbzPath = m_cbzPath; }
|
||||
return QStringLiteral("image://cbz/") + coverPath
|
||||
+ QStringLiteral("?cbz=")
|
||||
+ QString::fromUtf8(QUrl::toPercentEncoding(cbzPath));
|
||||
}
|
||||
|
||||
QString CbzImageProvider::scheme() const
|
||||
{
|
||||
return QStringLiteral("cbz");
|
||||
}
|
||||
|
||||
void CbzImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void CbzImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_cbzPath.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef CBZIMAGEPROVIDER_H
|
||||
#define CBZIMAGEPROVIDER_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QRunnable>
|
||||
#include <QString>
|
||||
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
class CbzImageResponse : public QQuickImageResponse, public QRunnable {
|
||||
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||
public:
|
||||
CbzImageResponse(const QString &cbzPath, const QString &entryName,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
QQuickTextureFactory *textureFactory() const override;
|
||||
void cancel() override;
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
const QString m_cbzPath;
|
||||
const QString m_entryName;
|
||||
const int m_displayWidth;
|
||||
const QSize m_requestedSize;
|
||||
QAtomicInt m_cancelled{0};
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
class CbzImageProvider : 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 loadImageFromCbz(const QString &cbzPath, const QString &entryName,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
private:
|
||||
mutable QMutex m_mutex;
|
||||
QString m_cbzPath;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
|
||||
#endif // CBZIMAGEPROVIDER_H
|
||||
@@ -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 {}; }
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef CBZREADER_H
|
||||
#define CBZREADER_H
|
||||
|
||||
#include "backend/FormatReader.h"
|
||||
#include <QByteArray>
|
||||
#include <QList>
|
||||
#include <QStringList>
|
||||
|
||||
class CbzReader : 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:
|
||||
bool parseArchive(const QString &filePath);
|
||||
|
||||
QString m_filePath;
|
||||
QByteArray m_contentHash;
|
||||
QString m_title;
|
||||
QString m_author;
|
||||
QString m_error;
|
||||
QStringList m_pageEntries;
|
||||
QString m_coverEntry;
|
||||
};
|
||||
|
||||
#endif // CBZREADER_H
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "ImageLoaderFactory.h"
|
||||
#include "CbzImageProvider.h"
|
||||
#include "EpubImageProvider.h"
|
||||
#include "Fb2ImageProvider.h"
|
||||
#include "WebImageProvider.h"
|
||||
@@ -53,5 +54,10 @@ std::unique_ptr<IImageLoader> ImageLoaderFactory::create(const QString &filePath
|
||||
loader->setFilePath(filePath);
|
||||
return loader;
|
||||
}
|
||||
if (ext == QLatin1String("cbz")) {
|
||||
auto loader = std::make_unique<CbzImageProvider>();
|
||||
loader->setFilePath(filePath);
|
||||
return loader;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "ImageProviderFactory.h"
|
||||
#include "CbzImageProvider.h"
|
||||
#include "EpubImageProvider.h"
|
||||
#include "Fb2ImageProvider.h"
|
||||
#include <QFileInfo>
|
||||
@@ -10,5 +11,7 @@ IImageProvider *ImageProviderFactory::create(const QString &filePath)
|
||||
return new EpubImageProvider();
|
||||
if (ext == QLatin1String("fb2"))
|
||||
return new Fb2ImageProvider();
|
||||
if (ext == QLatin1String("cbz"))
|
||||
return new CbzImageProvider();
|
||||
return nullptr;
|
||||
}
|
||||
Reference in new issue
Block a user