Files
asteria-qt/src/main.cpp
T
2026-03-23 23:20:25 +01:00

156 lines
6.0 KiB
C++

#include <QApplication>
#include <QDateTime>
#include <QFileInfo>
#include <QSize>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QQuickWindow>
#include <QSGRendererInterface>
#include "backend/BookController.h"
#include "backend/ReadingModel.h"
#include "backend/AppState.h"
#include "backend/NativeFileDialogService.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);
// 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"));
ReadingModel readingModel;
BookController bookController;
bookController.setReadingModel(&readingModel);
AppState appState;
appState.restore();
bookController.setAppState(&appState);
#ifdef HAS_QT_DBUS
IFileDialogService *fileDialogService;
if (QDBusConnection::sessionBus().isConnected()) {
fileDialogService = new PortalFileDialogService(&bookController);
} else {
fileDialogService = new NativeFileDialogService(&bookController);
}
#else
IFileDialogService *fileDialogService = new NativeFileDialogService(&bookController);
#endif
bookController.setFileDialogService(fileDialogService);
if (const QStringList args = QCoreApplication::arguments(); args.size() > 1) {
const QString& filePath = args.at(1);
if (QFileInfo::exists(filePath))
appState.setLastFilePath(filePath);
}
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty(QStringLiteral("bookController"), &bookController);
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
engine.rootContext()->setContextProperty(QStringLiteral("appState"), &appState);
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);
}
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&bookController, &appState, &readingModel]() {
if (bookController.hasBook()) {
appState.setLastFilePath(bookController.currentFilePath());
appState.setLastChapter(readingModel.currentChapter());
appState.saveBookPosition(bookController.currentFilePath(),
readingModel.currentChapter(),
appState.lastScrollIndex(),
appState.lastScrollOffset());
appState.addOrUpdateLibraryBook(bookController.currentFilePath(),
bookController.bookTitle(),
bookController.bookAuthor(),
readingModel.progress());
} else {
appState.setLastFilePath(QString());
}
appState.save();
});
return QApplication::exec();
}