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
|
||||
// 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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
auto it = m_registry.find(ext);
|
||||
if (it == m_registry.end())
|
||||
return nullptr;
|
||||
return it.value()();
|
||||
}
|
||||
|
||||
if (ext == QLatin1String("txt"))
|
||||
return std::make_unique<TxtReader>();
|
||||
if (ext == QLatin1String("epub"))
|
||||
return std::make_unique<EpubReader>();
|
||||
|
||||
return nullptr;
|
||||
QStringList ReaderFactory::supportedExtensions() const
|
||||
{
|
||||
return m_registry.keys();
|
||||
}
|
||||
@@ -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
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"); }
|
||||
@@ -19,6 +19,7 @@ public:
|
||||
QString author() override;
|
||||
QString coverImagePath() override;
|
||||
QByteArray contentHash() override;
|
||||
QString fileType() const override;
|
||||
|
||||
private:
|
||||
QString m_filePath;
|
||||
|
||||
@@ -214,3 +214,5 @@ QByteArray TxtReader::contentHash()
|
||||
{
|
||||
return m_contentHash;
|
||||
}
|
||||
|
||||
QString TxtReader::fileType() const { return QStringLiteral("txt"); }
|
||||
@@ -16,6 +16,7 @@ public:
|
||||
QString author() override;
|
||||
QString coverImagePath() override;
|
||||
QByteArray contentHash() override;
|
||||
QString fileType() const override;
|
||||
|
||||
private:
|
||||
QString m_content;
|
||||
|
||||
+10
-2
@@ -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 ---
|
||||
|
||||
@@ -52,6 +52,9 @@ ApplicationWindow {
|
||||
function onLoadingChanged() {
|
||||
if (book.loading) window._bookInitializing = true
|
||||
}
|
||||
function onErrorMessageChanged() {
|
||||
if (book.errorMessage !== "") window._bookInitializing = false
|
||||
}
|
||||
}
|
||||
|
||||
Connections {
|
||||
|
||||
Reference in new issue
Block a user