summaryrefslogtreecommitdiff
path: root/notification.cpp
blob: ba8456ea7fc2a34a7dc6a83506dbf13467a534ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *  libwatchfish - library with common functionality for SailfishOS smartwatch connector programs.
 *  Copyright (C) 2015 Javier S. Pedro <dev.git@javispedro.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusPendingCall>
#include <QtDBus/QDBusMessage>
#include "notification.h"

namespace watchfish
{

namespace
{
struct Action
{
	QString service;
	QString path;
	QString iface;
	QString method;
	QStringList args;
};
}

struct NotificationPrivate
{
	uint id;
	QString sender;
	QString summary;
	QString body;
	QDateTime timestamp;
	QString icon;
	quint8 urgency;
	bool transient;
	QString previewSummary;
	QString previewBody;
	QHash<QString, Action> actions;
};

Notification::Notification(uint id, QObject *parent) : QObject(parent), d_ptr(new NotificationPrivate)
{
	Q_D(Notification);
	d->id = id;
}

Notification::~Notification()
{
}

uint Notification::id() const
{
	Q_D(const Notification);
	return d->id;
}

QString Notification::sender() const
{
	Q_D(const Notification);
	return d->sender;
}

void Notification::setSender(const QString &sender)
{
	Q_D(Notification);
	if (sender != d->sender) {
		d->sender = sender;
		emit senderChanged();
	}
}

QString Notification::summary() const
{
	Q_D(const Notification);
	return d->summary;
}

void Notification::setSummary(const QString &summary)
{
	Q_D(Notification);
	if (summary != d->summary) {
		d->summary = summary;
		emit summaryChanged();
	}
}

QString Notification::body() const
{
	Q_D(const Notification);
	return d->body;
}

void Notification::setBody(const QString &body)
{
	Q_D(Notification);
	if (body != d->body) {
		d->body = body;
		emit bodyChanged();
	}
}

QDateTime Notification::timestamp() const
{
	Q_D(const Notification);
	return d->timestamp;
}

void Notification::setTimestamp(const QDateTime &dt)
{
	Q_D(Notification);
	if (dt != d->timestamp) {
		d->timestamp = dt;
		emit timestampChanged();
	}
}

QString Notification::icon() const
{
	Q_D(const Notification);
	return d->icon;
}

void Notification::setIcon(const QString &icon)
{
	Q_D(Notification);
	if (icon != d->icon) {
		d->icon = icon;
		emit iconChanged();
	}
}

int Notification::urgency() const
{
	Q_D(const Notification);
	return d->urgency;
}

void Notification::setUrgency(int urgency)
{
	Q_D(Notification);
	if (urgency != d->urgency) {
		d->urgency = urgency;
		emit urgencyChanged();
	}
}

bool Notification::transient() const
{
	Q_D(const Notification);
	return d->transient;
}

void Notification::setTransient(bool transient)
{
	Q_D(Notification);
	if (transient != d->transient) {
		d->transient = transient;
		emit transientChanged();
	}
}

QString Notification::previewSummary() const
{
	Q_D(const Notification);
	return d->previewSummary;
}

void Notification::setPreviewSummary(const QString &summary)
{
	Q_D(Notification);
	if (summary != d->previewSummary) {
		d->previewSummary = summary;
		emit previewSummaryChanged();
	}
}

QString Notification::previewBody() const
{
	Q_D(const Notification);
	return d->previewBody;
}

void Notification::setPreviewBody(const QString &body)
{
	Q_D(Notification);
	if (body != d->previewBody) {
		d->previewBody = body;
		emit previewBodyChanged();
	}
}

QStringList Notification::actions() const
{
	Q_D(const Notification);
	return d->actions.keys();
}

void Notification::addDBusAction(const QString &action, const QString &service, const QString &path, const QString &iface, const QString &method, const QStringList &args)
{
	Q_D(Notification);
	Action &a = d->actions[action];
	a.service = service;
	a.path = path;
	a.iface = iface;
	a.method = method;
	a.args = args;
}

void Notification::invokeAction(const QString &action)
{
	Q_D(Notification);
	if (d->actions.contains(action)) {
		const Action &a = d->actions[action];
		if (!a.service.isEmpty()) {
			QDBusMessage msg = QDBusMessage::createMethodCall(a.service, a.path, a.iface, a.method);
			foreach (const QString &arg, a.args) {
				msg << arg;
			}
			QDBusConnection::sessionBus().asyncCall(msg);
		}
	}
}

void Notification::close()
{
	Q_D(Notification);
	QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Notifications",
													  "/org/freedesktop/Notifications",
													  "org.freedesktop.Notifications",
													  "CloseNotification");
	msg << quint32(d->id);
	QDBusConnection::sessionBus().asyncCall(msg);
}

}