2 * empathy-connection-managers.c - Source for EmpathyConnectionManagers
3 * Copyright (C) 2009 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
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.
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.
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
26 #include <telepathy-glib/connection-manager.h>
27 #include <telepathy-glib/util.h>
29 #include "empathy-connection-managers.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include <libempathy/empathy-debug.h>
35 static GObject *managers = NULL;
37 G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
47 static guint signals[LAST_SIGNAL] = {0};
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
57 /* private structure */
58 typedef struct _EmpathyConnectionManagersPriv
59 EmpathyConnectionManagersPriv;
61 struct _EmpathyConnectionManagersPriv
63 gboolean dispose_has_run;
72 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
74 EmpathyConnectionManagersPriv *priv =
75 G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
76 EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
80 priv->dbus = tp_dbus_daemon_dup (NULL);
81 g_assert (priv->dbus != NULL);
83 empathy_connection_managers_update (obj);
85 /* allocate any data required by the object here */
88 static void empathy_connection_managers_dispose (GObject *object);
91 empathy_connection_managers_constructor (GType type,
92 guint n_construct_params,
93 GObjectConstructParam *construct_params)
96 return g_object_ref (managers);
99 G_OBJECT_CLASS (empathy_connection_managers_parent_class)->constructor
100 (type, n_construct_params, construct_params);
102 g_object_add_weak_pointer (managers, (gpointer) &managers);
110 empathy_connection_managers_get_property (GObject *object,
115 EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
116 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
121 g_value_set_boolean (value, priv->ready);
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 empathy_connection_managers_class_init (
131 EmpathyConnectionManagersClass *empathy_connection_managers_class)
133 GObjectClass *object_class =
134 G_OBJECT_CLASS (empathy_connection_managers_class);
136 g_type_class_add_private (empathy_connection_managers_class, sizeof
137 (EmpathyConnectionManagersPriv));
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;
143 g_object_class_install_property (object_class, PROP_READY,
144 g_param_spec_boolean ("ready",
146 "Whether the connection manager information is ready to be used",
148 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
150 signals[UPDATED] = g_signal_new ("updated",
151 G_TYPE_FROM_CLASS (object_class),
154 g_cclosure_marshal_generic,
159 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
161 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
164 for (l = priv->cms ; l != NULL ; l = g_list_next (l))
166 g_object_unref (l->data);
168 g_list_free (priv->cms);
174 empathy_connection_managers_dispose (GObject *object)
176 EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
177 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
179 if (priv->dispose_has_run)
182 priv->dispose_has_run = TRUE;
184 if (priv->dbus != NULL)
185 g_object_unref (priv->dbus);
188 empathy_connection_managers_free_cm_list (self);
190 /* release any references held by the object here */
192 if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
193 G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
196 EmpathyConnectionManagers *
197 empathy_connection_managers_dup_singleton (void)
199 return EMPATHY_CONNECTION_MANAGERS (
200 g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
204 empathy_connection_managers_is_ready (EmpathyConnectionManagers *self)
206 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
211 empathy_connection_managers_listed_cb (TpConnectionManager * const *cms,
215 GObject *weak_object)
217 EmpathyConnectionManagers *self =
218 EMPATHY_CONNECTION_MANAGERS (weak_object);
219 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
220 TpConnectionManager * const *iter;
222 empathy_connection_managers_free_cm_list (self);
226 DEBUG ("Failed to get connection managers: %s", error->message);
230 for (iter = cms ; iter != NULL && *iter != NULL; iter++)
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));
238 g_object_ref (weak_object);
242 g_object_notify (weak_object, "ready");
244 g_signal_emit (weak_object, signals[UPDATED], 0);
245 g_object_unref (weak_object);
249 empathy_connection_managers_update (EmpathyConnectionManagers *self)
251 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
253 tp_list_connection_managers (priv->dbus,
254 empathy_connection_managers_listed_cb,
255 NULL, NULL, G_OBJECT (self));
259 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
261 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
266 TpConnectionManager *
267 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
270 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
273 for (l = priv->cms ; l != NULL; l = g_list_next (l))
275 TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
277 if (!tp_strdiff (c->name, cm))
285 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
287 EmpathyConnectionManagersPriv *priv;
289 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
291 priv = GET_PRIV (self);
293 return g_list_length (priv->cms);
297 notify_ready_cb (EmpathyConnectionManagers *self,
299 GSimpleAsyncResult *result)
301 g_simple_async_result_complete (result);
302 g_object_unref (result);
306 empathy_connection_managers_prepare_async (
307 EmpathyConnectionManagers *self,
308 GAsyncReadyCallback callback,
311 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
312 GSimpleAsyncResult *result;
314 result = g_simple_async_result_new (G_OBJECT (managers),
315 callback, user_data, empathy_connection_managers_prepare_finish);
319 g_simple_async_result_complete_in_idle (result);
320 g_object_unref (result);
324 g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
329 empathy_connection_managers_prepare_finish (
330 EmpathyConnectionManagers *self,
331 GAsyncResult *result,
334 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
336 g_return_val_if_fail (g_simple_async_result_is_valid (result,
337 G_OBJECT (self), empathy_connection_managers_prepare_finish), FALSE);
339 if (g_simple_async_result_propagate_error (simple, error))