From 0004ebaad62474cfa2eb5f5c6bf8f29b6e6e4070 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Tue, 6 Jan 2009 16:47:19 +0000 Subject: Add an EmpathyContactMonitor object to monitor changes in the contact list. svn path=/trunk/; revision=2050 --- libempathy/Makefile.am | 3 + libempathy/empathy-contact-list.c | 12 ++ libempathy/empathy-contact-list.h | 6 +- libempathy/empathy-contact-monitor.c | 396 +++++++++++++++++++++++++++++++++++ libempathy/empathy-contact-monitor.h | 62 ++++++ libempathy/empathy-types.h | 29 +++ 6 files changed, 507 insertions(+), 1 deletion(-) create mode 100644 libempathy/empathy-contact-monitor.c create mode 100644 libempathy/empathy-contact-monitor.h create mode 100644 libempathy/empathy-types.h (limited to 'libempathy') diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am index 9a4535cd6..c1bdd6248 100644 --- a/libempathy/Makefile.am +++ b/libempathy/Makefile.am @@ -25,6 +25,7 @@ libempathy_la_SOURCES = \ empathy-contact-groups.c \ empathy-contact-list.c \ empathy-contact-manager.c \ + empathy-contact-monitor.c \ empathy-debug.c \ empathy-dispatcher.c \ empathy-idle.c \ @@ -67,6 +68,7 @@ libempathy_headers = \ empathy-contact-groups.h \ empathy-contact-list.h \ empathy-contact-manager.h \ + empathy-contact-monitor.h \ empathy-debug.h \ empathy-dispatcher.h \ empathy-idle.h \ @@ -86,6 +88,7 @@ libempathy_headers = \ empathy-tp-roomlist.h \ empathy-tp-tube.h \ empathy-tube-handler.h \ + empathy-types.h \ empathy-utils.h libempathy_includedir = $(includedir)/libempathy/ diff --git a/libempathy/empathy-contact-list.c b/libempathy/empathy-contact-list.c index f0c8826e5..1fe894e99 100644 --- a/libempathy/empathy-contact-list.c +++ b/libempathy/empathy-contact-list.c @@ -125,6 +125,18 @@ empathy_contact_list_get_members (EmpathyContactList *list) return NULL; } +EmpathyContactMonitor * +empathy_contact_list_get_monitor (EmpathyContactList *list) +{ + g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL); + + if (EMPATHY_CONTACT_LIST_GET_IFACE (list)->get_monitor) { + return EMPATHY_CONTACT_LIST_GET_IFACE (list)->get_monitor (list); + } + + return NULL; +} + GList * empathy_contact_list_get_pendings (EmpathyContactList *list) { diff --git a/libempathy/empathy-contact-list.h b/libempathy/empathy-contact-list.h index 0b13031ba..3e90e8cac 100644 --- a/libempathy/empathy-contact-list.h +++ b/libempathy/empathy-contact-list.h @@ -24,6 +24,7 @@ #include +#include "empathy-types.h" #include "empathy-contact.h" #include "empathy-tp-group.h" @@ -34,7 +35,6 @@ G_BEGIN_DECLS #define EMPATHY_IS_CONTACT_LIST(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_CONTACT_LIST)) #define EMPATHY_CONTACT_LIST_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), EMPATHY_TYPE_CONTACT_LIST, EmpathyContactListIface)) -typedef struct _EmpathyContactList EmpathyContactList; typedef struct _EmpathyContactListIface EmpathyContactListIface; struct _EmpathyContactListIface { @@ -63,6 +63,8 @@ struct _EmpathyContactListIface { const gchar *new_group); void (*remove_group) (EmpathyContactList *list, const gchar *group); + EmpathyContactMonitor * + (*get_monitor) (EmpathyContactList *list); }; GType empathy_contact_list_get_type (void) G_GNUC_CONST; @@ -88,6 +90,8 @@ void empathy_contact_list_rename_group (EmpathyContactList *list, const gchar *new_group); void empathy_contact_list_remove_group (EmpathyContactList *list, const gchar *group); +EmpathyContactMonitor * + empathy_contact_list_get_monitor (EmpathyContactList *list); G_END_DECLS diff --git a/libempathy/empathy-contact-monitor.c b/libempathy/empathy-contact-monitor.c new file mode 100644 index 000000000..04a384bbf --- /dev/null +++ b/libempathy/empathy-contact-monitor.c @@ -0,0 +1,396 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 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: Cosimo Cecchi + */ + +#include + +#include + +#include "empathy-contact-monitor.h" +#include "empathy-contact-list.h" + +#include "empathy-contact.h" +#include "empathy-utils.h" +#include "empathy-marshal.h" + +#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactMonitor) + +typedef struct { + EmpathyContactList *proxy; + GPtrArray *contacts; +} EmpathyContactMonitorPriv; + +enum { + CONTACT_ADDED, + CONTACT_AVATAR_CHANGED, + CONTACT_CAPABILITIES_CHANGED, + CONTACT_NAME_CHANGED, + CONTACT_PRESENCE_CHANGED, + CONTACT_PRESENCE_MESSAGE_CHANGED, + CONTACT_REMOVED, + LAST_SIGNAL +}; + +enum { + PROP_0, + PROP_PROXY +}; + +static void contact_remove_foreach (EmpathyContact *contact, + EmpathyContactMonitor *monitor); + +static guint signals[LAST_SIGNAL]; + +G_DEFINE_TYPE (EmpathyContactMonitor, empathy_contact_monitor, G_TYPE_OBJECT); + +static void +do_set_property (GObject *object, + guint param_id, + const GValue *value, + GParamSpec *pspec) +{ + switch (param_id) { + case PROP_PROXY: + empathy_contact_monitor_set_proxy + (EMPATHY_CONTACT_MONITOR (object), + g_value_get_object (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; +} + +static void +do_get_property (GObject *object, + guint param_id, + GValue *value, + GParamSpec *pspec) +{ + EmpathyContactMonitorPriv *priv = GET_PRIV (object); + + switch (param_id) { + case PROP_PROXY: + g_value_set_object (value, priv->proxy); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; +} + +static void +do_finalize (GObject *obj) +{ + EmpathyContactMonitorPriv *priv; + + priv = GET_PRIV (obj); + + if (priv->contacts) { + g_ptr_array_foreach (priv->contacts, + (GFunc) contact_remove_foreach, obj); + g_ptr_array_free (priv->contacts, TRUE); + priv->contacts = NULL; + } + + G_OBJECT_CLASS (empathy_contact_monitor_parent_class)->finalize (obj); +} + +static void +empathy_contact_monitor_class_init (EmpathyContactMonitorClass *klass) +{ + GObjectClass *oclass = G_OBJECT_CLASS (klass); + + oclass->finalize = do_finalize; + oclass->get_property = do_get_property; + oclass->set_property = do_set_property; + + g_object_class_install_property (oclass, + PROP_PROXY, + g_param_spec_object ("proxy", + "Monitor's proxy", + "The contact list associated we're monitoring", + EMPATHY_TYPE_CONTACT_LIST, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); + + signals[CONTACT_ADDED] = + g_signal_new ("contact-added", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, EMPATHY_TYPE_CONTACT); + signals[CONTACT_AVATAR_CHANGED] = + g_signal_new ("contact-avatar-changed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, EMPATHY_TYPE_CONTACT); + signals[CONTACT_CAPABILITIES_CHANGED] = + g_signal_new ("contact-capabilities-changed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, EMPATHY_TYPE_CONTACT); + signals[CONTACT_NAME_CHANGED] = + g_signal_new ("contact-name-changed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + _empathy_marshal_VOID__OBJECT_STRING, + G_TYPE_NONE, + 2, EMPATHY_TYPE_CONTACT, + G_TYPE_STRING); + signals[CONTACT_PRESENCE_CHANGED] = + g_signal_new ("contact-presence-changed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + _empathy_marshal_VOID__OBJECT_ENUM_ENUM, + G_TYPE_NONE, + 3, EMPATHY_TYPE_CONTACT, + MC_TYPE_PRESENCE, + MC_TYPE_PRESENCE); + signals[CONTACT_PRESENCE_MESSAGE_CHANGED] = + g_signal_new ("contact-presence-message-changed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + _empathy_marshal_VOID__OBJECT_STRING, + G_TYPE_NONE, + 2, EMPATHY_TYPE_CONTACT, + G_TYPE_STRING); + signals[CONTACT_REMOVED] = + g_signal_new ("contact-removed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, EMPATHY_TYPE_CONTACT); + + g_type_class_add_private (klass, sizeof (EmpathyContactMonitorPriv)); +} + +static void +empathy_contact_monitor_init (EmpathyContactMonitor *self) +{ + EmpathyContactMonitorPriv *priv = + G_TYPE_INSTANCE_GET_PRIVATE (self, + EMPATHY_TYPE_CONTACT_MONITOR, EmpathyContactMonitorPriv); + + self->priv = priv; + priv->contacts = NULL; + priv->proxy = NULL; +} + +static void +contact_monitor_presence_changed_cb (EmpathyContact *contact, + McPresence current_presence, + McPresence previous_presence, + EmpathyContactMonitor *self) +{ + g_signal_emit (self, signals[CONTACT_PRESENCE_CHANGED], 0, contact, + current_presence, previous_presence); +} + +static void +contact_monitor_presence_message_changed_cb (EmpathyContact *contact, + GParamSpec *pspec, + EmpathyContactMonitor *self) +{ + const char *status; + + /* use the status so that we always have a presence message */ + status = empathy_contact_get_status (contact); + + g_signal_emit (self, signals[CONTACT_PRESENCE_MESSAGE_CHANGED], 0, + contact, status); +} + +static void +contact_monitor_name_changed_cb (EmpathyContact *contact, + GParamSpec *pspec, + EmpathyContactMonitor *self) +{ + const char *name; + + name = empathy_contact_get_name (contact); + + g_signal_emit (self, signals[CONTACT_NAME_CHANGED], 0, contact, name); +} + +static void +contact_monitor_avatar_changed_cb (EmpathyContact *contact, + GParamSpec *pspec, + EmpathyContactMonitor *self) +{ + /* don't emit a pixbuf in the signal, as we don't know how large + * a client would like it to be. + */ + + g_signal_emit (self, signals[CONTACT_AVATAR_CHANGED], 0, contact); +} + +static void +contact_monitor_capabilities_changed_cb (EmpathyContact *contact, + GParamSpec *pspec, + EmpathyContactMonitor *self) +{ + g_signal_emit (self, signals[CONTACT_CAPABILITIES_CHANGED], 0, contact); +} + +static void +contact_add (EmpathyContactMonitor *monitor, + EmpathyContact *contact) +{ + EmpathyContactMonitorPriv *priv = GET_PRIV (monitor); + + g_signal_connect (contact, "presence-changed", + G_CALLBACK (contact_monitor_presence_changed_cb), + monitor); + g_signal_connect (contact, "notify::presence-message", + G_CALLBACK (contact_monitor_presence_message_changed_cb), + monitor); + g_signal_connect (contact, "notify::name", + G_CALLBACK (contact_monitor_name_changed_cb), + monitor); + g_signal_connect (contact, "notify::avatar", + G_CALLBACK (contact_monitor_avatar_changed_cb), + monitor); + g_signal_connect (contact, "notify::capabilities", + G_CALLBACK (contact_monitor_capabilities_changed_cb), + monitor); + + g_ptr_array_add (priv->contacts, g_object_ref (contact)); + + g_signal_emit (monitor, signals[CONTACT_ADDED], 0, contact); +} + +static void +contact_remove (EmpathyContactMonitor *monitor, + EmpathyContact *contact) +{ + EmpathyContactMonitorPriv *priv = GET_PRIV (monitor); + + g_signal_handlers_disconnect_by_func (contact, + G_CALLBACK (contact_monitor_presence_changed_cb), + monitor); + g_signal_handlers_disconnect_by_func (contact, + G_CALLBACK (contact_monitor_presence_message_changed_cb), + monitor); + g_signal_handlers_disconnect_by_func (contact, + G_CALLBACK (contact_monitor_name_changed_cb), + monitor); + g_signal_handlers_disconnect_by_func (contact, + G_CALLBACK (contact_monitor_avatar_changed_cb), + monitor); + g_signal_handlers_disconnect_by_func (contact, + G_CALLBACK (contact_monitor_capabilities_changed_cb), + monitor); + + g_ptr_array_remove (priv->contacts, contact); + + g_signal_emit (monitor, signals[CONTACT_REMOVED], 0, contact); + + g_object_unref (contact); +} + +static void +contact_remove_foreach (EmpathyContact *contact, + EmpathyContactMonitor *monitor) +{ + contact_remove (monitor, contact); +} + +static void +cl_members_changed_cb (EmpathyContactList *cl, + EmpathyContact *contact, + EmpathyContact *actor, + guint reason, + gchar *message, + gboolean is_member, + EmpathyContactMonitor *monitor) +{ + if (is_member) { + contact_add (monitor, contact); + } else { + contact_remove (monitor, contact); + } +} + +/* public methods */ + +void +empathy_contact_monitor_set_proxy (EmpathyContactMonitor *self, + EmpathyContactList *proxy) +{ + EmpathyContactMonitorPriv *priv; + + g_assert (EMPATHY_IS_CONTACT_MONITOR (self)); + g_assert (EMPATHY_IS_CONTACT_LIST (proxy)); + + priv = GET_PRIV (self); + + if (priv->contacts != NULL) { + g_ptr_array_foreach (priv->contacts, + (GFunc) contact_remove_foreach, self); + g_ptr_array_free (priv->contacts, TRUE); + priv->contacts = NULL; + } + + if (priv->proxy != NULL) { + g_object_unref (priv->proxy); + priv->proxy = NULL; + } + + priv->proxy = g_object_ref (proxy); + priv->contacts = g_ptr_array_new (); + + g_signal_connect (proxy, "members-changed", + G_CALLBACK (cl_members_changed_cb), self); +} + +EmpathyContactMonitor * +empathy_contact_monitor_new_for_proxy (EmpathyContactList *proxy) +{ + EmpathyContactMonitor *retval; + + g_assert (EMPATHY_IS_CONTACT_LIST (proxy)); + + retval = g_object_new (EMPATHY_TYPE_CONTACT_MONITOR, + "proxy", proxy, NULL); + + return retval; +} diff --git a/libempathy/empathy-contact-monitor.h b/libempathy/empathy-contact-monitor.h new file mode 100644 index 000000000..e9a060544 --- /dev/null +++ b/libempathy/empathy-contact-monitor.h @@ -0,0 +1,62 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 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: Cosimo Cecchi + */ + +#ifndef __EMPATHY_CONTACT_MONITOR_H__ +#define __EMPATHY_CONTACT_MONITOR_H__ + +#include + +#include "empathy-types.h" + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_CONTACT_MONITOR (empathy_contact_monitor_get_type ()) +#define EMPATHY_CONTACT_MONITOR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_CONTACT_MONITOR, EmpathyContactMonitor)) +#define EMPATHY_CONTACT_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), EMPATHY_TYPE_CONTACT_MONITOR, EmpathyContactMonitorClass)) +#define EMPATHY_IS_CONTACT_MONITOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_CONTACT_MONITOR)) +#define EMPATHY_IS_CONTACT_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_CONTACT_MONITOR)) +#define EMPATHY_CONTACT_MONITOR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_CONTACT_MONITOR, EmpathyContactMonitorClass)) + +typedef struct _EmpathyContactMonitorClass EmpathyContactMonitorClass; + +struct _EmpathyContactMonitor { + GObject parent; + gpointer priv; +}; + +struct _EmpathyContactMonitorClass { + GObjectClass parent_class; +}; + +GType empathy_contact_monitor_get_type (void); + +/* public methods */ + +void +empathy_contact_monitor_set_proxy (EmpathyContactMonitor *self, + EmpathyContactList *proxy); + +EmpathyContactMonitor * +empathy_contact_monitor_new_for_proxy (EmpathyContactList *proxy); + +G_END_DECLS + +#endif /* __EMPATHY_CONTACT_MONITOR_H__ */ diff --git a/libempathy/empathy-types.h b/libempathy/empathy-types.h new file mode 100644 index 000000000..0ca300efa --- /dev/null +++ b/libempathy/empathy-types.h @@ -0,0 +1,29 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 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: Cosimo Cecchi + */ + +#ifndef __EMPATHY_TYPES_H__ +#define __EMPATHY_TYPES_H__ + +typedef struct _EmpathyContactList EmpathyContactList; + +typedef struct _EmpathyContactMonitor EmpathyContactMonitor; + +#endif /* __EMPATHY_TYPES_H__ */ \ No newline at end of file -- cgit v1.2.3