2
* Copyright (C) 2008 Dmitry Suzdalev <dimsuz@gmail.com>
4
* This program is free software you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "notificationsengine.h"
21
#include "notificationservice.h"
22
#include "notificationsadaptor.h"
26
#include <Plasma/DataContainer>
27
#include <Plasma/Service>
31
#include <kiconloader.h>
33
NotificationsEngine::NotificationsEngine( QObject* parent, const QVariantList& args )
34
: Plasma::DataEngine( parent, args ), m_nextId( 1 )
36
new NotificationsAdaptor(this);
38
QDBusConnection dbus = QDBusConnection::sessionBus();
39
dbus.registerService( "org.freedesktop.Notifications" );
40
dbus.registerObject( "/org/freedesktop/Notifications", this );
43
NotificationsEngine::~NotificationsEngine()
45
QDBusConnection dbus = QDBusConnection::sessionBus();
46
dbus.unregisterService( "org.freedesktop.Notifications" );
49
void NotificationsEngine::init()
53
inline void copyLineRGB32(QRgb* dst, const char* src, int width)
55
const char* end = src + width * 3;
56
for (; src != end; ++dst, src+=3) {
57
*dst = qRgb(src[0], src[1], src[2]);
61
inline void copyLineARGB32(QRgb* dst, const char* src, int width)
63
const char* end = src + width * 4;
64
for (; src != end; ++dst, src+=4) {
65
*dst = qRgba(src[0], src[1], src[2], src[3]);
69
static QImage decodeNotificationSpecImageHint(const QDBusArgument& arg)
71
int width, height, rowStride, hasAlpha, bitsPerSample, channels;
77
arg >> width >> height >> rowStride >> hasAlpha >> bitsPerSample >> channels >> pixels;
79
//kDebug() << width << height << rowStride << hasAlpha << bitsPerSample << channels;
81
#define SANITY_CHECK(condition) \
83
kWarning() << "Sanity check failed on" << #condition; \
87
SANITY_CHECK(width > 0);
88
SANITY_CHECK(width < 2048);
89
SANITY_CHECK(height > 0);
90
SANITY_CHECK(height < 2048);
91
SANITY_CHECK(rowStride > 0);
95
QImage::Format format = QImage::Format_Invalid;
96
void (*fcn)(QRgb*, const char*, int) = 0;
97
if (bitsPerSample == 8) {
99
format = QImage::Format_ARGB32;
100
fcn = copyLineARGB32;
101
} else if (channels == 3) {
102
format = QImage::Format_RGB32;
106
if (format == QImage::Format_Invalid) {
107
kWarning() << "Unsupported image format (hasAlpha:" << hasAlpha << "bitsPerSample:" << bitsPerSample << "channels:" << channels << ")";
111
QImage image(width, height, format);
113
end = ptr + pixels.length();
114
for (int y=0; y<height; ++y, ptr += rowStride) {
115
if (ptr + channels * width > end) {
116
kWarning() << "Image data is incomplete. y:" << y << "height:" << height;
119
fcn((QRgb*)image.scanLine(y), ptr, width);
125
static QString findImageForSpecImagePath(const QString &_path)
127
QString path = _path;
128
if (path.startsWith(QLatin1String("file:"))) {
130
path = url.toLocalFile();
132
return KIconLoader::global()->iconPath(path, -KIconLoader::SizeHuge,
133
true /* canReturnNull */);
136
uint NotificationsEngine::Notify(const QString &app_name, uint replaces_id,
137
const QString &app_icon, const QString &summary, const QString &body,
138
const QStringList &actions, const QVariantMap &hints, int timeout)
141
id = replaces_id ? replaces_id : m_nextId++;
143
QString appname_str = app_name;
144
if (appname_str.isEmpty()) {
145
appname_str = i18n("Unknown Application");
149
const int AVERAGE_WORD_LENGTH = 6;
150
const int WORD_PER_MINUTE = 250;
151
int count = summary.length() + body.length();
152
timeout = 60000 * count / AVERAGE_WORD_LENGTH / WORD_PER_MINUTE;
154
// Add two seconds for the user to notice the notification, and ensure
155
// it last at least five seconds, otherwise all the user see is a
157
timeout = 2000 + qMin(timeout, 3000);
160
const QString source = QString("notification %1").arg(id);
162
Plasma::DataContainer *container = containerForSource(source);
163
if (container && container->data()["expireTimeout"].toInt() != timeout) {
164
int timerId = m_sourceTimers.value(source);
166
m_sourceTimers.remove(source);
167
m_timeouts.remove(timerId);
171
Plasma::DataEngine::Data notificationData;
172
notificationData.insert("id", QString::number(id));
173
notificationData.insert("appName", appname_str);
174
notificationData.insert("appIcon", app_icon);
175
notificationData.insert("summary", summary);
176
notificationData.insert("body", body);
177
notificationData.insert("actions", actions);
178
notificationData.insert("expireTimeout", timeout);
181
if (hints.contains("image_data")) {
182
QDBusArgument arg = hints["image_data"].value<QDBusArgument>();
183
image = decodeNotificationSpecImageHint(arg);
184
} else if (hints.contains("image_path")) {
185
QString path = findImageForSpecImagePath(hints["image_path"].toString());
186
if (!path.isEmpty()) {
189
} else if (hints.contains("icon_data")) {
190
// This hint was in use in version 1.0 of the spec but has been
191
// replaced by "image_data" in version 1.1. We need to support it for
192
// users of the 1.0 version of the spec.
193
QDBusArgument arg = hints["icon_data"].value<QDBusArgument>();
194
image = decodeNotificationSpecImageHint(arg);
196
notificationData.insert("image", image);
198
if (hints.contains("urgency")) {
199
notificationData.insert("urgency", hints["urgency"].toInt());
202
setData(source, notificationData );
205
int timerId = startTimer(timeout);
206
m_sourceTimers.insert(source, timerId);
207
m_timeouts.insert(timerId, source);
213
void NotificationsEngine::timerEvent(QTimerEvent *event)
215
const QString source = m_timeouts.value(event->timerId());
216
if (!source.isEmpty()) {
217
killTimer(event->timerId());
218
m_sourceTimers.remove(source);
219
m_timeouts.remove(event->timerId());
220
removeSource(source);
221
emit NotificationClosed(source.split(" ").last().toInt(), 1);
225
Plasma::DataEngine::timerEvent(event);
228
void NotificationsEngine::CloseNotification(uint id)
230
removeSource(QString("notification %1").arg(id));
231
emit NotificationClosed(id, 3);
234
void NotificationsEngine::userClosedNotification(uint id)
236
removeSource(QString("notification %1").arg(id));
237
emit NotificationClosed(id, 2);
240
Plasma::Service* NotificationsEngine::serviceForSource(const QString& source)
242
return new NotificationService(this, source);
245
QStringList NotificationsEngine::GetCapabilities()
256
// FIXME: Signature is ugly
257
QString NotificationsEngine::GetServerInformation(QString& vendor, QString& version, QString& specVersion)
260
version = "1.0"; // FIXME
265
void NotificationsEngine::createNotification(const QString &appName, const QString &appIcon, const QString &summary, const QString &body, int timeout)
267
const QString source = QString("notification %1").arg(++m_nextId);
268
Plasma::DataEngine::Data notificationData;
269
notificationData.insert("id", QString::number(m_nextId));
270
notificationData.insert("appName", appName);
271
notificationData.insert("appIcon", appIcon);
272
notificationData.insert("summary", summary);
273
notificationData.insert("body", body);
274
notificationData.insert("expireTimeout", timeout);
276
setData(source, notificationData );
279
K_EXPORT_PLASMA_DATAENGINE(notifications, NotificationsEngine)
281
#include "notificationsengine.moc"