Improve format and error handling

This commit is contained in:
Agel committed 2026-04-08 20:41:14 +02:00
1 parent 503324e7e3
commit 86e44e5d2a
23 files changed
+138 -49

No files matched your search

+2
View File
@@ -34,6 +34,8 @@ public:
// 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. "txt", "epub").
virtual QString fileType() const = 0;
};
+2 -1
View File
@@ -3,6 +3,7 @@
#include <QObject>
#include <QString>
#include <QStringList>
class IFileDialogService : public QObject {
Q_OBJECT
@@ -10,7 +11,7 @@ public:
explicit IFileDialogService(QObject *parent = nullptr) : QObject(parent) {}
~IFileDialogService() override = default;
virtual void open() = 0;
virtual void open(const QStringList &extensions) = 0;
signals:
void fileSelected(const QString &path);
+12 -6
View File
@@ -3,14 +3,19 @@
#include "ReaderFactory.h"
#include <QtConcurrent/QtConcurrent>
LibraryBookModel::LibraryBookModel(QObject *parent)
LibraryBookModel::LibraryBookModel(ReaderFactory &factory, QObject *parent)
: QObject(parent)
, m_factory(factory)
{
connect(this, &LibraryBookModel::metadataLoaded, this, &LibraryBookModel::onMetadataLoaded);
}
LibraryBookModel::LibraryBookModel(const QString &filePath, double progress, const QString &lastOpened, QObject *parent)
: QObject(parent), m_filePath(filePath), m_progress(progress), m_lastOpened(lastOpened)
LibraryBookModel::LibraryBookModel(const QString &filePath, double progress, const QString &lastOpened, ReaderFactory &factory, QObject *parent)
: QObject(parent)
, m_filePath(filePath)
, m_progress(progress)
, m_lastOpened(lastOpened)
, m_factory(factory)
{
connect(this, &LibraryBookModel::metadataLoaded, this, &LibraryBookModel::onMetadataLoaded);
}
@@ -75,8 +80,9 @@ void LibraryBookModel::loadMetadata()
QPointer<LibraryBookModel> self = this;
QString filePath = m_filePath;
m_metadataFuture = QtConcurrent::run([self, filePath]() {
auto reader = ReaderFactory::create(filePath);
ReaderFactory *factory = &m_factory;
m_metadataFuture = QtConcurrent::run([self, filePath, factory]() {
auto reader = factory->create(filePath);
QString title;
QString author;
bool success = false;
@@ -108,7 +114,7 @@ void LibraryBookModel::loadCoverImage(const QString &epubBasePath)
if (!m_coverImageUrl.isEmpty() || m_filePath.isEmpty())
return;
auto reader = ReaderFactory::create(m_filePath);
auto reader = m_factory.create(m_filePath);
if (reader && reader->loadMetadata(m_filePath)) {
QString coverPath = reader->coverImagePath();
if (!coverPath.isEmpty()) {
+5 -2
View File
@@ -6,6 +6,8 @@
#include <QPointer>
#include <QFuture>
#include "ReaderFactory.h"
class FormatReader;
class LibraryBookModel : public QObject {
@@ -18,8 +20,8 @@ class LibraryBookModel : public QObject {
Q_PROPERTY(QString coverImageUrl READ coverImageUrl NOTIFY coverImageLoaded)
public:
explicit LibraryBookModel(QObject *parent = nullptr);
explicit LibraryBookModel(const QString &filePath, double progress = 0.0, const QString &lastOpened = QString(), QObject *parent = nullptr);
explicit LibraryBookModel(ReaderFactory &factory, QObject *parent = nullptr);
explicit LibraryBookModel(const QString &filePath, double progress, const QString &lastOpened, ReaderFactory &factory, QObject *parent = nullptr);
~LibraryBookModel() override;
QString filePath() const;
@@ -54,6 +56,7 @@ private:
QString m_coverImageUrl;
bool m_metadataLoaded = false;
QFuture<void> m_metadataFuture;
ReaderFactory &m_factory;
};
#endif // LIBRARYBOOKMODEL_H
+3 -2
View File
@@ -2,8 +2,9 @@
#include "LibraryBookModel.h"
#include <QDateTime>
LibraryModel::LibraryModel(QObject *parent)
LibraryModel::LibraryModel(ReaderFactory &factory, QObject *parent)
: QAbstractListModel(parent)
, m_factory(factory)
{
// Initialize role names
m_roleNames[FilePathRole] = "filePath";
@@ -98,7 +99,7 @@ void LibraryModel::addOrUpdateBook(const QString &filePath, double progress, con
}
// New book — LibraryModel takes ownership via parent
auto* book = new LibraryBookModel(filePath, progress, lastOpened, this);
auto* book = new LibraryBookModel(filePath, progress, lastOpened, m_factory, this);
connect(book, &LibraryBookModel::metadataLoaded, this, &LibraryModel::onBookMetadataLoaded);
beginInsertRows(QModelIndex(), 0, 0);
+4 -1
View File
@@ -5,6 +5,8 @@
#include <QList>
#include <QHash>
#include "ReaderFactory.h"
class LibraryBookModel;
class LibraryModel : public QAbstractListModel
@@ -23,7 +25,7 @@ public:
};
Q_ENUM(LibraryRoles)
explicit LibraryModel(QObject *parent = nullptr);
explicit LibraryModel(ReaderFactory &factory, QObject *parent = nullptr);
~LibraryModel() override;
// QAbstractItemModel interface
@@ -59,6 +61,7 @@ private slots:
private:
QList<LibraryBookModel*> m_books;
QHash<int, QByteArray> m_roleNames;
ReaderFactory &m_factory;
};
#endif // LIBRARYMODEL_H
+8 -6
View File
@@ -6,13 +6,15 @@ NativeFileDialogService::NativeFileDialogService(QObject *parent)
{
}
void NativeFileDialogService::open()
void NativeFileDialogService::open(const QStringList &extensions)
{
QString path = QFileDialog::getOpenFileName(
nullptr,
QStringLiteral("Open Book"),
QString(),
QStringLiteral("Book files (*.txt *.fb2 *.epub *.pdf *.doc *.rtf *.html *.htm *.docx *.djvu *.odt *.chm);;All files (*)"));
QStringList patterns;
for (const QString &e : extensions)
patterns << QStringLiteral("*.") + e;
const QString filter = QStringLiteral("Book files (") + patterns.join(QLatin1Char(' '))
+ QStringLiteral(");;All files (*)");
QString path = QFileDialog::getOpenFileName(nullptr, QStringLiteral("Open Book"),
QString(), filter);
if (!path.isEmpty())
emit fileSelected(path);
}
+1 -1
View File
@@ -7,7 +7,7 @@ class NativeFileDialogService : public IFileDialogService {
Q_OBJECT
public:
explicit NativeFileDialogService(QObject *parent = nullptr);
void open() override;
void open(const QStringList &extensions) override;
};
#endif // NATIVEFILEDIALOGSERVICE_H
+3 -6
View File
@@ -68,7 +68,7 @@ PortalFileDialogService::PortalFileDialogService(QObject *parent)
{
}
void PortalFileDialogService::open()
void PortalFileDialogService::open(const QStringList &extensions)
{
auto bus = QDBusConnection::sessionBus();
@@ -94,11 +94,8 @@ void PortalFileDialogService::open()
// Build file filters using registered types (no raw QDBusArgument in QVariant)
PortalFilter bookFilter;
bookFilter.name = QStringLiteral("Book files");
for (const char *ext : {"*.txt", "*.fb2", "*.epub", "*.pdf", "*.doc",
"*.rtf", "*.html", "*.htm", "*.docx",
"*.djvu", "*.odt", "*.chm"}) {
bookFilter.globs.append({0, QString::fromLatin1(ext)});
}
for (const QString &ext : extensions)
bookFilter.globs.append({0, QStringLiteral("*.") + ext});
PortalFilter allFilter;
allFilter.name = QStringLiteral("All files");
+1 -1
View File
@@ -9,7 +9,7 @@ class PortalFileDialogService : public IFileDialogService {
Q_OBJECT
public:
explicit PortalFileDialogService(QObject *parent = nullptr);
void open() override;
void open(const QStringList &extensions) override;
private slots:
void handlePortalResponse(uint response, const QVariantMap &results);
+17 -9
View File
@@ -1,18 +1,26 @@
#include "ReaderFactory.h"
#include "FormatReader.h"
#include "formats/TxtReader.h"
#include "formats/EpubReader.h"
#include <QFileInfo>
std::unique_ptr<FormatReader> ReaderFactory::create(const QString &filePath)
ReaderFactory::ReaderFactory(QList<CreatorFn> creators)
{
for (const auto &creator : creators) {
auto probe = creator();
m_registry[probe->fileType()] = creator;
}
}
std::unique_ptr<FormatReader> ReaderFactory::create(const QString &filePath) const
{
const QString ext = QFileInfo(filePath).suffix().toLower();
if (ext == QLatin1String("txt"))
return std::make_unique<TxtReader>();
if (ext == QLatin1String("epub"))
return std::make_unique<EpubReader>();
auto it = m_registry.find(ext);
if (it == m_registry.end())
return nullptr;
return it.value()();
}
QStringList ReaderFactory::supportedExtensions() const
{
return m_registry.keys();
}
+13 -2
View File
@@ -1,14 +1,25 @@
#ifndef READERFACTORY_H
#define READERFACTORY_H
#include <QMap>
#include <QStringList>
#include <functional>
#include <memory>
#include <QString>
class FormatReader;
class ReaderFactory {
public:
static std::unique_ptr<FormatReader> create(const QString &filePath);
using CreatorFn = std::function<std::unique_ptr<FormatReader>()>;
// Constructs the factory by probing each creator via fileType() to build the registry.
explicit ReaderFactory(QList<CreatorFn> creators);
std::unique_ptr<FormatReader> create(const QString &filePath) const;
QStringList supportedExtensions() const;
private:
QMap<QString, CreatorFn> m_registry; // extension -> creator
};
#endif // READERFACTORY_H
+4 -3
View File
@@ -1,13 +1,14 @@
#include "Book.h"
#include "backend/ReaderFactory.h"
#include <QtConcurrent>
Book::Book(IFileDialogService &dialog,
BookPositionStore &positions,
ReaderFactory &factory,
QObject *parent)
: QObject(parent)
, m_dialog(dialog)
, m_positions(positions)
, m_factory(factory)
{
connect(&m_dialog, &IFileDialogService::fileSelected,
this, &Book::openFromPath);
@@ -59,7 +60,7 @@ void Book::openFromPath(const QString &path)
void Book::browseAndOpen()
{
m_dialog.open();
m_dialog.open(m_factory.supportedExtensions());
}
void Book::openImpl(const QString &filePath)
@@ -78,7 +79,7 @@ void Book::openImpl(const QString &filePath)
const int generation = ++m_openGeneration;
m_openFuture = QtConcurrent::run([this, filePath, generation]() {
auto reader = ReaderFactory::create(filePath);
auto reader = m_factory.create(filePath);
if (!reader) {
QMetaObject::invokeMethod(this, [this, generation]() {
if (generation != m_openGeneration) return;
+3
View File
@@ -10,6 +10,7 @@
#include "backend/FormatReader.h"
#include "backend/IFileDialogService.h"
#include "backend/ReaderFactory.h"
#include "backend/state/BookPositionStore.h"
class Book : public QObject {
@@ -25,6 +26,7 @@ class Book : public QObject {
public:
Book(IFileDialogService &dialog,
BookPositionStore &positions,
ReaderFactory &factory,
QObject *parent = nullptr);
~Book() override;
@@ -71,6 +73,7 @@ private:
IFileDialogService &m_dialog;
BookPositionStore &m_positions;
ReaderFactory &m_factory;
std::unique_ptr<FormatReader> m_reader;
QString m_coverImageUrl;
QString m_errorMessage;
+2
View File
@@ -422,3 +422,5 @@ Chapter EpubReader::loadChapter(int index)
return Chapter{index, chTitle, chunkXhtml(m_spineRawContent[index], m_spineHrefs[index])};
}
QString EpubReader::fileType() const { return QStringLiteral("epub"); }
+1
View File
@@ -19,6 +19,7 @@ public:
QString author() override;
QString coverImagePath() override;
QByteArray contentHash() override;
QString fileType() const override;
private:
QString m_filePath;
+2
View File
@@ -214,3 +214,5 @@ QByteArray TxtReader::contentHash()
{
return m_contentHash;
}
QString TxtReader::fileType() const { return QStringLiteral("txt"); }
+1
View File
@@ -16,6 +16,7 @@ public:
QString author() override;
QString coverImagePath() override;
QByteArray contentHash() override;
QString fileType() const override;
private:
QString m_content;
+10 -2
View File
@@ -16,7 +16,10 @@
#include "backend/LibraryModel.h"
#include "backend/ReadingModel.h"
#include "backend/NativeFileDialogService.h"
#include "backend/ReaderFactory.h"
#include "backend/book/Book.h"
#include "backend/formats/TxtReader.h"
#include "backend/formats/EpubReader.h"
#include "backend/state/BookPositionStore.h"
#include "backend/state/ReadingPreferences.h"
#include "backend/state/UiState.h"
@@ -94,7 +97,12 @@ int main(int argc, char *argv[])
UiState uiState(appState);
BookPositionStore bookPositions(appState);
LibraryModel libraryModel;
ReaderFactory readerFactory({
[] { return std::make_unique<TxtReader>(); },
[] { return std::make_unique<EpubReader>(); },
});
LibraryModel libraryModel(readerFactory);
for (const QVariantMap &entry : appState.loadArray(QStringLiteral("library"))) {
const QString fp = entry.value(QStringLiteral("filePath")).toString();
if (fp.isEmpty()) continue;
@@ -122,7 +130,7 @@ int main(int argc, char *argv[])
IFileDialogService *fileDialogService = new NativeFileDialogService(nullptr);
#endif
Book book(*fileDialogService, bookPositions);
Book book(*fileDialogService, bookPositions, readerFactory);
ReadingModel readingModel(bookPositions);
// --- QML engine ---
+3
View File
@@ -52,6 +52,9 @@ ApplicationWindow {
function onLoadingChanged() {
if (book.loading) window._bookInitializing = true
}
function onErrorMessageChanged() {
if (book.errorMessage !== "") window._bookInitializing = false
}
}
Connections {
+28 -5
View File
@@ -1,6 +1,16 @@
#include <QtTest>
#include "backend/ReaderFactory.h"
#include "backend/FormatReader.h"
#include "backend/formats/TxtReader.h"
#include "backend/formats/EpubReader.h"
static ReaderFactory makeFactory()
{
return ReaderFactory({
[] { return std::make_unique<TxtReader>(); },
[] { return std::make_unique<EpubReader>(); },
});
}
class ReaderFactoryTest : public QObject {
Q_OBJECT
@@ -8,33 +18,46 @@ class ReaderFactoryTest : public QObject {
private slots:
void createTxtReader()
{
auto reader = ReaderFactory::create("file.txt");
auto factory = makeFactory();
auto reader = factory.create("file.txt");
QVERIFY(reader != nullptr);
}
void createTxtReaderUpperCase()
{
auto reader = ReaderFactory::create("file.TXT");
auto factory = makeFactory();
auto reader = factory.create("file.TXT");
QVERIFY(reader != nullptr);
}
void createUnknownFormat()
{
auto reader = ReaderFactory::create("file.xyz");
auto factory = makeFactory();
auto reader = factory.create("file.xyz");
QVERIFY(reader == nullptr);
}
void createNoExtension()
{
auto reader = ReaderFactory::create("noext");
auto factory = makeFactory();
auto reader = factory.create("noext");
QVERIFY(reader == nullptr);
}
void createEmptyPath()
{
auto reader = ReaderFactory::create("");
auto factory = makeFactory();
auto reader = factory.create("");
QVERIFY(reader == nullptr);
}
void supportedExtensions()
{
auto factory = makeFactory();
const QStringList exts = factory.supportedExtensions();
QVERIFY(exts.contains(QStringLiteral("txt")));
QVERIFY(exts.contains(QStringLiteral("epub")));
}
};
QTEST_MAIN(ReaderFactoryTest)
+2
View File
@@ -32,6 +32,7 @@ public:
QString coverImagePath() override { return {}; }
QByteArray contentHash() override { return QByteArray("fakehash"); }
bool loadMetadata(const QString &) override { return true; }
QString fileType() const override { return QStringLiteral("txt"); }
};
class SingleChapterReader : public FormatReader {
@@ -47,6 +48,7 @@ public:
QString coverImagePath() override { return {}; }
QByteArray contentHash() override { return QByteArray("singlehash"); }
bool loadMetadata(const QString &) override { return true; }
QString fileType() const override { return QStringLiteral("txt"); }
};
class ReadingModelTest : public QObject {
+11 -2
View File
@@ -6,6 +6,9 @@
#include "backend/AppState.h"
#include "backend/IFileDialogService.h"
#include "backend/ReaderFactory.h"
#include "backend/formats/TxtReader.h"
#include "backend/formats/EpubReader.h"
#include "backend/state/BookPositionStore.h"
#include "backend/book/Book.h"
@@ -14,7 +17,7 @@ class FakeDialogService : public IFileDialogService {
Q_OBJECT
public:
explicit FakeDialogService(QObject *parent = nullptr) : IFileDialogService(parent) {}
void open() override { /* no-op */ }
void open(const QStringList &) override { /* no-op */ }
void triggerFile(const QString &path) { emit fileSelected(path); }
};
@@ -46,6 +49,7 @@ private:
AppState *appState = nullptr;
BookPositionStore *store = nullptr;
FakeDialogService *dialog = nullptr;
ReaderFactory *factory = nullptr;
Book *book = nullptr;
Ctx() {
@@ -55,11 +59,16 @@ private:
appState = new AppState(*s);
store = new BookPositionStore(*appState);
dialog = new FakeDialogService;
book = new Book(*dialog, *store);
factory = new ReaderFactory({
[] { return std::make_unique<TxtReader>(); },
[] { return std::make_unique<EpubReader>(); },
});
book = new Book(*dialog, *store, *factory);
}
~Ctx() {
delete book;
delete dialog;
delete factory;
delete store;
delete appState;
delete s;