From a8fd3f49bf6509bd4545aec3292d2fc022d847ca Mon Sep 17 00:00:00 2001 From: charles kerr Date: Fri, 1 Jan 2016 15:58:55 -0600 Subject: make a SoundPlayer interface so we can mock it in the tests this requires an annoying amount of scaffolding: 1. implement gst and mock classes to implement the SoundPlayer interface 2. modify notifier to take a SoundPlayer argument in its ctor 3. modify service to take a Notifier argument in its ctor instead of instantiating it on its own 4. change main to update the startup steps for player/notifier/service --- src/notifier.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 9 deletions(-) (limited to 'src/notifier.c') diff --git a/src/notifier.c b/src/notifier.c index 63e7c89..b50f48c 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -22,10 +22,10 @@ #include "dbus-shared.h" #include "notifier.h" #include "utils.h" -#include "sound.h" +#include "sound-player.h" #ifdef HAS_URLDISPATCHER -# include +#include #endif #include @@ -51,10 +51,12 @@ enum { 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]; @@ -82,6 +84,8 @@ typedef struct gboolean caps_queried; gboolean actions_supported; + + IndicatorPowerSoundPlayer * sound_player; } IndicatorPowerNotifierPrivate; @@ -140,17 +144,19 @@ get_battery_power_level (IndicatorPowerDevice * battery) ***/ static void -play_low_battery_sound (void) +play_low_battery_sound (IndicatorPowerNotifier * self) { - const gchar * key; + const gchar * const key = "Low battery.ogg"; gchar * filename; + priv_t * const p = get_priv(self); + + g_return_if_fail (p->sound_player != NULL); - key = "Low battery.ogg"; filename = datafile_find(DATAFILE_TYPE_SOUND, key); if (filename != NULL) { gchar * uri = g_filename_to_uri(filename, NULL, NULL); - sound_play_uri(uri); + indicator_power_sound_player_play_uri (p->sound_player, uri); g_free(uri); g_free(filename); } @@ -330,7 +336,7 @@ on_battery_property_changed (IndicatorPowerNotifier * self) ((new_power_level != POWER_LEVEL_OK) && new_discharging && !old_discharging)) { notification_show (self); - play_low_battery_sound(); + play_low_battery_sound (self); } else if (!new_discharging || (new_power_level == POWER_LEVEL_OK)) { @@ -361,6 +367,10 @@ my_get_property (GObject * o, 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); } @@ -380,6 +390,10 @@ my_set_property (GObject * o, 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); } @@ -446,6 +460,13 @@ indicator_power_notifier_class_init (IndicatorPowerNotifierClass * klass) 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 battery", + G_TYPE_OBJECT, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_properties (object_class, LAST_PROP, properties); } @@ -454,9 +475,11 @@ indicator_power_notifier_class_init (IndicatorPowerNotifierClass * klass) ***/ 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); } @@ -495,6 +518,26 @@ indicator_power_notifier_set_battery (IndicatorPowerNotifier * self, } } +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) -- cgit v1.2.3