From 08c8b9c6d63784f3db1fa3423e629db3ca461b94 Mon Sep 17 00:00:00 2001 From: Rahix Date: Sat, 17 Feb 2018 21:45:44 +0100 Subject: Implement notification sounds Add a new setting to toggle notification sounds on or off. Plays the systems default instant messaging message sound (message-new-instant) whenever a notification is shown if toggled on. --- cmake/FindCanberra.cmake | 10 ++++++++++ libdino/src/entity/conversation.vala | 4 ++++ libdino/src/entity/settings.vala | 10 ++++++++++ main/CMakeLists.txt | 1 + main/data/settings_dialog.ui | 14 +++++++++++++- main/src/ui/notifications.vala | 8 ++++++++ main/src/ui/settings_dialog.vala | 3 +++ 7 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 cmake/FindCanberra.cmake diff --git a/cmake/FindCanberra.cmake b/cmake/FindCanberra.cmake new file mode 100644 index 00000000..fbbe1fef --- /dev/null +++ b/cmake/FindCanberra.cmake @@ -0,0 +1,10 @@ +include(PkgConfigWithFallback) +find_pkg_config_with_fallback(Canberra + PKG_CONFIG_NAME libcanberra + LIB_NAMES canberra + INCLUDE_NAMES canberra.h +) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Canberra + REQUIRED_VARS Canberra_LIBRARY) diff --git a/libdino/src/entity/conversation.vala b/libdino/src/entity/conversation.vala index 9026e33f..0dfa64f2 100644 --- a/libdino/src/entity/conversation.vala +++ b/libdino/src/entity/conversation.vala @@ -101,6 +101,10 @@ public class Conversation : Object { return notify_setting != NotifySetting.DEFAULT ? notify_setting : get_notification_default_setting(stream_interactor); } + public bool get_sound_setting(StreamInteractor stream_interactor) { + return Application.get_default().settings.sound; + } + public NotifySetting get_notification_default_setting(StreamInteractor stream_interactor) { Xmpp.XmppStream? stream = stream_interactor.get_stream(account); if (!Application.get_default().settings.notifications) return NotifySetting.OFF; diff --git a/libdino/src/entity/settings.vala b/libdino/src/entity/settings.vala index f94a92ca..32d38c90 100644 --- a/libdino/src/entity/settings.vala +++ b/libdino/src/entity/settings.vala @@ -10,6 +10,7 @@ public class Settings : Object { send_typing_ = col_to_bool_or_default("send_typing", true); send_marker_ = col_to_bool_or_default("send_marker", true); notifications_ = col_to_bool_or_default("notifications", true); + sound_ = col_to_bool_or_default("sound", true); convert_utf8_smileys_ = col_to_bool_or_default("convert_utf8_smileys", true); current_width = col_to_int_or_default("window_width", 1200); @@ -56,6 +57,15 @@ public class Settings : Object { } } + private bool sound_; + public bool sound { + get { return sound_; } + set { + db.settings.insert().or("REPLACE").value(db.settings.key, "sound").value(db.settings.value, value.to_string()).perform(); + sound_ = value; + } + } + private bool convert_utf8_smileys_; public bool convert_utf8_smileys { get { return convert_utf8_smileys_; } diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 58e1ab7d..0ef6099d 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -9,6 +9,7 @@ find_packages(MAIN_PACKAGES REQUIRED GModule GObject GTK3>=3.22 + Canberra ) set(RESOURCE_LIST diff --git a/main/data/settings_dialog.ui b/main/data/settings_dialog.ui index 9d18bdc7..861d8b74 100644 --- a/main/data/settings_dialog.ui +++ b/main/data/settings_dialog.ui @@ -53,6 +53,18 @@ 1 + + + Play a sound when a new message arrives + True + + + 0 + 3 + 1 + 1 + + Convert smileys to emojis @@ -60,7 +72,7 @@ 0 - 3 + 4 1 1 diff --git a/main/src/ui/notifications.vala b/main/src/ui/notifications.vala index 246452ea..2eb144e4 100644 --- a/main/src/ui/notifications.vala +++ b/main/src/ui/notifications.vala @@ -14,11 +14,14 @@ public class Notifications : Object { private HashMap notifications = new HashMap(Conversation.hash_func, Conversation.equals_func); private Set? active_conversation_ids = null; private Set? active_ids = new HashSet(); + private Canberra.Context sound_context; public Notifications(StreamInteractor stream_interactor, Gtk.Window window) { this.stream_interactor = stream_interactor; this.window = window; + Canberra.Context.create(out sound_context); + stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect((focused_conversation) => { if (active_conversation_ids == null) { Gee.List conversations = stream_interactor.get_module(ConversationManager.IDENTITY).get_active_conversations(); @@ -68,6 +71,11 @@ public class Notifications : Object { active_conversation_ids.add(conversation.id.to_string()); window.urgency_hint = true; } + if (conversation.get_sound_setting(stream_interactor)) { + sound_context.play (0, + Canberra.PROP_EVENT_ID, "message-new-instant", + Canberra.PROP_EVENT_DESCRIPTION, "New Dino message"); + } } private void on_received_subscription_request(Jid jid, Account account) { diff --git a/main/src/ui/settings_dialog.vala b/main/src/ui/settings_dialog.vala index e40b2993..58c86bde 100644 --- a/main/src/ui/settings_dialog.vala +++ b/main/src/ui/settings_dialog.vala @@ -8,6 +8,7 @@ class SettingsDialog : Dialog { [GtkChild] private CheckButton typing_checkbutton; [GtkChild] private CheckButton marker_checkbutton; [GtkChild] private CheckButton notification_checkbutton; + [GtkChild] private CheckButton sound_checkbutton; [GtkChild] private CheckButton emoji_checkbutton; Dino.Entities.Settings settings = Dino.Application.get_default().settings; @@ -18,11 +19,13 @@ class SettingsDialog : Dialog { typing_checkbutton.active = settings.send_typing; marker_checkbutton.active = settings.send_marker; notification_checkbutton.active = settings.notifications; + sound_checkbutton.active = settings.sound; emoji_checkbutton.active = settings.convert_utf8_smileys; typing_checkbutton.toggled.connect(() => { settings.send_typing = typing_checkbutton.active; } ); marker_checkbutton.toggled.connect(() => { settings.send_marker = marker_checkbutton.active; } ); notification_checkbutton.toggled.connect(() => { settings.notifications = notification_checkbutton.active; } ); + sound_checkbutton.toggled.connect(() => { settings.sound = sound_checkbutton.active; } ); emoji_checkbutton.toggled.connect(() => { settings.convert_utf8_smileys = emoji_checkbutton.active; }); } } -- cgit v1.2.3-70-g09d2