old render cleanup part 3

This commit is contained in:
Agel committed 2026-10-25 15:43:51 +01:00
1 parent cef762c3ee
commit 3faedd4251
45 files changed
+1057 -1135

No files matched your search

+3 -35
View File
@@ -1,9 +1,7 @@
#include "EntryImageProvider.h"
#include <QImage>
#include <QMutexLocker>
#include <QThreadPool>
#include <QUrl>
#include "AsyncImageResponse.h"
#include "ImageScaling.h"
@@ -36,20 +34,14 @@ EntryImageProvider::EntryImageProvider(SourceFactory sourceFactory, QString sche
QQuickImageResponse *EntryImageProvider::requestImageResponse(const QString &id,
const QSize &requestedSize)
{
QString sourcePath;
{
QMutexLocker lock(&m_mutex);
sourcePath = m_sourcePath;
}
// id may be "entry/name.jpg?src=<encoded-path>"
const ImageUrl::Parsed parsed = ImageUrl::parseProviderId(id);
const QString entryName = parsed.entry;
if (!parsed.sourcePath.isEmpty())
sourcePath = parsed.sourcePath;
const QString sourcePath = parsed.sourcePath;
auto *response = new AsyncImageResponse(
[factory = m_sourceFactory, sourcePath, entryName, requestedSize]() -> QImage {
if (sourcePath.isEmpty())
return {};
auto source = factory();
if (!source->open(sourcePath))
return {};
@@ -58,27 +50,3 @@ QQuickImageResponse *EntryImageProvider::requestImageResponse(const QString &id,
QThreadPool::globalInstance()->start(response);
return response;
}
void EntryImageProvider::setFilePath(const QString &filePath)
{
QMutexLocker lock(&m_mutex);
m_sourcePath = filePath;
}
QString EntryImageProvider::coverImageUrl(const QString &coverPath) const
{
QString sourcePath;
{ QMutexLocker lock(&m_mutex); sourcePath = m_sourcePath; }
return ImageUrl::build(m_scheme, coverPath, sourcePath);
}
QString EntryImageProvider::scheme() const
{
return m_scheme;
}
void EntryImageProvider::clear()
{
QMutexLocker lock(&m_mutex);
m_sourcePath.clear();
}
+3 -9
View File
@@ -1,14 +1,13 @@
#pragma once
#include "IByteSource.h"
#include "backend/IImageProvider.h"
#include <QQuickAsyncImageProvider>
#include <functional>
#include <memory>
#include <QMutex>
#include <QString>
class EntryImageProvider : public IImageProvider {
class EntryImageProvider : public QQuickAsyncImageProvider {
public:
using SourceFactory = std::function<std::unique_ptr<IByteSource>()>;
@@ -17,10 +16,7 @@ public:
QQuickImageResponse *requestImageResponse(const QString &id,
const QSize &requestedSize) override;
void setFilePath(const QString &filePath) override;
void clear() override;
QString coverImageUrl(const QString &coverPath) const override;
QString scheme() const override;
QString scheme() const { return m_scheme; }
static QImage loadImageFromSource(IByteSource &source,
const QString &entryName,
@@ -29,6 +25,4 @@ public:
private:
SourceFactory m_sourceFactory;
QString m_scheme;
mutable QMutex m_mutex;
QString m_sourcePath;
};
+15 -9
View File
@@ -2,10 +2,23 @@
#include "EntryImageProvider.h"
#include "backend/FormatReader.h"
#include "backend/formats/comic/ComicReader.h"
#include <QFileInfo>
#include <QQmlEngine>
FormatSpec FormatSpec::comic(const QString &ext, bool matchesDirectory, const QString &scheme,
SourceFactory makeSource)
{
return FormatSpec{
ext,
matchesDirectory,
scheme,
[makeSource, scheme] { return std::make_unique<ComicReader>(makeSource, scheme); },
makeSource
};
}
FormatRegistry::FormatRegistry(QVector<FormatSpec> specs)
: m_specs(std::move(specs))
{
@@ -30,14 +43,6 @@ std::unique_ptr<FormatReader> FormatRegistry::createReader(const QString &filePa
return spec ? spec->makeReader() : nullptr;
}
IImageProvider *FormatRegistry::createImageProvider(const QString &filePath) const
{
const FormatSpec *spec = specFor(filePath);
if (!spec || spec->scheme.isEmpty() || !spec->makeSource)
return nullptr;
return new EntryImageProvider(spec->makeSource, spec->scheme);
}
QStringList FormatRegistry::supportedExtensions() const
{
QStringList out;
@@ -49,8 +54,9 @@ QStringList FormatRegistry::supportedExtensions() const
void FormatRegistry::registerImageProviders(QQmlEngine &engine) const
{
for (const FormatSpec &spec : m_specs)
for (const FormatSpec &spec : m_specs) {
if (!spec.scheme.isEmpty() && spec.makeSource)
engine.addImageProvider(spec.scheme,
new EntryImageProvider(spec.makeSource, spec.scheme));
}
}
+8 -8
View File
@@ -8,18 +8,23 @@
class FormatReader;
class IByteSource;
class IImageProvider;
class QQmlEngine;
// One row per supported format. Everything a format needs — reader, image
// byte source, provider scheme — is declared in one place; adding a format
// means adding one row to the table built in main().
struct FormatSpec {
using ReaderFactory = std::function<std::unique_ptr<FormatReader>()>;
using SourceFactory = std::function<std::unique_ptr<IByteSource>()>;
QString extension; // lowercase, without dot; unused when matchesDirectory
bool matchesDirectory = false;
QString scheme; // image:// scheme; empty = format has no images
std::function<std::unique_ptr<FormatReader>()> makeReader;
std::function<std::unique_ptr<IByteSource>()> makeSource; // required when scheme is set
ReaderFactory makeReader;
SourceFactory makeSource; // required when scheme is set
static FormatSpec comic(const QString &ext, bool matchesDirectory, const QString &scheme,
SourceFactory makeSource);
};
class FormatRegistry {
@@ -27,11 +32,6 @@ public:
explicit FormatRegistry(QVector<FormatSpec> specs);
std::unique_ptr<FormatReader> createReader(const QString &filePath) const;
// New provider for the format, or nullptr for formats without images.
// Caller owns the pointer (the QML engine takes ownership on registration).
IImageProvider *createImageProvider(const QString &filePath) const;
QStringList supportedExtensions() const;
// Register one provider per scheme with the QML engine (engine takes ownership).
+13
View File
@@ -1,6 +1,8 @@
#pragma once
#include <QByteArray>
#include <QCryptographicHash>
#include <QFile>
#include <QString>
#include <QStringList>
@@ -22,4 +24,15 @@ public:
// MD5 of the source. For archives: MD5 of file bytes.
// For directories: MD5 of sorted "name:size\n" lines.
virtual QByteArray contentHash() const = 0;
// Shared MD5 calculation for files on disk.
static QByteArray hashFileMd5(const QString &filePath)
{
if (filePath.isEmpty())
return {};
QFile f(filePath);
if (f.open(QIODevice::ReadOnly))
return QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
return {};
}
};
+37 -9
View File
@@ -1,29 +1,57 @@
#include "ImageUrl.h"
#include <QDir>
#include <QLatin1Char>
#include <QLatin1String>
#include <QUrl>
namespace {
const QLatin1String kSrcKey("src=");
QString querySuffix(const QString &sourcePath)
{
return QStringLiteral("?src=")
+ QString::fromUtf8(QUrl::toPercentEncoding(sourcePath));
}
}
namespace ImageUrl {
QString build(const QString &scheme, const QString &entry)
QString querySuffix(const QString &sourcePath)
{
return QStringLiteral("image://") + scheme + QLatin1Char('/') + entry;
if (sourcePath.isEmpty())
return {};
return QStringLiteral("?src=")
+ QString::fromUtf8(QUrl::toPercentEncoding(sourcePath));
}
QString build(const QString &scheme, const QString &entry, const QString &sourcePath)
{
return build(scheme, entry) + querySuffix(sourcePath);
QString url = QStringLiteral("image://") + scheme + QLatin1Char('/') + entry;
if (!sourcePath.isEmpty())
url += querySuffix(sourcePath);
return url;
}
QString resolve(const QString &scheme,
const QString &href,
const QString &sourcePath,
const QString &baseDir)
{
if (href.isEmpty())
return {};
if (href.startsWith(QLatin1String("http://")) || href.startsWith(QLatin1String("https://")))
return QStringLiteral("image://web/") + href;
if (href.startsWith(QLatin1String("image://")))
return href;
QString entry = href;
if (entry.startsWith(QLatin1Char('#')))
entry.remove(0, 1);
if (!baseDir.isEmpty())
entry = QDir::cleanPath(baseDir + entry);
while (entry.startsWith(QLatin1Char('/')))
entry.remove(0, 1);
return build(scheme, entry, sourcePath);
}
Parsed parseProviderId(const QString &id)
+12 -4
View File
@@ -5,15 +5,23 @@
// Single authority for the image-provider URL grammar:
// image://<scheme>/<entry>[?src=<percent-encoded-source-path>]
// The optional src query names the container file (archive, document) the
// entry lives in; when absent, providers fall back to their current file.
// entry lives in.
namespace ImageUrl {
QString build(const QString &scheme, const QString &entry);
QString build(const QString &scheme, const QString &entry, const QString &sourcePath);
QString querySuffix(const QString &sourcePath);
QString build(const QString &scheme, const QString &entry, const QString &sourcePath = {});
// Resolves an image reference from a book (relative path, #fragment, or http/https URL):
// - "http://..." or "https://..." -> "image://web/http..."
// - embedded entry (resolved against optional baseDir for EPUB XHTML) -> "image://<scheme>/<entry>?src=<sourcePath>"
QString resolve(const QString &scheme,
const QString &href,
const QString &sourcePath,
const QString &baseDir = {});
struct Parsed {
QString entry;
QString sourcePath; // empty = use the provider's current file
QString sourcePath; // empty if no source parameter was present
};
// Parse the id handed to QQuickAsyncImageProvider (scheme already stripped,
-12
View File
@@ -1,12 +0,0 @@
#pragma once
#include "ComicReader.h"
#include "backend/formats/sources/RarByteSource.h"
class CbrReader : public ComicReader {
public:
CbrReader()
: ComicReader([] { return std::make_unique<RarByteSource>(); },
QStringLiteral("cbr"),
QStringLiteral("cbr")) {}
};
-12
View File
@@ -1,12 +0,0 @@
#pragma once
#include "ComicReader.h"
#include "backend/formats/sources/ZipByteSource.h"
class CbzReader : public ComicReader {
public:
CbzReader()
: ComicReader([] { return std::make_unique<ZipByteSource>(); },
QStringLiteral("cbz"),
QStringLiteral("cbz")) {}
};
+33 -37
View File
@@ -57,25 +57,13 @@ static void parseComicInfo(const QByteArray &data,
// ComicReader
// ---------------------------------------------------------------------------
ComicReader::ComicReader(SourceFactory sourceFactory, QString fileType, QString scheme)
ComicReader::ComicReader(SourceFactory sourceFactory, 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, IByteSource &source)
bool ComicReader::parseOpenSource(const QString &filePath, IByteSource &source, bool metadataOnly)
{
const QStringList allEntries = source.listEntries();
@@ -92,25 +80,27 @@ bool ComicReader::parseOpenSource(const QString &filePath, IByteSource &source)
}
imageEntries.sort();
if (imageEntries.isEmpty()) {
m_error = QStringLiteral("No images found: ") + filePath;
return false;
}
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();
if (!metadataOnly) {
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
@@ -150,7 +140,7 @@ bool ComicReader::open(const QString &filePath)
return false;
}
m_contentHash = source->contentHash();
return parseOpenSource(filePath, *source);
return parseOpenSource(filePath, *source, false);
}
bool ComicReader::loadMetadata(const QString &filePath)
@@ -160,8 +150,22 @@ bool ComicReader::loadMetadata(const QString &filePath)
m_error.clear();
m_pageEntries.clear();
m_coverEntry.clear();
m_contentHash.clear();
m_filePath.clear();
return parseSource(filePath);
auto source = m_sourceFactory();
if (!source->open(filePath)) {
m_error = QStringLiteral("Cannot open comic source: ") + filePath;
return false;
}
return parseOpenSource(filePath, *source, true);
}
QString ComicReader::coverImageUrl() const
{
if (m_coverEntry.isEmpty())
return {};
return ImageUrl::build(m_scheme, m_coverEntry, m_filePath);
}
QVector<PageInfo> ComicReader::pages() const
@@ -169,15 +173,7 @@ QVector<PageInfo> ComicReader::pages() const
QVector<PageInfo> result;
result.reserve(m_pageEntries.size());
for (const PageEntry &page : m_pageEntries) {
result.append({ImageUrl::build(m_scheme, page.entry), page.naturalSize});
result.append({ImageUrl::build(m_scheme, page.entry, m_filePath), page.naturalSize});
}
return result;
}
QString ComicReader::lastError() { return m_error; }
QString ComicReader::title() { return m_title; }
QString ComicReader::author() { return m_author; }
QString ComicReader::coverImagePath() { return m_coverEntry; }
QString ComicReader::filePath() { return m_filePath; }
QByteArray ComicReader::contentHash() { return m_contentHash; }
QString ComicReader::fileType() const { return m_fileType; }
+9 -11
View File
@@ -14,17 +14,17 @@ class ComicReader : public FormatReader {
public:
using SourceFactory = std::function<std::unique_ptr<IByteSource>()>;
ComicReader(SourceFactory sourceFactory, QString fileType, QString scheme);
ComicReader(SourceFactory sourceFactory, QString scheme);
ReadingMode mode() const override { return ReadingMode::Comic; }
bool open(const QString &filePath) override;
bool loadMetadata(const QString &filePath) override;
QString lastError() override;
QString filePath() override;
QString title() override;
QString author() override;
QString coverImagePath() override;
QByteArray contentHash() override;
QString fileType() const override;
QString lastError() const override { return m_error; }
QString filePath() const override { return m_filePath; }
QString title() const override { return m_title; }
QString author() const override { return m_author; }
QString coverImageUrl() const override;
QByteArray contentHash() const override { return m_contentHash; }
QVector<PageInfo> pages() const override;
@@ -34,11 +34,9 @@ private:
QSize naturalSize;
};
bool parseSource(const QString &filePath);
bool parseOpenSource(const QString &filePath, IByteSource &source);
bool parseOpenSource(const QString &filePath, IByteSource &source, bool metadataOnly = false);
SourceFactory m_sourceFactory;
QString m_fileType;
QString m_scheme;
QString m_filePath;
-12
View File
@@ -1,12 +0,0 @@
#pragma once
#include "ComicReader.h"
#include "backend/formats/sources/DirByteSource.h"
class FolderReader : public ComicReader {
public:
FolderReader()
: ComicReader([] { return std::make_unique<DirByteSource>(); },
QStringLiteral("folder"),
QStringLiteral("dir")) {}
};
+2 -10
View File
@@ -1,8 +1,5 @@
#include "RarByteSource.h"
#include <QCryptographicHash>
#include <QFile>
#include <archive.h>
#include <archive_entry.h>
@@ -75,12 +72,7 @@ QByteArray RarByteSource::readEntry(const QString &entryName) const
QByteArray RarByteSource::contentHash() const
{
// Computed lazily: hashing reads the whole file, which providers opening
// a source per image request must not pay for.
if (m_hash.isEmpty() && !m_path.isEmpty()) {
QFile f(m_path);
if (f.open(QIODevice::ReadOnly))
m_hash = QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
}
if (m_hash.isEmpty())
m_hash = IByteSource::hashFileMd5(m_path);
return m_hash;
}
+43 -14
View File
@@ -1,11 +1,45 @@
#include "ZipByteSource.h"
#include "backend/ZipUtils.h"
#include <QCryptographicHash>
#include <QFile>
#include <minizip/unzip.h>
#include <memory>
namespace {
QByteArray readZipEntry(unzFile zf, const char *entryName)
{
if (unzLocateFile(zf, entryName, 0) != UNZ_OK)
return {};
if (unzOpenCurrentFile(zf) != UNZ_OK)
return {};
QByteArray result;
char buf[4096];
int bytesRead;
while ((bytesRead = unzReadCurrentFile(zf, buf, sizeof(buf))) > 0)
result.append(buf, bytesRead);
unzCloseCurrentFile(zf);
return (bytesRead == 0) ? result : QByteArray{};
}
QStringList listZipEntries(unzFile zf)
{
QStringList entries;
if (unzGoToFirstFile(zf) != UNZ_OK)
return entries;
do {
char name[4096];
unz_file_info64 info{};
if (unzGetCurrentFileInfo64(zf, &info, name, static_cast<uLong>(sizeof(name) - 1),
nullptr, 0, nullptr, 0) == UNZ_OK) {
name[sizeof(name) - 1] = '\0';
entries.append(QString::fromUtf8(name));
}
} while (unzGoToNextFile(zf) == UNZ_OK);
return entries;
}
} // namespace
bool ZipByteSource::open(const QString &path)
{
@@ -16,7 +50,7 @@ bool ZipByteSource::open(const QString &path)
unzFile zf = unzOpen(path.toUtf8().constData());
if (!zf)
return false;
m_entries = ZipUtils::listZipEntries(zf);
m_entries = listZipEntries(zf);
unzClose(zf);
m_path = path;
@@ -37,17 +71,12 @@ QByteArray ZipByteSource::readEntry(const QString &entryName) const
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());
return readZipEntry(zf, entryName.toUtf8().constData());
}
QByteArray ZipByteSource::contentHash() const
{
// Computed lazily: hashing reads the whole file, which providers opening
// a source per image request must not pay for.
if (m_hash.isEmpty() && !m_path.isEmpty()) {
QFile f(m_path);
if (f.open(QIODevice::ReadOnly))
m_hash = QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
}
if (m_hash.isEmpty())
m_hash = IByteSource::hashFileMd5(m_path);
return m_hash;
}