Remove obsolete BookController files and update documentation

BookController was superseded by the Book class during a prior refactoring.
The old files were never removed and are not in CMakeLists.txt. Deleted:
- src/backend/BookController.h/cpp (not in build, calls non-existent AppState APIs)
- tests/BookControllerTest.cpp (not in CMake test targets)

Updated CLAUDE.md architecture documentation to reference Book instead of
BookController for all book management operations and tests.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
AgelandClaude Haiku 4.5 committed 2026-04-08 14:22:34 +02:00
1 parent 9280c838b2
commit efd52a57ef
4 files changed
+8 -658

No files matched your search

+8 -6
View File
@@ -131,7 +131,7 @@ When `CMakeLists.txt` gains a new `find_package` or `pkg_check_modules` dependen
| Object | Responsibility | | Object | Responsibility |
|---|---| |---|---|
| `BookController` | Open/close book, owns reader (via `ReaderFactory`), exposes metadata (title, author), TOC | | `Book` | Open/close book, owns reader (via `ReaderFactory`), exposes metadata (title, author), TOC |
| `ReadingModel` | QAbstractListModel exposing chapter chunks (ContentRole), position, next/prev navigation, progress | | `ReadingModel` | QAbstractListModel exposing chapter chunks (ContentRole), position, next/prev navigation, progress |
| `AppState` | Window geometry, last file, preferences (`QSettings`) | | `AppState` | Window geometry, last file, preferences (`QSettings`) |
@@ -175,11 +175,11 @@ Chapter mapping by format: TXT = 1 chapter (whole file). FB2 = `<section>` eleme
### Async Book Opening ### Async Book Opening
`BookController::openBook(path)` runs reader creation + metadata loading in `QtConcurrent::run()`. Emits `bookReady()` or `bookError(QString)` when done. `Book::openBook()` runs reader creation + metadata loading in `QtConcurrent::run()`. Emits `bookReady()` or `bookError(QString)` when done.
### Error Handling ### Error Handling
`FormatReader::open()` returns `bool`. Failed readers expose `QString lastError()`. `BookController` translates errors to a `QString errorMessage` property + `error(QString)` signal. QML shows an `ErrorPopup.qml` component. Error cases: file not found, unsupported format, corrupt archive, encoding failure, permission denied. `FormatReader::open()` returns `bool`. Failed readers expose `QString lastError()`. `Book` translates errors to a `QString errorMessage` property + `error(QString)` signal. QML shows an `ErrorPopup.qml` component. Error cases: file not found, unsupported format, corrupt archive, encoding failure, permission denied.
### File Structure ### File Structure
@@ -187,7 +187,8 @@ Chapter mapping by format: TXT = 1 chapter (whole file). FB2 = `<section>` eleme
src/ src/
main.cpp main.cpp
backend/ backend/
BookController.h/cpp # Open/close, metadata, TOC — exposed to QML book/
Book.h/cpp # Open/close, metadata, TOC — exposed to QML
ReadingModel.h/cpp # Chapter content, position, navigation — exposed to QML ReadingModel.h/cpp # Chapter content, position, navigation — exposed to QML
AppState.h/cpp # Window/session state (QSettings) — exposed to QML AppState.h/cpp # Window/session state (QSettings) — exposed to QML
FormatReader.h # Abstract interface + Chapter/TocEntry structs FormatReader.h # Abstract interface + Chapter/TocEntry structs
@@ -209,7 +210,7 @@ src/
Main.qml Main.qml
components/ components/
Header.qml Header.qml
SideBar.qml # Renders TOC from BookController SideBar.qml # Renders TOC from Book
PageView.qml # ListView-based virtualized rendering of ReadingModel chunks PageView.qml # ListView-based virtualized rendering of ReadingModel chunks
Footer.qml Footer.qml
ErrorPopup.qml # Error display popup ErrorPopup.qml # Error display popup
@@ -222,7 +223,8 @@ tests/
TxtReaderTest.cpp TxtReaderTest.cpp
Fb2ReaderTest.cpp Fb2ReaderTest.cpp
EpubReaderTest.cpp EpubReaderTest.cpp
BookControllerTest.cpp book/
BookTest.cpp
ReadingModelTest.cpp ReadingModelTest.cpp
AppStateTest.cpp AppStateTest.cpp
fixtures/ fixtures/
-266
View File
@@ -1,266 +0,0 @@
#include "BookController.h"
#include "AppState.h"
#include "ImageLoaderFactory.h"
#include "ImageProviderFactory.h"
#include "ReaderFactory.h"
#include "ReadingModel.h"
#include <QtConcurrent>
BookController::BookController(QObject *parent)
: QObject(parent)
{
}
BookController::~BookController()
{
if (m_openFuture.isRunning()) {
m_openFuture.cancel();
m_openFuture.waitForFinished();
}
}
QString BookController::bookTitle() const
{
return m_bookTitle;
}
QString BookController::bookAuthor() const
{
return m_bookAuthor;
}
bool BookController::loading() const
{
return m_loading;
}
QString BookController::errorMessage() const
{
return m_errorMessage;
}
bool BookController::hasBook() const
{
return m_reader != nullptr;
}
QString BookController::currentFilePath() const
{
return m_currentFilePath;
}
bool BookController::startupRestore() const
{
return m_startupRestore;
}
QList<TocEntry> BookController::toc() const
{
if (m_reader)
return m_reader->tableOfContents();
return {};
}
QString BookController::coverImageUrl() const
{
return m_coverImageUrl;
}
void BookController::setReadingModel(ReadingModel *model)
{
m_readingModel = model;
if (m_readingModel) {
connect(m_readingModel, &ReadingModel::restoreReady,
this, [this]() {
if (!m_loading)
return;
m_loading = false;
emit loadingChanged();
emit bookReady();
m_startupRestore = false;
});
}
}
void BookController::setAppState(AppState *state)
{
m_appState = state;
}
void BookController::setFileDialogService(IFileDialogService *service)
{
m_fileDialogService = service;
if (m_fileDialogService)
connect(m_fileDialogService, &IFileDialogService::fileSelected,
this, &BookController::openBookFromPath);
}
void BookController::uiReady()
{
if (!m_appState || m_appState->lastFilePath().isEmpty()) {
emit noSavedBook();
return;
}
m_startupRestore = true;
openBook(m_appState->lastFileUrl());
}
void BookController::openBookFromPath(const QString &filePath)
{
openBook(QUrl::fromLocalFile(filePath));
}
void BookController::openBook(const QUrl &fileUrl)
{
const QString filePath = fileUrl.toLocalFile();
if (m_reader && m_appState && m_readingModel) {
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_readingModel->progress());
}
if (m_reader)
closeBook();
m_loading = true;
emit loadingChanged();
m_errorMessage.clear();
emit errorMessageChanged();
const int generation = ++m_openGeneration;
m_openFuture = QtConcurrent::run([this, filePath, generation]() {
// --- background thread: all file I/O ---
auto reader = ReaderFactory::create(filePath);
if (!reader) {
QMetaObject::invokeMethod(this, [this, generation]() {
if (generation != m_openGeneration) return;
m_loading = false;
emit loadingChanged();
m_errorMessage = QStringLiteral("No format readers available");
emit errorMessageChanged();
emit bookError(m_errorMessage);
}, Qt::QueuedConnection);
return;
}
if (!reader->open(filePath)) {
const QString err = reader->lastError();
QMetaObject::invokeMethod(this, [this, generation, err]() {
if (generation != m_openGeneration) return;
m_loading = false;
emit loadingChanged();
m_errorMessage = err;
emit errorMessageChanged();
emit bookError(m_errorMessage);
}, Qt::QueuedConnection);
return;
}
// Cheap metadata reads while still on background thread
const QString title = reader->title();
const QString author = reader->author();
const QString coverPath = reader->coverImagePath();
// Box the unique_ptr in a shared_ptr so the lambda capturing it is copyable
// (QMetaObject::invokeMethod requires a copyable functor for queued delivery).
auto readerBox = std::make_shared<std::unique_ptr<FormatReader>>(std::move(reader));
// --- main thread: model / provider setup ---
QMetaObject::invokeMethod(this, [this, generation, filePath,
readerBox, title, author, coverPath]() {
if (generation != m_openGeneration) return;
m_reader = std::move(*readerBox);
m_currentFilePath = filePath;
m_bookTitle = title;
m_bookAuthor = author;
IImageProvider *rawProvider = ImageProviderFactory::create(filePath);
m_ownedProvider.reset();
if (rawProvider && m_providerRegistrar) {
m_providerRegistrar(rawProvider->scheme(), rawProvider);
} else {
m_ownedProvider.reset(rawProvider);
}
m_imageProvider = rawProvider;
if (m_imageProvider) {
m_imageProvider->setFilePath(filePath);
m_imageProvider->setDisplayWidth(m_contentWidth);
}
if (!coverPath.isEmpty() && m_imageProvider)
m_coverImageUrl = m_imageProvider->coverImageUrl(coverPath);
else
m_coverImageUrl.clear();
if (m_readingModel) {
if (m_appState)
m_readingModel->setFontParams(m_appState->fontSize(), m_appState->lineSpacing());
m_readingModel->setImageLoader(ImageLoaderFactory::create(filePath));
m_readingModel->setCoverImageUrl(m_coverImageUrl);
m_readingModel->setReader(m_reader.get());
}
if (m_readingModel && m_appState) {
m_readingModel->measure(true);
emit bookChanged();
} else {
m_loading = false;
emit loadingChanged();
emit bookChanged();
emit bookReady();
m_startupRestore = false;
}
}, Qt::QueuedConnection);
});
}
void BookController::browseAndOpen()
{
if (m_fileDialogService)
m_fileDialogService->open();
}
void BookController::closeBook()
{
if (m_reader && m_appState && m_readingModel && !m_currentFilePath.isEmpty()) {
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_readingModel->progress());
}
if (m_imageProvider)
m_imageProvider->clear();
m_imageProvider = nullptr;
m_ownedProvider.reset();
if (m_readingModel)
m_readingModel->setReader(nullptr);
m_reader.reset();
m_bookTitle.clear();
m_bookAuthor.clear();
m_currentFilePath.clear();
m_coverImageUrl.clear();
emit bookChanged();
}
void BookController::clearError()
{
if (m_errorMessage.isEmpty())
return;
m_errorMessage.clear();
emit errorMessageChanged();
}
void BookController::setImageProviderRegistrar(
std::function<void(const QString &scheme, IImageProvider *)> fn)
{
m_providerRegistrar = std::move(fn);
}
void BookController::setContentWidth(int width)
{
m_contentWidth = width;
if (m_imageProvider)
m_imageProvider->setDisplayWidth(width);
if (m_readingModel)
m_readingModel->setContentWidth(width);
}
-90
View File
@@ -1,90 +0,0 @@
#ifndef BOOKCONTROLLER_H
#define BOOKCONTROLLER_H
#include <QFuture>
#include <QString>
#include <QList>
#include <functional>
#include <memory>
#include "FormatReader.h"
#include "IFileDialogService.h"
#include "IImageLoader.h"
#include "IImageProvider.h"
class AppState;
class ReadingModel;
class BookController : public QObject {
Q_OBJECT
Q_PROPERTY(QString bookTitle READ bookTitle NOTIFY bookChanged)
Q_PROPERTY(QString bookAuthor READ bookAuthor NOTIFY bookChanged)
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged)
Q_PROPERTY(QList<TocEntry> toc READ toc NOTIFY bookChanged)
Q_PROPERTY(QString currentFilePath READ currentFilePath NOTIFY bookChanged)
Q_PROPERTY(QString coverImageUrl READ coverImageUrl NOTIFY bookChanged)
Q_PROPERTY(bool startupRestore READ startupRestore NOTIFY bookReady)
public:
explicit BookController(QObject *parent = nullptr);
~BookController();
QString bookTitle() const;
QString bookAuthor() const;
bool loading() const;
QString errorMessage() const;
bool hasBook() const;
QList<TocEntry> toc() const;
QString currentFilePath() const;
QString coverImageUrl() const;
bool startupRestore() const;
void setReadingModel(ReadingModel *model);
void setAppState(AppState *state);
void setFileDialogService(IFileDialogService *service);
// Called each time a new image provider is created for a book.
// The callback must register the provider with the QML engine (which takes ownership).
void setImageProviderRegistrar(std::function<void(const QString &scheme, IImageProvider *)> fn);
// Called by QML once the window is fully laid out. Reads AppState to decide
// whether to open the last book or emit noSavedBook().
Q_INVOKABLE void uiReady();
Q_INVOKABLE void openBook(const QUrl &fileUrl);
Q_INVOKABLE void openBookFromPath(const QString &filePath);
Q_INVOKABLE void browseAndOpen();
Q_INVOKABLE void closeBook();
Q_INVOKABLE void clearError();
Q_INVOKABLE void setContentWidth(int width);
signals:
void bookReady();
void bookError(const QString &error);
void bookChanged();
void loadingChanged();
void errorMessageChanged();
void noSavedBook(); // emitted by uiReady() when there is no last file to open
private:
std::unique_ptr<FormatReader> m_reader;
ReadingModel *m_readingModel = nullptr;
AppState *m_appState = nullptr;
IFileDialogService *m_fileDialogService = nullptr;
std::function<void(const QString &, IImageProvider *)> m_providerRegistrar;
// Non-owning when the QML engine owns the provider; owning otherwise (e.g., in tests).
std::unique_ptr<IImageProvider> m_ownedProvider;
IImageProvider *m_imageProvider = nullptr;
QString m_bookTitle;
QString m_bookAuthor;
QString m_currentFilePath;
QString m_coverImageUrl;
bool m_loading = false;
QString m_errorMessage;
bool m_startupRestore = false;
int m_contentWidth = 0;
int m_openGeneration = 0; // incremented per openBook call; callbacks check for staleness
QFuture<void> m_openFuture;
};
#endif // BOOKCONTROLLER_H
-296
View File
@@ -1,296 +0,0 @@
#include <QtTest>
#include <QSignalSpy>
#include <QUrl>
#include <QSettings>
#include "backend/BookController.h"
#include "backend/ReadingModel.h"
#include "backend/AppState.h"
#include "backend/IFileDialogService.h"
#include "backend/LibraryModel.h"
class FakeFileDialogService : public IFileDialogService {
Q_OBJECT
public:
explicit FakeFileDialogService(QObject *parent = nullptr)
: IFileDialogService(parent) {}
void open() override { openCalled = true; }
void simulateSelection(const QString &path) { emit fileSelected(path); }
bool openCalled = false;
};
class BookControllerTest : public QObject {
Q_OBJECT
private:
QString fixturePath(const QString &filename) const
{
return QStringLiteral(FIXTURES_DIR) + "/" + filename;
}
private slots:
void initTestCase()
{
QCoreApplication::setOrganizationName(QStringLiteral("AsteriaTest"));
QCoreApplication::setApplicationName(QStringLiteral("BookControllerTest"));
}
void cleanup()
{
QSettings().clear();
}
void openValidBook()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QCOMPARE(readySpy.count(), 1);
QVERIFY(ctrl.hasBook());
QCOMPARE(ctrl.bookTitle(), QStringLiteral("sample"));
}
void openInvalidPath()
{
BookController ctrl;
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/nonexistent_asteria_file.txt"));
QVERIFY(errorSpy.wait(5000));
QCOMPARE(errorSpy.count(), 1);
QVERIFY(!ctrl.errorMessage().isEmpty());
QVERIFY(!ctrl.hasBook());
}
void openUnsupportedFormat()
{
BookController ctrl;
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
QVERIFY(errorSpy.wait(5000));
QCOMPARE(errorSpy.count(), 1);
QCOMPARE(ctrl.errorMessage(), QStringLiteral("No format readers available"));
QVERIFY(!ctrl.hasBook());
}
void closeBook()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QSignalSpy changedSpy(&ctrl, &BookController::bookChanged);
ctrl.closeBook();
QVERIFY(!ctrl.hasBook());
QVERIFY(ctrl.bookTitle().isEmpty());
QVERIFY(ctrl.bookAuthor().isEmpty());
QCOMPARE(changedSpy.count(), 1);
}
void openBookSetsReadingModel()
{
BookController ctrl;
ReadingModel model;
ctrl.setReadingModel(&model);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(model.totalChapters() > 0);
}
void loadingTransitions()
{
BookController ctrl;
QSignalSpy loadingSpy(&ctrl, &BookController::loadingChanged);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
// loading goes true then false → 2 emissions
QCOMPARE(loadingSpy.count(), 1);
QVERIFY(ctrl.loading());
QVERIFY(readySpy.wait(5000));
QCOMPARE(loadingSpy.count(), 2);
// After completion, loading should be false
QVERIFY(!ctrl.loading());
}
void currentFilePathInitiallyEmpty()
{
BookController ctrl;
QVERIFY(ctrl.currentFilePath().isEmpty());
}
void currentFilePathSetOnOpen()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QCOMPARE(ctrl.currentFilePath(), fixturePath("sample.txt"));
}
void currentFilePathClearedOnClose()
{
BookController ctrl;
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
ctrl.closeBook();
QVERIFY(ctrl.currentFilePath().isEmpty());
}
void clearErrorResetsMessage()
{
BookController ctrl;
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
QVERIFY(errorSpy.wait(5000));
QVERIFY(!ctrl.errorMessage().isEmpty());
QSignalSpy spy(&ctrl, &BookController::errorMessageChanged);
ctrl.clearError();
QVERIFY(ctrl.errorMessage().isEmpty());
QCOMPARE(spy.count(), 1);
}
void openBookSavesPositionOfPrevious()
{
AppState appState;
ReadingModel model;
BookController ctrl;
ctrl.setReadingModel(&model);
ctrl.setAppState(&appState);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QString firstPath = ctrl.currentFilePath();
// Attempt to open a second book — position of first book should be saved
// regardless of whether the second open succeeds
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
// Position for sample.txt should have been saved (0 at minimum)
QVariantMap pos = appState.loadBookPosition(firstPath);
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 0);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 0);
}
// IFileDialogService tests
void browseAndOpenCallsServiceOpen() const
{
BookController ctrl;
FakeFileDialogService svc;
ctrl.setFileDialogService(&svc);
ctrl.browseAndOpen();
QVERIFY(svc.openCalled);
}
void browseAndOpenUsesService() const
{
BookController ctrl;
FakeFileDialogService svc;
ctrl.setFileDialogService(&svc);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
svc.simulateSelection(fixturePath("sample.txt"));
QVERIFY(readySpy.wait(5000));
QCOMPARE(readySpy.count(), 1);
QVERIFY(ctrl.hasBook());
}
void browseAndOpenNoopWhenNoService() const
{
BookController ctrl;
// No service set — must not crash
ctrl.browseAndOpen();
QVERIFY(!ctrl.hasBook());
}
void coverImageUrlInitiallyEmpty()
{
BookController ctrl;
QVERIFY(ctrl.coverImageUrl().isEmpty());
}
void txtBookHasNoCoverImage()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QVERIFY(ctrl.coverImageUrl().isEmpty());
}
void coverImageUrlClearedOnClose()
{
BookController ctrl;
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
ctrl.closeBook();
QVERIFY(ctrl.coverImageUrl().isEmpty());
}
void openBookUpdatesLibrary()
{
AppState appState;
ReadingModel model;
BookController ctrl;
ctrl.setReadingModel(&model);
ctrl.setAppState(&appState);
auto* libraryModel = qobject_cast<QAbstractListModel*>(appState.libraryModel());
QCOMPARE(libraryModel->rowCount(), 0);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
// First book open does NOT update the library (only previous reader is saved)
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QCOMPARE(libraryModel->rowCount(), 0);
// Closing the book should save it to library
ctrl.closeBook();
QCOMPARE(libraryModel->rowCount(), 1);
QCOMPARE(libraryModel->data(libraryModel->index(0), LibraryModel::FilePathRole).toString(),
fixturePath("sample.txt"));
}
void closeBookUpdatesLibrary()
{
AppState appState;
ReadingModel model;
BookController ctrl;
ctrl.setReadingModel(&model);
ctrl.setAppState(&appState);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
ctrl.closeBook();
auto* libraryModel = qobject_cast<QAbstractListModel*>(appState.libraryModel());
QCOMPARE(libraryModel->rowCount(), 1);
QCOMPARE(libraryModel->data(libraryModel->index(0), LibraryModel::FilePathRole).toString(),
fixturePath("sample.txt"));
}
};
QTEST_MAIN(BookControllerTest)
#include "BookControllerTest.moc"