Warning, file /utilities/toad/src/main.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2022 Felipe Kinoshita <kinofhek@gmail.com>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include <QIcon>
0005 #include <QQmlApplicationEngine>
0006 #include <QUrl>
0007 #include <QtQml>
0008 #include <QQuickStyle>
0009 #include <QQuickWindow>
0010 
0011 #include "about.h"
0012 #include "version-tasks.h"
0013 #include <KAboutData>
0014 #include <KLocalizedContext>
0015 #include <KLocalizedString>
0016 
0017 #ifndef Q_OS_ANDROID
0018 #include <QApplication>
0019 
0020 #include <KDBusService>
0021 #include <KWindowSystem>
0022 #else
0023 #include <QGuiApplication>
0024 #endif
0025 
0026 constexpr auto APPLICATION_ID = "org.kde.tasks";
0027 
0028 #include "config.h"
0029 #include "controller.h"
0030 #include "tasksmodel.h"
0031 
0032 Q_DECL_EXPORT int main(int argc, char *argv[])
0033 {
0034     QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
0035 #ifndef Q_OS_ANDROID
0036     QApplication app(argc, argv);
0037     QQuickStyle::setStyle(QStringLiteral("org.kde.desktop"));
0038 #else
0039     QGuiApplication app(argc, argv);
0040     QQuickStyle::setStyle(QStringLiteral("org.kde.breeze"));
0041 #endif
0042 
0043     KLocalizedString::setApplicationDomain("toad");
0044 
0045     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0046     QCoreApplication::setApplicationName(QStringLiteral("tasks"));
0047 
0048     KAboutData aboutData(
0049                          // The program name used internally.
0050                          QStringLiteral("tasks"),
0051                          // A displayable program name string.
0052                          i18nc("@title", "Tasks"),
0053                          // The program version string.
0054                          QStringLiteral(TASKS_VERSION_STRING),
0055                          // Short description of what the app does.
0056                          i18n("Organize your tasks"),
0057                          // The license this code is released under.
0058                          KAboutLicense::GPL,
0059                          // Copyright Statement.
0060                          i18n("© 2022 Felipe Kinoshita"));
0061     aboutData.addAuthor(i18nc("@info:credit", "Felipe Kinoshita"), i18nc("@info:credit", "Author"), QStringLiteral("kinofhek@gmail.com"), QStringLiteral("https://fhek.gitlab.io"));
0062     aboutData.setBugAddress("https://invent.kde.org/utilities/toad/-/issues/new");
0063     KAboutData::setApplicationData(aboutData);
0064     QGuiApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("org.kde.tasks")));
0065 
0066     QQmlApplicationEngine engine;
0067 
0068     auto config = Config::self();
0069 
0070     qmlRegisterSingletonInstance(APPLICATION_ID, 1, 0, "Config", config);
0071 
0072     AboutType about;
0073     qmlRegisterSingletonInstance(APPLICATION_ID, 1, 0, "AboutType", &about);
0074 
0075     Controller controller;
0076     qmlRegisterSingletonInstance(APPLICATION_ID, 1, 0, "Controller", &controller);
0077 
0078     qmlRegisterUncreatableType<TasksModel>(APPLICATION_ID, 1,0 , "TasksModel", QStringLiteral("Must be created from C++"));
0079     auto tasksModel = new TasksModel(qApp);
0080 
0081     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0082     engine.setInitialProperties(
0083         /* QMap<QString, QVariant> or QVariantMap */ {
0084             { "tasksModel", QVariant::fromValue(tasksModel) }
0085         }
0086     );
0087     engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
0088 
0089     if (engine.rootObjects().isEmpty()) {
0090         return -1;
0091     }
0092 
0093 #ifndef Q_OS_ANDROID
0094     KDBusService service(KDBusService::Unique);
0095 
0096     // Raise window when new instance is requested e.g middle click on taskmanager
0097     QObject::connect(&service, &KDBusService::activateRequested, &engine, [&engine](const QStringList & /*arguments*/, const QString & /*workingDirectory*/) {
0098         const auto rootObjects = engine.rootObjects();
0099         for (auto obj : rootObjects) {
0100             auto view = qobject_cast<QQuickWindow *>(obj);
0101             if (view) {
0102                 KWindowSystem::updateStartupId(view);
0103                 KWindowSystem::activateWindow(view);
0104                 return;
0105             }
0106         }
0107     });
0108 #endif
0109 
0110     // Restore window size and position
0111     const auto rootObjects = engine.rootObjects();
0112     for (auto obj : rootObjects) {
0113         auto view = qobject_cast<QQuickWindow *>(obj);
0114         if (view) {
0115             if (view->isVisible()) {
0116                 controller.restoreWindowGeometry(view);
0117             }
0118             break;
0119         }
0120     }
0121 
0122     return app.exec();
0123 }