File: notificationserver.cpp

package info (click to toggle)
cutefish-core 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,076 kB
  • sloc: cpp: 11,327; xml: 443; sh: 29; makefile: 6
file content (169 lines) | stat: -rw-r--r-- 4,989 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
/*
 * SPDX-FileCopyrightText: 2021 Reion Wong <reion@cutefishos.com>
 * SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "notificationserver.h"
#include "dbus/notificationsadaptor.h"
#include "utils.h"

#include <QDebug>

static NotificationServer *NOTIFICATION_SERVER_SELF = nullptr;

NotificationServer *NotificationServer::self()
{
    if (!NOTIFICATION_SERVER_SELF)
        NOTIFICATION_SERVER_SELF = new NotificationServer;

    return NOTIFICATION_SERVER_SELF;
}

NotificationServer::NotificationServer(QObject *parent)
    : QObject(parent)
    , m_notificationWatcher(nullptr)
{
    new NotificationsAdaptor(this);

    QDBusConnectionInterface *iface = QDBusConnection::sessionBus().interface();

    QDBusConnection::sessionBus().registerObject("/org/freedesktop/Notifications", this);
    auto registration = iface->registerService("org.freedesktop.Notifications",
                                                QDBusConnectionInterface::ReplaceExistingService,
                                                QDBusConnectionInterface::DontAllowReplacement);
}

uint NotificationServer::Notify(const QString &app_name,
                                uint replaces_id,
                                const QString &app_icon,
                                const QString &summary,
                                const QString &body,
                                const QStringList &actions,
                                const QVariantMap &hints,
                                int timeout)
{
    Q_UNUSED(hints);

    uint id = 0;

    const bool wasReplaced = replaces_id > 0;

    if (wasReplaced) {
        id = replaces_id;
    } else {
        if (!m_highestId)
            ++m_highestId;

        id = m_highestId;
        ++m_highestId;
    }

    Notification notification;
    notification.id = id;
    notification.created = QDateTime::currentDateTimeUtc();
    notification.service = message().service();
    notification.summary = summary;
    notification.body = body;
    notification.appName = app_name;
    notification.appIcon = app_icon;
    notification.actions = actions;
    notification.timeout = timeout;

    if (notification.appIcon.startsWith("file://"))
        notification.appIcon = notification.appIcon.replace("file://", "");

    uint pid = 0;
    QDBusReply<uint> pidReply = connection().interface()->servicePid(message().service());
    if (pidReply.isValid()) {
        pid = pidReply.value();
    }

    if (pid > 0) {
        // 查找 app name
        if (notification.appIcon.isEmpty()) {
            // const QString processName = Utils::processNameFromPid(pid);
            // notification.appIcon = processName;
        }
    }

    if (m_lastNotification.appName == notification.appName &&
            m_lastNotification.summary == notification.summary &&
            m_lastNotification.body == notification.body &&
            m_lastNotification.created.msecsTo(notification.created) < 1000) {
        return 0;
    }

    m_lastNotification = notification;

    if (wasReplaced) {
        notification.updated = QDateTime::currentDateTimeUtc();
        emit notificationReplaced(replaces_id, notification);
    } else {
        emit notificationAdded(notification);
    }

    return id;
}

void NotificationServer::CloseNotification(uint id)
{
    Q_UNUSED(id);
}

QStringList NotificationServer::GetCapabilities() const
{
    return QStringList{ QStringLiteral("body"),
                        QStringLiteral("body-hyperlinks"),
                        QStringLiteral("body-markup"),
                        QStringLiteral("body-images"),
                        QStringLiteral("icon-static"),
                        QStringLiteral("actions"),
                        QStringLiteral("persistence"),
                        QStringLiteral("inline-reply"),
                        QStringLiteral("inhibitions") };
}

QString NotificationServer::GetServerInformation(QString &vendor, QString &version, QString &specVersion) const
{
    vendor = "CutefishOS";
    version = "1.0";
    specVersion = "1.2";

    return "Cutefish";
}

uint NotificationServer::Inhibit(const QString &desktop_entry, const QString &reason, const QVariantMap &hints)
{
    return 0;
}

void NotificationServer::UnInhibit(uint cookie)
{
    Q_UNUSED(cookie)
}

bool NotificationServer::inhibited() const
{
    return false;
}

void NotificationServer::RegisterWatcher()
{
    m_notificationWatcher->addWatchedService(message().service());
}

void NotificationServer::UnRegisterWatcher()
{
    m_notificationWatcher->removeWatchedService(message().service());
}

void NotificationServer::InvokeAction(uint id, const QString &actionKey)
{
    Q_EMIT ActionInvoked(id, actionKey);
}

void NotificationServer::closeNotification(uint id, NotificationServer::CloseReason reason)
{
    emit notificationRemoved(id, reason);
}