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>
26 #include <telepathy-glib/telepathy-glib-dbus.h>
28 #include "empathy-tp-chat.h"
29 #include "empathy-utils.h"
31 G_DEFINE_TYPE (EmpathyClientFactory
, empathy_client_factory
,
32 TP_TYPE_AUTOMATIC_CLIENT_FACTORY
)
34 #define chainup ((TpSimpleClientFactoryClass *) \
35 empathy_client_factory_parent_class)
38 empathy_client_factory_create_channel (TpSimpleClientFactory
*factory
,
41 const GHashTable
*properties
,
44 const gchar
*chan_type
;
46 chan_type
= tp_asv_get_string (properties
, TP_PROP_CHANNEL_CHANNEL_TYPE
);
48 if (!tp_strdiff (chan_type
, TP_IFACE_CHANNEL_TYPE_TEXT
))
50 return TP_CHANNEL (empathy_tp_chat_new (
51 TP_SIMPLE_CLIENT_FACTORY (factory
), conn
, path
,
55 return chainup
->create_channel (factory
, conn
, path
, properties
, error
);
59 empathy_client_factory_dup_channel_features (TpSimpleClientFactory
*factory
,
65 features
= chainup
->dup_channel_features (factory
, channel
);
67 feature
= TP_CHANNEL_FEATURE_CONTACTS
;
68 g_array_append_val (features
, feature
);
70 if (EMPATHY_IS_TP_CHAT (channel
))
72 feature
= TP_TEXT_CHANNEL_FEATURE_CHAT_STATES
;
73 g_array_append_val (features
, feature
);
75 feature
= EMPATHY_TP_CHAT_FEATURE_READY
;
76 g_array_append_val (features
, feature
);
83 empathy_client_factory_dup_account_features (TpSimpleClientFactory
*factory
,
89 features
= chainup
->dup_account_features (factory
, account
);
91 feature
= TP_ACCOUNT_FEATURE_CONNECTION
;
92 g_array_append_val (features
, feature
);
94 feature
= TP_ACCOUNT_FEATURE_ADDRESSING
;
95 g_array_append_val (features
, feature
);
97 feature
= TP_ACCOUNT_FEATURE_STORAGE
;
98 g_array_append_val (features
, feature
);
104 empathy_client_factory_dup_connection_features (TpSimpleClientFactory
*factory
,
105 TpConnection
*connection
)
110 features
= chainup
->dup_connection_features (factory
, connection
);
112 feature
= TP_CONNECTION_FEATURE_CAPABILITIES
;
113 g_array_append_val (features
, feature
);
115 feature
= TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS
;
116 g_array_append_val (features
, feature
);
118 feature
= TP_CONNECTION_FEATURE_CONTACT_INFO
;
119 g_array_append_val (features
, feature
);
121 feature
= TP_CONNECTION_FEATURE_BALANCE
;
122 g_array_append_val (features
, feature
);
124 feature
= TP_CONNECTION_FEATURE_CONTACT_BLOCKING
;
125 g_array_append_val (features
, feature
);
127 /* Most empathy-* may allow user to add a contact to his contact list. We
128 * need this property to check if the connection allows it. It's cheap to
129 * prepare anyway as it will just call GetAll() on the ContactList iface. */
130 feature
= TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES
;
131 g_array_append_val (features
, feature
);
137 empathy_client_factory_dup_contact_features (TpSimpleClientFactory
*factory
,
138 TpConnection
*connection
)
141 TpContactFeature extra_features
[] = {
142 TP_CONTACT_FEATURE_ALIAS
,
143 TP_CONTACT_FEATURE_PRESENCE
,
144 TP_CONTACT_FEATURE_AVATAR_TOKEN
,
145 TP_CONTACT_FEATURE_AVATAR_DATA
,
146 TP_CONTACT_FEATURE_CAPABILITIES
,
147 /* Needed by empathy_individual_add_menu_item_new to check if a contact
148 * is already in the contact list. This feature is pretty cheap to
149 * prepare as it doesn't prepare the full roster. */
150 TP_CONTACT_FEATURE_SUBSCRIPTION_STATES
,
151 TP_CONTACT_FEATURE_CONTACT_GROUPS
,
152 TP_CONTACT_FEATURE_CLIENT_TYPES
,
155 features
= chainup
->dup_contact_features (factory
, connection
);
157 g_array_append_vals (features
, extra_features
, G_N_ELEMENTS (extra_features
));
163 empathy_client_factory_class_init (EmpathyClientFactoryClass
*cls
)
165 TpSimpleClientFactoryClass
*simple_class
= (TpSimpleClientFactoryClass
*) cls
;
167 simple_class
->create_channel
= empathy_client_factory_create_channel
;
168 simple_class
->dup_channel_features
=
169 empathy_client_factory_dup_channel_features
;
171 simple_class
->dup_account_features
=
172 empathy_client_factory_dup_account_features
;
174 simple_class
->dup_connection_features
=
175 empathy_client_factory_dup_connection_features
;
177 simple_class
->dup_contact_features
=
178 empathy_client_factory_dup_contact_features
;
182 empathy_client_factory_init (EmpathyClientFactory
*self
)
186 static EmpathyClientFactory
*
187 empathy_client_factory_new (TpDBusDaemon
*dbus
)
189 return g_object_new (EMPATHY_TYPE_CLIENT_FACTORY
,
194 EmpathyClientFactory
*
195 empathy_client_factory_dup (void)
197 static EmpathyClientFactory
*singleton
= NULL
;
199 GError
*error
= NULL
;
201 if (singleton
!= NULL
)
202 return g_object_ref (singleton
);
204 dbus
= tp_dbus_daemon_dup (&error
);
207 g_warning ("Failed to get TpDBusDaemon: %s", error
->message
);
208 g_error_free (error
);
212 singleton
= empathy_client_factory_new (dbus
);
213 g_object_unref (dbus
);
215 g_object_add_weak_pointer (G_OBJECT (singleton
), (gpointer
) &singleton
);
221 dup_contact_cb (GObject
*source
,
222 GAsyncResult
*result
,
225 GSimpleAsyncResult
*my_result
= user_data
;
226 GError
*error
= NULL
;
229 contact
= tp_connection_dup_contact_by_id_finish (TP_CONNECTION (source
),
234 g_simple_async_result_take_error (my_result
, error
);
238 g_simple_async_result_set_op_res_gpointer (my_result
,
239 empathy_contact_dup_from_tp_contact (contact
), g_object_unref
);
241 g_object_unref (contact
);
244 g_simple_async_result_complete (my_result
);
245 g_object_unref (my_result
);
249 empathy_client_factory_dup_contact_by_id_async (
250 EmpathyClientFactory
*self
,
251 TpConnection
*connection
,
253 GAsyncReadyCallback callback
,
256 GSimpleAsyncResult
*result
;
259 g_return_if_fail (EMPATHY_IS_CLIENT_FACTORY (self
));
260 g_return_if_fail (id
!= NULL
);
262 result
= g_simple_async_result_new ((GObject
*) self
, callback
, user_data
,
263 empathy_client_factory_dup_contact_by_id_async
);
265 features
= empathy_client_factory_dup_contact_features (
266 TP_SIMPLE_CLIENT_FACTORY (self
), connection
);
268 tp_connection_dup_contact_by_id_async (connection
, id
, features
->len
,
269 (TpContactFeature
* ) features
->data
, dup_contact_cb
, result
);
271 g_array_unref (features
);
275 empathy_client_factory_dup_contact_by_id_finish (
276 EmpathyClientFactory
*self
,
277 GAsyncResult
*result
,
280 tpaw_implement_finish_return_copy_pointer (self
,
281 empathy_client_factory_dup_contact_by_id_async
, g_object_ref
);