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
22 #include "empathy-connection-managers.h"
24 #include "empathy-utils.h"
26 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
27 #include "empathy-debug.h"
29 static GObject *managers = NULL;
31 G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
41 static guint signals[LAST_SIGNAL] = {0};
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
51 /* private structure */
52 typedef struct _EmpathyConnectionManagersPriv
53 EmpathyConnectionManagersPriv;
55 struct _EmpathyConnectionManagersPriv
57 gboolean dispose_has_run;
66 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
68 EmpathyConnectionManagersPriv *priv =
69 G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
70 EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
74 priv->dbus = tp_dbus_daemon_dup (NULL);
75 g_assert (priv->dbus != NULL);
77 empathy_connection_managers_update (obj);
79 /* allocate any data required by the object here */
82 static void empathy_connection_managers_dispose (GObject *object);
85 empathy_connection_managers_constructor (GType type,
86 guint n_construct_params,
87 GObjectConstructParam *construct_params)
90 return g_object_ref (managers);
93 G_OBJECT_CLASS (empathy_connection_managers_parent_class)->constructor
94 (type, n_construct_params, construct_params);
96 g_object_add_weak_pointer (managers, (gpointer) &managers);
104 empathy_connection_managers_get_property (GObject *object,
109 EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
110 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
115 g_value_set_boolean (value, priv->ready);
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 empathy_connection_managers_class_init (
125 EmpathyConnectionManagersClass *empathy_connection_managers_class)
127 GObjectClass *object_class =
128 G_OBJECT_CLASS (empathy_connection_managers_class);
130 g_type_class_add_private (empathy_connection_managers_class, sizeof
131 (EmpathyConnectionManagersPriv));
133 object_class->constructor = empathy_connection_managers_constructor;
134 object_class->dispose = empathy_connection_managers_dispose;
135 object_class->get_property = empathy_connection_managers_get_property;
137 g_object_class_install_property (object_class, PROP_READY,
138 g_param_spec_boolean ("ready",
140 "Whether the connection manager information is ready to be used",
142 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
144 signals[UPDATED] = g_signal_new ("updated",
145 G_TYPE_FROM_CLASS (object_class),
148 g_cclosure_marshal_generic,
153 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
155 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
158 for (l = priv->cms ; l != NULL ; l = g_list_next (l))
160 g_object_unref (l->data);
162 g_list_free (priv->cms);
168 empathy_connection_managers_dispose (GObject *object)
170 EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
171 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
173 if (priv->dispose_has_run)
176 priv->dispose_has_run = TRUE;
178 if (priv->dbus != NULL)
179 g_object_unref (priv->dbus);
182 empathy_connection_managers_free_cm_list (self);
184 /* release any references held by the object here */
186 if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
187 G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
190 EmpathyConnectionManagers *
191 empathy_connection_managers_dup_singleton (void)
193 return EMPATHY_CONNECTION_MANAGERS (
194 g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
198 empathy_connection_managers_is_ready (EmpathyConnectionManagers *self)
200 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
205 empathy_connection_managers_listed_cb (GObject *source,
206 GAsyncResult *result,
209 TpWeakRef *wr = user_data;
210 GError *error = NULL;
211 EmpathyConnectionManagers *self = tp_weak_ref_dup_object (wr);
213 EmpathyConnectionManagersPriv *priv;
217 tp_weak_ref_destroy (wr);
221 priv = GET_PRIV (self);
223 empathy_connection_managers_free_cm_list (self);
225 cms = tp_list_connection_managers_finish (result, &error);
228 DEBUG ("Failed to get connection managers: %s", error->message);
229 g_error_free (error);
233 for (l = cms ; l != NULL; l = g_list_next (l))
235 TpConnectionManager *cm = l->data;
237 /* only list cms that didn't hit errors */
238 if (tp_proxy_is_prepared (cm, TP_CONNECTION_MANAGER_FEATURE_CORE))
239 priv->cms = g_list_prepend (priv->cms, g_object_ref (cm));
246 g_object_notify (G_OBJECT (self), "ready");
249 g_signal_emit (self, signals[UPDATED], 0);
250 g_object_unref (self);
251 tp_weak_ref_destroy (wr);
255 empathy_connection_managers_update (EmpathyConnectionManagers *self)
257 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
259 tp_list_connection_managers_async (priv->dbus,
260 empathy_connection_managers_listed_cb,
261 tp_weak_ref_new (self, NULL, NULL));
265 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
267 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
272 TpConnectionManager *
273 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
276 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
279 for (l = priv->cms ; l != NULL; l = g_list_next (l))
281 TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
283 if (!tp_strdiff (tp_connection_manager_get_name (c), cm))
291 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
293 EmpathyConnectionManagersPriv *priv;
295 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
297 priv = GET_PRIV (self);
299 return g_list_length (priv->cms);
303 notify_ready_cb (EmpathyConnectionManagers *self,
305 GSimpleAsyncResult *result)
307 g_simple_async_result_complete (result);
308 g_object_unref (result);
312 empathy_connection_managers_prepare_async (
313 EmpathyConnectionManagers *self,
314 GAsyncReadyCallback callback,
317 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
318 GSimpleAsyncResult *result;
320 result = g_simple_async_result_new (G_OBJECT (managers),
321 callback, user_data, empathy_connection_managers_prepare_finish);
325 g_simple_async_result_complete_in_idle (result);
326 g_object_unref (result);
330 g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
335 empathy_connection_managers_prepare_finish (
336 EmpathyConnectionManagers *self,
337 GAsyncResult *result,
340 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
342 g_return_val_if_fail (g_simple_async_result_is_valid (result,
343 G_OBJECT (self), empathy_connection_managers_prepare_finish), FALSE);
345 if (g_simple_async_result_propagate_error (simple, error))