File: notificationsengine.cpp

package info (click to toggle)
plasma-workspace 4%3A5.27.5-2%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 102,040 kB
  • sloc: cpp: 121,800; xml: 3,238; python: 645; perl: 586; sh: 254; javascript: 113; ruby: 62; makefile: 15; ansic: 13
file content (239 lines) | stat: -rw-r--r-- 8,629 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
    SPDX-FileCopyrightText: 2008 Dmitry Suzdalev <dimsuz@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "notificationsengine.h"
#include "notificationservice.h"

#include "notification.h"
#include "server.h"

#include <KConfig>
#include <KConfigGroup>
#include <KNotifyConfigWidget>
#include <KService>
#include <KSharedConfig>
#include <QGuiApplication>
#include <klocalizedstring.h>

#include <Plasma/DataContainer>
#include <Plasma/Service>

#include <QImage>

#include "debug.h"

using namespace NotificationManager;

NotificationsEngine::NotificationsEngine(QObject *parent, const QVariantList &args)
    : Plasma::DataEngine(parent, args)
{
    init();
}

NotificationsEngine::~NotificationsEngine()
{
}

void NotificationsEngine::init()
{
    connect(&Server::self(), &Server::notificationAdded, this, [this](const Notification &notification) {
        notificationAdded(notification);
    });

    connect(&Server::self(), &Server::notificationReplaced, this, [this](uint replacedId, const Notification &notification) {
        // Notification will already have the correct identical ID
        Q_UNUSED(replacedId);
        notificationAdded(notification);
    });

    connect(&Server::self(), &Server::notificationRemoved, this, [this](uint id, Server::CloseReason reason) {
        Q_UNUSED(reason);
        const QString source = QStringLiteral("notification %1").arg(id);
        // if we don't have that notification in our local list,
        // it has already been closed so don't notify a second time
        if (m_activeNotifications.remove(source) > 0) {
            removeSource(source);
        }
    });

    Server::self().init();
}

void NotificationsEngine::notificationAdded(const Notification &notification)
{
    const QString app_name = notification.applicationName();
    const QString appRealName = notification.notifyRcName();
    const QString eventId = notification.eventId(); // FIXME = hints[QStringLiteral("x-kde-eventId")].toString();
    const QStringList urls = QUrl::toStringList(notification.urls());
    const QString desktopEntry = notification.desktopEntry();
    const QString summary = notification.summary();

    QString bodyFinal = notification.body(); // is already sanitized by NotificationManager
    QString summaryFinal = notification.summary();
    int timeout = notification.timeout();

    if (bodyFinal.isEmpty()) {
        // some ridiculous apps will send just a title (#372112), in that case, treat it as though there's only a body
        bodyFinal = summary;
        summaryFinal = app_name;
    }

    uint id = notification.id(); // replaces_id ? replaces_id : m_nextId++;

    QString appname_str = app_name;
    if (appname_str.isEmpty()) {
        appname_str = i18n("Unknown Application");
    }

    bool isPersistent = (timeout == 0);

    const int AVERAGE_WORD_LENGTH = 6;
    const int WORD_PER_MINUTE = 250;
    int count = notification.summary().length() + notification.body().length() - strlen("<?xml version=\"1.0\"><html></html>");

    // -1 is "server default", 0 is persistent with "server default" display time,
    // anything more should honor the setting
    if (timeout <= 0) {
        timeout = 60000 * count / AVERAGE_WORD_LENGTH / WORD_PER_MINUTE;

        // Add two seconds for the user to notice the notification, and ensure
        // it last at least five seconds, otherwise all the user see is a
        // flash
        timeout = 2000 + qMax(timeout, 3000);
    }

    const QString source = QStringLiteral("notification %1").arg(id);

    Plasma::DataEngine::Data notificationData;
    notificationData.insert(QStringLiteral("id"), QString::number(id));
    notificationData.insert(QStringLiteral("eventId"), eventId);
    notificationData.insert(QStringLiteral("appName"), notification.applicationName());
    // TODO should be proper passed in icon?
    notificationData.insert(QStringLiteral("appIcon"), notification.applicationIconName());
    notificationData.insert(QStringLiteral("summary"), summaryFinal);
    notificationData.insert(QStringLiteral("body"), bodyFinal);

    QStringList actions;
    for (int i = 0; i < notification.actionNames().count(); ++i) {
        actions << notification.actionNames().at(i) << notification.actionLabels().at(i);
    }
    // NotificationManager hides the configure and default stuff from us but we need to re-add them
    // to the actions list for compatibility
    if (!notification.configureActionLabel().isEmpty()) {
        actions << QStringLiteral("settings") << notification.configureActionLabel();
    }
    if (notification.hasDefaultAction()) {
        actions << QStringLiteral("default") << QString();
    }

    notificationData.insert(QStringLiteral("actions"), actions);
    notificationData.insert(QStringLiteral("isPersistent"), isPersistent);
    notificationData.insert(QStringLiteral("expireTimeout"), timeout);

    notificationData.insert(QStringLiteral("desktopEntry"), desktopEntry);

    KService::Ptr service = KService::serviceByStorageId(desktopEntry);
    if (service) {
        notificationData.insert(QStringLiteral("appServiceName"), service->name());
        notificationData.insert(QStringLiteral("appServiceIcon"), service->icon());
    }

    notificationData.insert(QStringLiteral("appRealName"), appRealName);
    // NotificationManager configurable is anything that has a notifyrc or desktop entry
    // but the old stuff assumes only stuff with notifyrc to be configurable
    notificationData.insert(QStringLiteral("configurable"), !notification.notifyRcName().isEmpty());

    QImage image = notification.image();
    notificationData.insert(QStringLiteral("image"), image.isNull() ? QVariant() : image);

    int urgency = -1;
    switch (notification.urgency()) {
    case Notifications::LowUrgency:
        urgency = 0;
        break;
    case Notifications::NormalUrgency:
        urgency = 1;
        break;
    case Notifications::CriticalUrgency:
        urgency = 2;
        break;
    }

    if (urgency > -1) {
        notificationData.insert(QStringLiteral("urgency"), urgency);
    }

    notificationData.insert(QStringLiteral("urls"), urls);

    setData(source, notificationData);

    m_activeNotifications.insert(source, notification.applicationName() + notification.summary());
}

