-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDBus.vala
More file actions
323 lines (275 loc) · 11.1 KB
/
DBus.vala
File metadata and controls
323 lines (275 loc) · 11.1 KB
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
/*
* Copyright 2019-2023 elementary, Inc. (https://elementary.io)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
[DBus (name = "org.freedesktop.Notifications")]
public class Notifications.Server : Object {
public enum CloseReason {
EXPIRED = 1,
DISMISSED = 2,
CLOSE_NOTIFICATION_CALL = 3,
UNDEFINED = 4
}
public signal void action_invoked (uint32 id, string action_key);
public signal void notification_closed (uint32 id, uint32 reason);
private const string X_CANONICAL_PRIVATE_SYNCHRONOUS = "x-canonical-private-synchronous";
private uint32 id_counter = 0;
private unowned DBusConnection connection;
private Fdo.ActionGroup action_group;
private Gee.Map<uint32, Bubble?> bubbles;
private Confirmation? confirmation;
private Settings settings;
private uint action_group_id;
private uint server_id;
public Server (DBusConnection connection) throws Error {
settings = new Settings ("io.elementary.notifications");
bubbles = new Gee.HashMap<uint32, Bubble?> ();
action_group = new Fdo.ActionGroup (this);
server_id = connection.register_object ("/org/freedesktop/Notifications", this);
action_group_id = connection.export_action_group ("/org/freedesktop/Notifications", action_group);
this.connection = connection;
action_invoked.connect ((id) => close_bubble (id));
notification_closed.connect ((id) => close_bubble (id));
}
~Server () {
connection.unexport_action_group (action_group_id);
connection.unregister_object (server_id);
}
private void close_bubble (uint32 id) {
Bubble? bubble;
if (bubbles.unset (id, out bubble)) {
if (bubble != null) {
bubble.close ();
}
action_group.remove_actions (id);
}
}
public void close_notification (uint32 id) throws DBusError, IOError {
if (!bubbles.has_key (id)) {
// according to spec, an empty dbus error should be sent if the notification doesn't exist (anymore)
throw new DBusError.FAILED ("");
}
notification_closed (id, CloseReason.CLOSE_NOTIFICATION_CALL);
}
public string [] get_capabilities () throws DBusError, IOError {
return {
"actions",
"body",
"body-markup",
"sound",
X_CANONICAL_PRIVATE_SYNCHRONOUS
};
}
public void get_server_information (
out string name,
out string vendor,
out string version,
out string spec_version
) throws DBusError, IOError {
name = "io.elementary.notifications";
vendor = "elementaryOS";
version = "0.1";
spec_version = "1.3";
}
public new uint32 notify (
string app_name,
uint32 replaces_id,
string app_icon,
string summary,
string body,
string[] actions,
HashTable<string, Variant> hints,
int32 expire_timeout,
BusName sender
) throws DBusError, IOError {
// Silence "Automatic suspend. Suspending soon because of inactivity." notifications
// These values and hints are taken from gnome-settings-daemon source code
// See: https://gitlab.gnome.org/GNOME/gnome-settings-daemon/-/blob/master/plugins/power/gsd-power-manager.c#L356
// We must check for app_icon == "" to not block low power notifications
if ("desktop-entry" in hints && hints["desktop-entry"].get_string () == "gnome-power-panel"
&& "urgency" in hints && hints["urgency"].get_byte () == 2
&& app_icon == ""
&& expire_timeout == 0
) {
debug ("Blocked GSD notification");
throw new DBusError.FAILED ("Notification Blocked");
}
var id = (replaces_id != 0 ? replaces_id : ++id_counter);
if (hints.contains (X_CANONICAL_PRIVATE_SYNCHRONOUS)) {
send_confirmation (app_icon, hints);
} else {
var notification = new Notification (app_name, app_icon, summary, body, hints);
notification.buttons = new GenericArray<Notification.Button?> (actions.length / 2);
// validate actions
for (var i = 0; i < actions.length; i += 2) {
if (actions[i] == "") {
continue;
}
var action_name = "fdo." + action_group.add_action (id, actions[i]);
if (actions[i] == "default") {
notification.default_action_name = action_name;
continue;
}
var label = actions[i + 1].strip ();
if (label == "") {
warning ("action '%s' sent without a label, skipping…", actions[i]);
continue;
}
notification.buttons.add ({ label, action_name });
}
if (!settings.get_boolean ("do-not-disturb") || notification.priority == GLib.NotificationPriority.URGENT) {
var app_settings = new Settings.with_path (
"io.elementary.notifications.applications",
settings.path.concat ("applications", "/", notification.app_id, "/")
);
if (app_settings.get_boolean ("bubbles")) {
if (bubbles.has_key (id) && bubbles[id] != null) {
bubbles[id].notification = notification;
} else {
bubbles[id] = new Bubble (notification);
bubbles[id].insert_action_group ("fdo", action_group);
bubbles[id].close_request.connect (() => {
bubbles[id] = null;
return Gdk.EVENT_PROPAGATE;
});
bubbles[id].closed.connect ((res) => {
if (res == CloseReason.EXPIRED && app_settings.get_boolean ("remember")) {
return;
}
notification_closed (id, res);
});
}
bubbles[id].present ();
}
var suppress_sound = (
"suppress-sound" in hints &&
hints["suppress-sound"].is_of_type (GLib.VariantType.BOOLEAN) &&
hints["suppress-sound"].get_boolean ()
);
if (app_settings.get_boolean ("sounds") && !suppress_sound) {
string sound;
var is_filename = false;
if ("sound-file" in hints && hints["sound-file"].is_of_type (GLib.VariantType.STRING)) {
sound = hints["sound-file"].get_string ();
is_filename = true;
} else if ("sound-name" in hints && hints["sound-name"].is_of_type (GLib.VariantType.STRING)) {
sound = hints["sound-namefile"].get_string ();
} else if ("category" in hints && hints["category"].is_of_type (VariantType.STRING)) {
sound = category_to_sound_name (hints["category"].get_string ());
} else {
sound = notification.priority != URGENT ? "dialog-information" : "dialog-warning";
}
send_sound (sound, is_filename);
}
}
}
return id;
}
private void send_confirmation (string icon_name, HashTable<string, Variant> hints) {
double progress_value;
Variant? val = hints.lookup ("value");
if (val != null) {
progress_value = val.get_int32 ().clamp (0, 100) / 100.0;
} else {
progress_value = -1;
}
// the sound indicator is an exception here, it won't emit a sound at all, even though for
// consistency it should. So we make it emit the default one.
var confirmation_type = hints.lookup (X_CANONICAL_PRIVATE_SYNCHRONOUS).get_string ();
if (confirmation_type == "indicator-sound") {
send_sound ("audio-volume-change");
}
if (confirmation == null) {
confirmation = new Notifications.Confirmation (
icon_name,
progress_value
);
confirmation.close_request.connect (() => {
confirmation = null;
return Gdk.EVENT_PROPAGATE;
});
} else {
confirmation.icon_name = icon_name;
confirmation.progress = progress_value;
}
confirmation.present ();
}
private void send_sound (string sound_name, bool is_filename = false) {
if (sound_name == "") {
return;
}
Canberra.Proplist props;
Canberra.Proplist.create (out props);
props.sets (Canberra.PROP_CANBERRA_CACHE_CONTROL, "volatile");
if (is_filename) {
props.sets (Canberra.PROP_MEDIA_FILENAME, sound_name);
} else {
props.sets (Canberra.PROP_EVENT_ID, sound_name);
}
CanberraGtk4.context_get ().play_full (0, props);
}
static unowned string category_to_sound_name (string category) {
unowned string sound;
switch (category) {
case "call":
sound = "dialog-information";
break;
case "call.ended":
sound = "phone-hangup";
break;
case "call.incoming":
sound = "phone-incoming-call";
break;
case "call.unanswered":
sound = "phone-outgoing-busy";
break;
case "device.added":
sound = "device-added";
break;
case "device.removed":
sound = "device-removed";
break;
case "im":
sound = "message";
break;
case "im.received":
sound = "message-new-instant";
break;
case "network.connected":
sound = "network-connectivity-established";
break;
case "network.disconnected":
sound = "network-connectivity-lost";
break;
case "presence.online":
sound = "service-login";
break;
case "presence.offline":
sound = "service-logout";
break;
// no sound at all
case "x-gnome.music":
sound = "";
break;
// generic errors
case "device.error":
case "email.bounced":
case "im.error":
case "network.error":
case "transfer.error":
sound = "dialog-error";
break;
// use generic default
case "network":
case "email":
case "email.arrived":
case "presence":
case "transfer":
case "transfer.complete":
default:
sound = "dialog-information";
break;
}
return sound;
}
}