Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sound-name and suppress-sound hints #124

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install Dependencies
run: |
apt update
apt install -y libhandy-1-dev libcanberra-dev libcanberra-gtk3-dev libgranite-dev libgtk-3-dev meson valac
apt install -y libhandy-1-dev libcanberra-dev libcanberra-gtk3-dev libgranite-dev libgtk-3-dev libnotify-dev meson valac
- name: Build
env:
DESTDIR: out
Expand Down
124 changes: 122 additions & 2 deletions demo/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class MainWindow : Gtk.ApplicationWindow {
private Gtk.Entry icon_entry;
private Gtk.Entry id_entry;
private Gtk.ComboBoxText priority_combobox;
private Gtk.ComboBoxText category_combobox;
private Gtk.ComboBoxText sound_combobox;
private Gtk.Switch suppress_sound_switch;
private Gtk.SpinButton action_spinbutton;

public MainWindow (Gtk.Application application) {
Expand Down Expand Up @@ -73,6 +76,62 @@ public class MainWindow : Gtk.ApplicationWindow {

action_spinbutton = new Gtk.SpinButton.with_range (0, 3, 1);

var libnotify_label = new Gtk.Label ("Libnotify Tests") {
halign = Gtk.Align.CENTER
};

var category_label = new Gtk.Label ("Category:");

category_combobox = new Gtk.ComboBoxText () {
hexpand = true
};
category_combobox.append_text ("");
category_combobox.append_text ("device");
category_combobox.append_text ("device.added");
category_combobox.append_text ("device.error");
category_combobox.append_text ("device.removed");
category_combobox.append_text ("email");
category_combobox.append_text ("email.arrived");
category_combobox.append_text ("email.bounced");
category_combobox.append_text ("im");
category_combobox.append_text ("im.error");
category_combobox.append_text ("im.received");
category_combobox.append_text ("network");
category_combobox.append_text ("network.connected");
category_combobox.append_text ("network.disconnected");
category_combobox.append_text ("network.error");
category_combobox.append_text ("presence");
category_combobox.append_text ("presence.offline");
category_combobox.append_text ("presence.online");
category_combobox.append_text ("transfer");
category_combobox.append_text ("transfer.complete");
category_combobox.append_text ("transfer.error");
category_combobox.set_active (0);

var sound_label = new Gtk.Label ("Sound Name:");

sound_combobox = new Gtk.ComboBoxText () {
hexpand = true
};
sound_combobox.append_text ("");
sound_combobox.append_text ("device-added");
sound_combobox.append_text ("device-removed");
sound_combobox.append_text ("dialog-error");
sound_combobox.append_text ("dialog-information");
sound_combobox.append_text ("message-new-instant");
sound_combobox.append_text ("message");
sound_combobox.append_text ("network-connectivity-established");
sound_combobox.append_text ("network-connectivity-lost");
sound_combobox.append_text ("service-login");
sound_combobox.append_text ("service-logout");
sound_combobox.set_active (0);

var suppress_sound_label = new Gtk.Label ("Suppress Sounds:");
suppress_sound_switch = new Gtk.Switch () {
active = false,
halign = Gtk.Align.START
};

var send_button = new Gtk.Button.with_label ("Send Notification") {
can_default = true,
halign = Gtk.Align.END,
Expand All @@ -94,7 +153,14 @@ public class MainWindow : Gtk.ApplicationWindow {
grid.attach (priority_combobox, 1, 4);
grid.attach (action_label, 0, 5);
grid.attach (action_spinbutton, 1, 5);
grid.attach (send_button, 0, 6, 2);
grid.attach (libnotify_label, 0, 6, 2);
grid.attach (category_label, 0, 7);
grid.attach (category_combobox, 1, 7);
grid.attach (sound_label, 0, 8);
grid.attach (sound_combobox, 1, 8);
grid.attach (suppress_sound_label, 0, 9);
grid.attach (suppress_sound_switch, 1, 9);
grid.attach (send_button, 0, 10, 2);

var toast = new Granite.Widgets.Toast ("");

Expand All @@ -105,7 +171,7 @@ public class MainWindow : Gtk.ApplicationWindow {
add (overlay);

send_button.has_default = true;
send_button.clicked.connect (send_notification);
send_button.clicked.connect (route_notification);

var toast_action = new SimpleAction ("toast", VariantType.STRING);

Expand All @@ -117,6 +183,16 @@ public class MainWindow : Gtk.ApplicationWindow {
});
}

private void route_notification () {
var category = category_combobox.get_active_text ();
var sound = sound_combobox.get_active_text ();
if (category.length > 0 || sound.length > 0) {
send_libnotify_notification ();
} else {
send_notification ();
}
}

private void send_notification () {
NotificationPriority priority;
switch (priority_combobox.active) {
Expand Down Expand Up @@ -154,4 +230,48 @@ public class MainWindow : Gtk.ApplicationWindow {

application.send_notification (id, notification);
}

private void send_libnotify_notification () {
Notify.init ("io.elementary.notifications.demo");

Notify.Urgency urgency;
switch (priority_combobox.active) {
case 3:
case 2:
urgency = Notify.Urgency.CRITICAL;
break;
case 0:
urgency = Notify.Urgency.LOW;
break;
case 1:
default:
urgency = Notify.Urgency.NORMAL;
break;
}

var notification = new Notify.Notification (title_entry.text, body_entry.text, "preferences-system-notifications") {
app_name = "io.elementary.notifications.demo"
};
notification.set_urgency (urgency);

var category = category_combobox.get_active_text ();
if (category.length > 0) {
notification.set_category (category_combobox.get_active_text ());
}

var sound = sound_combobox.get_active_text ();
if (sound.length > 0) {
Variant sound_name = new Variant ("s", sound_combobox.get_active_text ());
notification.set_hint ("sound-name", sound_name);
}

Variant suppress_sound = new Variant ("b", suppress_sound_switch.active);
notification.set_hint ("suppress-sound", suppress_sound);

try {
notification.show ();
} catch (Error e) {
critical ("Failed to send notification: %s", e.message);
}
}
}
1 change: 1 addition & 0 deletions demo/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ executable(
dependencies : [
dependency ('granite'),
dependency ('gtk+-3.0'),
dependency ('libnotify'),
],
install : true
)
Expand Down
11 changes: 9 additions & 2 deletions src/DBus.vala
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,16 @@ public class Notifications.Server : Object {
}

private void send_sound (HashTable<string,Variant> hints, string sound_name = "dialog-information") {
unowned Variant? variant = null;

if ((variant = hints.lookup ("suppress-sound")) != null && variant.is_of_type (VariantType.BOOLEAN) && variant.get_boolean ()) {
return;
}

if (sound_name == "dialog-information") {
Variant? variant = hints.lookup ("category");
if (variant != null) {
if ((variant = hints.lookup ("sound-name")) != null && variant.is_of_type (VariantType.STRING)) {
sound_name = variant.get_string ();
} else if ((variant = hints.lookup ("category")) != null && variant.is_of_type (VariantType.STRING)) {
sound_name = category_to_sound_name (variant.get_string ());
}
}
Expand Down