]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connection-managers.c
93985f5da572f6c20f230bd58d82d1292c28d7f2
[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 (GObject *source,
212     GAsyncResult *result,
213     gpointer user_data)
214 {
215   TpWeakRef *wr = user_data;
216   GError *error = NULL;
217   EmpathyConnectionManagers *self = tp_weak_ref_dup_object (wr);
218   GList *cms, *l;
219   EmpathyConnectionManagersPriv *priv;
220
221   if (self == NULL)
222     {
223       tp_weak_ref_destroy (wr);
224       return;
225     }
226
227   priv = GET_PRIV (self);
228
229   empathy_connection_managers_free_cm_list (self);
230
231   cms = tp_list_connection_managers_finish (result, &error);
232   if (error != NULL)
233     {
234       DEBUG ("Failed to get connection managers: %s", error->message);
235       g_error_free (error);
236       goto out;
237     }
238
239   for (l = cms ; l != NULL; l = g_list_next (l))
240     {
241       TpConnectionManager *cm = l->data;
242
243       /* only list cms that didn't hit errors */
244       if (tp_proxy_is_prepared (cm, TP_CONNECTION_MANAGER_FEATURE_CORE))
245         priv->cms = g_list_prepend (priv->cms, g_object_ref (cm));
246     }
247
248 out:
249   if (!priv->ready)
250     {
251       priv->ready = TRUE;
252       g_object_notify (G_OBJECT (self), "ready");
253     }
254
255   g_signal_emit (self, signals[UPDATED], 0);
256   g_object_unref (self);
257   tp_weak_ref_destroy (wr);
258 }
259
260 void
261 empathy_connection_managers_update (EmpathyConnectionManagers *self)
262 {
263   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
264
265   tp_list_connection_managers_async (priv->dbus,
266     empathy_connection_managers_listed_cb,
267     tp_weak_ref_new (self, NULL, NULL));
268 }
269
270 GList *
271 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
272 {
273   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
274
275   return priv->cms;
276 }
277
278 TpConnectionManager *
279 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
280   const gchar *cm)
281 {
282   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
283   GList *l;
284
285   for (l = priv->cms ; l != NULL; l = g_list_next (l))
286     {
287       TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
288
289       if (!tp_strdiff (tp_connection_manager_get_name (c), cm))
290         return c;
291     }
292
293   return NULL;
294 }
295
296 guint
297 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
298 {
299   EmpathyConnectionManagersPriv *priv;
300
301   g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
302
303   priv = GET_PRIV (self);
304
305   return g_list_length (priv->cms);
306 }
307
308 static void
309 notify_ready_cb (EmpathyConnectionManagers *self,
310     GParamSpec *spec,
311     GSimpleAsyncResult *result)
312 {
313   g_simple_async_result_complete (result);
314   g_object_unref (result);
315 }
316
317 void
318 empathy_connection_managers_prepare_async (
319     EmpathyConnectionManagers *self,
320     GAsyncReadyCallback callback,
321     gpointer user_data)
322 {
323   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
324   GSimpleAsyncResult *result;
325
326   result = g_simple_async_result_new (G_OBJECT (managers),
327       callback, user_data, empathy_connection_managers_prepare_finish);
328
329   if (priv->ready)
330     {
331       g_simple_async_result_complete_in_idle (result);
332       g_object_unref (result);
333       return;
334     }
335
336   g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
337       result);
338 }
339
340 gboolean
341 empathy_connection_managers_prepare_finish (
342     EmpathyConnectionManagers *self,
343     GAsyncResult *result,
344     GError **error)
345 {
346   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
347
348   g_return_val_if_fail (g_simple_async_result_is_valid (result,
349           G_OBJECT (self), empathy_connection_managers_prepare_finish), FALSE);
350
351   if (g_simple_async_result_propagate_error (simple, error))
352     return FALSE;
353
354   return TRUE;
355 }