2 * Copyright (C) 2010 Collabora Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA
19 * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
24 #include "empathy-client-factory.h"
26 #include "empathy-tp-chat.h"
27 #include "empathy-utils.h"
29 G_DEFINE_TYPE (EmpathyClientFactory, empathy_client_factory,
30 TP_TYPE_AUTOMATIC_CLIENT_FACTORY)
32 #define chainup ((TpSimpleClientFactoryClass *) \
33 empathy_client_factory_parent_class)
36 empathy_client_factory_create_channel (TpSimpleClientFactory *factory,
39 const GHashTable *properties,
42 const gchar *chan_type;
44 chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
46 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
48 return TP_CHANNEL (empathy_tp_chat_new (
49 TP_SIMPLE_CLIENT_FACTORY (factory), conn, path,
53 return chainup->create_channel (factory, conn, path, properties, error);
57 empathy_client_factory_dup_channel_features (TpSimpleClientFactory *factory,
63 features = chainup->dup_channel_features (factory, channel);
65 feature = TP_CHANNEL_FEATURE_CONTACTS;
66 g_array_append_val (features, feature);
68 if (EMPATHY_IS_TP_CHAT (channel))
70 feature = TP_TEXT_CHANNEL_FEATURE_CHAT_STATES;
71 g_array_append_val (features, feature);
73 feature = EMPATHY_TP_CHAT_FEATURE_READY;
74 g_array_append_val (features, feature);
81 empathy_client_factory_dup_account_features (TpSimpleClientFactory *factory,
87 features = chainup->dup_account_features (factory, account);
89 feature = TP_ACCOUNT_FEATURE_CONNECTION;
90 g_array_append_val (features, feature);
92 feature = TP_ACCOUNT_FEATURE_ADDRESSING;
93 g_array_append_val (features, feature);
95 feature = TP_ACCOUNT_FEATURE_STORAGE;
96 g_array_append_val (features, feature);
102 empathy_client_factory_dup_connection_features (TpSimpleClientFactory *factory,
103 TpConnection *connection)
108 features = chainup->dup_connection_features (factory, connection);
110 feature = TP_CONNECTION_FEATURE_CAPABILITIES;
111 g_array_append_val (features, feature);
113 feature = TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS;
114 g_array_append_val (features, feature);
116 feature = TP_CONNECTION_FEATURE_CONTACT_INFO;
117 g_array_append_val (features, feature);
119 feature = TP_CONNECTION_FEATURE_BALANCE;
120 g_array_append_val (features, feature);
122 feature = TP_CONNECTION_FEATURE_CONTACT_BLOCKING;
123 g_array_append_val (features, feature);
125 /* Most empathy-* may allow user to add a contact to his contact list. We
126 * need this property to check if the connection allows it. It's cheap to
127 * prepare anyway as it will just call GetAll() on the ContactList iface. */
128 feature = TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES;
129 g_array_append_val (features, feature);
135 empathy_client_factory_dup_contact_features (TpSimpleClientFactory *factory,
136 TpConnection *connection)
139 TpContactFeature extra_features[] = {
140 TP_CONTACT_FEATURE_ALIAS,
141 TP_CONTACT_FEATURE_PRESENCE,
142 TP_CONTACT_FEATURE_AVATAR_TOKEN,
143 TP_CONTACT_FEATURE_AVATAR_DATA,
144 TP_CONTACT_FEATURE_CAPABILITIES,
145 /* Needed by empathy_individual_add_menu_item_new to check if a contact
146 * is already in the contact list. This feature is pretty cheap to
147 * prepare as it doesn't prepare the full roster. */
148 TP_CONTACT_FEATURE_SUBSCRIPTION_STATES,
149 TP_CONTACT_FEATURE_CONTACT_GROUPS,
150 TP_CONTACT_FEATURE_CLIENT_TYPES,
153 features = chainup->dup_contact_features (factory, connection);
155 g_array_append_vals (features, extra_features, G_N_ELEMENTS (extra_features));
161 empathy_client_factory_class_init (EmpathyClientFactoryClass *cls)
163 TpSimpleClientFactoryClass *simple_class = (TpSimpleClientFactoryClass *) cls;
165 simple_class->create_channel = empathy_client_factory_create_channel;
166 simple_class->dup_channel_features =
167 empathy_client_factory_dup_channel_features;
169 simple_class->dup_account_features =
170 empathy_client_factory_dup_account_features;
172 simple_class->dup_connection_features =
173 empathy_client_factory_dup_connection_features;
175 simple_class->dup_contact_features =
176 empathy_client_factory_dup_contact_features;
180 empathy_client_factory_init (EmpathyClientFactory *self)
184 static EmpathyClientFactory *
185 empathy_client_factory_new (TpDBusDaemon *dbus)
187 return g_object_new (EMPATHY_TYPE_CLIENT_FACTORY,
192 EmpathyClientFactory *
193 empathy_client_factory_dup (void)
195 static EmpathyClientFactory *singleton = NULL;
197 GError *error = NULL;
199 if (singleton != NULL)
200 return g_object_ref (singleton);
202 dbus = tp_dbus_daemon_dup (&error);
205 g_warning ("Failed to get TpDBusDaemon: %s", error->message);
206 g_error_free (error);
210 singleton = empathy_client_factory_new (dbus);
211 g_object_unref (dbus);
213 g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);
219 dup_contact_cb (GObject *source,
220 GAsyncResult *result,
223 GSimpleAsyncResult *my_result = user_data;
224 GError *error = NULL;
227 contact = tp_connection_dup_contact_by_id_finish (TP_CONNECTION (source),
232 g_simple_async_result_take_error (my_result, error);
236 g_simple_async_result_set_op_res_gpointer (my_result,
237 empathy_contact_dup_from_tp_contact (contact), g_object_unref);
239 g_object_unref (contact);
242 g_simple_async_result_complete (my_result);
243 g_object_unref (my_result);
247 empathy_client_factory_dup_contact_by_id_async (
248 EmpathyClientFactory *self,
249 TpConnection *connection,
251 GAsyncReadyCallback callback,
254 GSimpleAsyncResult *result;
257 g_return_if_fail (EMPATHY_IS_CLIENT_FACTORY (self));
258 g_return_if_fail (id != NULL);
260 result = g_simple_async_result_new ((GObject *) self, callback, user_data,
261 empathy_client_factory_dup_contact_by_id_async);
263 features = empathy_client_factory_dup_contact_features (
264 TP_SIMPLE_CLIENT_FACTORY (self), connection);
266 tp_connection_dup_contact_by_id_async (connection, id, features->len,
267 (TpContactFeature * ) features->data, dup_contact_cb, result);
269 g_array_unref (features);
273 empathy_client_factory_dup_contact_by_id_finish (
274 EmpathyClientFactory *self,
275 GAsyncResult *result,
278 empathy_implement_finish_return_copy_pointer (self,
279 empathy_client_factory_dup_contact_by_id_async, g_object_ref);