-
Notifications
You must be signed in to change notification settings - Fork 7
/
notifications_client.dart
368 lines (318 loc) · 13.2 KB
/
notifications_client.dart
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import 'dart:async';
import 'package:dbus/dbus.dart';
/// Contains information reported by the notifications server.
class NotificationsServerInformation {
/// Name of the server.
final String name;
/// The vendor of the sever.
final String vendor;
/// The version of the server.
final String version;
/// The notifications specification version this server implements.
final String specVersion;
const NotificationsServerInformation(
this.name, this.vendor, this.version, this.specVersion);
@override
String toString() =>
"$runtimeType(name: '$name', vendor: '$vendor', version: '$version', specVersion: '$specVersion')";
}
/// Categories of notifications.
class NotificationCategory {
/// Name of this category.
final String name;
const NotificationCategory(this.name);
factory NotificationCategory.device() => NotificationCategory('device');
factory NotificationCategory.deviceAdded() =>
NotificationCategory('device.added');
factory NotificationCategory.deviceError() =>
NotificationCategory('device.error');
factory NotificationCategory.deviceRemoved() =>
NotificationCategory('device.removed');
factory NotificationCategory.email() => NotificationCategory('email');
factory NotificationCategory.emailArrived() =>
NotificationCategory('email.arrived');
factory NotificationCategory.emailBounced() =>
NotificationCategory('email.bounced');
factory NotificationCategory.im() => NotificationCategory('im');
factory NotificationCategory.imError() => NotificationCategory('imError');
factory NotificationCategory.imReceived() =>
NotificationCategory('imReceived');
factory NotificationCategory.network() => NotificationCategory('network');
factory NotificationCategory.networkConnected() =>
NotificationCategory('network.connected');
factory NotificationCategory.networkDisconnected() =>
NotificationCategory('network.disconnected');
factory NotificationCategory.networkError() =>
NotificationCategory('network.error');
}
/// Urgency of a notification
enum NotificationUrgency { low, normal, critical }
/// A hint about how to display this notification.
class NotificationHint {
/// Unique key for this hint.
final String key;
/// The value of this hint.
final DBusValue value;
/// Creates a custom notification hint.
const NotificationHint(this.key, this.value);
/// This notification should have its action IDs intepreted as icon names.
factory NotificationHint.actionIcons() {
return NotificationHint('action-icons', DBusBoolean(true));
}
/// This notification is of type [category].
factory NotificationHint.category(NotificationCategory category) {
return NotificationHint('category', DBusString(category.name));
}
/// This notification is from the application with the desktop file [name].desktop.
factory NotificationHint.desktopEntry(String name) {
return NotificationHint('desktop-entry', DBusString(name));
}
/// This notification should show use the supplied raw image data for its icon.
/// [data] is the raw data for the image.
/// [width] and [height] are the width and height of the image in pixels.
/// [rowStride] is the number of bytes per row in [data].
/// [hasAlpha] is true if the image has an alpha channel.
/// [bitsPerSample] is the number of bits in each color sample.
/// [channels] is the number of channels in the image (e.g. 3 for RGB, 4 for RGBA).
factory NotificationHint.imageData(int width, int height, Iterable<int> data,
{int rowStride = -1,
bool hasAlpha = false,
int bitsPerSample = 8,
int channels = 3}) {
if (rowStride < 0) {
rowStride = ((width * channels * bitsPerSample) / 8).ceil();
}
return NotificationHint(
'image-data',
DBusStruct([
DBusInt32(width),
DBusInt32(height),
DBusInt32(rowStride),
DBusBoolean(hasAlpha),
DBusInt32(bitsPerSample),
DBusInt32(channels),
DBusArray.byte(data)
]));
}
/// This notification should use the image at [path] as the icon.
factory NotificationHint.imagePath(String path) {
return NotificationHint('image-path', DBusString(path));
}
/// This notification should not be removed when its action is invoked. It will be removed when explicitly removed by this client or the user.
factory NotificationHint.resident() {
return NotificationHint('resident', DBusBoolean(true));
}
/// This notification should play the sound file at [path] when shown.
factory NotificationHint.soundFile(String path) {
return NotificationHint('sound-file', DBusString(path));
}
/// This notification should play the sound from the sound theme with [name] when shown.
factory NotificationHint.soundName(String name) {
return NotificationHint('sound-name', DBusString(name));
}
/// This notification should not trigger any any sounds that would normally play for notifications.
factory NotificationHint.suppressSound() {
return NotificationHint('suppress-sound', DBusBoolean(true));
}
/// This notification should be transient and by-pass the server's persistent capability.
factory NotificationHint.transient() {
return NotificationHint('transient', DBusBoolean(true));
}
/// This notification should be placed at the given [x] and [y] co-ordinates.
factory NotificationHint.location(int x, int y) {
return NotificationHint(
'*location', DBusStruct([DBusInt32(x), DBusInt32(y)]));
}
/// This notification should have the given [urgency] level.
factory NotificationHint.urgency(NotificationUrgency urgency) {
var urgencyValue = -1;
if (urgency == NotificationUrgency.low) {
urgencyValue = 0;
} else if (urgency == NotificationUrgency.normal) {
urgencyValue = 1;
} else if (urgency == NotificationUrgency.critical) {
urgencyValue = 2;
}
return NotificationHint('urgency', DBusByte(urgencyValue));
}
}
/// An action the user can perform on a notification.
class NotificationAction {
/// Unique key for this action.
final String key;
/// Label to show to the user.
final String label;
/// Creates a new notification action.
const NotificationAction(this.key, this.label);
}
/// The reason a notification was closed.
enum NotificationClosedReason { expired, dismissed, closed, unknown }
/// A notification that was created by this client.
class Notification {
/// The client this notification is part of.
final NotificationsClient client;
/// Id number assigned to this notification.
final int id;
/// The action that was chosen by the user.
Future<String> get action => _actionCompleter.future;
/// The reason the notification was closed.
Future<NotificationClosedReason> get closeReason => _closeCompleter.future;
final _actionCompleter = Completer<String>();
final _closeCompleter = Completer<NotificationClosedReason>();
/// Creates a new notification object.
/// You should not need to use this constructor directly and instead use a value returned from [NotificationsClient.notify].
/// Creating a notification from an ID may be required if you had no manage a notification created by other code.
Notification(this.client, this.id) {
// Subscribe signals here in case this wasn't created by our client.
client._subscribeSignals();
client._notifications[id] = this;
}
/// Closes this notification.
Future<void> close() async {
await client._object.callMethod(
'org.freedesktop.Notifications', 'CloseNotification', [DBusUint32(id)],
replySignature: DBusSignature(''));
}
}
/// A client that connects to the notifications server.
class NotificationsClient {
/// The bus this client is connected to.
final DBusClient _bus;
final bool _closeBus;
late final DBusRemoteObject _object;
// Signal subscriptions.
StreamSubscription? _actionInvokedSubscription;
StreamSubscription? _notificationClosedSubscription;
// Notifications in progress.
final _notifications = <int, Notification>{};
/// Creates a new notification client. If [bus] is provided connect to the given D-Bus server.
NotificationsClient({DBusClient? bus})
: _bus = bus ?? DBusClient.session(),
_closeBus = bus == null {
_object = DBusRemoteObject(_bus,
name: 'org.freedesktop.Notifications',
path: DBusObjectPath('/org/freedesktop/Notifications'));
}
/// Sends a notification with a [summary] and optional [body].
///
/// [appName] is a human readable name for the application that generated the notification, e.g. 'Firefox Browser'.
/// [appIcon] is either a URI (e.g. 'file:///usr/share/icons/firefox.png') or an icon theme name (e.g. 'web-browser').
/// [expireTimeoutMs] specified the expiration timeout in milliseconds with -1 used for the system default and 0 for no expiration.
/// [replacesId] is the ID of an existing notification this notification replaces.
/// [actions] is a list of actions the user can perform on this notification.
/// [hints] is a list of hints about how the notification should be shown.
///
/// Returns an object representing the notification.
Future<Notification> notify(String summary,
{String body = '',
String appName = '',
String appIcon = '',
int expireTimeoutMs = -1,
int replacesId = 0,
List<NotificationHint> hints = const [],
List<NotificationAction> actions = const []}) async {
_subscribeSignals();
var actionsValues = <String>[];
for (var action in actions) {
actionsValues.add(action.key);
actionsValues.add(action.label);
}
var hintsValues = <String, DBusValue>{};
for (var hint in hints) {
if (hint.key == '*location') {
var locationValues = hint.value.asStruct();
hintsValues['x'] = locationValues.elementAt(0);
hintsValues['y'] = locationValues.elementAt(1);
} else {
hintsValues[hint.key] = hint.value;
}
}
var result = await _object.callMethod(
'org.freedesktop.Notifications',
'Notify',
[
DBusString(appName),
DBusUint32(replacesId),
DBusString(appIcon),
DBusString(summary),
DBusString(body),
DBusArray.string(actionsValues),
DBusDict.stringVariant(hintsValues),
DBusInt32(expireTimeoutMs)
],
replySignature: DBusSignature('u'));
var id = result.returnValues[0].asUint32();
return Notification(this, id);
}
/// Gets the capabilities of the notifications server.
Future<List<String>> getCapabilities() async {
var result = await _object.callMethod(
'org.freedesktop.Notifications', 'GetCapabilities', [],
replySignature: DBusSignature('as'));
return result.returnValues[0].asStringArray().toList();
}
/// Gets information about the notifications server.
Future<NotificationsServerInformation> getServerInformation() async {
var result = await _object.callMethod(
'org.freedesktop.Notifications', 'GetServerInformation', [],
replySignature: DBusSignature('ssss'));
var values = result.returnValues;
return NotificationsServerInformation(values[0].asString(),
values[1].asString(), values[2].asString(), values[3].asString());
}
/// Terminates all active connections. If a client remains unclosed, the Dart process may not terminate.
Future<void> close() async {
if (_actionInvokedSubscription != null) {
await _actionInvokedSubscription?.cancel();
_actionInvokedSubscription = null;
}
if (_notificationClosedSubscription != null) {
await _notificationClosedSubscription?.cancel();
_notificationClosedSubscription = null;
}
if (_closeBus) {
await _bus.close();
}
}
/// Subscribe to the signals for actions and notifications being closed.
void _subscribeSignals() {
// Ensure the signals are only subscribed once.
if (_actionInvokedSubscription != null) {
return;
}
var actionsInvokedSignals = DBusRemoteObjectSignalStream(
object: _object,
interface: 'org.freedesktop.Notifications',
name: 'ActionInvoked',
signature: DBusSignature('us'));
_actionInvokedSubscription = actionsInvokedSignals.listen((signal) {
var id = signal.values[0].asUint32();
var actionKey = signal.values[1].asString();
var notification = _notifications[id];
if (notification != null) {
notification._actionCompleter.complete(actionKey);
}
});
var closedSignals = DBusRemoteObjectSignalStream(
object: _object,
interface: 'org.freedesktop.Notifications',
name: 'NotificationClosed',
signature: DBusSignature('uu'));
_notificationClosedSubscription = closedSignals.listen((signal) {
var id = signal.values[0].asUint32();
var reasonId = signal.values[1].asUint32();
var reason = NotificationClosedReason.unknown;
if (reasonId == 1) {
reason = NotificationClosedReason.expired;
} else if (reasonId == 2) {
reason = NotificationClosedReason.dismissed;
} else if (reasonId == 3) {
reason = NotificationClosedReason.closed;
}
var notification = _notifications.remove(id);
if (notification != null) {
notification._closeCompleter.complete(reason);
}
});
}
}