]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-sound-manager.c
8d51f7391049431599f2b11bd85b25db288d7123
[empathy.git] / libempathy-gtk / empathy-sound-manager.c
1 /*
2  * empathy-sound-manager.c - Various sound related utility functions.
3  * Copyright (C) 2009 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <config.h>
21
22 #include "empathy-sound-manager.h"
23
24 #include <canberra-gtk.h>
25 #include <glib/gi18n-lib.h>
26 #include <gtk/gtk.h>
27
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>
32
33 typedef struct {
34   EmpathySound sound_id;
35   const char * event_ca_id;
36   const char * event_ca_description;
37   const char * key;
38 } EmpathySoundEntry;
39
40 typedef struct {
41   GtkWidget *widget;
42   gint sound_id;
43   guint play_interval;
44   guint replay_timeout_id;
45 } EmpathyRepeatableSound;
46
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 },
69 };
70
71 G_DEFINE_TYPE (EmpathySoundManager, empathy_sound_manager, G_TYPE_OBJECT)
72
73 struct _EmpathySoundManagerPrivate
74 {
75   gpointer unused;
76 };
77
78 static void
79 empathy_sound_manager_class_init (EmpathySoundManagerClass *cls)
80 {
81   g_type_class_add_private (cls, sizeof (EmpathySoundManagerPrivate));
82 }
83
84 static void
85 empathy_sound_manager_init (EmpathySoundManager *self)
86 {
87   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
88       EMPATHY_TYPE_SOUND_MANAGER, EmpathySoundManagerPrivate);
89 }
90
91 EmpathySoundManager *
92 empathy_sound_manager_dup_singleton (void)
93 {
94   static EmpathySoundManager *manager = NULL;
95
96   if (manager != NULL)
97       return g_object_ref (manager);
98
99   manager = g_object_new (EMPATHY_TYPE_SOUND_MANAGER, NULL);
100
101   g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
102   return manager;
103 }
104
105 /* An hash table containing currently repeating sounds. The format is the
106  * following:
107  * Key: An EmpathySound
108  * Value : The EmpathyRepeatableSound associated with that EmpathySound. */
109 static GHashTable *repeating_sounds;
110
111 static gboolean
112 empathy_sound_pref_is_enabled (EmpathySound sound_id)
113 {
114   EmpathySoundEntry *entry;
115   GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
116   gboolean res;
117
118   entry = &(sound_entries[sound_id]);
119   g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
120
121   if (entry->key == NULL)
122     {
123       res = TRUE;
124       goto finally;
125     }
126
127   res = g_settings_get_boolean (gsettings, EMPATHY_PREFS_SOUNDS_ENABLED);
128
129   if (!res)
130     goto finally;
131
132   if (!empathy_check_available_state ())
133     {
134       if (g_settings_get_boolean (gsettings,
135             EMPATHY_PREFS_SOUNDS_DISABLED_AWAY))
136         {
137           res = FALSE;
138           goto finally;
139         }
140     }
141
142   res = g_settings_get_boolean (gsettings, entry->key);
143
144 finally:
145   g_object_unref (gsettings);
146
147   return res;
148 }
149
150 /**
151  * empathy_sound_stop:
152  * @sound_id: The #EmpathySound to stop playing.
153  *
154  * Stop playing a sound. If it has been stated in loop with
155  * empathy_sound_start_playing(), it will also stop replaying.
156  */
157 void
158 empathy_sound_stop (EmpathySound sound_id)
159 {
160   EmpathySoundEntry *entry;
161
162   g_return_if_fail (sound_id < LAST_EMPATHY_SOUND);
163
164   entry = &(sound_entries[sound_id]);
165   g_return_if_fail (entry->sound_id == sound_id);
166
167   if (repeating_sounds != NULL)
168     {
169       EmpathyRepeatableSound *repeatable_sound;
170
171       repeatable_sound = g_hash_table_lookup (repeating_sounds,
172           GINT_TO_POINTER (sound_id));
173       if (repeatable_sound != NULL)
174         {
175           /* The sound must be stopped... If it is waiting for replay, remove
176            * it from hash table to cancel. Otherwise we'll cancel the sound
177            * being played. */
178           if (repeatable_sound->replay_timeout_id != 0)
179             {
180               g_hash_table_remove (repeating_sounds, GINT_TO_POINTER (sound_id));
181               return;
182             }
183         }
184     }
185
186   ca_context_cancel (ca_gtk_context_get (), entry->sound_id);
187 }
188
189 static gboolean
190 empathy_sound_play_internal (GtkWidget *widget, EmpathySound sound_id,
191   ca_finish_callback_t callback, gpointer user_data)
192 {
193   EmpathySoundEntry *entry;
194   ca_context *c;
195   ca_proplist *p = NULL;
196
197   entry = &(sound_entries[sound_id]);
198   g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
199
200   c = ca_gtk_context_get ();
201   ca_context_cancel (c, entry->sound_id);
202
203   DEBUG ("Play sound \"%s\" (%s)",
204          entry->event_ca_id,
205          entry->event_ca_description);
206
207   if (ca_proplist_create (&p) < 0)
208     goto failed;
209
210   if (ca_proplist_sets (p, CA_PROP_EVENT_ID, entry->event_ca_id) < 0)
211     goto failed;
212
213   if (ca_proplist_sets (p, CA_PROP_EVENT_DESCRIPTION,
214           gettext (entry->event_ca_description)) < 0)
215     goto failed;
216
217   if (ca_gtk_proplist_set_for_widget (p, widget) < 0)
218     goto failed;
219
220   ca_context_play_full (ca_gtk_context_get (), entry->sound_id, p, callback,
221       user_data);
222
223   ca_proplist_destroy (p);
224
225   return TRUE;
226
227 failed:
228   if (p != NULL)
229     ca_proplist_destroy (p);
230
231   return FALSE;
232 }
233
234 /**
235  * empathy_sound_play_full:
236  * @widget: The #GtkWidget from which the sound is originating.
237  * @sound_id: The #EmpathySound to play.
238  * @callback: The #ca_finish_callback_t function that will be called when the
239  *            sound  has stopped playing.
240  * @user_data: user data to pass to the function.
241  *
242  * Plays a sound.
243  *
244  * Returns %TRUE if the sound has successfully started playing, otherwise
245  * returning %FALSE and @callback won't be called.
246  *
247  * This function returns %FALSE if the sound is already playing in loop using
248  * %empathy_sound_start_playing.
249  *
250  * This function returns %FALSE if the sound is disabled in empathy preferences.
251  *
252  * Return value: %TRUE if the sound has successfully started playing, %FALSE
253  *               otherwise.
254  */
255 gboolean
256 empathy_sound_play_full (GtkWidget *widget, EmpathySound sound_id,
257   ca_finish_callback_t callback, gpointer user_data)
258 {
259   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
260   g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
261
262   if (!empathy_sound_pref_is_enabled (sound_id))
263     return FALSE;
264
265   /* The sound might already be playing repeatedly. If it's the case, we
266    * immediadely return since there's no need to make it play again */
267   if (repeating_sounds != NULL &&
268       g_hash_table_lookup (repeating_sounds, GINT_TO_POINTER (sound_id)) != NULL)
269     return FALSE;
270
271   return empathy_sound_play_internal (widget, sound_id, callback, user_data);
272 }
273
274 /**
275  * empathy_sound_play:
276  * @widget: The #GtkWidget from which the sound is originating.
277  * @sound_id: The #EmpathySound to play.
278  *
279  * Plays a sound. See %empathy_sound_play_full for details.'
280  *
281  * Return value: %TRUE if the sound has successfully started playing, %FALSE
282  *               otherwise.
283  */
284 gboolean
285 empathy_sound_play (GtkWidget *widget, EmpathySound sound_id)
286 {
287   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
288   g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
289
290   return empathy_sound_play_full (widget, sound_id, NULL, NULL);
291 }
292
293 static void playing_finished_cb (ca_context *c, guint id, int error_code,
294   gpointer user_data);
295
296 static gboolean
297 playing_timeout_cb (gpointer data)
298 {
299   EmpathyRepeatableSound *repeatable_sound = data;
300   gboolean playing;
301
302   repeatable_sound->replay_timeout_id = 0;
303
304   playing = empathy_sound_play_internal (repeatable_sound->widget,
305       repeatable_sound->sound_id, playing_finished_cb, data);
306
307   if (!playing)
308     {
309       DEBUG ("Failed to replay sound, stop repeating");
310       g_hash_table_remove (repeating_sounds,
311           GINT_TO_POINTER (repeatable_sound->sound_id));
312     }
313
314   return FALSE;
315 }
316
317 static void
318 playing_finished_cb (ca_context *c, guint id, int error_code,
319   gpointer user_data)
320 {
321   EmpathyRepeatableSound *repeatable_sound = user_data;
322
323   if (error_code != CA_SUCCESS)
324     {
325       DEBUG ("Error: %s", ca_strerror (error_code));
326       g_hash_table_remove (repeating_sounds,
327           GINT_TO_POINTER (repeatable_sound->sound_id));
328       return;
329     }
330
331   repeatable_sound->replay_timeout_id = g_timeout_add (
332       repeatable_sound->play_interval, playing_timeout_cb, user_data);
333 }
334
335 static void
336 empathy_sound_widget_destroyed_cb (GtkWidget *widget, gpointer user_data)
337 {
338   EmpathyRepeatableSound *repeatable_sound = user_data;
339
340   /* The sound must be stopped... If it is waiting for replay, remove
341    * it from hash table to cancel. Otherwise playing_finished_cb will be
342    * called with an error. */
343   if (repeatable_sound->replay_timeout_id != 0)
344     {
345       g_hash_table_remove (repeating_sounds,
346           GINT_TO_POINTER (repeatable_sound->sound_id));
347     }
348 }
349
350 static void
351 repeating_sounds_item_delete (gpointer data)
352 {
353   EmpathyRepeatableSound *repeatable_sound = data;
354
355   if (repeatable_sound->replay_timeout_id != 0)
356     g_source_remove (repeatable_sound->replay_timeout_id);
357
358   g_signal_handlers_disconnect_by_func (repeatable_sound->widget,
359       empathy_sound_widget_destroyed_cb, repeatable_sound);
360
361   g_slice_free (EmpathyRepeatableSound, repeatable_sound);
362 }
363
364 /**
365  * empathy_sound_start_playing:
366  * @widget: The #GtkWidget from which the sound is originating.
367  * @sound_id: The #EmpathySound to play.
368  * @timeout_before_replay: The amount of time, in milliseconds, between two
369  *                         consecutive play.
370  *
371  * Start playing a sound in loop. To stop the sound, call empathy_call_stop ()
372  * by passing it the same @sound_id. Note that if you start playing a sound
373  * multiple times, you'll have to call %empathy_sound_stop the same number of
374  * times.
375  *
376  * Return value: %TRUE if the sound has successfully started playing.
377  */
378 gboolean
379 empathy_sound_start_playing (GtkWidget *widget, EmpathySound sound_id,
380     guint timeout_before_replay)
381 {
382   EmpathyRepeatableSound *repeatable_sound;
383   gboolean playing = FALSE;
384
385   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
386   g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
387
388   if (!empathy_sound_pref_is_enabled (sound_id))
389     return FALSE;
390
391   if (repeating_sounds == NULL)
392     {
393       repeating_sounds = g_hash_table_new_full (g_direct_hash, g_direct_equal,
394           NULL, repeating_sounds_item_delete);
395     }
396   else if (g_hash_table_lookup (repeating_sounds,
397                GINT_TO_POINTER (sound_id)) != NULL)
398     {
399       /* The sound is already playing in loop. No need to continue. */
400       return FALSE;
401     }
402
403   repeatable_sound = g_slice_new0 (EmpathyRepeatableSound);
404   repeatable_sound->widget = widget;
405   repeatable_sound->sound_id = sound_id;
406   repeatable_sound->play_interval = timeout_before_replay;
407   repeatable_sound->replay_timeout_id = 0;
408
409   g_hash_table_insert (repeating_sounds, GINT_TO_POINTER (sound_id),
410       repeatable_sound);
411
412   g_signal_connect (G_OBJECT (widget), "destroy",
413       G_CALLBACK (empathy_sound_widget_destroyed_cb),
414       repeatable_sound);
415
416   playing = empathy_sound_play_internal (widget, sound_id, playing_finished_cb,
417         repeatable_sound);
418
419   if (!playing)
420       g_hash_table_remove (repeating_sounds, GINT_TO_POINTER (sound_id));
421
422   return playing;
423 }