Manga (CBZ) Support
This commit is contained in:
12 files changed
+937
-17
No files matched your search
@@ -10,25 +10,26 @@ Asteria Qt is a cross-platform desktop book/document reader built with Qt6/C++.
|
|||||||
|
|
||||||
### Format Support
|
### Format Support
|
||||||
|
|
||||||
Initial implementation: **TXT, FB2, EPUB**
|
Initial implementation: **TXT, FB2, EPUB, CBZ**
|
||||||
|
|
||||||
Full target format list (derived from actual library inventory):
|
Full target format list (derived from actual library inventory):
|
||||||
|
|
||||||
| Format | Count | Priority | Approach |
|
| Format | Count | Priority | Approach |
|
||||||
|---|---|---|---|
|
|---|-------|---|---|
|
||||||
| EPUB | ~2500 | initial | Unzip (QuaZip/minizip) + parse OPF spine + render HTML |
|
| EPUB | ~2500 | initial | Unzip (QuaZip/minizip) + parse OPF spine + render HTML |
|
||||||
| FB2 | ~1900 | initial | `QXmlStreamReader` (pure XML) |
|
| FB2 | ~1900 | initial | `QXmlStreamReader` (pure XML) |
|
||||||
| TXT | ~170 | initial | `QTextStream` + encoding detection (uchardet/ICU) |
|
| TXT | ~170 | initial | `QTextStream` + encoding detection (uchardet/ICU) |
|
||||||
| PDF | ~520 | high | `QtPdf` module (`QPdfDocument`, Qt 6.4+) |
|
| CBZ | ??? | initial | Unzip (minizip) + parse optional ComicInfo.xml + one image per chunk |
|
||||||
| DOC | ~330 | high | `libwpd` or antiword subprocess |
|
| PDF | ~520 | high | `QtPdf` module (`QPdfDocument`, Qt 6.4+) |
|
||||||
| RTF | ~260 | high | `QTextDocumentReader` or RTF→HTML |
|
| DOC | ~330 | high | `libwpd` or antiword subprocess |
|
||||||
| HTML/HTM | ~350 | high | `QTextDocument` / `QWebEngineView` |
|
| RTF | ~260 | high | `QTextDocumentReader` or RTF→HTML |
|
||||||
| DOCX | ~65 | medium | Unzip + parse `word/document.xml` (`QXmlStreamReader`) |
|
| HTML/HTM | ~350 | high | `QTextDocument` / `QWebEngineView` |
|
||||||
| DJVU | ~60 | medium | `DjVuLibre` |
|
| DOCX | ~65 | medium | Unzip + parse `word/document.xml` (`QXmlStreamReader`) |
|
||||||
| ODT | ~5 | medium | Unzip + parse OpenDocument XML |
|
| DJVU | ~60 | medium | `DjVuLibre` |
|
||||||
| CHM | ~5 | medium | `CHMLib` |
|
| ODT | ~5 | medium | Unzip + parse OpenDocument XML |
|
||||||
|
| CHM | ~5 | medium | `CHMLib` |
|
||||||
| PDB | small | low | Palm/Mobipocket format |
|
| PDB | small | low | Palm/Mobipocket format |
|
||||||
| ZIP/RAR | ~210 | low | Inspect contents; open book files within archive |
|
| ZIP/RAR | ~210 | low | Inspect contents; open book files within archive |
|
||||||
|
|
||||||
> ACSM files are Adobe DRM tokens — not directly readable; skip.
|
> ACSM files are Adobe DRM tokens — not directly readable; skip.
|
||||||
> PPT/XLS and source code files (.py, .cs, etc.) are in the library from programming books; no need to render them.
|
> PPT/XLS and source code files (.py, .cs, etc.) are in the library from programming books; no need to render them.
|
||||||
@@ -180,7 +181,7 @@ struct Chapter {
|
|||||||
- `QList<TocEntry> tableOfContents()` — empty list if a format has no TOC (e.g., TXT)
|
- `QList<TocEntry> tableOfContents()` — empty list if a format has no TOC (e.g., TXT)
|
||||||
- `QString title()`, `QString author()` — metadata
|
- `QString title()`, `QString author()` — metadata
|
||||||
|
|
||||||
Chapter mapping by format: TXT = 1 chapter (whole file). FB2 = `<section>` elements. EPUB = spine items. PDF = outline entries or individual pages.
|
Chapter mapping by format: TXT = 1 chapter (whole file). FB2 = `<section>` elements. EPUB = spine items. CBZ = 1 chapter (whole archive). PDF = outline entries or individual pages.
|
||||||
|
|
||||||
**Chunking:** Each format reader splits content into chunks natively — chunking at the reader level is trivial and reliable compared to splitting arbitrary HTML. TxtReader splits raw text at ~4096 character boundaries (preferring `\n`, falling back to whitespace), then converts each chunk to HTML independently. Each chunk is a standalone HTML fragment (contains its own `<p>` tags).
|
**Chunking:** Each format reader splits content into chunks natively — chunking at the reader level is trivial and reliable compared to splitting arbitrary HTML. TxtReader splits raw text at ~4096 character boundaries (preferring `\n`, falling back to whitespace), then converts each chunk to HTML independently. Each chunk is a standalone HTML fragment (contains its own `<p>` tags).
|
||||||
|
|
||||||
@@ -216,6 +217,8 @@ src/
|
|||||||
TxtReader.h/cpp
|
TxtReader.h/cpp
|
||||||
Fb2Reader.h/cpp
|
Fb2Reader.h/cpp
|
||||||
EpubReader.h/cpp
|
EpubReader.h/cpp
|
||||||
|
CbzReader.h/cpp
|
||||||
|
CbzImageProvider.h/cpp
|
||||||
PdfReader.h/cpp
|
PdfReader.h/cpp
|
||||||
RtfReader.h/cpp
|
RtfReader.h/cpp
|
||||||
DocReader.h/cpp
|
DocReader.h/cpp
|
||||||
|
|||||||
+18
-4
@@ -75,6 +75,10 @@ set(SOURCES
|
|||||||
src/backend/formats/Fb2Reader.cpp
|
src/backend/formats/Fb2Reader.cpp
|
||||||
src/backend/formats/Fb2ImageProvider.h
|
src/backend/formats/Fb2ImageProvider.h
|
||||||
src/backend/formats/Fb2ImageProvider.cpp
|
src/backend/formats/Fb2ImageProvider.cpp
|
||||||
|
src/backend/formats/CbzReader.h
|
||||||
|
src/backend/formats/CbzReader.cpp
|
||||||
|
src/backend/formats/CbzImageProvider.h
|
||||||
|
src/backend/formats/CbzImageProvider.cpp
|
||||||
src/backend/formats/WebImageProvider.h
|
src/backend/formats/WebImageProvider.h
|
||||||
src/backend/formats/WebImageProvider.cpp
|
src/backend/formats/WebImageProvider.cpp
|
||||||
src/backend/formats/ImageProviderFactory.h
|
src/backend/formats/ImageProviderFactory.h
|
||||||
@@ -240,11 +244,16 @@ add_asteria_test(Fb2ReaderTest tests/formats/Fb2ReaderTest.cpp
|
|||||||
src/backend/formats/Fb2Reader.cpp)
|
src/backend/formats/Fb2Reader.cpp)
|
||||||
target_link_libraries(Fb2ReaderTest PRIVATE Qt6::Xml)
|
target_link_libraries(Fb2ReaderTest PRIVATE Qt6::Xml)
|
||||||
|
|
||||||
|
add_asteria_test(CbzReaderTest tests/formats/CbzReaderTest.cpp
|
||||||
|
src/backend/formats/CbzReader.cpp)
|
||||||
|
target_link_libraries(CbzReaderTest PRIVATE PkgConfig::MINIZIP Qt6::Xml Qt6::Gui)
|
||||||
|
|
||||||
add_asteria_test(ReaderFactoryTest tests/ReaderFactoryTest.cpp
|
add_asteria_test(ReaderFactoryTest tests/ReaderFactoryTest.cpp
|
||||||
src/backend/formats/ReaderFactory.cpp
|
src/backend/formats/ReaderFactory.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.cpp
|
src/backend/formats/EpubReader.cpp
|
||||||
src/backend/formats/Fb2Reader.cpp)
|
src/backend/formats/Fb2Reader.cpp
|
||||||
|
src/backend/formats/CbzReader.cpp)
|
||||||
target_link_libraries(ReaderFactoryTest PRIVATE PkgConfig::MINIZIP Qt6::Xml)
|
target_link_libraries(ReaderFactoryTest PRIVATE PkgConfig::MINIZIP Qt6::Xml)
|
||||||
|
|
||||||
add_asteria_test(ChunkMeasurerTest tests/ChunkMeasurerTest.cpp
|
add_asteria_test(ChunkMeasurerTest tests/ChunkMeasurerTest.cpp
|
||||||
@@ -259,6 +268,7 @@ add_asteria_test(ReadingModelTest tests/ReadingModelTest.cpp
|
|||||||
src/backend/ChunkMeasurer.cpp
|
src/backend/ChunkMeasurer.cpp
|
||||||
src/backend/formats/EpubImageProvider.cpp
|
src/backend/formats/EpubImageProvider.cpp
|
||||||
src/backend/formats/Fb2ImageProvider.cpp
|
src/backend/formats/Fb2ImageProvider.cpp
|
||||||
|
src/backend/formats/CbzImageProvider.cpp
|
||||||
src/backend/formats/WebImageProvider.cpp
|
src/backend/formats/WebImageProvider.cpp
|
||||||
src/backend/formats/ImageProviderFactory.cpp
|
src/backend/formats/ImageProviderFactory.cpp
|
||||||
src/backend/AppState.cpp
|
src/backend/AppState.cpp
|
||||||
@@ -268,7 +278,8 @@ add_asteria_test(ReadingModelTest tests/ReadingModelTest.cpp
|
|||||||
src/backend/formats/ReaderFactory.cpp
|
src/backend/formats/ReaderFactory.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.cpp
|
src/backend/formats/EpubReader.cpp
|
||||||
src/backend/formats/Fb2Reader.cpp)
|
src/backend/formats/Fb2Reader.cpp
|
||||||
|
src/backend/formats/CbzReader.cpp)
|
||||||
target_link_libraries(ReadingModelTest PRIVATE Qt6::Gui Qt6::Concurrent Qt6::Quick Qt6::Network PkgConfig::MINIZIP Qt6::Xml)
|
target_link_libraries(ReadingModelTest PRIVATE Qt6::Gui Qt6::Concurrent Qt6::Quick Qt6::Network PkgConfig::MINIZIP Qt6::Xml)
|
||||||
set_tests_properties(ReadingModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(ReadingModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
@@ -298,7 +309,8 @@ add_asteria_test(BookTest tests/book/BookTest.cpp
|
|||||||
src/backend/NativeFileDialogService.cpp
|
src/backend/NativeFileDialogService.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.cpp
|
src/backend/formats/EpubReader.cpp
|
||||||
src/backend/formats/Fb2Reader.cpp)
|
src/backend/formats/Fb2Reader.cpp
|
||||||
|
src/backend/formats/CbzReader.cpp)
|
||||||
target_link_libraries(BookTest PRIVATE Qt6::Concurrent Qt6::Widgets PkgConfig::MINIZIP Qt6::Xml)
|
target_link_libraries(BookTest PRIVATE Qt6::Concurrent Qt6::Widgets PkgConfig::MINIZIP Qt6::Xml)
|
||||||
set_tests_properties(BookTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(BookTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
@@ -311,11 +323,13 @@ add_asteria_test(HistoryBookModelTest tests/HistoryBookModelTest.cpp
|
|||||||
src/backend/HistoryBookModel.cpp
|
src/backend/HistoryBookModel.cpp
|
||||||
src/backend/formats/EpubImageProvider.cpp
|
src/backend/formats/EpubImageProvider.cpp
|
||||||
src/backend/formats/Fb2ImageProvider.cpp
|
src/backend/formats/Fb2ImageProvider.cpp
|
||||||
|
src/backend/formats/CbzImageProvider.cpp
|
||||||
src/backend/formats/ImageProviderFactory.cpp
|
src/backend/formats/ImageProviderFactory.cpp
|
||||||
src/backend/formats/ReaderFactory.cpp
|
src/backend/formats/ReaderFactory.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.cpp
|
src/backend/formats/EpubReader.cpp
|
||||||
src/backend/formats/Fb2Reader.cpp)
|
src/backend/formats/Fb2Reader.cpp
|
||||||
|
src/backend/formats/CbzReader.cpp)
|
||||||
target_link_libraries(HistoryBookModelTest PRIVATE
|
target_link_libraries(HistoryBookModelTest PRIVATE
|
||||||
Qt6::Concurrent Qt6::Quick Qt6::Gui Qt6::Xml PkgConfig::MINIZIP)
|
Qt6::Concurrent Qt6::Quick Qt6::Gui Qt6::Xml PkgConfig::MINIZIP)
|
||||||
set_tests_properties(HistoryBookModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(HistoryBookModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
|
#include <QStringList>
|
||||||
#include <minizip/unzip.h>
|
#include <minizip/unzip.h>
|
||||||
|
|
||||||
namespace ZipUtils {
|
namespace ZipUtils {
|
||||||
@@ -24,4 +25,22 @@ inline QByteArray readZipEntry(unzFile zf, const char *entryName)
|
|||||||
return (bytesRead == 0) ? result : QByteArray{};
|
return (bytesRead == 0) ? result : QByteArray{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List all entry names in an open unzFile handle.
|
||||||
|
inline 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 ZipUtils
|
} // namespace ZipUtils
|
||||||
@@ -0,0 +1,194 @@
|
|||||||
|
#include "CbzImageProvider.h"
|
||||||
|
|
||||||
|
#include "backend/ZipUtils.h"
|
||||||
|
|
||||||
|
#include <QImage>
|
||||||
|
#include <QMutexLocker>
|
||||||
|
#include <QQuickTextureFactory>
|
||||||
|
#include <QThreadPool>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
|
#include <minizip/unzip.h>
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// CbzImageResponse
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
CbzImageResponse::CbzImageResponse(const QString &cbzPath, const QString &entryName,
|
||||||
|
int displayWidth, const QSize &requestedSize)
|
||||||
|
: m_cbzPath(cbzPath)
|
||||||
|
, m_entryName(entryName)
|
||||||
|
, m_displayWidth(displayWidth)
|
||||||
|
, m_requestedSize(requestedSize)
|
||||||
|
{
|
||||||
|
setAutoDelete(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
QQuickTextureFactory *CbzImageResponse::textureFactory() const
|
||||||
|
{
|
||||||
|
if (m_image.isNull())
|
||||||
|
return nullptr;
|
||||||
|
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CbzImageResponse::cancel()
|
||||||
|
{
|
||||||
|
m_cancelled.storeRelaxed(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CbzImageResponse::run()
|
||||||
|
{
|
||||||
|
if (m_cancelled.loadRelaxed())
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_image = CbzImageProvider::loadImageFromCbz(m_cbzPath, m_entryName,
|
||||||
|
m_displayWidth, m_requestedSize);
|
||||||
|
|
||||||
|
if (m_cancelled.loadRelaxed())
|
||||||
|
return;
|
||||||
|
|
||||||
|
emit finished();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// CbzImageProvider
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
QImage CbzImageProvider::loadImageFromCbz(const QString &cbzPath, const QString &entryName,
|
||||||
|
int displayWidth, const QSize &requestedSize)
|
||||||
|
{
|
||||||
|
if (cbzPath.isEmpty() || entryName.isEmpty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
unzFile zf = unzOpen(cbzPath.toUtf8().constData());
|
||||||
|
if (!zf)
|
||||||
|
return {};
|
||||||
|
auto closeGuard = [](void *p) { unzClose(static_cast<unzFile>(p)); };
|
||||||
|
std::unique_ptr<void, decltype(closeGuard)> guard(zf, closeGuard);
|
||||||
|
|
||||||
|
const QByteArray data = ZipUtils::readZipEntry(zf, entryName.toUtf8().constData());
|
||||||
|
if (data.isEmpty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
QImage image;
|
||||||
|
if (!image.loadFromData(data))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (requestedSize.width() > 0 || requestedSize.height() > 0) {
|
||||||
|
int targetW = requestedSize.width() > 0 ? requestedSize.width() : -1;
|
||||||
|
int targetH = requestedSize.height() > 0 ? requestedSize.height() : -1;
|
||||||
|
if (displayWidth > 0 && targetW > 0 && targetW > displayWidth)
|
||||||
|
targetW = displayWidth;
|
||||||
|
if (targetW > 0 && targetH > 0)
|
||||||
|
image = image.scaled(QSize(targetW, targetH), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||||
|
else if (targetW > 0)
|
||||||
|
image = image.scaledToWidth(targetW, Qt::SmoothTransformation);
|
||||||
|
else
|
||||||
|
image = image.scaledToHeight(targetH, Qt::SmoothTransformation);
|
||||||
|
} else if (displayWidth > 0 && image.width() > displayWidth) {
|
||||||
|
image = image.scaledToWidth(displayWidth, Qt::SmoothTransformation);
|
||||||
|
}
|
||||||
|
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
QQuickImageResponse *CbzImageProvider::requestImageResponse(const QString &id,
|
||||||
|
const QSize &requestedSize)
|
||||||
|
{
|
||||||
|
QString cbzPath;
|
||||||
|
int displayWidth;
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_mutex);
|
||||||
|
cbzPath = m_cbzPath;
|
||||||
|
displayWidth = m_displayWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id may be "entry/name.jpg?cbz=<encoded-path>"
|
||||||
|
// Qt Quick passes the path portion of the URL in percent-encoded form
|
||||||
|
// (e.g. "{HQ}" becomes "%7BHQ%7D"), so decode it back to get the real entry name.
|
||||||
|
QString entryName = id;
|
||||||
|
const int qmark = id.indexOf(QLatin1Char('?'));
|
||||||
|
if (qmark >= 0) {
|
||||||
|
entryName = id.left(qmark);
|
||||||
|
const QString query = id.mid(qmark + 1);
|
||||||
|
const QLatin1String key("cbz=");
|
||||||
|
if (query.startsWith(key))
|
||||||
|
cbzPath = QUrl::fromPercentEncoding(query.mid(key.size()).toUtf8());
|
||||||
|
}
|
||||||
|
entryName = QUrl::fromPercentEncoding(entryName.toUtf8());
|
||||||
|
|
||||||
|
auto *response = new CbzImageResponse(cbzPath, entryName, displayWidth, requestedSize);
|
||||||
|
QThreadPool::globalInstance()->start(response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
QImage CbzImageProvider::load(const QUrl &url, int maxWidth) const
|
||||||
|
{
|
||||||
|
if (url.scheme() != QLatin1String("image"))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
QString entryName = url.path();
|
||||||
|
if (entryName.startsWith(QLatin1Char('/')))
|
||||||
|
entryName = entryName.mid(1);
|
||||||
|
if (entryName.isEmpty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
QString cbzPath;
|
||||||
|
if (url.hasQuery()) {
|
||||||
|
const QString q = url.query();
|
||||||
|
const QLatin1String key("cbz=");
|
||||||
|
if (q.startsWith(key))
|
||||||
|
cbzPath = QUrl::fromPercentEncoding(q.mid(key.size()).toUtf8());
|
||||||
|
}
|
||||||
|
if (cbzPath.isEmpty()) {
|
||||||
|
QMutexLocker lock(&m_mutex);
|
||||||
|
cbzPath = m_cbzPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
return loadImageFromCbz(cbzPath, entryName, maxWidth, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IImageLoader> CbzImageProvider::clone() const
|
||||||
|
{
|
||||||
|
QString cbzPath;
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_mutex);
|
||||||
|
cbzPath = m_cbzPath;
|
||||||
|
}
|
||||||
|
auto c = std::make_unique<CbzImageProvider>();
|
||||||
|
c->setFilePath(cbzPath);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CbzImageProvider::setFilePath(const QString &filePath)
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_mutex);
|
||||||
|
m_cbzPath = filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CbzImageProvider::coverImageUrl(const QString &coverPath) const
|
||||||
|
{
|
||||||
|
QString cbzPath;
|
||||||
|
{ QMutexLocker lock(&m_mutex); cbzPath = m_cbzPath; }
|
||||||
|
return QStringLiteral("image://cbz/") + coverPath
|
||||||
|
+ QStringLiteral("?cbz=")
|
||||||
|
+ QString::fromUtf8(QUrl::toPercentEncoding(cbzPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CbzImageProvider::scheme() const
|
||||||
|
{
|
||||||
|
return QStringLiteral("cbz");
|
||||||
|
}
|
||||||
|
|
||||||
|
void CbzImageProvider::setDisplayWidth(int width)
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_mutex);
|
||||||
|
m_displayWidth = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CbzImageProvider::clear()
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_mutex);
|
||||||
|
m_cbzPath.clear();
|
||||||
|
m_displayWidth = 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#ifndef CBZIMAGEPROVIDER_H
|
||||||
|
#define CBZIMAGEPROVIDER_H
|
||||||
|
|
||||||
|
#include <QMutex>
|
||||||
|
#include <QRunnable>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
#include "backend/IImageLoader.h"
|
||||||
|
#include "backend/IImageProvider.h"
|
||||||
|
|
||||||
|
class CbzImageResponse : public QQuickImageResponse, public QRunnable {
|
||||||
|
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||||
|
public:
|
||||||
|
CbzImageResponse(const QString &cbzPath, const QString &entryName,
|
||||||
|
int displayWidth, const QSize &requestedSize);
|
||||||
|
|
||||||
|
QQuickTextureFactory *textureFactory() const override;
|
||||||
|
void cancel() override;
|
||||||
|
void run() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const QString m_cbzPath;
|
||||||
|
const QString m_entryName;
|
||||||
|
const int m_displayWidth;
|
||||||
|
const QSize m_requestedSize;
|
||||||
|
QAtomicInt m_cancelled{0};
|
||||||
|
QImage m_image;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CbzImageProvider : public IImageProvider, public IImageLoader {
|
||||||
|
public:
|
||||||
|
QQuickImageResponse *requestImageResponse(const QString &id,
|
||||||
|
const QSize &requestedSize) override;
|
||||||
|
|
||||||
|
void setFilePath(const QString &filePath) override;
|
||||||
|
void setDisplayWidth(int width) override;
|
||||||
|
void clear() override;
|
||||||
|
QString coverImageUrl(const QString &coverPath) const override;
|
||||||
|
QString scheme() const override;
|
||||||
|
|
||||||
|
QImage load(const QUrl &url, int maxWidth) const override;
|
||||||
|
std::unique_ptr<IImageLoader> clone() const override;
|
||||||
|
|
||||||
|
static QImage loadImageFromCbz(const QString &cbzPath, const QString &entryName,
|
||||||
|
int displayWidth, const QSize &requestedSize);
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable QMutex m_mutex;
|
||||||
|
QString m_cbzPath;
|
||||||
|
int m_displayWidth = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CBZIMAGEPROVIDER_H
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
#include "CbzReader.h"
|
||||||
|
|
||||||
|
#include "backend/ZipUtils.h"
|
||||||
|
|
||||||
|
#include <QCryptographicHash>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QSet>
|
||||||
|
#include <QXmlStreamReader>
|
||||||
|
|
||||||
|
#include <minizip/unzip.h>
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// ComicInfo.xml parsing
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static void parseComicInfo(const QByteArray &data,
|
||||||
|
QString &outTitle, QString &outAuthor, int &outCoverIndex)
|
||||||
|
{
|
||||||
|
QXmlStreamReader xml(data);
|
||||||
|
QString title, series, number, writer;
|
||||||
|
outCoverIndex = -1;
|
||||||
|
|
||||||
|
while (!xml.atEnd()) {
|
||||||
|
xml.readNext();
|
||||||
|
if (!xml.isStartElement())
|
||||||
|
continue;
|
||||||
|
const auto name = xml.name();
|
||||||
|
if (name == QLatin1String("Title"))
|
||||||
|
title = xml.readElementText().trimmed();
|
||||||
|
else if (name == QLatin1String("Series"))
|
||||||
|
series = xml.readElementText().trimmed();
|
||||||
|
else if (name == QLatin1String("Number"))
|
||||||
|
number = xml.readElementText().trimmed();
|
||||||
|
else if (name == QLatin1String("Writer"))
|
||||||
|
writer = xml.readElementText().trimmed();
|
||||||
|
else if (name == QLatin1String("Page")) {
|
||||||
|
const auto attrs = xml.attributes();
|
||||||
|
if (attrs.value(QLatin1String("Type")) == QLatin1String("FrontCover")) {
|
||||||
|
bool ok;
|
||||||
|
const int idx = attrs.value(QLatin1String("Image")).toInt(&ok);
|
||||||
|
if (ok && outCoverIndex < 0)
|
||||||
|
outCoverIndex = idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!title.isEmpty()) {
|
||||||
|
outTitle = title;
|
||||||
|
} else if (!series.isEmpty()) {
|
||||||
|
outTitle = series;
|
||||||
|
if (!number.isEmpty())
|
||||||
|
outTitle += QStringLiteral(" #") + number;
|
||||||
|
}
|
||||||
|
outAuthor = writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// CbzReader
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static const QSet<QString> kImageExtensions = {
|
||||||
|
QStringLiteral("jpg"), QStringLiteral("jpeg"),
|
||||||
|
QStringLiteral("png"), QStringLiteral("gif"),
|
||||||
|
QStringLiteral("webp")
|
||||||
|
};
|
||||||
|
|
||||||
|
bool CbzReader::parseArchive(const QString &filePath)
|
||||||
|
{
|
||||||
|
unzFile zf = unzOpen(filePath.toUtf8().constData());
|
||||||
|
if (!zf) {
|
||||||
|
m_error = QStringLiteral("Cannot open as ZIP archive: ") + filePath;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto closeGuard = [](void *p) { unzClose(static_cast<unzFile>(p)); };
|
||||||
|
std::unique_ptr<void, decltype(closeGuard)> guard(zf, closeGuard);
|
||||||
|
|
||||||
|
const QStringList allEntries = ZipUtils::listZipEntries(zf);
|
||||||
|
|
||||||
|
for (const QString &entry : allEntries) {
|
||||||
|
if (entry.startsWith(QLatin1String("__MACOSX/")))
|
||||||
|
continue;
|
||||||
|
const QString baseName = entry.contains(QLatin1Char('/'))
|
||||||
|
? entry.section(QLatin1Char('/'), -1) : entry;
|
||||||
|
if (baseName.startsWith(QLatin1Char('.')))
|
||||||
|
continue;
|
||||||
|
if (kImageExtensions.contains(QFileInfo(entry).suffix().toLower()))
|
||||||
|
m_pageEntries.append(entry);
|
||||||
|
}
|
||||||
|
m_pageEntries.sort();
|
||||||
|
|
||||||
|
if (m_pageEntries.isEmpty()) {
|
||||||
|
m_error = QStringLiteral("No images found in archive: ") + filePath;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_coverEntry = m_pageEntries.first();
|
||||||
|
|
||||||
|
// Look for ComicInfo.xml (case-insensitive, archive-root only)
|
||||||
|
for (const QString &entry : allEntries) {
|
||||||
|
if (entry.compare(QLatin1String("ComicInfo.xml"), Qt::CaseInsensitive) == 0) {
|
||||||
|
const QByteArray comicInfoData = ZipUtils::readZipEntry(zf, entry.toUtf8().constData());
|
||||||
|
if (!comicInfoData.isEmpty()) {
|
||||||
|
int coverIndex = -1;
|
||||||
|
parseComicInfo(comicInfoData, m_title, m_author, coverIndex);
|
||||||
|
if (coverIndex >= 0 && coverIndex < m_pageEntries.size())
|
||||||
|
m_coverEntry = m_pageEntries[coverIndex];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_title.isEmpty())
|
||||||
|
m_title = QFileInfo(filePath).completeBaseName();
|
||||||
|
|
||||||
|
m_filePath = filePath;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CbzReader::open(const QString &filePath)
|
||||||
|
{
|
||||||
|
m_title.clear();
|
||||||
|
m_author.clear();
|
||||||
|
m_error.clear();
|
||||||
|
m_pageEntries.clear();
|
||||||
|
m_coverEntry.clear();
|
||||||
|
m_contentHash.clear();
|
||||||
|
m_filePath.clear();
|
||||||
|
|
||||||
|
QFile f(filePath);
|
||||||
|
if (f.open(QIODevice::ReadOnly)) {
|
||||||
|
m_contentHash = QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseArchive(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CbzReader::loadMetadata(const QString &filePath)
|
||||||
|
{
|
||||||
|
m_title.clear();
|
||||||
|
m_author.clear();
|
||||||
|
m_error.clear();
|
||||||
|
m_pageEntries.clear();
|
||||||
|
m_coverEntry.clear();
|
||||||
|
m_filePath.clear();
|
||||||
|
|
||||||
|
return parseArchive(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
Chapter CbzReader::loadChapter(int index)
|
||||||
|
{
|
||||||
|
if (index != 0 || m_pageEntries.isEmpty())
|
||||||
|
return Chapter{index, {}, {}};
|
||||||
|
|
||||||
|
QStringList chunks;
|
||||||
|
chunks.reserve(m_pageEntries.size());
|
||||||
|
for (const QString &entry : m_pageEntries) {
|
||||||
|
chunks.append(QStringLiteral("<p style=\"text-align:center;margin:0\">"
|
||||||
|
"<img src=\"image://cbz/%1\"/></p>").arg(entry));
|
||||||
|
}
|
||||||
|
return Chapter{0, m_title, chunks};
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CbzReader::lastError() { return m_error; }
|
||||||
|
QString CbzReader::title() { return m_title; }
|
||||||
|
QString CbzReader::author() { return m_author; }
|
||||||
|
QString CbzReader::coverImagePath() { return m_coverEntry; }
|
||||||
|
int CbzReader::chapterCount() { return m_pageEntries.isEmpty() ? 0 : 1; }
|
||||||
|
QString CbzReader::filePath() { return m_filePath; }
|
||||||
|
QByteArray CbzReader::contentHash() { return m_contentHash; }
|
||||||
|
QString CbzReader::fileType() const { return QStringLiteral("cbz"); }
|
||||||
|
|
||||||
|
QList<TocEntry> CbzReader::tableOfContents() { return {}; }
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef CBZREADER_H
|
||||||
|
#define CBZREADER_H
|
||||||
|
|
||||||
|
#include "backend/FormatReader.h"
|
||||||
|
#include <QByteArray>
|
||||||
|
#include <QList>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
class CbzReader : public FormatReader {
|
||||||
|
public:
|
||||||
|
bool open(const QString &filePath) override;
|
||||||
|
bool loadMetadata(const QString &filePath) override;
|
||||||
|
QString lastError() override;
|
||||||
|
int chapterCount() override;
|
||||||
|
Chapter loadChapter(int index) override;
|
||||||
|
QList<TocEntry> tableOfContents() override;
|
||||||
|
QString filePath() override;
|
||||||
|
QString title() override;
|
||||||
|
QString author() override;
|
||||||
|
QString coverImagePath() override;
|
||||||
|
QByteArray contentHash() override;
|
||||||
|
QString fileType() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool parseArchive(const QString &filePath);
|
||||||
|
|
||||||
|
QString m_filePath;
|
||||||
|
QByteArray m_contentHash;
|
||||||
|
QString m_title;
|
||||||
|
QString m_author;
|
||||||
|
QString m_error;
|
||||||
|
QStringList m_pageEntries;
|
||||||
|
QString m_coverEntry;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CBZREADER_H
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "ImageLoaderFactory.h"
|
#include "ImageLoaderFactory.h"
|
||||||
|
#include "CbzImageProvider.h"
|
||||||
#include "EpubImageProvider.h"
|
#include "EpubImageProvider.h"
|
||||||
#include "Fb2ImageProvider.h"
|
#include "Fb2ImageProvider.h"
|
||||||
#include "WebImageProvider.h"
|
#include "WebImageProvider.h"
|
||||||
@@ -53,5 +54,10 @@ std::unique_ptr<IImageLoader> ImageLoaderFactory::create(const QString &filePath
|
|||||||
loader->setFilePath(filePath);
|
loader->setFilePath(filePath);
|
||||||
return loader;
|
return loader;
|
||||||
}
|
}
|
||||||
|
if (ext == QLatin1String("cbz")) {
|
||||||
|
auto loader = std::make_unique<CbzImageProvider>();
|
||||||
|
loader->setFilePath(filePath);
|
||||||
|
return loader;
|
||||||
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "ImageProviderFactory.h"
|
#include "ImageProviderFactory.h"
|
||||||
|
#include "CbzImageProvider.h"
|
||||||
#include "EpubImageProvider.h"
|
#include "EpubImageProvider.h"
|
||||||
#include "Fb2ImageProvider.h"
|
#include "Fb2ImageProvider.h"
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
@@ -10,5 +11,7 @@ IImageProvider *ImageProviderFactory::create(const QString &filePath)
|
|||||||
return new EpubImageProvider();
|
return new EpubImageProvider();
|
||||||
if (ext == QLatin1String("fb2"))
|
if (ext == QLatin1String("fb2"))
|
||||||
return new Fb2ImageProvider();
|
return new Fb2ImageProvider();
|
||||||
|
if (ext == QLatin1String("cbz"))
|
||||||
|
return new CbzImageProvider();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,8 @@
|
|||||||
#include "backend/formats/EpubReader.h"
|
#include "backend/formats/EpubReader.h"
|
||||||
#include "backend/formats/Fb2Reader.h"
|
#include "backend/formats/Fb2Reader.h"
|
||||||
#include "backend/formats/Fb2ImageProvider.h"
|
#include "backend/formats/Fb2ImageProvider.h"
|
||||||
|
#include "backend/formats/CbzReader.h"
|
||||||
|
#include "backend/formats/CbzImageProvider.h"
|
||||||
#include "backend/state/BookPositionStore.h"
|
#include "backend/state/BookPositionStore.h"
|
||||||
#include "backend/state/ReadingPreferences.h"
|
#include "backend/state/ReadingPreferences.h"
|
||||||
#include "backend/state/UiState.h"
|
#include "backend/state/UiState.h"
|
||||||
@@ -106,6 +108,7 @@ int main(int argc, char *argv[])
|
|||||||
[] { return std::make_unique<TxtReader>(); },
|
[] { return std::make_unique<TxtReader>(); },
|
||||||
[] { return std::make_unique<EpubReader>(); },
|
[] { return std::make_unique<EpubReader>(); },
|
||||||
[] { return std::make_unique<Fb2Reader>(); },
|
[] { return std::make_unique<Fb2Reader>(); },
|
||||||
|
[] { return std::make_unique<CbzReader>(); },
|
||||||
});
|
});
|
||||||
|
|
||||||
HistoryModel historyModel(readerFactory);
|
HistoryModel historyModel(readerFactory);
|
||||||
@@ -144,6 +147,7 @@ int main(int argc, char *argv[])
|
|||||||
engine.addImageProvider(QStringLiteral("web"), new WebImageProvider());
|
engine.addImageProvider(QStringLiteral("web"), new WebImageProvider());
|
||||||
engine.addImageProvider(QStringLiteral("epub"), new EpubImageProvider());
|
engine.addImageProvider(QStringLiteral("epub"), new EpubImageProvider());
|
||||||
engine.addImageProvider(QStringLiteral("fb2"), new Fb2ImageProvider());
|
engine.addImageProvider(QStringLiteral("fb2"), new Fb2ImageProvider());
|
||||||
|
engine.addImageProvider(QStringLiteral("cbz"), new CbzImageProvider());
|
||||||
engine.addImageProvider(QStringLiteral("fileicon"), new FileIconProvider());
|
engine.addImageProvider(QStringLiteral("fileicon"), new FileIconProvider());
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
||||||
#ifdef QT_DEBUG
|
#ifdef QT_DEBUG
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
#include "backend/FormatReader.h"
|
#include "backend/FormatReader.h"
|
||||||
#include "backend/formats/TxtReader.h"
|
#include "backend/formats/TxtReader.h"
|
||||||
#include "backend/formats/EpubReader.h"
|
#include "backend/formats/EpubReader.h"
|
||||||
|
#include "backend/formats/CbzReader.h"
|
||||||
|
|
||||||
static ReaderFactory makeFactory()
|
static ReaderFactory makeFactory()
|
||||||
{
|
{
|
||||||
return ReaderFactory({
|
return ReaderFactory({
|
||||||
[] { return std::make_unique<TxtReader>(); },
|
[] { return std::make_unique<TxtReader>(); },
|
||||||
[] { return std::make_unique<EpubReader>(); },
|
[] { return std::make_unique<EpubReader>(); },
|
||||||
|
[] { return std::make_unique<CbzReader>(); },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,12 +53,20 @@ private slots:
|
|||||||
QVERIFY(reader == nullptr);
|
QVERIFY(reader == nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void createCbzReader()
|
||||||
|
{
|
||||||
|
auto factory = makeFactory();
|
||||||
|
auto reader = factory.create("file.cbz");
|
||||||
|
QVERIFY(reader != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
void supportedExtensions()
|
void supportedExtensions()
|
||||||
{
|
{
|
||||||
auto factory = makeFactory();
|
auto factory = makeFactory();
|
||||||
const QStringList exts = factory.supportedExtensions();
|
const QStringList exts = factory.supportedExtensions();
|
||||||
QVERIFY(exts.contains(QStringLiteral("txt")));
|
QVERIFY(exts.contains(QStringLiteral("txt")));
|
||||||
QVERIFY(exts.contains(QStringLiteral("epub")));
|
QVERIFY(exts.contains(QStringLiteral("epub")));
|
||||||
|
QVERIFY(exts.contains(QStringLiteral("cbz")));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,404 @@
|
|||||||
|
#include <QtTest/QtTest>
|
||||||
|
#include <QBuffer>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QImage>
|
||||||
|
#include <QTemporaryFile>
|
||||||
|
|
||||||
|
#include <minizip/zip.h>
|
||||||
|
|
||||||
|
#include "backend/formats/CbzReader.h"
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Fixture helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static QByteArray makePng(int w = 8, int h = 8, QColor color = Qt::blue)
|
||||||
|
{
|
||||||
|
QImage img(w, h, QImage::Format_RGB888);
|
||||||
|
img.fill(color);
|
||||||
|
QByteArray data;
|
||||||
|
QBuffer buf(&data);
|
||||||
|
buf.open(QIODevice::WriteOnly);
|
||||||
|
img.save(&buf, "PNG");
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool addEntry(zipFile zf, const char *name, const QByteArray &data)
|
||||||
|
{
|
||||||
|
zip_fileinfo fi{};
|
||||||
|
if (zipOpenNewFileInZip(zf, name, &fi,
|
||||||
|
nullptr, 0, nullptr, 0, nullptr,
|
||||||
|
Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK)
|
||||||
|
return false;
|
||||||
|
if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) {
|
||||||
|
zipCloseFileInZip(zf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return zipCloseFileInZip(zf) == ZIP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a CBZ at a temp path with the given entries (name -> bytes).
|
||||||
|
// Caller must delete the returned path when done.
|
||||||
|
static QString buildCbz(const QList<QPair<QString, QByteArray>> &entries)
|
||||||
|
{
|
||||||
|
QTemporaryFile tmp;
|
||||||
|
tmp.setFileTemplate(QDir::tempPath() + "/cbz_test_XXXXXX.cbz");
|
||||||
|
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) {
|
||||||
|
QFile::remove(path);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto &[name, data] : entries) {
|
||||||
|
if (!addEntry(zf, name.toUtf8().constData(), data)) {
|
||||||
|
zipClose(zf, nullptr);
|
||||||
|
QFile::remove(path);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
zipClose(zf, nullptr);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Test class
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class CbzReaderTest : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
QStringList m_tempFiles;
|
||||||
|
|
||||||
|
QString makeCbz(const QList<QPair<QString, QByteArray>> &entries)
|
||||||
|
{
|
||||||
|
const QString p = buildCbz(entries);
|
||||||
|
if (!p.isEmpty())
|
||||||
|
m_tempFiles.append(p);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void cleanupTestCase()
|
||||||
|
{
|
||||||
|
for (const QString &p : m_tempFiles)
|
||||||
|
QFile::remove(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- error cases ---
|
||||||
|
|
||||||
|
void openNonexistentFile()
|
||||||
|
{
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(!reader.open("/does/not/exist.cbz"));
|
||||||
|
QVERIFY(!reader.lastError().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void openNonZipFile()
|
||||||
|
{
|
||||||
|
QTemporaryFile tmp;
|
||||||
|
tmp.setAutoRemove(false);
|
||||||
|
QVERIFY(tmp.open());
|
||||||
|
tmp.write("this is not a zip file");
|
||||||
|
tmp.close();
|
||||||
|
m_tempFiles.append(tmp.fileName());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(!reader.open(tmp.fileName()));
|
||||||
|
QVERIFY(!reader.lastError().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void openZipWithNoImages()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({{"readme.txt", "hello"}});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(!reader.open(path));
|
||||||
|
QVERIFY(!reader.lastError().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- page count and chapter structure ---
|
||||||
|
|
||||||
|
void pageCount()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"page001.jpg", makePng()},
|
||||||
|
{"page002.jpg", makePng(8, 8, Qt::red)},
|
||||||
|
{"page003.jpg", makePng(8, 8, Qt::green)},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QCOMPARE(reader.chapterCount(), 1);
|
||||||
|
|
||||||
|
const Chapter ch = reader.loadChapter(0);
|
||||||
|
QCOMPARE(ch.contentChunks.size(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pagesAreSorted()
|
||||||
|
{
|
||||||
|
// Add images in reverse order — they must come out sorted
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"page003.png", makePng(8, 8, Qt::red)},
|
||||||
|
{"page001.png", makePng(8, 8, Qt::blue)},
|
||||||
|
{"page002.png", makePng(8, 8, Qt::green)},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
const Chapter ch = reader.loadChapter(0);
|
||||||
|
QCOMPARE(ch.contentChunks.size(), 3);
|
||||||
|
QVERIFY(ch.contentChunks.at(0).contains(QLatin1String("page001.png")));
|
||||||
|
QVERIFY(ch.contentChunks.at(1).contains(QLatin1String("page002.png")));
|
||||||
|
QVERIFY(ch.contentChunks.at(2).contains(QLatin1String("page003.png")));
|
||||||
|
}
|
||||||
|
|
||||||
|
void oneChunkPerPage()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"p01.jpg", makePng()},
|
||||||
|
{"p02.jpg", makePng()},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
const Chapter ch = reader.loadChapter(0);
|
||||||
|
QCOMPARE(ch.contentChunks.size(), 2);
|
||||||
|
|
||||||
|
// Each chunk is an HTML fragment with a single image://cbz/ img tag
|
||||||
|
for (const QString &chunk : ch.contentChunks) {
|
||||||
|
QVERIFY(chunk.contains(QLatin1String("image://cbz/")));
|
||||||
|
QCOMPARE(chunk.count(QLatin1String("<img")), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void outOfRangeChapterReturnsEmpty()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({{"page001.png", makePng()}});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
const Chapter ch = reader.loadChapter(5);
|
||||||
|
QVERIFY(ch.contentChunks.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void emptyChapterCountBeforeOpen()
|
||||||
|
{
|
||||||
|
CbzReader reader;
|
||||||
|
QCOMPARE(reader.chapterCount(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- metadata: ComicInfo.xml present ---
|
||||||
|
|
||||||
|
void metadataTitle()
|
||||||
|
{
|
||||||
|
const QByteArray comicInfo =
|
||||||
|
"<?xml version=\"1.0\"?>"
|
||||||
|
"<ComicInfo><Title>My Comic</Title><Writer>Art Spiegelman</Writer></ComicInfo>";
|
||||||
|
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"ComicInfo.xml", comicInfo},
|
||||||
|
{"page001.jpg", makePng()},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QCOMPARE(reader.title(), QStringLiteral("My Comic"));
|
||||||
|
QCOMPARE(reader.author(), QStringLiteral("Art Spiegelman"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void metadataSeriesAndNumber()
|
||||||
|
{
|
||||||
|
const QByteArray comicInfo =
|
||||||
|
"<?xml version=\"1.0\"?>"
|
||||||
|
"<ComicInfo><Series>Batman</Series><Number>42</Number></ComicInfo>";
|
||||||
|
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"ComicInfo.xml", comicInfo},
|
||||||
|
{"page001.jpg", makePng()},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QCOMPARE(reader.title(), QStringLiteral("Batman #42"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void metadataTitleTakesPriorityOverSeries()
|
||||||
|
{
|
||||||
|
const QByteArray comicInfo =
|
||||||
|
"<?xml version=\"1.0\"?>"
|
||||||
|
"<ComicInfo>"
|
||||||
|
" <Title>Special Edition</Title>"
|
||||||
|
" <Series>Batman</Series><Number>42</Number>"
|
||||||
|
"</ComicInfo>";
|
||||||
|
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"ComicInfo.xml", comicInfo},
|
||||||
|
{"page001.jpg", makePng()},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QCOMPARE(reader.title(), QStringLiteral("Special Edition"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- metadata: no ComicInfo.xml ---
|
||||||
|
|
||||||
|
void metadataFallbackToFilename()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({{"page001.png", makePng()}});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QVERIFY(!reader.title().isEmpty());
|
||||||
|
// Title must equal the file's base name (no extension)
|
||||||
|
const QString expected = QFileInfo(path).completeBaseName();
|
||||||
|
QCOMPARE(reader.title(), expected);
|
||||||
|
QVERIFY(reader.author().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void tableOfContentsIsEmpty()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({{"page001.png", makePng()}});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QVERIFY(reader.tableOfContents().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- cover ---
|
||||||
|
|
||||||
|
void coverFallbackToFirstSortedPage()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"page002.jpg", makePng(8, 8, Qt::red)},
|
||||||
|
{"page001.jpg", makePng(8, 8, Qt::blue)},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QCOMPARE(reader.coverImagePath(), QStringLiteral("page001.jpg"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void coverFromComicInfoFrontCover()
|
||||||
|
{
|
||||||
|
// Page Image="1" is the front cover → second sorted image
|
||||||
|
const QByteArray comicInfo =
|
||||||
|
"<?xml version=\"1.0\"?>"
|
||||||
|
"<ComicInfo>"
|
||||||
|
" <Title>Test</Title>"
|
||||||
|
" <Pages>"
|
||||||
|
" <Page Image=\"0\" />"
|
||||||
|
" <Page Image=\"1\" Type=\"FrontCover\" />"
|
||||||
|
" </Pages>"
|
||||||
|
"</ComicInfo>";
|
||||||
|
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"ComicInfo.xml", comicInfo},
|
||||||
|
{"page001.jpg", makePng(8, 8, Qt::blue)},
|
||||||
|
{"page002.jpg", makePng(8, 8, Qt::red)},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QCOMPARE(reader.coverImagePath(), QStringLiteral("page002.jpg"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- content hash ---
|
||||||
|
|
||||||
|
void contentHashIsStable()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({{"page001.png", makePng()}});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader r1, r2;
|
||||||
|
QVERIFY(r1.open(path));
|
||||||
|
QVERIFY(r2.open(path));
|
||||||
|
QVERIFY(!r1.contentHash().isEmpty());
|
||||||
|
QCOMPARE(r1.contentHash(), r2.contentHash());
|
||||||
|
}
|
||||||
|
|
||||||
|
void contentHashEmptyBeforeOpen()
|
||||||
|
{
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.contentHash().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- loadMetadata lightweight path ---
|
||||||
|
|
||||||
|
void loadMetadataSetsTitle()
|
||||||
|
{
|
||||||
|
const QByteArray comicInfo =
|
||||||
|
"<?xml version=\"1.0\"?>"
|
||||||
|
"<ComicInfo><Title>Light Meta</Title></ComicInfo>";
|
||||||
|
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"ComicInfo.xml", comicInfo},
|
||||||
|
{"page001.jpg", makePng()},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.loadMetadata(path));
|
||||||
|
QCOMPARE(reader.title(), QStringLiteral("Light Meta"));
|
||||||
|
// contentHash must be empty (loadMetadata is lightweight)
|
||||||
|
QVERIFY(reader.contentHash().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadMetadataSetsCover()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"page001.jpg", makePng(8, 8, Qt::blue)},
|
||||||
|
{"page002.jpg", makePng(8, 8, Qt::red)},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.loadMetadata(path));
|
||||||
|
QCOMPARE(reader.coverImagePath(), QStringLiteral("page001.jpg"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- file type ---
|
||||||
|
|
||||||
|
void fileType()
|
||||||
|
{
|
||||||
|
CbzReader reader;
|
||||||
|
QCOMPARE(reader.fileType(), QStringLiteral("cbz"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- skips __MACOSX entries ---
|
||||||
|
|
||||||
|
void macosxEntriesSkipped()
|
||||||
|
{
|
||||||
|
const QString path = makeCbz({
|
||||||
|
{"__MACOSX/._page001.jpg", makePng(8, 8, Qt::red)},
|
||||||
|
{"page001.jpg", makePng(8, 8, Qt::blue)},
|
||||||
|
});
|
||||||
|
QVERIFY(!path.isEmpty());
|
||||||
|
|
||||||
|
CbzReader reader;
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
QCOMPARE(reader.loadChapter(0).contentChunks.size(), 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(CbzReaderTest)
|
||||||
|
#include "CbzReaderTest.moc"
|
||||||
Reference in new issue
Block a user