File indexing completed on 2024-11-24 04:50:40

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "../config-merkuro.h"
0005 #include "importer.h"
0006 #include "mousetracker.h"
0007 #include <KAboutData>
0008 #include <KConfig>
0009 #include <KConfigGroup>
0010 #include <KDBusService>
0011 #include <KLocalizedContext>
0012 #include <KLocalizedString>
0013 #include <KWindowConfig>
0014 #include <KWindowSystem>
0015 #include <QApplication>
0016 #include <QCommandLineParser>
0017 #include <QDBusConnection>
0018 #include <QDBusConnectionInterface>
0019 #include <QDir>
0020 #include <QIcon>
0021 #include <QQmlApplicationEngine>
0022 #include <QQmlContext>
0023 #include <QQuickStyle>
0024 #include <QQuickWindow>
0025 
0026 static void raiseWindow(QWindow *window)
0027 {
0028     KWindowSystem::updateStartupId(window);
0029     KWindowSystem::activateWindow(window);
0030 }
0031 
0032 int main(int argc, char *argv[])
0033 {
0034     QGuiApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
0035     QApplication app(argc, argv);
0036     KLocalizedString::setApplicationDomain(QByteArrayLiteral("merkuro"));
0037     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0038     QCoreApplication::setApplicationName(QStringLiteral("Merkuro"));
0039 
0040     // Default to org.kde.desktop style unless the user forces another style
0041     if (qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_STYLE")) {
0042         QQuickStyle::setStyle(QStringLiteral("org.kde.desktop"));
0043     }
0044 
0045 #if defined(Q_OS_WIN) || defined(Q_OS_MAC)
0046     QApplication::setStyle(QStringLiteral("breeze"));
0047 #endif
0048 
0049     KAboutData aboutData(
0050         // The program name used internally.
0051         QStringLiteral("merkuro.calendar"),
0052         // A displayable program name string.
0053         i18nc("@title", "Merkuro Calendar"),
0054         QStringLiteral(MERKURO_VERSION_STRING),
0055         // Short description of what the app does.
0056         i18n("Calendar Application"),
0057         // The license this code is released under.
0058         KAboutLicense::GPL_V3,
0059         // Copyright Statement.
0060         i18n("(c) KDE Community 2021"));
0061     aboutData.setBugAddress("https://bugs.kde.org/enter_bug.cgi?format=guided&product=merkuro&version=" + QStringLiteral(MERKURO_VERSION_STRING).toUtf8());
0062     aboutData.addAuthor(i18nc("@info:credit", "Carl Schwan"),
0063                         i18nc("@info:credit", "Maintainer"),
0064                         QStringLiteral("carl@carlschwan.eu"),
0065                         QStringLiteral("https://carlschwan.eu"));
0066     aboutData.addAuthor(i18nc("@info:credit", "Clau Cambra"),
0067                         i18nc("@info:credit", "Maintainer"),
0068                         QStringLiteral("claudio.cambra@gmail.com"),
0069                         QStringLiteral("https://claudiocambra.com"));
0070     aboutData.addAuthor(i18nc("@info:credit", "Felipe Kinoshita"),
0071                         i18nc("@info:credit", "Developer"),
0072                         QStringLiteral("kinofhek@gmail.com"),
0073                         QStringLiteral("https://fhek.gitlab.io"));
0074     KAboutData::setApplicationData(aboutData);
0075     QGuiApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("org.kde.merkuro.calendar")));
0076 
0077     QCommandLineParser parser;
0078     aboutData.setupCommandLine(&parser);
0079     parser.process(app);
0080     aboutData.processCommandLine(&parser);
0081 
0082     const auto mouseTracker = MouseTracker::instance();
0083     qmlRegisterSingletonInstance("org.kde.merkuro.calendar.private", 1, 0, "MouseTracker", mouseTracker);
0084 
0085     KDBusService service(KDBusService::Unique);
0086 
0087     qmlRegisterType<Importer>("org.kde.merkuro.calendar.private", 1, 0, "Importer");
0088 
0089     QQmlApplicationEngine engine;
0090 
0091     QObject::connect(&service, &KDBusService::activateRequested, &engine, [&engine, &parser](const QStringList &arguments, const QString &workingDirectory) {
0092         parser.parse(arguments);
0093         const auto rootObjects = engine.rootObjects();
0094 
0095         const QStringList args = parser.positionalArguments();
0096 
0097         for (auto obj : rootObjects) {
0098             auto view = qobject_cast<QQuickWindow *>(obj);
0099             if (view) {
0100                 raiseWindow(view);
0101                 return;
0102             }
0103 
0104             auto importer = view->findChild<Importer *>(QStringLiteral("ImportHandler"));
0105             for (const auto &arg : args) {
0106                 Q_EMIT importer->importCalendarFromFile(QUrl::fromUserInput(arg, QDir::currentPath(), QUrl::AssumeLocalFile));
0107             }
0108         }
0109     });
0110 
0111     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0112     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
0113 
0114     if (engine.rootObjects().isEmpty()) {
0115         return -1;
0116     }
0117 
0118     const auto rootObjects = engine.rootObjects();
0119     for (auto obj : rootObjects) {
0120         const auto view = qobject_cast<QQuickWindow *>(obj);
0121         if (view) {
0122             KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
0123             KConfigGroup windowGroup(&dataResource, QStringLiteral("Window"));
0124             KWindowConfig::restoreWindowSize(view, windowGroup);
0125             KWindowConfig::restoreWindowPosition(view, windowGroup);
0126 
0127             view->installEventFilter(mouseTracker);
0128 
0129             const auto args = parser.positionalArguments();
0130 
0131             auto importer = view->findChild<Importer *>(QStringLiteral("ImportHandler"));
0132             for (const auto &arg : args) {
0133                 Q_EMIT importer->importCalendarFromFile(QUrl::fromUserInput(arg, QDir::currentPath(), QUrl::AssumeLocalFile));
0134             }
0135 
0136             break;
0137         }
0138     }
0139 
0140     QDBusConnection::sessionBus().interface()->startService(QStringLiteral("org.kde.merkuroac"));
0141 
0142     return app.exec();
0143 }