Skip to content

Commit 93fbe22

Browse files
authored
Handle "urgency" and "desktop-entry" hints in server (#265)
1 parent b54d379 commit 93fbe22

File tree

2 files changed

+52
-63
lines changed

2 files changed

+52
-63
lines changed

src/DBus.vala

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public class Notifications.Server : Object {
7474
out string version,
7575
out string spec_version
7676
) throws DBusError, IOError {
77-
7877
name = "io.elementary.notifications";
7978
vendor = "elementaryOS";
8079
version = "0.1";
@@ -110,8 +109,34 @@ public class Notifications.Server : Object {
110109
if (hints.contains (X_CANONICAL_PRIVATE_SYNCHRONOUS)) {
111110
send_confirmation (app_icon, hints);
112111
} else {
113-
var notification = new Notification (app_name, app_icon, summary, body, hints);
114-
notification.buttons = new GenericArray<Notification.Button?> (actions.length / 2);
112+
DesktopAppInfo? app_info = null;
113+
if ("desktop-entry" in hints && hints["desktop-entry"].is_of_type (VariantType.STRING)) {
114+
app_info = new DesktopAppInfo ("%s.desktop".printf (hints["desktop-entry"].get_string ()));
115+
}
116+
117+
var notification = new Notification (app_name, app_icon, summary, body, app_info, hints) {
118+
buttons = new GenericArray<Notification.Button?> (actions.length / 2)
119+
};
120+
121+
// GLib.Notification.set_priority ()
122+
// convert between freedesktop urgency levels and GLib.NotificationPriority levels
123+
// See: https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html#urgency-levels
124+
if ("urgency" in hints && hints["urgency"].is_of_type (VariantType.BYTE)) {
125+
switch (hints["urgency"].get_byte ()) {
126+
case 0:
127+
notification.priority = LOW;
128+
break;
129+
case 1:
130+
notification.priority = NORMAL;
131+
break;
132+
case 2:
133+
notification.priority = URGENT;
134+
break;
135+
default:
136+
warning ("unknown urgency value: %i, ignoring", hints["urgency"].get_byte ());
137+
break;
138+
}
139+
}
115140

116141
// validate actions
117142
for (var i = 0; i < actions.length; i += 2) {

src/Notification.vala

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,21 @@
11
/*
2-
* Copyright 2020-2023 elementary, Inc. (https://elementary.io)
3-
*
4-
* This program is free software; you can redistribute it and/or
5-
* modify it under the terms of the GNU General Public
6-
* License as published by the Free Software Foundation; either
7-
* version 3 of the License, or (at your option) any later version.
8-
*
9-
* This program is distributed in the hope that it will be useful,
10-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12-
* General Public License for more details.
13-
*
14-
* You should have received a copy of the GNU General Public
15-
* License along with this program; if not, write to the
16-
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17-
* Boston, MA 02110-1301 USA
18-
*
19-
*/
20-
21-
public class Notifications.Notification : GLib.Object {
2+
* Copyright 2020-2025 elementary, Inc. (https://elementary.io)
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
public class Notifications.Notification : Object {
227
private const string OTHER_APP_ID = "gala-other";
238

24-
public GLib.DesktopAppInfo? app_info { get; private set; default = null; }
25-
public GLib.NotificationPriority priority { get; private set; default = GLib.NotificationPriority.NORMAL; }
26-
public HashTable<string, Variant> hints { get; construct; }
27-
public string app_icon { get; construct; }
28-
public string app_id { get; private set; default = OTHER_APP_ID; }
299
public string app_name { get; construct; }
30-
public string body { get; construct set; }
10+
public string app_icon { get; construct; }
3111
public string summary { get; construct set; }
12+
public string body { get; construct set; }
13+
public DesktopAppInfo? app_info { get; construct; }
14+
public HashTable<string, Variant> hints { get; construct; }
15+
16+
public GLib.NotificationPriority priority { get; set; default = NORMAL; }
17+
18+
public string app_id { get; private set; default = OTHER_APP_ID; }
3219

3320
public GLib.Icon? primary_icon { get; set; default = null; }
3421
public GLib.Icon? badge_icon { get; set; default = null; }
@@ -47,12 +34,13 @@ public class Notifications.Notification : GLib.Object {
4734
string action_name;
4835
}
4936

50-
public Notification (string app_name, string app_icon, string summary, string body, HashTable<string, Variant> hints) {
37+
public Notification (string app_name, string app_icon, string summary, string body, DesktopAppInfo? app_info, HashTable<string, Variant> hints) {
5138
Object (
5239
app_name: app_name,
5340
app_icon: app_icon,
5441
summary: summary,
5542
body: body,
43+
app_info: app_info,
5644
hints: hints
5745
);
5846
}
@@ -67,39 +55,13 @@ public class Notifications.Notification : GLib.Object {
6755
}
6856

6957
construct {
70-
unowned Variant? variant = null;
71-
72-
// GLib.Notification.set_priority ()
73-
// convert between freedesktop urgency levels and GLib.NotificationPriority levels
74-
// See: https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html#urgency-levels
75-
if ("urgency" in hints && hints["urgency"].is_of_type (VariantType.BYTE)) {
76-
switch (hints["urgency"].get_byte ()) {
77-
case 0:
78-
priority = LOW;
79-
break;
80-
case 1:
81-
priority = NORMAL;
82-
break;
83-
case 2:
84-
priority = URGENT;
85-
break;
86-
default:
87-
warning ("unknown urgency value: %i, ignoring", hints["urgency"].get_byte ());
88-
break;
89-
}
90-
}
91-
92-
if ("desktop-entry" in hints && hints["desktop-entry"].is_of_type (VariantType.STRING)) {
93-
app_info = new DesktopAppInfo ("%s.desktop".printf (hints["desktop-entry"].get_string ()));
94-
95-
if (app_info != null && app_info.get_boolean ("X-GNOME-UsesNotifications")) {
96-
var app_info_id = app_info.get_id ();
97-
if (app_info_id != null) {
98-
if (app_info_id.has_suffix (".desktop")) {
99-
app_id = app_info_id.substring (0, app_info_id.length - ".desktop".length);
100-
} else {
101-
app_id = app_info_id;
102-
}
58+
if (app_info != null && app_info.get_boolean ("X-GNOME-UsesNotifications")) {
59+
var app_info_id = app_info.get_id ();
60+
if (app_info_id != null) {
61+
if (app_info_id.has_suffix (".desktop")) {
62+
app_id = app_info_id.substring (0, app_info_id.length - ".desktop".length);
63+
} else {
64+
app_id = app_info_id;
10365
}
10466
}
10567
}
@@ -117,6 +79,8 @@ public class Notifications.Notification : GLib.Object {
11779
primary_icon = new ThemedIcon (app_icon);
11880
}
11981

82+
unowned Variant? variant = null;
83+
12084
// GLib.Notification.set_icon ()
12185
if ((variant = hints.lookup ("image-path")) != null || (variant = hints.lookup ("image_path")) != null) {
12286
var image_path = variant.get_string ();

0 commit comments

Comments
 (0)