void NotificationsEngine::removeNotification(uint id, uint closeReason)
{
    const QString source = QStringLiteral("notification %1").arg(id);
    // if we don't have that notification in our local list,
    // it has already been closed so don't notify a second time
    if (m_activeNotifications.remove(source) > 0) {
        removeSource(source);
        Server::self().closeNotification(id, static_cast<Server::CloseReason>(closeReason));
    }
}

Plasma::Service *NotificationsEngine::serviceForSource(const QString &source)
{
    return new NotificationService(this, source);
}

int NotificationsEngine::createNotification(const QString &appName,
                                            const QString &appIcon,
                                            const QString &summary,
                                            const QString &body,
                                            int timeout,
                                            const QStringList &actions,
                                            const QVariantMap &hints)
{
    Notification notification;
    notification.setApplicationName(appName);
    notification.setApplicationIconName(appIcon);
    notification.setSummary(summary);
    notification.setBody(body); // sanitizes
    notification.setActions(actions);
    notification.setTimeout(timeout);
    notification.processHints(hints);
    Server::self().add(notification);
    return 0;
}

void NotificationsEngine::configureNotification(const QString &appName, const QString &eventId)
{
    KNotifyConfigWidget *widget = KNotifyConfigWidget::configure(nullptr, appName);
    if (!eventId.isEmpty()) {
        widget->selectEvent(eventId);
    }
}

QSharedPointer<NotificationInhibiton> NotificationsEngine::createInhibition(const QString &hint, const QString &value)
{
    auto ni = new NotificationInhibiton;
    ni->hint = hint;
    ni->value = value;

    QPointer<NotificationsEngine> guard(this);
    QSharedPointer<NotificationInhibiton> rc(ni, [this, guard](NotificationInhibiton *ni) {
        if (guard) {
            m_inhibitions.removeOne(ni);
        }
        delete ni;
    });
    m_inhibitions.append(ni);
    return rc;
}

K_PLUGIN_CLASS_WITH_JSON(NotificationsEngine, "plasma-dataengine-notifications.json")

#include "notificationsengine.moc"