]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connection-managers.c
factor out create_individual_from_persona()
[empathy.git] / libempathy / empathy-connection-managers.c
1 /*
2  * empathy-connection-managers.c - Source for EmpathyConnectionManagers
3  * Copyright (C) 2009 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <telepathy-glib/connection-manager.h>
27 #include <telepathy-glib/util.h>
28
29 #include "empathy-connection-managers.h"
30 #include "empathy-utils.h"
31
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include <libempathy/empathy-debug.h>
34
35 static GObject *managers = NULL;
36
37 G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
38     G_TYPE_OBJECT)
39
40 /* signal enum */
41 enum
42 {
43     UPDATED,
44     LAST_SIGNAL
45 };
46
47 static guint signals[LAST_SIGNAL] = {0};
48
49 /* properties */
50 enum {
51   PROP_READY = 1
52 };
53
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
55
56
57 /* private structure */
58 typedef struct _EmpathyConnectionManagersPriv
59   EmpathyConnectionManagersPriv;
60
61 struct _EmpathyConnectionManagersPriv
62 {
63   gboolean dispose_has_run;
64   gboolean ready;
65
66   GList *cms;
67
68   TpDBusDaemon *dbus;
69 };
70
71 static void
72 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
73 {
74   EmpathyConnectionManagersPriv *priv =
75     G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
76       EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
77
78   obj->priv = priv;
79
80   priv->dbus = tp_dbus_daemon_dup (NULL);
81   g_assert (priv->dbus != NULL);
82
83   empathy_connection_managers_update (obj);
84
85   /* allocate any data required by the object here */
86 }
87
88 static void empathy_connection_managers_dispose (GObject *object);
89
90 static GObject *
91 empathy_connection_managers_constructor (GType type,
92                         guint n_construct_params,
93                         GObjectConstructParam *construct_params)
94 {
95   if (managers != NULL)
96     return g_object_ref (managers);
97
98   managers =
99       G_OBJECT_CLASS (empathy_connection_managers_parent_class)->constructor
100           (type, n_construct_params, construct_params);
101
102   g_object_add_weak_pointer (managers, (gpointer) &managers);
103
104   return managers;
105 }
106
107
108
109 static void
110 empathy_connection_managers_get_property (GObject *object,
111     guint prop_id,
112     GValue *value,
113     GParamSpec *pspec)
114 {
115   EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
116   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
117
118   switch (prop_id)
119     {
120       case PROP_READY:
121         g_value_set_boolean (value, priv->ready);
122         break;
123       default:
124         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125         break;
126     }
127 }
128
129 static void
130 empathy_connection_managers_class_init (
131     EmpathyConnectionManagersClass *empathy_connection_managers_class)
132 {
133   GObjectClass *object_class =
134       G_OBJECT_CLASS (empathy_connection_managers_class);
135
136   g_type_class_add_private (empathy_connection_managers_class, sizeof
137       (EmpathyConnectionManagersPriv));
138
139   object_class->constructor = empathy_connection_managers_constructor;
140   object_class->dispose = empathy_connection_managers_dispose;
141   object_class->get_property = empathy_connection_managers_get_property;
142
143   g_object_class_install_property (object_class, PROP_READY,
144     g_param_spec_boolean ("ready",
145       "Ready",
146       "Whether the connection manager information is ready to be used",
147       FALSE,
148       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
149
150   signals[UPDATED] = g_signal_new ("updated",
151     G_TYPE_FROM_CLASS (object_class),
152     G_SIGNAL_RUN_LAST,
153     0, NULL, NULL,
154     g_cclosure_marshal_generic,
155     G_TYPE_NONE, 0);
156 }
157
158 static void
159 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
160 {
161   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
162   GList *l;
163
164   for (l = priv->cms ; l != NULL ; l = g_list_next (l))
165     {
166       g_object_unref (l->data);
167     }
168   g_list_free (priv->cms);
169
170   priv->cms = NULL;
171 }
172
173 static void
174 empathy_connection_managers_dispose (GObject *object)
175 {
176   EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
177   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
178
179   if (priv->dispose_has_run)
180     return;
181
182   priv->dispose_has_run = TRUE;
183
184   if (priv->dbus != NULL)
185     g_object_unref (priv->dbus);
186   priv->dbus = NULL;
187
188   empathy_connection_managers_free_cm_list (self);
189
190   /* release any references held by the object here */
191
192   if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
193     G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
194 }
195
196 EmpathyConnectionManagers *
197 empathy_connection_managers_dup_singleton (void)
198 {
199   return EMPATHY_CONNECTION_MANAGERS (
200       g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
201 }
202
203 gboolean
204 empathy_connection_managers_is_ready (EmpathyConnectionManagers *self)
205 {
206   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
207   return priv->ready;
208 }
209
210 static void
211 empathy_connection_managers_listed_cb (TpConnectionManager * const *cms,
212     gsize n_cms,
213     const GError *error,
214     gpointer user_data,
215     GObject *weak_object)
216 {
217   EmpathyConnectionManagers *self =
218     EMPATHY_CONNECTION_MANAGERS (weak_object);
219   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
220   TpConnectionManager * const *iter;
221
222   empathy_connection_managers_free_cm_list (self);
223
224   if (error != NULL)
225     {
226       DEBUG ("Failed to get connection managers: %s", error->message);
227       goto out;
228     }
229
230   for (iter = cms ; iter != NULL && *iter != NULL; iter++)
231     {
232       /* only list cms that didn't hit errors */
233       if (tp_proxy_is_prepared (*iter, TP_CONNECTION_MANAGER_FEATURE_CORE))
234         priv->cms = g_list_prepend (priv->cms, g_object_ref (*iter));
235     }
236
237 out:
238   g_object_ref (weak_object);
239   if (!priv->ready)
240     {
241       priv->ready = TRUE;
242       g_object_notify (weak_object, "ready");
243     }
244   g_signal_emit (weak_object, signals[UPDATED], 0);
245   g_object_unref (weak_object);
246 }
247
248 void
249 empathy_connection_managers_update (EmpathyConnectionManagers *self)
250 {
251   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
252
253   tp_list_connection_managers (priv->dbus,
254     empathy_connection_managers_listed_cb,
255     NULL, NULL, G_OBJECT (self));
256 }
257
258 GList *
259 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
260 {
261   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
262
263   return priv->cms;
264 }
265
266 TpConnectionManager *
267 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
268   const gchar *cm)
269 {
270   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
271   GList *l;
272
273   for (l = priv->cms ; l != NULL; l = g_list_next (l))
274     {
275       TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
276
277       if (!tp_strdiff (c->name, cm))
278         return c;
279     }
280
281   return NULL;
282 }
283
284 guint
285 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
286 {
287   EmpathyConnectionManagersPriv *priv;
288
289   g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
290
291   priv = GET_PRIV (self);
292
293   return g_list_length (priv->cms);
294 }
295
296 static void
297 notify_ready_cb (EmpathyConnectionManagers *self,
298     GParamSpec *spec,
299     GSimpleAsyncResult *result)
300 {
301   g_simple_async_result_complete (result);
302   g_object_unref (result);
303 }
304
305 void
306 empathy_connection_managers_prepare_async (
307     EmpathyConnectionManagers *self,
308     GAsyncReadyCallback callback,
309     gpointer user_data)
310 {
311   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
312   GSimpleAsyncResult *result;
313
314   result = g_simple_async_result_new (G_OBJECT (managers),
315       callback, user_data, empathy_connection_managers_prepare_finish);
316
317   if (priv->ready)
318     {
319       g_simple_async_result_complete_in_idle (result);
320       g_object_unref (result);
321       return;
322     }
323
324   g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
325       result);
326 }
327
328 gboolean
329 empathy_connection_managers_prepare_finish (
330     EmpathyConnectionManagers *self,
331     GAsyncResult *result,
332     GError **error)
333 {
334   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
335
336   g_return_val_if_fail (g_simple_async_result_is_valid (result,
337           G_OBJECT (self), empathy_connection_managers_prepare_finish), FALSE);
338
339   if (g_simple_async_result_propagate_error (simple, error))
340     return FALSE;
341
342   return TRUE;
343 }