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