~arjunak234-deactivatedaccount/kde-workspace/fix125114

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/notifications/notificationsengine.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2008 Dmitry Suzdalev <dimsuz@gmail.com>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
18
*/
 
19
 
 
20
#include "notificationsengine.h"
 
21
#include "notificationservice.h"
 
22
#include "notificationsadaptor.h"
 
23
 
 
24
#include <KDebug>
 
25
 
 
26
#include <Plasma/DataContainer>
 
27
#include <Plasma/Service>
 
28
 
 
29
#include <QImage>
 
30
 
 
31
#include <kiconloader.h>
 
32
 
 
33
NotificationsEngine::NotificationsEngine( QObject* parent, const QVariantList& args )
 
34
    : Plasma::DataEngine( parent, args ), m_nextId( 1 )
 
35
{
 
36
    new NotificationsAdaptor(this);
 
37
 
 
38
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
39
    dbus.registerService( "org.freedesktop.Notifications" );
 
40
    dbus.registerObject( "/org/freedesktop/Notifications", this );
 
41
}
 
42
 
 
43
NotificationsEngine::~NotificationsEngine()
 
44
{
 
45
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
46
    dbus.unregisterService( "org.freedesktop.Notifications" );
 
47
}
 
48
 
 
49
void NotificationsEngine::init()
 
50
{
 
51
}
 
52
 
 
53
inline void copyLineRGB32(QRgb* dst, const char* src, int width)
 
54
{
 
55
    const char* end = src + width * 3;
 
56
    for (; src != end; ++dst, src+=3) {
 
57
        *dst = qRgb(src[0], src[1], src[2]);
 
58
    }
 
59
}
 
60
 
 
61
inline void copyLineARGB32(QRgb* dst, const char* src, int width)
 
62
{
 
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]);
 
66
    }
 
67
}
 
68
 
 
69
static QImage decodeNotificationSpecImageHint(const QDBusArgument& arg)
 
70
{
 
71
    int width, height, rowStride, hasAlpha, bitsPerSample, channels;
 
72
    QByteArray pixels;
 
73
    char* ptr;
 
74
    char* end;
 
75
 
 
76
    arg.beginStructure();
 
77
    arg >> width >> height >> rowStride >> hasAlpha >> bitsPerSample >> channels >> pixels;
 
78
    arg.endStructure();
 
79
    //kDebug() << width << height << rowStride << hasAlpha << bitsPerSample << channels;
 
80
 
 
81
    #define SANITY_CHECK(condition) \
 
82
    if (!(condition)) { \
 
83
        kWarning() << "Sanity check failed on" << #condition; \
 
84
        return QImage(); \
 
85
    }
 
86
 
 
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);
 
92
 
 
93
    #undef SANITY_CHECK
 
94
 
 
95
    QImage::Format format = QImage::Format_Invalid;
 
96
    void (*fcn)(QRgb*, const char*, int) = 0;
 
97
    if (bitsPerSample == 8) {
 
98
        if (channels == 4) {
 
99
            format = QImage::Format_ARGB32;
 
100
            fcn = copyLineARGB32;
 
101
        } else if (channels == 3) {
 
102
            format = QImage::Format_RGB32;
 
103
            fcn = copyLineRGB32;
 
104
        }
 
105
    }
 
106
    if (format == QImage::Format_Invalid) {
 
107
        kWarning() << "Unsupported image format (hasAlpha:" << hasAlpha << "bitsPerSample:" << bitsPerSample << "channels:" << channels << ")";
 
108
        return QImage();
 
109
    }
 
110
 
 
111
    QImage image(width, height, format);
 
112
    ptr = pixels.data();
 
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;
 
117
            break;
 
118
        }
 
119
        fcn((QRgb*)image.scanLine(y), ptr, width);
 
120
    }
 
121
 
 
122
    return image;
 
123
}
 
124
 
 
125
static QString findImageForSpecImagePath(const QString &_path)
 
