286 lines
11 KiB
C++
286 lines
11 KiB
C++
#include <QApplication>
|
|
#include <QDateTime>
|
|
#include <QFileInfo>
|
|
#include <QSize>
|
|
#include <QQmlApplicationEngine>
|
|
#include <QQmlContext>
|
|
#include <QQuickStyle>
|
|
#include <QQuickWindow>
|
|
#include <QSettings>
|
|
#include <QSGRendererInterface>
|
|
|
|
#include "backend/AppState.h"
|
|
#include "backend/IImageProvider.h"
|
|
#include "backend/formats/ImageLoaderFactory.h"
|
|
#include "backend/formats/ImageProviderFactory.h"
|
|
#include "backend/formats/EpubImageProvider.h"
|
|
#include "backend/formats/FileIconProvider.h"
|
|
#include "backend/formats/WebImageProvider.h"
|
|
#include "backend/HistoryModel.h"
|
|
#include "backend/ReadingModel.h"
|
|
#include "backend/NativeFileDialogService.h"
|
|
#include "backend/formats/ReaderFactory.h"
|
|
#include "backend/book/Book.h"
|
|
#include "backend/formats/TxtReader.h"
|
|
#include "backend/formats/EpubReader.h"
|
|
#include "backend/formats/Fb2Reader.h"
|
|
#include "backend/formats/Fb2ImageProvider.h"
|
|
#include "backend/formats/CbzReader.h"
|
|
#include "backend/formats/CbzImageProvider.h"
|
|
#include "backend/state/BookPositionStore.h"
|
|
#include "backend/state/ReadingPreferences.h"
|
|
#include "backend/state/UiState.h"
|
|
|
|
#ifdef HAS_QT_DBUS
|
|
#include <QDBusConnection>
|
|
#include "backend/PortalFileDialogService.h"
|
|
#endif
|
|
|
|
void myMessageHandler(const QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
{
|
|
QString timestamp = QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd hh:mm:ss.zzz"));
|
|
|
|
QString level;
|
|
switch (type) {
|
|
case QtDebugMsg: level = QStringLiteral("DEBUG"); break;
|
|
case QtInfoMsg: level = QStringLiteral("INFO"); break;
|
|
case QtWarningMsg: level = QStringLiteral("WARNING"); break;
|
|
case QtCriticalMsg: level = QStringLiteral("CRITICAL"); break;
|
|
case QtFatalMsg: level = QStringLiteral("FATAL"); break;
|
|
}
|
|
|
|
QString source;
|
|
if (context.category && qstrcmp(context.category, "default") != 0) {
|
|
source = QString::fromUtf8(context.category);
|
|
} else if (context.file) {
|
|
QString fileName = QFileInfo(QString::fromUtf8(context.file)).fileName();
|
|
source = QStringLiteral("%1:%2").arg(fileName).arg(context.line);
|
|
}
|
|
|
|
QString formattedMsg;
|
|
if (source.isEmpty()) {
|
|
formattedMsg = QStringLiteral("%1 [%2] %3").arg(timestamp, level, msg);
|
|
} else {
|
|
formattedMsg = QStringLiteral("%1 [%2] [%3] %4").arg(timestamp, level, source, msg);
|
|
}
|
|
|
|
QTextStream(stdout, QIODevice::WriteOnly) << formattedMsg << Qt::endl;
|
|
|
|
if (type == QtFatalMsg) {
|
|
abort();
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
qInstallMessageHandler(myMessageHandler);
|
|
QSettings::setDefaultFormat(QSettings::IniFormat);
|
|
|
|
// Select graphics API per platform
|
|
#if defined(Q_OS_MACOS)
|
|
QQuickWindow::setGraphicsApi(QSGRendererInterface::Metal);
|
|
#else
|
|
QQuickWindow::setGraphicsApi(QSGRendererInterface::Vulkan);
|
|
#endif
|
|
|
|
QApplication app(argc, argv);
|
|
QApplication::setApplicationName(QStringLiteral("Asteria"));
|
|
QApplication::setOrganizationName(QStringLiteral("Asteria"));
|
|
QApplication::setDesktopFileName(QStringLiteral("asteria-qt"));
|
|
QIcon appIcon;
|
|
appIcon.addFile(QStringLiteral(":/Asteria/packaging/icons/16x16/asteria-qt.png"), QSize(16, 16));
|
|
appIcon.addFile(QStringLiteral(":/Asteria/packaging/icons/32x32/asteria-qt.png"), QSize(32, 32));
|
|
appIcon.addFile(QStringLiteral(":/Asteria/packaging/icons/48x48/asteria-qt.png"), QSize(48, 48));
|
|
appIcon.addFile(QStringLiteral(":/Asteria/packaging/icons/64x64/asteria-qt.png"), QSize(64, 64));
|
|
QApplication::setWindowIcon(appIcon);
|
|
|
|
QQuickStyle::setStyle(QStringLiteral("Basic"));
|
|
|
|
// --- Object graph (construction order = dependency order) ---
|
|
QSettings settings;
|
|
AppState appState(settings);
|
|
|
|
ReadingPreferences readingPreferences(appState);
|
|
UiState uiState(appState);
|
|
BookPositionStore bookPositions(appState);
|
|
|
|
ReaderFactory readerFactory({
|
|
[] { return std::make_unique<TxtReader>(); },
|
|
[] { return std::make_unique<EpubReader>(); },
|
|
[] { return std::make_unique<Fb2Reader>(); },
|
|
[] { return std::make_unique<CbzReader>(); },
|
|
});
|
|
|
|
HistoryModel historyModel(readerFactory);
|
|
for (const QVariantMap &entry : appState.loadArray(QStringLiteral("history"))) {
|
|
const QString fp = entry.value(QStringLiteral("filePath")).toString();
|
|
if (fp.isEmpty()) continue;
|
|
double progress = entry.value(QStringLiteral("progress"), 0.0).toDouble();
|
|
progress = qBound(0.0, progress, 1.0);
|
|
const QString lo = entry.value(QStringLiteral("lastOpened")).toString();
|
|
historyModel.addOrUpdateBook(fp, progress, lo);
|
|
}
|
|
|
|
// Command-line override: treat first argument as the file to open on startup
|
|
if (const QStringList args = QCoreApplication::arguments(); args.size() > 1) {
|
|
const QString &argPath = args.at(1);
|
|
if (QFileInfo::exists(argPath))
|
|
uiState.setLastFilePath(argPath);
|
|
}
|
|
|
|
#if defined(HAS_QT_DBUS) && defined(Q_OS_LINUX)
|
|
IFileDialogService *fileDialogService;
|
|
if (QDBusConnection::sessionBus().isConnected()) {
|
|
fileDialogService = new PortalFileDialogService(nullptr);
|
|
} else {
|
|
fileDialogService = new NativeFileDialogService(nullptr);
|
|
}
|
|
#else
|
|
IFileDialogService *fileDialogService = new NativeFileDialogService(nullptr);
|
|
#endif
|
|
|
|
Book book(*fileDialogService, bookPositions, readerFactory);
|
|
ReadingModel readingModel(bookPositions);
|
|
|
|
// --- QML engine ---
|
|
QQmlApplicationEngine engine;
|
|
engine.addImageProvider(QStringLiteral("web"), new WebImageProvider());
|
|
engine.addImageProvider(QStringLiteral("epub"), new EpubImageProvider());
|
|
engine.addImageProvider(QStringLiteral("fb2"), new Fb2ImageProvider());
|
|
engine.addImageProvider(QStringLiteral("cbz"), new CbzImageProvider());
|
|
engine.addImageProvider(QStringLiteral("fileicon"), new FileIconProvider());
|
|
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
|
#ifdef QT_DEBUG
|
|
true
|
|
#else
|
|
false
|
|
#endif
|
|
);
|
|
|
|
engine.rootContext()->setContextProperty(QStringLiteral("book"), &book);
|
|
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
|
|
engine.rootContext()->setContextProperty(QStringLiteral("readingPreferences"), &readingPreferences);
|
|
engine.rootContext()->setContextProperty(QStringLiteral("uiState"), &uiState);
|
|
engine.rootContext()->setContextProperty(QStringLiteral("historyModel"), &historyModel);
|
|
|
|
// --- Cross-class signal wiring ---
|
|
|
|
// Typography changes → re-push font params into ReadingModel
|
|
QObject::connect(&readingPreferences, &ReadingPreferences::fontSizeChanged, [&]{
|
|
readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing());
|
|
});
|
|
QObject::connect(&readingPreferences, &ReadingPreferences::lineSpacingChanged, [&]{
|
|
readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing());
|
|
});
|
|
|
|
// Track the current image provider and content width so both stay in sync.
|
|
IImageProvider *currentImageProvider = nullptr;
|
|
int currentContentWidth = 0;
|
|
|
|
// Before a new book open: snapshot the outgoing book's progress into the library.
|
|
// Scroll position is handled by PageView's scroll tracker via book.saveCurrentPosition().
|
|
QObject::connect(&book, &Book::willOpenNewBook, [&]{
|
|
if (book.hasBook())
|
|
historyModel.addOrUpdateBook(book.filePath(), readingModel.progress());
|
|
});
|
|
|
|
// Real-time history update: fired by PageView's scrollTrackTimer on every scroll.
|
|
QObject::connect(&book, &Book::positionSaved, [&]{
|
|
historyModel.addOrUpdateBook(book.filePath(), readingModel.progress());
|
|
});
|
|
|
|
// After the new reader is installed: wire up image provider + ReadingModel
|
|
QObject::connect(&book, &Book::bookChanged, [&]{
|
|
currentImageProvider = nullptr;
|
|
|
|
if (!book.hasBook()) {
|
|
readingModel.setReader(nullptr);
|
|
return;
|
|
}
|
|
|
|
FormatReader *reader = book.reader();
|
|
const QString filePath = reader->filePath();
|
|
|
|
IImageProvider *provider = ImageProviderFactory::create(filePath);
|
|
if (provider) {
|
|
engine.removeImageProvider(provider->scheme());
|
|
engine.addImageProvider(provider->scheme(), provider);
|
|
provider->setFilePath(filePath);
|
|
provider->setDisplayWidth(currentContentWidth);
|
|
currentImageProvider = provider;
|
|
|
|
if (!reader->coverImagePath().isEmpty())
|
|
book.setCoverImageUrl(provider->coverImageUrl(reader->coverImagePath()));
|
|
}
|
|
|
|
readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing());
|
|
readingModel.setImageLoader(ImageLoaderFactory::create(filePath));
|
|
readingModel.setCoverImageUrl(book.coverImageUrl());
|
|
readingModel.setReader(reader);
|
|
readingModel.measure(true);
|
|
|
|
uiState.setLastFilePath(filePath);
|
|
historyModel.addOrUpdateBook(filePath, -1.0);
|
|
});
|
|
|
|
// Propagate content-width changes to image provider + reading model.
|
|
// Called from Book::setContentWidth which is triggered by QML's PageView.
|
|
QObject::connect(&book, &Book::contentWidthChanged, [&](int width){
|
|
currentContentWidth = width;
|
|
if (currentImageProvider)
|
|
currentImageProvider->setDisplayWidth(width);
|
|
readingModel.setContentWidth(width);
|
|
});
|
|
|
|
// Quit: flush everything to persistent storage.
|
|
// Scroll position was last saved by PageView's scroll tracker; we save library progress here.
|
|
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]{
|
|
if (book.hasBook()) {
|
|
historyModel.addOrUpdateBook(book.filePath(), readingModel.progress());
|
|
uiState.setLastFilePath(book.filePath());
|
|
} else {
|
|
uiState.setLastFilePath(QString());
|
|
}
|
|
|
|
readingPreferences.save();
|
|
uiState.save();
|
|
|
|
QList<QVariantMap> items;
|
|
for (int i = 0; i < historyModel.rowCount(); ++i) {
|
|
items.append({
|
|
{QStringLiteral("filePath"), historyModel.filePath(i)},
|
|
{QStringLiteral("progress"), historyModel.progress(i)},
|
|
{QStringLiteral("lastOpened"), historyModel.lastOpened(i)},
|
|
});
|
|
}
|
|
appState.saveArray(QStringLiteral("history"), items);
|
|
|
|
qInfo() << "Settings saved to:" << settings.fileName();
|
|
});
|
|
|
|
const QUrl url(QStringLiteral("qrc:/Asteria/Main.qml"));
|
|
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
|
&app, []() { QCoreApplication::exit(-1); },
|
|
Qt::QueuedConnection);
|
|
engine.load(url);
|
|
|
|
if (const auto *window = qobject_cast<QQuickWindow *>(engine.rootObjects().value(0))) {
|
|
const auto api = window->rendererInterface()->graphicsApi();
|
|
const char *apiName = [api]() {
|
|
switch (api) {
|
|
case QSGRendererInterface::OpenGL: return "OpenGL";
|
|
case QSGRendererInterface::Vulkan: return "Vulkan";
|
|
case QSGRendererInterface::Direct3D11: return "Direct3D 11";
|
|
case QSGRendererInterface::Direct3D12: return "Direct3D 12";
|
|
case QSGRendererInterface::Metal: return "Metal";
|
|
case QSGRendererInterface::Software: return "Software";
|
|
case QSGRendererInterface::Null: return "Null";
|
|
default: return "Unknown";
|
|
}
|
|
}();
|
|
qInfo("Rendering API: %s", apiName);
|
|
}
|
|
|
|
return QApplication::exec();
|
|
}
|