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>
23 #include "empathy-client-factory.h"
25 #include <tp-account-widgets/tpaw-utils.h>
27 #include "empathy-tp-chat.h"
28 #include "empathy-utils.h"
30 G_DEFINE_TYPE (EmpathyClientFactory, empathy_client_factory,
31 TP_TYPE_AUTOMATIC_CLIENT_FACTORY)
33 #define chainup ((TpSimpleClientFactoryClass *) \
34 empathy_client_factory_parent_class)
37 empathy_client_factory_create_channel (TpSimpleClientFactory *factory,
40 const GHashTable *properties,
43 const gchar *chan_type;
45 chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
47 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
49 return TP_CHANNEL (empathy_tp_chat_new (
50 TP_SIMPLE_CLIENT_FACTORY (factory), conn, path,
54 return chainup->create_channel (factory, conn, path, properties, error);
58 empathy_client_factory_dup_channel_features (TpSimpleClientFactory *factory,
64 features = chainup->dup_channel_features (factory, channel);
66 feature = TP_CHANNEL_FEATURE_CONTACTS;
67 g_array_append_val (features, feature);
69 if (EMPATHY_IS_TP_CHAT (channel))
71 feature = TP_TEXT_CHANNEL_FEATURE_CHAT_STATES;
72 g_array_append_val (features, feature);
74 feature = EMPATHY_TP_CHAT_FEATURE_READY;
75 g_array_append_val (features, feature);
82 empathy_client_factory_dup_account_features (TpSimpleClientFactory *factory,
88 features = chainup->dup_account_features (factory, account);
90 feature = TP_ACCOUNT_FEATURE_CONNECTION;
91 g_array_append_val (features, feature);
93 feature = TP_ACCOUNT_FEATURE_ADDRESSING;
94 g_array_append_val (features, feature);
96 feature = TP_ACCOUNT_FEATURE_STORAGE;
97 g_array_append_val (features, feature);
103 empathy_client_factory_dup_connection_features (TpSimpleClientFactory *factory,
104 TpConnection *connection)
109 features = chainup->dup_connection_features (factory, connection);
111 feature = TP_CONNECTION_FEATURE_CAPABILITIES;
112 g_array_append_val (features, feature);
114 feature = TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS;
115 g_array_append_val (features, feature);
117 feature = TP_CONNECTION_FEATURE_CONTACT_INFO;
118 g_array_append_val (features, feature);
120 feature = TP_CONNECTION_FEATURE_BALANCE;
121 g_array_append_val (features, feature);
123 feature = TP_CONNECTION_FEATURE_CONTACT_BLOCKING;
124 g_array_append_val (features, feature);
126 /* Most empathy-* may allow user to add a contact to his contact list. We
127 * need this property to check if the connection allows it. It's cheap to
128 * prepare anyway as it will just call GetAll() on the ContactList iface. */
129 feature = TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES;
130 g_array_append_val (features, feature);
136 empathy_client_factory_dup_contact_features (TpSimpleClientFactory *factory,
137 TpConnection *connection)
140 TpContactFeature extra_features[] = {
141 TP_CONTACT_FEATURE_ALIAS,
142 TP_CONTACT_FEATURE_PRESENCE,
143 TP_CONTACT_FEATURE_AVATAR_TOKEN,
144 TP_CONTACT_FEATURE_AVATAR_DATA,
145 TP_CONTACT_FEATURE_CAPABILITIES,
146 /* Needed by empathy_individual_add_menu_item_new to check if a contact
147 * is already in the contact list. This feature is pretty cheap to
148 * prepare as it doesn't prepare the full roster. */
149 TP_CONTACT_FEATURE_SUBSCRIPTION_STATES,
150 TP_CONTACT_FEATURE_CONTACT_GROUPS,
151 TP_CONTACT_FEATURE_CLIENT_TYPES,
154 features = chainup->dup_contact_features (factory, connection);
156 g_array_append_vals (features, extra_features, G_N_ELEMENTS (extra_features));
162 empathy_client_factory_class_init (EmpathyClientFactoryClass *cls)
164 TpSimpleClientFactoryClass *simple_class = (TpSimpleClientFactoryClass *) cls;
166 simple_class->create_channel = empathy_client_factory_create_channel;
167 simple_class->dup_channel_features =
168 empathy_client_factory_dup_channel_features;
170 simple_class->dup_account_features =
171 empathy_client_factory_dup_account_features;
173 simple_class->dup_connection_features =
174 empathy_client_factory_dup_connection_features;
176 simple_class->dup_contact_features =
177 empathy_client_factory_dup_contact_features;
181 empathy_client_factory_init (EmpathyClientFactory *self)
185 static EmpathyClientFactory *
186 empathy_client_factory_new (TpDBusDaemon *dbus)
188 return g_object_new (EMPATHY_TYPE_CLIENT_FACTORY,
193 EmpathyClientFactory *
194 empathy_client_factory_dup (void)
196 static EmpathyClientFactory *singleton = NULL;
198 GError *error = NULL;
200 if (singleton != NULL)
201 return g_object_ref (singleton);
203 dbus = tp_dbus_daemon_dup (&error);
206 g_warning ("Failed to get TpDBusDaemon: %s", error->message);
207 g_error_free (error);
211 singleton = empathy_client_factory_new (dbus);
212 g_object_unref (dbus);
214 g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);
220 dup_contact_cb (GObject *source,
221 GAsyncResult *result,
224 GSimpleAsyncResult *my_result = user_data;
225 GError *error = NULL;
228 contact = tp_connection_dup_contact_by_id_finish (TP_CONNECTION (source),
233 g_simple_async_result_take_error (my_result, error);
237 g_simple_async_result_set_op_res_gpointer (my_result,
238 empathy_contact_dup_from_tp_contact (contact), g_object_unref);
240 g_object_unref (contact);
243 g_simple_async_result_complete (my_result);
244 g_object_unref (my_result);
248 empathy_client_factory_dup_contact_by_id_async (
249 EmpathyClientFactory *self,
250 TpConnection *connection,
252 GAsyncReadyCallback callback,
255 GSimpleAsyncResult *result;
258 g_return_if_fail (EMPATHY_IS_CLIENT_FACTORY (self));
259 g_return_if_fail (id != NULL);
261 result = g_simple_async_result_new ((GObject *) self, callback, user_data,
262 empathy_client_factory_dup_contact_by_id_async);
264 features = empathy_client_factory_dup_contact_features (
265 TP_SIMPLE_CLIENT_FACTORY (self), connection);
267 tp_connection_dup_contact_by_id_async (connection, id, features->len,
268 (TpContactFeature * ) features->data, dup_contact_cb, result);
270 g_array_unref (features);
274 empathy_client_factory_dup_contact_by_id_finish (
275 EmpathyClientFactory *self,
276 GAsyncResult *result,
279 tpaw_implement_finish_return_copy_pointer (self,
280 empathy_client_factory_dup_contact_by_id_async, g_object_ref);