]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-sound-manager.c
factor out start_gnome_contacts()
[empathy.git] / libempathy-gtk / empathy-sound-manager.c
1 /*
2  * empathy-sound-manager.c - Various sound related utility functions.
3  * Copyright (C) 2009-2010 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   EmpathySoundManager *self;
46 } EmpathyRepeatableSound;
47
48 /* NOTE: these entries MUST be in the same order than EmpathySound enum */
49 static EmpathySoundEntry sound_entries[LAST_EMPATHY_SOUND] = {
50   { EMPATHY_SOUND_MESSAGE_INCOMING, "message-new-instant",
51     N_("Received an instant message"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE } ,
52   { EMPATHY_SOUND_MESSAGE_OUTGOING, "message-sent-instant",
53     N_("Sent an instant message"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE } ,
54   { EMPATHY_SOUND_CONVERSATION_NEW, "message-new-instant",
55     N_("Incoming chat request"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
56   { EMPATHY_SOUND_CONTACT_CONNECTED, "service-login",
57     N_("Contact connected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
58   { EMPATHY_SOUND_CONTACT_DISCONNECTED, "service-logout",
59     N_("Contact disconnected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
60   { EMPATHY_SOUND_ACCOUNT_CONNECTED, "service-login",
61     N_("Connected to server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
62   { EMPATHY_SOUND_ACCOUNT_DISCONNECTED, "service-logout",
63     N_("Disconnected from server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT },
64   { EMPATHY_SOUND_PHONE_INCOMING, "phone-incoming-call",
65     N_("Incoming voice call"), NULL },
66   { EMPATHY_SOUND_PHONE_OUTGOING, "phone-outgoing-calling",
67     N_("Outgoing voice call"), NULL },
68   { EMPATHY_SOUND_PHONE_HANGUP, "phone-hangup",
69     N_("Voice call ended"), NULL },
70 };
71
72 G_DEFINE_TYPE (EmpathySoundManager, empathy_sound_manager, G_TYPE_OBJECT)
73
74 struct _EmpathySoundManagerPrivate
75 {
76   /* A hash table containing currently repeating sounds. The format is the
77    * following:
78    * Key: An EmpathySound
79    * Value : The EmpathyRepeatableSound associated with that EmpathySound. */
80   GHashTable *repeating_sounds;
81   GSettings *gsettings_sound;
82 };
83
84 static void
85 empathy_sound_manager_dispose (GObject *object)
86 {
87   EmpathySoundManager *self = (EmpathySoundManager *) object;
88
89   tp_clear_pointer (&self->priv->repeating_sounds, g_hash_table_unref);
90   tp_clear_object (&self->priv->gsettings_sound);
91
92   G_OBJECT_CLASS (empathy_sound_manager_parent_class)->dispose (object);
93 }
94
95 static void
96 empathy_sound_manager_class_init (EmpathySoundManagerClass *cls)
97 {
98   GObjectClass *object_class = G_OBJECT_CLASS (cls);
99
100   object_class->dispose = empathy_sound_manager_dispose;
101
102   g_type_class_add_private (cls, sizeof (EmpathySoundManagerPrivate));
103 }
104
105 static void
106 empathy_sound_widget_destroyed_cb (GtkWidget *widget,
107     gpointer user_data)
108 {
109   EmpathyRepeatableSound *repeatable_sound = user_data;
110
111   /* The sound must be stopped... If it is waiting for replay, remove
112    * it from hash table to cancel. Otherwise playing_finished_cb will be
113    * called with an error. */
114   if (repeatable_sound->replay_timeout_id != 0)
115     {
116       g_hash_table_remove (repeatable_sound->self->priv->repeating_sounds,
117           GINT_TO_POINTER (repeatable_sound->sound_id));
118     }
119 }
120
121 static void
122 repeating_sounds_item_delete (gpointer data)
123 {
124   EmpathyRepeatableSound *repeatable_sound = data;
125
126   if (repeatable_sound->replay_timeout_id != 0)
127     g_source_remove (repeatable_sound->replay_timeout_id);
128
129   if (repeatable_sound->widget != NULL)
130     {
131       g_signal_handlers_disconnect_by_func (repeatable_sound->widget,
132           empathy_sound_widget_destroyed_cb, repeatable_sound);
133     }
134
135   g_object_unref (repeatable_sound->self);
136
137   g_slice_free (EmpathyRepeatableSound, repeatable_sound);
138 }
139
140 static void
141 empathy_sound_manager_init (EmpathySoundManager *self)
142 {
143   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
144       EMPATHY_TYPE_SOUND_MANAGER, EmpathySoundManagerPrivate);
145
146   self->priv->repeating_sounds = g_hash_table_new_full (NULL, NULL,
147       NULL, repeating_sounds_item_delete);
148
149   self->priv->gsettings_sound = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
150 }
151
152 EmpathySoundManager *
153 empathy_sound_manager_dup_singleton (void)
154 {
155   static EmpathySoundManager *manager = NULL;
156
157   if (G_LIKELY (manager != NULL))
158       return g_object_ref (manager);
159
160   manager = g_object_new (EMPATHY_TYPE_SOUND_MANAGER, NULL);
161
162   g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
163   return manager;
164 }
165
166 static gboolean
167 empathy_sound_pref_is_enabled (EmpathySoundManager *self,
168     EmpathySound sound_id)
169 {
170   EmpathySoundEntry *entry;
171
172   entry = &(sound_entries[sound_id]);
173   g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
174
175   if (entry->key == NULL)
176     return TRUE;
177
178   if (! g_settings_get_boolean (self->priv->gsettings_sound,
179       EMPATHY_PREFS_SOUNDS_ENABLED))
180     return FALSE;
181
182   if (!empathy_check_available_state ())
183     {
184       if (g_settings_get_boolean (self->priv->gsettings_sound,
185             EMPATHY_PREFS_SOUNDS_DISABLED_AWAY))
186         return FALSE;
187     }
188
189   return g_settings_get_boolean (self->priv->gsettings_sound, entry->key);
190 }
191
192 /**
193  * empathy_sound_manager_stop:
194  * @self: a #EmpathySoundManager
195  * @sound_id: The #EmpathySound to stop playing.
196  *
197  * Stop playing a sound. If it has been stated in loop with
198  * empathy_sound_start_playing(), it will also stop replaying.
199  */
200 void
201 empathy_sound_manager_stop (EmpathySoundManager *self,
202     EmpathySound sound_id)
203 {
204   EmpathySoundEntry *entry;
205   EmpathyRepeatableSound *repeatable_sound;
206
207   g_return_if_fail (sound_id < LAST_EMPATHY_SOUND);
208
209   entry = &(sound_entries[sound_id]);
210   g_return_if_fail (entry->sound_id == sound_id);
211
212   repeatable_sound = g_hash_table_lookup (self->priv->repeating_sounds,
213       GINT_TO_POINTER (sound_id));
214   if (repeatable_sound != NULL)
215     {
216       /* The sound must be stopped... If it is waiting for replay, remove
217        * it from hash table to cancel. Otherwise we'll cancel the sound
218        * being played. */
219       if (repeatable_sound->replay_timeout_id != 0)
220         {
221           g_hash_table_remove (self->priv->repeating_sounds,
222               GINT_TO_POINTER (sound_id));
223           return;
224         }
225     }
226
227   ca_context_cancel (ca_gtk_context_get (), entry->sound_id);
228 }
229
230 static gboolean
231 empathy_sound_play_internal (GtkWidget *widget, EmpathySound sound_id,
232   ca_finish_callback_t callback, gpointer user_data)
233 {
234   EmpathySoundEntry *entry;
235   ca_context *c;
236   ca_proplist *p = NULL;
237
238   entry = &(sound_entries[sound_id]);
239   g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
240
241   c = ca_gtk_context_get ();
242   ca_context_cancel (c, entry->sound_id);
243
244   DEBUG ("Play sound \"%s\" (%s)",
245          entry->event_ca_id,
246          entry->event_ca_description);
247
248   if (ca_proplist_create (&p) < 0)
249     goto failed;
250
251   if (ca_proplist_sets (p, CA_PROP_EVENT_ID, entry->event_ca_id) < 0)
252     goto failed;
253
254   if (ca_proplist_sets (p, CA_PROP_EVENT_DESCRIPTION,
255           gettext (entry->event_ca_description)) < 0)
256     goto failed;
257
258   if (widget != NULL)
259     {
260       if (ca_gtk_proplist_set_for_widget (p, widget) < 0)
261         goto failed;
262     }
263
264   ca_context_play_full (ca_gtk_context_get (), entry->sound_id, p, callback,
265       user_data);
266
267   ca_proplist_destroy (p);
268
269   return TRUE;
270
271 failed:
272   if (p != NULL)
273     ca_proplist_destroy (p);
274
275   return FALSE;
276 }
277
278 /**
279  * empathy_sound_manager_play_full:
280  * @self: a #EmpathySoundManager
281  * @widget: The #GtkWidget from which the sound is originating.
282  * @sound_id: The #EmpathySound to play.
283  * @callback: The #ca_finish_callback_t function that will be called when the
284  *            sound  has stopped playing.
285  * @user_data: user data to pass to the function.
286  *
287  * Plays a sound.
288  *
289  * Returns %TRUE if the sound has successfully started playing, otherwise
290  * returning %FALSE and @callback won't be called.
291  *
292  * This function returns %FALSE if the sound is already playing in loop using
293  * %empathy_sound_start_playing.
294  *
295  * This function returns %FALSE if the sound is disabled in empathy preferences.
296  *
297  * Return value: %TRUE if the sound has successfully started playing, %FALSE
298  *               otherwise.
299  */
300 gboolean
301 empathy_sound_manager_play_full (EmpathySoundManager *self,
302     GtkWidget *widget,
303     EmpathySound sound_id,
304     ca_finish_callback_t callback,
305     gpointer user_data)
306 {
307   g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), FALSE);
308   g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
309
310   if (!empathy_sound_pref_is_enabled (self, sound_id))
311     return FALSE;
312
313   /* The sound might already be playing repeatedly. If it's the case, we
314    * immediadely return since there's no need to make it play again */
315   if (g_hash_table_lookup (self->priv->repeating_sounds,
316         GINT_TO_POINTER (sound_id)) != NULL)
317     return FALSE;
318
319   return empathy_sound_play_internal (widget, sound_id, callback, user_data);
320 }
321
322 /**
323  * empathy_sound_manager_play:
324  * @self: a #EmpathySoundManager
325  * @widget: The #GtkWidget from which the sound is originating.
326  * @sound_id: The #EmpathySound to play.
327  *
328  * Plays a sound. See %empathy_sound_manager_play_full for details.'
329  *
330  * Return value: %TRUE if the sound has successfully started playing, %FALSE
331  *               otherwise.
332  */
333 gboolean
334 empathy_sound_manager_play (EmpathySoundManager *self,
335     GtkWidget *widget,
336     EmpathySound sound_id)
337 {
338   g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), FALSE);
339   g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
340
341   return empathy_sound_manager_play_full (self, widget, sound_id, NULL, NULL);
342 }
343
344 static void playing_finished_cb (ca_context *c, guint id, int error_code,
345   gpointer user_data);
346
347 static gboolean
348 playing_timeout_cb (gpointer data)
349 {
350   EmpathyRepeatableSound *repeatable_sound = data;
351   gboolean playing;
352
353   repeatable_sound->replay_timeout_id = 0;
354
355   playing = empathy_sound_play_internal (repeatable_sound->widget,
356       repeatable_sound->sound_id, playing_finished_cb, data);
357
358   if (!playing)
359     {
360       DEBUG ("Failed to replay sound, stop repeating");
361       g_hash_table_remove (repeatable_sound->self->priv->repeating_sounds,
362           GINT_TO_POINTER (repeatable_sound->sound_id));
363     }
364
365   return FALSE;
366 }
367
368 static void
369 playing_finished_cb (ca_context *c, guint id, int error_code,
370   gpointer user_data)
371 {
372   EmpathyRepeatableSound *repeatable_sound = user_data;
373
374   if (error_code != CA_SUCCESS)
375     {
376       DEBUG ("Error: %s", ca_strerror (error_code));
377       g_hash_table_remove (repeatable_sound->self->priv->repeating_sounds,
378           GINT_TO_POINTER (repeatable_sound->sound_id));
379       return;
380     }
381
382   repeatable_sound->replay_timeout_id = g_timeout_add (
383       repeatable_sound->play_interval, playing_timeout_cb, user_data);
384 }
385
386 /**
387  * empathy_sound_manager_start_playing:
388  * @self: a #EmpathySoundManager
389  * @widget: The #GtkWidget from which the sound is originating.
390  * @sound_id: The #EmpathySound to play.
391  * @timeout_before_replay: The amount of time, in milliseconds, between two
392  *                         consecutive play.
393  *
394  * Start playing a sound in loop. To stop the sound, call empathy_call_stop ()
395  * by passing it the same @sound_id. Note that if you start playing a sound
396  * multiple times, you'll have to call %empathy_sound_stop the same number of
397  * times.
398  *
399  * Return value: %TRUE if the sound has successfully started playing.
400  */
401 gboolean
402 empathy_sound_manager_start_playing (EmpathySoundManager *self,
403     GtkWidget *widget,
404     EmpathySound sound_id,
405     guint timeout_before_replay)
406 {
407   EmpathyRepeatableSound *repeatable_sound;
408   gboolean playing = FALSE;
409
410   g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), FALSE);
411   g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
412
413   if (!empathy_sound_pref_is_enabled (self, sound_id))
414     return FALSE;
415
416   if (g_hash_table_lookup (self->priv->repeating_sounds,
417                GINT_TO_POINTER (sound_id)) != NULL)
418     {
419       /* The sound is already playing in loop. No need to continue. */
420       return FALSE;
421     }
422
423   repeatable_sound = g_slice_new0 (EmpathyRepeatableSound);
424   repeatable_sound->widget = widget;
425   repeatable_sound->sound_id = sound_id;
426   repeatable_sound->play_interval = timeout_before_replay;
427   repeatable_sound->replay_timeout_id = 0;
428   repeatable_sound->self = g_object_ref (self);
429
430   g_hash_table_insert (self->priv->repeating_sounds, GINT_TO_POINTER (sound_id),
431       repeatable_sound);
432
433   if (widget != NULL)
434     {
435       g_signal_connect (G_OBJECT (widget), "destroy",
436           G_CALLBACK (empathy_sound_widget_destroyed_cb),
437           repeatable_sound);
438     }
439
440   playing = empathy_sound_play_internal (widget, sound_id, playing_finished_cb,
441         repeatable_sound);
442
443   if (!playing)
444       g_hash_table_remove (self->priv->repeating_sounds,
445           GINT_TO_POINTER (sound_id));
446
447   return playing;
448 }