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
240
241
242
243
244
245
246
247
248
|
#include "notificationmanager.h"
#include <QDebug>
#include <QTimer>
#include <QFile>
#include <QSettings>
#include <QDBusInterface>
#include <QDBusPendingReply>
//#include "PersonalNotification.h"
class NotificationManagerPrivate
{
Q_DECLARE_PUBLIC(NotificationManager)
public:
NotificationManagerPrivate(NotificationManager *q)
: q_ptr(q),
interface(NULL),
connected(false)
{ /*...*/ }
NotificationManager *q_ptr;
QDBusInterface *interface;
bool connected;
};
NotificationManager::NotificationManager(Settings *settings, QObject *parent)
: QObject(parent), l(metaObject()->className()), d_ptr(new NotificationManagerPrivate(this)), settings(settings)
{
Q_D(NotificationManager);
QDBusConnection::sessionBus().registerObject("/org/freedesktop/Notifications", this, QDBusConnection::ExportAllSlots);
d->interface = new QDBusInterface("org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus");
d->interface->call("AddMatch", "interface='org.freedesktop.Notifications',member='Notify',type='method_call',eavesdrop='true'");
this->initialize();
}
NotificationManager::~NotificationManager()
{
Q_D(NotificationManager);
delete d;
}
void NotificationManager::initialize(bool notifyError)
{
Q_D(NotificationManager);
bool success = false;
if(d->interface->isValid())
{
success = true;
}
if(!(d->connected = success))
{
QTimer::singleShot(2000, this, SLOT(initialize()));
if(notifyError) emit this->error("Failed to connect to Notifications D-Bus service.");
}
}
QDBusInterface* NotificationManager::interface() const
{
Q_D(const NotificationManager);
return d->interface;
}
QString NotificationManager::getCleanAppName(QString app_name)
{
QString desktopFile = QString("/usr/share/applications/%1.desktop").arg(app_name);
QFile testFile(desktopFile);
if (testFile.exists()) {
QSettings settings(desktopFile, QSettings::IniFormat);
settings.beginGroup("Desktop Entry");
QString cleanName = settings.value("Name").toString();
settings.endGroup();
if (!cleanName.isEmpty()) {
return cleanName;
}
}
return app_name;
}
QStringHash NotificationManager::getCategoryParams(QString category)
{
if (!category.isEmpty()) {
QString categoryConfigFile = QString("/usr/share/lipstick/notificationcategories/%1.conf").arg(category);
QFile testFile(categoryConfigFile);
if (testFile.exists()) {
QStringHash categories;
QSettings settings(categoryConfigFile, QSettings::IniFormat);
const QStringList settingKeys = settings.allKeys();
foreach (const QString &settingKey, settingKeys) {
categories[settingKey] = settings.value(settingKey).toString();
}
return categories;
}
}
return QStringHash();
}
uint NotificationManager::Notify(const QString &app_name, uint replaces_id, const QString &app_icon,
const QString &summary, const QString &body, const QStringList &actions, const QVariantHash &hints, int expire_timeout)
{
Q_UNUSED(replaces_id);
Q_UNUSED(app_icon);
Q_UNUSED(actions);
Q_UNUSED(expire_timeout);
// new place to check notification owner in Sailfish 1.1.6
QString owner = hints.value("x-nemo-owner", "").toString();
qCDebug(l) << Q_FUNC_INFO << "Got notification via dbus from" << this->getCleanAppName(app_name) << " Owner: " << owner;
qCDebug(l) << hints;
// Avoid sending a reply for this method call, since we've received it because we're eavesdropping.
// The actual target of the method call will send the proper reply.
Q_ASSERT(calledFromDBus());
setDelayedReply(true);
// Look up the notification category and its parameters
QString category = hints.value("category", "").toString();
QStringHash categoryParams = this->getCategoryParams(category);
// Ignore transient and hidden notifications (notif hints override category hints)
// Hack this to accept transient -preview and -summary notifications, as we don't know how to decode the actual notifs yet
if (hints.value("transient", categoryParams.value("transient", "false")).toString() == "true") {
qCDebug(l) << "Ignoring transient notification from " << owner;
return 0;
}
else if (hints.value("x-nemo-hidden", "false").toString() == "true" ) {
qCDebug(l) << "Ignoring hidden notification from " << owner;
return 0;
}
if (category == "x-nemo.email") {
if (!settings->property("notificationsEmails").toBool()) {
qCDebug(l) << "Ignoring email notification because of setting!";
return 0;
}
emit this->emailNotify(app_name, body, summary);
}
else if (category == "x-nemo.call.missed") {
if (!settings->property("notificationsMissedCall").toBool()) {
qCDebug(l) << "Ignoring MissedCall notification because of setting!";
return 0;
}
emit this->missedCallNotify(hints.value("x-nemo-preview-summary", summary).toString(),
hints.value("x-nemo-preview-body", body).toString()
);
}
else if (category == "x-nemo.messaging.sms.preview") {
if (!settings->property("notificationsCommhistoryd").toBool()) {
qCDebug(l) << "Ignoring commhistoryd notification because of setting!";
return 0;
}
emit this->smsNotify(hints.value("x-nemo-preview-summary", summary).toString(),
hints.value("x-nemo-preview-body", body).toString()
);
}
else if (hints.value("x-nemo-origin-package").toString() == "com.whatsapp"
|| owner.toLower().contains("whatsup")) {
if (!settings->property("notificationsWhatsapp").toBool()) {
qCDebug(l) << "Ignoring whatsapp notification because of setting!";
return 0;
}
emit this->whatsappNotify(hints.value("x-nemo-preview-summary", summary).toString(),
hints.value("x-nemo-preview-body", body).toString()
);
}
else if (hints.value("x-nemo-origin-package").toString() == "org.telegram.messenger"
|| category.startsWith("harbour.sailorgram")) {
if (!settings->property("notificationsTelegram").toBool()) {
qCDebug(l) << "Ignoring telegram notification because of setting!";
return 0;
}
emit this->telegramNotify(hints.value("x-nemo-preview-summary", summary).toString(),
hints.value("x-nemo-preview-body", body).toString()
);
}
else if (hints.value("x-nemo-origin-package").toString() == "com.google.android.apps.babel"
|| owner == "harbour-hangish") {
if (!settings->property("notificationsHangouts").toBool()) {
qCDebug(l) << "Ignoring telegram notification because of setting!";
return 0;
}
emit this->hangoutsNotify(hints.value("x-nemo-preview-summary", summary).toString(),
hints.value("x-nemo-preview-body", body).toString()
);
}
else if (app_name == "twitter-notifications-client" || owner == "twitter-notifications-client") {
if (!settings->property("notificationsTwitter").toBool()) {
qCDebug(l) << "Ignoring twitter notification because of setting!";
return 0;
}
emit this->twitterNotify(hints.value("x-nemo-preview-summary", summary).toString(),
hints.value("x-nemo-preview-body", body).toString()
);
} else {
// Prioritize x-nemo-preview* over dbus direct summary and body
QString subject = hints.value("x-nemo-preview-summary", summary).toString();
QString data = hints.value("x-nemo-preview-body", body).toString();
int prio = categoryParams.value("x-nemo-priority", "0").toInt();
qCDebug(l) << "MSG Prio:" << prio;
if (!settings->property("notificationsAll").toBool() && prio <= 10) {
qCDebug(l) << "Ignoring notification because of setting! (all)";
return 0;
}
if (!settings->property("notificationsOther").toBool() && prio < 90) {
qCDebug(l) << "Ignoring notification because of setting! (other)";
return 0;
}
if (subject.isEmpty()) {
subject = summary;
}
if (data.isEmpty()) {
data = body;
}
//Prioritize data over subject
if (data.isEmpty() && !subject.isEmpty()) {
data = subject;
subject = "";
}
//Never send empty data and subject
if (data.isEmpty() && subject.isEmpty()) {
qCWarning(l) << Q_FUNC_INFO << "Empty subject and data in dbus app:" << app_name;
return 0;
}
emit this->emailNotify(this->getCleanAppName(app_name), data, subject);
}
return 0;
}
|