]> git.0d.be Git - empathy.git/blob - libempathy/empathy-client-factory.c
Reorder header inclusions accordingly to the Telepathy coding style
[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 #include "empathy-client-factory.h"
24
25 #include "empathy-tp-chat.h"
26 #include "empathy-utils.h"
27
28 G_DEFINE_TYPE (EmpathyClientFactory, empathy_client_factory,
29     TP_TYPE_AUTOMATIC_CLIENT_FACTORY)
30
31 #define chainup ((TpSimpleClientFactoryClass *) \
32     empathy_client_factory_parent_class)
33
34 static TpChannel *
35 empathy_client_factory_create_channel (TpSimpleClientFactory *factory,
36     TpConnection *conn,
37     const gchar *path,
38     const GHashTable *properties,
39     GError **error)
40 {
41   const gchar *chan_type;
42
43   chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
44
45   if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
46     {
47       return TP_CHANNEL (empathy_tp_chat_new (
48             TP_SIMPLE_CLIENT_FACTORY (factory), conn, path,
49             properties));
50     }
51
52   return chainup->create_channel (factory, conn, path, properties, error);
53 }
54
55 static GArray *
56 empathy_client_factory_dup_channel_features (TpSimpleClientFactory *factory,
57     TpChannel *channel)
58 {
59   GArray *features;
60   GQuark feature;
61
62   features = chainup->dup_channel_features (factory, channel);
63
64   feature = TP_CHANNEL_FEATURE_CONTACTS;
65   g_array_append_val (features, feature);
66
67   if (EMPATHY_IS_TP_CHAT (channel))
68     {
69       feature = TP_TEXT_CHANNEL_FEATURE_CHAT_STATES;
70       g_array_append_val (features, feature);
71
72       feature = EMPATHY_TP_CHAT_FEATURE_READY;
73       g_array_append_val (features, feature);
74     }
75
76   return features;
77 }
78
79 static GArray *
80 empathy_client_factory_dup_account_features (TpSimpleClientFactory *factory,
81     TpAccount *account)
82 {
83   GArray *features;
84   GQuark feature;
85
86   features = chainup->dup_account_features (factory, account);
87
88   feature = TP_ACCOUNT_FEATURE_CONNECTION;
89   g_array_append_val (features, feature);
90
91   feature = TP_ACCOUNT_FEATURE_ADDRESSING;
92   g_array_append_val (features, feature);
93
94   feature = TP_ACCOUNT_FEATURE_STORAGE;
95   g_array_append_val (features, feature);
96
97   return features;
98 }
99
100 static GArray *
101 empathy_client_factory_dup_connection_features (TpSimpleClientFactory *factory,
102     TpConnection *connection)
103 {
104   GArray *features;
105   GQuark feature;
106
107   features = chainup->dup_connection_features (factory, connection);
108
109   feature = TP_CONNECTION_FEATURE_CAPABILITIES;
110   g_array_append_val (features, feature);
111
112   feature = TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS;
113   g_array_append_val (features, feature);
114
115   feature = TP_CONNECTION_FEATURE_CONTACT_INFO;
116   g_array_append_val (features, feature);
117
118   feature = TP_CONNECTION_FEATURE_BALANCE;
119   g_array_append_val (features, feature);
120
121   feature = TP_CONNECTION_FEATURE_CONTACT_BLOCKING;
122   g_array_append_val (features, feature);
123
124   /* Most empathy-* may allow user to add a contact to his contact list. We
125    * need this property to check if the connection allows it. It's cheap to
126    * prepare anyway as it will just call GetAll() on the ContactList iface. */
127   feature = TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES;
128   g_array_append_val (features, feature);
129
130   return features;
131 }
132
133 static GArray *
134 empathy_client_factory_dup_contact_features (TpSimpleClientFactory *factory,
135         TpConnection *connection)
136 {
137   GArray *features;
138   TpContactFeature extra_features[] = {
139       TP_CONTACT_FEATURE_ALIAS,
140       TP_CONTACT_FEATURE_PRESENCE,
141       TP_CONTACT_FEATURE_AVATAR_TOKEN,
142       TP_CONTACT_FEATURE_AVATAR_DATA,
143       TP_CONTACT_FEATURE_CAPABILITIES,
144       /* Needed by empathy_individual_add_menu_item_new to check if a contact
145        * is already in the contact list. This feature is pretty cheap to
146        * prepare as it doesn't prepare the full roster. */
147       TP_CONTACT_FEATURE_SUBSCRIPTION_STATES,
148       TP_CONTACT_FEATURE_CONTACT_GROUPS,
149       TP_CONTACT_FEATURE_CLIENT_TYPES,
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 }