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