Add back history view cover photos
This commit is contained in:
11 files changed
+476
-28
No files matched your search
@@ -79,6 +79,8 @@ set(SOURCES
|
||||
src/backend/formats/ImageLoaderFactory.cpp
|
||||
src/backend/formats/ReaderFactory.h
|
||||
src/backend/formats/ReaderFactory.cpp
|
||||
src/backend/formats/FileIconProvider.h
|
||||
src/backend/formats/FileIconProvider.cpp
|
||||
)
|
||||
|
||||
|
||||
@@ -292,3 +294,19 @@ add_asteria_test(EpubImageProviderTest tests/EpubImageProviderTest.cpp
|
||||
target_link_libraries(EpubImageProviderTest PRIVATE PkgConfig::MINIZIP Qt6::Quick Qt6::Gui)
|
||||
set_tests_properties(EpubImageProviderTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||
|
||||
add_asteria_test(HistoryBookModelTest tests/HistoryBookModelTest.cpp
|
||||
src/backend/HistoryBookModel.cpp
|
||||
src/backend/formats/EpubImageProvider.cpp
|
||||
src/backend/formats/ImageProviderFactory.cpp
|
||||
src/backend/formats/ReaderFactory.cpp
|
||||
src/backend/formats/TxtReader.cpp
|
||||
src/backend/formats/EpubReader.cpp)
|
||||
target_link_libraries(HistoryBookModelTest PRIVATE
|
||||
Qt6::Concurrent Qt6::Quick Qt6::Gui Qt6::Xml PkgConfig::MINIZIP)
|
||||
set_tests_properties(HistoryBookModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||
|
||||
add_asteria_test(FileIconProviderTest tests/FileIconProviderTest.cpp
|
||||
src/backend/formats/FileIconProvider.cpp)
|
||||
target_link_libraries(FileIconProviderTest PRIVATE Qt6::Quick Qt6::Gui Qt6::Widgets)
|
||||
set_tests_properties(FileIconProviderTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include "HistoryBookModel.h"
|
||||
#include "FormatReader.h"
|
||||
#include "IImageProvider.h"
|
||||
#include "formats/ImageProviderFactory.h"
|
||||
#include "formats/ReaderFactory.h"
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
#include <QUrl>
|
||||
|
||||
HistoryBookModel::HistoryBookModel(ReaderFactory &factory, QObject *parent)
|
||||
: QObject(parent)
|
||||
@@ -93,11 +96,24 @@ void HistoryBookModel::loadMetadata()
|
||||
success = true;
|
||||
}
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, title, author, success]() {
|
||||
QString coverImageUrl;
|
||||
if (success) {
|
||||
QString coverPath = reader->coverImagePath();
|
||||
if (!coverPath.isEmpty()) {
|
||||
std::unique_ptr<IImageProvider> provider(ImageProviderFactory::create(filePath));
|
||||
if (provider) {
|
||||
provider->setFilePath(filePath);
|
||||
coverImageUrl = provider->coverImageUrl(coverPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, title, author, coverImageUrl, success]() {
|
||||
if (!self || !success)
|
||||
return;
|
||||
self->m_title = title;
|
||||
self->m_author = author;
|
||||
self->m_coverImageUrl = coverImageUrl;
|
||||
self->m_metadataLoaded = true;
|
||||
emit self->metadataLoaded();
|
||||
}, Qt::QueuedConnection);
|
||||
@@ -106,20 +122,4 @@ void HistoryBookModel::loadMetadata()
|
||||
|
||||
void HistoryBookModel::onMetadataLoaded()
|
||||
{
|
||||
// This slot can be used for any post-processing after metadata is loaded
|
||||
}
|
||||
|
||||
void HistoryBookModel::loadCoverImage(const QString &epubBasePath)
|
||||
{
|
||||
if (!m_coverImageUrl.isEmpty() || m_filePath.isEmpty())
|
||||
return;
|
||||
|
||||
auto reader = m_factory.create(m_filePath);
|
||||
if (reader && reader->loadMetadata(m_filePath)) {
|
||||
QString coverPath = reader->coverImagePath();
|
||||
if (!coverPath.isEmpty()) {
|
||||
m_coverImageUrl = QStringLiteral("image://epub/") + coverPath;
|
||||
emit coverImageLoaded();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,6 @@ public:
|
||||
QString coverImageUrl() const;
|
||||
|
||||
void loadMetadata();
|
||||
void loadCoverImage(const QString &epubBasePath);
|
||||
|
||||
public slots:
|
||||
void onMetadataLoaded();
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <QMutexLocker>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
@@ -64,7 +65,20 @@ QQuickImageResponse *EpubImageProvider::requestImageResponse(const QString &id,
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
auto *response = new EpubImageResponse(epubPath, id, displayWidth, requestedSize);
|
||||
// id may be "internal/path.jpg" (old format) or
|
||||
// "internal/path.jpg?epub=<percent-encoded-epub-path>" (new embedded format).
|
||||
QString internalPath = id;
|
||||
const int qmark = id.indexOf(QLatin1Char('?'));
|
||||
if (qmark >= 0) {
|
||||
internalPath = id.left(qmark);
|
||||
const QString query = id.mid(qmark + 1);
|
||||
// Parse epub=<value> from query string
|
||||
const QLatin1String key("epub=");
|
||||
if (query.startsWith(key))
|
||||
epubPath = QUrl::fromPercentEncoding(query.mid(key.size()).toUtf8());
|
||||
}
|
||||
|
||||
auto *response = new EpubImageResponse(epubPath, internalPath, displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
@@ -119,16 +133,26 @@ QImage EpubImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
if (url.scheme() != QLatin1String("image"))
|
||||
return {};
|
||||
|
||||
QString zipPath = url.path();
|
||||
if (zipPath.startsWith(QLatin1Char('/')))
|
||||
zipPath = zipPath.mid(1);
|
||||
if (zipPath.isEmpty())
|
||||
return {};
|
||||
|
||||
QString epubPath;
|
||||
{
|
||||
if (url.hasQuery()) {
|
||||
// Embedded format: epub path is in the query string as epub=<encoded>
|
||||
const QString q = url.query();
|
||||
const QLatin1String key("epub=");
|
||||
if (q.startsWith(key))
|
||||
epubPath = QUrl::fromPercentEncoding(q.mid(key.size()).toUtf8());
|
||||
}
|
||||
if (epubPath.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
epubPath = m_epubPath;
|
||||
}
|
||||
|
||||
return loadImageFromEpub(epubPath, zipPath, maxWidth, {});
|
||||
}
|
||||
|
||||
@@ -152,7 +176,11 @@ void EpubImageProvider::setFilePath(const QString &filePath)
|
||||
|
||||
QString EpubImageProvider::coverImageUrl(const QString &coverPath) const
|
||||
{
|
||||
return QStringLiteral("image://epub/") + coverPath;
|
||||
QString epubPath;
|
||||
{ QMutexLocker lock(&m_mutex); epubPath = m_epubPath; }
|
||||
return QStringLiteral("image://epub/") + coverPath
|
||||
+ QStringLiteral("?epub=")
|
||||
+ QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
}
|
||||
|
||||
QString EpubImageProvider::scheme() const
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "FileIconProvider.h"
|
||||
#include <QFileIconProvider>
|
||||
#include <QFileInfo>
|
||||
#include <QIcon>
|
||||
|
||||
FileIconProvider::FileIconProvider()
|
||||
: QQuickImageProvider(QQuickImageProvider::Image)
|
||||
{}
|
||||
|
||||
QImage FileIconProvider::requestImage(const QString &id, QSize *size,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
// QML strips the leading slash from the URL path component, so restore it on Unix.
|
||||
QString filePath = id.startsWith(QLatin1Char('/')) ? id : QLatin1Char('/') + id;
|
||||
|
||||
QFileIconProvider iconProvider;
|
||||
QIcon icon = iconProvider.icon(QFileInfo(filePath));
|
||||
if (icon.isNull())
|
||||
icon = iconProvider.icon(QFileIconProvider::File);
|
||||
|
||||
QSize target = (requestedSize.width() > 0 && requestedSize.height() > 0)
|
||||
? requestedSize : QSize(60, 80);
|
||||
|
||||
QImage image = icon.pixmap(target).toImage();
|
||||
if (size)
|
||||
*size = image.size();
|
||||
return image;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <QQuickImageProvider>
|
||||
|
||||
// Serves OS file-type icons under image://fileicon/<absolute-file-path>
|
||||
class FileIconProvider : public QQuickImageProvider {
|
||||
public:
|
||||
FileIconProvider();
|
||||
QImage requestImage(const QString &id, QSize *size,
|
||||
const QSize &requestedSize) override;
|
||||
};
|
||||
@@ -13,6 +13,8 @@
|
||||
#include "backend/IImageProvider.h"
|
||||
#include "backend/formats/ImageLoaderFactory.h"
|
||||
#include "backend/formats/ImageProviderFactory.h"
|
||||
#include "backend/formats/EpubImageProvider.h"
|
||||
#include "backend/formats/FileIconProvider.h"
|
||||
#include "backend/formats/WebImageProvider.h"
|
||||
#include "backend/HistoryModel.h"
|
||||
#include "backend/ReadingModel.h"
|
||||
@@ -137,6 +139,8 @@ int main(int argc, char *argv[])
|
||||
// --- QML engine ---
|
||||
QQmlApplicationEngine engine;
|
||||
engine.addImageProvider(QStringLiteral("web"), new WebImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("epub"), new EpubImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("fileicon"), new FileIconProvider());
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
||||
#ifdef QT_DEBUG
|
||||
true
|
||||
|
||||
@@ -38,19 +38,32 @@ Rectangle {
|
||||
anchors.bottomMargin: 12
|
||||
spacing: 8
|
||||
|
||||
Image {
|
||||
id: coverImage
|
||||
Item {
|
||||
id: coverSlot
|
||||
visible: filePath !== ""
|
||||
width: 60
|
||||
height: 80
|
||||
source: coverImageUrl
|
||||
fillMode: Image.PreserveAspectFit
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
|
||||
Image {
|
||||
anchors.fill: parent
|
||||
source: filePath !== "" ? ("image://fileicon/" + filePath) : ""
|
||||
fillMode: Image.PreserveAspectFit
|
||||
visible: coverImage.status !== Image.Ready
|
||||
}
|
||||
|
||||
Image {
|
||||
id: coverImage
|
||||
anchors.fill: parent
|
||||
source: coverImageUrl
|
||||
fillMode: Image.PreserveAspectFit
|
||||
visible: status === Image.Ready
|
||||
}
|
||||
}
|
||||
|
||||
Column {
|
||||
id: bookInfoCol
|
||||
width: parent.width - removeBtn.width - parent.spacing - (coverImage.visible ? coverImage.width + 8 : 0)
|
||||
width: parent.width - removeBtn.width - parent.spacing - (coverSlot.visible ? coverSlot.width + parent.spacing : 0)
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
spacing: 3
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <QSize>
|
||||
#include <QTemporaryFile>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
#include <minizip/zip.h>
|
||||
|
||||
@@ -43,7 +44,8 @@ static QByteArray makePng(int w, int h, QRgb color = qRgb(255, 0, 0))
|
||||
|
||||
// Build a minimal EPUB-like ZIP containing a single image entry.
|
||||
// Returns the path of the temp file (caller removes).
|
||||
static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH)
|
||||
static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH,
|
||||
QRgb color = qRgb(255, 0, 0))
|
||||
{
|
||||
QTemporaryFile tmp;
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/provider_test_XXXXXX.epub");
|
||||
@@ -55,7 +57,7 @@ static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH)
|
||||
zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE);
|
||||
if (!zf) return {};
|
||||
|
||||
const bool ok = addZipEntry(zf, entryPath, makePng(imgW, imgH));
|
||||
const bool ok = addZipEntry(zf, entryPath, makePng(imgW, imgH, color));
|
||||
zipClose(zf, nullptr);
|
||||
return ok ? path : QString{};
|
||||
}
|
||||
@@ -211,6 +213,106 @@ private slots:
|
||||
QVERIFY(response.textureFactory() == nullptr);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Embedded-path URL format tests
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
void coverImageUrlEmbedsEpubPath()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
provider.setFilePath(epubPath);
|
||||
|
||||
const QString url = provider.coverImageUrl(QStringLiteral("OEBPS/cover.jpg"));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
// New format: image://epub/<internal-path>?epub=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/cover.jpg?epub=")),
|
||||
qPrintable("Expected query-param URL, got: " + url));
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
QVERIFY2(url.contains(encodedPath),
|
||||
qPrintable("URL should contain encoded epub path. URL: " + url));
|
||||
}
|
||||
|
||||
void requestImageResponseUsesEmbeddedPath()
|
||||
{
|
||||
// Build two EPUBs with different color images; prove the embedded path in the URL
|
||||
// overrides m_epubPath on the provider.
|
||||
const QString epubA = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
const QString epubB = buildEpubWithPng("img.png", 4, 4, qRgb(0, 255, 0));
|
||||
QVERIFY(!epubA.isEmpty() && !epubB.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
provider.setFilePath(epubA); // configured for A
|
||||
|
||||
// Build an embedded-format id pointing to B via query parameter
|
||||
const QString encodedB = QString::fromUtf8(QUrl::toPercentEncoding(epubB));
|
||||
const QString id = QStringLiteral("img.png?epub=") + encodedB;
|
||||
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(id, QSize());
|
||||
QSignalSpy spy(resp, &QQuickImageResponse::finished);
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
if (spy.isEmpty()) spy.wait(5000);
|
||||
|
||||
QFile::remove(epubA);
|
||||
QFile::remove(epubB);
|
||||
|
||||
QVERIFY(resp->textureFactory() != nullptr);
|
||||
// The loaded image should be green (from B), not red (from A)
|
||||
const QImage img = resp->textureFactory()->image();
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.pixel(0, 0), qRgb(0, 255, 0));
|
||||
|
||||
delete resp;
|
||||
}
|
||||
|
||||
void loadMethodHandlesEmbeddedUrlFormat()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
// m_epubPath is intentionally NOT set — embedded URL must supply it via query
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
const QUrl url(QStringLiteral("image://epub/img.png?epub=") + encodedPath);
|
||||
|
||||
const QImage img = provider.load(url, 0);
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.pixel(0, 0), qRgb(255, 0, 0));
|
||||
}
|
||||
|
||||
void requestImageResponseFallsBackToProviderPath()
|
||||
{
|
||||
// Without a leading slash in id, the provider should use m_epubPath.
|
||||
const QString epubA = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
QVERIFY(!epubA.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
provider.setFilePath(epubA);
|
||||
|
||||
// No query param — old format, uses m_epubPath
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(
|
||||
QStringLiteral("img.png"), QSize());
|
||||
QSignalSpy spy(resp, &QQuickImageResponse::finished);
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
if (spy.isEmpty()) spy.wait(5000);
|
||||
|
||||
QFile::remove(epubA);
|
||||
|
||||
QVERIFY(resp->textureFactory() != nullptr);
|
||||
const QImage img = resp->textureFactory()->image();
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.pixel(0, 0), qRgb(255, 0, 0));
|
||||
|
||||
delete resp;
|
||||
}
|
||||
|
||||
void responseAfterClearIsNull()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#include <QtTest/QtTest>
|
||||
#include <QApplication>
|
||||
#include <QImage>
|
||||
#include <QSize>
|
||||
|
||||
#include "backend/formats/FileIconProvider.h"
|
||||
|
||||
class FileIconProviderTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void requestImageReturnsValidImageForExistingFile()
|
||||
{
|
||||
FileIconProvider provider;
|
||||
QSize size;
|
||||
// Use a known fixture file
|
||||
const QString path = QStringLiteral(FIXTURES_DIR) + "/sample.txt";
|
||||
QImage image = provider.requestImage(path, &size, QSize());
|
||||
|
||||
QVERIFY(!image.isNull());
|
||||
QVERIFY(size.width() > 0);
|
||||
QVERIFY(size.height() > 0);
|
||||
}
|
||||
|
||||
void requestImageReturnsValidImageForNonexistentFile()
|
||||
{
|
||||
FileIconProvider provider;
|
||||
QSize size;
|
||||
// QFileIconProvider returns a generic icon even for missing files
|
||||
QImage image = provider.requestImage(
|
||||
QStringLiteral("/tmp/nonexistent_asteria_test.epub"), &size, QSize());
|
||||
|
||||
QVERIFY(!image.isNull());
|
||||
}
|
||||
|
||||
void requestImageHonorsRequestedSize()
|
||||
{
|
||||
FileIconProvider provider;
|
||||
QSize size;
|
||||
const QSize requested(32, 32);
|
||||
const QString path = QStringLiteral(FIXTURES_DIR) + "/sample.txt";
|
||||
QImage image = provider.requestImage(path, &size, requested);
|
||||
|
||||
QVERIFY(!image.isNull());
|
||||
// Icon pixmaps may be slightly different sizes depending on the platform,
|
||||
// but should be non-empty and at most the requested size.
|
||||
QVERIFY(image.width() > 0);
|
||||
QVERIFY(image.height() > 0);
|
||||
}
|
||||
};
|
||||
|
||||
// QFileIconProvider requires QApplication (not just QCoreApplication)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
FileIconProviderTest test;
|
||||
return QTest::qExec(&test, argc, argv);
|
||||
}
|
||||
|
||||
#include "FileIconProviderTest.moc"
|
||||
@@ -0,0 +1,186 @@
|
||||
#include <QtTest/QtTest>
|
||||
#include <QBuffer>
|
||||
#include <QDir>
|
||||
#include <QImage>
|
||||
#include <QSignalSpy>
|
||||
#include <QTemporaryFile>
|
||||
#include <QUrl>
|
||||
|
||||
#include <minizip/zip.h>
|
||||
|
||||
#include "backend/HistoryBookModel.h"
|
||||
#include "backend/FormatReader.h"
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/formats/TxtReader.h"
|
||||
#include "backend/formats/ReaderFactory.h"
|
||||
|
||||
static ReaderFactory makeFactory()
|
||||
{
|
||||
return ReaderFactory({
|
||||
[] { return std::make_unique<TxtReader>(); },
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static bool addEntry(zipFile zf, const char *name, const QByteArray &data, bool store = false)
|
||||
{
|
||||
zip_fileinfo fi{};
|
||||
int method = store ? Z_NO_COMPRESSION : Z_DEFLATED;
|
||||
int level = store ? 0 : Z_DEFAULT_COMPRESSION;
|
||||
if (zipOpenNewFileInZip(zf, name, &fi,
|
||||
nullptr, 0, nullptr, 0, nullptr,
|
||||
method, level) != ZIP_OK)
|
||||
return false;
|
||||
if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) {
|
||||
zipCloseFileInZip(zf);
|
||||
return false;
|
||||
}
|
||||
return zipCloseFileInZip(zf) == ZIP_OK;
|
||||
}
|
||||
|
||||
static QByteArray makePng(int w = 4, int h = 4, QRgb color = qRgb(255, 0, 0))
|
||||
{
|
||||
QImage img(w, h, QImage::Format_RGB32);
|
||||
img.fill(color);
|
||||
QByteArray data;
|
||||
QBuffer buf(&data);
|
||||
buf.open(QIODevice::WriteOnly);
|
||||
img.save(&buf, "PNG");
|
||||
return data;
|
||||
}
|
||||
|
||||
// Build a minimal EPUB with a cover image declared in OPF metadata.
|
||||
static QString buildEpubWithCover()
|
||||
{
|
||||
static const char kMimetype[] = "application/epub+zip";
|
||||
|
||||
static const char kContainer[] =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">"
|
||||
" <rootfiles>"
|
||||
" <rootfile full-path=\"OEBPS/content.opf\""
|
||||
" media-type=\"application/oebps-package+xml\"/>"
|
||||
" </rootfiles>"
|
||||
"</container>";
|
||||
|
||||
// OPF with <meta name="cover" content="cover-img"/> referencing the cover item.
|
||||
static const char kOpf[] =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\" unique-identifier=\"BookId\">"
|
||||
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
|
||||
" <dc:title>Cover Test Book</dc:title>"
|
||||
" <dc:creator>Test Author</dc:creator>"
|
||||
" <meta name=\"cover\" content=\"cover-img\"/>"
|
||||
" </metadata>"
|
||||
" <manifest>"
|
||||
" <item id=\"cover-img\" href=\"Images/cover.png\" media-type=\"image/png\"/>"
|
||||
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
|
||||
" </manifest>"
|
||||
" <spine>"
|
||||
" <itemref idref=\"ch1\"/>"
|
||||
" </spine>"
|
||||
"</package>";
|
||||
|
||||
static const char kChapter[] =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
|
||||
" <body><p>Content</p></body>"
|
||||
"</html>";
|
||||
|
||||
QTemporaryFile tmp;
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/hbm_cover_test_XXXXXX.epub");
|
||||
tmp.setAutoRemove(false);
|
||||
if (!tmp.open()) return {};
|
||||
const QString path = tmp.fileName();
|
||||
tmp.close();
|
||||
|
||||
zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE);
|
||||
if (!zf) return {};
|
||||
|
||||
bool ok = true;
|
||||
ok = ok && addEntry(zf, "mimetype", QByteArray(kMimetype), /*store=*/true);
|
||||
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
|
||||
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(kOpf));
|
||||
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter));
|
||||
ok = ok && addEntry(zf, "OEBPS/Images/cover.png", makePng());
|
||||
|
||||
zipClose(zf, nullptr);
|
||||
return ok ? path : QString{};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Test class
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class HistoryBookModelTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void loadMetadataPopulatesCoverImageUrlForEpub()
|
||||
{
|
||||
const QString epubPath = buildEpubWithCover();
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
ReaderFactory factory = makeFactory();
|
||||
HistoryBookModel model(epubPath, 0.0, QString(), factory);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
model.loadMetadata();
|
||||
|
||||
// Wait up to 5 seconds for the async metadata load to complete
|
||||
QVERIFY(spy.wait(5000));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
const QString url = model.coverImageUrl();
|
||||
QVERIFY2(!url.isEmpty(), "coverImageUrl should be set after loadMetadata for an EPUB with cover");
|
||||
// Format: image://epub/<internal-path>?epub=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/Images/cover.png?epub=")),
|
||||
qPrintable("Expected query-param URL format, got: " + url));
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
QVERIFY2(url.contains(encodedPath),
|
||||
qPrintable("URL should contain encoded epub path. URL: " + url));
|
||||
}
|
||||
|
||||
void loadMetadataLeavesCoverImageUrlEmptyForTxt()
|
||||
{
|
||||
const QString txtPath = QStringLiteral(FIXTURES_DIR) + "/sample.txt";
|
||||
QVERIFY(QFile::exists(txtPath));
|
||||
|
||||
ReaderFactory factory = makeFactory();
|
||||
HistoryBookModel model(txtPath, 0.0, QString(), factory);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
model.loadMetadata();
|
||||
|
||||
QVERIFY(spy.wait(5000));
|
||||
|
||||
QVERIFY2(model.coverImageUrl().isEmpty(),
|
||||
"TXT files have no cover; coverImageUrl should remain empty");
|
||||
}
|
||||
|
||||
void loadMetadataPopulatesTitle()
|
||||
{
|
||||
const QString epubPath = buildEpubWithCover();
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
ReaderFactory factory = makeFactory();
|
||||
HistoryBookModel model(epubPath, 0.0, QString(), factory);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
model.loadMetadata();
|
||||
QVERIFY(spy.wait(5000));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QCOMPARE(model.title(), QStringLiteral("Cover Test Book"));
|
||||
QCOMPARE(model.author(), QStringLiteral("Test Author"));
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(HistoryBookModelTest)
|
||||
#include "HistoryBookModelTest.moc"
|
||||
Reference in new issue
Block a user