CBR and folder formats
This commit is contained in:
40 files changed
+1028
-349
No files matched your search
@@ -0,0 +1 @@
|
||||
// Cbr shells are header-only (constructors inline in Cbr.h).
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
// Convenience header — includes both reader and provider shells.
|
||||
// Use CbrReader.h if you only need the reader (avoids Qt::Quick dependency).
|
||||
|
||||
#include "CbrReader.h"
|
||||
#include "CbrProvider.h"
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicImageProvider.h"
|
||||
#include "RarComicSource.h"
|
||||
|
||||
class CbrImageProvider : public ComicImageProvider {
|
||||
public:
|
||||
CbrImageProvider()
|
||||
: ComicImageProvider([] { return std::make_unique<RarComicSource>(); },
|
||||
QStringLiteral("cbr")) {}
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicReader.h"
|
||||
#include "RarComicSource.h"
|
||||
|
||||
class CbrReader : public ComicReader {
|
||||
public:
|
||||
CbrReader()
|
||||
: ComicReader([] { return std::make_unique<RarComicSource>(); },
|
||||
QStringLiteral("cbr"),
|
||||
QStringLiteral("cbr")) {}
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
// Cbz shells are header-only (constructors inline in Cbz.h).
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
// Convenience header — includes both reader and provider shells.
|
||||
// Use CbzReader.h if you only need the reader (avoids Qt::Quick dependency).
|
||||
|
||||
#include "CbzReader.h"
|
||||
#include "CbzProvider.h"
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicImageProvider.h"
|
||||
#include "ZipComicSource.h"
|
||||
|
||||
class CbzImageProvider : public ComicImageProvider {
|
||||
public:
|
||||
CbzImageProvider()
|
||||
: ComicImageProvider([] { return std::make_unique<ZipComicSource>(); },
|
||||
QStringLiteral("cbz")) {}
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicReader.h"
|
||||
#include "ZipComicSource.h"
|
||||
|
||||
class CbzReader : public ComicReader {
|
||||
public:
|
||||
CbzReader()
|
||||
: ComicReader([] { return std::make_unique<ZipComicSource>(); },
|
||||
QStringLiteral("cbz"),
|
||||
QStringLiteral("cbz")) {}
|
||||
};
|
||||
@@ -0,0 +1,202 @@
|
||||
#include "ComicImageProvider.h"
|
||||
|
||||
#include <QImage>
|
||||
#include <QMutexLocker>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ComicImageResponse
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
ComicImageResponse::ComicImageResponse(SourceFactory sourceFactory,
|
||||
const QString &sourcePath,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize)
|
||||
: m_sourceFactory(std::move(sourceFactory))
|
||||
, m_sourcePath(sourcePath)
|
||||
, m_entryName(entryName)
|
||||
, m_displayWidth(displayWidth)
|
||||
, m_requestedSize(requestedSize)
|
||||
{
|
||||
setAutoDelete(false);
|
||||
}
|
||||
|
||||
QQuickTextureFactory *ComicImageResponse::textureFactory() const
|
||||
{
|
||||
if (m_image.isNull())
|
||||
return nullptr;
|
||||
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
||||
}
|
||||
|
||||
void ComicImageResponse::cancel()
|
||||
{
|
||||
m_cancelled.storeRelaxed(1);
|
||||
}
|
||||
|
||||
void ComicImageResponse::run()
|
||||
{
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
auto source = m_sourceFactory();
|
||||
if (source->open(m_sourcePath))
|
||||
m_image = ComicImageProvider::loadImageFromSource(*source, m_sourcePath,
|
||||
m_entryName, m_displayWidth,
|
||||
m_requestedSize);
|
||||
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
emit finished();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ComicImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage ComicImageProvider::loadImageFromSource(IComicSource &source,
|
||||
const QString &/*sourcePath*/,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
if (entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
const QByteArray data = source.readEntry(entryName);
|
||||
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;
|
||||
}
|
||||
|
||||
ComicImageProvider::ComicImageProvider(SourceFactory sourceFactory, QString scheme)
|
||||
: m_sourceFactory(std::move(sourceFactory))
|
||||
, m_scheme(std::move(scheme))
|
||||
{
|
||||
}
|
||||
|
||||
QQuickImageResponse *ComicImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString sourcePath;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
// id may be "entry/name.jpg?src=<encoded-path>"
|
||||
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("src=");
|
||||
if (query.startsWith(key))
|
||||
sourcePath = QUrl::fromPercentEncoding(query.mid(key.size()).toUtf8());
|
||||
}
|
||||
entryName = QUrl::fromPercentEncoding(entryName.toUtf8());
|
||||
|
||||
auto *response = new ComicImageResponse(m_sourceFactory, sourcePath, entryName,
|
||||
displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage ComicImageProvider::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 sourcePath;
|
||||
if (url.hasQuery()) {
|
||||
const QString q = url.query();
|
||||
const QLatin1String key("src=");
|
||||
if (q.startsWith(key))
|
||||
sourcePath = QUrl::fromPercentEncoding(q.mid(key.size()).toUtf8());
|
||||
}
|
||||
if (sourcePath.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
|
||||
auto source = m_sourceFactory();
|
||||
if (!source->open(sourcePath))
|
||||
return {};
|
||||
return loadImageFromSource(*source, sourcePath, entryName, maxWidth, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> ComicImageProvider::clone() const
|
||||
{
|
||||
QString sourcePath;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
auto c = std::make_unique<ComicImageProvider>(m_sourceFactory, m_scheme);
|
||||
c->setFilePath(sourcePath);
|
||||
return c;
|
||||
}
|
||||
|
||||
void ComicImageProvider::setFilePath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_sourcePath = filePath;
|
||||
}
|
||||
|
||||
QString ComicImageProvider::coverImageUrl(const QString &coverPath) const
|
||||
{
|
||||
QString sourcePath;
|
||||
{ QMutexLocker lock(&m_mutex); sourcePath = m_sourcePath; }
|
||||
return QStringLiteral("image://") + m_scheme + QLatin1Char('/') + coverPath
|
||||
+ QStringLiteral("?src=")
|
||||
+ QString::fromUtf8(QUrl::toPercentEncoding(sourcePath));
|
||||
}
|
||||
|
||||
QString ComicImageProvider::scheme() const
|
||||
{
|
||||
return m_scheme;
|
||||
}
|
||||
|
||||
void ComicImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void ComicImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_sourcePath.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComicSource.h"
|
||||
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <QAtomicInt>
|
||||
#include <QMutex>
|
||||
#include <QRunnable>
|
||||
#include <QString>
|
||||
|
||||
class ComicImageResponse : public QQuickImageResponse, public QRunnable {
|
||||
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||
public:
|
||||
using SourceFactory = std::function<std::unique_ptr<IComicSource>()>;
|
||||
|
||||
ComicImageResponse(SourceFactory sourceFactory,
|
||||
const QString &sourcePath,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize);
|
||||
|
||||
QQuickTextureFactory *textureFactory() const override;
|
||||
void cancel() override;
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
SourceFactory m_sourceFactory;
|
||||
const QString m_sourcePath;
|
||||
const QString m_entryName;
|
||||
const int m_displayWidth;
|
||||
const QSize m_requestedSize;
|
||||
QAtomicInt m_cancelled{0};
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
class ComicImageProvider : public IImageProvider, public IImageLoader {
|
||||
public:
|
||||
using SourceFactory = std::function<std::unique_ptr<IComicSource>()>;
|
||||
|
||||
ComicImageProvider(SourceFactory sourceFactory, QString scheme);
|
||||
|
||||
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 loadImageFromSource(IComicSource &source,
|
||||
const QString &sourcePath,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize);
|
||||
|
||||
private:
|
||||
SourceFactory m_sourceFactory;
|
||||
QString m_scheme;
|
||||
mutable QMutex m_mutex;
|
||||
QString m_sourcePath;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
@@ -0,0 +1,206 @@
|
||||
#include "ComicReader.h"
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QFileInfo>
|
||||
#include <QImageReader>
|
||||
#include <QSet>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 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;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ComicReader
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static const QSet<QString> kImageExtensions = {
|
||||
QStringLiteral("jpg"), QStringLiteral("jpeg"),
|
||||
QStringLiteral("png"), QStringLiteral("gif"),
|
||||
QStringLiteral("webp")
|
||||
};
|
||||
|
||||
ComicReader::ComicReader(SourceFactory sourceFactory, QString fileType, QString scheme)
|
||||
: m_sourceFactory(std::move(sourceFactory))
|
||||
, m_fileType(std::move(fileType))
|
||||
, m_scheme(std::move(scheme))
|
||||
{
|
||||
}
|
||||
|
||||
// Shared parsing logic given an already-opened source.
|
||||
bool ComicReader::parseSource(const QString &filePath)
|
||||
{
|
||||
auto source = m_sourceFactory();
|
||||
if (!source->open(filePath)) {
|
||||
m_error = QStringLiteral("Cannot open comic source: ") + filePath;
|
||||
return false;
|
||||
}
|
||||
return parseOpenSource(filePath, *source);
|
||||
}
|
||||
|
||||
bool ComicReader::parseOpenSource(const QString &filePath, IComicSource &source)
|
||||
{
|
||||
const QStringList allEntries = source.listEntries();
|
||||
|
||||
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();
|
||||
|
||||
m_pageEntries.reserve(imageEntries.size());
|
||||
for (const QString &entry : imageEntries) {
|
||||
QSize naturalSize;
|
||||
const QByteArray data = source.readEntry(entry);
|
||||
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: ") + filePath;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_coverEntry = m_pageEntries.first().entry;
|
||||
|
||||
// ComicInfo.xml: archive root only, case-insensitive
|
||||
for (const QString &entry : allEntries) {
|
||||
if (entry.compare(QLatin1String("ComicInfo.xml"), Qt::CaseInsensitive) == 0) {
|
||||
const QByteArray data = source.readEntry(entry);
|
||||
if (!data.isEmpty()) {
|
||||
int coverIndex = -1;
|
||||
parseComicInfo(data, 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 ComicReader::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();
|
||||
|
||||
auto source = m_sourceFactory();
|
||||
if (!source->open(filePath)) {
|
||||
m_error = QStringLiteral("Cannot open comic source: ") + filePath;
|
||||
return false;
|
||||
}
|
||||
m_contentHash = source->contentHash();
|
||||
return parseOpenSource(filePath, *source);
|
||||
}
|
||||
|
||||
bool ComicReader::loadMetadata(const QString &filePath)
|
||||
{
|
||||
m_title.clear();
|
||||
m_author.clear();
|
||||
m_error.clear();
|
||||
m_pageEntries.clear();
|
||||
m_coverEntry.clear();
|
||||
m_filePath.clear();
|
||||
return parseSource(filePath);
|
||||
}
|
||||
|
||||
Chapter ComicReader::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://%1/%2\"/></p>")
|
||||
.arg(m_scheme, page.entry));
|
||||
}
|
||||
return Chapter{0, m_title, chunks};
|
||||
}
|
||||
|
||||
QVector<QSize> ComicReader::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 ComicReader::lastError() { return m_error; }
|
||||
QString ComicReader::title() { return m_title; }
|
||||
QString ComicReader::author() { return m_author; }
|
||||
QString ComicReader::coverImagePath() { return m_coverEntry; }
|
||||
int ComicReader::chapterCount() { return m_pageEntries.isEmpty() ? 0 : 1; }
|
||||
QString ComicReader::filePath() { return m_filePath; }
|
||||
QByteArray ComicReader::contentHash() { return m_contentHash; }
|
||||
QString ComicReader::fileType() const { return m_fileType; }
|
||||
|
||||
QList<TocEntry> ComicReader::tableOfContents() { return {}; }
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "backend/FormatReader.h"
|
||||
#include "IComicSource.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <QByteArray>
|
||||
#include <QList>
|
||||
#include <QSize>
|
||||
#include <QStringList>
|
||||
#include <QVector>
|
||||
|
||||
class ComicReader : public FormatReader {
|
||||
public:
|
||||
using SourceFactory = std::function<std::unique_ptr<IComicSource>()>;
|
||||
|
||||
ComicReader(SourceFactory sourceFactory, QString fileType, QString scheme);
|
||||
|
||||
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;
|
||||
|
||||
RenderingMode preferredRenderingMode() const override { return RenderingMode::Manga; }
|
||||
QVector<QSize> chapterPageSizes(int chapterIndex) const override;
|
||||
|
||||
private:
|
||||
struct PageEntry {
|
||||
QString entry;
|
||||
QSize naturalSize;
|
||||
};
|
||||
|
||||
bool parseSource(const QString &filePath);
|
||||
bool parseOpenSource(const QString &filePath, IComicSource &source);
|
||||
|
||||
SourceFactory m_sourceFactory;
|
||||
QString m_fileType;
|
||||
QString m_scheme;
|
||||
|
||||
QString m_filePath;
|
||||
QByteArray m_contentHash;
|
||||
QString m_title;
|
||||
QString m_author;
|
||||
QString m_error;
|
||||
QVector<PageEntry> m_pageEntries;
|
||||
QString m_coverEntry;
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
#include "DirComicSource.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
|
||||
bool DirComicSource::open(const QString &path)
|
||||
{
|
||||
m_dir.clear();
|
||||
m_hash.clear();
|
||||
m_entries.clear();
|
||||
|
||||
const QDir d(path);
|
||||
if (!d.exists())
|
||||
return false;
|
||||
|
||||
const auto infos = d.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
|
||||
if (infos.isEmpty())
|
||||
return false;
|
||||
|
||||
QByteArray hashInput;
|
||||
for (const QFileInfo &fi : infos) {
|
||||
m_entries.append(fi.fileName());
|
||||
hashInput += fi.fileName().toUtf8()
|
||||
+ QByteArrayLiteral(":")
|
||||
+ QByteArray::number(fi.size())
|
||||
+ QByteArrayLiteral("\n");
|
||||
}
|
||||
m_hash = QCryptographicHash::hash(hashInput, QCryptographicHash::Md5);
|
||||
m_dir = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
QStringList DirComicSource::listEntries() const
|
||||
{
|
||||
return m_entries;
|
||||
}
|
||||
|
||||
QByteArray DirComicSource::readEntry(const QString &entryName) const
|
||||
{
|
||||
if (m_dir.isEmpty() || entryName.isEmpty())
|
||||
return {};
|
||||
QFile f(m_dir + QLatin1Char('/') + entryName);
|
||||
if (!f.open(QIODevice::ReadOnly))
|
||||
return {};
|
||||
return f.readAll();
|
||||
}
|
||||
|
||||
QByteArray DirComicSource::contentHash() const
|
||||
{
|
||||
return m_hash;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComicSource.h"
|
||||
|
||||
class DirComicSource : public IComicSource {
|
||||
public:
|
||||
bool open(const QString &path) override;
|
||||
QStringList listEntries() const override;
|
||||
QByteArray readEntry(const QString &entryName) const override;
|
||||
QByteArray contentHash() const override;
|
||||
|
||||
private:
|
||||
QString m_dir;
|
||||
QByteArray m_hash;
|
||||
QStringList m_entries;
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
// Folder shells are header-only (constructors inline in Folder.h).
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
// Convenience header — includes both reader and provider shells.
|
||||
// Use FolderReader.h if you only need the reader (avoids Qt::Quick dependency).
|
||||
|
||||
#include "FolderReader.h"
|
||||
#include "FolderProvider.h"
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicImageProvider.h"
|
||||
#include "DirComicSource.h"
|
||||
|
||||
class FolderImageProvider : public ComicImageProvider {
|
||||
public:
|
||||
FolderImageProvider()
|
||||
: ComicImageProvider([] { return std::make_unique<DirComicSource>(); },
|
||||
QStringLiteral("dir")) {}
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicReader.h"
|
||||
#include "DirComicSource.h"
|
||||
|
||||
class FolderReader : public ComicReader {
|
||||
public:
|
||||
FolderReader()
|
||||
: ComicReader([] { return std::make_unique<DirComicSource>(); },
|
||||
QStringLiteral("<dir>"),
|
||||
QStringLiteral("dir")) {}
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
// Abstracts how raw image bytes are obtained from a named entry.
|
||||
// Implementations: ZipComicSource (minizip), RarComicSource (libarchive), DirComicSource (QDir/QFile).
|
||||
class IComicSource {
|
||||
public:
|
||||
virtual ~IComicSource() = default;
|
||||
|
||||
// Opens the source. After returning true, listEntries/readEntry/contentHash are valid.
|
||||
virtual bool open(const QString &path) = 0;
|
||||
|
||||
// All entry names in the source's natural order. Caller filters and sorts.
|
||||
virtual QStringList listEntries() const = 0;
|
||||
|
||||
// Full bytes of one named entry. Returns empty on miss.
|
||||
virtual QByteArray readEntry(const QString &entryName) const = 0;
|
||||
|
||||
// MD5 of the source. For archives: MD5 of file bytes.
|
||||
// For directories: MD5 of sorted "name:size\n" lines.
|
||||
virtual QByteArray contentHash() const = 0;
|
||||
};
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "RarComicSource.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QFile>
|
||||
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
|
||||
bool RarComicSource::open(const QString &path)
|
||||
{
|
||||
m_path.clear();
|
||||
m_hash.clear();
|
||||
m_entries.clear();
|
||||
|
||||
// Walk headers only to build the entry list (no payload decompression here).
|
||||
struct archive *a = archive_read_new();
|
||||
archive_read_support_format_rar(a);
|
||||
archive_read_support_format_rar5(a);
|
||||
const bool opened = archive_read_open_filename(a, path.toUtf8().constData(), 16384) == ARCHIVE_OK;
|
||||
if (opened) {
|
||||
struct archive_entry *e;
|
||||
while (archive_read_next_header(a, &e) == ARCHIVE_OK) {
|
||||
if (archive_entry_filetype(e) == AE_IFREG)
|
||||
m_entries.append(QString::fromUtf8(archive_entry_pathname(e)));
|
||||
archive_read_data_skip(a);
|
||||
}
|
||||
}
|
||||
archive_read_free(a);
|
||||
|
||||
if (!opened || m_entries.isEmpty())
|
||||
return false;
|
||||
|
||||
QFile f(path);
|
||||
if (f.open(QIODevice::ReadOnly)) {
|
||||
m_hash = QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
|
||||
f.close();
|
||||
}
|
||||
|
||||
m_path = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
QStringList RarComicSource::listEntries() const
|
||||
{
|
||||
return m_entries;
|
||||
}
|
||||
|
||||
QByteArray RarComicSource::readEntry(const QString &entryName) const
|
||||
{
|
||||
if (m_path.isEmpty() || entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
const QByteArray nameBytes = entryName.toUtf8();
|
||||
|
||||
struct archive *a = archive_read_new();
|
||||
archive_read_support_format_rar(a);
|
||||
archive_read_support_format_rar5(a);
|
||||
|
||||
if (archive_read_open_filename(a, m_path.toUtf8().constData(), 16384) != ARCHIVE_OK) {
|
||||
archive_read_free(a);
|
||||
return {};
|
||||
}
|
||||
|
||||
QByteArray result;
|
||||
struct archive_entry *e;
|
||||
while (archive_read_next_header(a, &e) == ARCHIVE_OK) {
|
||||
if (qstrcmp(archive_entry_pathname(e), nameBytes.constData()) == 0) {
|
||||
const la_int64_t sz = archive_entry_size(e);
|
||||
if (sz > 0) result.reserve(static_cast<int>(sz));
|
||||
char chunk[65536];
|
||||
la_ssize_t n;
|
||||
while ((n = archive_read_data(a, chunk, sizeof(chunk))) > 0)
|
||||
result.append(chunk, static_cast<int>(n));
|
||||
break;
|
||||
}
|
||||
archive_read_data_skip(a);
|
||||
}
|
||||
archive_read_free(a);
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray RarComicSource::contentHash() const
|
||||
{
|
||||
return m_hash;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComicSource.h"
|
||||
|
||||
class RarComicSource : public IComicSource {
|
||||
public:
|
||||
bool open(const QString &path) override;
|
||||
QStringList listEntries() const override;
|
||||
QByteArray readEntry(const QString &entryName) const override;
|
||||
QByteArray contentHash() const override;
|
||||
|
||||
private:
|
||||
QString m_path;
|
||||
QByteArray m_hash;
|
||||
QStringList m_entries;
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "ZipComicSource.h"
|
||||
|
||||
#include "backend/ZipUtils.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QFile>
|
||||
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
bool ZipComicSource::open(const QString &path)
|
||||
{
|
||||
m_path.clear();
|
||||
m_hash.clear();
|
||||
m_entries.clear();
|
||||
|
||||
unzFile zf = unzOpen(path.toUtf8().constData());
|
||||
if (!zf)
|
||||
return false;
|
||||
m_entries = ZipUtils::listZipEntries(zf);
|
||||
unzClose(zf);
|
||||
|
||||
QFile f(path);
|
||||
if (f.open(QIODevice::ReadOnly)) {
|
||||
m_hash = QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
|
||||
f.close();
|
||||
}
|
||||
|
||||
m_path = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
QStringList ZipComicSource::listEntries() const
|
||||
{
|
||||
return m_entries;
|
||||
}
|
||||
|
||||
QByteArray ZipComicSource::readEntry(const QString &entryName) const
|
||||
{
|
||||
if (m_path.isEmpty() || entryName.isEmpty())
|
||||
return {};
|
||||
unzFile zf = unzOpen(m_path.toUtf8().constData());
|
||||
if (!zf)
|
||||
return {};
|
||||
auto closeGuard = [](void *p) { unzClose(static_cast<unzFile>(p)); };
|
||||
std::unique_ptr<void, decltype(closeGuard)> guard(zf, closeGuard);
|
||||
return ZipUtils::readZipEntry(zf, entryName.toUtf8().constData());
|
||||
}
|
||||
|
||||
QByteArray ZipComicSource::contentHash() const
|
||||
{
|
||||
return m_hash;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComicSource.h"
|
||||
|
||||
class ZipComicSource : public IComicSource {
|
||||
public:
|
||||
bool open(const QString &path) override;
|
||||
QStringList listEntries() const override;
|
||||
QByteArray readEntry(const QString &entryName) const override;
|
||||
QByteArray contentHash() const override;
|
||||
|
||||
private:
|
||||
QString m_path;
|
||||
QByteArray m_hash;
|
||||
QStringList m_entries;
|
||||
};
|
||||
Reference in new issue
Block a user