2009-03-25 Alejandro Pinheiro Iglesias <apinheiro@igalia.com>
[hildon] / src / hildon-sound.c
1 /*
2  * This file is a part of libhildon
3  *
4  * Copyright (C) 2005-2008 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Kimmo Hämäläinen <kimmo.hamalainen@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-sound
27  * @short_description: libcanberra-based utility function for playing a sound.
28  * 
29  */
30
31 #include <unistd.h>
32 #include <gconf/gconf-client.h>
33 #include <canberra.h>
34 #include <canberra-gtk.h>
35
36 #include "hildon-sound.h"
37
38 #define ALARM_GCONF_PATH "/apps/osso/sound/system_alert_volume"
39
40 /**
41  * hildon_play_system_sound:
42  * @sample: sound file to play
43  * 
44  * Plays the given sample using libcanberra.
45  * Volume level is received from gconf. 
46  */
47 void 
48 hildon_play_system_sound(const gchar *sample)
49 {
50     float volume = 0;
51     int ret;
52     ca_context *ca_con = NULL;
53     ca_proplist *pl = NULL;
54
55 #if 0 /* FIXME: Check volume handling. Would be great not to use Gconf... */
56     GConfClient *client;
57     GConfValue *value;
58     gint gconf_vol;
59
60     /*
61      * The volume is from -0dB to -6dB,
62        The full volume is marked as 2 in gconf */
63     client = gconf_client_get_default ();
64     value = gconf_client_get (client, ALARM_GCONF_PATH, NULL);
65
66     /* We want error cases to match full volume, not silence, so
67        we do not want to use gconf_client_get_int */
68     if (!value || value->type != GCONF_VALUE_INT)
69         gconf_vol = 2;
70     else
71         gconf_vol = gconf_value_get_int(value);
72
73     if (value)
74         gconf_value_free(value);
75     g_object_unref (client);
76
77     volume = ((1.0 - (float)gconf_vol / 2.0)) * (-6.0);
78 #endif
79
80     ca_con = ca_gtk_context_get ();
81
82     if (ca_con == NULL) {
83       g_warning ("ca_gtk_context_get doesn't return a proper context \n");
84       return;
85     }
86
87     ca_proplist_create(&pl);
88     ca_proplist_sets(pl, CA_PROP_MEDIA_FILENAME, sample);
89     ca_proplist_setf(pl, CA_PROP_CANBERRA_VOLUME, "%f", volume);
90
91     ret = ca_context_play_full(ca_con, 0, pl, NULL, NULL);
92     g_debug("ca_context_play_full (vol %f): %s\n", volume, ca_strerror(ret));
93
94     ca_proplist_destroy(pl);
95 }