diff -Nru indicator-power-12.10.6+15.10.20151005.1/CMakeLists.txt indicator-power-12.10.6+16.04.20160105/CMakeLists.txt --- indicator-power-12.10.6+15.10.20151005.1/CMakeLists.txt 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/CMakeLists.txt 2016-01-05 14:37:48.000000000 +0000 @@ -7,7 +7,8 @@ set(PACKAGE ${CMAKE_PROJECT_NAME}) set(GETTEXT_PACKAGE "indicator-power") add_definitions (-DGETTEXT_PACKAGE="${GETTEXT_PACKAGE}" - -DGNOMELOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}") + -DGNOMELOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}" + -DLOW_BATTERY_SOUND="Low battery.ogg") option (enable_tests "Build the package's automatic tests." ON) option (enable_lcov "Generate lcov code coverage reports." ON) @@ -38,6 +39,7 @@ gio-unix-2.0>=2.36 gudev-1.0>=204 libnotify>=0.7.6 + gstreamer-1.0>=1.2 url-dispatcher-1>=1) include_directories (SYSTEM ${SERVICE_DEPS_INCLUDE_DIRS}) diff -Nru indicator-power-12.10.6+15.10.20151005.1/data/CMakeLists.txt indicator-power-12.10.6+16.04.20160105/data/CMakeLists.txt --- indicator-power-12.10.6+15.10.20151005.1/data/CMakeLists.txt 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/data/CMakeLists.txt 2016-01-05 14:37:48.000000000 +0000 @@ -88,3 +88,12 @@ install (FILES "${UNITY_INDICATOR_FILE}" DESTINATION "${UNITY_INDICATOR_DIR}") + +## +## Sounds +## + +# where to install +set (DATA_HOME "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}") +message (STATUS "${DATA_HOME} is the sounds/ install dir") +install (DIRECTORY sounds DESTINATION ${DATA_HOME}) Binary files /tmp/tmpVG3g1f/OBbOpfRTLh/indicator-power-12.10.6+15.10.20151005.1/data/sounds/Low battery.ogg and /tmp/tmpVG3g1f/uqBPQC3_bi/indicator-power-12.10.6+16.04.20160105/data/sounds/Low battery.ogg differ diff -Nru indicator-power-12.10.6+15.10.20151005.1/debian/changelog indicator-power-12.10.6+16.04.20160105/debian/changelog --- indicator-power-12.10.6+15.10.20151005.1/debian/changelog 2016-01-08 16:38:44.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/debian/changelog 2016-01-08 16:38:44.000000000 +0000 @@ -1,3 +1,15 @@ +indicator-power (12.10.6+16.04.20160105-0ubuntu1) xenial; urgency=medium + + [ Charles Kerr ] + * Play a 'low battery' sound when the low battery notification is + shown. (LP: #1470767) + + [ charles kerr ] + * Play a 'low battery' sound when the low battery notification is + shown. (LP: #1470767) + + -- Charles Kerr Tue, 05 Jan 2016 14:37:51 +0000 + indicator-power (12.10.6+15.10.20151005.1-0ubuntu1) wily; urgency=medium [ CI Train Bot ] diff -Nru indicator-power-12.10.6+15.10.20151005.1/debian/control indicator-power-12.10.6+16.04.20160105/debian/control --- indicator-power-12.10.6+15.10.20151005.1/debian/control 2016-01-08 16:38:44.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/debian/control 2016-01-08 16:38:44.000000000 +0000 @@ -7,7 +7,10 @@ libglib2.0-dev (>= 2.36), libgudev-1.0-dev, liburl-dispatcher1-dev, + libgstreamer1.0-dev, python:any, +# for com.ubuntu.touch.AccountsService.Sound.xml + accountsservice-ubuntu-schemas, # for packaging debhelper (>= 9), dh-translations, diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/CMakeLists.txt indicator-power-12.10.6+16.04.20160105/src/CMakeLists.txt --- indicator-power-12.10.6+15.10.20151005.1/src/CMakeLists.txt 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/CMakeLists.txt 2016-01-05 14:37:48.000000000 +0000 @@ -6,13 +6,16 @@ # handwritten sources set(SERVICE_MANUAL_SOURCES brightness.c + datafiles.c device-provider-mock.c device-provider-upower.c device-provider.c device.c notifier.c testing.c - service.c) + service.c + sound-player.c + sound-player-gst.c) # generated sources include(GdbusCodegen) @@ -29,6 +32,11 @@ com.canonical.indicator.power Dbus ${CMAKE_SOURCE_DIR}/data/com.canonical.indicator.power.Testing.xml) +add_gdbus_codegen_with_namespace(SERVICE_GENERATED_SOURCES dbus-accounts-sound + com.ubuntu.touch + Dbus + /usr/share/accountsservice/interfaces/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 -Nru indicator-power-12.10.6+15.10.20151005.1/src/datafiles.c indicator-power-12.10.6+16.04.20160105/src/datafiles.c --- indicator-power-12.10.6+15.10.20151005.1/src/datafiles.c 1970-01-01 00:00:00.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/datafiles.c 2016-01-05 14:37:48.000000000 +0000 @@ -0,0 +1,71 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include "datafiles.h" + +static const gchar* +get_directory_prefix_for_type (DatafileType type) +{ + switch (type) + { + case DATAFILE_TYPE_SOUND: + return "sounds"; + + default: + g_critical("unknown type"); + return ""; + } +} + +static gchar* +test_directory_for_file(const char* dir, DatafileType type, const char* basename) +{ + gchar* filename = g_build_filename(dir, + GETTEXT_PACKAGE, + get_directory_prefix_for_type(type), + basename, + NULL); + + g_debug("looking for \"%s\" at \"%s\"", basename, filename); + if (g_file_test(filename, G_FILE_TEST_EXISTS)) + return filename; + + g_free(filename); + return NULL; +} + +gchar* +datafile_find(DatafileType type, const char * basename) +{ + gchar * filename; + const gchar * user_data_dir; + const gchar * const * system_data_dirs; + gsize i; + + user_data_dir = g_get_user_data_dir(); + if ((filename = test_directory_for_file(user_data_dir, type, basename))) + return filename; + + system_data_dirs = g_get_system_data_dirs(); + for (i=0; system_data_dirs && system_data_dirs[i]; ++i) + if ((filename = test_directory_for_file(system_data_dirs[i], type, basename))) + return filename; + + return NULL; +} diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/datafiles.h indicator-power-12.10.6+16.04.20160105/src/datafiles.h --- indicator-power-12.10.6+15.10.20151005.1/src/datafiles.h 1970-01-01 00:00:00.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/datafiles.h 2016-01-05 14:37:48.000000000 +0000 @@ -0,0 +1,37 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#ifndef __INDICATOR_POWER_DATAFILES_H__ +#define __INDICATOR_POWER_DATAFILES_H__ + +#include + +G_BEGIN_DECLS + +typedef enum +{ + DATAFILE_TYPE_SOUND +} +DatafileType; + +gchar* datafile_find(DatafileType type, const char * basename); + +G_END_DECLS + +#endif /* __INDICATOR_POWER_DATAFILES_H__ */ diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/main.c indicator-power-12.10.6+16.04.20160105/src/main.c --- indicator-power-12.10.6+15.10.20151005.1/src/main.c 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/main.c 2016-01-05 14:37:48.000000000 +0000 @@ -1,8 +1,5 @@ /* - * Copyright 2013 Canonical Ltd. - * - * Authors: - * Charles Kerr + * Copyright 2013-2016 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published @@ -15,6 +12,9 @@ * * You should have received a copy of the GNU General Public License along * with this program. If not, see . + * + * Authors: + * Charles Kerr */ #include @@ -23,7 +23,9 @@ #include #include "device.h" +#include "notifier.h" #include "service.h" +#include "sound-player-gst.h" #include "testing.h" /*** @@ -40,6 +42,8 @@ int main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) { + IndicatorPowerSoundPlayer * sound_player; + IndicatorPowerNotifier * notifier; IndicatorPowerService * service; IndicatorPowerTesting * testing; GMainLoop * loop; @@ -50,7 +54,9 @@ textdomain (GETTEXT_PACKAGE); /* run */ - service = indicator_power_service_new (NULL); + sound_player = indicator_power_sound_player_gst_new (); + notifier = indicator_power_notifier_new (sound_player); + service = indicator_power_service_new(NULL, notifier); testing = indicator_power_testing_new (service); loop = g_main_loop_new (NULL, FALSE); g_signal_connect (service, INDICATOR_POWER_SERVICE_SIGNAL_NAME_LOST, @@ -59,7 +65,9 @@ /* cleanup */ g_main_loop_unref (loop); - g_clear_object (&service); g_clear_object (&testing); + g_clear_object (&service); + g_clear_object (¬ifier); + g_clear_object (&sound_player); return 0; } diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/notifier.c indicator-power-12.10.6+16.04.20160105/src/notifier.c --- indicator-power-12.10.6+15.10.20151005.1/src/notifier.c 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/notifier.c 2016-01-05 14:37:48.000000000 +0000 @@ -1,5 +1,5 @@ /* - * Copyright 2014 Canonical Ltd. + * Copyright 2014-2016 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published @@ -17,9 +17,12 @@ * Charles Kerr */ +#include "datafiles.h" +#include "dbus-accounts-sound.h" #include "dbus-battery.h" #include "dbus-shared.h" #include "notifier.h" +#include "sound-player.h" #include @@ -46,10 +49,12 @@ { PROP_0, PROP_BATTERY, + PROP_SOUND_PLAYER, LAST_PROP }; #define PROP_BATTERY_NAME "battery" +#define PROP_SOUND_PLAYER_NAME "sound-player" static GParamSpec * properties[LAST_PROP]; @@ -77,6 +82,12 @@ gboolean caps_queried; gboolean actions_supported; + + IndicatorPowerSoundPlayer * sound_player; + + GCancellable * cancellable; + DbusAccountsServiceSound * accounts_service_sound_proxy; + gboolean accounts_service_sound_proxy_pending; } IndicatorPowerNotifierPrivate; @@ -131,6 +142,82 @@ } /*** +**** 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)) + { + get_priv(gself)->accounts_service_sound_proxy_pending = FALSE; + g_debug("%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); + g_clear_object (&p->accounts_service_sound_proxy); + p->accounts_service_sound_proxy = proxy; + p->accounts_service_sound_proxy_pending = FALSE; + } +} + +static gboolean +silent_mode (IndicatorPowerNotifier * self) +{ + priv_t * const p = get_priv (self); + + /* if we don't have a proxy yet, assume we're in silent mode + as a "do no harm" level of response */ + if (p->accounts_service_sound_proxy_pending) + return TRUE; + + 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) +{ + const gchar * const key = LOW_BATTERY_SOUND; + 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) + { + gchar * uri = g_filename_to_uri(filename, NULL, NULL); + indicator_power_sound_player_play_uri (p->sound_player, uri); + g_free(uri); + g_free(filename); + } + else + { + g_warning("Unable to find '%s' in XDG data dirs", key); + } +} + +/*** **** Notifications ***/ @@ -300,6 +387,7 @@ ((new_power_level != POWER_LEVEL_OK) && new_discharging && !old_discharging)) { notification_show (self); + play_low_battery_sound (self); } else if (!new_discharging || (new_power_level == POWER_LEVEL_OK)) { @@ -330,6 +418,10 @@ g_value_set_object (value, p->battery); break; + case PROP_SOUND_PLAYER: + g_value_set_object (value, p->sound_player); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -349,6 +441,10 @@ indicator_power_notifier_set_battery (self, g_value_get_object(value)); break; + case PROP_SOUND_PLAYER: + indicator_power_notifier_set_sound_player (self, g_value_get_object(value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -360,10 +456,18 @@ 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); } @@ -382,7 +486,6 @@ **** Instantiation ***/ - static void indicator_power_notifier_init (IndicatorPowerNotifier * self) { @@ -394,8 +497,22 @@ p->power_level = POWER_LEVEL_OK; + p->cancellable = g_cancellable_new(); + if (!instance_count++ && !notify_init("indicator-power-service")) g_critical("Unable to initialize libnotify! Notifications might not be shown."); + + p->accounts_service_sound_proxy_pending = TRUE; + 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 @@ -415,6 +532,13 @@ G_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + properties[PROP_SOUND_PLAYER] = g_param_spec_object ( + PROP_SOUND_PLAYER_NAME, + "Sound Player", + "The current sound player", + G_TYPE_OBJECT, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_properties (object_class, LAST_PROP, properties); } @@ -423,9 +547,11 @@ ***/ IndicatorPowerNotifier * -indicator_power_notifier_new (void) +indicator_power_notifier_new (IndicatorPowerSoundPlayer * sound_player) { - GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER, NULL); + GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER, + PROP_SOUND_PLAYER_NAME, sound_player, + NULL); return INDICATOR_POWER_NOTIFIER (o); } @@ -465,6 +591,26 @@ } void +indicator_power_notifier_set_sound_player (IndicatorPowerNotifier * self, + IndicatorPowerSoundPlayer * sound_player) +{ + priv_t * p; + + g_return_if_fail(INDICATOR_IS_POWER_NOTIFIER(self)); + g_return_if_fail((sound_player == NULL) || INDICATOR_IS_POWER_SOUND_PLAYER(sound_player)); + + p = get_priv (self); + + if (p->sound_player == sound_player) + return; + + g_clear_object(&p->sound_player); + + if (sound_player != NULL) + p->sound_player = g_object_ref(sound_player); +} + +void indicator_power_notifier_set_bus (IndicatorPowerNotifier * self, GDBusConnection * bus) { diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/notifier.h indicator-power-12.10.6+16.04.20160105/src/notifier.h --- indicator-power-12.10.6+15.10.20151005.1/src/notifier.h 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/notifier.h 2016-01-05 14:37:48.000000000 +0000 @@ -1,5 +1,5 @@ /* - * Copyright 2014 Canonical Ltd. + * Copyright 2014-2016 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published @@ -23,6 +23,7 @@ #include #include "device.h" +#include "sound-player.h" G_BEGIN_DECLS @@ -55,7 +56,7 @@ GType indicator_power_notifier_get_type (void); -IndicatorPowerNotifier * indicator_power_notifier_new (void); +IndicatorPowerNotifier * indicator_power_notifier_new (IndicatorPowerSoundPlayer * sound_player); void indicator_power_notifier_set_bus (IndicatorPowerNotifier * self, GDBusConnection * connection); @@ -63,6 +64,9 @@ void indicator_power_notifier_set_battery (IndicatorPowerNotifier * self, IndicatorPowerDevice * battery); +void indicator_power_notifier_set_sound_player (IndicatorPowerNotifier * self, + IndicatorPowerSoundPlayer * battery); + #define POWER_LEVEL_STR_OK "ok" #define POWER_LEVEL_STR_LOW "low" #define POWER_LEVEL_STR_VERY_LOW "very_low" diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/service.c indicator-power-12.10.6+16.04.20160105/src/service.c --- indicator-power-12.10.6+15.10.20151005.1/src/service.c 2015-10-05 18:44:19.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/service.c 2016-01-05 14:37:48.000000000 +0000 @@ -1,9 +1,5 @@ /* - * Copyright 2013 Canonical Ltd. - * - * Authors: - * Charles Kerr - * Ted Gould + * Copyright 2013-2016 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published @@ -16,6 +12,10 @@ * * You should have received a copy of the GNU General Public License along * with this program. If not, see . + * + * Authors: + * Charles Kerr + * Ted Gould */ #include @@ -53,6 +53,7 @@ PROP_0, PROP_BUS, PROP_DEVICE_PROVIDER, + PROP_NOTIFIER, LAST_PROP }; @@ -952,7 +953,8 @@ g_object_notify_by_pspec (G_OBJECT(self), properties[PROP_BUS]); /* export the battery properties */ - indicator_power_notifier_set_bus (p->notifier, connection); + if (p->notifier != NULL) + indicator_power_notifier_set_bus (p->notifier, connection); /* export the actions */ if ((id = g_dbus_connection_export_action_group (connection, @@ -1094,6 +1096,10 @@ g_value_set_object (value, p->device_provider); break; + case PROP_NOTIFIER: + g_value_set_object (value, p->notifier); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -1113,6 +1119,10 @@ indicator_power_service_set_device_provider (self, g_value_get_object (value)); break; + case PROP_NOTIFIER: + indicator_power_service_set_notifier (self, g_value_get_object (value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -1155,6 +1165,7 @@ g_clear_object (&p->conn); indicator_power_service_set_device_provider (self, NULL); + indicator_power_service_set_notifier (self, NULL); G_OBJECT_CLASS (indicator_power_service_parent_class)->dispose (o); } @@ -1178,8 +1189,6 @@ p->settings = g_settings_new ("com.canonical.indicator.power"); - p->notifier = indicator_power_notifier_new (); - p->brightness = indicator_power_brightness_new(); g_signal_connect_swapped(p->brightness, "notify::percentage", G_CALLBACK(update_brightness_action_state), self); @@ -1241,6 +1250,13 @@ G_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + properties[PROP_NOTIFIER] = g_param_spec_object ( + "notifier", + "Notifier", + "Notifies user of important battery changes", + G_TYPE_OBJECT, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_properties (object_class, LAST_PROP, properties); } @@ -1249,10 +1265,12 @@ ***/ IndicatorPowerService * -indicator_power_service_new (IndicatorPowerDeviceProvider * device_provider) +indicator_power_service_new (IndicatorPowerDeviceProvider * device_provider, + IndicatorPowerNotifier * notifier) { GObject * o = g_object_new (INDICATOR_TYPE_POWER_SERVICE, "device-provider", device_provider, + "notifier", notifier, NULL); return INDICATOR_POWER_SERVICE (o); @@ -1291,6 +1309,29 @@ } } +void +indicator_power_service_set_notifier (IndicatorPowerService * self, + IndicatorPowerNotifier * notifier) +{ + priv_t * p; + + g_return_if_fail (INDICATOR_IS_POWER_SERVICE (self)); + g_return_if_fail (!notifier || INDICATOR_IS_POWER_NOTIFIER (notifier)); + p = self->priv; + + if (p->notifier == notifier) + return; + + g_clear_object (&p->notifier); + + if (notifier != NULL) + { + p->notifier = g_object_ref (notifier); + indicator_power_notifier_set_bus (p->notifier, p->conn); + } +} + + /* If a device has multiple batteries and uses only one of them at a time, they should be presented as separate items inside the battery menu, but everywhere else they should be aggregated (bug 880881). diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/service.h indicator-power-12.10.6+16.04.20160105/src/service.h --- indicator-power-12.10.6+15.10.20151005.1/src/service.h 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/service.h 2016-01-05 14:37:48.000000000 +0000 @@ -24,6 +24,7 @@ #include #include "device-provider.h" +#include "notifier.h" G_BEGIN_DECLS @@ -64,11 +65,15 @@ GType indicator_power_service_get_type (void); -IndicatorPowerService * indicator_power_service_new (IndicatorPowerDeviceProvider * provider); +IndicatorPowerService * indicator_power_service_new (IndicatorPowerDeviceProvider * provider, + IndicatorPowerNotifier * notifier); void indicator_power_service_set_device_provider (IndicatorPowerService * self, IndicatorPowerDeviceProvider * provider); +void indicator_power_service_set_notifier (IndicatorPowerService * self, + IndicatorPowerNotifier * notifier); + IndicatorPowerDevice * indicator_power_service_choose_primary_device (GList * devices); diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/sound-player.c indicator-power-12.10.6+16.04.20160105/src/sound-player.c --- indicator-power-12.10.6+15.10.20151005.1/src/sound-player.c 1970-01-01 00:00:00.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/sound-player.c 2016-01-05 14:37:48.000000000 +0000 @@ -0,0 +1,45 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include "sound-player.h" + +G_DEFINE_INTERFACE (IndicatorPowerSoundPlayer, + indicator_power_sound_player, + 0) + +static void +indicator_power_sound_player_default_init (IndicatorPowerSoundPlayerInterface * klass G_GNUC_UNUSED) +{ +} + +/*** +**** PUBLIC API +***/ + +void +indicator_power_sound_player_play_uri (IndicatorPowerSoundPlayer * self, + const gchar * uri) +{ + IndicatorPowerSoundPlayerInterface * iface; + + g_return_if_fail (INDICATOR_IS_POWER_SOUND_PLAYER (self)); + iface = INDICATOR_POWER_SOUND_PLAYER_GET_INTERFACE (self); + g_return_if_fail (iface->play_uri != NULL); + iface->play_uri (self, uri); +} diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/sound-player-gst.c indicator-power-12.10.6+16.04.20160105/src/sound-player-gst.c --- indicator-power-12.10.6+15.10.20151005.1/src/sound-player-gst.c 1970-01-01 00:00:00.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/sound-player-gst.c 2016-01-05 14:37:48.000000000 +0000 @@ -0,0 +1,173 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include "sound-player.h" +#include "sound-player-gst.h" + +#include + + +/*** +**** private struct +***/ + +typedef struct +{ + int unused; +} +IndicatorPowerSoundPlayerGSTPrivate; + +typedef IndicatorPowerSoundPlayerGSTPrivate priv_t; + +#define get_priv(o) ((priv_t*)indicator_power_sound_player_gst_get_instance_private(o)) + + +/*** +**** GObject boilerplate +***/ + +static void indicator_power_device_provider_interface_init ( + IndicatorPowerSoundPlayerInterface * iface); + +G_DEFINE_TYPE_WITH_CODE ( + IndicatorPowerSoundPlayerGST, + indicator_power_sound_player_gst, + G_TYPE_OBJECT, + G_ADD_PRIVATE(IndicatorPowerSoundPlayerGST) + G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_POWER_SOUND_PLAYER, + indicator_power_device_provider_interface_init)) + +/*** +**** GSTREAMER +***/ + +static void +gst_init_once(void) +{ + static gboolean gst_init_checked = FALSE; + + if (G_UNLIKELY(!gst_init_checked)) + { + GError* error = NULL; + if (!gst_init_check(NULL, NULL, &error)) + { + g_critical("Unable to play alarm sound: %s", error->message); + g_error_free(error); + } + gst_init_checked = TRUE; + } +} + +static gboolean bus_callback(GstBus* bus G_GNUC_UNUSED, GstMessage* msg, gpointer gelement) +{ + const GstMessageType message_type = GST_MESSAGE_TYPE(msg); + + if (GST_MESSAGE_SRC(msg) != gelement) + return G_SOURCE_CONTINUE; + + /* on eos, cleanup the element and cancel our gst bus subscription */ + if (message_type == GST_MESSAGE_EOS) + { + g_debug("got GST_MESSAGE_EOS on sound play"); + gst_element_set_state(GST_ELEMENT(gelement), GST_STATE_NULL); + gst_object_unref(gelement); + return G_SOURCE_REMOVE; + } + + /* on stream start, set the media role to 'alert' if we're using pulsesink */ + if (message_type == GST_MESSAGE_STREAM_START) + { + GstElement* audio_sink = NULL; + g_debug("got GST_MESSAGE_STREAM_START on sound play"); + g_object_get(gelement, "audio-sink", &audio_sink, NULL); + if (audio_sink != NULL) + { + GstPluginFeature* feature; + feature = GST_PLUGIN_FEATURE_CAST(GST_ELEMENT_GET_CLASS(audio_sink)->elementfactory); + if (feature && g_strcmp0(gst_plugin_feature_get_name(feature), "pulsesink") == 0) + { + const gchar* const props_str = "props,media.role=alert"; + GstStructure* props = gst_structure_from_string(props_str, NULL); + g_debug("setting audio sink properties to '%s'", props_str); + g_object_set(audio_sink, "stream-properties", props, NULL); + g_clear_pointer(&props, gst_structure_free); + } + gst_object_unref(audio_sink); + } + } + + return G_SOURCE_CONTINUE; +} + +/*** +**** IndicatorPowerSoundPlayer virtual functions +***/ + +static void +my_play_uri (IndicatorPowerSoundPlayer * self G_GNUC_UNUSED, const gchar * uri) +{ + GstElement * element; + GstBus * bus; + + /* create the element */ + element = gst_element_factory_make("playbin", NULL); + + /* start listening for gst events */ + bus = gst_pipeline_get_bus(GST_PIPELINE(element)); + gst_bus_add_watch(bus, bus_callback, element); + gst_object_unref(bus); + + /* play the sound */ + g_debug("Playing '%s'", uri); + g_object_set(element, "uri", uri, NULL); + gst_element_set_state(element, GST_STATE_PLAYING); +} + +/*** +**** Instantiation +***/ + +static void +indicator_power_sound_player_gst_class_init (IndicatorPowerSoundPlayerGSTClass * klass G_GNUC_UNUSED) +{ + gst_init_once(); +} + +static void +indicator_power_device_provider_interface_init (IndicatorPowerSoundPlayerInterface * iface) +{ + iface->play_uri = my_play_uri; +} + +static void +indicator_power_sound_player_gst_init (IndicatorPowerSoundPlayerGST * self G_GNUC_UNUSED) +{ +} + +/*** +**** Public API +***/ + +IndicatorPowerSoundPlayer * +indicator_power_sound_player_gst_new(void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, NULL); + + return INDICATOR_POWER_SOUND_PLAYER (o); +} diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/sound-player-gst.h indicator-power-12.10.6+16.04.20160105/src/sound-player-gst.h --- indicator-power-12.10.6+15.10.20151005.1/src/sound-player-gst.h 1970-01-01 00:00:00.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/sound-player-gst.h 2016-01-05 14:37:48.000000000 +0000 @@ -0,0 +1,70 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#ifndef __INDICATOR_POWER_SOUND_PLAYER_GST__H__ +#define __INDICATOR_POWER_SOUND_PLAYER_GST__H__ + +#include /* parent class */ + +#include "device-provider.h" + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_POWER_SOUND_PLAYER_GST \ + (indicator_power_sound_player_gst_get_type()) + +#define INDICATOR_POWER_SOUND_PLAYER_GST(o) \ + (G_TYPE_CHECK_INSTANCE_CAST ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, \ + IndicatorPowerSoundPlayerGST)) + +#define INDICATOR_POWER_SOUND_PLAYER_GST_GET_CLASS(o) \ + (G_TYPE_INSTANCE_GET_CLASS ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, \ + IndicatorPowerSoundPlayerGSTClass)) + +#define INDICATOR_IS_POWER_SOUND_PLAYER_GST(o) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_GST)) + +typedef struct _IndicatorPowerSoundPlayerGST + IndicatorPowerSoundPlayerGST; +typedef struct _IndicatorPowerSoundPlayerGSTClass + IndicatorPowerSoundPlayerGSTClass; + +/** + * An IndicatorPowerSoundPlayer which gets its devices from GST. + */ +struct _IndicatorPowerSoundPlayerGST +{ + GObject parent_instance; +}; + +struct _IndicatorPowerSoundPlayerGSTClass +{ + GObjectClass parent_class; +}; + +GType indicator_power_sound_player_gst_get_type (void); + +IndicatorPowerSoundPlayer * indicator_power_sound_player_gst_new (void); + +G_END_DECLS + +#endif /* __INDICATOR_POWER_SOUND_PLAYER_GST__H__ */ diff -Nru indicator-power-12.10.6+15.10.20151005.1/src/sound-player.h indicator-power-12.10.6+16.04.20160105/src/sound-player.h --- indicator-power-12.10.6+15.10.20151005.1/src/sound-player.h 1970-01-01 00:00:00.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/src/sound-player.h 2016-01-05 14:37:48.000000000 +0000 @@ -0,0 +1,72 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#ifndef __INDICATOR_POWER_SOUND_PLAYER__H__ +#define __INDICATOR_POWER_SOUND_PLAYER__H__ + +#include + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_POWER_SOUND_PLAYER \ + (indicator_power_sound_player_get_type ()) + +#define INDICATOR_POWER_SOUND_PLAYER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER, \ + IndicatorPowerSoundPlayer)) + +#define INDICATOR_IS_POWER_SOUND_PLAYER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_TYPE_POWER_SOUND_PLAYER)) + +#define INDICATOR_POWER_SOUND_PLAYER_GET_INTERFACE(inst) \ + (G_TYPE_INSTANCE_GET_INTERFACE ((inst), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER, \ + IndicatorPowerSoundPlayerInterface)) + +typedef struct _IndicatorPowerSoundPlayer + IndicatorPowerSoundPlayer; + +typedef struct _IndicatorPowerSoundPlayerInterface + IndicatorPowerSoundPlayerInterface; + +/** + * An interface class for an object that plays sounds. + */ +struct _IndicatorPowerSoundPlayerInterface +{ + GTypeInterface parent_iface; + + /* virtual functions */ + void (*play_uri) (IndicatorPowerSoundPlayer * self, + const gchar * uri); +}; + +GType indicator_power_sound_player_get_type (void); + +/*** +**** +***/ + +void indicator_power_sound_player_play_uri (IndicatorPowerSoundPlayer * self, + const gchar * uri); + +G_END_DECLS + +#endif /* __INDICATOR_POWER_SOUND_PLAYER__H__ */ diff -Nru indicator-power-12.10.6+15.10.20151005.1/tests/CMakeLists.txt indicator-power-12.10.6+16.04.20160105/tests/CMakeLists.txt --- indicator-power-12.10.6+15.10.20151005.1/tests/CMakeLists.txt 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/tests/CMakeLists.txt 2016-01-05 14:37:48.000000000 +0000 @@ -14,11 +14,23 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g ${C_WARNING_ARGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-weak-vtables -Wno-global-constructors") # Google Test +# build the mocks +set(MOCK_LIB "indicatorpowerservicemocks") +set(MOCK_SOURCES sound-player-mock.c) +set_source_files_properties(${MOCK_SOURCES} + PROPERTIES COMPILE_FLAGS "${C_WARNING_ARGS} -g -std=c99") +add_library(${MOCK_LIB} STATIC ${MOCK_SOURCES}) + # build the necessary schemas set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES gschemas.compiled) set_source_files_properties (gschemas.compiled GENERATED) +# make a XDG_DATA_HOME for sounds/ +set(XDG_DATA_HOME "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_DATADIR}") +add_definitions(-DXDG_DATA_HOME="${XDG_DATA_HOME}") +file(COPY "${CMAKE_SOURCE_DIR}/data/sounds" DESTINATION "${XDG_DATA_HOME}/${CMAKE_PROJECT_NAME}") + # GSettings: # compile the indicator-power schema into a gschemas.compiled file in this directory, # and help the tests to find that file by setting -DSCHEMA_DIR @@ -44,8 +56,8 @@ set (TEST_NAME ${name}) add_executable (${TEST_NAME} ${TEST_NAME}.cc gschemas.compiled) add_test (${TEST_NAME} ${TEST_NAME}) - add_dependencies (${TEST_NAME} libindicatorpowerservice) - target_link_libraries (${TEST_NAME} indicatorpowerservice gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS}) + add_dependencies (${TEST_NAME} ${MOCK_LIB} libindicatorpowerservice) + target_link_libraries (${TEST_NAME} ${MOCK_LIB} indicatorpowerservice gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS}) endfunction() add_test_by_name(test-notify) add_test(NAME dear-reader-the-next-test-takes-80-seconds COMMAND true) diff -Nru indicator-power-12.10.6+15.10.20151005.1/tests/glib-fixture.h indicator-power-12.10.6+16.04.20160105/tests/glib-fixture.h --- indicator-power-12.10.6+15.10.20151005.1/tests/glib-fixture.h 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/tests/glib-fixture.h 2016-01-05 14:37:48.000000000 +0000 @@ -52,7 +52,7 @@ if (expected_log[level] != n) for (size_t i=0; i. + * + * Authors: + * Charles Kerr + */ + +#include "sound-player.h" +#include "sound-player-mock.h" + +enum +{ + SIGNAL_URI_PLAYED, + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL] = { 0 }; + +/*** +**** GObject boilerplate +***/ + +static void indicator_power_sound_player_interface_init ( + IndicatorPowerSoundPlayerInterface * iface); + +G_DEFINE_TYPE_WITH_CODE ( + IndicatorPowerSoundPlayerMock, + indicator_power_sound_player_mock, + G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_POWER_SOUND_PLAYER, + indicator_power_sound_player_interface_init)) + +/*** +**** IndicatorPowerSoundPlayer virtual functions +***/ + +static void +my_play_uri (IndicatorPowerSoundPlayer * self, const gchar * uri) +{ + g_signal_emit (self, signals[SIGNAL_URI_PLAYED], 0, uri, NULL); +} + +/*** +**** Instantiation +***/ + +static void +indicator_power_sound_player_mock_class_init (IndicatorPowerSoundPlayerMockClass * klass G_GNUC_UNUSED) +{ + signals[SIGNAL_URI_PLAYED] = g_signal_new ( + "uri-played", + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (IndicatorPowerSoundPlayerMockClass, uri_played), + NULL, NULL, + g_cclosure_marshal_VOID__STRING, + G_TYPE_NONE, 1, G_TYPE_STRING); +} + +static void +indicator_power_sound_player_interface_init (IndicatorPowerSoundPlayerInterface * iface) +{ + iface->play_uri = my_play_uri; +} + +static void +indicator_power_sound_player_mock_init (IndicatorPowerSoundPlayerMock * self G_GNUC_UNUSED) +{ +} + +/*** +**** Public API +***/ + +IndicatorPowerSoundPlayer * +indicator_power_sound_player_mock_new (void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, NULL); + + return INDICATOR_POWER_SOUND_PLAYER (o); +} + diff -Nru indicator-power-12.10.6+15.10.20151005.1/tests/sound-player-mock.h indicator-power-12.10.6+16.04.20160105/tests/sound-player-mock.h --- indicator-power-12.10.6+15.10.20151005.1/tests/sound-player-mock.h 1970-01-01 00:00:00.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/tests/sound-player-mock.h 2016-01-05 14:37:48.000000000 +0000 @@ -0,0 +1,75 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#ifndef __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__ +#define __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__ + +#include /* parent class */ + +#include "sound-player.h" + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK \ + (indicator_power_sound_player_mock_get_type()) + +#define INDICATOR_POWER_SOUND_PLAYER_MOCK(o) \ + (G_TYPE_CHECK_INSTANCE_CAST ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, \ + IndicatorPowerSoundPlayerMock)) + +#define INDICATOR_POWER_SOUND_PLAYER_MOCK_GET_CLASS(o) \ + (G_TYPE_INSTANCE_GET_CLASS ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, \ + IndicatorPowerSoundPlayerMockClass)) + +#define INDICATOR_IS_POWER_SOUND_PLAYER_MOCK(o) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK)) + +typedef struct _IndicatorPowerSoundPlayerMock + IndicatorPowerSoundPlayerMock; +typedef struct _IndicatorPowerSoundPlayerMockPriv + IndicatorPowerSoundPlayerMockPriv; +typedef struct _IndicatorPowerSoundPlayerMockClass + IndicatorPowerSoundPlayerMockClass; + +/** + * An IndicatorPowerSoundPlayer which gets its devices from Mock. + */ +struct _IndicatorPowerSoundPlayerMock +{ + GObject parent_instance; +}; + +struct _IndicatorPowerSoundPlayerMockClass +{ + GObjectClass parent_class; + + /* signals */ + void (*uri_played) (IndicatorPowerSoundPlayer * self, const gchar* uri); +}; + +GType indicator_power_sound_player_mock_get_type (void); + +IndicatorPowerSoundPlayer * indicator_power_sound_player_mock_new (void); + +G_END_DECLS + +#endif /* __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__ */ diff -Nru indicator-power-12.10.6+15.10.20151005.1/tests/test-notify.cc indicator-power-12.10.6+16.04.20160105/tests/test-notify.cc --- indicator-power-12.10.6+15.10.20151005.1/tests/test-notify.cc 2015-10-05 18:44:12.000000000 +0000 +++ indicator-power-12.10.6+16.04.20160105/tests/test-notify.cc 2016-01-05 14:37:48.000000000 +0000 @@ -1,5 +1,5 @@ /* - * Copyright 2014 Canonical Ltd. + * Copyright 2014-2016 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published @@ -23,6 +23,7 @@ #include "dbus-shared.h" #include "device.h" #include "notifier.h" +#include "sound-player-mock.h" #include @@ -77,6 +78,8 @@ { super::SetUp(); + g_setenv ("XDG_DATA_HOME", XDG_DATA_HOME, TRUE); + // init DBusMock / dbus-test-runner service = dbus_test_service_new(nullptr); @@ -273,7 +276,8 @@ // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. - auto notifier = indicator_power_notifier_new (); + auto sound_player = indicator_power_sound_player_mock_new (); + auto notifier = indicator_power_notifier_new (sound_player); indicator_power_notifier_set_battery (notifier, battery); indicator_power_notifier_set_bus (notifier, bus); wait_msec(); @@ -315,14 +319,23 @@ // cleanup g_dbus_connection_signal_unsubscribe (bus, sub_tag); - g_object_unref (notifier); g_object_unref (battery); + g_object_unref (notifier); + g_object_unref (sound_player); } /*** **** ***/ +namespace +{ + static void on_uri_played(IndicatorPowerSoundPlayer*, const char* uri, gpointer glast_uri) + { + *static_cast(glast_uri) = uri; + } +} + TEST_F(NotifyFixture, EventsThatChangeNotifications) { // GetCapabilities returns an array containing 'actions', so that we'll @@ -343,9 +356,18 @@ UP_DEVICE_STATE_DISCHARGING, 30); + // the file we expect to play on a low battery notification... + const char* expected_file = XDG_DATA_HOME "/" GETTEXT_PACKAGE "/sounds/" LOW_BATTERY_SOUND; + char* tmp = g_filename_to_uri(expected_file, nullptr, nullptr); + const std::string low_power_uri {tmp}; + g_clear_pointer(&tmp, g_free); + // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. - auto notifier = indicator_power_notifier_new (); + std::string last_uri; + auto sound_player = indicator_power_sound_player_mock_new (); + g_signal_connect(sound_player, "uri-played", G_CALLBACK(on_uri_played), &last_uri); + auto notifier = indicator_power_notifier_new (sound_player); indicator_power_notifier_set_battery (notifier, battery); indicator_power_notifier_set_bus (notifier, bus); ChangedParams changed_params; @@ -363,6 +385,7 @@ // test setup case wait_msec(); EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); + EXPECT_TRUE(last_uri.empty()); // change the percent past the 'low' threshold and confirm that // a) the power level changes @@ -373,38 +396,48 @@ EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields); EXPECT_EQ (indicator_power_notifier_get_power_level(battery), changed_params.power_level); EXPECT_TRUE (changed_params.is_warning); + EXPECT_EQ (low_power_uri, last_uri); // now test that the warning changes if the level goes down even lower... + last_uri.clear(); changed_params = ChangedParams(); set_battery_percentage (battery, percent_very_low); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL, changed_params.fields); EXPECT_STREQ (POWER_LEVEL_STR_VERY_LOW, changed_params.power_level.c_str()); + EXPECT_EQ (low_power_uri, last_uri); // ...and that the warning is taken down if the battery is plugged back in... + last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, nullptr); wait_msec(); EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields); EXPECT_FALSE (changed_params.is_warning); + EXPECT_TRUE(last_uri.empty()); // ...and that it comes back if we unplug again... + last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, nullptr); wait_msec(); EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields); EXPECT_TRUE (changed_params.is_warning); + EXPECT_EQ (low_power_uri, last_uri); // ...and that it's taken down if the power level is OK + last_uri.clear(); changed_params = ChangedParams(); set_battery_percentage (battery, percent_low+1); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields); EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); EXPECT_FALSE (changed_params.is_warning); + EXPECT_TRUE(last_uri.empty()); // cleanup g_dbus_connection_signal_unsubscribe (bus, sub_tag); g_object_unref (notifier); g_object_unref (battery); + g_object_unref (sound_player); }