]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connection-managers.c
Merge branch 'gnome-3-8'
[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 #include "empathy-connection-managers.h"
23
24 #include "empathy-utils.h"
25
26 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
27 #include "empathy-debug.h"
28
29 static GObject *managers = NULL;
30
31 G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
32     G_TYPE_OBJECT)
33
34 /* signal enum */
35 enum
36 {
37     UPDATED,
38     LAST_SIGNAL
39 };
40
41 static guint signals[LAST_SIGNAL] = {0};
42
43 /* properties */
44 enum {
45   PROP_READY = 1
46 };
47
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
49
50
51 /* private structure */
52 typedef struct _EmpathyConnectionManagersPriv
53   EmpathyConnectionManagersPriv;
54
55 struct _EmpathyConnectionManagersPriv
56 {
57   gboolean dispose_has_run;
58   gboolean ready;
59
60   GList *cms;
61
62   TpDBusDaemon *dbus;
63 };
64
65 static void
66 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
67 {
68   EmpathyConnectionManagersPriv *priv =
69     G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
70       EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
71
72   obj->priv = priv;
73
74   priv->dbus = tp_dbus_daemon_dup (NULL);
75   g_assert (priv->dbus != NULL);
76
77   empathy_connection_managers_update (obj);
78
79   /* allocate any data required by the object here */
80 }
81
82 static void empathy_connection_managers_dispose (GObject *object);
83
84 static GObject *
85 empathy_connection_managers_constructor (GType type,
86                         guint n_construct_params,
87                         GObjectConstructParam *construct_params)
88 {
89   if (managers != NULL)
90     return g_object_ref (managers);
91
92   managers =
93       G_OBJECT_CLASS (empathy_connection_managers_parent_class)->constructor
94           (type, n_construct_params, construct_params);
95
96   g_object_add_weak_pointer (managers, (gpointer) &managers);
97
98   return managers;
99 }
100
101
102
103 static void
104 empathy_connection_managers_get_property (GObject *object,
105     guint prop_id,
106     GValue *value,
107     GParamSpec *pspec)
108 {
109   EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
110   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
111
112   switch (prop_id)
113     {
114       case PROP_READY:
115         g_value_set_boolean (value, priv->ready);
116         break;
117       default:
118         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119         break;
120     }
121 }
122
123 static void
124 empathy_connection_managers_class_init (
125     EmpathyConnectionManagersClass *empathy_connection_managers_class)
126 {
127   GObjectClass *object_class =
128       G_OBJECT_CLASS (empathy_connection_managers_class);
129
130   g_type_class_add_private (empathy_connection_managers_class, sizeof
131       (EmpathyConnectionManagersPriv));
132
133   object_class->constructor = empathy_connection_managers_constructor;
134   object_class->dispose = empathy_connection_managers_dispose;
135   object_class->get_property = empathy_connection_managers_get_property;
136
137   g_object_class_install_property (object_class, PROP_READY,
138     g_param_spec_boolean ("ready",
139       "Ready",
140       "Whether the connection manager information is ready to be used",
141       FALSE,
142       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
143
144   signals[UPDATED] = g_signal_new ("updated",
145     G_TYPE_FROM_CLASS (object_class),
146     G_SIGNAL_RUN_LAST,
147     0, NULL, NULL,
148     g_cclosure_marshal_generic,
149     G_TYPE_NONE, 0);
150 }
151
152 static void
153 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
154 {
155   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
156   GList *l;
157
158   for (l = priv->cms ; l != NULL ; l = g_list_next (l))
159     {
160       g_object_unref (l->data);
161     }
162   g_list_free (priv->cms);
163
164   priv->cms = NULL;
165 }
166
167 static void
168 empathy_connection_managers_dispose (GObject *object)
169 {
170   EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
171   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
172
173   if (priv->dispose_has_run)
174     return;
175
176   priv->dispose_has_run = TRUE;
177
178   if (priv->dbus != NULL)
179     g_object_unref (priv->dbus);
180   priv->dbus = NULL;
181
182   empathy_connection_managers_free_cm_list (self);
183
184   /* release any references held by the object here */
185
186   if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
187     G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
188 }
189
190 EmpathyConnectionManagers *
191 empathy_connection_managers_dup_singleton (void)
192 {
193   return EMPATHY_CONNECTION_MANAGERS (
194       g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
195 }
196
197 gboolean
198 empathy_connection_managers_is_ready (EmpathyConnectionManagers *self)
199 {
200   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
201   return priv->ready;
202 }
203
204 static void
205 empathy_connection_managers_listed_cb (GObject *source,
206     GAsyncResult *result,
207     gpointer user_data)
208 {
209   TpWeakRef *wr = user_data;
210   GError *error = NULL;
211   EmpathyConnectionManagers *self = tp_weak_ref_dup_object (wr);
212   GList *cms, *l;
213   EmpathyConnectionManagersPriv *priv;
214
215   if (self == NULL)
216     {
217       tp_weak_ref_destroy (wr);
218       return;
219     }
220
221   priv = GET_PRIV (self);
222
223   empathy_connection_managers_free_cm_list (self);
224
225   cms = tp_list_connection_managers_finish (result, &error);
226   if (error != NULL)
227     {
228       DEBUG ("Failed to get connection managers: %s", error->message);
229       g_error_free (error);
230       goto out;
231     }
232
233   for (l = cms ; l != NULL; l = g_list_next (l))
234     {
235       TpConnectionManager *cm = l->data;
236
237       /* only list cms that didn't hit errors */
238       if (tp_proxy_is_prepared (cm, TP_CONNECTION_MANAGER_FEATURE_CORE))
239         priv->cms = g_list_prepend (priv->cms, g_object_ref (cm));
240     }
241
242 out:
243   if (!priv->ready)
244     {
245       priv->ready = TRUE;
246       g_object_notify (G_OBJECT (self), "ready");
247     }
248
249   g_signal_emit (self, signals[UPDATED], 0);
250   g_object_unref (self);
251   tp_weak_ref_destroy (wr);
252 }
253
254 void
255 empathy_connection_managers_update (EmpathyConnectionManagers *self)
256 {
257   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
258
259   tp_list_connection_managers_async (priv->dbus,
260     empathy_connection_managers_listed_cb,
261     tp_weak_ref_new (self, NULL, NULL));
262 }
263
264 GList *
265 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
266 {
267   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
268
269   return priv->cms;
270 }
271
272 TpConnectionManager *
273 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
274   const gchar *cm)
275 {
276   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
277   GList *l;
278
279   for (l = priv->cms ; l != NULL; l = g_list_next (l))
280     {
281       TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
282
283       if (!tp_strdiff (tp_connection_manager_get_name (c), cm))
284         return c;
285     }
286
287   return NULL;
288 }
289
290 guint
291 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
292 {
293   EmpathyConnectionManagersPriv *priv;
294
295   g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
296
297   priv = GET_PRIV (self);
298
299   return g_list_length (priv->cms);
300 }
301
302 static void
303 notify_ready_cb (EmpathyConnectionManagers *self,
304     GParamSpec *spec,
305     GSimpleAsyncResult *result)
306 {
307   g_simple_async_result_complete (result);
308   g_object_unref (result);
309 }
310
311 void
312 empathy_connection_managers_prepare_async (
313     EmpathyConnectionManagers *self,
314     GAsyncReadyCallback callback,
315     gpointer user_data)
316 {
317   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
318   GSimpleAsyncResult *result;
319
320   result = g_simple_async_result_new (G_OBJECT (managers),
321       callback, user_data, empathy_connection_managers_prepare_finish);
322
323   if (priv->ready)
324     {
325       g_simple_async_result_complete_in_idle (result);
326       g_object_unref (result);
327       return;
328     }
329
330   g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
331       result);
332 }
333
334 gboolean
335 empathy_connection_managers_prepare_finish (
336     EmpathyConnectionManagers *self,
337     GAsyncResult *result,
338     GError **error)
339 {
340   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
341
342   g_return_val_if_fail (g_simple_async_result_is_valid (result,
343           G_OBJECT (self), empathy_connection_managers_prepare_finish), FALSE);
344
345   if (g_simple_async_result_propagate_error (simple, error))
346     return FALSE;
347
348   return TRUE;
349 }