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