summaryrefslogtreecommitdiffstats
path: root/src/manager-lib/qmlinprocnotificationimpl.cpp
blob: f2fb0dafd830ce2aeef07e93fb497f85b12dc5ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QQmlEngine>
#include <QQmlExpression>
#include <QQmlInfo>

#include "logging.h"
#include "application.h"
#include "qmlinprocruntime.h"
#include "qmlinprocnotificationimpl.h"
#include "notification.h"
#include "notificationmanager.h"

using namespace Qt::StringLiterals;


QT_BEGIN_NAMESPACE_AM

QVector<QPointer<Notification>> QmlInProcNotificationImpl::s_allNotifications;


QmlInProcNotificationImpl::QmlInProcNotificationImpl(Notification *notification,
                                                     const QString &applicationId)
    : NotificationImpl(notification)
    , m_applicationId(applicationId)
{
    initialize();
}

void QmlInProcNotificationImpl::classBegin()
{
    if (m_applicationId.isEmpty()) {
        if (auto rt = QmlInProcRuntime::determineRuntime(notification())) {
            if (rt->application())
                m_applicationId = rt->application()->id();
        } else {
            m_applicationId = u":sysui"_s;
        }
    }
    if (m_applicationId.isEmpty())
        qCCritical(LogQmlRuntime) << "Notification could not determine the application's runtime to get the applicationId.";
    Q_ASSERT(!m_applicationId.isEmpty());
}

void QmlInProcNotificationImpl::initialize()
{
    static bool once = false;
    if (once)
        return;
    once = true;

    auto nm = NotificationManager::instance();

    QObject::connect(&nm->internalSignals, &NotificationManagerInternalSignals::actionInvoked,
                     nm, [](uint notificationId, const QString &actionId) {
        qDebug("Notification action triggered signal: %u %s", notificationId, qPrintable(actionId));
        for (const QPointer<Notification> &n : std::as_const(s_allNotifications)) {
            if (n->notificationId() == notificationId) {
                n->triggerAction(actionId);
                break;
            }
        }
    });

    QObject::connect(&nm->internalSignals, &NotificationManagerInternalSignals::notificationClosed,
                     nm, [](uint notificationId, uint reason) {
        Q_UNUSED(reason)

        qDebug("Notification was closed signal: %u", notificationId);
        // quick fix: in case apps have been closed items are null (see AUTOSUITE-14)
        s_allNotifications.removeAll(nullptr);
        for (const QPointer<Notification> &n : std::as_const(s_allNotifications)) {
            if (n->notificationId() == notificationId) {
                n->close();
                s_allNotifications.removeAll(n);
                break;
            }
        }
    });
}

void QmlInProcNotificationImpl::close()
{
    NotificationManager::instance()->closeNotification(notification()->notificationId());
}

uint QmlInProcNotificationImpl::show()
{
    auto *n = notification();

    s_allNotifications << n;
    return NotificationManager::instance()->showNotification(m_applicationId, n->notificationId(),
                                                             n->icon().toString(), n->summary(),
                                                             n->body(), n->libnotifyActionList(),
                                                             n->libnotifyHints(), n->timeout());
}

QT_END_NAMESPACE_AM