-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/*
- * Copyright (C) 2004-2007 Imendio AB
+ * Copyright (C) 2007-2009 Collabora Ltd.
*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * 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 program is distributed in the hope that it will be useful,
+ * 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
- * General Public License for more details.
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * 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: Mikael Hallendal <micke@imendio.com>
- * Martyn Russell <martyn@imendio.com>
+ * Authors: Xavier Claessens <xclaesse@gmail.com>
+ * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
*/
#include "config.h"
#include <string.h>
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
+
+#include <telepathy-glib/util.h>
+#include <libmissioncontrol/mc-enum-types.h>
#include "empathy-contact.h"
+#include "empathy-account-manager.h"
#include "empathy-utils.h"
-#include "empathy-debug.h"
-
-#define DEBUG_DOMAIN "Contact"
-
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT, EmpathyContactPriv))
+#include "empathy-enum-types.h"
+#include "empathy-marshal.h"
-typedef struct _EmpathyContactPriv EmpathyContactPriv;
+#define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
+#include "empathy-debug.h"
-struct _EmpathyContactPriv {
- gchar *id;
- gchar *name;
- EmpathyAvatar *avatar;
- McAccount *account;
- EmpathyPresence *presence;
- GList *groups;
- EmpathySubscription subscription;
- guint handle;
- gboolean is_user;
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
+typedef struct {
+ TpContact *tp_contact;
+ McAccount *account;
+ gchar *id;
+ gchar *name;
+ EmpathyAvatar *avatar;
+ McPresence presence;
+ gchar *presence_message;
+ guint handle;
+ EmpathyCapabilities capabilities;
+ gboolean is_user;
+ guint hash;
+} EmpathyContactPriv;
+
+static void contact_finalize (GObject *object);
+static void contact_get_property (GObject *object, guint param_id,
+ GValue *value, GParamSpec *pspec);
+static void contact_set_property (GObject *object, guint param_id,
+ const GValue *value, GParamSpec *pspec);
+
+G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
+
+enum
+{
+ PROP_0,
+ PROP_TP_CONTACT,
+ PROP_ACCOUNT,
+ PROP_ID,
+ PROP_NAME,
+ PROP_AVATAR,
+ PROP_PRESENCE,
+ PROP_PRESENCE_MESSAGE,
+ PROP_HANDLE,
+ PROP_CAPABILITIES,
+ PROP_IS_USER,
};
-static void contact_class_init (EmpathyContactClass *class);
-static void contact_init (EmpathyContact *contact);
-static void contact_finalize (GObject *object);
-static void contact_get_property (GObject *object,
- guint param_id,
- GValue *value,
- GParamSpec *pspec);
-static void contact_set_property (GObject *object,
- guint param_id,
- const GValue *value,
- GParamSpec *pspec);
-
enum {
- PROP_0,
- PROP_ID,
- PROP_NAME,
- PROP_AVATAR,
- PROP_ACCOUNT,
- PROP_PRESENCE,
- PROP_GROUPS,
- PROP_SUBSCRIPTION,
- PROP_HANDLE,
- PROP_IS_USER
+ PRESENCE_CHANGED,
+ LAST_SIGNAL
};
-static gpointer parent_class = NULL;
+static guint signals[LAST_SIGNAL];
-GType
-empathy_contact_get_gtype (void)
+static void
+tp_contact_notify_cb (TpContact *tp_contact,
+ GParamSpec *param,
+ GObject *contact)
+{
+ EmpathyContactPriv *priv = GET_PRIV (contact);
+
+ /* Forward property notifications */
+ if (!tp_strdiff (param->name, "alias"))
+ g_object_notify (contact, "name");
+ else if (!tp_strdiff (param->name, "presence-type")) {
+ McPresence presence;
+
+ presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
+ g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, priv->presence);
+ priv->presence = presence;
+ g_object_notify (contact, "presence");
+ }
+ else if (!tp_strdiff (param->name, "presence-message"))
+ g_object_notify (contact, "presence-message");
+ else if (!tp_strdiff (param->name, "identifier"))
+ g_object_notify (contact, "id");
+ else if (!tp_strdiff (param->name, "handle"))
+ g_object_notify (contact, "handle");
+}
+
+static void
+contact_dispose (GObject *object)
{
- static GType type = 0;
+ EmpathyContactPriv *priv = GET_PRIV (object);
- if (!type) {
- static const GTypeInfo info = {
- sizeof (EmpathyContactClass),
- NULL, /* base_init */
- NULL, /* base_finalize */
- (GClassInitFunc) contact_class_init,
- NULL, /* class_finalize */
- NULL, /* class_data */
- sizeof (EmpathyContact),
- 0, /* n_preallocs */
- (GInstanceInitFunc) contact_init
- };
+ if (priv->tp_contact)
+ {
+ g_signal_handlers_disconnect_by_func (priv->tp_contact,
+ tp_contact_notify_cb, object);
+ g_object_unref (priv->tp_contact);
+ }
+ priv->tp_contact = NULL;
- type = g_type_register_static (G_TYPE_OBJECT,
- "EmpathyContact",
- &info, 0);
- }
+ if (priv->account)
+ g_object_unref (priv->account);
+ priv->account = NULL;
- return type;
+ G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
}
static void
-contact_class_init (EmpathyContactClass *class)
-{
- GObjectClass *object_class;
-
- object_class = G_OBJECT_CLASS (class);
- parent_class = g_type_class_peek_parent (class);
-
- object_class->finalize = contact_finalize;
- object_class->get_property = contact_get_property;
- object_class->set_property = contact_set_property;
-
- g_object_class_install_property (object_class,
- PROP_ID,
- g_param_spec_string ("id",
- "Contact id",
- "String identifying contact",
- NULL,
- G_PARAM_READWRITE));
-
- g_object_class_install_property (object_class,
- PROP_NAME,
- g_param_spec_string ("name",
- "Contact Name",
- "The name of the contact",
- NULL,
- G_PARAM_READWRITE));
-
- g_object_class_install_property (object_class,
- PROP_AVATAR,
- g_param_spec_boxed ("avatar",
- "Avatar image",
- "The avatar image",
- EMPATHY_TYPE_AVATAR,
- G_PARAM_READWRITE));
-
- g_object_class_install_property (object_class,
- PROP_ACCOUNT,
- g_param_spec_object ("account",
- "Contact Account",
- "The account associated with the contact",
- MC_TYPE_ACCOUNT,
- G_PARAM_READWRITE));
-
- g_object_class_install_property (object_class,
- PROP_PRESENCE,
- g_param_spec_object ("presence",
- "Contact presence",
- "Presence of contact",
- EMPATHY_TYPE_PRESENCE,
- G_PARAM_READWRITE));
-
- g_object_class_install_property (object_class,
- PROP_GROUPS,
- g_param_spec_pointer ("groups",
- "Contact groups",
- "Groups of contact",
- G_PARAM_READWRITE));
-
- g_object_class_install_property (object_class,
- PROP_SUBSCRIPTION,
- g_param_spec_int ("subscription",
- "Contact Subscription",
- "The subscription status of the contact",
- EMPATHY_SUBSCRIPTION_NONE,
- EMPATHY_SUBSCRIPTION_BOTH,
- EMPATHY_SUBSCRIPTION_NONE,
- G_PARAM_READWRITE));
-
-
- g_object_class_install_property (object_class,
- PROP_HANDLE,
- g_param_spec_uint ("handle",
- "Contact Handle",
- "The handle of the contact",
- 0,
- G_MAXUINT,
- 0,
- G_PARAM_READWRITE));
- g_object_class_install_property (object_class,
- PROP_IS_USER,
- g_param_spec_boolean ("is-user",
- "Contact is-user",
- "Is contact the user",
- FALSE,
- G_PARAM_READWRITE));
-
- g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
+empathy_contact_class_init (EmpathyContactClass *class)
+{
+ GObjectClass *object_class;
+
+ object_class = G_OBJECT_CLASS (class);
+
+ object_class->finalize = contact_finalize;
+ object_class->dispose = contact_dispose;
+ object_class->get_property = contact_get_property;
+ object_class->set_property = contact_set_property;
+
+ g_object_class_install_property (object_class,
+ PROP_TP_CONTACT,
+ g_param_spec_object ("tp-contact",
+ "TpContact",
+ "The TpContact associated with the contact",
+ TP_TYPE_CONTACT,
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_ACCOUNT,
+ g_param_spec_object ("account",
+ "The account",
+ "The account associated with the contact",
+ MC_TYPE_ACCOUNT,
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_ID,
+ g_param_spec_string ("id",
+ "Contact id",
+ "String identifying contact",
+ NULL,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_NAME,
+ g_param_spec_string ("name",
+ "Contact Name",
+ "The name of the contact",
+ NULL,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_AVATAR,
+ g_param_spec_boxed ("avatar",
+ "Avatar image",
+ "The avatar image",
+ EMPATHY_TYPE_AVATAR,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_PRESENCE,
+ g_param_spec_uint ("presence",
+ "Contact presence",
+ "Presence of contact",
+ MC_PRESENCE_UNSET,
+ LAST_MC_PRESENCE,
+ MC_PRESENCE_UNSET,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_PRESENCE_MESSAGE,
+ g_param_spec_string ("presence-message",
+ "Contact presence message",
+ "Presence message of contact",
+ NULL,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_HANDLE,
+ g_param_spec_uint ("handle",
+ "Contact Handle",
+ "The handle of the contact",
+ 0,
+ G_MAXUINT,
+ 0,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_CAPABILITIES,
+ g_param_spec_flags ("capabilities",
+ "Contact Capabilities",
+ "Capabilities of the contact",
+ EMPATHY_TYPE_CAPABILITIES,
+ EMPATHY_CAPABILITIES_UNKNOWN,
+ G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_IS_USER,
+ g_param_spec_boolean ("is-user",
+ "Contact is-user",
+ "Is contact the user",
+ FALSE,
+ G_PARAM_READWRITE));
+
+ signals[PRESENCE_CHANGED] =
+ g_signal_new ("presence-changed",
+ G_TYPE_FROM_CLASS (class),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ _empathy_marshal_VOID__ENUM_ENUM,
+ G_TYPE_NONE,
+ 2, MC_TYPE_PRESENCE,
+ MC_TYPE_PRESENCE);
+
+ g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
}
static void
-contact_init (EmpathyContact *contact)
+empathy_contact_init (EmpathyContact *contact)
{
+ EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
+ EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
+
+ contact->priv = priv;
}
static void
contact_finalize (GObject *object)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- priv = GET_PRIV (object);
+ priv = GET_PRIV (object);
- empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
+ DEBUG ("finalize: %p", object);
- g_free (priv->name);
- g_free (priv->id);
+ g_free (priv->name);
+ g_free (priv->id);
+ g_free (priv->presence_message);
- if (priv->avatar) {
- empathy_avatar_unref (priv->avatar);
- }
+ if (priv->avatar)
+ empathy_avatar_unref (priv->avatar);
- if (priv->presence) {
- g_object_unref (priv->presence);
- }
+ G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
+}
- if (priv->groups) {
- g_list_foreach (priv->groups, (GFunc) g_free, NULL);
- g_list_free (priv->groups);
- }
+static void
+set_tp_contact (EmpathyContact *contact,
+ TpContact *tp_contact)
+{
+ EmpathyContactPriv *priv = GET_PRIV (contact);
- if (priv->account) {
- g_object_unref (priv->account);
- }
+ if (tp_contact == NULL)
+ return;
- (G_OBJECT_CLASS (parent_class)->finalize) (object);
+ g_assert (priv->tp_contact == NULL);
+ priv->tp_contact = g_object_ref (tp_contact);
+ priv->presence = empathy_contact_get_presence (contact);
+
+ g_signal_connect (priv->tp_contact, "notify",
+ G_CALLBACK (tp_contact_notify_cb), contact);
}
static void
-contact_get_property (GObject *object,
- guint param_id,
- GValue *value,
- GParamSpec *pspec)
-{
- EmpathyContactPriv *priv;
-
- priv = GET_PRIV (object);
-
- switch (param_id) {
- case PROP_ID:
- g_value_set_string (value,
- empathy_contact_get_id (EMPATHY_CONTACT (object)));
- break;
- case PROP_NAME:
- g_value_set_string (value,
- empathy_contact_get_name (EMPATHY_CONTACT (object)));
- break;
- case PROP_AVATAR:
- g_value_set_boxed (value, priv->avatar);
- break;
- case PROP_ACCOUNT:
- g_value_set_object (value, priv->account);
- break;
- case PROP_PRESENCE:
- g_value_set_object (value, priv->presence);
- break;
- case PROP_GROUPS:
- g_value_set_pointer (value, priv->groups);
- break;
- case PROP_SUBSCRIPTION:
- g_value_set_int (value, priv->subscription);
- break;
- case PROP_HANDLE:
- g_value_set_uint (value, priv->handle);
- break;
- case PROP_IS_USER:
- g_value_set_boolean (value, priv->is_user);
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
- break;
- };
+contact_get_property (GObject *object,
+ guint param_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ EmpathyContact *contact = EMPATHY_CONTACT (object);
+
+ switch (param_id)
+ {
+ case PROP_TP_CONTACT:
+ g_value_set_object (value, empathy_contact_get_tp_contact (contact));
+ break;
+ case PROP_ACCOUNT:
+ g_value_set_object (value, empathy_contact_get_account (contact));
+ break;
+ case PROP_ID:
+ g_value_set_string (value, empathy_contact_get_id (contact));
+ break;
+ case PROP_NAME:
+ g_value_set_string (value, empathy_contact_get_name (contact));
+ break;
+ case PROP_AVATAR:
+ g_value_set_boxed (value, empathy_contact_get_avatar (contact));
+ break;
+ case PROP_PRESENCE:
+ g_value_set_uint (value, empathy_contact_get_presence (contact));
+ break;
+ case PROP_PRESENCE_MESSAGE:
+ g_value_set_string (value, empathy_contact_get_presence_message (contact));
+ break;
+ case PROP_HANDLE:
+ g_value_set_uint (value, empathy_contact_get_handle (contact));
+ break;
+ case PROP_CAPABILITIES:
+ g_value_set_flags (value, empathy_contact_get_capabilities (contact));
+ break;
+ case PROP_IS_USER:
+ g_value_set_boolean (value, empathy_contact_is_user (contact));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+ break;
+ };
}
static void
-contact_set_property (GObject *object,
- guint param_id,
- const GValue *value,
- GParamSpec *pspec)
-{
- EmpathyContactPriv *priv;
-
- priv = GET_PRIV (object);
-
- switch (param_id) {
- case PROP_ID:
- empathy_contact_set_id (EMPATHY_CONTACT (object),
- g_value_get_string (value));
- break;
- case PROP_NAME:
- empathy_contact_set_name (EMPATHY_CONTACT (object),
- g_value_get_string (value));
- break;
- case PROP_AVATAR:
- empathy_contact_set_avatar (EMPATHY_CONTACT (object),
- g_value_get_boxed (value));
- break;
- case PROP_ACCOUNT:
- empathy_contact_set_account (EMPATHY_CONTACT (object),
- MC_ACCOUNT (g_value_get_object (value)));
- break;
- case PROP_PRESENCE:
- empathy_contact_set_presence (EMPATHY_CONTACT (object),
- EMPATHY_PRESENCE (g_value_get_object (value)));
- break;
- case PROP_GROUPS:
- empathy_contact_set_groups (EMPATHY_CONTACT (object),
- g_value_get_pointer (value));
- break;
- case PROP_SUBSCRIPTION:
- empathy_contact_set_subscription (EMPATHY_CONTACT (object),
- g_value_get_int (value));
- break;
- case PROP_HANDLE:
- empathy_contact_set_handle (EMPATHY_CONTACT (object),
- g_value_get_uint (value));
- break;
- case PROP_IS_USER:
- empathy_contact_set_is_user (EMPATHY_CONTACT (object),
- g_value_get_boolean (value));
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
- break;
- };
+contact_set_property (GObject *object,
+ guint param_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ EmpathyContact *contact = EMPATHY_CONTACT (object);
+ EmpathyContactPriv *priv = GET_PRIV (object);
+
+ switch (param_id)
+ {
+ case PROP_TP_CONTACT:
+ set_tp_contact (contact, g_value_get_object (value));
+ break;
+ case PROP_ACCOUNT:
+ g_assert (priv->account == NULL);
+ priv->account = g_value_dup_object (value);
+ break;
+ case PROP_ID:
+ empathy_contact_set_id (contact, g_value_get_string (value));
+ break;
+ case PROP_NAME:
+ empathy_contact_set_name (contact, g_value_get_string (value));
+ break;
+ case PROP_AVATAR:
+ empathy_contact_set_avatar (contact, g_value_get_boxed (value));
+ break;
+ case PROP_PRESENCE:
+ empathy_contact_set_presence (contact, g_value_get_uint (value));
+ break;
+ case PROP_PRESENCE_MESSAGE:
+ empathy_contact_set_presence_message (contact, g_value_get_string (value));
+ break;
+ case PROP_HANDLE:
+ empathy_contact_set_handle (contact, g_value_get_uint (value));
+ break;
+ case PROP_CAPABILITIES:
+ empathy_contact_set_capabilities (contact, g_value_get_flags (value));
+ break;
+ case PROP_IS_USER:
+ empathy_contact_set_is_user (contact, g_value_get_boolean (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+ break;
+ };
}
EmpathyContact *
-empathy_contact_new (McAccount *account)
+empathy_contact_new (TpContact *tp_contact)
{
- return g_object_new (EMPATHY_TYPE_CONTACT,
- "account", account,
- NULL);
+ g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
+
+ return g_object_new (EMPATHY_TYPE_CONTACT,
+ "tp-contact", tp_contact,
+ NULL);
}
-EmpathyContact *
-empathy_contact_new_full (McAccount *account,
- const gchar *id,
- const gchar *name)
+TpContact *
+empathy_contact_get_tp_contact (EmpathyContact *contact)
{
- return g_object_new (EMPATHY_TYPE_CONTACT,
- "account", account,
- "name", name,
- "id", id,
- NULL);
+ EmpathyContactPriv *priv;
+
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+ priv = GET_PRIV (contact);
+
+ return priv->tp_contact;
}
const gchar *
empathy_contact_get_id (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- if (priv->id) {
- return priv->id;
- }
+ if (priv->tp_contact != NULL)
+ return tp_contact_get_identifier (priv->tp_contact);
- return "";
+ return priv->id;
}
-const gchar *
-empathy_contact_get_name (EmpathyContact *contact)
+void
+empathy_contact_set_id (EmpathyContact *contact,
+ const gchar *id)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
+
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_if_fail (id != NULL);
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
+ priv = GET_PRIV (contact);
- priv = GET_PRIV (contact);
+ /* We temporally ref the contact because it could be destroyed
+ * during the signal emition */
+ g_object_ref (contact);
+ if (tp_strdiff (id, priv->id))
+ {
+ g_free (priv->id);
+ priv->id = g_strdup (id);
- if (G_STR_EMPTY (priv->name)) {
- return empathy_contact_get_id (contact);
- }
+ g_object_notify (G_OBJECT (contact), "id");
+ if (EMP_STR_EMPTY (priv->name))
+ g_object_notify (G_OBJECT (contact), "name");
+ }
- return priv->name;
+ g_object_unref (contact);
}
-EmpathyAvatar *
-empathy_contact_get_avatar (EmpathyContact *contact)
+const gchar *
+empathy_contact_get_name (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
+
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+ priv = GET_PRIV (contact);
- priv = GET_PRIV (contact);
+ if (priv->tp_contact != NULL)
+ return tp_contact_get_alias (priv->tp_contact);
- return priv->avatar;
+ if (EMP_STR_EMPTY (priv->name))
+ return empathy_contact_get_id (contact);
+
+ return priv->name;
}
-McAccount *
-empathy_contact_get_account (EmpathyContact *contact)
+void
+empathy_contact_set_name (EmpathyContact *contact,
+ const gchar *name)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- return priv->account;
+ g_object_ref (contact);
+ if (tp_strdiff (name, priv->name))
+ {
+ g_free (priv->name);
+ priv->name = g_strdup (name);
+ g_object_notify (G_OBJECT (contact), "name");
+ }
+ g_object_unref (contact);
}
-EmpathyPresence *
-empathy_contact_get_presence (EmpathyContact *contact)
+EmpathyAvatar *
+empathy_contact_get_avatar (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- return priv->presence;
+ return priv->avatar;
}
-GList *
-empathy_contact_get_groups (EmpathyContact *contact)
+void
+empathy_contact_set_avatar (EmpathyContact *contact,
+ EmpathyAvatar *avatar)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- return priv->groups;
-}
-
-EmpathySubscription
-empathy_contact_get_subscription (EmpathyContact *contact)
-{
- EmpathyContactPriv *priv;
+ if (priv->avatar == avatar)
+ return;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
- EMPATHY_SUBSCRIPTION_NONE);
+ if (priv->avatar)
+ {
+ empathy_avatar_unref (priv->avatar);
+ priv->avatar = NULL;
+ }
- priv = GET_PRIV (contact);
+ if (avatar)
+ priv->avatar = empathy_avatar_ref (avatar);
- return priv->subscription;
+ g_object_notify (G_OBJECT (contact), "avatar");
}
-guint
-empathy_contact_get_handle (EmpathyContact *contact)
+McAccount *
+empathy_contact_get_account (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- return priv->handle;
+ if (priv->account == NULL && priv->tp_contact != NULL)
+ {
+ EmpathyAccountManager *manager;
+ TpConnection *connection;
+
+ /* FIXME: This assume the account manager already exists */
+ manager = empathy_account_manager_dup_singleton ();
+ connection = tp_contact_get_connection (priv->tp_contact);
+ priv->account = empathy_account_manager_get_account (manager, connection);
+ g_object_ref (priv->account);
+ g_object_unref (manager);
+ }
+
+ return priv->account;
}
-gboolean
-empathy_contact_is_user (EmpathyContact *contact)
+TpConnection *
+empathy_contact_get_connection (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
+
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
+ priv = GET_PRIV (contact);
- priv = GET_PRIV (contact);
+ if (priv->tp_contact != NULL)
+ return tp_contact_get_connection (priv->tp_contact);
- return priv->is_user;
+ return NULL;
}
-void
-empathy_contact_set_id (EmpathyContact *contact,
- const gchar *id)
+static McPresence
+presence_type_to_mc_presence (TpConnectionPresenceType type)
{
- EmpathyContactPriv *priv;
+ switch (type)
+ {
+ case TP_CONNECTION_PRESENCE_TYPE_UNSET:
+ case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
+ case TP_CONNECTION_PRESENCE_TYPE_ERROR:
+ return MC_PRESENCE_UNSET;
+ case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
+ return MC_PRESENCE_OFFLINE;
+ case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
+ return MC_PRESENCE_AVAILABLE;
+ case TP_CONNECTION_PRESENCE_TYPE_AWAY:
+ return MC_PRESENCE_AWAY;
+ case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
+ return MC_PRESENCE_EXTENDED_AWAY;
+ case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
+ return MC_PRESENCE_HIDDEN;
+ case TP_CONNECTION_PRESENCE_TYPE_BUSY:
+ return MC_PRESENCE_DO_NOT_DISTURB;
+ }
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- g_return_if_fail (id != NULL);
+ return MC_PRESENCE_UNSET;
+}
+
+McPresence
+empathy_contact_get_presence (EmpathyContact *contact)
+{
+ EmpathyContactPriv *priv;
- priv = GET_PRIV (contact);
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
- if (priv->id && strcmp (id, priv->id) == 0) {
- return;
- }
+ priv = GET_PRIV (contact);
- g_free (priv->id);
- priv->id = g_strdup (id);
+ if (priv->tp_contact != NULL)
+ return presence_type_to_mc_presence (tp_contact_get_presence_type (
+ priv->tp_contact));
- g_object_notify (G_OBJECT (contact), "id");
+ return priv->presence;
}
void
-empathy_contact_set_name (EmpathyContact *contact,
- const gchar *name)
+empathy_contact_set_presence (EmpathyContact *contact,
+ McPresence presence)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
+ McPresence old_presence;
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- g_return_if_fail (name != NULL);
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- if (priv->name && strcmp (name, priv->name) == 0) {
- return;
- }
+ if (presence == priv->presence)
+ return;
- g_free (priv->name);
- priv->name = g_strdup (name);
+ old_presence = priv->presence;
+ priv->presence = presence;
- g_object_notify (G_OBJECT (contact), "name");
+ g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
+
+ g_object_notify (G_OBJECT (contact), "presence");
}
-void
-empathy_contact_set_avatar (EmpathyContact *contact,
- EmpathyAvatar *avatar)
+const gchar *
+empathy_contact_get_presence_message (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
-
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
-
- priv = GET_PRIV (contact);
+ EmpathyContactPriv *priv;
- if (priv->avatar == avatar) {
- return;
- }
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
- if (priv->avatar) {
- empathy_avatar_unref (priv->avatar);
- priv->avatar = NULL;
- }
+ priv = GET_PRIV (contact);
- if (avatar) {
- priv->avatar = empathy_avatar_ref (avatar);
- }
+ if (priv->tp_contact != NULL)
+ return tp_contact_get_presence_message (priv->tp_contact);
- g_object_notify (G_OBJECT (contact), "avatar");
+ return priv->presence_message;
}
void
-empathy_contact_set_account (EmpathyContact *contact,
- McAccount *account)
+empathy_contact_set_presence_message (EmpathyContact *contact,
+ const gchar *message)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv = GET_PRIV (contact);
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- g_return_if_fail (MC_IS_ACCOUNT (account));
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- priv = GET_PRIV (contact);
+ if (!tp_strdiff (message, priv->presence_message))
+ return;
- if (account == priv->account) {
- return;
- }
+ g_free (priv->presence_message);
+ priv->presence_message = g_strdup (message);
- if (priv->account) {
- g_object_unref (priv->account);
- }
- priv->account = g_object_ref (account);
-
- g_object_notify (G_OBJECT (contact), "account");
+ g_object_notify (G_OBJECT (contact), "presence-message");
}
-void
-empathy_contact_set_presence (EmpathyContact *contact,
- EmpathyPresence *presence)
+guint
+empathy_contact_get_handle (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- if (presence == priv->presence) {
- return;
- }
+ if (priv->tp_contact != NULL)
+ return tp_contact_get_handle (priv->tp_contact);
- if (priv->presence) {
- g_object_unref (priv->presence);
- priv->presence = NULL;
- }
-
- if (presence) {
- priv->presence = g_object_ref (presence);
- }
-
- g_object_notify (G_OBJECT (contact), "presence");
+ return priv->handle;
}
void
-empathy_contact_set_groups (EmpathyContact *contact,
- GList *groups)
+empathy_contact_set_handle (EmpathyContact *contact,
+ guint handle)
{
- EmpathyContactPriv *priv;
- GList *old_groups, *l;
+ EmpathyContactPriv *priv;
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- old_groups = priv->groups;
- priv->groups = NULL;
+ g_object_ref (contact);
+ if (handle != priv->handle)
+ {
+ priv->handle = handle;
+ g_object_notify (G_OBJECT (contact), "handle");
+ }
+ g_object_unref (contact);
+}
+
+EmpathyCapabilities
+empathy_contact_get_capabilities (EmpathyContact *contact)
+{
+ EmpathyContactPriv *priv;
- for (l = groups; l; l = l->next) {
- priv->groups = g_list_append (priv->groups,
- g_strdup (l->data));
- }
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
- g_list_foreach (old_groups, (GFunc) g_free, NULL);
- g_list_free (old_groups);
+ priv = GET_PRIV (contact);
- g_object_notify (G_OBJECT (contact), "groups");
+ return priv->capabilities;
}
void
-empathy_contact_set_subscription (EmpathyContact *contact,
- EmpathySubscription subscription)
+empathy_contact_set_capabilities (EmpathyContact *contact,
+ EmpathyCapabilities capabilities)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- if (priv->subscription == subscription) {
- return;
- }
+ if (priv->capabilities == capabilities)
+ return;
- priv->subscription = subscription;
+ priv->capabilities = capabilities;
- g_object_notify (G_OBJECT (contact), "subscription");
+ g_object_notify (G_OBJECT (contact), "capabilities");
}
-void
-empathy_contact_set_handle (EmpathyContact *contact,
- guint handle)
+gboolean
+empathy_contact_is_user (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- if (priv->handle == handle) {
- return;
- }
-
- priv->handle = handle;
-
- g_object_notify (G_OBJECT (contact), "handle");
+ return priv->is_user;
}
void
empathy_contact_set_is_user (EmpathyContact *contact,
- gboolean is_user)
+ gboolean is_user)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- if (priv->is_user == is_user) {
- return;
- }
+ if (priv->is_user == is_user)
+ return;
- priv->is_user = is_user;
+ priv->is_user = is_user;
- g_object_notify (G_OBJECT (contact), "is-user");
+ g_object_notify (G_OBJECT (contact), "is-user");
}
-void
-empathy_contact_add_group (EmpathyContact *contact,
- const gchar *group)
+gboolean
+empathy_contact_is_online (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
-
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- g_return_if_fail (group != NULL);
-
- priv = GET_PRIV (contact);
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
- if (!g_list_find_custom (priv->groups, group, (GCompareFunc) strcmp)) {
- priv->groups = g_list_prepend (priv->groups, g_strdup (group));
- g_object_notify (G_OBJECT (contact), "groups");
- }
+ return (empathy_contact_get_presence (contact) > MC_PRESENCE_OFFLINE);
}
-void
-empathy_contact_remove_group (EmpathyContact *contact,
- const gchar *group)
+const gchar *
+empathy_contact_get_status (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
- GList *l;
+ const gchar *message;
- g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- g_return_if_fail (group != NULL);
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
- priv = GET_PRIV (contact);
+ message = empathy_contact_get_presence_message (contact);
+ if (!EMP_STR_EMPTY (message))
+ return message;
- l = g_list_find_custom (priv->groups, group, (GCompareFunc) strcmp);
- if (l) {
- g_free (l->data);
- priv->groups = g_list_delete_link (priv->groups, l);
- g_object_notify (G_OBJECT (contact), "groups");
- }
+ return empathy_presence_get_default_message (
+ empathy_contact_get_presence (contact));
}
gboolean
-empathy_contact_is_online (EmpathyContact *contact)
+empathy_contact_can_voip (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
- priv = GET_PRIV (contact);
+ priv = GET_PRIV (contact);
- if (!priv->presence) {
- return FALSE;
- }
-
- return (empathy_presence_get_state (priv->presence) > MC_PRESENCE_OFFLINE);
+ return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
+ EMPATHY_CAPABILITIES_VIDEO);
}
gboolean
-empathy_contact_is_in_group (EmpathyContact *contact,
- const gchar *group)
+empathy_contact_can_send_files (EmpathyContact *contact)
{
- EmpathyContactPriv *priv;
-
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
- g_return_val_if_fail (!G_STR_EMPTY (group), FALSE);
+ EmpathyContactPriv *priv;
- priv = GET_PRIV (contact);
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
- if (g_list_find_custom (priv->groups, group, (GCompareFunc) strcmp)) {
- return TRUE;
- }
+ priv = GET_PRIV (contact);
- return FALSE;
+ return priv->capabilities & EMPATHY_CAPABILITIES_FT;
}
-const gchar *
-empathy_contact_get_status (EmpathyContact *contact)
+static gchar *
+contact_get_avatar_filename (EmpathyContact *contact,
+ const gchar *token)
{
- EmpathyContactPriv *priv;
+ EmpathyContactPriv *priv = GET_PRIV (contact);
+ McAccount *account;
+ gchar *avatar_path;
+ gchar *avatar_file;
+ gchar *token_escaped;
+ gchar *contact_escaped;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
+ if (EMP_STR_EMPTY (priv->id))
+ return NULL;
- priv = GET_PRIV (contact);
+ contact_escaped = tp_escape_as_identifier (priv->id);
+ token_escaped = tp_escape_as_identifier (token);
+ account = empathy_contact_get_account (contact);
- if (priv->presence) {
- const gchar *status;
+ /* FIXME: Do not use the account, but proto/cm instead */
+ avatar_path = g_build_filename (g_get_user_cache_dir (),
+ PACKAGE_NAME,
+ "avatars",
+ mc_account_get_unique_name (account),
+ contact_escaped,
+ NULL);
+ g_mkdir_with_parents (avatar_path, 0700);
- status = empathy_presence_get_status (priv->presence);
- if (!status) {
- McPresence state;
+ avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
- state = empathy_presence_get_state (priv->presence);
- status = empathy_presence_state_get_default_status (state);
- }
+ g_free (contact_escaped);
+ g_free (token_escaped);
+ g_free (avatar_path);
- return status;
- }
+ return avatar_file;
+}
- return empathy_presence_state_get_default_status (MC_PRESENCE_OFFLINE);
+void
+empathy_contact_load_avatar_data (EmpathyContact *contact,
+ const guchar *data,
+ const gsize len,
+ const gchar *format,
+ const gchar *token)
+{
+ EmpathyAvatar *avatar;
+ gchar *filename;
+ GError *error = NULL;
+
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_if_fail (data != NULL);
+ g_return_if_fail (len > 0);
+ g_return_if_fail (format != NULL);
+ g_return_if_fail (!EMP_STR_EMPTY (token));
+
+ /* Load and set the avatar */
+ avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
+ g_strdup (token));
+ empathy_contact_set_avatar (contact, avatar);
+ empathy_avatar_unref (avatar);
+
+ /* Save to cache if not yet in it */
+ filename = contact_get_avatar_filename (contact, token);
+ if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
+ {
+ if (!empathy_avatar_save_to_file (avatar, filename, &error))
+ {
+ DEBUG ("Failed to save avatar in cache: %s",
+ error ? error->message : "No error given");
+ g_clear_error (&error);
+ }
+ else
+ DEBUG ("Avatar saved to %s", filename);
+ }
+ g_free (filename);
}
gboolean
-empathy_contact_equal (gconstpointer v1,
- gconstpointer v2)
+empathy_contact_load_avatar_cache (EmpathyContact *contact,
+ const gchar *token)
+{
+ EmpathyAvatar *avatar = NULL;
+ gchar *filename;
+ gchar *data = NULL;
+ gsize len;
+ GError *error = NULL;
+
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
+ g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
+
+ /* Load the avatar from file if it exists */
+ filename = contact_get_avatar_filename (contact, token);
+ if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
+ {
+ if (!g_file_get_contents (filename, &data, &len, &error))
+ {
+ DEBUG ("Failed to load avatar from cache: %s",
+ error ? error->message : "No error given");
+ g_clear_error (&error);
+ }
+ }
+
+ if (data)
+ {
+ DEBUG ("Avatar loaded from %s", filename);
+ avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
+ empathy_contact_set_avatar (contact, avatar);
+ empathy_avatar_unref (avatar);
+ }
+
+ g_free (filename);
+
+ return data != NULL;
+}
+
+GType
+empathy_avatar_get_type (void)
{
- McAccount *account_a;
- McAccount *account_b;
- const gchar *id_a;
- const gchar *id_b;
+ static GType type_id = 0;
- g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
- g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
+ if (!type_id)
+ {
+ type_id = g_boxed_type_register_static ("EmpathyAvatar",
+ (GBoxedCopyFunc) empathy_avatar_ref,
+ (GBoxedFreeFunc) empathy_avatar_unref);
+ }
- account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
- account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
+ return type_id;
+}
- id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
- id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
+EmpathyAvatar *
+empathy_avatar_new (guchar *data,
+ gsize len,
+ gchar *format,
+ gchar *token)
+{
+ EmpathyAvatar *avatar;
+
+ avatar = g_slice_new0 (EmpathyAvatar);
+ avatar->data = data;
+ avatar->len = len;
+ avatar->format = format;
+ avatar->token = token;
+ avatar->refcount = 1;
- return empathy_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
+ return avatar;
}
-guint
-empathy_contact_hash (gconstpointer key)
+void
+empathy_avatar_unref (EmpathyAvatar *avatar)
{
- EmpathyContactPriv *priv;
- guint hash;
+ g_return_if_fail (avatar != NULL);
- g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
+ avatar->refcount--;
+ if (avatar->refcount == 0)
+ {
+ g_free (avatar->data);
+ g_free (avatar->format);
+ g_free (avatar->token);
+ g_slice_free (EmpathyAvatar, avatar);
+ }
+}
+
+EmpathyAvatar *
+empathy_avatar_ref (EmpathyAvatar *avatar)
+{
+ g_return_val_if_fail (avatar != NULL, NULL);
- priv = GET_PRIV (EMPATHY_CONTACT (key));
+ avatar->refcount++;
- hash = empathy_account_hash (empathy_contact_get_account (EMPATHY_CONTACT (key)));
- hash += g_str_hash (empathy_contact_get_id (EMPATHY_CONTACT (key)));
+ return avatar;
+}
- return hash;
+/**
+ * empathy_avatar_save_to_file:
+ * @avatar: the avatar
+ * @filename: name of a file to write avatar to
+ * @error: return location for a GError, or NULL
+ *
+ * Save the avatar to a file named filename
+ *
+ * Returns: %TRUE on success, %FALSE if an error occurred
+ */
+gboolean
+empathy_avatar_save_to_file (EmpathyAvatar *self,
+ const gchar *filename,
+ GError **error)
+{
+ return g_file_set_contents (filename, self->data, self->len, error);
}