From 97130ad08300c3ec6ab2165ff7506cd7f25af0d6 Mon Sep 17 00:00:00 2001 From: charles kerr Date: Fri, 1 Jan 2016 20:08:05 -0600 Subject: honor com.ubuntu.touch.AccountsService.Sound.SilentMode --- src/CMakeLists.txt | 5 ++ src/com.ubuntu.touch.AccountsService.Sound.xml | 47 +++++++++++++++++++ src/notifier.c | 64 +++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/com.ubuntu.touch.AccountsService.Sound.xml diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c99ce46..01a0558 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,6 +39,11 @@ add_gdbus_codegen_with_namespace(SERVICE_GENERATED_SOURCES dbus-testing org.ayatana.indicator.power Dbus ${CMAKE_SOURCE_DIR}/data/org.ayatana.indicator.power.Testing.xml) +add_gdbus_codegen_with_namespace(SERVICE_GENERATED_SOURCES dbus-accounts-sound + com.ubuntu.touch + Dbus + ${CMAKE_SOURCE_DIR}/src/com.ubuntu.touch.AccountsService.Sound.xml) + # add the bin dir to our include path so the code can find the generated header files include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/src/com.ubuntu.touch.AccountsService.Sound.xml b/src/com.ubuntu.touch.AccountsService.Sound.xml new file mode 100644 index 0000000..6e2ca5f --- /dev/null +++ b/src/com.ubuntu.touch.AccountsService.Sound.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/notifier.c b/src/notifier.c index 7d81f3a..ad7efb0 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -18,6 +18,7 @@ */ #include "datafiles.h" +#include "dbus-accounts-sound.h" #include "dbus-battery.h" #include "dbus-shared.h" #include "notifier.h" @@ -86,6 +87,9 @@ typedef struct gboolean actions_supported; IndicatorPowerSoundPlayer * sound_player; + + GCancellable * cancellable; + DbusAccountsServiceSound * accounts_service_sound_proxy; } IndicatorPowerNotifierPrivate; @@ -143,6 +147,40 @@ get_battery_power_level (IndicatorPowerDevice * battery) **** Sounds ***/ +static void +on_sound_proxy_ready (GObject * source_object G_GNUC_UNUSED, + GAsyncResult * res, + gpointer gself) +{ + GError * error; + DbusAccountsServiceSound * proxy; + + error = NULL; + proxy = dbus_accounts_service_sound_proxy_new_for_bus_finish (res, &error); + if (error != NULL) + { + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("%s Couldn't find accounts service sound proxy: %s", G_STRLOC, error->message); + + g_clear_error(&error); + } + else + { + IndicatorPowerNotifier * const self = INDICATOR_POWER_NOTIFIER(gself); + priv_t * const p = get_priv (self); + p->accounts_service_sound_proxy = proxy; + } +} + +static gboolean +silent_mode (IndicatorPowerNotifier * self) +{ + priv_t * const p = get_priv (self); + + return (p->accounts_service_sound_proxy != NULL) + && (dbus_accounts_service_sound_get_silent_mode(p->accounts_service_sound_proxy)); +} + static void play_low_battery_sound (IndicatorPowerNotifier * self) { @@ -150,8 +188,13 @@ play_low_battery_sound (IndicatorPowerNotifier * self) gchar * filename; priv_t * const p = get_priv(self); + /* can't play? */ g_return_if_fail (p->sound_player != NULL); + /* won't play? */ + if (silent_mode(self)) + return; + filename = datafile_find(DATAFILE_TYPE_SOUND, key); if (filename != NULL) { @@ -405,11 +448,18 @@ my_dispose (GObject * o) IndicatorPowerNotifier * const self = INDICATOR_POWER_NOTIFIER(o); priv_t * const p = get_priv (self); + if (p->cancellable != NULL) + { + g_cancellable_cancel(p->cancellable); + g_clear_object(&p->cancellable); + } + indicator_power_notifier_set_bus (self, NULL); indicator_power_notifier_set_sound_player (self, NULL); notification_clear (self); indicator_power_notifier_set_battery (self, NULL); g_clear_object (&p->dbus_battery); + g_clear_object (&p->accounts_service_sound_proxy); G_OBJECT_CLASS (indicator_power_notifier_parent_class)->dispose (o); } @@ -428,7 +478,6 @@ my_finalize (GObject * o G_GNUC_UNUSED) **** Instantiation ***/ - static void indicator_power_notifier_init (IndicatorPowerNotifier * self) { @@ -440,8 +489,21 @@ indicator_power_notifier_init (IndicatorPowerNotifier * self) p->power_level = POWER_LEVEL_OK; + p->cancellable = g_cancellable_new(); + if (!instance_count++ && !notify_init("ayatana-indicator-power-service")) g_critical("Unable to initialize libnotify! Notifications might not be shown."); + + gchar* object_path = g_strdup_printf("/org/freedesktop/Accounts/User%lu", (gulong)getuid()); + dbus_accounts_service_sound_proxy_new_for_bus( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, + "org.freedesktop.Accounts", + object_path, + p->cancellable, + on_sound_proxy_ready, + self); + g_clear_pointer(&object_path, g_free); } static void -- cgit v1.2.3