83 lines
2.6 KiB
C++
83 lines
2.6 KiB
C++
#include "AppCoordinator.h"
|
|
|
|
#include "AppState.h"
|
|
#include "HistoryModel.h"
|
|
#include "ReadingModel.h"
|
|
#include "book/Book.h"
|
|
#include "state/ReadingPreferences.h"
|
|
#include "state/UiState.h"
|
|
|
|
#include <QCoreApplication>
|
|
#include <QSettings>
|
|
|
|
AppCoordinator::AppCoordinator(Book &book,
|
|
ReadingModel &readingModel,
|
|
HistoryModel &historyModel,
|
|
UiState &uiState,
|
|
ReadingPreferences &readingPreferences,
|
|
AppState &appState,
|
|
QSettings &settings,
|
|
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)
|
|
{
|
|
connect(&m_book, &Book::willOpenNewBook, this, &AppCoordinator::onWillOpenNewBook);
|
|
connect(&m_readingModel, &ReadingModel::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()
|
|
{
|
|
if (m_book.hasBook())
|
|
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
|
}
|
|
|
|
// After the new reader is installed: configure ReadingModel
|
|
void AppCoordinator::onBookChanged()
|
|
{
|
|
if (!m_book.hasBook()) {
|
|
m_readingModel.setReader(nullptr);
|
|
return;
|
|
}
|
|
|
|
FormatReader *reader = m_book.reader();
|
|
const QString filePath = reader->filePath();
|
|
m_readingModel.setReader(reader);
|
|
m_uiState.setLastFilePath(filePath);
|
|
m_historyModel.addOrUpdateBook(filePath, -1.0);
|
|
}
|
|
|
|
// Quit: flush everything to persistent storage.
|
|
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_historyModel.save(m_appState);
|
|
m_readingPreferences.save();
|
|
m_uiState.save();
|
|
|
|
qInfo() << "Settings saved to:" << m_settings.fileName();
|
|
}
|