Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
@@ -1 +0,0 @@
|
||||
// Cbr shells are header-only (constructors inline in Cbr.h).
|
||||
@@ -1,7 +0,0 @@
|
||||
#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"
|
||||
@@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicImageProvider.h"
|
||||
#include "RarComicSource.h"
|
||||
|
||||
class CbrImageProvider : public ComicImageProvider {
|
||||
public:
|
||||
CbrImageProvider()
|
||||
: ComicImageProvider([] { return std::make_unique<RarComicSource>(); },
|
||||
QStringLiteral("cbr")) {}
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicReader.h"
|
||||
#include "RarComicSource.h"
|
||||
#include "backend/formats/sources/RarByteSource.h"
|
||||
|
||||
class CbrReader : public ComicReader {
|
||||
public:
|
||||
CbrReader()
|
||||
: ComicReader([] { return std::make_unique<RarComicSource>(); },
|
||||
: ComicReader([] { return std::make_unique<RarByteSource>(); },
|
||||
QStringLiteral("cbr"),
|
||||
QStringLiteral("cbr")) {}
|
||||
};
|
||||
@@ -1 +0,0 @@
|
||||
// Cbz shells are header-only (constructors inline in Cbz.h).
|
||||
@@ -1,7 +0,0 @@
|
||||
#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"
|
||||
@@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicImageProvider.h"
|
||||
#include "ZipComicSource.h"
|
||||
|
||||
class CbzImageProvider : public ComicImageProvider {
|
||||
public:
|
||||
CbzImageProvider()
|
||||
: ComicImageProvider([] { return std::make_unique<ZipComicSource>(); },
|
||||
QStringLiteral("cbz")) {}
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicReader.h"
|
||||
#include "ZipComicSource.h"
|
||||
#include "backend/formats/sources/ZipByteSource.h"
|
||||
|
||||
class CbzReader : public ComicReader {
|
||||
public:
|
||||
CbzReader()
|
||||
: ComicReader([] { return std::make_unique<ZipComicSource>(); },
|
||||
: ComicReader([] { return std::make_unique<ZipByteSource>(); },
|
||||
QStringLiteral("cbz"),
|
||||
QStringLiteral("cbz")) {}
|
||||
};
|
||||
@@ -1,202 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
#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;
|
||||
};
|
||||
@@ -3,9 +3,11 @@
|
||||
#include <QBuffer>
|
||||
#include <QFileInfo>
|
||||
#include <QImageReader>
|
||||
#include <QSet>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
#include "backend/formats/ImageExtensions.h"
|
||||
#include "backend/formats/ImageUrl.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ComicInfo.xml parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -55,12 +57,6 @@ static void parseComicInfo(const QByteArray &data,
|
||||
// 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))
|
||||
@@ -79,7 +75,7 @@ bool ComicReader::parseSource(const QString &filePath)
|
||||
return parseOpenSource(filePath, *source);
|
||||
}
|
||||
|
||||
bool ComicReader::parseOpenSource(const QString &filePath, IComicSource &source)
|
||||
bool ComicReader::parseOpenSource(const QString &filePath, IByteSource &source)
|
||||
{
|
||||
const QStringList allEntries = source.listEntries();
|
||||
|
||||
@@ -91,7 +87,7 @@ bool ComicReader::parseOpenSource(const QString &filePath, IComicSource &source)
|
||||
? entry.section(QLatin1Char('/'), -1) : entry;
|
||||
if (baseName.startsWith(QLatin1Char('.')))
|
||||
continue;
|
||||
if (kImageExtensions.contains(QFileInfo(entry).suffix().toLower()))
|
||||
if (imageFileExtensions().contains(QFileInfo(entry).suffix().toLower()))
|
||||
imageEntries.append(entry);
|
||||
}
|
||||
imageEntries.sort();
|
||||
@@ -177,8 +173,8 @@ Chapter ComicReader::loadChapter(int index)
|
||||
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));
|
||||
"<img src=\"%1\"/></p>")
|
||||
.arg(ImageUrl::build(m_scheme, page.entry)));
|
||||
}
|
||||
return Chapter{0, m_title, chunks};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "backend/FormatReader.h"
|
||||
#include "IComicSource.h"
|
||||
#include "backend/formats/IByteSource.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
class ComicReader : public FormatReader {
|
||||
public:
|
||||
using SourceFactory = std::function<std::unique_ptr<IComicSource>()>;
|
||||
using SourceFactory = std::function<std::unique_ptr<IByteSource>()>;
|
||||
|
||||
ComicReader(SourceFactory sourceFactory, QString fileType, QString scheme);
|
||||
|
||||
@@ -40,7 +40,7 @@ private:
|
||||
};
|
||||
|
||||
bool parseSource(const QString &filePath);
|
||||
bool parseOpenSource(const QString &filePath, IComicSource &source);
|
||||
bool parseOpenSource(const QString &filePath, IByteSource &source);
|
||||
|
||||
SourceFactory m_sourceFactory;
|
||||
QString m_fileType;
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#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;
|
||||
};
|
||||
@@ -1 +0,0 @@
|
||||
// Folder shells are header-only (constructors inline in Folder.h).
|
||||
@@ -1,7 +0,0 @@
|
||||
#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"
|
||||
@@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicImageProvider.h"
|
||||
#include "DirComicSource.h"
|
||||
|
||||
class FolderImageProvider : public ComicImageProvider {
|
||||
public:
|
||||
FolderImageProvider()
|
||||
: ComicImageProvider([] { return std::make_unique<DirComicSource>(); },
|
||||
QStringLiteral("dir")) {}
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "ComicReader.h"
|
||||
#include "DirComicSource.h"
|
||||
#include "backend/formats/sources/DirByteSource.h"
|
||||
|
||||
class FolderReader : public ComicReader {
|
||||
public:
|
||||
FolderReader()
|
||||
: ComicReader([] { return std::make_unique<DirComicSource>(); },
|
||||
QStringLiteral("<dir>"),
|
||||
: ComicReader([] { return std::make_unique<DirByteSource>(); },
|
||||
QStringLiteral("folder"),
|
||||
QStringLiteral("dir")) {}
|
||||
};
|
||||
@@ -1,25 +0,0 @@
|
||||
#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;
|
||||
};
|
||||
@@ -1,85 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#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;
|
||||
};
|
||||
@@ -1,52 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#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