~lukas-kde/unity-notifications/cmdLineCheck

« back to all changes in this revision

Viewing changes to src/NotificationServer.cpp

Notifications are now owned by the client that opened them
Approved by: Charles Kerr

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <QDBusConnectionInterface>
27
27
#include <QSharedPointer>
28
28
 
 
29
static const char* LOCAL_OWNER = "local";
 
30
 
 
31
static bool isAuthorised(const QString& clientId, QSharedPointer<Notification> notification)
 
32
{
 
33
    return (clientId == LOCAL_OWNER) || (notification->getClientId() == clientId);
 
34
}
 
35
 
29
36
NotificationServer::NotificationServer(const QDBusConnection& connection, NotificationModel &m, QObject *parent) :
30
37
    QObject(parent), model(m), idCounter(0), m_connection(connection) {
31
38
 
146
153
        idCounter = 1;
147
154
}
148
155
 
 
156
QString NotificationServer::messageSender() {
 
157
    QString sender(LOCAL_OWNER);
 
158
        if (calledFromDBus()) {
 
159
                sender = message().service();
 
160
        }
 
161
        return sender;
 
162
}
 
163
 
149
164
unsigned int NotificationServer::Notify(const QString &app_name, uint replaces_id,
150
165
                           const QString &app_icon, const QString &summary,
151
166
                           const QString &body, const QStringList &actions,
157
172
    int currentId = 0;
158
173
 
159
174
    bool newNotification = true;
 
175
    QString clientId = messageSender();
160
176
 
161
177
    QSharedPointer<Notification> notification;
162
178
    if (replaces_id != 0) {
163
179
        if (model.hasNotification(replaces_id)) {
164
180
            newNotification = false;
165
181
            notification = model.getNotification(replaces_id);
 
182
            if (!isAuthorised(clientId, notification)) {
 
183
                auto message =
 
184
                        QString::fromUtf8(
 
185
                                "Client '%1' tried to update notification %2, which it does not own.").arg(
 
186
                                clientId).arg(replaces_id);
 
187
                qWarning() << message;
 
188
                sendErrorReply(QDBusError::InvalidArgs, message);
 
189
                return FAILURE;
 
190
            }
166
191
        } else {
167
192
            notification = buildNotification(replaces_id, hints);
168
193
        }
240
265
    QVariant value = hints[VALUE_HINT];
241
266
    notification->setValue(value.toInt());
242
267
 
243
 
    if(newNotification) {
 
268
    notification->setClientId(clientId);
 
269
 
 
270
    if (newNotification) {
244
271
        model.insertNotification(notification);
245
272
    } else {
246
273
        model.notificationUpdated(currentId);
249
276
}
250
277
 
251
278
void NotificationServer::CloseNotification (unsigned int id) {
252
 
    Q_EMIT NotificationClosed(id, 1);
253
 
    model.removeNotification(id);
 
279
    if (model.hasNotification(id)) {
 
280
        auto clientId = messageSender();
 
281
        auto notification = model.getNotification(id);
 
282
        if (!isAuthorised(clientId, notification))
 
283
        {
 
284
            auto message =
 
285
                    QString::fromUtf8(
 
286
                            "Client '%1' tried to close notification %2, which it does not own.").arg(
 
287
                            clientId).arg(id);
 
288
            qWarning() << message;
 
289
            sendErrorReply(QDBusError::InvalidArgs, message);
 
290
            return;
 
291
        }
 
292
        Q_EMIT NotificationClosed(id, 1);
 
293
        model.removeNotification(id);
 
294
    }
254
295
}
255
296
 
256
297
QString NotificationServer::GetServerInformation (QString &vendor, QString &version, QString &specVersion) const {