#include <telepathy-glib/account-manager.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/util.h>
-#ifdef ENABEL_TPL
-#include <telepathy-logger/contact.h>
-#endif /* ENABLE_TPL */
+
+#include <folks/folks.h>
+#include <folks/folks-telepathy.h>
#if HAVE_GEOCLUE
#include <geoclue/geoclue-geocode.h>
typedef struct {
TpContact *tp_contact;
TpAccount *account;
+ FolksPersona *persona;
gchar *id;
- gchar *name;
+ gchar *alias;
EmpathyAvatar *avatar;
TpConnectionPresenceType presence;
- gchar *presence_message;
guint handle;
EmpathyCapabilities capabilities;
gboolean is_user;
* more fields by searching the address using geoclue.
*/
GHashTable *location;
+ GHashTable *groups;
} EmpathyContactPriv;
static void contact_finalize (GObject *object);
static void set_capabilities_from_tp_caps (EmpathyContact *self,
TpCapabilities *caps);
+static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
+
G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
enum
PROP_0,
PROP_TP_CONTACT,
PROP_ACCOUNT,
+ PROP_PERSONA,
PROP_ID,
- PROP_NAME,
+ PROP_ALIAS,
PROP_AVATAR,
PROP_PRESENCE,
PROP_PRESENCE_MESSAGE,
static guint signals[LAST_SIGNAL];
+/* TpContact* -> EmpathyContact*, both borrowed ref */
+static GHashTable *contacts_table = NULL;
+
static void
tp_contact_notify_cb (TpContact *tp_contact,
GParamSpec *param,
/* Forward property notifications */
if (!tp_strdiff (param->name, "alias"))
- g_object_notify (contact, "name");
+ g_object_notify (contact, "alias");
else if (!tp_strdiff (param->name, "presence-type")) {
TpConnectionPresenceType 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"))
set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
tp_contact_get_capabilities (tp_contact));
}
+ else if (!tp_strdiff (param->name, "avatar-file"))
+ {
+ contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
+ }
+}
+
+static void
+folks_persona_notify_cb (FolksPersona *folks_persona,
+ GParamSpec *param,
+ GObject *contact)
+{
+ if (!tp_strdiff (param->name, "presence-message"))
+ g_object_notify (contact, "presence-message");
}
static void
if (priv->tp_contact)
{
+ g_hash_table_remove (contacts_table, priv->tp_contact);
g_signal_handlers_disconnect_by_func (priv->tp_contact,
tp_contact_notify_cb, object);
g_object_unref (priv->tp_contact);
g_object_unref (priv->account);
priv->account = NULL;
+ if (priv->persona)
+ {
+ g_signal_handlers_disconnect_by_func (priv->persona,
+ folks_persona_notify_cb, object);
+ g_object_unref (priv->persona);
+ }
+ priv->persona = NULL;
+
if (priv->avatar != NULL)
{
empathy_avatar_unref (priv->avatar);
TP_TYPE_ACCOUNT,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class,
+ PROP_PERSONA,
+ g_param_spec_object ("persona",
+ "Persona",
+ "The FolksPersona associated with the contact",
+ FOLKS_TYPE_PERSONA,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
g_object_class_install_property (object_class,
PROP_ID,
g_param_spec_string ("id",
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
- PROP_NAME,
- g_param_spec_string ("name",
- "Contact Name",
- "The name of the contact",
+ PROP_ALIAS,
+ g_param_spec_string ("alias",
+ "Contact alias",
+ "An alias for the contact",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
contact->priv = priv;
priv->location = NULL;
+ priv->groups = NULL;
}
static void
DEBUG ("finalize: %p", object);
- g_free (priv->name);
+ if (priv->groups != NULL)
+ g_hash_table_destroy (priv->groups);
+ g_free (priv->alias);
g_free (priv->id);
- g_free (priv->presence_message);
G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
}
{
EmpathyContactPriv *priv = GET_PRIV (contact);
GHashTable *location;
+ TpHandle self_handle;
+ TpHandle handle;
if (tp_contact == NULL)
return;
set_capabilities_from_tp_caps (contact,
tp_contact_get_capabilities (tp_contact));
+ contact_set_avatar_from_tp_contact (contact);
+
+ /* Set is-user property. Note that it could still be the handle is
+ * different from the connection's self handle, in the case the handle
+ * comes from a group interface. */
+ self_handle = tp_connection_get_self_handle (
+ tp_contact_get_connection (tp_contact));
+ handle = tp_contact_get_handle (tp_contact);
+ empathy_contact_set_is_user (contact, self_handle == handle);
+
g_signal_connect (priv->tp_contact, "notify",
G_CALLBACK (tp_contact_notify_cb), contact);
}
case PROP_ACCOUNT:
g_value_set_object (value, empathy_contact_get_account (contact));
break;
+ case PROP_PERSONA:
+ g_value_set_object (value, empathy_contact_get_persona (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));
+ case PROP_ALIAS:
+ g_value_set_string (value, empathy_contact_get_alias (contact));
break;
case PROP_AVATAR:
g_value_set_boxed (value, empathy_contact_get_avatar (contact));
g_assert (priv->account == NULL);
priv->account = g_value_dup_object (value);
break;
+ case PROP_PERSONA:
+ empathy_contact_set_persona (contact, g_value_get_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));
+ case PROP_ALIAS:
+ empathy_contact_set_alias (contact, g_value_get_string (value));
break;
case PROP_AVATAR:
empathy_contact_set_avatar (contact, g_value_get_boxed (value));
NULL);
}
-#ifdef ENABLE_TPL
EmpathyContact *
empathy_contact_from_tpl_contact (TpAccount *account,
- TplContact *tpl_contact)
+ TplEntity *tpl_entity)
{
EmpathyContact *retval;
gboolean is_user;
- g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
+ g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
- is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
+ is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
retval = g_object_new (EMPATHY_TYPE_CONTACT,
- "id", tpl_contact_get_alias (tpl_contact),
- "name", tpl_contact_get_identifier (tpl_contact),
+ "id", tpl_entity_get_alias (tpl_entity),
+ "alias", tpl_entity_get_identifier (tpl_entity),
"account", account,
"is-user", is_user,
NULL);
- if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
+ if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
empathy_contact_load_avatar_cache (retval,
- tpl_contact_get_avatar_token (tpl_contact));
+ tpl_entity_get_avatar_token (tpl_entity));
return retval;
}
-#endif /* ENABLE_TPL */
EmpathyContact *
empathy_contact_new_for_log (TpAccount *account,
const gchar *id,
- const gchar *name,
+ const gchar *alias,
gboolean is_user)
{
g_return_val_if_fail (id != NULL, NULL);
return g_object_new (EMPATHY_TYPE_CONTACT,
"account", account,
"id", id,
- "name", name,
+ "alias", alias,
"is-user", is_user,
NULL);
}
priv->id = g_strdup (id);
g_object_notify (G_OBJECT (contact), "id");
- if (EMP_STR_EMPTY (priv->name))
- g_object_notify (G_OBJECT (contact), "name");
+ if (EMP_STR_EMPTY (priv->alias))
+ g_object_notify (G_OBJECT (contact), "alias");
}
g_object_unref (contact);
}
const gchar *
-empathy_contact_get_name (EmpathyContact *contact)
+empathy_contact_get_alias (EmpathyContact *contact)
{
EmpathyContactPriv *priv;
const gchar *alias;
if (priv->tp_contact != NULL)
alias = tp_contact_get_alias (priv->tp_contact);
else
- alias = priv->name;
+ alias = priv->alias;
if (!EMP_STR_EMPTY (alias))
return alias;
}
void
-empathy_contact_set_name (EmpathyContact *contact,
- const gchar *name)
+empathy_contact_set_alias (EmpathyContact *contact,
+ const gchar *alias)
{
EmpathyContactPriv *priv;
+ FolksPersona *persona;
g_return_if_fail (EMPATHY_IS_CONTACT (contact));
priv = GET_PRIV (contact);
g_object_ref (contact);
- if (tp_strdiff (name, priv->name))
+
+ /* Set the alias on the persona if possible */
+ persona = empathy_contact_get_persona (contact);
+ if (persona != NULL && FOLKS_IS_ALIAS (persona))
+ {
+ DEBUG ("Setting alias for contact %s to %s",
+ empathy_contact_get_id (contact), alias);
+
+ folks_alias_set_alias (FOLKS_ALIAS (persona), alias);
+ }
+
+ if (tp_strdiff (alias, priv->alias))
{
- g_free (priv->name);
- priv->name = g_strdup (name);
- g_object_notify (G_OBJECT (contact), "name");
+ g_free (priv->alias);
+ priv->alias = g_strdup (alias);
+ g_object_notify (G_OBJECT (contact), "alias");
}
+
g_object_unref (contact);
}
+static void
+groups_change_group_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ FolksGroups *groups = FOLKS_GROUPS (source);
+ GError *error = NULL;
+
+ folks_groups_change_group_finish (groups, result, &error);
+ if (error != NULL)
+ {
+ g_warning ("failed to change group: %s", error->message);
+ g_clear_error (&error);
+ }
+}
+
+void
+empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
+ gboolean is_member)
+{
+ EmpathyContactPriv *priv;
+ FolksPersona *persona;
+
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_if_fail (group != NULL);
+
+ priv = GET_PRIV (contact);
+
+ /* Normally pass through the changes to the persona */
+ persona = empathy_contact_get_persona (contact);
+ if (persona != NULL)
+ {
+ if (FOLKS_IS_GROUPS (persona))
+ folks_groups_change_group (FOLKS_GROUPS (persona), group, is_member,
+ groups_change_group_cb, contact);
+ return;
+ }
+
+ /* If the persona doesn't exist yet, we have to cache the changes until it
+ * does */
+ if (priv->groups == NULL)
+ {
+ priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
+ NULL);
+ }
+
+ g_hash_table_insert (priv->groups, g_strdup (group),
+ GUINT_TO_POINTER (is_member));
+}
+
EmpathyAvatar *
empathy_contact_get_avatar (EmpathyContact *contact)
{
return priv->account;
}
+FolksPersona *
+empathy_contact_get_persona (EmpathyContact *contact)
+{
+ EmpathyContactPriv *priv;
+
+ g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+ priv = GET_PRIV (contact);
+
+ if (priv->persona == NULL && priv->tp_contact != NULL)
+ {
+ /* FIXME: This is disgustingly slow */
+ /* Query for the persona */
+ EmpathyIndividualManager *manager;
+ GList *individuals, *l;
+
+ manager = empathy_individual_manager_dup_singleton ();
+ individuals = empathy_individual_manager_get_members (manager);
+
+ for (l = individuals; l != NULL; l = l->next)
+ {
+ GList *personas, *j;
+ FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
+
+ personas = folks_individual_get_personas (individual);
+ for (j = personas; j != NULL; j = j->next)
+ {
+ TpfPersona *persona = j->data;
+
+ if (TPF_IS_PERSONA (persona))
+ {
+ TpContact *tp_contact = tpf_persona_get_contact (persona);
+
+ if (tp_contact == priv->tp_contact)
+ {
+ /* Found the right persona */
+ priv->persona = g_object_ref (persona);
+ goto finished;
+ }
+ }
+ }
+ }
+
+finished:
+ g_list_free (individuals);
+ g_object_unref (manager);
+ }
+
+ return priv->persona;
+}
+
+void
+empathy_contact_set_persona (EmpathyContact *contact,
+ FolksPersona *persona)
+{
+ EmpathyContactPriv *priv;
+
+ g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+ g_return_if_fail (TPF_IS_PERSONA (persona));
+
+ priv = GET_PRIV (contact);
+
+ if (persona == priv->persona)
+ return;
+
+ if (priv->persona != NULL)
+ g_object_unref (priv->persona);
+ priv->persona = g_object_ref (persona);
+
+ g_signal_connect (priv->persona, "notify",
+ G_CALLBACK (folks_persona_notify_cb), contact);
+
+ g_object_notify (G_OBJECT (contact), "persona");
+
+ /* Set the persona's alias, since ours could've been set using
+ * empathy_contact_set_alias() before we had a persona; this happens when
+ * adding a contact. */
+ empathy_contact_set_alias (contact, priv->alias);
+
+ /* Set the persona's groups */
+ if (priv->groups != NULL)
+ {
+ folks_groups_set_groups (FOLKS_GROUPS (persona), priv->groups);
+ g_hash_table_destroy (priv->groups);
+ priv->groups = NULL;
+ }
+}
+
TpConnection *
empathy_contact_get_connection (EmpathyContact *contact)
{
priv = GET_PRIV (contact);
- if (priv->tp_contact != NULL)
- return tp_contact_get_presence_message (priv->tp_contact);
+ if (priv->persona != NULL)
+ return folks_presence_get_presence_message (FOLKS_PRESENCE (priv->persona));
- return priv->presence_message;
+ return NULL;
}
void
g_return_if_fail (EMPATHY_IS_CONTACT (contact));
- if (!tp_strdiff (message, priv->presence_message))
- return;
-
- g_free (priv->presence_message);
- priv->presence_message = g_strdup (message);
-
- g_object_notify (G_OBJECT (contact), "presence-message");
+ if (priv->persona != NULL)
+ {
+ folks_presence_set_presence_message (FOLKS_PRESENCE (priv->persona),
+ message);
+ }
}
guint
}
gboolean
-empathy_contact_can_use_stream_tube (EmpathyContact *contact)
+empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
{
EmpathyContactPriv *priv;
priv = GET_PRIV (contact);
- return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
+ return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
}
static gchar *
return avatar_file;
}
-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 */
- filename = contact_get_avatar_filename (contact, token);
- avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
- g_strdup (token), filename);
- empathy_contact_set_avatar (contact, avatar);
- empathy_avatar_unref (avatar);
-
- /* Save to cache if not yet in it */
- 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);
- }
-}
-
gboolean
empathy_contact_load_avatar_cache (EmpathyContact *contact,
const gchar *token)
g_free (avatar->data);
g_free (avatar->format);
g_free (avatar->token);
+ g_free (avatar->filename);
g_slice_free (EmpathyAvatar, avatar);
}
}
}
else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
{
- capabilities |= EMPATHY_CAPABILITIES_STREAM_TUBE;
+ const gchar *service;
+
+ service = tp_asv_get_string (fixed_prop,
+ TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
+
+ if (!tp_strdiff (service, "rfb"))
+ capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
}
else if (!tp_strdiff (chan_type,
TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
capabilities = tp_caps_to_capabilities (caps);
empathy_contact_set_capabilities (self, capabilities);
}
+
+static void
+contact_set_avatar_from_tp_contact (EmpathyContact *contact)
+{
+ EmpathyContactPriv *priv = GET_PRIV (contact);
+ const gchar *mime;
+ const gchar *token;
+ GFile *file;
+
+ token = tp_contact_get_avatar_token (priv->tp_contact);
+ mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
+ file = tp_contact_get_avatar_file (priv->tp_contact);
+
+ if (file != NULL)
+ {
+ EmpathyAvatar *avatar;
+ gchar *data;
+ gsize len;
+
+ g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
+ avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
+ g_file_get_path (file));
+ empathy_contact_set_avatar (contact, avatar);
+ empathy_avatar_unref (avatar);
+ }
+ else
+ {
+ empathy_contact_set_avatar (contact, NULL);
+ }
+}
+
+EmpathyContact *
+empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
+{
+ EmpathyContact *contact = NULL;
+
+ g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
+
+ if (contacts_table == NULL)
+ contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
+ else
+ contact = g_hash_table_lookup (contacts_table, tp_contact);
+
+ if (contact == NULL)
+ {
+ contact = empathy_contact_new (tp_contact);
+
+ /* The hash table does not keep any ref.
+ * contact keeps a ref to tp_contact, and is removed from the table in
+ * contact_dispose() */
+ g_hash_table_insert (contacts_table, tp_contact, contact);
+ }
+ else
+ {
+ g_object_ref (contact);
+ }
+
+ return contact;
+}
+