-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
NotificationServer.cxx
183 lines (140 loc) · 5.3 KB
/
NotificationServer.cxx
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
/*
This file is part of liquidshell.
SPDX-FileCopyrightText: 2017 - 2024 Martin Koller <kollix@aon.at>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <NotificationServer.hxx>
#include <NotificationList.hxx>
#include <notificationsadaptor.h>
#include <QIcon>
#include <QDBusConnection>
#include <QDebug>
#include <KLocalizedString>
#include <KService>
#include <KIconLoader>
//--------------------------------------------------------------------------------
NotificationServer::NotificationServer(QWidget *parent)
: SysTrayItem(parent, "preferences-desktop-notification")
{
new NotificationsAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
if ( dbus.registerService("org.freedesktop.Notifications") )
{
if ( !dbus.registerObject("/org/freedesktop/Notifications", this) )
dbus.unregisterService("org.freedesktop.Notifications");
}
notificationList = new NotificationList(this);
connect(notificationList, &NotificationList::listNowEmpty, this, &NotificationServer::hide);
connect(notificationList, &NotificationList::itemsCountChanged,
[this]()
{
show();
setToolTip(makeToolTip());
}
);
hide();
setAvoidPopup(getAvoidPopup()); // adjust icon for current state
connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this,
[this]() { setAvoidPopup(getAvoidPopup()); });
}
//--------------------------------------------------------------------------------
QString NotificationServer::makeToolTip() const
{
QString tip = "<html>";
tip += i18np("%1 notification", "%1 notifications", notificationList->itemCount());
if ( notificationList->itemCount() < 4 )
{
for (const NotifyItem *item : notificationList->getItems())
{
tip += "<hr>";
tip += item->timeLabel->text() + " ";
QString title = (item->appName == item->summary) ? item->appName : (item->appName + ": " + item->summary);
tip += "<b>" + title + "</b>";
tip += "<br>" + item->body;
}
}
tip += "</html>";
return tip;
}
//--------------------------------------------------------------------------------
QStringList NotificationServer::GetCapabilities()
{
return QStringList()
<< "body"
<< "body-hyperlinks"
<< "body-images"
<< "body-markup"
<< "icon-static"
<< "persistence"
<< "actions"
;
}
//--------------------------------------------------------------------------------
void NotificationServer::CloseNotification(uint id)
{
//qDebug() << "CloseNotification" << id;
notificationList->closeItem(id);
emit NotificationClosed(id, CloseReason::Closed);
}
//--------------------------------------------------------------------------------
QString NotificationServer::GetServerInformation(QString &vendor, QString &version, QString &spec_version)
{
vendor = "KDE";
version = "1.0";
spec_version = "1.2";
return "liquidshell";
}
//--------------------------------------------------------------------------------
uint NotificationServer::Notify(const QString &app_name, uint replaces_id, const QString &app_icon,
const QString &summary, const QString &theBody, const QStringList &actions,
const QVariantMap &hints, int timeout)
{
//qDebug() << "app" << app_name << "summary" << summary << "body" << theBody << "timeout" << timeout
//<< "replaceId" << replaces_id << "hints" << hints << "actions" << actions << "app_icon" << app_icon;
uint newId;
if ( replaces_id != 0 )
notificationList->closeItem(newId = replaces_id); // reuse id
else
newId = notifyId++;
QString body(theBody);
body.replace("\n", "<br>");
// icon preference order: https://specifications.freedesktop.org/notification-spec/latest/ar01s05.html
QIcon icon;
if ( hints.contains("image-data") )
icon = hints["image-data"].value<QIcon>();
if ( icon.isNull() && hints.contains("image-path") )
icon = QIcon(hints["image-path"].toString());
if ( icon.isNull() && !app_icon.isEmpty() )
icon = QIcon::fromTheme(app_icon);
if ( icon.isNull() && hints.contains("icon_data") )
icon = hints["icon_data"].value<QIcon>();
QString appName = app_name;
if ( appName.isEmpty() && hints.contains("desktop-entry") )
{
KService::Ptr service = KService::serviceByDesktopName(hints["desktop-entry"].toString().toLower());
if ( service )
appName = service->name();
}
notificationList->addItem(newId, appName, summary, body, icon, actions, hints, timeout);
return newId;
}
//--------------------------------------------------------------------------------
QWidget *NotificationServer::getDetailsList()
{
return notificationList;
}
//--------------------------------------------------------------------------------
void NotificationServer::setAvoidPopup(bool avoid)
{
notificationList->setAvoidPopup(avoid);
if ( avoid )
setPixmap(QIcon::fromTheme(iconName).pixmap(size(), QIcon::Disabled));
else
setPixmap(QIcon::fromTheme(iconName).pixmap(size()));
}
//--------------------------------------------------------------------------------
bool NotificationServer::getAvoidPopup() const
{
return notificationList->getAvoidPopup();
}
//--------------------------------------------------------------------------------