From c9f2cd4f71e619f5a418090981afebbef43226c1 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 25 Jan 2011 11:13:51 +0100 Subject: rename EmpathyIdle to EmpathyPresenceManager (#640532) It's doing more than idle management now. --- libempathy/Makefile.am | 4 +- libempathy/empathy-account-settings.c | 11 +- libempathy/empathy-idle.c | 732 --------------------------------- libempathy/empathy-idle.h | 74 ---- libempathy/empathy-presence-manager.c | 733 ++++++++++++++++++++++++++++++++++ libempathy/empathy-presence-manager.h | 74 ++++ libempathy/empathy-utils.c | 10 +- 7 files changed, 820 insertions(+), 818 deletions(-) delete mode 100644 libempathy/empathy-idle.c delete mode 100644 libempathy/empathy-idle.h create mode 100644 libempathy/empathy-presence-manager.c create mode 100644 libempathy/empathy-presence-manager.h (limited to 'libempathy') diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am index ef85581b1..25153eab8 100644 --- a/libempathy/Makefile.am +++ b/libempathy/Makefile.am @@ -43,7 +43,7 @@ libempathy_headers = \ empathy-ft-factory.h \ empathy-ft-handler.h \ empathy-gsettings.h \ - empathy-idle.h \ + empathy-presence-manager.h \ empathy-individual-manager.h \ empathy-irc-network-manager.h \ empathy-irc-network.h \ @@ -84,7 +84,7 @@ libempathy_la_SOURCES = \ empathy-dispatcher.c \ empathy-ft-factory.c \ empathy-ft-handler.c \ - empathy-idle.c \ + empathy-presence-manager.c \ empathy-individual-manager.c \ empathy-irc-network-manager.c \ empathy-irc-network.c \ diff --git a/libempathy/empathy-account-settings.c b/libempathy/empathy-account-settings.c index a90aaa8f4..8c673dfcf 100644 --- a/libempathy/empathy-account-settings.c +++ b/libempathy/empathy-account-settings.c @@ -31,7 +31,7 @@ #include "empathy-connection-managers.h" #include "empathy-keyring.h" #include "empathy-utils.h" -#include "empathy-idle.h" +#include "empathy-presence-manager.h" #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT #include @@ -1499,13 +1499,14 @@ empathy_account_settings_do_create_account (EmpathyAccountSettings *settings) TpConnectionPresenceType type; gchar *status; gchar *message; - EmpathyIdle *idle; + EmpathyPresenceManager *presence_mgr; properties = tp_asv_new (NULL, NULL); - idle = empathy_idle_dup_singleton (); - type = empathy_idle_get_requested_presence (idle, &status, &message); - g_object_unref (idle); + presence_mgr = empathy_presence_manager_dup_singleton (); + type = empathy_presence_manager_get_requested_presence (presence_mgr, &status, + &message); + g_object_unref (presence_mgr); if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET) { diff --git a/libempathy/empathy-idle.c b/libempathy/empathy-idle.c deleted file mode 100644 index 12686daa4..000000000 --- a/libempathy/empathy-idle.c +++ /dev/null @@ -1,732 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Copyright (C) 2007-2008 Collabora Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * Authors: Xavier Claessens - */ - -#include - -#include - -#include -#include - -#include -#include -#include - -#include "empathy-idle.h" -#include "empathy-utils.h" -#include "empathy-connectivity.h" - -#define DEBUG_FLAG EMPATHY_DEBUG_OTHER -#include "empathy-debug.h" - -/* Number of seconds before entering extended autoaway. */ -#define EXT_AWAY_TIME (30*60) - -/* Number of seconds to consider an account in the "just connected" state - * for. */ -#define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10 - -#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle) -typedef struct { - DBusGProxy *gs_proxy; - EmpathyConnectivity *connectivity; - gulong state_change_signal_id; - - gboolean ready; - - TpConnectionPresenceType state; - gchar *status; - gboolean auto_away; - - TpConnectionPresenceType away_saved_state; - TpConnectionPresenceType saved_state; - gchar *saved_status; - - gboolean is_idle; - guint ext_away_timeout; - - TpAccountManager *manager; - gulong idle_presence_changed_id; - - /* pointer to a TpAccount --> glong of time of connection */ - GHashTable *connect_times; - - TpConnectionPresenceType requested_presence_type; - gchar *requested_status_message; - -} EmpathyIdlePriv; - -typedef enum { - SESSION_STATUS_AVAILABLE, - SESSION_STATUS_INVISIBLE, - SESSION_STATUS_BUSY, - SESSION_STATUS_IDLE, - SESSION_STATUS_UNKNOWN -} SessionStatus; - -enum { - PROP_0, - PROP_STATE, - PROP_STATUS, - PROP_AUTO_AWAY -}; - -G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT); - -static EmpathyIdle * idle_singleton = NULL; - -static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = { - NULL, - "offline", - "available", - "away", - "xa", - "hidden", - "busy", - NULL, - NULL, -}; - -static void -idle_presence_changed_cb (TpAccountManager *manager, - TpConnectionPresenceType state, - gchar *status, - gchar *status_message, - EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) - /* Assume our presence is offline if MC reports UNSET */ - state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE; - - DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state, - status_message); - - g_free (priv->status); - priv->state = state; - if (EMP_STR_EMPTY (status_message)) - priv->status = NULL; - else - priv->status = g_strdup (status_message); - - g_object_notify (G_OBJECT (idle), "state"); - g_object_notify (G_OBJECT (idle), "status"); -} - -static gboolean -idle_ext_away_cb (EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - DEBUG ("Going to extended autoaway"); - empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY); - priv->ext_away_timeout = 0; - - return FALSE; -} - -static void -idle_ext_away_stop (EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - if (priv->ext_away_timeout) { - g_source_remove (priv->ext_away_timeout); - priv->ext_away_timeout = 0; - } -} - -static void -idle_ext_away_start (EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - if (priv->ext_away_timeout != 0) { - return; - } - priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME, - (GSourceFunc) idle_ext_away_cb, - idle); -} - -static void -idle_session_status_changed_cb (DBusGProxy *gs_proxy, - SessionStatus status, - EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - gboolean is_idle; - - priv = GET_PRIV (idle); - - is_idle = (status == SESSION_STATUS_IDLE); - - DEBUG ("Session idle state changed, %s -> %s", - priv->is_idle ? "yes" : "no", - is_idle ? "yes" : "no"); - - if (!priv->auto_away || - (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET && - (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE || - priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) { - /* We don't want to go auto away OR we explicitely asked to be - * offline, nothing to do here */ - priv->is_idle = is_idle; - return; - } - - if (is_idle && !priv->is_idle) { - TpConnectionPresenceType new_state; - /* We are now idle */ - - idle_ext_away_start (idle); - - if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { - /* We are disconnected, when coming back from away - * we want to restore the presence before the - * disconnection. */ - priv->away_saved_state = priv->saved_state; - } else { - priv->away_saved_state = priv->state; - } - - new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY; - if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) { - new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY; - } - - DEBUG ("Going to autoaway. Saved state=%d, new state=%d", - priv->away_saved_state, new_state); - empathy_idle_set_state (idle, new_state); - } else if (!is_idle && priv->is_idle) { - /* We are no more idle, restore state */ - - idle_ext_away_stop (idle); - - /* Only try and set the presence if the away saved state is not - * unset. This is an odd case because it means that the session - * didn't notify us of the state change to idle, and as a - * result, we couldn't save the current state at that time. - */ - if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { - DEBUG ("Restoring state to %d", - priv->away_saved_state); - - empathy_idle_set_state (idle,priv->away_saved_state); - } else { - DEBUG ("Away saved state is unset. This means that we " - "weren't told when the session went idle. " - "As a result, I'm not trying to set presence"); - } - - priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; - } - - priv->is_idle = is_idle; -} - -static void -idle_state_change_cb (EmpathyConnectivity *connectivity, - gboolean new_online, - EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - if (!new_online) { - /* We are no longer connected */ - DEBUG ("Disconnected: Save state %d (%s)", - priv->state, priv->status); - priv->saved_state = priv->state; - g_free (priv->saved_status); - priv->saved_status = g_strdup (priv->status); - empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE); - } - else if (new_online - && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { - /* We are now connected */ - DEBUG ("Reconnected: Restore state %d (%s)", - priv->saved_state, priv->saved_status); - empathy_idle_set_presence (idle, - priv->saved_state, - priv->saved_status); - priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; - g_free (priv->saved_status); - priv->saved_status = NULL; - } -} - -static void -idle_finalize (GObject *object) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (object); - - g_free (priv->status); - g_free (priv->requested_status_message); - - if (priv->gs_proxy) { - g_object_unref (priv->gs_proxy); - } - - g_signal_handler_disconnect (priv->connectivity, - priv->state_change_signal_id); - priv->state_change_signal_id = 0; - - if (priv->manager != NULL) { - g_signal_handler_disconnect (priv->manager, - priv->idle_presence_changed_id); - g_object_unref (priv->manager); - } - - g_object_unref (priv->connectivity); - - g_hash_table_destroy (priv->connect_times); - priv->connect_times = NULL; - - idle_ext_away_stop (EMPATHY_IDLE (object)); -} - -static GObject * -idle_constructor (GType type, - guint n_props, - GObjectConstructParam *props) -{ - GObject *retval; - - if (idle_singleton) { - retval = g_object_ref (idle_singleton); - } else { - retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor - (type, n_props, props); - - idle_singleton = EMPATHY_IDLE (retval); - g_object_add_weak_pointer (retval, (gpointer) &idle_singleton); - } - - return retval; -} - -static const gchar * -empathy_idle_get_status (EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - if (G_UNLIKELY (!priv->ready)) - g_critical (G_STRLOC ": %s called before AccountManager ready", - G_STRFUNC); - - if (!priv->status) { - return empathy_presence_get_default_message (priv->state); - } - - return priv->status; -} - -static void -idle_get_property (GObject *object, - guint param_id, - GValue *value, - GParamSpec *pspec) -{ - EmpathyIdlePriv *priv; - EmpathyIdle *idle; - - priv = GET_PRIV (object); - idle = EMPATHY_IDLE (object); - - switch (param_id) { - case PROP_STATE: - g_value_set_enum (value, empathy_idle_get_state (idle)); - break; - case PROP_STATUS: - g_value_set_string (value, empathy_idle_get_status (idle)); - break; - case PROP_AUTO_AWAY: - g_value_set_boolean (value, empathy_idle_get_auto_away (idle)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); - break; - }; -} - -static void -idle_set_property (GObject *object, - guint param_id, - const GValue *value, - GParamSpec *pspec) -{ - EmpathyIdlePriv *priv; - EmpathyIdle *idle; - - priv = GET_PRIV (object); - idle = EMPATHY_IDLE (object); - - switch (param_id) { - case PROP_STATE: - empathy_idle_set_state (idle, g_value_get_enum (value)); - break; - case PROP_STATUS: - empathy_idle_set_status (idle, g_value_get_string (value)); - break; - case PROP_AUTO_AWAY: - empathy_idle_set_auto_away (idle, g_value_get_boolean (value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); - break; - }; -} - -static void -empathy_idle_class_init (EmpathyIdleClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - object_class->finalize = idle_finalize; - object_class->constructor = idle_constructor; - object_class->get_property = idle_get_property; - object_class->set_property = idle_set_property; - - g_object_class_install_property (object_class, - PROP_STATE, - g_param_spec_uint ("state", - "state", - "state", - 0, NUM_TP_CONNECTION_PRESENCE_TYPES, - TP_CONNECTION_PRESENCE_TYPE_UNSET, - G_PARAM_READWRITE)); - g_object_class_install_property (object_class, - PROP_STATUS, - g_param_spec_string ("status", - "status", - "status", - NULL, - G_PARAM_READWRITE)); - - g_object_class_install_property (object_class, - PROP_AUTO_AWAY, - g_param_spec_boolean ("auto-away", - "Automatic set presence to away", - "Should it set presence to away if inactive", - FALSE, - G_PARAM_READWRITE)); - - g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv)); -} - -static void -account_status_changed_cb (TpAccount *account, - guint old_status, - guint new_status, - guint reason, - gchar *dbus_error_name, - GHashTable *details, - gpointer user_data) -{ - EmpathyIdle *idle = EMPATHY_IDLE (user_data); - EmpathyIdlePriv *priv = GET_PRIV (idle); - GTimeVal val; - - if (new_status == TP_CONNECTION_STATUS_CONNECTED) { - g_get_current_time (&val); - g_hash_table_insert (priv->connect_times, account, - GINT_TO_POINTER (val.tv_sec)); - } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) { - g_hash_table_remove (priv->connect_times, account); - } -} - -static void -account_manager_ready_cb (GObject *source_object, - GAsyncResult *result, - gpointer user_data) -{ - EmpathyIdle *idle = user_data; - TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object); - EmpathyIdlePriv *priv; - TpConnectionPresenceType state; - gchar *status, *status_message; - GList *accounts, *l; - GError *error = NULL; - - /* In case we've been finalized before reading this callback */ - if (idle_singleton == NULL) - return; - - priv = GET_PRIV (idle); - priv->ready = TRUE; - - if (!tp_account_manager_prepare_finish (account_manager, result, &error)) { - DEBUG ("Failed to prepare account manager: %s", error->message); - g_error_free (error); - return; - } - - state = tp_account_manager_get_most_available_presence (priv->manager, - &status, &status_message); - - idle_presence_changed_cb (account_manager, state, status, - status_message, idle); - - accounts = tp_account_manager_get_valid_accounts (priv->manager); - for (l = accounts; l != NULL; l = l->next) { - tp_g_signal_connect_object (l->data, "status-changed", - G_CALLBACK (account_status_changed_cb), - idle, 0); - } - g_list_free (accounts); - - g_free (status); - g_free (status_message); -} - -static void -empathy_idle_init (EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle, - EMPATHY_TYPE_IDLE, EmpathyIdlePriv); - TpDBusDaemon *dbus; - - idle->priv = priv; - priv->is_idle = FALSE; - - priv->manager = tp_account_manager_dup (); - - tp_account_manager_prepare_async (priv->manager, NULL, - account_manager_ready_cb, idle); - - priv->idle_presence_changed_id = g_signal_connect (priv->manager, - "most-available-presence-changed", - G_CALLBACK (idle_presence_changed_cb), idle); - - dbus = tp_dbus_daemon_dup (NULL); - - priv->gs_proxy = dbus_g_proxy_new_for_name ( - tp_proxy_get_dbus_connection (dbus), - "org.gnome.SessionManager", - "/org/gnome/SessionManager/Presence", - "org.gnome.SessionManager.Presence"); - if (priv->gs_proxy) { - dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged", - G_TYPE_UINT, G_TYPE_INVALID); - dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged", - G_CALLBACK (idle_session_status_changed_cb), - idle, NULL); - } else { - DEBUG ("Failed to get gs proxy"); - } - - g_object_unref (dbus); - - priv->connectivity = empathy_connectivity_dup_singleton (); - priv->state_change_signal_id = g_signal_connect (priv->connectivity, - "state-change", G_CALLBACK (idle_state_change_cb), idle); - - priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal); -} - -EmpathyIdle * -empathy_idle_dup_singleton (void) -{ - return g_object_new (EMPATHY_TYPE_IDLE, NULL); -} - -TpConnectionPresenceType -empathy_idle_get_state (EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - if (G_UNLIKELY (!priv->ready)) - g_critical (G_STRLOC ": %s called before AccountManager ready", - G_STRFUNC); - - return priv->state; -} - -void -empathy_idle_set_state (EmpathyIdle *idle, - TpConnectionPresenceType state) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - empathy_idle_set_presence (idle, state, priv->status); -} - -void -empathy_idle_set_status (EmpathyIdle *idle, - const gchar *status) -{ - EmpathyIdlePriv *priv; - - priv = GET_PRIV (idle); - - empathy_idle_set_presence (idle, priv->state, status); -} - -static void -empathy_idle_do_set_presence (EmpathyIdle *idle, - TpConnectionPresenceType status_type, - const gchar *status_message) -{ - EmpathyIdlePriv *priv = GET_PRIV (idle); - const gchar *status; - - g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES); - - status = presence_type_to_status[status_type]; - - g_return_if_fail (status != NULL); - - /* We possibly should be sure that the account manager is prepared, but - * sometimes this isn't possible, like when exiting. In other words, - * we need a callback to empathy_idle_set_presence to be sure the - * presence is set on all accounts successfully. - * However, in practice, this is fine as we've already prepared the - * account manager here in _init. */ - tp_account_manager_set_all_requested_presences (priv->manager, - status_type, status, status_message); -} - -void -empathy_idle_set_presence (EmpathyIdle *idle, - TpConnectionPresenceType state, - const gchar *status) -{ - EmpathyIdlePriv *priv; - const gchar *default_status; - - priv = GET_PRIV (idle); - - DEBUG ("Changing presence to %s (%d)", status, state); - - g_free (priv->requested_status_message); - priv->requested_presence_type = state; - priv->requested_status_message = g_strdup (status); - - /* Do not set translated default messages */ - default_status = empathy_presence_get_default_message (state); - if (!tp_strdiff (status, default_status)) { - status = NULL; - } - - if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE && - !empathy_connectivity_is_online (priv->connectivity)) { - DEBUG ("Empathy is not online"); - - priv->saved_state = state; - if (tp_strdiff (priv->status, status)) { - g_free (priv->saved_status); - priv->saved_status = NULL; - if (!EMP_STR_EMPTY (status)) { - priv->saved_status = g_strdup (status); - } - } - return; - } - - empathy_idle_do_set_presence (idle, state, status); -} - -gboolean -empathy_idle_get_auto_away (EmpathyIdle *idle) -{ - EmpathyIdlePriv *priv = GET_PRIV (idle); - - return priv->auto_away; -} - -void -empathy_idle_set_auto_away (EmpathyIdle *idle, - gboolean auto_away) -{ - EmpathyIdlePriv *priv = GET_PRIV (idle); - - priv->auto_away = auto_away; - - g_object_notify (G_OBJECT (idle), "auto-away"); -} - -TpConnectionPresenceType -empathy_idle_get_requested_presence (EmpathyIdle *idle, - gchar **status, - gchar **status_message) -{ - EmpathyIdlePriv *priv = GET_PRIV (idle); - - if (status != NULL) { - *status = g_strdup (presence_type_to_status[priv->requested_presence_type]); - } - - if (status_message != NULL) { - *status_message = g_strdup (priv->requested_status_message); - } - - return priv->requested_presence_type; -} - -/* This function returns %TRUE if EmpathyIdle considers the account - * @account as having just connected recently. Otherwise, it returns - * %FALSE. In doubt, %FALSE is returned. */ -gboolean -empathy_idle_account_is_just_connected (EmpathyIdle *idle, - TpAccount *account) -{ - EmpathyIdlePriv *priv = GET_PRIV (idle); - GTimeVal val; - gpointer ptr; - glong t; - - if (tp_account_get_connection_status (account, NULL) - != TP_CONNECTION_STATUS_CONNECTED) { - return FALSE; - } - - ptr = g_hash_table_lookup (priv->connect_times, account); - - if (ptr == NULL) { - return FALSE; - } - - t = GPOINTER_TO_INT (ptr); - - g_get_current_time (&val); - - return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS; -} diff --git a/libempathy/empathy-idle.h b/libempathy/empathy-idle.h deleted file mode 100644 index 95b99dc40..000000000 --- a/libempathy/empathy-idle.h +++ /dev/null @@ -1,74 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Copyright (C) 2007-2008 Collabora Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * Authors: Xavier Claessens - */ - -#ifndef __EMPATHY_IDLE_H__ -#define __EMPATHY_IDLE_H__ - -#include - -#include -#include - -G_BEGIN_DECLS - -#define EMPATHY_TYPE_IDLE (empathy_idle_get_type ()) -#define EMPATHY_IDLE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_IDLE, EmpathyIdle)) -#define EMPATHY_IDLE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), EMPATHY_TYPE_IDLE, EmpathyIdleClass)) -#define EMPATHY_IS_IDLE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_IDLE)) -#define EMPATHY_IS_IDLE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_IDLE)) -#define EMPATHY_IDLE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_IDLE, EmpathyIdleClass)) - -typedef struct _EmpathyIdle EmpathyIdle; -typedef struct _EmpathyIdleClass EmpathyIdleClass; - -struct _EmpathyIdle { - GObject parent; - gpointer priv; -}; - -struct _EmpathyIdleClass { - GObjectClass parent_class; -}; - -GType empathy_idle_get_type (void) G_GNUC_CONST; -EmpathyIdle *empathy_idle_dup_singleton (void); -TpConnectionPresenceType empathy_idle_get_state (EmpathyIdle *idle); -void empathy_idle_set_state (EmpathyIdle *idle, - TpConnectionPresenceType state); -void empathy_idle_set_status (EmpathyIdle *idle, - const gchar *status); -void empathy_idle_set_presence (EmpathyIdle *idle, - TpConnectionPresenceType state, - const gchar *status); -gboolean empathy_idle_get_auto_away (EmpathyIdle *idle); -void empathy_idle_set_auto_away (EmpathyIdle *idle, - gboolean auto_away); - -TpConnectionPresenceType empathy_idle_get_requested_presence (EmpathyIdle *idle, - gchar **status, - gchar **status_message); - -gboolean empathy_idle_account_is_just_connected (EmpathyIdle *idle, - TpAccount *account); - -G_END_DECLS - -#endif /* __EMPATHY_IDLE_H__ */ diff --git a/libempathy/empathy-presence-manager.c b/libempathy/empathy-presence-manager.c new file mode 100644 index 000000000..035b50aba --- /dev/null +++ b/libempathy/empathy-presence-manager.c @@ -0,0 +1,733 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2007-2008 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Xavier Claessens + */ + +#include "empathy-presence-manager.h" + +#include + +#include + +#include +#include + +#include +#include +#include + +#include "empathy-utils.h" +#include "empathy-connectivity.h" + +#define DEBUG_FLAG EMPATHY_DEBUG_OTHER +#include "empathy-debug.h" + +/* Number of seconds before entering extended autoaway. */ +#define EXT_AWAY_TIME (30*60) + +/* Number of seconds to consider an account in the "just connected" state + * for. */ +#define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10 + +#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceManager) +typedef struct { + DBusGProxy *gs_proxy; + EmpathyConnectivity *connectivity; + gulong state_change_signal_id; + + gboolean ready; + + TpConnectionPresenceType state; + gchar *status; + gboolean auto_away; + + TpConnectionPresenceType away_saved_state; + TpConnectionPresenceType saved_state; + gchar *saved_status; + + gboolean is_idle; + guint ext_away_timeout; + + TpAccountManager *manager; + gulong idle_presence_changed_id; + + /* pointer to a TpAccount --> glong of time of connection */ + GHashTable *connect_times; + + TpConnectionPresenceType requested_presence_type; + gchar *requested_status_message; + +} EmpathyPresenceManagerPriv; + +typedef enum { + SESSION_STATUS_AVAILABLE, + SESSION_STATUS_INVISIBLE, + SESSION_STATUS_BUSY, + SESSION_STATUS_IDLE, + SESSION_STATUS_UNKNOWN +} SessionStatus; + +enum { + PROP_0, + PROP_STATE, + PROP_STATUS, + PROP_AUTO_AWAY +}; + +G_DEFINE_TYPE (EmpathyPresenceManager, empathy_presence_manager, G_TYPE_OBJECT); + +static EmpathyPresenceManager * idle_singleton = NULL; + +static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = { + NULL, + "offline", + "available", + "away", + "xa", + "hidden", + "busy", + NULL, + NULL, +}; + +static void +idle_presence_changed_cb (TpAccountManager *manager, + TpConnectionPresenceType state, + gchar *status, + gchar *status_message, + EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) + /* Assume our presence is offline if MC reports UNSET */ + state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE; + + DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state, + status_message); + + g_free (priv->status); + priv->state = state; + if (EMP_STR_EMPTY (status_message)) + priv->status = NULL; + else + priv->status = g_strdup (status_message); + + g_object_notify (G_OBJECT (idle), "state"); + g_object_notify (G_OBJECT (idle), "status"); +} + +static gboolean +idle_ext_away_cb (EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + DEBUG ("Going to extended autoaway"); + empathy_presence_manager_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY); + priv->ext_away_timeout = 0; + + return FALSE; +} + +static void +idle_ext_away_stop (EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + if (priv->ext_away_timeout) { + g_source_remove (priv->ext_away_timeout); + priv->ext_away_timeout = 0; + } +} + +static void +idle_ext_away_start (EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + if (priv->ext_away_timeout != 0) { + return; + } + priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME, + (GSourceFunc) idle_ext_away_cb, + idle); +} + +static void +idle_session_status_changed_cb (DBusGProxy *gs_proxy, + SessionStatus status, + EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + gboolean is_idle; + + priv = GET_PRIV (idle); + + is_idle = (status == SESSION_STATUS_IDLE); + + DEBUG ("Session idle state changed, %s -> %s", + priv->is_idle ? "yes" : "no", + is_idle ? "yes" : "no"); + + if (!priv->auto_away || + (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET && + (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE || + priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) { + /* We don't want to go auto away OR we explicitely asked to be + * offline, nothing to do here */ + priv->is_idle = is_idle; + return; + } + + if (is_idle && !priv->is_idle) { + TpConnectionPresenceType new_state; + /* We are now idle */ + + idle_ext_away_start (idle); + + if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { + /* We are disconnected, when coming back from away + * we want to restore the presence before the + * disconnection. */ + priv->away_saved_state = priv->saved_state; + } else { + priv->away_saved_state = priv->state; + } + + new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY; + if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) { + new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY; + } + + DEBUG ("Going to autoaway. Saved state=%d, new state=%d", + priv->away_saved_state, new_state); + empathy_presence_manager_set_state (idle, new_state); + } else if (!is_idle && priv->is_idle) { + /* We are no more idle, restore state */ + + idle_ext_away_stop (idle); + + /* Only try and set the presence if the away saved state is not + * unset. This is an odd case because it means that the session + * didn't notify us of the state change to idle, and as a + * result, we couldn't save the current state at that time. + */ + if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { + DEBUG ("Restoring state to %d", + priv->away_saved_state); + + empathy_presence_manager_set_state (idle,priv->away_saved_state); + } else { + DEBUG ("Away saved state is unset. This means that we " + "weren't told when the session went idle. " + "As a result, I'm not trying to set presence"); + } + + priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; + } + + priv->is_idle = is_idle; +} + +static void +idle_state_change_cb (EmpathyConnectivity *connectivity, + gboolean new_online, + EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + if (!new_online) { + /* We are no longer connected */ + DEBUG ("Disconnected: Save state %d (%s)", + priv->state, priv->status); + priv->saved_state = priv->state; + g_free (priv->saved_status); + priv->saved_status = g_strdup (priv->status); + empathy_presence_manager_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE); + } + else if (new_online + && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { + /* We are now connected */ + DEBUG ("Reconnected: Restore state %d (%s)", + priv->saved_state, priv->saved_status); + empathy_presence_manager_set_presence (idle, + priv->saved_state, + priv->saved_status); + priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; + g_free (priv->saved_status); + priv->saved_status = NULL; + } +} + +static void +idle_finalize (GObject *object) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (object); + + g_free (priv->status); + g_free (priv->requested_status_message); + + if (priv->gs_proxy) { + g_object_unref (priv->gs_proxy); + } + + g_signal_handler_disconnect (priv->connectivity, + priv->state_change_signal_id); + priv->state_change_signal_id = 0; + + if (priv->manager != NULL) { + g_signal_handler_disconnect (priv->manager, + priv->idle_presence_changed_id); + g_object_unref (priv->manager); + } + + g_object_unref (priv->connectivity); + + g_hash_table_destroy (priv->connect_times); + priv->connect_times = NULL; + + idle_ext_away_stop (EMPATHY_PRESENCE_MANAGER (object)); +} + +static GObject * +idle_constructor (GType type, + guint n_props, + GObjectConstructParam *props) +{ + GObject *retval; + + if (idle_singleton) { + retval = g_object_ref (idle_singleton); + } else { + retval = G_OBJECT_CLASS (empathy_presence_manager_parent_class)->constructor + (type, n_props, props); + + idle_singleton = EMPATHY_PRESENCE_MANAGER (retval); + g_object_add_weak_pointer (retval, (gpointer) &idle_singleton); + } + + return retval; +} + +static const gchar * +empathy_presence_manager_get_status (EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + if (G_UNLIKELY (!priv->ready)) + g_critical (G_STRLOC ": %s called before AccountManager ready", + G_STRFUNC); + + if (!priv->status) { + return empathy_presence_get_default_message (priv->state); + } + + return priv->status; +} + +static void +idle_get_property (GObject *object, + guint param_id, + GValue *value, + GParamSpec *pspec) +{ + EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManager *idle; + + priv = GET_PRIV (object); + idle = EMPATHY_PRESENCE_MANAGER (object); + + switch (param_id) { + case PROP_STATE: + g_value_set_enum (value, empathy_presence_manager_get_state (idle)); + break; + case PROP_STATUS: + g_value_set_string (value, empathy_presence_manager_get_status (idle)); + break; + case PROP_AUTO_AWAY: + g_value_set_boolean (value, empathy_presence_manager_get_auto_away (idle)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; +} + +static void +idle_set_property (GObject *object, + guint param_id, + const GValue *value, + GParamSpec *pspec) +{ + EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManager *idle; + + priv = GET_PRIV (object); + idle = EMPATHY_PRESENCE_MANAGER (object); + + switch (param_id) { + case PROP_STATE: + empathy_presence_manager_set_state (idle, g_value_get_enum (value)); + break; + case PROP_STATUS: + empathy_presence_manager_set_status (idle, g_value_get_string (value)); + break; + case PROP_AUTO_AWAY: + empathy_presence_manager_set_auto_away (idle, g_value_get_boolean (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; +} + +static void +empathy_presence_manager_class_init (EmpathyPresenceManagerClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = idle_finalize; + object_class->constructor = idle_constructor; + object_class->get_property = idle_get_property; + object_class->set_property = idle_set_property; + + g_object_class_install_property (object_class, + PROP_STATE, + g_param_spec_uint ("state", + "state", + "state", + 0, NUM_TP_CONNECTION_PRESENCE_TYPES, + TP_CONNECTION_PRESENCE_TYPE_UNSET, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_STATUS, + g_param_spec_string ("status", + "status", + "status", + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, + PROP_AUTO_AWAY, + g_param_spec_boolean ("auto-away", + "Automatic set presence to away", + "Should it set presence to away if inactive", + FALSE, + G_PARAM_READWRITE)); + + g_type_class_add_private (object_class, sizeof (EmpathyPresenceManagerPriv)); +} + +static void +account_status_changed_cb (TpAccount *account, + guint old_status, + guint new_status, + guint reason, + gchar *dbus_error_name, + GHashTable *details, + gpointer user_data) +{ + EmpathyPresenceManager *idle = EMPATHY_PRESENCE_MANAGER (user_data); + EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + GTimeVal val; + + if (new_status == TP_CONNECTION_STATUS_CONNECTED) { + g_get_current_time (&val); + g_hash_table_insert (priv->connect_times, account, + GINT_TO_POINTER (val.tv_sec)); + } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) { + g_hash_table_remove (priv->connect_times, account); + } +} + +static void +account_manager_ready_cb (GObject *source_object, + GAsyncResult *result, + gpointer user_data) +{ + EmpathyPresenceManager *idle = user_data; + TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object); + EmpathyPresenceManagerPriv *priv; + TpConnectionPresenceType state; + gchar *status, *status_message; + GList *accounts, *l; + GError *error = NULL; + + /* In case we've been finalized before reading this callback */ + if (idle_singleton == NULL) + return; + + priv = GET_PRIV (idle); + priv->ready = TRUE; + + if (!tp_account_manager_prepare_finish (account_manager, result, &error)) { + DEBUG ("Failed to prepare account manager: %s", error->message); + g_error_free (error); + return; + } + + state = tp_account_manager_get_most_available_presence (priv->manager, + &status, &status_message); + + idle_presence_changed_cb (account_manager, state, status, + status_message, idle); + + accounts = tp_account_manager_get_valid_accounts (priv->manager); + for (l = accounts; l != NULL; l = l->next) { + tp_g_signal_connect_object (l->data, "status-changed", + G_CALLBACK (account_status_changed_cb), + idle, 0); + } + g_list_free (accounts); + + g_free (status); + g_free (status_message); +} + +static void +empathy_presence_manager_init (EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle, + EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPriv); + TpDBusDaemon *dbus; + + idle->priv = priv; + priv->is_idle = FALSE; + + priv->manager = tp_account_manager_dup (); + + tp_account_manager_prepare_async (priv->manager, NULL, + account_manager_ready_cb, idle); + + priv->idle_presence_changed_id = g_signal_connect (priv->manager, + "most-available-presence-changed", + G_CALLBACK (idle_presence_changed_cb), idle); + + dbus = tp_dbus_daemon_dup (NULL); + + priv->gs_proxy = dbus_g_proxy_new_for_name ( + tp_proxy_get_dbus_connection (dbus), + "org.gnome.SessionManager", + "/org/gnome/SessionManager/Presence", + "org.gnome.SessionManager.Presence"); + if (priv->gs_proxy) { + dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged", + G_TYPE_UINT, G_TYPE_INVALID); + dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged", + G_CALLBACK (idle_session_status_changed_cb), + idle, NULL); + } else { + DEBUG ("Failed to get gs proxy"); + } + + g_object_unref (dbus); + + priv->connectivity = empathy_connectivity_dup_singleton (); + priv->state_change_signal_id = g_signal_connect (priv->connectivity, + "state-change", G_CALLBACK (idle_state_change_cb), idle); + + priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal); +} + +EmpathyPresenceManager * +empathy_presence_manager_dup_singleton (void) +{ + return g_object_new (EMPATHY_TYPE_PRESENCE_MANAGER, NULL); +} + +TpConnectionPresenceType +empathy_presence_manager_get_state (EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + if (G_UNLIKELY (!priv->ready)) + g_critical (G_STRLOC ": %s called before AccountManager ready", + G_STRFUNC); + + return priv->state; +} + +void +empathy_presence_manager_set_state (EmpathyPresenceManager *idle, + TpConnectionPresenceType state) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + empathy_presence_manager_set_presence (idle, state, priv->status); +} + +void +empathy_presence_manager_set_status (EmpathyPresenceManager *idle, + const gchar *status) +{ + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (idle); + + empathy_presence_manager_set_presence (idle, priv->state, status); +} + +static void +empathy_presence_manager_do_set_presence (EmpathyPresenceManager *idle, + TpConnectionPresenceType status_type, + const gchar *status_message) +{ + EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + const gchar *status; + + g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES); + + status = presence_type_to_status[status_type]; + + g_return_if_fail (status != NULL); + + /* We possibly should be sure that the account manager is prepared, but + * sometimes this isn't possible, like when exiting. In other words, + * we need a callback to empathy_presence_manager_set_presence to be sure the + * presence is set on all accounts successfully. + * However, in practice, this is fine as we've already prepared the + * account manager here in _init. */ + tp_account_manager_set_all_requested_presences (priv->manager, + status_type, status, status_message); +} + +void +empathy_presence_manager_set_presence (EmpathyPresenceManager *idle, + TpConnectionPresenceType state, + const gchar *status) +{ + EmpathyPresenceManagerPriv *priv; + const gchar *default_status; + + priv = GET_PRIV (idle); + + DEBUG ("Changing presence to %s (%d)", status, state); + + g_free (priv->requested_status_message); + priv->requested_presence_type = state; + priv->requested_status_message = g_strdup (status); + + /* Do not set translated default messages */ + default_status = empathy_presence_get_default_message (state); + if (!tp_strdiff (status, default_status)) { + status = NULL; + } + + if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE && + !empathy_connectivity_is_online (priv->connectivity)) { + DEBUG ("Empathy is not online"); + + priv->saved_state = state; + if (tp_strdiff (priv->status, status)) { + g_free (priv->saved_status); + priv->saved_status = NULL; + if (!EMP_STR_EMPTY (status)) { + priv->saved_status = g_strdup (status); + } + } + return; + } + + empathy_presence_manager_do_set_presence (idle, state, status); +} + +gboolean +empathy_presence_manager_get_auto_away (EmpathyPresenceManager *idle) +{ + EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + + return priv->auto_away; +} + +void +empathy_presence_manager_set_auto_away (EmpathyPresenceManager *idle, + gboolean auto_away) +{ + EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + + priv->auto_away = auto_away; + + g_object_notify (G_OBJECT (idle), "auto-away"); +} + +TpConnectionPresenceType +empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *idle, + gchar **status, + gchar **status_message) +{ + EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + + if (status != NULL) { + *status = g_strdup (presence_type_to_status[priv->requested_presence_type]); + } + + if (status_message != NULL) { + *status_message = g_strdup (priv->requested_status_message); + } + + return priv->requested_presence_type; +} + +/* This function returns %TRUE if EmpathyPresenceManager considers the account + * @account as having just connected recently. Otherwise, it returns + * %FALSE. In doubt, %FALSE is returned. */ +gboolean +empathy_presence_manager_account_is_just_connected (EmpathyPresenceManager *idle, + TpAccount *account) +{ + EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + GTimeVal val; + gpointer ptr; + glong t; + + if (tp_account_get_connection_status (account, NULL) + != TP_CONNECTION_STATUS_CONNECTED) { + return FALSE; + } + + ptr = g_hash_table_lookup (priv->connect_times, account); + + if (ptr == NULL) { + return FALSE; + } + + t = GPOINTER_TO_INT (ptr); + + g_get_current_time (&val); + + return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS; +} diff --git a/libempathy/empathy-presence-manager.h b/libempathy/empathy-presence-manager.h new file mode 100644 index 000000000..bdbe4cf62 --- /dev/null +++ b/libempathy/empathy-presence-manager.h @@ -0,0 +1,74 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2007-2008 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Xavier Claessens + */ + +#ifndef __EMPATHY_PRESENCE_MANAGER_H__ +#define __EMPATHY_PRESENCE_MANAGER_H__ + +#include + +#include +#include + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_PRESENCE_MANAGER (empathy_presence_manager_get_type ()) +#define EMPATHY_PRESENCE_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManager)) +#define EMPATHY_PRESENCE_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerClass)) +#define EMPATHY_IS_PRESENCE_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_PRESENCE_MANAGER)) +#define EMPATHY_IS_PRESENCE_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_PRESENCE_MANAGER)) +#define EMPATHY_PRESENCE_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerClass)) + +typedef struct _EmpathyPresenceManager EmpathyPresenceManager; +typedef struct _EmpathyPresenceManagerClass EmpathyPresenceManagerClass; + +struct _EmpathyPresenceManager { + GObject parent; + gpointer priv; +}; + +struct _EmpathyPresenceManagerClass { + GObjectClass parent_class; +}; + +GType empathy_presence_manager_get_type (void) G_GNUC_CONST; +EmpathyPresenceManager *empathy_presence_manager_dup_singleton (void); +TpConnectionPresenceType empathy_presence_manager_get_state (EmpathyPresenceManager *idle); +void empathy_presence_manager_set_state (EmpathyPresenceManager *idle, + TpConnectionPresenceType state); +void empathy_presence_manager_set_status (EmpathyPresenceManager *idle, + const gchar *status); +void empathy_presence_manager_set_presence (EmpathyPresenceManager *idle, + TpConnectionPresenceType state, + const gchar *status); +gboolean empathy_presence_manager_get_auto_away (EmpathyPresenceManager *idle); +void empathy_presence_manager_set_auto_away (EmpathyPresenceManager *idle, + gboolean auto_away); + +TpConnectionPresenceType empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *idle, + gchar **status, + gchar **status_message); + +gboolean empathy_presence_manager_account_is_just_connected (EmpathyPresenceManager *idle, + TpAccount *account); + +G_END_DECLS + +#endif /* __EMPATHY_PRESENCE_MANAGER_H__ */ diff --git a/libempathy/empathy-utils.c b/libempathy/empathy-utils.c index 6bbd53f86..3730e43a8 100644 --- a/libempathy/empathy-utils.c +++ b/libempathy/empathy-utils.c @@ -49,7 +49,7 @@ #include "empathy-contact-manager.h" #include "empathy-individual-manager.h" #include "empathy-dispatcher.h" -#include "empathy-idle.h" +#include "empathy-presence-manager.h" #include "empathy-tp-call.h" #include "empathy-tp-contact-factory.h" @@ -482,11 +482,11 @@ gboolean empathy_check_available_state (void) { TpConnectionPresenceType presence; - EmpathyIdle *idle; + EmpathyPresenceManager *presence_mgr; - idle = empathy_idle_dup_singleton (); - presence = empathy_idle_get_state (idle); - g_object_unref (idle); + presence_mgr = empathy_presence_manager_dup_singleton (); + presence = empathy_presence_manager_get_state (presence_mgr); + g_object_unref (presence_mgr); if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE && presence != TP_CONNECTION_PRESENCE_TYPE_UNSET) { -- cgit v1.2.3 From 4c2cba97c84799564a7b0efb14bbff1f75b5c427 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 25 Jan 2011 11:50:35 +0100 Subject: presence-manager: port to new coding style --- libempathy/empathy-presence-manager.c | 1048 +++++++++++++++++---------------- libempathy/empathy-presence-manager.h | 62 +- 2 files changed, 570 insertions(+), 540 deletions(-) (limited to 'libempathy') diff --git a/libempathy/empathy-presence-manager.c b/libempathy/empathy-presence-manager.c index 035b50aba..059934649 100644 --- a/libempathy/empathy-presence-manager.c +++ b/libempathy/empathy-presence-manager.c @@ -1,6 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* - * Copyright (C) 2007-2008 Collabora Ltd. + * Copyright (C) 2007-2011 Collabora Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -46,688 +45,709 @@ #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceManager) -typedef struct { - DBusGProxy *gs_proxy; - EmpathyConnectivity *connectivity; - gulong state_change_signal_id; - gboolean ready; +typedef struct +{ + DBusGProxy *gs_proxy; + EmpathyConnectivity *connectivity; + gulong state_change_signal_id; + + gboolean ready; - TpConnectionPresenceType state; - gchar *status; - gboolean auto_away; + TpConnectionPresenceType state; + gchar *status; + gboolean auto_away; - TpConnectionPresenceType away_saved_state; - TpConnectionPresenceType saved_state; - gchar *saved_status; + TpConnectionPresenceType away_saved_state; + TpConnectionPresenceType saved_state; + gchar *saved_status; - gboolean is_idle; - guint ext_away_timeout; + gboolean is_idle; + guint ext_away_timeout; - TpAccountManager *manager; - gulong idle_presence_changed_id; + TpAccountManager *manager; + gulong most_available_presence_changed_id; - /* pointer to a TpAccount --> glong of time of connection */ - GHashTable *connect_times; + /* pointer to a TpAccount --> glong of time of connection */ + GHashTable *connect_times; - TpConnectionPresenceType requested_presence_type; - gchar *requested_status_message; + TpConnectionPresenceType requested_presence_type; + gchar *requested_status_message; } EmpathyPresenceManagerPriv; -typedef enum { - SESSION_STATUS_AVAILABLE, - SESSION_STATUS_INVISIBLE, - SESSION_STATUS_BUSY, - SESSION_STATUS_IDLE, - SESSION_STATUS_UNKNOWN +typedef enum +{ + SESSION_STATUS_AVAILABLE, + SESSION_STATUS_INVISIBLE, + SESSION_STATUS_BUSY, + SESSION_STATUS_IDLE, + SESSION_STATUS_UNKNOWN } SessionStatus; -enum { - PROP_0, - PROP_STATE, - PROP_STATUS, - PROP_AUTO_AWAY +enum +{ + PROP_0, + PROP_STATE, + PROP_STATUS, + PROP_AUTO_AWAY }; G_DEFINE_TYPE (EmpathyPresenceManager, empathy_presence_manager, G_TYPE_OBJECT); -static EmpathyPresenceManager * idle_singleton = NULL; - -static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = { - NULL, - "offline", - "available", - "away", - "xa", - "hidden", - "busy", - NULL, - NULL, +static EmpathyPresenceManager * singleton = NULL; + +static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = +{ + NULL, + "offline", + "available", + "away", + "xa", + "hidden", + "busy", + NULL, + NULL, }; static void -idle_presence_changed_cb (TpAccountManager *manager, - TpConnectionPresenceType state, - gchar *status, - gchar *status_message, - EmpathyPresenceManager *idle) +most_available_presence_changed (TpAccountManager *manager, + TpConnectionPresenceType state, + gchar *status, + gchar *status_message, + EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (idle); + priv = GET_PRIV (self); - if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) - /* Assume our presence is offline if MC reports UNSET */ - state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE; + if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) + /* Assume our presence is offline if MC reports UNSET */ + state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE; - DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state, - status_message); + DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state, + status_message); - g_free (priv->status); - priv->state = state; - if (EMP_STR_EMPTY (status_message)) - priv->status = NULL; - else - priv->status = g_strdup (status_message); + g_free (priv->status); + priv->state = state; + if (EMP_STR_EMPTY (status_message)) + priv->status = NULL; + else + priv->status = g_strdup (status_message); - g_object_notify (G_OBJECT (idle), "state"); - g_object_notify (G_OBJECT (idle), "status"); + g_object_notify (G_OBJECT (self), "state"); + g_object_notify (G_OBJECT (self), "status"); } static gboolean -idle_ext_away_cb (EmpathyPresenceManager *idle) +ext_away_cb (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (idle); + priv = GET_PRIV (self); - DEBUG ("Going to extended autoaway"); - empathy_presence_manager_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY); - priv->ext_away_timeout = 0; + DEBUG ("Going to extended autoaway"); + empathy_presence_manager_set_state (self, + TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY); + priv->ext_away_timeout = 0; - return FALSE; + return FALSE; } static void -idle_ext_away_stop (EmpathyPresenceManager *idle) +next_away_stop (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (idle); + priv = GET_PRIV (self); - if (priv->ext_away_timeout) { - g_source_remove (priv->ext_away_timeout); - priv->ext_away_timeout = 0; - } + if (priv->ext_away_timeout) + { + g_source_remove (priv->ext_away_timeout); + priv->ext_away_timeout = 0; + } } static void -idle_ext_away_start (EmpathyPresenceManager *idle) +ext_away_start (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (self); - priv = GET_PRIV (idle); + if (priv->ext_away_timeout != 0) + return; - if (priv->ext_away_timeout != 0) { - return; - } - priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME, - (GSourceFunc) idle_ext_away_cb, - idle); + priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME, + (GSourceFunc) ext_away_cb, self); } static void -idle_session_status_changed_cb (DBusGProxy *gs_proxy, - SessionStatus status, - EmpathyPresenceManager *idle) +session_status_changed_cb (DBusGProxy *gs_proxy, + SessionStatus status, + EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - gboolean is_idle; - - priv = GET_PRIV (idle); - - is_idle = (status == SESSION_STATUS_IDLE); - - DEBUG ("Session idle state changed, %s -> %s", - priv->is_idle ? "yes" : "no", - is_idle ? "yes" : "no"); - - if (!priv->auto_away || - (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET && - (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE || - priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) { - /* We don't want to go auto away OR we explicitely asked to be - * offline, nothing to do here */ - priv->is_idle = is_idle; - return; - } - - if (is_idle && !priv->is_idle) { - TpConnectionPresenceType new_state; - /* We are now idle */ - - idle_ext_away_start (idle); - - if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { - /* We are disconnected, when coming back from away - * we want to restore the presence before the - * disconnection. */ - priv->away_saved_state = priv->saved_state; - } else { - priv->away_saved_state = priv->state; - } - - new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY; - if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) { - new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY; - } - - DEBUG ("Going to autoaway. Saved state=%d, new state=%d", - priv->away_saved_state, new_state); - empathy_presence_manager_set_state (idle, new_state); - } else if (!is_idle && priv->is_idle) { - /* We are no more idle, restore state */ - - idle_ext_away_stop (idle); - - /* Only try and set the presence if the away saved state is not - * unset. This is an odd case because it means that the session - * didn't notify us of the state change to idle, and as a - * result, we couldn't save the current state at that time. - */ - if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { - DEBUG ("Restoring state to %d", - priv->away_saved_state); - - empathy_presence_manager_set_state (idle,priv->away_saved_state); - } else { - DEBUG ("Away saved state is unset. This means that we " - "weren't told when the session went idle. " - "As a result, I'm not trying to set presence"); - } - - priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; - } - - priv->is_idle = is_idle; + EmpathyPresenceManagerPriv *priv; + gboolean is_idle; + + priv = GET_PRIV (self); + + is_idle = (status == SESSION_STATUS_IDLE); + + DEBUG ("Session idle state changed, %s -> %s", + priv->is_idle ? "yes" : "no", + is_idle ? "yes" : "no"); + + if (!priv->auto_away || + (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET && + (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE || + priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) + { + /* We don't want to go auto away OR we explicitely asked to be + * offline, nothing to do here */ + priv->is_idle = is_idle; + return; + } + + if (is_idle && !priv->is_idle) + { + TpConnectionPresenceType new_state; + /* We are now idle */ + + ext_away_start (self); + + if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) + /* We are disconnected, when coming back from away + * we want to restore the presence before the + * disconnection. */ + priv->away_saved_state = priv->saved_state; + else + priv->away_saved_state = priv->state; + + new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY; + if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) + new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY; + + DEBUG ("Going to autoaway. Saved state=%d, new state=%d", + priv->away_saved_state, new_state); + empathy_presence_manager_set_state (self, new_state); + } + else if (!is_idle && priv->is_idle) + { + /* We are no more idle, restore state */ + + next_away_stop (self); + + /* Only try and set the presence if the away saved state is not + * unset. This is an odd case because it means that the session + * didn't notify us of the state change to idle, and as a + * result, we couldn't save the current state at that time. + */ + if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) + { + DEBUG ("Restoring state to %d", + priv->away_saved_state); + + empathy_presence_manager_set_state (self, priv->away_saved_state); + } + else + { + DEBUG ("Away saved state is unset. This means that we " + "weren't told when the session went idle. " + "As a result, I'm not trying to set presence"); + } + + priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; + } + + priv->is_idle = is_idle; } static void -idle_state_change_cb (EmpathyConnectivity *connectivity, - gboolean new_online, - EmpathyPresenceManager *idle) +state_change_cb (EmpathyConnectivity *connectivity, + gboolean new_online, + EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (idle); - - if (!new_online) { - /* We are no longer connected */ - DEBUG ("Disconnected: Save state %d (%s)", - priv->state, priv->status); - priv->saved_state = priv->state; - g_free (priv->saved_status); - priv->saved_status = g_strdup (priv->status); - empathy_presence_manager_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE); - } - else if (new_online - && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { - /* We are now connected */ - DEBUG ("Reconnected: Restore state %d (%s)", - priv->saved_state, priv->saved_status); - empathy_presence_manager_set_presence (idle, - priv->saved_state, - priv->saved_status); - priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; - g_free (priv->saved_status); - priv->saved_status = NULL; - } + EmpathyPresenceManagerPriv *priv; + + priv = GET_PRIV (self); + + if (!new_online) + { + /* We are no longer connected */ + DEBUG ("Disconnected: Save state %d (%s)", + priv->state, priv->status); + priv->saved_state = priv->state; + g_free (priv->saved_status); + priv->saved_status = g_strdup (priv->status); + empathy_presence_manager_set_state (self, + TP_CONNECTION_PRESENCE_TYPE_OFFLINE); + } + else if (new_online + && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) + { + /* We are now connected */ + DEBUG ("Reconnected: Restore state %d (%s)", + priv->saved_state, priv->saved_status); + empathy_presence_manager_set_presence (self, + priv->saved_state, + priv->saved_status); + priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; + g_free (priv->saved_status); + priv->saved_status = NULL; + } } static void -idle_finalize (GObject *object) +presence_manager_finalize (GObject *object) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (object); + priv = GET_PRIV (object); - g_free (priv->status); - g_free (priv->requested_status_message); + g_free (priv->status); + g_free (priv->requested_status_message); - if (priv->gs_proxy) { - g_object_unref (priv->gs_proxy); - } + if (priv->gs_proxy) + g_object_unref (priv->gs_proxy); - g_signal_handler_disconnect (priv->connectivity, - priv->state_change_signal_id); - priv->state_change_signal_id = 0; + g_signal_handler_disconnect (priv->connectivity, + priv->state_change_signal_id); + priv->state_change_signal_id = 0; - if (priv->manager != NULL) { - g_signal_handler_disconnect (priv->manager, - priv->idle_presence_changed_id); - g_object_unref (priv->manager); - } + if (priv->manager != NULL) + { + g_signal_handler_disconnect (priv->manager, + priv->most_available_presence_changed_id); + g_object_unref (priv->manager); + } - g_object_unref (priv->connectivity); + g_object_unref (priv->connectivity); - g_hash_table_destroy (priv->connect_times); - priv->connect_times = NULL; + g_hash_table_destroy (priv->connect_times); + priv->connect_times = NULL; - idle_ext_away_stop (EMPATHY_PRESENCE_MANAGER (object)); + next_away_stop (EMPATHY_PRESENCE_MANAGER (object)); } static GObject * -idle_constructor (GType type, - guint n_props, - GObjectConstructParam *props) +presence_manager_constructor (GType type, + guint n_props, + GObjectConstructParam *props) { - GObject *retval; - - if (idle_singleton) { - retval = g_object_ref (idle_singleton); - } else { - retval = G_OBJECT_CLASS (empathy_presence_manager_parent_class)->constructor - (type, n_props, props); - - idle_singleton = EMPATHY_PRESENCE_MANAGER (retval); - g_object_add_weak_pointer (retval, (gpointer) &idle_singleton); - } - - return retval; + GObject *retval; + + if (singleton) + { + retval = g_object_ref (singleton); + } + else + { + retval = G_OBJECT_CLASS (empathy_presence_manager_parent_class)-> + constructor (type, n_props, props); + + singleton = EMPATHY_PRESENCE_MANAGER (retval); + g_object_add_weak_pointer (retval, (gpointer) &singleton); + } + + return retval; } static const gchar * -empathy_presence_manager_get_status (EmpathyPresenceManager *idle) +empathy_presence_manager_get_status (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (idle); + priv = GET_PRIV (self); - if (G_UNLIKELY (!priv->ready)) - g_critical (G_STRLOC ": %s called before AccountManager ready", - G_STRFUNC); + if (G_UNLIKELY (!priv->ready)) + g_critical (G_STRLOC ": %s called before AccountManager ready", + G_STRFUNC); - if (!priv->status) { - return empathy_presence_get_default_message (priv->state); - } + if (!priv->status) + return empathy_presence_get_default_message (priv->state); - return priv->status; + return priv->status; } static void -idle_get_property (GObject *object, - guint param_id, - GValue *value, - GParamSpec *pspec) +presence_manager_get_property (GObject *object, + guint param_id, + GValue *value, + GParamSpec *pspec) { - EmpathyPresenceManagerPriv *priv; - EmpathyPresenceManager *idle; - - priv = GET_PRIV (object); - idle = EMPATHY_PRESENCE_MANAGER (object); - - switch (param_id) { - case PROP_STATE: - g_value_set_enum (value, empathy_presence_manager_get_state (idle)); - break; - case PROP_STATUS: - g_value_set_string (value, empathy_presence_manager_get_status (idle)); - break; - case PROP_AUTO_AWAY: - g_value_set_boolean (value, empathy_presence_manager_get_auto_away (idle)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); - break; - }; + EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManager *self; + + priv = GET_PRIV (object); + self = EMPATHY_PRESENCE_MANAGER (object); + + switch (param_id) + { + case PROP_STATE: + g_value_set_enum (value, empathy_presence_manager_get_state (self)); + break; + case PROP_STATUS: + g_value_set_string (value, empathy_presence_manager_get_status (self)); + break; + case PROP_AUTO_AWAY: + g_value_set_boolean (value, + empathy_presence_manager_get_auto_away (self)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; } static void -idle_set_property (GObject *object, - guint param_id, - const GValue *value, - GParamSpec *pspec) +presence_manager_set_property (GObject *object, + guint param_id, + const GValue *value, + GParamSpec *pspec) { - EmpathyPresenceManagerPriv *priv; - EmpathyPresenceManager *idle; - - priv = GET_PRIV (object); - idle = EMPATHY_PRESENCE_MANAGER (object); - - switch (param_id) { - case PROP_STATE: - empathy_presence_manager_set_state (idle, g_value_get_enum (value)); - break; - case PROP_STATUS: - empathy_presence_manager_set_status (idle, g_value_get_string (value)); - break; - case PROP_AUTO_AWAY: - empathy_presence_manager_set_auto_away (idle, g_value_get_boolean (value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); - break; - }; + EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManager *self; + + priv = GET_PRIV (object); + self = EMPATHY_PRESENCE_MANAGER (object); + + switch (param_id) + { + case PROP_STATE: + empathy_presence_manager_set_state (self, g_value_get_enum (value)); + break; + case PROP_STATUS: + empathy_presence_manager_set_status (self, g_value_get_string (value)); + break; + case PROP_AUTO_AWAY: + empathy_presence_manager_set_auto_away (self, + g_value_get_boolean (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; } static void empathy_presence_manager_class_init (EmpathyPresenceManagerClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - object_class->finalize = idle_finalize; - object_class->constructor = idle_constructor; - object_class->get_property = idle_get_property; - object_class->set_property = idle_set_property; - - g_object_class_install_property (object_class, - PROP_STATE, - g_param_spec_uint ("state", - "state", - "state", - 0, NUM_TP_CONNECTION_PRESENCE_TYPES, - TP_CONNECTION_PRESENCE_TYPE_UNSET, - G_PARAM_READWRITE)); - g_object_class_install_property (object_class, - PROP_STATUS, - g_param_spec_string ("status", - "status", - "status", - NULL, - G_PARAM_READWRITE)); - - g_object_class_install_property (object_class, - PROP_AUTO_AWAY, - g_param_spec_boolean ("auto-away", - "Automatic set presence to away", - "Should it set presence to away if inactive", - FALSE, - G_PARAM_READWRITE)); - - g_type_class_add_private (object_class, sizeof (EmpathyPresenceManagerPriv)); + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = presence_manager_finalize; + object_class->constructor = presence_manager_constructor; + object_class->get_property = presence_manager_get_property; + object_class->set_property = presence_manager_set_property; + + g_object_class_install_property (object_class, + PROP_STATE, + g_param_spec_uint ("state", "state", "state", + 0, NUM_TP_CONNECTION_PRESENCE_TYPES, + TP_CONNECTION_PRESENCE_TYPE_UNSET, + G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, + PROP_STATUS, + g_param_spec_string ("status","status", "status", + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, + PROP_AUTO_AWAY, + g_param_spec_boolean ("auto-away", "Automatic set presence to away", + "Should it set presence to away if inactive", + FALSE, + G_PARAM_READWRITE)); + + g_type_class_add_private (object_class, sizeof (EmpathyPresenceManagerPriv)); } static void account_status_changed_cb (TpAccount *account, - guint old_status, - guint new_status, - guint reason, - gchar *dbus_error_name, - GHashTable *details, - gpointer user_data) + guint old_status, + guint new_status, + guint reason, + gchar *dbus_error_name, + GHashTable *details, + gpointer user_data) { - EmpathyPresenceManager *idle = EMPATHY_PRESENCE_MANAGER (user_data); - EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); - GTimeVal val; - - if (new_status == TP_CONNECTION_STATUS_CONNECTED) { - g_get_current_time (&val); - g_hash_table_insert (priv->connect_times, account, - GINT_TO_POINTER (val.tv_sec)); - } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) { - g_hash_table_remove (priv->connect_times, account); - } + EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (user_data); + EmpathyPresenceManagerPriv *priv = GET_PRIV (self); + GTimeVal val; + + if (new_status == TP_CONNECTION_STATUS_CONNECTED) + { + g_get_current_time (&val); + g_hash_table_insert (priv->connect_times, account, + GINT_TO_POINTER (val.tv_sec)); + } + else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) + { + g_hash_table_remove (priv->connect_times, account); + } } static void account_manager_ready_cb (GObject *source_object, - GAsyncResult *result, - gpointer user_data) + GAsyncResult *result, + gpointer user_data) { - EmpathyPresenceManager *idle = user_data; - TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object); - EmpathyPresenceManagerPriv *priv; - TpConnectionPresenceType state; - gchar *status, *status_message; - GList *accounts, *l; - GError *error = NULL; - - /* In case we've been finalized before reading this callback */ - if (idle_singleton == NULL) - return; - - priv = GET_PRIV (idle); - priv->ready = TRUE; - - if (!tp_account_manager_prepare_finish (account_manager, result, &error)) { - DEBUG ("Failed to prepare account manager: %s", error->message); - g_error_free (error); - return; - } - - state = tp_account_manager_get_most_available_presence (priv->manager, - &status, &status_message); - - idle_presence_changed_cb (account_manager, state, status, - status_message, idle); - - accounts = tp_account_manager_get_valid_accounts (priv->manager); - for (l = accounts; l != NULL; l = l->next) { - tp_g_signal_connect_object (l->data, "status-changed", - G_CALLBACK (account_status_changed_cb), - idle, 0); - } - g_list_free (accounts); - - g_free (status); - g_free (status_message); + EmpathyPresenceManager *self = user_data; + TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object); + EmpathyPresenceManagerPriv *priv; + TpConnectionPresenceType state; + gchar *status, *status_message; + GList *accounts, *l; + GError *error = NULL; + + /* In case we've been finalized before reading this callback */ + if (singleton == NULL) + return; + + priv = GET_PRIV (self); + priv->ready = TRUE; + + if (!tp_account_manager_prepare_finish (account_manager, result, &error)) + { + DEBUG ("Failed to prepare account manager: %s", error->message); + g_error_free (error); + return; + } + + state = tp_account_manager_get_most_available_presence (priv->manager, + &status, &status_message); + + most_available_presence_changed (account_manager, state, status, + status_message, self); + + accounts = tp_account_manager_get_valid_accounts (priv->manager); + for (l = accounts; l != NULL; l = l->next) + { + tp_g_signal_connect_object (l->data, "status-changed", + G_CALLBACK (account_status_changed_cb), self, 0); + } + g_list_free (accounts); + + g_free (status); + g_free (status_message); } static void -empathy_presence_manager_init (EmpathyPresenceManager *idle) +empathy_presence_manager_init (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle, - EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPriv); - TpDBusDaemon *dbus; - - idle->priv = priv; - priv->is_idle = FALSE; - - priv->manager = tp_account_manager_dup (); - - tp_account_manager_prepare_async (priv->manager, NULL, - account_manager_ready_cb, idle); - - priv->idle_presence_changed_id = g_signal_connect (priv->manager, - "most-available-presence-changed", - G_CALLBACK (idle_presence_changed_cb), idle); - - dbus = tp_dbus_daemon_dup (NULL); - - priv->gs_proxy = dbus_g_proxy_new_for_name ( - tp_proxy_get_dbus_connection (dbus), - "org.gnome.SessionManager", - "/org/gnome/SessionManager/Presence", - "org.gnome.SessionManager.Presence"); - if (priv->gs_proxy) { - dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged", - G_TYPE_UINT, G_TYPE_INVALID); - dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged", - G_CALLBACK (idle_session_status_changed_cb), - idle, NULL); - } else { - DEBUG ("Failed to get gs proxy"); - } - - g_object_unref (dbus); - - priv->connectivity = empathy_connectivity_dup_singleton (); - priv->state_change_signal_id = g_signal_connect (priv->connectivity, - "state-change", G_CALLBACK (idle_state_change_cb), idle); - - priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal); + EmpathyPresenceManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPriv); + TpDBusDaemon *dbus; + + self->priv = priv; + priv->is_idle = FALSE; + + priv->manager = tp_account_manager_dup (); + + tp_account_manager_prepare_async (priv->manager, NULL, + account_manager_ready_cb, self); + + priv->most_available_presence_changed_id = g_signal_connect (priv->manager, + "most-available-presence-changed", + G_CALLBACK (most_available_presence_changed), self); + + dbus = tp_dbus_daemon_dup (NULL); + + priv->gs_proxy = dbus_g_proxy_new_for_name ( + tp_proxy_get_dbus_connection (dbus), + "org.gnome.SessionManager", + "/org/gnome/SessionManager/Presence", + "org.gnome.SessionManager.Presence"); + + if (priv->gs_proxy) + { + dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged", + G_TYPE_UINT, G_TYPE_INVALID); + dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged", + G_CALLBACK (session_status_changed_cb), + self, NULL); + } + else + { + DEBUG ("Failed to get gs proxy"); + } + + g_object_unref (dbus); + + priv->connectivity = empathy_connectivity_dup_singleton (); + priv->state_change_signal_id = g_signal_connect (priv->connectivity, + "state-change", G_CALLBACK (state_change_cb), self); + + priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal); } EmpathyPresenceManager * empathy_presence_manager_dup_singleton (void) { - return g_object_new (EMPATHY_TYPE_PRESENCE_MANAGER, NULL); + return g_object_new (EMPATHY_TYPE_PRESENCE_MANAGER, NULL); } TpConnectionPresenceType -empathy_presence_manager_get_state (EmpathyPresenceManager *idle) +empathy_presence_manager_get_state (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (idle); + priv = GET_PRIV (self); - if (G_UNLIKELY (!priv->ready)) - g_critical (G_STRLOC ": %s called before AccountManager ready", - G_STRFUNC); + if (G_UNLIKELY (!priv->ready)) + g_critical (G_STRLOC ": %s called before AccountManager ready", + G_STRFUNC); - return priv->state; + return priv->state; } void -empathy_presence_manager_set_state (EmpathyPresenceManager *idle, - TpConnectionPresenceType state) +empathy_presence_manager_set_state (EmpathyPresenceManager *self, + TpConnectionPresenceType state) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (idle); + priv = GET_PRIV (self); - empathy_presence_manager_set_presence (idle, state, priv->status); + empathy_presence_manager_set_presence (self, state, priv->status); } void -empathy_presence_manager_set_status (EmpathyPresenceManager *idle, - const gchar *status) +empathy_presence_manager_set_status (EmpathyPresenceManager *self, + const gchar *status) { - EmpathyPresenceManagerPriv *priv; + EmpathyPresenceManagerPriv *priv; - priv = GET_PRIV (idle); + priv = GET_PRIV (self); - empathy_presence_manager_set_presence (idle, priv->state, status); + empathy_presence_manager_set_presence (self, priv->state, status); } static void -empathy_presence_manager_do_set_presence (EmpathyPresenceManager *idle, - TpConnectionPresenceType status_type, - const gchar *status_message) +empathy_presence_manager_do_set_presence (EmpathyPresenceManager *self, + TpConnectionPresenceType status_type, + const gchar *status_message) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); - const gchar *status; + EmpathyPresenceManagerPriv *priv = GET_PRIV (self); + const gchar *status; - g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES); + g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES); - status = presence_type_to_status[status_type]; + status = presence_type_to_status[status_type]; - g_return_if_fail (status != NULL); + g_return_if_fail (status != NULL); - /* We possibly should be sure that the account manager is prepared, but - * sometimes this isn't possible, like when exiting. In other words, - * we need a callback to empathy_presence_manager_set_presence to be sure the - * presence is set on all accounts successfully. - * However, in practice, this is fine as we've already prepared the - * account manager here in _init. */ - tp_account_manager_set_all_requested_presences (priv->manager, - status_type, status, status_message); + /* We possibly should be sure that the account manager is prepared, but + * sometimes this isn't possible, like when exiting. In other words, + * we need a callback to empathy_presence_manager_set_presence to be sure the + * presence is set on all accounts successfully. + * However, in practice, this is fine as we've already prepared the + * account manager here in _init. */ + tp_account_manager_set_all_requested_presences (priv->manager, + status_type, status, status_message); } void -empathy_presence_manager_set_presence (EmpathyPresenceManager *idle, - TpConnectionPresenceType state, - const gchar *status) +empathy_presence_manager_set_presence (EmpathyPresenceManager *self, + TpConnectionPresenceType state, + const gchar *status) { - EmpathyPresenceManagerPriv *priv; - const gchar *default_status; - - priv = GET_PRIV (idle); - - DEBUG ("Changing presence to %s (%d)", status, state); - - g_free (priv->requested_status_message); - priv->requested_presence_type = state; - priv->requested_status_message = g_strdup (status); - - /* Do not set translated default messages */ - default_status = empathy_presence_get_default_message (state); - if (!tp_strdiff (status, default_status)) { - status = NULL; - } - - if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE && - !empathy_connectivity_is_online (priv->connectivity)) { - DEBUG ("Empathy is not online"); - - priv->saved_state = state; - if (tp_strdiff (priv->status, status)) { - g_free (priv->saved_status); - priv->saved_status = NULL; - if (!EMP_STR_EMPTY (status)) { - priv->saved_status = g_strdup (status); - } - } - return; - } - - empathy_presence_manager_do_set_presence (idle, state, status); + EmpathyPresenceManagerPriv *priv; + const gchar *default_status; + + priv = GET_PRIV (self); + + DEBUG ("Changing presence to %s (%d)", status, state); + + g_free (priv->requested_status_message); + priv->requested_presence_type = state; + priv->requested_status_message = g_strdup (status); + + /* Do not set translated default messages */ + default_status = empathy_presence_get_default_message (state); + if (!tp_strdiff (status, default_status)) + status = NULL; + + if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE && + !empathy_connectivity_is_online (priv->connectivity)) + { + DEBUG ("Empathy is not online"); + + priv->saved_state = state; + if (tp_strdiff (priv->status, status)) + { + g_free (priv->saved_status); + priv->saved_status = NULL; + if (!EMP_STR_EMPTY (status)) + priv->saved_status = g_strdup (status); + } + return; + } + + empathy_presence_manager_do_set_presence (self, state, status); } gboolean -empathy_presence_manager_get_auto_away (EmpathyPresenceManager *idle) +empathy_presence_manager_get_auto_away (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + EmpathyPresenceManagerPriv *priv = GET_PRIV (self); - return priv->auto_away; + return priv->auto_away; } void -empathy_presence_manager_set_auto_away (EmpathyPresenceManager *idle, - gboolean auto_away) +empathy_presence_manager_set_auto_away (EmpathyPresenceManager *self, + gboolean auto_away) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + EmpathyPresenceManagerPriv *priv = GET_PRIV (self); - priv->auto_away = auto_away; + priv->auto_away = auto_away; - g_object_notify (G_OBJECT (idle), "auto-away"); + g_object_notify (G_OBJECT (self), "auto-away"); } TpConnectionPresenceType -empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *idle, +empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *self, gchar **status, gchar **status_message) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); + EmpathyPresenceManagerPriv *priv = GET_PRIV (self); - if (status != NULL) { - *status = g_strdup (presence_type_to_status[priv->requested_presence_type]); - } + if (status != NULL) + *status = g_strdup (presence_type_to_status[priv->requested_presence_type]); - if (status_message != NULL) { - *status_message = g_strdup (priv->requested_status_message); - } + if (status_message != NULL) + *status_message = g_strdup (priv->requested_status_message); - return priv->requested_presence_type; + return priv->requested_presence_type; } /* This function returns %TRUE if EmpathyPresenceManager considers the account * @account as having just connected recently. Otherwise, it returns * %FALSE. In doubt, %FALSE is returned. */ gboolean -empathy_presence_manager_account_is_just_connected (EmpathyPresenceManager *idle, - TpAccount *account) +empathy_presence_manager_account_is_just_connected ( + EmpathyPresenceManager *self, + TpAccount *account) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (idle); - GTimeVal val; - gpointer ptr; - glong t; + EmpathyPresenceManagerPriv *priv = GET_PRIV (self); + GTimeVal val; + gpointer ptr; + glong t; - if (tp_account_get_connection_status (account, NULL) - != TP_CONNECTION_STATUS_CONNECTED) { - return FALSE; - } + if (tp_account_get_connection_status (account, NULL) + != TP_CONNECTION_STATUS_CONNECTED) + return FALSE; - ptr = g_hash_table_lookup (priv->connect_times, account); + ptr = g_hash_table_lookup (priv->connect_times, account); - if (ptr == NULL) { - return FALSE; - } + if (ptr == NULL) + return FALSE; - t = GPOINTER_TO_INT (ptr); + t = GPOINTER_TO_INT (ptr); - g_get_current_time (&val); + g_get_current_time (&val); - return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS; + return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS; } diff --git a/libempathy/empathy-presence-manager.h b/libempathy/empathy-presence-manager.h index bdbe4cf62..05ce5c6ad 100644 --- a/libempathy/empathy-presence-manager.h +++ b/libempathy/empathy-presence-manager.h @@ -1,6 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* - * Copyright (C) 2007-2008 Collabora Ltd. + * Copyright (C) 2007-2011 Collabora Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -40,34 +39,45 @@ typedef struct _EmpathyPresenceManager EmpathyPresenceManager; typedef struct _EmpathyPresenceManagerClass EmpathyPresenceManagerClass; struct _EmpathyPresenceManager { - GObject parent; - gpointer priv; + GObject parent; + gpointer priv; }; -struct _EmpathyPresenceManagerClass { - GObjectClass parent_class; +struct _EmpathyPresenceManagerClass +{ + GObjectClass parent_class; }; -GType empathy_presence_manager_get_type (void) G_GNUC_CONST; -EmpathyPresenceManager *empathy_presence_manager_dup_singleton (void); -TpConnectionPresenceType empathy_presence_manager_get_state (EmpathyPresenceManager *idle); -void empathy_presence_manager_set_state (EmpathyPresenceManager *idle, - TpConnectionPresenceType state); -void empathy_presence_manager_set_status (EmpathyPresenceManager *idle, - const gchar *status); -void empathy_presence_manager_set_presence (EmpathyPresenceManager *idle, - TpConnectionPresenceType state, - const gchar *status); -gboolean empathy_presence_manager_get_auto_away (EmpathyPresenceManager *idle); -void empathy_presence_manager_set_auto_away (EmpathyPresenceManager *idle, - gboolean auto_away); - -TpConnectionPresenceType empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *idle, - gchar **status, - gchar **status_message); - -gboolean empathy_presence_manager_account_is_just_connected (EmpathyPresenceManager *idle, - TpAccount *account); +GType empathy_presence_manager_get_type (void) G_GNUC_CONST; + +EmpathyPresenceManager * empathy_presence_manager_dup_singleton (void); + +TpConnectionPresenceType empathy_presence_manager_get_state ( + EmpathyPresenceManager *self); + +void empathy_presence_manager_set_state (EmpathyPresenceManager *self, + TpConnectionPresenceType state); + +void empathy_presence_manager_set_status (EmpathyPresenceManager *self, + const gchar *status); + +void empathy_presence_manager_set_presence (EmpathyPresenceManager *self, + TpConnectionPresenceType state, + const gchar *status); + +gboolean empathy_presence_manager_get_auto_away (EmpathyPresenceManager *self); + +void empathy_presence_manager_set_auto_away (EmpathyPresenceManager *self, + gboolean auto_away); + +TpConnectionPresenceType empathy_presence_manager_get_requested_presence ( + EmpathyPresenceManager *self, + gchar **status, + gchar **status_message); + +gboolean empathy_presence_manager_account_is_just_connected ( + EmpathyPresenceManager *self, + TpAccount *account); G_END_DECLS -- cgit v1.2.3 From 7e877988467139f1d2bee59a68f28436620d0a0f Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 25 Jan 2011 12:16:28 +0100 Subject: use the self->priv pattern rather than GET_PRIV() --- libempathy/empathy-presence-manager.c | 279 +++++++++++++--------------------- libempathy/empathy-presence-manager.h | 3 +- 2 files changed, 111 insertions(+), 171 deletions(-) (limited to 'libempathy') diff --git a/libempathy/empathy-presence-manager.c b/libempathy/empathy-presence-manager.c index 059934649..2adc3921f 100644 --- a/libempathy/empathy-presence-manager.c +++ b/libempathy/empathy-presence-manager.c @@ -44,9 +44,7 @@ * for. */ #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10 -#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceManager) - -typedef struct +struct _EmpathyPresenceManagerPrivate { DBusGProxy *gs_proxy; EmpathyConnectivity *connectivity; @@ -73,8 +71,7 @@ typedef struct TpConnectionPresenceType requested_presence_type; gchar *requested_status_message; - -} EmpathyPresenceManagerPriv; +}; typedef enum { @@ -117,10 +114,6 @@ most_available_presence_changed (TpAccountManager *manager, gchar *status_message, EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) /* Assume our presence is offline if MC reports UNSET */ state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE; @@ -128,12 +121,12 @@ most_available_presence_changed (TpAccountManager *manager, DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state, status_message); - g_free (priv->status); - priv->state = state; + g_free (self->priv->status); + self->priv->state = state; if (EMP_STR_EMPTY (status_message)) - priv->status = NULL; + self->priv->status = NULL; else - priv->status = g_strdup (status_message); + self->priv->status = g_strdup (status_message); g_object_notify (G_OBJECT (self), "state"); g_object_notify (G_OBJECT (self), "status"); @@ -142,14 +135,10 @@ most_available_presence_changed (TpAccountManager *manager, static gboolean ext_away_cb (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - DEBUG ("Going to extended autoaway"); empathy_presence_manager_set_state (self, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY); - priv->ext_away_timeout = 0; + self->priv->ext_away_timeout = 0; return FALSE; } @@ -157,28 +146,20 @@ ext_away_cb (EmpathyPresenceManager *self) static void next_away_stop (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - - if (priv->ext_away_timeout) + if (self->priv->ext_away_timeout) { - g_source_remove (priv->ext_away_timeout); - priv->ext_away_timeout = 0; + g_source_remove (self->priv->ext_away_timeout); + self->priv->ext_away_timeout = 0; } } static void ext_away_start (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - - if (priv->ext_away_timeout != 0) + if (self->priv->ext_away_timeout != 0) return; - priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME, + self->priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME, (GSourceFunc) ext_away_cb, self); } @@ -187,52 +168,49 @@ session_status_changed_cb (DBusGProxy *gs_proxy, SessionStatus status, EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; gboolean is_idle; - priv = GET_PRIV (self); - is_idle = (status == SESSION_STATUS_IDLE); DEBUG ("Session idle state changed, %s -> %s", - priv->is_idle ? "yes" : "no", + self->priv->is_idle ? "yes" : "no", is_idle ? "yes" : "no"); - if (!priv->auto_away || - (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET && - (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE || - priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) + if (!self->priv->auto_away || + (self->priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET && + (self->priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE || + self->priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) { /* We don't want to go auto away OR we explicitely asked to be * offline, nothing to do here */ - priv->is_idle = is_idle; + self->priv->is_idle = is_idle; return; } - if (is_idle && !priv->is_idle) + if (is_idle && !self->priv->is_idle) { TpConnectionPresenceType new_state; /* We are now idle */ ext_away_start (self); - if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) + if (self->priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) /* We are disconnected, when coming back from away * we want to restore the presence before the * disconnection. */ - priv->away_saved_state = priv->saved_state; + self->priv->away_saved_state = self->priv->saved_state; else - priv->away_saved_state = priv->state; + self->priv->away_saved_state = self->priv->state; new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY; - if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) + if (self->priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY; DEBUG ("Going to autoaway. Saved state=%d, new state=%d", - priv->away_saved_state, new_state); + self->priv->away_saved_state, new_state); empathy_presence_manager_set_state (self, new_state); } - else if (!is_idle && priv->is_idle) + else if (!is_idle && self->priv->is_idle) { /* We are no more idle, restore state */ @@ -243,12 +221,13 @@ session_status_changed_cb (DBusGProxy *gs_proxy, * didn't notify us of the state change to idle, and as a * result, we couldn't save the current state at that time. */ - if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) + if (self->priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { DEBUG ("Restoring state to %d", - priv->away_saved_state); + self->priv->away_saved_state); - empathy_presence_manager_set_state (self, priv->away_saved_state); + empathy_presence_manager_set_state (self, + self->priv->away_saved_state); } else { @@ -257,10 +236,10 @@ session_status_changed_cb (DBusGProxy *gs_proxy, "As a result, I'm not trying to set presence"); } - priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; + self->priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; } - priv->is_idle = is_idle; + self->priv->is_idle = is_idle; } static void @@ -268,64 +247,58 @@ state_change_cb (EmpathyConnectivity *connectivity, gboolean new_online, EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - if (!new_online) { /* We are no longer connected */ DEBUG ("Disconnected: Save state %d (%s)", - priv->state, priv->status); - priv->saved_state = priv->state; - g_free (priv->saved_status); - priv->saved_status = g_strdup (priv->status); + self->priv->state, self->priv->status); + self->priv->saved_state = self->priv->state; + g_free (self->priv->saved_status); + self->priv->saved_status = g_strdup (self->priv->status); empathy_presence_manager_set_state (self, TP_CONNECTION_PRESENCE_TYPE_OFFLINE); } else if (new_online - && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) + && self->priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) { /* We are now connected */ DEBUG ("Reconnected: Restore state %d (%s)", - priv->saved_state, priv->saved_status); + self->priv->saved_state, self->priv->saved_status); empathy_presence_manager_set_presence (self, - priv->saved_state, - priv->saved_status); - priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; - g_free (priv->saved_status); - priv->saved_status = NULL; + self->priv->saved_state, + self->priv->saved_status); + self->priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET; + g_free (self->priv->saved_status); + self->priv->saved_status = NULL; } } static void presence_manager_finalize (GObject *object) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (object); + EmpathyPresenceManager *self = (EmpathyPresenceManager *) object; - g_free (priv->status); - g_free (priv->requested_status_message); + g_free (self->priv->status); + g_free (self->priv->requested_status_message); - if (priv->gs_proxy) - g_object_unref (priv->gs_proxy); + if (self->priv->gs_proxy) + g_object_unref (self->priv->gs_proxy); - g_signal_handler_disconnect (priv->connectivity, - priv->state_change_signal_id); - priv->state_change_signal_id = 0; + g_signal_handler_disconnect (self->priv->connectivity, + self->priv->state_change_signal_id); + self->priv->state_change_signal_id = 0; - if (priv->manager != NULL) + if (self->priv->manager != NULL) { - g_signal_handler_disconnect (priv->manager, - priv->most_available_presence_changed_id); - g_object_unref (priv->manager); + g_signal_handler_disconnect (self->priv->manager, + self->priv->most_available_presence_changed_id); + g_object_unref (self->priv->manager); } - g_object_unref (priv->connectivity); + g_object_unref (self->priv->connectivity); - g_hash_table_destroy (priv->connect_times); - priv->connect_times = NULL; + g_hash_table_destroy (self->priv->connect_times); + self->priv->connect_times = NULL; next_away_stop (EMPATHY_PRESENCE_MANAGER (object)); } @@ -356,18 +329,14 @@ presence_manager_constructor (GType type, static const gchar * empathy_presence_manager_get_status (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - - if (G_UNLIKELY (!priv->ready)) + if (G_UNLIKELY (!self->priv->ready)) g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC); - if (!priv->status) - return empathy_presence_get_default_message (priv->state); + if (!self->priv->status) + return empathy_presence_get_default_message (self->priv->state); - return priv->status; + return self->priv->status; } static void @@ -376,11 +345,7 @@ presence_manager_get_property (GObject *object, GValue *value, GParamSpec *pspec) { - EmpathyPresenceManagerPriv *priv; - EmpathyPresenceManager *self; - - priv = GET_PRIV (object); - self = EMPATHY_PRESENCE_MANAGER (object); + EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object); switch (param_id) { @@ -406,11 +371,7 @@ presence_manager_set_property (GObject *object, const GValue *value, GParamSpec *pspec) { - EmpathyPresenceManagerPriv *priv; - EmpathyPresenceManager *self; - - priv = GET_PRIV (object); - self = EMPATHY_PRESENCE_MANAGER (object); + EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object); switch (param_id) { @@ -460,7 +421,8 @@ empathy_presence_manager_class_init (EmpathyPresenceManagerClass *klass) FALSE, G_PARAM_READWRITE)); - g_type_class_add_private (object_class, sizeof (EmpathyPresenceManagerPriv)); + g_type_class_add_private (object_class, + sizeof (EmpathyPresenceManagerPrivate)); } static void @@ -473,18 +435,17 @@ account_status_changed_cb (TpAccount *account, gpointer user_data) { EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (user_data); - EmpathyPresenceManagerPriv *priv = GET_PRIV (self); GTimeVal val; if (new_status == TP_CONNECTION_STATUS_CONNECTED) { g_get_current_time (&val); - g_hash_table_insert (priv->connect_times, account, + g_hash_table_insert (self->priv->connect_times, account, GINT_TO_POINTER (val.tv_sec)); } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) { - g_hash_table_remove (priv->connect_times, account); + g_hash_table_remove (self->priv->connect_times, account); } } @@ -495,7 +456,6 @@ account_manager_ready_cb (GObject *source_object, { EmpathyPresenceManager *self = user_data; TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object); - EmpathyPresenceManagerPriv *priv; TpConnectionPresenceType state; gchar *status, *status_message; GList *accounts, *l; @@ -505,8 +465,7 @@ account_manager_ready_cb (GObject *source_object, if (singleton == NULL) return; - priv = GET_PRIV (self); - priv->ready = TRUE; + self->priv->ready = TRUE; if (!tp_account_manager_prepare_finish (account_manager, result, &error)) { @@ -515,13 +474,13 @@ account_manager_ready_cb (GObject *source_object, return; } - state = tp_account_manager_get_most_available_presence (priv->manager, + state = tp_account_manager_get_most_available_presence (self->priv->manager, &status, &status_message); most_available_presence_changed (account_manager, state, status, status_message, self); - accounts = tp_account_manager_get_valid_accounts (priv->manager); + accounts = tp_account_manager_get_valid_accounts (self->priv->manager); for (l = accounts; l != NULL; l = l->next) { tp_g_signal_connect_object (l->data, "status-changed", @@ -536,35 +495,36 @@ account_manager_ready_cb (GObject *source_object, static void empathy_presence_manager_init (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPriv); TpDBusDaemon *dbus; - self->priv = priv; - priv->is_idle = FALSE; + self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPrivate); - priv->manager = tp_account_manager_dup (); + self->priv->is_idle = FALSE; - tp_account_manager_prepare_async (priv->manager, NULL, + self->priv->manager = tp_account_manager_dup (); + + tp_account_manager_prepare_async (self->priv->manager, NULL, account_manager_ready_cb, self); - priv->most_available_presence_changed_id = g_signal_connect (priv->manager, - "most-available-presence-changed", - G_CALLBACK (most_available_presence_changed), self); + self->priv->most_available_presence_changed_id = g_signal_connect ( + self->priv->manager, + "most-available-presence-changed", + G_CALLBACK (most_available_presence_changed), self); dbus = tp_dbus_daemon_dup (NULL); - priv->gs_proxy = dbus_g_proxy_new_for_name ( + self->priv->gs_proxy = dbus_g_proxy_new_for_name ( tp_proxy_get_dbus_connection (dbus), "org.gnome.SessionManager", "/org/gnome/SessionManager/Presence", "org.gnome.SessionManager.Presence"); - if (priv->gs_proxy) + if (self->priv->gs_proxy) { - dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged", + dbus_g_proxy_add_signal (self->priv->gs_proxy, "StatusChanged", G_TYPE_UINT, G_TYPE_INVALID); - dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged", + dbus_g_proxy_connect_signal (self->priv->gs_proxy, "StatusChanged", G_CALLBACK (session_status_changed_cb), self, NULL); } @@ -575,11 +535,12 @@ empathy_presence_manager_init (EmpathyPresenceManager *self) g_object_unref (dbus); - priv->connectivity = empathy_connectivity_dup_singleton (); - priv->state_change_signal_id = g_signal_connect (priv->connectivity, + self->priv->connectivity = empathy_connectivity_dup_singleton (); + self->priv->state_change_signal_id = g_signal_connect ( + self->priv->connectivity, "state-change", G_CALLBACK (state_change_cb), self); - priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal); + self->priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal); } EmpathyPresenceManager * @@ -591,37 +552,25 @@ empathy_presence_manager_dup_singleton (void) TpConnectionPresenceType empathy_presence_manager_get_state (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - - if (G_UNLIKELY (!priv->ready)) + if (G_UNLIKELY (!self->priv->ready)) g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC); - return priv->state; + return self->priv->state; } void empathy_presence_manager_set_state (EmpathyPresenceManager *self, TpConnectionPresenceType state) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - - empathy_presence_manager_set_presence (self, state, priv->status); + empathy_presence_manager_set_presence (self, state, self->priv->status); } void empathy_presence_manager_set_status (EmpathyPresenceManager *self, const gchar *status) { - EmpathyPresenceManagerPriv *priv; - - priv = GET_PRIV (self); - - empathy_presence_manager_set_presence (self, priv->state, status); + empathy_presence_manager_set_presence (self, self->priv->state, status); } static void @@ -629,7 +578,6 @@ empathy_presence_manager_do_set_presence (EmpathyPresenceManager *self, TpConnectionPresenceType status_type, const gchar *status_message) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (self); const gchar *status; g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES); @@ -644,7 +592,7 @@ empathy_presence_manager_do_set_presence (EmpathyPresenceManager *self, * presence is set on all accounts successfully. * However, in practice, this is fine as we've already prepared the * account manager here in _init. */ - tp_account_manager_set_all_requested_presences (priv->manager, + tp_account_manager_set_all_requested_presences (self->priv->manager, status_type, status, status_message); } @@ -653,16 +601,13 @@ empathy_presence_manager_set_presence (EmpathyPresenceManager *self, TpConnectionPresenceType state, const gchar *status) { - EmpathyPresenceManagerPriv *priv; const gchar *default_status; - priv = GET_PRIV (self); - DEBUG ("Changing presence to %s (%d)", status, state); - g_free (priv->requested_status_message); - priv->requested_presence_type = state; - priv->requested_status_message = g_strdup (status); + g_free (self->priv->requested_status_message); + self->priv->requested_presence_type = state; + self->priv->requested_status_message = g_strdup (status); /* Do not set translated default messages */ default_status = empathy_presence_get_default_message (state); @@ -670,17 +615,17 @@ empathy_presence_manager_set_presence (EmpathyPresenceManager *self, status = NULL; if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE && - !empathy_connectivity_is_online (priv->connectivity)) + !empathy_connectivity_is_online (self->priv->connectivity)) { DEBUG ("Empathy is not online"); - priv->saved_state = state; - if (tp_strdiff (priv->status, status)) + self->priv->saved_state = state; + if (tp_strdiff (self->priv->status, status)) { - g_free (priv->saved_status); - priv->saved_status = NULL; + g_free (self->priv->saved_status); + self->priv->saved_status = NULL; if (!EMP_STR_EMPTY (status)) - priv->saved_status = g_strdup (status); + self->priv->saved_status = g_strdup (status); } return; } @@ -691,18 +636,14 @@ empathy_presence_manager_set_presence (EmpathyPresenceManager *self, gboolean empathy_presence_manager_get_auto_away (EmpathyPresenceManager *self) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (self); - - return priv->auto_away; + return self->priv->auto_away; } void empathy_presence_manager_set_auto_away (EmpathyPresenceManager *self, gboolean auto_away) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (self); - - priv->auto_away = auto_away; + self->priv->auto_away = auto_away; g_object_notify (G_OBJECT (self), "auto-away"); } @@ -712,15 +653,14 @@ empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *self, gchar **status, gchar **status_message) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (self); - if (status != NULL) - *status = g_strdup (presence_type_to_status[priv->requested_presence_type]); + *status = g_strdup (presence_type_to_status[ + self->priv->requested_presence_type]); if (status_message != NULL) - *status_message = g_strdup (priv->requested_status_message); + *status_message = g_strdup (self->priv->requested_status_message); - return priv->requested_presence_type; + return self->priv->requested_presence_type; } /* This function returns %TRUE if EmpathyPresenceManager considers the account @@ -731,7 +671,6 @@ empathy_presence_manager_account_is_just_connected ( EmpathyPresenceManager *self, TpAccount *account) { - EmpathyPresenceManagerPriv *priv = GET_PRIV (self); GTimeVal val; gpointer ptr; glong t; @@ -740,7 +679,7 @@ empathy_presence_manager_account_is_just_connected ( != TP_CONNECTION_STATUS_CONNECTED) return FALSE; - ptr = g_hash_table_lookup (priv->connect_times, account); + ptr = g_hash_table_lookup (self->priv->connect_times, account); if (ptr == NULL) return FALSE; diff --git a/libempathy/empathy-presence-manager.h b/libempathy/empathy-presence-manager.h index 05ce5c6ad..63599cbfd 100644 --- a/libempathy/empathy-presence-manager.h +++ b/libempathy/empathy-presence-manager.h @@ -37,10 +37,11 @@ G_BEGIN_DECLS typedef struct _EmpathyPresenceManager EmpathyPresenceManager; typedef struct _EmpathyPresenceManagerClass EmpathyPresenceManagerClass; +typedef struct _EmpathyPresenceManagerPrivate EmpathyPresenceManagerPrivate; struct _EmpathyPresenceManager { GObject parent; - gpointer priv; + EmpathyPresenceManagerPrivate *priv; }; struct _EmpathyPresenceManagerClass -- cgit v1.2.3 From 1c3a8da098c9efdd7ece9c8375473f584a374aae Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 25 Jan 2011 12:17:40 +0100 Subject: presence_manager_finalize: chain up --- libempathy/empathy-presence-manager.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'libempathy') diff --git a/libempathy/empathy-presence-manager.c b/libempathy/empathy-presence-manager.c index 2adc3921f..558421a88 100644 --- a/libempathy/empathy-presence-manager.c +++ b/libempathy/empathy-presence-manager.c @@ -301,6 +301,8 @@ presence_manager_finalize (GObject *object) self->priv->connect_times = NULL; next_away_stop (EMPATHY_PRESENCE_MANAGER (object)); + + G_OBJECT_CLASS (empathy_presence_manager_parent_class)->finalize (object); } static GObject * -- cgit v1.2.3 From 273fee5fe68846380765235aae3d9761c349f50c Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 25 Jan 2011 12:21:48 +0100 Subject: presence-manager: add a dispose function and use tp_clear_* --- libempathy/empathy-presence-manager.c | 37 ++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'libempathy') diff --git a/libempathy/empathy-presence-manager.c b/libempathy/empathy-presence-manager.c index 558421a88..48cf5a677 100644 --- a/libempathy/empathy-presence-manager.c +++ b/libempathy/empathy-presence-manager.c @@ -274,34 +274,42 @@ state_change_cb (EmpathyConnectivity *connectivity, } static void -presence_manager_finalize (GObject *object) +presence_manager_dispose (GObject *object) { EmpathyPresenceManager *self = (EmpathyPresenceManager *) object; - g_free (self->priv->status); - g_free (self->priv->requested_status_message); - - if (self->priv->gs_proxy) - g_object_unref (self->priv->gs_proxy); + tp_clear_object (&self->priv->gs_proxy); - g_signal_handler_disconnect (self->priv->connectivity, - self->priv->state_change_signal_id); - self->priv->state_change_signal_id = 0; + if (self->priv->state_change_signal_id != 0) + { + g_signal_handler_disconnect (self->priv->connectivity, + self->priv->state_change_signal_id); + self->priv->state_change_signal_id = 0; + } if (self->priv->manager != NULL) { g_signal_handler_disconnect (self->priv->manager, self->priv->most_available_presence_changed_id); - g_object_unref (self->priv->manager); + tp_clear_object (&self->priv->manager); } - g_object_unref (self->priv->connectivity); - - g_hash_table_destroy (self->priv->connect_times); - self->priv->connect_times = NULL; + tp_clear_object (&self->priv->connectivity); + tp_clear_pointer (&self->priv->connect_times, g_hash_table_unref); next_away_stop (EMPATHY_PRESENCE_MANAGER (object)); + G_OBJECT_CLASS (empathy_presence_manager_parent_class)->dispose (object); +} + +static void +presence_manager_finalize (GObject *object) +{ + EmpathyPresenceManager *self = (EmpathyPresenceManager *) object; + + g_free (self->priv->status); + g_free (self->priv->requested_status_message); + G_OBJECT_CLASS (empathy_presence_manager_parent_class)->finalize (object); } @@ -398,6 +406,7 @@ empathy_presence_manager_class_init (EmpathyPresenceManagerClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->dispose = presence_manager_dispose; object_class->finalize = presence_manager_finalize; object_class->constructor = presence_manager_constructor; object_class->get_property = presence_manager_get_property; -- cgit v1.2.3 From 08d9529b553d70b3fb4b422f2453fc2247a15f20 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 25 Jan 2011 12:25:12 +0100 Subject: use tp_g_signal_connect_object() --- libempathy/empathy-presence-manager.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) (limited to 'libempathy') diff --git a/libempathy/empathy-presence-manager.c b/libempathy/empathy-presence-manager.c index 48cf5a677..c3eae6583 100644 --- a/libempathy/empathy-presence-manager.c +++ b/libempathy/empathy-presence-manager.c @@ -48,7 +48,6 @@ struct _EmpathyPresenceManagerPrivate { DBusGProxy *gs_proxy; EmpathyConnectivity *connectivity; - gulong state_change_signal_id; gboolean ready; @@ -64,7 +63,6 @@ struct _EmpathyPresenceManagerPrivate guint ext_away_timeout; TpAccountManager *manager; - gulong most_available_presence_changed_id; /* pointer to a TpAccount --> glong of time of connection */ GHashTable *connect_times; @@ -279,20 +277,7 @@ presence_manager_dispose (GObject *object) EmpathyPresenceManager *self = (EmpathyPresenceManager *) object; tp_clear_object (&self->priv->gs_proxy); - - if (self->priv->state_change_signal_id != 0) - { - g_signal_handler_disconnect (self->priv->connectivity, - self->priv->state_change_signal_id); - self->priv->state_change_signal_id = 0; - } - - if (self->priv->manager != NULL) - { - g_signal_handler_disconnect (self->priv->manager, - self->priv->most_available_presence_changed_id); - tp_clear_object (&self->priv->manager); - } + tp_clear_object (&self->priv->manager); tp_clear_object (&self->priv->connectivity); tp_clear_pointer (&self->priv->connect_times, g_hash_table_unref); @@ -518,10 +503,9 @@ empathy_presence_manager_init (EmpathyPresenceManager *self) tp_account_manager_prepare_async (self->priv->manager, NULL, account_manager_ready_cb, self); - self->priv->most_available_presence_changed_id = g_signal_connect ( - self->priv->manager, + tp_g_signal_connect_object (self->priv->manager, "most-available-presence-changed", - G_CALLBACK (most_available_presence_changed), self); + G_CALLBACK (most_available_presence_changed), self, 0); dbus = tp_dbus_daemon_dup (NULL); @@ -547,9 +531,9 @@ empathy_presence_manager_init (EmpathyPresenceManager *self) g_object_unref (dbus); self->priv->connectivity = empathy_connectivity_dup_singleton (); - self->priv->state_change_signal_id = g_signal_connect ( - self->priv->connectivity, - "state-change", G_CALLBACK (state_change_cb), self); + + tp_g_signal_connect_object (self->priv->connectivity, + "state-change", G_CALLBACK (state_change_cb), self, 0); self->priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal); } -- cgit v1.2.3