Files
asteria-qt/src/backend/AppCoordinator.cpp
T
2026-10-25 14:22:12 +01:00

114 lines
3.8 KiB
C++

#include "AppCoordinator.h"
#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,
ReadingModel &readingModel,
HistoryModel &historyModel,
UiState &uiState,
ReadingPreferences &readingPreferences,
AppState &appState,
QSettings &settings,
FormatRegistry &formatRegistry,
QQmlApplicationEngine &engine,
QObject *parent)
: QObject(parent)
, m_book(book)
, m_readingModel(readingModel)
, m_historyModel(historyModel)
, m_uiState(uiState)
, 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_book, &Book::bookChanged, this, &AppCoordinator::onBookChanged);
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
this, &AppCoordinator::onAboutToQuit);
}
// Before a new book open: snapshot the outgoing book's progress into the library.
void AppCoordinator::onWillOpenNewBook()
{
if (m_book.hasBook())
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
}
// Real-time history update: fired when position is saved.
void AppCoordinator::onPositionSaved()
{
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
}
// After the new reader is installed: wire up image provider + ReadingModel
void AppCoordinator::onBookChanged()
{
m_currentImageProvider = nullptr;
if (!m_book.hasBook()) {
m_readingModel.setReader(nullptr);
return;
}
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()) {
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
m_uiState.setLastFilePath(m_book.filePath());
} else {
m_uiState.setLastFilePath(QString());
}
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();
}