File indexing completed on 2022-09-27 16:37:28

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     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0043     QCoreApplication::setApplicationName(QStringLiteral("tasks"));
0044 
0045     KAboutData aboutData(
0046                          // The program name used internally.
0047                          QStringLiteral("tasks"),
0048                          // A displayable program name string.
0049                          i18nc("@title", "Tasks"),
0050                          // The program version string.
0051                          QStringLiteral(TASKS_VERSION_STRING),
0052                          // Short description of what the app does.
0053                          i18n("Organize your tasks"),
0054                          // The license this code is released under.
0055                          KAboutLicense::GPL,
0056                          // Copyright Statement.
0057                          i18n("© 2022 Felipe Kinoshita"));
0058     aboutData.addAuthor(i18nc("@info:credit", "Felipe Kinoshita"), i18nc("@info:credit", "Author"), QStringLiteral("kinofhek@gmail.com"), QStringLiteral("https://fhek.gitlab.io"));
0059     aboutData.setBugAddress("https://invent.kde.org/utilities/toad/-/issues/new");
0060     KAboutData::setApplicationData(aboutData);
0061     QGuiApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("org.kde.tasks")));
0062 
0063     QQmlApplicationEngine engine;
0064 
0065     auto config = Config::self();
0066 
0067     qmlRegisterSingletonInstance(APPLICATION_ID, 1, 0, "Config", config);
0068 
0069     AboutType about;
0070     qmlRegisterSingletonInstance(APPLICATION_ID, 1, 0, "AboutType", &about);
0071 
0072     Controller controller;
0073     qmlRegisterSingletonInstance(APPLICATION_ID, 1, 0, "Controller", &controller);
0074 
0075     qmlRegisterUncreatableType<TasksModel>(APPLICATION_ID, 1,0 , "TasksModel", QStringLiteral("Must be created from C++"));
0076     auto tasksModel = new TasksModel(qApp);
0077 
0078     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0079     engine.setInitialProperties(
0080         /* QMap<QString, QVariant> or QVariantMap */ {
0081             { "tasksModel", QVariant::fromValue(tasksModel) }
0082         }
0083     );
0084     engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
0085 
0086     if (engine.rootObjects().isEmpty()) {
0087         return -1;
0088     }
0089 
0090 #ifndef Q_OS_ANDROID
0091     KDBusService service(KDBusService::Unique);
0092 
0093     // Raise window when new instance is requested e.g middle click on taskmanager
0094     QObject::connect(&service, &KDBusService::activateRequested, &engine, [&engine](const QStringList & /*arguments*/, const QString & /*workingDirectory*/) {
0095         const auto rootObjects = engine.rootObjects();
0096         for (auto obj : rootObjects) {
0097             auto view = qobject_cast<QQuickWindow *>(obj);
0098             if (view) {
0099                 KWindowSystem::updateStartupId(view);
0100                 KWindowSystem::activateWindow(view);
0101                 return;
0102             }
0103         }
0104     });
0105 #endif
0106 
0107     // Restore window size and position
0108     const auto rootObjects = engine.rootObjects();
0109     for (auto obj : rootObjects) {
0110         auto view = qobject_cast<QQuickWindow *>(obj);
0111         if (view) {
0112             if (view->isVisible()) {
0113                 controller.restoreWindowGeometry(view);
0114             }
0115             break;
0116         }
0117     }
0118 
0119     return app.exec();
0120 }