old render cleanup part 3
This commit is contained in:
45 files changed
+1057
-1135
No files matched your search
@@ -2,15 +2,12 @@
|
||||
|
||||
#include "AppState.h"
|
||||
#include "HistoryModel.h"
|
||||
#include "IImageProvider.h"
|
||||
#include "ReadingModel.h"
|
||||
#include "book/Book.h"
|
||||
#include "formats/FormatRegistry.h"
|
||||
#include "state/ReadingPreferences.h"
|
||||
#include "state/UiState.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QSettings>
|
||||
|
||||
AppCoordinator::AppCoordinator(Book &book,
|
||||
@@ -20,8 +17,6 @@ AppCoordinator::AppCoordinator(Book &book,
|
||||
ReadingPreferences &readingPreferences,
|
||||
AppState &appState,
|
||||
QSettings &settings,
|
||||
FormatRegistry &formatRegistry,
|
||||
QQmlApplicationEngine &engine,
|
||||
QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_book(book)
|
||||
@@ -31,11 +26,9 @@ AppCoordinator::AppCoordinator(Book &book,
|
||||
, m_readingPreferences(readingPreferences)
|
||||
, m_appState(appState)
|
||||
, m_settings(settings)
|
||||
, m_formatRegistry(formatRegistry)
|
||||
, m_engine(engine)
|
||||
{
|
||||
connect(&m_book, &Book::willOpenNewBook, this, &AppCoordinator::onWillOpenNewBook);
|
||||
connect(&m_book, &Book::positionSaved, this, &AppCoordinator::onPositionSaved);
|
||||
connect(&m_readingModel, &ReadingModel::positionSaved, this, &AppCoordinator::onPositionSaved);
|
||||
connect(&m_book, &Book::bookChanged, this, &AppCoordinator::onBookChanged);
|
||||
|
||||
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
|
||||
@@ -52,14 +45,13 @@ void AppCoordinator::onWillOpenNewBook()
|
||||
// Real-time history update: fired when position is saved.
|
||||
void AppCoordinator::onPositionSaved()
|
||||
{
|
||||
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
||||
if (m_book.hasBook())
|
||||
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
||||
}
|
||||
|
||||
// After the new reader is installed: wire up image provider + ReadingModel
|
||||
// After the new reader is installed: configure ReadingModel
|
||||
void AppCoordinator::onBookChanged()
|
||||
{
|
||||
m_currentImageProvider = nullptr;
|
||||
|
||||
if (!m_book.hasBook()) {
|
||||
m_readingModel.setReader(nullptr);
|
||||
return;
|
||||
@@ -67,26 +59,12 @@ void AppCoordinator::onBookChanged()
|
||||
|
||||
FormatReader *reader = m_book.reader();
|
||||
const QString filePath = reader->filePath();
|
||||
|
||||
IImageProvider *provider = m_formatRegistry.createImageProvider(filePath);
|
||||
if (provider) {
|
||||
m_engine.removeImageProvider(provider->scheme());
|
||||
m_engine.addImageProvider(provider->scheme(), provider);
|
||||
provider->setFilePath(filePath);
|
||||
m_currentImageProvider = provider;
|
||||
|
||||
if (!reader->coverImagePath().isEmpty())
|
||||
m_book.setCoverImageUrl(provider->coverImageUrl(reader->coverImagePath()));
|
||||
}
|
||||
|
||||
m_readingModel.setReader(reader);
|
||||
|
||||
m_uiState.setLastFilePath(filePath);
|
||||
m_historyModel.addOrUpdateBook(filePath, -1.0);
|
||||
}
|
||||
|
||||
// Quit: flush everything to persistent storage.
|
||||
// Scroll position was last saved by PageView's scroll tracker; we save library progress here.
|
||||
void AppCoordinator::onAboutToQuit()
|
||||
{
|
||||
if (m_book.hasBook()) {
|
||||
@@ -96,18 +74,9 @@ void AppCoordinator::onAboutToQuit()
|
||||
m_uiState.setLastFilePath(QString());
|
||||
}
|
||||
|
||||
m_historyModel.save(m_appState);
|
||||
m_readingPreferences.save();
|
||||
m_uiState.save();
|
||||
|
||||
QList<QVariantMap> items;
|
||||
for (int i = 0; i < m_historyModel.rowCount(); ++i) {
|
||||
items.append({
|
||||
{QStringLiteral("filePath"), m_historyModel.filePath(i)},
|
||||
{QStringLiteral("progress"), m_historyModel.progress(i)},
|
||||
{QStringLiteral("lastOpened"), m_historyModel.lastOpened(i)},
|
||||
});
|
||||
}
|
||||
m_appState.saveArray(QStringLiteral("history"), items);
|
||||
|
||||
qInfo() << "Settings saved to:" << m_settings.fileName();
|
||||
}
|
||||
@@ -4,19 +4,14 @@
|
||||
|
||||
class AppState;
|
||||
class Book;
|
||||
class FormatRegistry;
|
||||
class HistoryModel;
|
||||
class IImageProvider;
|
||||
class QQmlApplicationEngine;
|
||||
class QSettings;
|
||||
class ReadingModel;
|
||||
class ReadingPreferences;
|
||||
class UiState;
|
||||
|
||||
// Wires the top-level objects together: reacts to book changes by swapping
|
||||
// image providers and configuring the reading model, keeps history and
|
||||
// persistent state in sync, and flushes everything on quit. main() only
|
||||
// constructs the object graph; the coordination lives here.
|
||||
// Wires the top-level objects together: reacts to book changes,
|
||||
// keeps history and persistent state in sync, and flushes everything on quit.
|
||||
class AppCoordinator : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
@@ -27,8 +22,6 @@ public:
|
||||
ReadingPreferences &readingPreferences,
|
||||
AppState &appState,
|
||||
QSettings &settings,
|
||||
FormatRegistry &formatRegistry,
|
||||
QQmlApplicationEngine &engine,
|
||||
QObject *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
@@ -45,8 +38,4 @@ private:
|
||||
ReadingPreferences &m_readingPreferences;
|
||||
AppState &m_appState;
|
||||
QSettings &m_settings;
|
||||
FormatRegistry &m_formatRegistry;
|
||||
QQmlApplicationEngine &m_engine;
|
||||
|
||||
IImageProvider *m_currentImageProvider = nullptr;
|
||||
};
|
||||
+36
-12
@@ -4,31 +4,55 @@
|
||||
#include <QByteArray>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QVector>
|
||||
|
||||
enum class ReadingMode {
|
||||
Comic, // Paged / paired image mode (CBZ, CBR, Folder)
|
||||
Text // Continuous / chaptered text mode (EPUB, FB2, TXT)
|
||||
};
|
||||
|
||||
struct PageInfo {
|
||||
QString imageUrl;
|
||||
QSize naturalSize;
|
||||
};
|
||||
|
||||
struct TocEntry {
|
||||
QString title;
|
||||
int chapterIndex = 0;
|
||||
int depth = 0;
|
||||
};
|
||||
|
||||
struct Chapter {
|
||||
int index = 0;
|
||||
QString title;
|
||||
QStringList chunks; // Standalone HTML/text fragments for virtualized rendering
|
||||
};
|
||||
|
||||
class FormatReader {
|
||||
public:
|
||||
virtual ~FormatReader() = default;
|
||||
|
||||
virtual ReadingMode mode() const = 0;
|
||||
virtual bool open(const QString &filePath) = 0;
|
||||
virtual bool loadMetadata(const QString &filePath) = 0;
|
||||
virtual QString lastError() = 0;
|
||||
virtual QString filePath() = 0;
|
||||
virtual QString title() = 0;
|
||||
virtual QString author() = 0;
|
||||
virtual QString coverImagePath() = 0;
|
||||
// MD5 of file contents, computed once during open(). Used as a stable key for
|
||||
// per-book state (bookmarks, etc.) that survives file renames.
|
||||
virtual QByteArray contentHash() = 0;
|
||||
// Returns the lowercase file extension this reader handles (e.g. "cbz", "cbr").
|
||||
virtual QString fileType() const = 0;
|
||||
virtual bool loadMetadata(const QString &filePath) { return open(filePath); }
|
||||
virtual QString lastError() const = 0;
|
||||
|
||||
virtual QVector<PageInfo> pages() const = 0;
|
||||
virtual QString filePath() const = 0;
|
||||
virtual QString title() const = 0;
|
||||
virtual QString author() const { return {}; }
|
||||
virtual QString coverImageUrl() const { return {}; }
|
||||
// MD5 of file contents, computed once during open(). Used as a stable key for
|
||||
// per-book state (bookmarks, position, etc.) that survives file renames.
|
||||
virtual QByteArray contentHash() const = 0;
|
||||
|
||||
// Comic mode (default empty for text readers)
|
||||
virtual QVector<PageInfo> pages() const { return {}; }
|
||||
|
||||
// Text mode (default empty for comic readers)
|
||||
virtual int chapterCount() const { return 0; }
|
||||
virtual Chapter loadChapter(int index) const { Q_UNUSED(index); return {}; }
|
||||
virtual QVector<TocEntry> tableOfContents() const { return {}; }
|
||||
};
|
||||
|
||||
#endif // FORMATREADER_H
|
||||
@@ -1,120 +0,0 @@
|
||||
#include "HistoryBookModel.h"
|
||||
#include "FormatReader.h"
|
||||
#include "IImageProvider.h"
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
#include <QFileInfo>
|
||||
#include <QUrl>
|
||||
|
||||
HistoryBookModel::HistoryBookModel(const QString &filePath, double progress, const QString &lastOpened, FormatRegistry ®istry, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_filePath(filePath)
|
||||
, m_progress(progress)
|
||||
, m_lastOpened(lastOpened)
|
||||
, m_registry(registry)
|
||||
{
|
||||
}
|
||||
|
||||
HistoryBookModel::~HistoryBookModel()
|
||||
{
|
||||
if (m_metadataFuture.isRunning())
|
||||
m_metadataFuture.waitForFinished();
|
||||
}
|
||||
|
||||
QString HistoryBookModel::filePath() const
|
||||
{
|
||||
return m_filePath;
|
||||
}
|
||||
|
||||
double HistoryBookModel::progress() const
|
||||
{
|
||||
return m_progress;
|
||||
}
|
||||
|
||||
void HistoryBookModel::setProgress(double progress)
|
||||
{
|
||||
if (qFuzzyCompare(m_progress, progress))
|
||||
return;
|
||||
m_progress = progress;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
QString HistoryBookModel::lastOpened() const
|
||||
{
|
||||
return m_lastOpened;
|
||||
}
|
||||
|
||||
void HistoryBookModel::setLastOpened(const QString &lastOpened)
|
||||
{
|
||||
if (m_lastOpened == lastOpened)
|
||||
return;
|
||||
m_lastOpened = lastOpened;
|
||||
emit lastOpenedChanged();
|
||||
}
|
||||
|
||||
QString HistoryBookModel::title() const
|
||||
{
|
||||
return m_title;
|
||||
}
|
||||
|
||||
QString HistoryBookModel::author() const
|
||||
{
|
||||
return m_author;
|
||||
}
|
||||
|
||||
QString HistoryBookModel::format() const
|
||||
{
|
||||
if (QFileInfo(m_filePath).isDir())
|
||||
return QStringLiteral("Folder");
|
||||
return m_filePath.section('.', -1).toUpper();
|
||||
}
|
||||
|
||||
QString HistoryBookModel::coverImageUrl() const
|
||||
{
|
||||
return m_coverImageUrl;
|
||||
}
|
||||
|
||||
void HistoryBookModel::loadMetadata()
|
||||
{
|
||||
if (m_metadataLoaded || m_filePath.isEmpty() || m_metadataFuture.isRunning())
|
||||
return;
|
||||
|
||||
QPointer<HistoryBookModel> self = this;
|
||||
QString filePath = m_filePath;
|
||||
|
||||
FormatRegistry *registry = &m_registry;
|
||||
m_metadataFuture = QtConcurrent::run([self, filePath, registry]() {
|
||||
auto reader = registry->createReader(filePath);
|
||||
QString title;
|
||||
QString author;
|
||||
bool success = false;
|
||||
|
||||
if (reader && reader->loadMetadata(filePath)) {
|
||||
title = reader->title();
|
||||
author = reader->author();
|
||||
success = true;
|
||||
}
|
||||
|
||||
QString coverImageUrl;
|
||||
if (success) {
|
||||
QString coverPath = reader->coverImagePath();
|
||||
if (!coverPath.isEmpty()) {
|
||||
std::unique_ptr<IImageProvider> provider(registry->createImageProvider(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->coverImageLoaded();
|
||||
emit self->metadataLoaded();
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
#ifndef HISTORYBOOKMODEL_H
|
||||
#define HISTORYBOOKMODEL_H
|
||||
|
||||
#include <QString>
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
#include <QFuture>
|
||||
|
||||
#include "formats/FormatRegistry.h"
|
||||
|
||||
class FormatReader;
|
||||
|
||||
class HistoryBookModel : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString filePath READ filePath CONSTANT)
|
||||
Q_PROPERTY(double progress READ progress WRITE setProgress NOTIFY progressChanged)
|
||||
Q_PROPERTY(QString lastOpened READ lastOpened WRITE setLastOpened NOTIFY lastOpenedChanged)
|
||||
Q_PROPERTY(QString title READ title NOTIFY metadataLoaded)
|
||||
Q_PROPERTY(QString author READ author NOTIFY metadataLoaded)
|
||||
Q_PROPERTY(QString format READ format NOTIFY metadataLoaded)
|
||||
Q_PROPERTY(QString coverImageUrl READ coverImageUrl NOTIFY coverImageLoaded)
|
||||
|
||||
public:
|
||||
explicit HistoryBookModel(const QString &filePath, double progress, const QString &lastOpened, FormatRegistry ®istry, QObject *parent = nullptr);
|
||||
~HistoryBookModel() override;
|
||||
|
||||
QString filePath() const;
|
||||
double progress() const;
|
||||
void setProgress(double progress);
|
||||
|
||||
QString lastOpened() const;
|
||||
void setLastOpened(const QString &lastOpened);
|
||||
|
||||
QString title() const;
|
||||
QString author() const;
|
||||
QString format() const;
|
||||
QString coverImageUrl() const;
|
||||
|
||||
void loadMetadata();
|
||||
|
||||
signals:
|
||||
void progressChanged();
|
||||
void lastOpenedChanged();
|
||||
void metadataLoaded();
|
||||
void coverImageLoaded();
|
||||
|
||||
private:
|
||||
QString m_filePath;
|
||||
double m_progress = 0.0;
|
||||
QString m_lastOpened;
|
||||
QString m_title;
|
||||
QString m_author;
|
||||
QString m_coverImageUrl;
|
||||
bool m_metadataLoaded = false;
|
||||
QFuture<void> m_metadataFuture;
|
||||
FormatRegistry &m_registry;
|
||||
};
|
||||
|
||||
#endif // HISTORYBOOKMODEL_H
|
||||
+163
-122
@@ -1,100 +1,133 @@
|
||||
#include "HistoryModel.h"
|
||||
#include "HistoryBookModel.h"
|
||||
#include "AppState.h"
|
||||
#include "FormatReader.h"
|
||||
#include "formats/FormatRegistry.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QFileInfo>
|
||||
#include <QPointer>
|
||||
#include <QtConcurrent>
|
||||
|
||||
namespace {
|
||||
QString formatForPath(const QString &filePath)
|
||||
{
|
||||
if (QFileInfo(filePath).isDir())
|
||||
return QStringLiteral("Folder");
|
||||
return filePath.section('.', -1).toUpper();
|
||||
}
|
||||
}
|
||||
|
||||
HistoryModel::HistoryModel(FormatRegistry ®istry, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_registry(registry)
|
||||
{
|
||||
// Initialize role names
|
||||
m_roleNames[FilePathRole] = "filePath";
|
||||
m_roleNames[TitleRole] = "title";
|
||||
m_roleNames[AuthorRole] = "author";
|
||||
m_roleNames[FormatRole] = "format";
|
||||
m_roleNames[ProgressRole] = "progress";
|
||||
m_roleNames[LastOpenedRole] = "lastOpened";
|
||||
m_roleNames[CoverImageUrlRole] = "coverImageUrl";
|
||||
}
|
||||
|
||||
HistoryModel::~HistoryModel()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
HistoryModel::~HistoryModel() = default;
|
||||
|
||||
int HistoryModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
return m_books.count();
|
||||
return m_entries.size();
|
||||
}
|
||||
|
||||
QVariant HistoryModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
||||
return QVariant();
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
|
||||
return {};
|
||||
|
||||
HistoryBookModel* book = m_books.at(index.row());
|
||||
if (!book)
|
||||
return QVariant();
|
||||
|
||||
const auto &entry = m_entries.at(index.row());
|
||||
switch (role) {
|
||||
case FilePathRole:
|
||||
return book->filePath();
|
||||
case TitleRole:
|
||||
return book->title();
|
||||
case AuthorRole:
|
||||
return book->author();
|
||||
case FormatRole:
|
||||
return book->format();
|
||||
case ProgressRole:
|
||||
return book->progress();
|
||||
case LastOpenedRole:
|
||||
return book->lastOpened();
|
||||
case CoverImageUrlRole:
|
||||
return book->coverImageUrl();
|
||||
default:
|
||||
return QVariant();
|
||||
case FilePathRole:
|
||||
return entry.filePath;
|
||||
case TitleRole:
|
||||
return entry.title;
|
||||
case AuthorRole:
|
||||
return entry.author;
|
||||
case FormatRole:
|
||||
return entry.format;
|
||||
case ProgressRole:
|
||||
return entry.progress;
|
||||
case LastOpenedRole:
|
||||
return entry.lastOpened;
|
||||
case CoverImageUrlRole:
|
||||
return entry.coverImageUrl;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool HistoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
|
||||
return false;
|
||||
|
||||
HistoryBookModel* book = m_books.at(index.row());
|
||||
|
||||
auto &entry = m_entries[index.row()];
|
||||
switch (role) {
|
||||
case ProgressRole:
|
||||
book->setProgress(value.toDouble());
|
||||
emit dataChanged(index, index, {ProgressRole});
|
||||
return true;
|
||||
case LastOpenedRole:
|
||||
book->setLastOpened(value.toString());
|
||||
emit dataChanged(index, index, {LastOpenedRole});
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
case ProgressRole:
|
||||
entry.progress = value.toDouble();
|
||||
emit dataChanged(index, index, {ProgressRole});
|
||||
return true;
|
||||
case LastOpenedRole:
|
||||
entry.lastOpened = value.toString();
|
||||
emit dataChanged(index, index, {LastOpenedRole});
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> HistoryModel::roleNames() const
|
||||
{
|
||||
return m_roleNames;
|
||||
return {
|
||||
{FilePathRole, "filePath"},
|
||||
{TitleRole, "title"},
|
||||
{AuthorRole, "author"},
|
||||
{FormatRole, "format"},
|
||||
{ProgressRole, "progress"},
|
||||
{LastOpenedRole, "lastOpened"},
|
||||
{CoverImageUrlRole, "coverImageUrl"}
|
||||
};
|
||||
}
|
||||
|
||||
void HistoryModel::load(AppState &state)
|
||||
{
|
||||
clear();
|
||||
for (const QVariantMap &item : state.loadArray(QStringLiteral("history"))) {
|
||||
const QString fp = item.value(QStringLiteral("filePath")).toString();
|
||||
if (fp.isEmpty()) continue;
|
||||
double progress = item.value(QStringLiteral("progress"), 0.0).toDouble();
|
||||
progress = qBound(0.0, progress, 1.0);
|
||||
const QString lo = item.value(QStringLiteral("lastOpened")).toString();
|
||||
addOrUpdateBook(fp, progress, lo);
|
||||
}
|
||||
}
|
||||
|
||||
void HistoryModel::save(AppState &state) const
|
||||
{
|
||||
QList<QVariantMap> items;
|
||||
items.reserve(m_entries.size());
|
||||
for (const auto &entry : m_entries) {
|
||||
items.append({
|
||||
{QStringLiteral("filePath"), entry.filePath},
|
||||
{QStringLiteral("progress"), entry.progress},
|
||||
{QStringLiteral("lastOpened"), entry.lastOpened},
|
||||
});
|
||||
}
|
||||
state.saveArray(QStringLiteral("history"), items);
|
||||
}
|
||||
|
||||
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened)
|
||||
{
|
||||
// Check if book already exists
|
||||
for (int i = 0; i < m_books.size(); i++) {
|
||||
if (m_books[i]->filePath() == filePath) {
|
||||
HistoryBookModel* existingBook = m_books[i];
|
||||
existingBook->setProgress(progress);
|
||||
existingBook->setLastOpened(lastOpened);
|
||||
for (int i = 0; i < m_entries.size(); ++i) {
|
||||
if (m_entries[i].filePath == filePath) {
|
||||
m_entries[i].progress = progress;
|
||||
m_entries[i].lastOpened = lastOpened;
|
||||
|
||||
if (i != 0) {
|
||||
beginMoveRows(QModelIndex(), i, i, QModelIndex(), 0);
|
||||
m_books.move(i, 0);
|
||||
m_entries.move(i, 0);
|
||||
endMoveRows();
|
||||
}
|
||||
emit dataChanged(createIndex(0, 0), createIndex(0, 0), {ProgressRole, LastOpenedRole});
|
||||
@@ -102,106 +135,114 @@ void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, con
|
||||
}
|
||||
}
|
||||
|
||||
// New book — HistoryModel takes ownership via parent
|
||||
auto* book = new HistoryBookModel(filePath, progress, lastOpened, m_registry, this);
|
||||
connect(book, &HistoryBookModel::metadataLoaded, this, &HistoryModel::onBookMetadataLoaded);
|
||||
HistoryEntry entry;
|
||||
entry.filePath = filePath;
|
||||
entry.progress = progress;
|
||||
entry.lastOpened = lastOpened;
|
||||
entry.format = formatForPath(filePath);
|
||||
|
||||
beginInsertRows(QModelIndex(), 0, 0);
|
||||
m_books.prepend(book);
|
||||
m_entries.prepend(entry);
|
||||
endInsertRows();
|
||||
|
||||
emit countChanged();
|
||||
book->loadMetadata();
|
||||
|
||||
requestMetadata(filePath);
|
||||
}
|
||||
|
||||
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress)
|
||||
{
|
||||
double resolvedProgress = progress;
|
||||
if (progress < 0.0) {
|
||||
for (const auto &entry : m_entries) {
|
||||
if (entry.filePath == filePath) {
|
||||
resolvedProgress = entry.progress;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (resolvedProgress < 0.0)
|
||||
resolvedProgress = 0.0;
|
||||
}
|
||||
addOrUpdateBook(filePath, resolvedProgress,
|
||||
QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
}
|
||||
|
||||
void HistoryModel::removeBook(const QString &filePath)
|
||||
{
|
||||
for (int i = 0; i < m_books.size(); i++) {
|
||||
if (m_books[i]->filePath() == filePath) {
|
||||
HistoryBookModel* book = m_books[i];
|
||||
|
||||
for (int i = 0; i < m_entries.size(); ++i) {
|
||||
if (m_entries[i].filePath == filePath) {
|
||||
beginRemoveRows(QModelIndex(), i, i);
|
||||
m_books.removeAt(i);
|
||||
m_entries.removeAt(i);
|
||||
endRemoveRows();
|
||||
|
||||
book->deleteLater();
|
||||
emit countChanged();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HistoryBookModel* HistoryModel::findBook(const QString &filePath) const
|
||||
{
|
||||
for (HistoryBookModel* book : m_books) {
|
||||
if (book->filePath() == filePath) {
|
||||
return book;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void HistoryModel::clear()
|
||||
{
|
||||
if (m_books.isEmpty())
|
||||
if (m_entries.isEmpty())
|
||||
return;
|
||||
|
||||
beginRemoveRows(QModelIndex(), 0, m_books.size() - 1);
|
||||
|
||||
for (HistoryBookModel* book : m_books)
|
||||
book->deleteLater();
|
||||
|
||||
m_books.clear();
|
||||
beginRemoveRows(QModelIndex(), 0, m_entries.size() - 1);
|
||||
m_entries.clear();
|
||||
endRemoveRows();
|
||||
|
||||
emit countChanged();
|
||||
}
|
||||
|
||||
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress)
|
||||
{
|
||||
// Sentinel: progress < 0 means "preserve the existing progress value"
|
||||
double resolvedProgress = progress;
|
||||
if (progress < 0.0) {
|
||||
HistoryBookModel *existing = findBook(filePath);
|
||||
resolvedProgress = existing ? existing->progress() : 0.0;
|
||||
}
|
||||
addOrUpdateBook(filePath, resolvedProgress,
|
||||
QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
}
|
||||
|
||||
QString HistoryModel::filePath(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return {};
|
||||
return m_books.at(index)->filePath();
|
||||
if (index < 0 || index >= m_entries.size()) return {};
|
||||
return m_entries.at(index).filePath;
|
||||
}
|
||||
|
||||
double HistoryModel::progress(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return 0.0;
|
||||
return m_books.at(index)->progress();
|
||||
if (index < 0 || index >= m_entries.size()) return 0.0;
|
||||
return m_entries.at(index).progress;
|
||||
}
|
||||
|
||||
QString HistoryModel::lastOpened(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return {};
|
||||
return m_books.at(index)->lastOpened();
|
||||
if (index < 0 || index >= m_entries.size()) return {};
|
||||
return m_entries.at(index).lastOpened;
|
||||
}
|
||||
|
||||
QList<HistoryBookModel*> HistoryModel::books() const
|
||||
void HistoryModel::requestMetadata(const QString &filePath)
|
||||
{
|
||||
return m_books;
|
||||
FormatRegistry *registry = &m_registry;
|
||||
QPointer<HistoryModel> self(this);
|
||||
|
||||
(void)QtConcurrent::run([self, filePath, registry]() {
|
||||
auto reader = registry->createReader(filePath);
|
||||
QString title;
|
||||
QString author;
|
||||
QString coverUrl;
|
||||
bool ok = false;
|
||||
|
||||
if (reader && reader->loadMetadata(filePath)) {
|
||||
title = reader->title();
|
||||
author = reader->author();
|
||||
coverUrl = reader->coverImageUrl();
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (!self)
|
||||
return;
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, filePath, title, author, coverUrl, ok]() {
|
||||
if (!self || !ok) return;
|
||||
for (int i = 0; i < self->m_entries.size(); ++i) {
|
||||
if (self->m_entries[i].filePath == filePath) {
|
||||
self->m_entries[i].title = title;
|
||||
self->m_entries[i].author = author;
|
||||
self->m_entries[i].coverImageUrl = coverUrl;
|
||||
self->m_entries[i].metadataLoaded = true;
|
||||
const QModelIndex idx = self->createIndex(i, 0);
|
||||
emit self->dataChanged(idx, idx, {TitleRole, AuthorRole, CoverImageUrlRole});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
}
|
||||
|
||||
void HistoryModel::onBookMetadataLoaded()
|
||||
{
|
||||
HistoryBookModel* book = qobject_cast<HistoryBookModel*>(sender());
|
||||
if (!book)
|
||||
return;
|
||||
|
||||
int row = m_books.indexOf(book);
|
||||
if (row >= 0) {
|
||||
QModelIndex index = this->index(row);
|
||||
// Update all metadata-related roles when metadata is loaded
|
||||
emit dataChanged(index, index, {TitleRole, AuthorRole, CoverImageUrlRole});
|
||||
}
|
||||
}
|
||||
+22
-19
@@ -2,12 +2,23 @@
|
||||
#define HISTORYMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QList>
|
||||
#include <QHash>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
#include "formats/FormatRegistry.h"
|
||||
class AppState;
|
||||
class FormatRegistry;
|
||||
|
||||
class HistoryBookModel;
|
||||
struct HistoryEntry {
|
||||
QString filePath;
|
||||
double progress = 0.0;
|
||||
QString lastOpened;
|
||||
QString title;
|
||||
QString author;
|
||||
QString format;
|
||||
QString coverImageUrl;
|
||||
bool metadataLoaded = false;
|
||||
};
|
||||
|
||||
class HistoryModel : public QAbstractListModel
|
||||
{
|
||||
@@ -29,40 +40,32 @@ public:
|
||||
explicit HistoryModel(FormatRegistry ®istry, QObject *parent = nullptr);
|
||||
~HistoryModel() override;
|
||||
|
||||
// QAbstractItemModel interface
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;
|
||||
|
||||
// Add QHash<int, QByteArray> for role names
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
// Methods to manage books
|
||||
// Persistence with AppState
|
||||
void load(AppState &state);
|
||||
void save(AppState &state) const;
|
||||
|
||||
Q_INVOKABLE void addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened);
|
||||
// Convenience overload: sentinel progress < 0 preserves the existing progress value.
|
||||
Q_INVOKABLE void addOrUpdateBook(const QString &filePath, double progress);
|
||||
Q_INVOKABLE void removeBook(const QString &filePath);
|
||||
Q_INVOKABLE HistoryBookModel* findBook(const QString &filePath) const;
|
||||
Q_INVOKABLE void clear();
|
||||
|
||||
// Indexed accessors for persistence (e.g., main.cpp's aboutToQuit loop).
|
||||
QString filePath(int index) const;
|
||||
double progress(int index) const;
|
||||
QString lastOpened(int index) const;
|
||||
|
||||
// Access to books for saving/loading
|
||||
QList<HistoryBookModel*> books() const;
|
||||
|
||||
signals:
|
||||
void countChanged();
|
||||
|
||||
private slots:
|
||||
void onBookMetadataLoaded();
|
||||
|
||||
private:
|
||||
QList<HistoryBookModel*> m_books;
|
||||
QHash<int, QByteArray> m_roleNames;
|
||||
void requestMetadata(const QString &filePath);
|
||||
|
||||
QList<HistoryEntry> m_entries;
|
||||
FormatRegistry &m_registry;
|
||||
};
|
||||
|
||||
#endif // HISTORYMODEL_H
|
||||
#endif // HISTORYMODEL_H
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
#include <QQuickAsyncImageProvider>
|
||||
#include <QString>
|
||||
|
||||
class IImageProvider : public QQuickAsyncImageProvider {
|
||||
public:
|
||||
virtual ~IImageProvider() = default;
|
||||
virtual void setFilePath(const QString &filePath) = 0;
|
||||
virtual void clear() = 0;
|
||||
// Returns the full QML image URL for a given internal path (e.g., "image://epub/OEBPS/cover.jpg")
|
||||
virtual QString coverImageUrl(const QString &coverPath) const = 0;
|
||||
// The scheme this provider is registered under with the QML engine (e.g., "epub")
|
||||
virtual QString scheme() const = 0;
|
||||
};
|
||||
+127
-15
@@ -2,20 +2,45 @@
|
||||
#include <algorithm>
|
||||
|
||||
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
||||
: QObject(parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_positions(positions)
|
||||
{
|
||||
}
|
||||
|
||||
ReadingModel::~ReadingModel() = default;
|
||||
|
||||
int ReadingModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid() || !m_isTextMode)
|
||||
return 0;
|
||||
return m_currentChapterData.chunks.size();
|
||||
}
|
||||
|
||||
QVariant ReadingModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || !m_isTextMode || index.row() < 0 || index.row() >= m_currentChapterData.chunks.size())
|
||||
return {};
|
||||
if (role == ContentRole)
|
||||
return m_currentChapterData.chunks.at(index.row());
|
||||
return {};
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ReadingModel::roleNames() const
|
||||
{
|
||||
return {{ContentRole, "content"}};
|
||||
}
|
||||
|
||||
bool ReadingModel::isTextMode() const { return m_isTextMode; }
|
||||
int ReadingModel::totalPagePairs() const { return m_pagination.count(); }
|
||||
bool ReadingModel::pageShift() const { return m_pageShift; }
|
||||
bool ReadingModel::mangaRTL() const { return m_mangaRTL; }
|
||||
int ReadingModel::currentChapter() const { return m_currentChapter; }
|
||||
int ReadingModel::chapterCount() const { return m_chapterCount; }
|
||||
QString ReadingModel::chapterTitle() const { return m_currentChapterData.title; }
|
||||
|
||||
double ReadingModel::progress() const
|
||||
{
|
||||
return qBound(0.0, m_yOffset, 1.0);
|
||||
return qBound(0.0, m_progress, 1.0);
|
||||
}
|
||||
|
||||
int ReadingModel::currentPage() const { return m_currentPage; }
|
||||
@@ -23,12 +48,74 @@ int ReadingModel::totalPages() const { return m_totalPages; }
|
||||
|
||||
void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion)
|
||||
{
|
||||
m_yOffset = proportion;
|
||||
const qreal clamped = qBound(0.0, proportion, 1.0);
|
||||
if (!qFuzzyCompare(m_progress, clamped)) {
|
||||
m_progress = clamped;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
const int page = pairIndexForChunk(topChunkIndex) + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
if (m_isTextMode) {
|
||||
const int page = topChunkIndex + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
} else {
|
||||
const int page = pairIndexForChunk(topChunkIndex) + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
}
|
||||
|
||||
saveCurrentPosition(topChunkIndex);
|
||||
}
|
||||
|
||||
void ReadingModel::saveCurrentPosition(int chunkIndex)
|
||||
{
|
||||
if (!m_reader) return;
|
||||
BookPosition pos;
|
||||
pos.chapterIndex = m_currentChapter;
|
||||
pos.chunkIndex = chunkIndex;
|
||||
pos.progress = m_progress;
|
||||
pos.pageShift = m_pageShift;
|
||||
pos.mangaRTL = m_mangaRTL;
|
||||
m_positions.save(m_reader->contentHash(), pos);
|
||||
emit positionSaved();
|
||||
}
|
||||
|
||||
void ReadingModel::loadChapter(int index)
|
||||
{
|
||||
if (!m_reader || !m_isTextMode)
|
||||
return;
|
||||
if (index < 0 || index >= m_chapterCount)
|
||||
return;
|
||||
|
||||
beginResetModel();
|
||||
m_currentChapter = index;
|
||||
m_currentChapterData = m_reader->loadChapter(index);
|
||||
m_totalPages = m_currentChapterData.chunks.size();
|
||||
m_currentPage = qMin(1, m_totalPages);
|
||||
endResetModel();
|
||||
|
||||
emit currentChapterChanged();
|
||||
emit totalPagesChanged();
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::nextChapter()
|
||||
{
|
||||
if (m_isTextMode && m_currentChapter + 1 < m_chapterCount) {
|
||||
loadChapter(m_currentChapter + 1);
|
||||
saveCurrentPosition(0);
|
||||
}
|
||||
}
|
||||
|
||||
void ReadingModel::previousChapter()
|
||||
{
|
||||
if (m_isTextMode && m_currentChapter > 0) {
|
||||
loadChapter(m_currentChapter - 1);
|
||||
saveCurrentPosition(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,8 +195,7 @@ void ReadingModel::setPageShift(bool shift, int currentPairIndex)
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
if (m_reader)
|
||||
m_positions.save(m_reader->contentHash(), currentChunk, m_mangaRTL, m_pageShift);
|
||||
saveCurrentPosition(currentChunk);
|
||||
|
||||
emit pageShiftChanged();
|
||||
emit pagePairsRebuilt(newPair);
|
||||
@@ -121,7 +207,7 @@ void ReadingModel::setMangaRTL(bool rtl)
|
||||
m_mangaRTL = rtl;
|
||||
if (m_reader) {
|
||||
const int currentChunk = (m_currentPage > 0) ? m_pagination.at(m_currentPage - 1).first : 0;
|
||||
m_positions.save(m_reader->contentHash(), currentChunk >= 0 ? currentChunk : 0, m_mangaRTL, m_pageShift);
|
||||
saveCurrentPosition(currentChunk >= 0 ? currentChunk : 0);
|
||||
}
|
||||
emit mangaRTLChanged();
|
||||
}
|
||||
@@ -132,7 +218,7 @@ void ReadingModel::loadPages()
|
||||
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
|
||||
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
|
||||
|
||||
if (m_reader) {
|
||||
if (m_reader && !m_isTextMode) {
|
||||
m_pages = m_reader->pages();
|
||||
}
|
||||
|
||||
@@ -143,18 +229,44 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
{
|
||||
m_reader = reader;
|
||||
|
||||
beginResetModel();
|
||||
m_currentChapterData = {};
|
||||
endResetModel();
|
||||
|
||||
const bool oldTextMode = m_isTextMode;
|
||||
m_isTextMode = (reader && reader->mode() == ReadingMode::Text);
|
||||
if (oldTextMode != m_isTextMode)
|
||||
emit modeChanged();
|
||||
|
||||
int restoreChunk = 0;
|
||||
int restoreChapter = 0;
|
||||
|
||||
if (reader) {
|
||||
const auto pos = m_positions.load(reader->contentHash());
|
||||
restoreChunk = pos.chunkIndex;
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
restoreChunk = pos.chunkIndex;
|
||||
restoreChapter = pos.chapterIndex;
|
||||
m_progress = pos.progress;
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
emit progressChanged();
|
||||
} else {
|
||||
m_progress = 0.0;
|
||||
m_pageShift = true;
|
||||
m_mangaRTL = true;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
loadPages();
|
||||
if (m_isTextMode && m_reader) {
|
||||
m_chapterCount = m_reader->chapterCount();
|
||||
emit chapterCountChanged();
|
||||
loadChapter(qBound(0, restoreChapter, qMax(0, m_chapterCount - 1)));
|
||||
} else {
|
||||
m_chapterCount = 0;
|
||||
m_currentChapter = 0;
|
||||
emit chapterCountChanged();
|
||||
emit currentChapterChanged();
|
||||
loadPages();
|
||||
}
|
||||
|
||||
if (m_reader) {
|
||||
QMetaObject::invokeMethod(this, [this, restoreChunk] {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef READINGMODEL_H
|
||||
#define READINGMODEL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractListModel>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
#include <QVariantList>
|
||||
@@ -10,31 +10,53 @@
|
||||
#include "reading/MangaPagination.h"
|
||||
#include "state/BookPositionStore.h"
|
||||
|
||||
class ReadingModel : public QObject {
|
||||
class ReadingModel : public QAbstractListModel {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(double progress READ progress NOTIFY currentPageChanged)
|
||||
Q_PROPERTY(bool isTextMode READ isTextMode NOTIFY modeChanged)
|
||||
Q_PROPERTY(double progress READ progress NOTIFY progressChanged)
|
||||
Q_PROPERTY(int currentPage READ currentPage NOTIFY currentPageChanged)
|
||||
Q_PROPERTY(int totalPages READ totalPages NOTIFY totalPagesChanged)
|
||||
Q_PROPERTY(int currentChapter READ currentChapter NOTIFY currentChapterChanged)
|
||||
Q_PROPERTY(int chapterCount READ chapterCount NOTIFY chapterCountChanged)
|
||||
Q_PROPERTY(QString chapterTitle READ chapterTitle NOTIFY currentChapterChanged)
|
||||
Q_PROPERTY(int totalPagePairs READ totalPagePairs NOTIFY totalPagePairsChanged)
|
||||
Q_PROPERTY(bool pageShift READ pageShift NOTIFY pageShiftChanged)
|
||||
Q_PROPERTY(bool mangaRTL READ mangaRTL NOTIFY mangaRTLChanged)
|
||||
|
||||
public:
|
||||
enum ReadingRoles {
|
||||
ContentRole = Qt::UserRole + 1
|
||||
};
|
||||
Q_ENUM(ReadingRoles)
|
||||
|
||||
explicit ReadingModel(BookPositionStore &positions, QObject *parent = nullptr);
|
||||
~ReadingModel() override;
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
bool isTextMode() const;
|
||||
double progress() const;
|
||||
int currentPage() const;
|
||||
int totalPages() const;
|
||||
int currentChapter() const;
|
||||
int chapterCount() const;
|
||||
QString chapterTitle() const;
|
||||
int totalPagePairs() const;
|
||||
bool pageShift() const;
|
||||
bool mangaRTL() const;
|
||||
|
||||
void setReader(FormatReader *reader);
|
||||
|
||||
// Called from QML on every page navigation
|
||||
// Called on navigation
|
||||
Q_INVOKABLE void setScrollInfo(int topChunkIndex, qreal proportion);
|
||||
|
||||
// Text chapter navigation
|
||||
Q_INVOKABLE void loadChapter(int index);
|
||||
Q_INVOKABLE void nextChapter();
|
||||
Q_INVOKABLE void previousChapter();
|
||||
|
||||
// Manga page-pair API
|
||||
Q_INVOKABLE QVariantList pagePair(int pairIndex) const;
|
||||
Q_INVOKABLE int pairIndexForChunk(int chunkIndex) const;
|
||||
@@ -43,36 +65,44 @@ public:
|
||||
// Called from MangaPageView when window orientation changes
|
||||
Q_INVOKABLE void setMangaLandscape(bool landscape);
|
||||
|
||||
// Per-book page-shift toggle: when ON, page 1 is shown alone after the cover.
|
||||
// currentPairIndex must be the pair the view is currently displaying.
|
||||
// Per-book page-shift toggle
|
||||
Q_INVOKABLE void setPageShift(bool shift, int currentPairIndex = 0);
|
||||
Q_INVOKABLE void setMangaRTL(bool rtl);
|
||||
|
||||
signals:
|
||||
void modeChanged();
|
||||
void progressChanged();
|
||||
void currentPageChanged();
|
||||
void totalPagesChanged();
|
||||
void currentChapterChanged();
|
||||
void chapterCountChanged();
|
||||
void totalPagePairsChanged();
|
||||
void pageShiftChanged();
|
||||
void mangaRTLChanged();
|
||||
// Fired after setPageShift rebuilds pairs; carries the new pair index to display.
|
||||
void pagePairsRebuilt(int newPairIndex);
|
||||
|
||||
void restoreReady(int chunkIndex);
|
||||
void positionSaved();
|
||||
|
||||
private:
|
||||
void loadPages();
|
||||
void rebuildPagePairs();
|
||||
void saveCurrentPosition(int chunkIndex);
|
||||
|
||||
FormatReader *m_reader = nullptr;
|
||||
bool m_isTextMode = false;
|
||||
int m_currentPage = 0;
|
||||
int m_totalPages = 0;
|
||||
int m_currentChapter = 0;
|
||||
int m_chapterCount = 0;
|
||||
Chapter m_currentChapterData;
|
||||
|
||||
bool m_mangaLandscape = false;
|
||||
bool m_pageShift = true;
|
||||
bool m_mangaRTL = true;
|
||||
|
||||
QVector<PageInfo> m_pages;
|
||||
MangaPagination m_pagination;
|
||||
qreal m_yOffset = 0.0;
|
||||
qreal m_progress = 0.0;
|
||||
|
||||
BookPositionStore &m_positions;
|
||||
};
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QStringList>
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
namespace ZipUtils {
|
||||
|
||||
// Read a single file from an open unzFile handle by name.
|
||||
// Returns empty QByteArray on failure.
|
||||
inline 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{};
|
||||
}
|
||||
|
||||
// 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
|
||||
+18
-19
@@ -5,12 +5,10 @@
|
||||
#include "backend/formats/ImageExtensions.h"
|
||||
|
||||
Book::Book(IFileDialogService &dialog,
|
||||
BookPositionStore &positions,
|
||||
FormatRegistry ®istry,
|
||||
QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_dialog(dialog)
|
||||
, m_positions(positions)
|
||||
, m_registry(registry)
|
||||
{
|
||||
connect(&m_dialog, &IFileDialogService::fileSelected,
|
||||
@@ -28,24 +26,34 @@ Book::~Book()
|
||||
bool Book::hasBook() const { return m_reader != nullptr; }
|
||||
bool Book::loading() const { return m_loading; }
|
||||
QString Book::errorMessage() const { return m_errorMessage; }
|
||||
QString Book::coverImageUrl() const { return m_coverImageUrl; }
|
||||
|
||||
QString Book::title() const { return m_reader ? m_reader->title() : QString(); }
|
||||
QString Book::author() const { return m_reader ? m_reader->author() : QString(); }
|
||||
QString Book::filePath() const { return m_reader ? m_reader->filePath() : QString(); }
|
||||
QString Book::coverImageUrl() const { return m_reader ? m_reader->coverImageUrl() : QString(); }
|
||||
|
||||
QVariantList Book::tableOfContents() const
|
||||
{
|
||||
if (!m_reader)
|
||||
return {};
|
||||
const auto toc = m_reader->tableOfContents();
|
||||
QVariantList list;
|
||||
list.reserve(toc.size());
|
||||
for (const auto &entry : toc) {
|
||||
list.append(QVariantMap{
|
||||
{QStringLiteral("title"), entry.title},
|
||||
{QStringLiteral("chapterIndex"), entry.chapterIndex},
|
||||
{QStringLiteral("depth"), entry.depth}
|
||||
});
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
FormatReader *Book::reader() const
|
||||
{
|
||||
return m_reader.get();
|
||||
}
|
||||
|
||||
void Book::setCoverImageUrl(const QString &url)
|
||||
{
|
||||
m_coverImageUrl = url;
|
||||
// bookChanged is already emitted after book open; no extra emit needed here.
|
||||
// If needed by QML after provider setup, a separate signal or re-emit can be added.
|
||||
}
|
||||
|
||||
void Book::openFromUrl(const QUrl &url)
|
||||
{
|
||||
openFromPath(url.toLocalFile());
|
||||
@@ -114,7 +122,6 @@ void Book::openImpl(const QString &filePath)
|
||||
if (generation != m_openGeneration) return;
|
||||
|
||||
m_reader = std::move(*readerBox);
|
||||
m_coverImageUrl.clear();
|
||||
m_loading = false;
|
||||
|
||||
emit loadingChanged();
|
||||
@@ -127,7 +134,6 @@ void Book::close()
|
||||
{
|
||||
if (!m_reader) return;
|
||||
m_reader.reset();
|
||||
m_coverImageUrl.clear();
|
||||
emit bookChanged();
|
||||
}
|
||||
|
||||
@@ -137,10 +143,3 @@ void Book::clearError()
|
||||
m_errorMessage.clear();
|
||||
emit errorMessageChanged();
|
||||
}
|
||||
|
||||
void Book::saveCurrentPosition(int chunkIndex, bool mangaRTL, bool pageShift)
|
||||
{
|
||||
if (!m_reader) return;
|
||||
m_positions.save(m_reader->contentHash(), chunkIndex, mangaRTL, pageShift);
|
||||
emit positionSaved();
|
||||
}
|
||||
+19
-27
@@ -6,41 +6,40 @@
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QVariantList>
|
||||
#include <memory>
|
||||
|
||||
#include "backend/FormatReader.h"
|
||||
#include "backend/IFileDialogService.h"
|
||||
#include "backend/formats/FormatRegistry.h"
|
||||
#include "backend/state/BookPositionStore.h"
|
||||
|
||||
class Book : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged)
|
||||
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
|
||||
Q_PROPERTY(QString title READ title NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString author READ author NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString filePath READ filePath NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString coverImageUrl READ coverImageUrl NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
|
||||
Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged)
|
||||
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
|
||||
Q_PROPERTY(QString title READ title NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString author READ author NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString filePath READ filePath NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString coverImageUrl READ coverImageUrl NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
|
||||
Q_PROPERTY(QVariantList tableOfContents READ tableOfContents NOTIFY bookChanged)
|
||||
|
||||
public:
|
||||
Book(IFileDialogService &dialog,
|
||||
BookPositionStore &positions,
|
||||
FormatRegistry ®istry,
|
||||
QObject *parent = nullptr);
|
||||
~Book() override;
|
||||
|
||||
bool hasBook() const;
|
||||
bool loading() const;
|
||||
QString title() const;
|
||||
QString author() const;
|
||||
QString filePath() const;
|
||||
QString coverImageUrl() const;
|
||||
QString errorMessage() const;
|
||||
bool hasBook() const;
|
||||
bool loading() const;
|
||||
QString title() const;
|
||||
QString author() const;
|
||||
QString filePath() const;
|
||||
QString coverImageUrl() const;
|
||||
QString errorMessage() const;
|
||||
QVariantList tableOfContents() const;
|
||||
|
||||
// Called by AppCoordinator::onBookChanged after the image provider resolves the cover URL.
|
||||
void setCoverImageUrl(const QString &url);
|
||||
|
||||
// Access the underlying reader for wiring purposes (e.g., to push into ReadingModel).
|
||||
// Access the underlying reader (e.g. to push into ReadingModel).
|
||||
FormatReader *reader() const;
|
||||
|
||||
Q_INVOKABLE void openFromUrl(const QUrl &url);
|
||||
@@ -49,14 +48,9 @@ public:
|
||||
Q_INVOKABLE void close();
|
||||
Q_INVOKABLE void clearError();
|
||||
|
||||
// Save current position. Called from QML; forwards to BookPositionStore.
|
||||
Q_INVOKABLE void saveCurrentPosition(int chunkIndex, bool mangaRTL = true, bool pageShift = true);
|
||||
|
||||
signals:
|
||||
// Emitted just before replacing the current reader with a new one.
|
||||
// Listeners can snapshot the outgoing book's progress before the swap.
|
||||
void willOpenNewBook();
|
||||
void positionSaved();
|
||||
|
||||
void bookChanged();
|
||||
void loadingChanged();
|
||||
@@ -66,10 +60,8 @@ private:
|
||||
void openImpl(const QString &filePath);
|
||||
|
||||
IFileDialogService &m_dialog;
|
||||
BookPositionStore &m_positions;
|
||||
FormatRegistry &m_registry;
|
||||
std::unique_ptr<FormatReader> m_reader;
|
||||
QString m_coverImageUrl;
|
||||
QString m_errorMessage;
|
||||
bool m_loading = false;
|
||||
int m_openGeneration = 0;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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,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).
|
||||
|
||||
@@ -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 {};
|
||||
}
|
||||
};
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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")) {}
|
||||
};
|
||||
@@ -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")) {}
|
||||
};
|
||||
@@ -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; }
|
||||
@@ -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;
|
||||
|
||||
@@ -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")) {}
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -11,20 +11,24 @@ BookPosition BookPositionStore::load(const QByteArray &contentMd5)
|
||||
return {};
|
||||
const QVariantMap d = m_state.loadSection(sectionKey(contentMd5));
|
||||
BookPosition pos;
|
||||
pos.chunkIndex = d.value(QStringLiteral("chunkIndex"), 0).toInt();
|
||||
pos.pageShift = d.value(QStringLiteral("pageShift"), true).toBool();
|
||||
pos.mangaRTL = d.value(QStringLiteral("mangaRTL"), true).toBool();
|
||||
pos.chapterIndex = d.value(QStringLiteral("chapterIndex"), 0).toInt();
|
||||
pos.chunkIndex = d.value(QStringLiteral("chunkIndex"), 0).toInt();
|
||||
pos.progress = d.value(QStringLiteral("progress"), 0.0).toReal();
|
||||
pos.pageShift = d.value(QStringLiteral("pageShift"), true).toBool();
|
||||
pos.mangaRTL = d.value(QStringLiteral("mangaRTL"), true).toBool();
|
||||
return pos;
|
||||
}
|
||||
|
||||
void BookPositionStore::save(const QByteArray &contentMd5, int chunkIndex, bool mangaRTL, bool pageShift)
|
||||
void BookPositionStore::save(const QByteArray &contentMd5, const BookPosition &pos)
|
||||
{
|
||||
if (contentMd5.isEmpty())
|
||||
return;
|
||||
m_state.saveSection(sectionKey(contentMd5), {
|
||||
{QStringLiteral("chunkIndex"), chunkIndex},
|
||||
{QStringLiteral("pageShift"), pageShift},
|
||||
{QStringLiteral("mangaRTL"), mangaRTL},
|
||||
{QStringLiteral("chapterIndex"), pos.chapterIndex},
|
||||
{QStringLiteral("chunkIndex"), pos.chunkIndex},
|
||||
{QStringLiteral("progress"), pos.progress},
|
||||
{QStringLiteral("pageShift"), pos.pageShift},
|
||||
{QStringLiteral("mangaRTL"), pos.mangaRTL},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
#include "backend/AppState.h"
|
||||
|
||||
struct BookPosition {
|
||||
int chunkIndex = 0;
|
||||
bool pageShift = true; // manga page-shift; default ON
|
||||
bool mangaRTL = true; // manga reading direction; default RTL
|
||||
int chapterIndex = 0; // Text mode: current chapter (0 for single-chapter / comic)
|
||||
int chunkIndex = 0; // Comic mode: page index; Text mode: top chunk index
|
||||
qreal progress = 0.0; // Normalized [0..1] scroll/reading progress
|
||||
bool pageShift = true; // Comic mode only
|
||||
bool mangaRTL = true; // Comic mode only
|
||||
};
|
||||
|
||||
class BookPositionStore {
|
||||
@@ -18,7 +20,7 @@ public:
|
||||
BookPosition load(const QByteArray &contentMd5);
|
||||
|
||||
// Save the current position for a book identified by MD5 of its contents.
|
||||
void save(const QByteArray &contentMd5, int chunkIndex, bool mangaRTL = true, bool pageShift = true);
|
||||
void save(const QByteArray &contentMd5, const BookPosition &pos);
|
||||
|
||||
private:
|
||||
AppState &m_state;
|
||||
|
||||
Reference in new issue
Block a user