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 (GObject *source,
212 GAsyncResult *result,
215 TpWeakRef *wr = user_data;
216 GError *error = NULL;
217 EmpathyConnectionManagers *self = tp_weak_ref_dup_object (wr);
219 EmpathyConnectionManagersPriv *priv;
223 tp_weak_ref_destroy (wr);
227 priv = GET_PRIV (self);
229 empathy_connection_managers_free_cm_list (self);
231 cms = tp_list_connection_managers_finish (result, &error);
234 DEBUG ("Failed to get connection managers: %s", error->message);
235 g_error_free (error);
239 for (l = cms ; l != NULL; l = g_list_next (l))
241 TpConnectionManager *cm = l->data;
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));
252 g_object_notify (G_OBJECT (self), "ready");
255 g_signal_emit (self, signals[UPDATED], 0);
256 g_object_unref (self);
257 tp_weak_ref_destroy (wr);
261 empathy_connection_managers_update (EmpathyConnectionManagers *self)
263 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
265 tp_list_connection_managers_async (priv->dbus,
266 empathy_connection_managers_listed_cb,
267 tp_weak_ref_new (self, NULL, NULL));
271 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
273 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
278 TpConnectionManager *
279 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
282 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
285 for (l = priv->cms ; l != NULL; l = g_list_next (l))
287 TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
289 if (!tp_strdiff (tp_connection_manager_get_name (c), cm))
297 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
299 EmpathyConnectionManagersPriv *priv;
301 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
303 priv = GET_PRIV (self);
305 return g_list_length (priv->cms);
309 notify_ready_cb (EmpathyConnectionManagers *self,
311 GSimpleAsyncResult *result)
313 g_simple_async_result_complete (result);
314 g_object_unref (result);
318 empathy_connection_managers_prepare_async (
319 EmpathyConnectionManagers *self,
320 GAsyncReadyCallback callback,
323 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
324 GSimpleAsyncResult *result;
326 result = g_simple_async_result_new (G_OBJECT (managers),
327 callback, user_data, empathy_connection_managers_prepare_finish);
331 g_simple_async_result_complete_in_idle (result);
332 g_object_unref (result);
336 g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
341 empathy_connection_managers_prepare_finish (
342 EmpathyConnectionManagers *self,
343 GAsyncResult *result,
346 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
348 g_return_val_if_fail (g_simple_async_result_is_valid (result,
349 G_OBJECT (self), empathy_connection_managers_prepare_finish), FALSE);
351 if (g_simple_async_result_propagate_error (simple, error))