1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include "empathy-tp-contact-factory.h"
26 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
27 #include "empathy-debug.h"
29 static TpContactFeature contact_features[] = {
30 TP_CONTACT_FEATURE_ALIAS,
31 TP_CONTACT_FEATURE_AVATAR_DATA,
32 TP_CONTACT_FEATURE_PRESENCE,
33 TP_CONTACT_FEATURE_LOCATION,
34 TP_CONTACT_FEATURE_CAPABILITIES,
35 TP_CONTACT_FEATURE_CLIENT_TYPES,
39 EmpathyTpContactFactoryContactCb contact_cb;
43 TpConnection *connection;
44 GetContactsCb callback;
46 GDestroyNotify destroy;
50 get_contacts_data_free (gpointer user_data)
52 GetContactsData *data = user_data;
55 data->destroy (data->user_data);
57 g_object_unref (data->connection);
59 g_slice_free (GetContactsData, data);
63 get_contact_by_id_cb (TpConnection *connection,
65 TpContact * const *contacts,
66 const gchar * const *requested_ids,
67 GHashTable *failed_id_errors,
72 GetContactsData *data = user_data;
73 EmpathyContact *contact = NULL;
75 if (n_contacts == 1) {
76 contact = empathy_contact_dup_from_tp_contact (contacts[0]);
78 else if (error == NULL) {
82 g_hash_table_iter_init (&iter, failed_id_errors);
83 while (g_hash_table_iter_next (&iter, NULL, &value)) {
91 if (data->callback.contact_cb) {
92 data->callback.contact_cb (data->connection,
95 data->user_data, weak_object);
99 g_object_unref (contact);
102 /* The callback is NOT given a reference to the EmpathyContact objects */
104 empathy_tp_contact_factory_get_from_id (TpConnection *connection,
106 EmpathyTpContactFactoryContactCb callback,
108 GDestroyNotify destroy,
109 GObject *weak_object)
111 GetContactsData *data;
113 g_return_if_fail (TP_IS_CONNECTION (connection));
114 g_return_if_fail (id != NULL);
116 data = g_slice_new (GetContactsData);
117 data->callback.contact_cb = callback;
118 data->user_data = user_data;
119 data->destroy = destroy;
120 data->connection = g_object_ref (connection);
121 tp_connection_get_contacts_by_id (connection,
123 G_N_ELEMENTS (contact_features),
125 get_contact_by_id_cb,
127 (GDestroyNotify) get_contacts_data_free,
131 /* The callback is NOT given a reference to the EmpathyContact objects */
133 get_contact_by_handle_cb (TpConnection *connection,
135 TpContact * const *contacts,
137 const TpHandle *failed,
140 GObject *weak_object)
142 GetContactsData *data = user_data;
143 EmpathyContact *contact = NULL;
146 if (n_contacts == 1) {
147 contact = empathy_contact_dup_from_tp_contact (contacts[0]);
151 /* tp-glib will provide an error only if the whole operation failed,
152 * but not if, for example, the handle was invalid. We create an error
153 * so the caller of empathy_tp_contact_factory_get_from_handle can
154 * rely on the error to check if the operation succeeded or not. */
156 err = g_error_new_literal (TP_ERROR, TP_ERROR_INVALID_HANDLE,
157 "handle is invalid");
160 err = g_error_copy (error);
164 if (data->callback.contact_cb) {
165 data->callback.contact_cb (data->connection,
168 data->user_data, weak_object);
171 g_clear_error (&err);
173 g_object_unref (contact);
177 empathy_tp_contact_factory_get_from_handle (TpConnection *connection,
179 EmpathyTpContactFactoryContactCb callback,
181 GDestroyNotify destroy,
182 GObject *weak_object)
184 GetContactsData *data;
186 g_return_if_fail (TP_IS_CONNECTION (connection));
188 data = g_slice_new (GetContactsData);
189 data->callback.contact_cb = callback;
190 data->user_data = user_data;
191 data->destroy = destroy;
192 data->connection = g_object_ref (connection);
193 tp_connection_get_contacts_by_handle (connection,
195 G_N_ELEMENTS (contact_features),
197 get_contact_by_handle_cb,
199 (GDestroyNotify) get_contacts_data_free,