20
20
#include "NotificationModel.h"
21
21
#include "NotificationServer.h"
22
22
#include "Notification.h"
23
#include <NotificationsAdaptor.h>
23
24
#include <QDBusMetaType>
26
#include <QDBusConnectionInterface>
25
27
#include <QSharedPointer>
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) {
32
DBusTypes::registerNotificationMetaTypes();
34
// Memory managed by Qt
35
new NotificationsAdaptor(this);
31
37
connect(this, SIGNAL(dataChanged(unsigned int)), &m, SLOT(onDataChanged(unsigned int)));
39
if(!m_connection.registerObject(DBUS_PATH, this)) {
40
qWarning() << "Could not register to DBus object.";
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.";
34
51
NotificationServer::~NotificationServer() {
52
m_connection.unregisterService(DBUS_SERVICE_NAME);
38
55
void NotificationServer::invokeAction(unsigned int id, const QString &action) {
67
84
return capabilities;
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();
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");
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;
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);
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();
174
211
notification->setHints(notifyHints);
176
QVariant secondaryIcon = hints[SECONDARY_ICON_HINT].variant();
213
QVariant secondaryIcon = hints[SECONDARY_ICON_HINT];
177
214
notification->setSecondaryIcon(secondaryIcon.toString());
179
QVariant value = hints[VALUE_HINT].variant();
216
QVariant value = hints[VALUE_HINT];
180
217
notification->setValue(value.toInt());
182
219
if(replaces_id) {
192
229
model.removeNotification(id);
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";
199
235
specVersion = "1.1";
236
return "Unity notification server";
202
239
void NotificationServer::onDataChanged(unsigned int id) {