empathy r2050 - trunk/libempathy



Author: xclaesse
Date: Tue Jan  6 16:47:19 2009
New Revision: 2050
URL: http://svn.gnome.org/viewvc/empathy?rev=2050&view=rev

Log:
Add an EmpathyContactMonitor object to monitor changes in the contact
list.

Added:
   trunk/libempathy/empathy-contact-monitor.c
   trunk/libempathy/empathy-contact-monitor.h
   trunk/libempathy/empathy-types.h
Modified:
   trunk/libempathy/Makefile.am
   trunk/libempathy/empathy-contact-list.c
   trunk/libempathy/empathy-contact-list.h

Modified: trunk/libempathy/Makefile.am
==============================================================================
--- trunk/libempathy/Makefile.am	(original)
+++ trunk/libempathy/Makefile.am	Tue Jan  6 16:47:19 2009
@@ -25,6 +25,7 @@
 	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 @@
 	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 @@
 	empathy-tp-roomlist.h			\
 	empathy-tp-tube.h			\
 	empathy-tube-handler.h			\
+	empathy-types.h				\
 	empathy-utils.h
 
 libempathy_includedir = $(includedir)/libempathy/

Modified: trunk/libempathy/empathy-contact-list.c
==============================================================================
--- trunk/libempathy/empathy-contact-list.c	(original)
+++ trunk/libempathy/empathy-contact-list.c	Tue Jan  6 16:47:19 2009
@@ -125,6 +125,18 @@
 	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)
 {

Modified: trunk/libempathy/empathy-contact-list.h
==============================================================================
--- trunk/libempathy/empathy-contact-list.h	(original)
+++ trunk/libempathy/empathy-contact-list.h	Tue Jan  6 16:47:19 2009
@@ -24,6 +24,7 @@
 
 #include <glib-object.h>
 
+#include "empathy-types.h"
 #include "empathy-contact.h"
 #include "empathy-tp-group.h"
 
@@ -34,7 +35,6 @@
 #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 @@
 					       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 @@
 						 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
 

Added: trunk/libempathy/empathy-contact-monitor.c
==============================================================================
--- (empty file)
+++ trunk/libempathy/empathy-contact-monitor.c	Tue Jan  6 16:47:19 2009
@@ -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 <cosimo cecchi collabora co uk>
+ */
+
+#include <glib-object.h>
+
+#include <libmissioncontrol/mc-enum-types.h>
+
+#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;
+}

Added: trunk/libempathy/empathy-contact-monitor.h
==============================================================================
--- (empty file)
+++ trunk/libempathy/empathy-contact-monitor.h	Tue Jan  6 16:47:19 2009
@@ -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 <cosimo cecchi collabora co uk>
+ */
+
+#ifndef __EMPATHY_CONTACT_MONITOR_H__
+#define __EMPATHY_CONTACT_MONITOR_H__
+
+#include <glib-object.h>
+
+#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__ */

Added: trunk/libempathy/empathy-types.h
==============================================================================
--- (empty file)
+++ trunk/libempathy/empathy-types.h	Tue Jan  6 16:47:19 2009
@@ -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 <cosimo cecchi collabora co uk>
+ */
+
+#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



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]