]> git.0d.be Git - empathy.git/blob - libempathy/empathy-client-factory.c
tp-chat: use tp_text_channel_get_chat_state()
[empathy.git] / libempathy / empathy-client-factory.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
18  *
19  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include "empathy-client-factory.h"
25
26 #include "empathy-tp-chat.h"
27 #include "empathy-utils.h"
28
29 G_DEFINE_TYPE (EmpathyClientFactory, empathy_client_factory,
30     TP_TYPE_AUTOMATIC_CLIENT_FACTORY)
31
32 #define chainup ((TpSimpleClientFactoryClass *) \
33     empathy_client_factory_parent_class)
34
35 static TpChannel *
36 empathy_client_factory_create_channel (TpSimpleClientFactory *factory,
37     TpConnection *conn,
38     const gchar *path,
39     const GHashTable *properties,
40     GError **error)
41 {
42   const gchar *chan_type;
43
44   chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
45
46   if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
47     {
48       TpAccount *account;
49
50       account = tp_connection_get_account (conn);
51
52       return TP_CHANNEL (empathy_tp_chat_new (
53             TP_SIMPLE_CLIENT_FACTORY (factory), account, conn, path,
54             properties));
55     }
56
57   return chainup->create_channel (factory, conn, path, properties, error);
58 }
59
60 static GArray *
61 empathy_client_factory_dup_channel_features (TpSimpleClientFactory *factory,
62     TpChannel *channel)
63 {
64   GArray *features;
65   GQuark feature;
66
67   features = chainup->dup_channel_features (factory, channel);
68
69   feature = TP_CHANNEL_FEATURE_CONTACTS;
70   g_array_append_val (features, feature);
71
72   if (EMPATHY_IS_TP_CHAT (channel))
73     {
74       feature = TP_TEXT_CHANNEL_FEATURE_CHAT_STATES;
75       g_array_append_val (features, feature);
76
77       feature = EMPATHY_TP_CHAT_FEATURE_READY;
78       g_array_append_val (features, feature);
79     }
80
81   return features;
82 }
83
84 static GArray *
85 empathy_client_factory_dup_account_features (TpSimpleClientFactory *factory,
86     TpAccount *account)
87 {
88   GArray *features;
89   GQuark feature;
90
91   features = chainup->dup_account_features (factory, account);
92
93   feature = TP_ACCOUNT_FEATURE_CONNECTION;
94   g_array_append_val (features, feature);
95
96   feature = TP_ACCOUNT_FEATURE_ADDRESSING;
97   g_array_append_val (features, feature);
98
99   return features;
100 }
101
102 static GArray *
103 empathy_client_factory_dup_connection_features (TpSimpleClientFactory *factory,
104     TpConnection *connection)
105 {
106   GArray *features;
107   GQuark feature;
108
109   features = chainup->dup_connection_features (factory, connection);
110
111   feature = TP_CONNECTION_FEATURE_CAPABILITIES;
112   g_array_append_val (features, feature);
113
114   feature = TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS;
115   g_array_append_val (features, feature);
116
117   feature = TP_CONNECTION_FEATURE_CONTACT_INFO;
118   g_array_append_val (features, feature);
119
120   feature = TP_CONNECTION_FEATURE_BALANCE;
121   g_array_append_val (features, feature);
122
123   feature = TP_CONNECTION_FEATURE_CONTACT_BLOCKING;
124   g_array_append_val (features, feature);
125
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);
131
132   return features;
133 }
134
135 static GArray *
136 empathy_client_factory_dup_contact_features (TpSimpleClientFactory *factory,
137         TpConnection *connection)
138 {
139   GArray *features;
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   };
151
152   features = chainup->dup_contact_features (factory, connection);
153
154   g_array_append_vals (features, extra_features, G_N_ELEMENTS (extra_features));
155
156   return features;
157 }
158
159 static void
160 empathy_client_factory_class_init (EmpathyClientFactoryClass *cls)
161 {
162   TpSimpleClientFactoryClass *simple_class = (TpSimpleClientFactoryClass *) cls;
163
164   simple_class->create_channel = empathy_client_factory_create_channel;
165   simple_class->dup_channel_features =
166     empathy_client_factory_dup_channel_features;
167
168   simple_class->dup_account_features =
169     empathy_client_factory_dup_account_features;
170
171   simple_class->dup_connection_features =
172     empathy_client_factory_dup_connection_features;
173
174   simple_class->dup_contact_features =
175     empathy_client_factory_dup_contact_features;
176 }
177
178 static void
179 empathy_client_factory_init (EmpathyClientFactory *self)
180 {
181 }
182
183 static EmpathyClientFactory *
184 empathy_client_factory_new (TpDBusDaemon *dbus)
185 {
186     return g_object_new (EMPATHY_TYPE_CLIENT_FACTORY,
187         "dbus-daemon", dbus,
188         NULL);
189 }
190
191 EmpathyClientFactory *
192 empathy_client_factory_dup (void)
193 {
194   static EmpathyClientFactory *singleton = NULL;
195   TpDBusDaemon *dbus;
196   GError *error = NULL;
197
198   if (singleton != NULL)
199     return g_object_ref (singleton);
200
201   dbus = tp_dbus_daemon_dup (&error);
202   if (dbus == NULL)
203     {
204       g_warning ("Failed to get TpDBusDaemon: %s", error->message);
205       g_error_free (error);
206       return NULL;
207     }
208
209   singleton = empathy_client_factory_new (dbus);
210   g_object_unref (dbus);
211
212   g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);
213
214   return singleton;
215 }
216
217 static void
218 dup_contact_cb (GObject *source,
219     GAsyncResult *result,
220     gpointer user_data)
221 {
222   GSimpleAsyncResult *my_result = user_data;
223   GError *error = NULL;
224   TpContact *contact;
225
226   contact = tp_connection_dup_contact_by_id_finish (TP_CONNECTION (source),
227       result, &error);
228
229   if (contact == NULL)
230     {
231       g_simple_async_result_take_error (my_result, error);
232     }
233   else
234     {
235       g_simple_async_result_set_op_res_gpointer (my_result,
236           empathy_contact_dup_from_tp_contact (contact), g_object_unref);
237
238       g_object_unref (contact);
239     }
240
241   g_simple_async_result_complete (my_result);
242   g_object_unref (my_result);
243 }
244
245 void
246 empathy_client_factory_dup_contact_by_id_async (
247     EmpathyClientFactory *self,
248     TpConnection *connection,
249     const gchar *id,
250     GAsyncReadyCallback callback,
251     gpointer user_data)
252 {
253   GSimpleAsyncResult *result;
254   GArray *features;
255
256   g_return_if_fail (EMPATHY_IS_CLIENT_FACTORY (self));
257   g_return_if_fail (id != NULL);
258
259   result = g_simple_async_result_new ((GObject *) self, callback, user_data,
260       empathy_client_factory_dup_contact_by_id_async);
261
262   features = empathy_client_factory_dup_contact_features (
263       TP_SIMPLE_CLIENT_FACTORY (self), connection);
264
265   tp_connection_dup_contact_by_id_async (connection, id, features->len,
266       (TpContactFeature * ) features->data, dup_contact_cb, result);
267
268   g_array_unref (features);
269 }
270
271 EmpathyContact *
272 empathy_client_factory_dup_contact_by_id_finish (
273     EmpathyClientFactory *self,
274     GAsyncResult *result,
275     GError **error)
276 {
277   empathy_implement_finish_return_copy_pointer (self,
278       empathy_client_factory_dup_contact_by_id_async, g_object_ref);
279 }