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