~unity-api-team/unity-notifications/trunk

« back to all changes in this revision

Viewing changes to src/NotificationServer.cpp

  • Committer: CI Train Bot
  • Author(s): Pete Woods
  • Date: 2015-06-23 15:50:11 UTC
  • mfrom: (224.1.8 add-dbus-tests)
  • Revision ID: ci-train-bot@canonical.com-20150623155011-bnt9ubw5elnay015
Add end to end DBus tests and use XML introspection data
Approved by: Charles Kerr

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include "NotificationModel.h"
21
21
#include "NotificationServer.h"
22
22
#include "Notification.h"
 
23
#include <NotificationsAdaptor.h>
23
24
#include <QDBusMetaType>
24
25
#include <QDebug>
 
26
#include <QDBusConnectionInterface>
25
27
#include <QSharedPointer>
26
28
 
27
 
NotificationServer::NotificationServer(NotificationModel &m, QObject *parent) :
28
 
    QDBusAbstractAdaptor(parent), model(m), idCounter(1) {
29
 
    qDBusRegisterMetaType<Hints>();
 
29
NotificationServer::NotificationServer(const QDBusConnection& connection, NotificationModel &m, QObject *parent) :
 
30
    QObject(parent), model(m), idCounter(1), m_connection(connection) {
 
31
 
 
32
    DBusTypes::registerNotificationMetaTypes();
 
33
 
 
34
    // Memory managed by Qt
 
35
    new NotificationsAdaptor(this);
30
36
 
31
37
    connect(this, SIGNAL(dataChanged(unsigned int)), &m, SLOT(onDataChanged(unsigned int)));
 
38
 
 
39
    if(!m_connection.registerObject(DBUS_PATH, this)) {
 
40
        qWarning() << "Could not register to DBus object.";
 
41
    }
 
42
 
 
43
    QDBusConnectionInterface *iface = m_connection.interface();
 
44
    auto reply = iface->registerService(DBUS_SERVICE_NAME, QDBusConnectionInterface::ReplaceExistingService,
 
45
                                        QDBusConnectionInterface::DontAllowReplacement);
 
46
    if(!reply.isValid() || reply.value() != QDBusConnectionInterface::ServiceRegistered) {
 
47
        qWarning() << "Notification DBus name already taken.";
 
48
    }
32
49
}
33
50
 
34
51
NotificationServer::~NotificationServer() {
35
 
 
 
52
    m_connection.unregisterService(DBUS_SERVICE_NAME);
36
53
}
37
54
 
38
55
void NotificationServer::invokeAction(unsigned int id, const QString &action) {
67
84
    return capabilities;
68
85
}
69
86
 
70
 
Notification* NotificationServer::buildNotification(NotificationID id, const Hints &hints) {
 
87
NotificationDataList NotificationServer::GetNotifications(const QString &app_name) {
 
88
    NotificationDataList results;
 
89
    for (auto notification: model.getAllNotifications()) {
 
90
        NotificationData data;
 
91
        data.appName = app_name;
 
92
        data.id = notification->getID();
 
93
        data.appIcon = notification->getIcon();
 
94
        data.summary = notification->getSummary();
 
95
        data.body = notification->getBody();
 
96
        data.actions = notification->getActions()->getRawActions();
 
97
        data.hints = notification->getHints();
 
98
        data.expireTimeout = notification->getDisplayTime();
 
99
 
 
100
        results << data;
 
101
    }
 
102
 
 
103
    return results;
 
104
}
 
105
 
 
106
Notification* NotificationServer::buildNotification(NotificationID id, const QVariantMap &hints) {
71
107
    int expireTimeout = 0;
72
108
    Notification::Urgency urg = Notification::Urgency::Low;
73
109
    if(hints.find(URGENCY_HINT) != hints.end()) {
74
 
        QVariant u = hints[URGENCY_HINT].variant();
 
110
        QVariant u = hints[URGENCY_HINT];
75
111
        if(!u.canConvert(QVariant::Int)) {
76
112
            fprintf(stderr, "Invalid urgency value.\n");
77
113
        } else {
84
120
        expireTimeout = 3000;
85
121
        ntype = Notification::Type::Confirmation;
86
122
    } else if (hints.find(SNAP_HINT) != hints.end()) {
87
 
        QVariant u = hints[TIMEOUT_HINT].variant();
 
123
        QVariant u = hints[TIMEOUT_HINT];
88
124
        if(!u.canConvert(QVariant::Int)) {
89
125
            expireTimeout = 60000;
90
126
        } else {
104
140
    return n;
105
141
}
106
142
 
107
 
unsigned int NotificationServer::Notify (const QString &app_name, unsigned int replaces_id, const QString &app_icon,
108
 
        const QString &summary, const QString &body,
109
 
        const QStringList &actions, const Hints &hints, int expire_timeout) {
 
143
unsigned int NotificationServer::Notify(const QString &app_name, uint replaces_id,
 
144
                           const QString &app_icon, const QString &summary,
 
145
                           const QString &body, const QStringList &actions,
 
146
                           const QVariantMap &hints, int expire_timeout) {
110
147
    const unsigned int FAILURE = 0; // Is this correct?
111
148
    const int minActions = 4;
112
149
    const int maxActions = 14;
168
205
    notification->setSummary(summary);
169
206
 
170
207
    QVariantMap notifyHints;
171
 
    for (Hints::const_iterator iter = hints.constBegin(), end = hints.constEnd(); iter != end; ++iter) {
172
 
        notifyHints[iter.key()] = iter.value().variant();
 
208
    for (auto iter = hints.constBegin(), end = hints.constEnd(); iter != end; ++iter) {
 
209
        notifyHints[iter.key()] = iter.value();
173
210
    }
174
211
    notification->setHints(notifyHints);
175
212
 
176
 
    QVariant secondaryIcon = hints[SECONDARY_ICON_HINT].variant();
 
213
    QVariant secondaryIcon = hints[SECONDARY_ICON_HINT];
177
214
    notification->setSecondaryIcon(secondaryIcon.toString());
178
215
 
179
 
    QVariant value = hints[VALUE_HINT].variant();
 
216
    QVariant value = hints[VALUE_HINT];
180
217
    notification->setValue(value.toInt());
181
218
 
182
219
    if(replaces_id) {
192
229
    model.removeNotification(id);
193
230
}
194
231
 
195
 
void NotificationServer::GetServerInformation (QString &name, QString &vendor, QString &version, QString &specVersion) const {
196
 
    name = "Unity notification server";
 
232
QString NotificationServer::GetServerInformation (QString &vendor, QString &version, QString &specVersion) const {
197
233
    vendor = "Canonical Ltd";
198
234
    version = "1.2";
199
235
    specVersion = "1.1";
 
236
    return "Unity notification server";
200
237
}
201
238
 
202
239
void NotificationServer::onDataChanged(unsigned int id) {