2 * empathy-sound-manager.c - Various sound related utility functions.
3 * Copyright (C) 2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "empathy-sound-manager.h"
24 #include <canberra-gtk.h>
25 #include <glib/gi18n-lib.h>
28 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
29 #include <libempathy/empathy-debug.h>
30 #include <libempathy/empathy-gsettings.h>
31 #include <libempathy/empathy-utils.h>
34 EmpathySound sound_id;
35 const char * event_ca_id;
36 const char * event_ca_description;
44 guint replay_timeout_id;
45 } EmpathyRepeatableSound;
47 /* NOTE: these entries MUST be in the same order than EmpathySound enum */
48 static EmpathySoundEntry sound_entries[LAST_EMPATHY_SOUND] = {
49 { EMPATHY_SOUND_MESSAGE_INCOMING, "message-new-instant",
50 N_("Received an instant message"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE } ,
51 { EMPATHY_SOUND_MESSAGE_OUTGOING, "message-sent-instant",
52 N_("Sent an instant message"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE } ,
53 { EMPATHY_SOUND_CONVERSATION_NEW, "message-new-instant",
54 N_("Incoming chat request"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
55 { EMPATHY_SOUND_CONTACT_CONNECTED, "service-login",
56 N_("Contact connected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
57 { EMPATHY_SOUND_CONTACT_DISCONNECTED, "service-logout",
58 N_("Contact disconnected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
59 { EMPATHY_SOUND_ACCOUNT_CONNECTED, "service-login",
60 N_("Connected to server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
61 { EMPATHY_SOUND_ACCOUNT_DISCONNECTED, "service-logout",
62 N_("Disconnected from server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT },
63 { EMPATHY_SOUND_PHONE_INCOMING, "phone-incoming-call",
64 N_("Incoming voice call"), NULL },
65 { EMPATHY_SOUND_PHONE_OUTGOING, "phone-outgoing-calling",
66 N_("Outgoing voice call"), NULL },
67 { EMPATHY_SOUND_PHONE_HANGUP, "phone-hangup",
68 N_("Voice call ended"), NULL },
71 G_DEFINE_TYPE (EmpathySoundManager, empathy_sound_manager, G_TYPE_OBJECT)
73 struct _EmpathySoundManagerPrivate
79 empathy_sound_manager_class_init (EmpathySoundManagerClass *cls)
81 g_type_class_add_private (cls, sizeof (EmpathySoundManagerPrivate));
85 empathy_sound_manager_init (EmpathySoundManager *self)
87 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
88 EMPATHY_TYPE_SOUND_MANAGER, EmpathySoundManagerPrivate);
92 empathy_sound_manager_dup_singleton (void)
94 static EmpathySoundManager *manager = NULL;
97 return g_object_ref (manager);
99 manager = g_object_new (EMPATHY_TYPE_SOUND_MANAGER, NULL);
101 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
105 /* An hash table containing currently repeating sounds. The format is the
107 * Key: An EmpathySound
108 * Value : The EmpathyRepeatableSound associated with that EmpathySound. */
109 static GHashTable *repeating_sounds;
112 empathy_sound_pref_is_enabled (EmpathySound sound_id)
114 EmpathySoundEntry *entry;
115 GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
118 entry = &(sound_entries[sound_id]);
119 g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
121 if (entry->key == NULL)
127 res = g_settings_get_boolean (gsettings, EMPATHY_PREFS_SOUNDS_ENABLED);
132 if (!empathy_check_available_state ())
134 if (g_settings_get_boolean (gsettings,
135 EMPATHY_PREFS_SOUNDS_DISABLED_AWAY))
142 res = g_settings_get_boolean (gsettings, entry->key);
145 g_object_unref (gsettings);
151 * empathy_sound_manager_stop:
152 * @self: a #EmpathySoundManager
153 * @sound_id: The #EmpathySound to stop playing.
155 * Stop playing a sound. If it has been stated in loop with
156 * empathy_sound_start_playing(), it will also stop replaying.
159 empathy_sound_manager_stop (EmpathySoundManager *self,
160 EmpathySound sound_id)
162 EmpathySoundEntry *entry;
164 g_return_if_fail (sound_id < LAST_EMPATHY_SOUND);
166 entry = &(sound_entries[sound_id]);
167 g_return_if_fail (entry->sound_id == sound_id);
169 if (repeating_sounds != NULL)
171 EmpathyRepeatableSound *repeatable_sound;
173 repeatable_sound = g_hash_table_lookup (repeating_sounds,
174 GINT_TO_POINTER (sound_id));
175 if (repeatable_sound != NULL)
177 /* The sound must be stopped... If it is waiting for replay, remove
178 * it from hash table to cancel. Otherwise we'll cancel the sound
180 if (repeatable_sound->replay_timeout_id != 0)
182 g_hash_table_remove (repeating_sounds, GINT_TO_POINTER (sound_id));
188 ca_context_cancel (ca_gtk_context_get (), entry->sound_id);
192 empathy_sound_play_internal (GtkWidget *widget, EmpathySound sound_id,
193 ca_finish_callback_t callback, gpointer user_data)
195 EmpathySoundEntry *entry;
197 ca_proplist *p = NULL;
199 entry = &(sound_entries[sound_id]);
200 g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
202 c = ca_gtk_context_get ();
203 ca_context_cancel (c, entry->sound_id);
205 DEBUG ("Play sound \"%s\" (%s)",
207 entry->event_ca_description);
209 if (ca_proplist_create (&p) < 0)
212 if (ca_proplist_sets (p, CA_PROP_EVENT_ID, entry->event_ca_id) < 0)
215 if (ca_proplist_sets (p, CA_PROP_EVENT_DESCRIPTION,
216 gettext (entry->event_ca_description)) < 0)
219 if (ca_gtk_proplist_set_for_widget (p, widget) < 0)
222 ca_context_play_full (ca_gtk_context_get (), entry->sound_id, p, callback,
225 ca_proplist_destroy (p);
231 ca_proplist_destroy (p);
237 * empathy_sound_manager_play_full:
238 * @self: a #EmpathySoundManager
239 * @widget: The #GtkWidget from which the sound is originating.
240 * @sound_id: The #EmpathySound to play.
241 * @callback: The #ca_finish_callback_t function that will be called when the
242 * sound has stopped playing.
243 * @user_data: user data to pass to the function.
247 * Returns %TRUE if the sound has successfully started playing, otherwise
248 * returning %FALSE and @callback won't be called.
250 * This function returns %FALSE if the sound is already playing in loop using
251 * %empathy_sound_start_playing.
253 * This function returns %FALSE if the sound is disabled in empathy preferences.
255 * Return value: %TRUE if the sound has successfully started playing, %FALSE
259 empathy_sound_manager_play_full (EmpathySoundManager *self,
261 EmpathySound sound_id,
262 ca_finish_callback_t callback,
265 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
266 g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
268 if (!empathy_sound_pref_is_enabled (sound_id))
271 /* The sound might already be playing repeatedly. If it's the case, we
272 * immediadely return since there's no need to make it play again */
273 if (repeating_sounds != NULL &&
274 g_hash_table_lookup (repeating_sounds, GINT_TO_POINTER (sound_id)) != NULL)
277 return empathy_sound_play_internal (widget, sound_id, callback, user_data);
281 * empathy_sound_manager_play:
282 * @self: a #EmpathySoundManager
283 * @widget: The #GtkWidget from which the sound is originating.
284 * @sound_id: The #EmpathySound to play.
286 * Plays a sound. See %empathy_sound_manager_play_full for details.'
288 * Return value: %TRUE if the sound has successfully started playing, %FALSE
292 empathy_sound_manager_play (EmpathySoundManager *self,
294 EmpathySound sound_id)
296 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
297 g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
299 return empathy_sound_manager_play_full (self, widget, sound_id, NULL, NULL);
302 static void playing_finished_cb (ca_context *c, guint id, int error_code,
306 playing_timeout_cb (gpointer data)
308 EmpathyRepeatableSound *repeatable_sound = data;
311 repeatable_sound->replay_timeout_id = 0;
313 playing = empathy_sound_play_internal (repeatable_sound->widget,
314 repeatable_sound->sound_id, playing_finished_cb, data);
318 DEBUG ("Failed to replay sound, stop repeating");
319 g_hash_table_remove (repeating_sounds,
320 GINT_TO_POINTER (repeatable_sound->sound_id));
327 playing_finished_cb (ca_context *c, guint id, int error_code,
330 EmpathyRepeatableSound *repeatable_sound = user_data;
332 if (error_code != CA_SUCCESS)
334 DEBUG ("Error: %s", ca_strerror (error_code));
335 g_hash_table_remove (repeating_sounds,
336 GINT_TO_POINTER (repeatable_sound->sound_id));
340 repeatable_sound->replay_timeout_id = g_timeout_add (
341 repeatable_sound->play_interval, playing_timeout_cb, user_data);
345 empathy_sound_widget_destroyed_cb (GtkWidget *widget, gpointer user_data)
347 EmpathyRepeatableSound *repeatable_sound = user_data;
349 /* The sound must be stopped... If it is waiting for replay, remove
350 * it from hash table to cancel. Otherwise playing_finished_cb will be
351 * called with an error. */
352 if (repeatable_sound->replay_timeout_id != 0)
354 g_hash_table_remove (repeating_sounds,
355 GINT_TO_POINTER (repeatable_sound->sound_id));
360 repeating_sounds_item_delete (gpointer data)
362 EmpathyRepeatableSound *repeatable_sound = data;
364 if (repeatable_sound->replay_timeout_id != 0)
365 g_source_remove (repeatable_sound->replay_timeout_id);
367 g_signal_handlers_disconnect_by_func (repeatable_sound->widget,
368 empathy_sound_widget_destroyed_cb, repeatable_sound);
370 g_slice_free (EmpathyRepeatableSound, repeatable_sound);
374 * empathy_sound_manager_start_playing:
375 * @self: a #EmpathySoundManager
376 * @widget: The #GtkWidget from which the sound is originating.
377 * @sound_id: The #EmpathySound to play.
378 * @timeout_before_replay: The amount of time, in milliseconds, between two
381 * Start playing a sound in loop. To stop the sound, call empathy_call_stop ()
382 * by passing it the same @sound_id. Note that if you start playing a sound
383 * multiple times, you'll have to call %empathy_sound_stop the same number of
386 * Return value: %TRUE if the sound has successfully started playing.
389 empathy_sound_manager_start_playing (EmpathySoundManager *self,
391 EmpathySound sound_id,
392 guint timeout_before_replay)
394 EmpathyRepeatableSound *repeatable_sound;
395 gboolean playing = FALSE;
397 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
398 g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
400 if (!empathy_sound_pref_is_enabled (sound_id))
403 if (repeating_sounds == NULL)
405 repeating_sounds = g_hash_table_new_full (g_direct_hash, g_direct_equal,
406 NULL, repeating_sounds_item_delete);
408 else if (g_hash_table_lookup (repeating_sounds,
409 GINT_TO_POINTER (sound_id)) != NULL)
411 /* The sound is already playing in loop. No need to continue. */
415 repeatable_sound = g_slice_new0 (EmpathyRepeatableSound);
416 repeatable_sound->widget = widget;
417 repeatable_sound->sound_id = sound_id;
418 repeatable_sound->play_interval = timeout_before_replay;
419 repeatable_sound->replay_timeout_id = 0;
421 g_hash_table_insert (repeating_sounds, GINT_TO_POINTER (sound_id),
424 g_signal_connect (G_OBJECT (widget), "destroy",
425 G_CALLBACK (empathy_sound_widget_destroyed_cb),
428 playing = empathy_sound_play_internal (widget, sound_id, playing_finished_cb,
432 g_hash_table_remove (repeating_sounds, GINT_TO_POINTER (sound_id));