Files
asteria-qt/src/main.cpp
T
2026-10-29 17:31:37 +01:00

196 lines
8.2 KiB
C++

#include <QApplication>
#include <QDateTime>
#include <QFileInfo>
#include <QSize>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QQuickWindow>
#include <QSettings>
#include <QSGRendererInterface>
#include "backend/AppCoordinator.h"
#include "backend/AppState.h"
#include "backend/formats/FileIconProvider.h"
#include "backend/formats/FormatRegistry.h"
#include "backend/formats/WebImageProvider.h"
#include "backend/formats/sources/DirByteSource.h"
#include "backend/formats/sources/Fb2ByteSource.h"
#include "backend/formats/sources/RarByteSource.h"
#include "backend/formats/sources/ZipByteSource.h"
#include "backend/HistoryModel.h"
#include "backend/ReadingModel.h"
#include "backend/reading/BookImageResolver.h"
#include "backend/reading/KnuthPlassItem.h"
#include "backend/NativeFileDialogService.h"
#include "backend/book/Book.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);
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);
// The format table: one row declares everything a format needs.
FormatRegistry formatRegistry({
FormatSpec::comic(QStringLiteral("cbz"), false, QStringLiteral("cbz"),
[] { return std::make_unique<ZipByteSource>(); }),
FormatSpec::comic(QStringLiteral("cbr"), false, QStringLiteral("cbr"),
[] { return std::make_unique<RarByteSource>(); }),
FormatSpec::comic({}, /*matchesDirectory=*/true, QStringLiteral("dir"),
[] { return std::make_unique<DirByteSource>(); }),
FormatSpec::epub([] { return std::make_unique<ZipByteSource>(); }),
FormatSpec::fb2(QStringLiteral("fb2"), [] { return std::make_unique<Fb2ByteSource>(); }),
FormatSpec::fb2(QStringLiteral("fbz"), [] { return std::make_unique<Fb2ByteSource>(); }),
FormatSpec::fb2(QStringLiteral("fb2.zip"), [] { return std::make_unique<Fb2ByteSource>(); }),
});
HistoryModel historyModel(formatRegistry);
historyModel.load(appState);
// 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, formatRegistry);
ReadingModel readingModel(bookPositions);
auto webStorage = std::make_shared<DiskWebImageStorage>();
auto *webProvider = new WebImageProvider(webStorage);
BookImageResolver imageResolver([&formatRegistry](const QString &scheme) {
return formatRegistry.createByteSourceForScheme(scheme);
}, webProvider);
KnuthPlassItem::setImageResolver(&imageResolver);
readingModel.setImageResolver(&imageResolver);
qRegisterMetaType<ChunkLayout>("ChunkLayout");
// --- QML engine ---
QQmlApplicationEngine engine;
qmlRegisterType<KnuthPlassItem>("Asteria", 1, 0, "KnuthPlassItem");
engine.addImageProvider(QStringLiteral("fileicon"), new FileIconProvider());
engine.addImageProvider(QStringLiteral("web"), webProvider);
formatRegistry.registerImageProviders(engine);
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);
// All cross-class signal wiring lives in the coordinator.
AppCoordinator coordinator(book, readingModel, historyModel, uiState,
readingPreferences, appState, settings);
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);
}
const int ret = QApplication::exec();
imageResolver.setWebProvider(nullptr);
return ret;
}