]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connection-managers.c
Merge branch 'master' into mc5
[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
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <telepathy-glib/connection-manager.h>
26 #include <telepathy-glib/util.h>
27
28 #include "empathy-connection-managers.h"
29 #include "empathy-utils.h"
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include <libempathy/empathy-debug.h>
33
34 static GObject *managers = NULL;
35
36 G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
37     G_TYPE_OBJECT)
38
39 /* signal enum */
40 enum
41 {
42     UPDATED,
43     LAST_SIGNAL
44 };
45
46 static guint signals[LAST_SIGNAL] = {0};
47
48 /* properties */
49 enum {
50   PROP_READY = 1
51 };
52
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
54
55
56 /* private structure */
57 typedef struct _EmpathyConnectionManagersPriv
58   EmpathyConnectionManagersPriv;
59
60 struct _EmpathyConnectionManagersPriv
61 {
62   gboolean dispose_has_run;
63   gboolean ready;
64
65   GList *cms;
66
67   TpDBusDaemon *dbus;
68 };
69
70 static void
71 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
72 {
73   EmpathyConnectionManagersPriv *priv =
74     G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
75       EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
76
77   obj->priv = priv;
78
79   priv->dbus = tp_dbus_daemon_dup (NULL);
80   g_assert (priv->dbus != NULL);
81
82   empathy_connection_managers_update (obj);
83
84   /* allocate any data required by the object here */
85 }
86
87 static void empathy_connection_managers_dispose (GObject *object);
88 static void empathy_connection_managers_finalize (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 *managers = EMPATHY_CONNECTION_MANAGERS (object);
116   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
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->finalize = empathy_connection_managers_finalize;
142   object_class->get_property = empathy_connection_managers_get_property;
143
144   g_object_class_install_property (object_class, PROP_READY,
145     g_param_spec_boolean ("ready",
146       "Ready",
147       "Whether the connection manager information is ready to be used",
148       FALSE,
149       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
150
151   signals[UPDATED] = g_signal_new ("updated",
152     G_TYPE_FROM_CLASS (object_class),
153     G_SIGNAL_RUN_LAST,
154     0, NULL, NULL,
155     g_cclosure_marshal_VOID__VOID,
156     G_TYPE_NONE, 0);
157 }
158
159 static void
160 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
161 {
162   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
163   GList *l;
164
165   for (l = priv->cms ; l != NULL ; l = g_list_next (l))
166     {
167       g_object_unref (l->data);
168     }
169   g_list_free (priv->cms);
170
171   priv->cms = NULL;
172 }
173
174 static void
175 empathy_connection_managers_dispose (GObject *object)
176 {
177   EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
178   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
179
180   if (priv->dispose_has_run)
181     return;
182
183   priv->dispose_has_run = TRUE;
184
185   if (priv->dbus != NULL)
186     g_object_unref (priv->dbus);
187   priv->dbus = NULL;
188
189   empathy_connection_managers_free_cm_list (self);
190
191   /* release any references held by the object here */
192
193   if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
194     G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
195 }
196
197 void
198 empathy_connection_managers_finalize (GObject *object)
199 {
200 #if 0
201   EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
202   EmpathyConnectionManagersPriv *priv =
203     EMPATHY_CONNECTION_MANAGERS_GET_PRIVATE (self);
204
205   /* free any data held directly by the object here */
206
207   G_OBJECT_CLASS (empathy_connection_managers_parent_class)->finalize (object);
208 #endif
209 }
210
211 EmpathyConnectionManagers *
212 empathy_connection_managers_dup_singleton (void)
213 {
214   return EMPATHY_CONNECTION_MANAGERS (
215       g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
216 }
217
218 gboolean
219 empathy_connection_managers_is_ready (EmpathyConnectionManagers *managers)
220 {
221   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
222   return priv->ready;
223 }
224
225 static void
226 empathy_connection_managers_listed_cb (TpConnectionManager * const *cms,
227     gsize n_cms,
228     const GError *error,
229     gpointer user_data,
230     GObject *weak_object)
231 {
232   EmpathyConnectionManagers *self =
233     EMPATHY_CONNECTION_MANAGERS (weak_object);
234   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
235   TpConnectionManager * const *iter;
236
237   empathy_connection_managers_free_cm_list (self);
238
239   if (error != NULL)
240     {
241       DEBUG ("Failed to get connection managers: %s", error->message);
242       goto out;
243     }
244
245   for (iter = cms ; iter != NULL && *iter != NULL; iter++)
246     {
247       /* only list cms that didn't hit errors */
248       if (tp_connection_manager_is_ready (*iter))
249         priv->cms = g_list_prepend (priv->cms, g_object_ref (*iter));
250     }
251
252 out:
253   g_object_ref (weak_object);
254   if (!priv->ready)
255     {
256       priv->ready = TRUE;
257       g_object_notify (weak_object, "ready");
258     }
259   g_signal_emit (weak_object, signals[UPDATED], 0);
260   g_object_unref (weak_object);
261 }
262
263 void
264 empathy_connection_managers_update (EmpathyConnectionManagers *managers)
265 {
266   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
267
268   tp_list_connection_managers (priv->dbus,
269     empathy_connection_managers_listed_cb,
270     NULL, NULL, G_OBJECT (managers));
271 }
272
273 GList *
274 empathy_connection_managers_get_cms (EmpathyConnectionManagers *managers)
275 {
276   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
277
278   return priv->cms;
279 }
280
281 TpConnectionManager *
282 empathy_connection_managers_get_cm (EmpathyConnectionManagers *managers,
283   const gchar *cm)
284 {
285   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
286   GList *l;
287
288   for (l = priv->cms ; l != NULL; l = g_list_next (l))
289     {
290       TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
291
292       if (!tp_strdiff (c->name, cm))
293         return c;
294     }
295
296   return NULL;
297 }
298
299 guint
300 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *managers)
301 {
302   EmpathyConnectionManagersPriv *priv;
303
304   g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (managers), 0);
305
306   priv = GET_PRIV (managers);
307
308   return g_list_length (priv->cms);
309 }