/*
* empathy-sound-manager.c - Various sound related utility functions.
- * Copyright (C) 2009 Collabora Ltd.
+ * Copyright (C) 2009-2010 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <config.h>
-
+#include "config.h"
#include "empathy-sound-manager.h"
-#include <canberra-gtk.h>
#include <glib/gi18n-lib.h>
-#include <gtk/gtk.h>
+
+#include "empathy-gsettings.h"
+#include "empathy-presence-manager.h"
+#include "empathy-utils.h"
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include <libempathy/empathy-debug.h>
-#include <libempathy/empathy-gsettings.h>
-#include <libempathy/empathy-utils.h>
+#include "empathy-debug.h"
typedef struct {
EmpathySound sound_id;
struct _EmpathySoundManagerPrivate
{
- /* An hash table containing currently repeating sounds. The format is the
+ /* A hash table containing currently repeating sounds. The format is the
* following:
* Key: An EmpathySound
* Value : The EmpathyRepeatableSound associated with that EmpathySound. */
GHashTable *repeating_sounds;
+ GSettings *gsettings_sound;
};
static void
EmpathySoundManager *self = (EmpathySoundManager *) object;
tp_clear_pointer (&self->priv->repeating_sounds, g_hash_table_unref);
+ tp_clear_object (&self->priv->gsettings_sound);
G_OBJECT_CLASS (empathy_sound_manager_parent_class)->dispose (object);
}
if (repeatable_sound->replay_timeout_id != 0)
g_source_remove (repeatable_sound->replay_timeout_id);
- g_signal_handlers_disconnect_by_func (repeatable_sound->widget,
- empathy_sound_widget_destroyed_cb, repeatable_sound);
+ if (repeatable_sound->widget != NULL)
+ {
+ g_signal_handlers_disconnect_by_func (repeatable_sound->widget,
+ empathy_sound_widget_destroyed_cb, repeatable_sound);
+ }
g_object_unref (repeatable_sound->self);
self->priv->repeating_sounds = g_hash_table_new_full (NULL, NULL,
NULL, repeating_sounds_item_delete);
+
+ self->priv->gsettings_sound = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
}
EmpathySoundManager *
{
static EmpathySoundManager *manager = NULL;
- if (manager != NULL)
+ if (G_LIKELY (manager != NULL))
return g_object_ref (manager);
manager = g_object_new (EMPATHY_TYPE_SOUND_MANAGER, NULL);
}
static gboolean
-empathy_sound_pref_is_enabled (EmpathySound sound_id)
+empathy_check_available_state (void)
+{
+ TpConnectionPresenceType most_available_requested_presence;
+ TpAccountManager *am;
+ GList *accounts;
+
+ /* We cannot use tp_account_manager_get_most_available_presence() or
+ * empathy_presence_manager_get_state() because it is the requested presence
+ * that matters, not the current presence.
+ * See https://bugzilla.gnome.org/show_bug.cgi?id=704454 */
+ most_available_requested_presence = TP_CONNECTION_PRESENCE_TYPE_UNSET;
+ am = tp_account_manager_dup ();
+ accounts = tp_account_manager_dup_valid_accounts (am);
+ while (accounts != NULL)
+ {
+ TpAccount *account = accounts->data;
+ TpConnectionPresenceType requested_presence;
+
+ requested_presence = tp_account_get_requested_presence (account,
+ NULL, NULL);
+
+ if (tp_connection_presence_type_cmp_availability (requested_presence,
+ most_available_requested_presence) > 0)
+ most_available_requested_presence = requested_presence;
+
+ g_object_unref (account);
+ accounts = g_list_delete_link (accounts, accounts);
+ }
+
+ g_object_unref (am);
+
+ if (most_available_requested_presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
+ most_available_requested_presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean
+empathy_sound_pref_is_enabled (EmpathySoundManager *self,
+ EmpathySound sound_id)
{
EmpathySoundEntry *entry;
- GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
- gboolean res;
entry = &(sound_entries[sound_id]);
g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
if (entry->key == NULL)
- {
- res = TRUE;
- goto finally;
- }
+ return TRUE;
- res = g_settings_get_boolean (gsettings, EMPATHY_PREFS_SOUNDS_ENABLED);
-
- if (!res)
- goto finally;
+ if (! g_settings_get_boolean (self->priv->gsettings_sound,
+ EMPATHY_PREFS_SOUNDS_ENABLED))
+ return FALSE;
if (!empathy_check_available_state ())
{
- if (g_settings_get_boolean (gsettings,
+ if (g_settings_get_boolean (self->priv->gsettings_sound,
EMPATHY_PREFS_SOUNDS_DISABLED_AWAY))
- {
- res = FALSE;
- goto finally;
- }
+ return FALSE;
}
- res = g_settings_get_boolean (gsettings, entry->key);
-
-finally:
- g_object_unref (gsettings);
-
- return res;
+ return g_settings_get_boolean (self->priv->gsettings_sound, entry->key);
}
/**
gettext (entry->event_ca_description)) < 0)
goto failed;
- if (ca_gtk_proplist_set_for_widget (p, widget) < 0)
- goto failed;
+ if (widget != NULL)
+ {
+ if (ca_gtk_proplist_set_for_widget (p, widget) < 0)
+ goto failed;
+ }
ca_context_play_full (ca_gtk_context_get (), entry->sound_id, p, callback,
user_data);
ca_finish_callback_t callback,
gpointer user_data)
{
- g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
+ g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), FALSE);
g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
- if (!empathy_sound_pref_is_enabled (sound_id))
+ if (!empathy_sound_pref_is_enabled (self, sound_id))
return FALSE;
/* The sound might already be playing repeatedly. If it's the case, we
GtkWidget *widget,
EmpathySound sound_id)
{
- g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
+ g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), FALSE);
g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
return empathy_sound_manager_play_full (self, widget, sound_id, NULL, NULL);
EmpathyRepeatableSound *repeatable_sound;
gboolean playing = FALSE;
- g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
+ g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), FALSE);
g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
- if (!empathy_sound_pref_is_enabled (sound_id))
+ if (!empathy_sound_pref_is_enabled (self, sound_id))
return FALSE;
if (g_hash_table_lookup (self->priv->repeating_sounds,
g_hash_table_insert (self->priv->repeating_sounds, GINT_TO_POINTER (sound_id),
repeatable_sound);
- g_signal_connect (G_OBJECT (widget), "destroy",
- G_CALLBACK (empathy_sound_widget_destroyed_cb),
- repeatable_sound);
+ if (widget != NULL)
+ {
+ g_signal_connect (G_OBJECT (widget), "destroy",
+ G_CALLBACK (empathy_sound_widget_destroyed_cb),
+ repeatable_sound);
+ }
playing = empathy_sound_play_internal (widget, sound_id, playing_finished_cb,
repeatable_sound);