126
{
 
127
    QString path = _path;
 
128
    if (path.startsWith(QLatin1String("file:"))) {
 
129
        KUrl url(path);
 
130
        path = url.toLocalFile();
 
131
    }
 
132
    return KIconLoader::global()->iconPath(path, -KIconLoader::SizeHuge,
 
133
                                           true /* canReturnNull */);
 
134
}
 
135
 
 
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)
 
139
{
 
140
    uint id = 0;
 
141
    id = replaces_id ? replaces_id : m_nextId++;
 
142
 
 
143
    QString appname_str = app_name;
 
144
    if (appname_str.isEmpty()) {
 
145
        appname_str = i18n("Unknown Application");
 
146
    }
 
147
 
 
148
    if (timeout == -1) {
 
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;
 
153
 
 
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
 
156
        // flash
 
157
        timeout = 2000 + qMin(timeout, 3000);
 
158
    }
 
159
 
 
160
    const QString source = QString("notification %1").arg(id);
 
161
    if (replaces_id) {
 
162
        Plasma::DataContainer *container = containerForSource(source);
 
163
        if (container && container->data()["expireTimeout"].toInt() != timeout) {
 
164
            int timerId = m_sourceTimers.value(source);
 
165
            killTimer(timerId);
 
166
            m_sourceTimers.remove(source);
 
167
            m_timeouts.remove(timerId);
 
168
        }
 
169
    }
 
170
 
 
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);
 
179
 
 
180
    QImage image;
 
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()) {
 
187
            image.load(path);
 
188
        }
 
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);
 
195
    }
 
196
    notificationData.insert("image", image);
 
197
 
 
198
    if (hints.contains("urgency")) {
 
199
        notificationData.insert("urgency", hints["urgency"].toInt());
 
200
    }
 
201
 
 
202
    setData(source, notificationData );
 
203
 
 
204
    if (timeout) {
 
205
        int timerId = startTimer(timeout);
 
206
        m_sourceTimers.insert(source, timerId);
 
207
        m_timeouts.insert(timerId, source);
 
208
    }
 
209
 
 
210
    return id;
 
211
}
 
212
 
 
213
void NotificationsEngine::timerEvent(QTimerEvent *event)
 
214
{
 
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);
 
222
        return;
 
223
    }
 
224
 
 
225
    Plasma::DataEngine::timerEvent(event);
 
226
}
 
227
 
 
228
void NotificationsEngine::CloseNotification(uint id)
 
229
{
 
230
    removeSource(QString("notification %1").arg(id));
 
231
    emit NotificationClosed(id, 3);
 
232
}
 
233
 
 
234
void NotificationsEngine::userClosedNotification(uint id)
 
235
{
 
236
    removeSource(QString("notification %1").arg(id));
 
237
    emit NotificationClosed(id, 2);
 
238
}
 
239
 
 
240
Plasma::Service* NotificationsEngine::serviceForSource(const QString& source)
 
241
{
 
242
    return new NotificationService(this, source);
 
243
}
 
244
 
 
245
QStringList NotificationsEngine::GetCapabilities()
 
246
{
 
247
    return QStringList()
 
248
        << "body"
 
249
        << "body-hyperlinks"
 
250
        << "body-markup"
 
251
        << "icon-static"
 
252
        << "actions"
 
253
        ;
 
254
}
 
255
 
 
256
// FIXME: Signature is ugly
 
257
QString NotificationsEngine::GetServerInformation(QString& vendor, QString& version, QString& specVersion)
 
258
{
 
259
    vendor = "KDE";
 
260
    version = "1.0"; // FIXME
 
261
    specVersion = "1.1";
 
262
    return "Plasma";
 
263
}
 
264
 
 
265
void NotificationsEngine::createNotification(const QString &appName, const QString &appIcon, const QString &summary, const QString &body, int timeout)
 
266
{
 
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);
 
275
 
 
276
    setData(source, notificationData );
 
277
}
 
278
 
 
279
K_EXPORT_PLASMA_DATAENGINE(notifications, NotificationsEngine)
 
280
 
 
281
#include "notificationsengine.moc"