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 "empathy-connection-managers.h"
27 #include "empathy-utils.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include <libempathy/empathy-debug.h>
32 static GObject *managers = NULL;
34 G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
44 static guint signals[LAST_SIGNAL] = {0};
51 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
54 /* private structure */
55 typedef struct _EmpathyConnectionManagersPriv
56 EmpathyConnectionManagersPriv;
58 struct _EmpathyConnectionManagersPriv
60 gboolean dispose_has_run;
69 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
71 EmpathyConnectionManagersPriv *priv =
72 G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
73 EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
77 priv->dbus = tp_dbus_daemon_dup (NULL);
78 g_assert (priv->dbus != NULL);
80 empathy_connection_managers_update (obj);
82 /* allocate any data required by the object here */
85 static void empathy_connection_managers_dispose (GObject *object);
88 empathy_connection_managers_constructor (GType type,
89 guint n_construct_params,
90 GObjectConstructParam *construct_params)
93 return g_object_ref (managers);
96 G_OBJECT_CLASS (empathy_connection_managers_parent_class)->constructor
97 (type, n_construct_params, construct_params);
99 g_object_add_weak_pointer (managers, (gpointer) &managers);
107 empathy_connection_managers_get_property (GObject *object,
112 EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
113 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
118 g_value_set_boolean (value, priv->ready);
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 empathy_connection_managers_class_init (
128 EmpathyConnectionManagersClass *empathy_connection_managers_class)
130 GObjectClass *object_class =
131 G_OBJECT_CLASS (empathy_connection_managers_class);
133 g_type_class_add_private (empathy_connection_managers_class, sizeof
134 (EmpathyConnectionManagersPriv));
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;
140 g_object_class_install_property (object_class, PROP_READY,
141 g_param_spec_boolean ("ready",
143 "Whether the connection manager information is ready to be used",
145 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
147 signals[UPDATED] = g_signal_new ("updated",
148 G_TYPE_FROM_CLASS (object_class),
151 g_cclosure_marshal_generic,
156 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
158 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
161 for (l = priv->cms ; l != NULL ; l = g_list_next (l))
163 g_object_unref (l->data);
165 g_list_free (priv->cms);
171 empathy_connection_managers_dispose (GObject *object)
173 EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
174 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
176 if (priv->dispose_has_run)
179 priv->dispose_has_run = TRUE;
181 if (priv->dbus != NULL)
182 g_object_unref (priv->dbus);
185 empathy_connection_managers_free_cm_list (self);
187 /* release any references held by the object here */
189 if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
190 G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
193 EmpathyConnectionManagers *
194 empathy_connection_managers_dup_singleton (void)
196 return EMPATHY_CONNECTION_MANAGERS (
197 g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
201 empathy_connection_managers_is_ready (EmpathyConnectionManagers *self)
203 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
208 empathy_connection_managers_listed_cb (GObject *source,
209 GAsyncResult *result,
212 TpWeakRef *wr = user_data;
213 GError *error = NULL;
214 EmpathyConnectionManagers *self = tp_weak_ref_dup_object (wr);
216 EmpathyConnectionManagersPriv *priv;
220 tp_weak_ref_destroy (wr);
224 priv = GET_PRIV (self);
226 empathy_connection_managers_free_cm_list (self);
228 cms = tp_list_connection_managers_finish (result, &error);
231 DEBUG ("Failed to get connection managers: %s", error->message);
232 g_error_free (error);
236 for (l = cms ; l != NULL; l = g_list_next (l))
238 TpConnectionManager *cm = l->data;
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));
249 g_object_notify (G_OBJECT (self), "ready");
252 g_signal_emit (self, signals[UPDATED], 0);
253 g_object_unref (self);
254 tp_weak_ref_destroy (wr);
258 empathy_connection_managers_update (EmpathyConnectionManagers *self)
260 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
262 tp_list_connection_managers_async (priv->dbus,
263 empathy_connection_managers_listed_cb,
264 tp_weak_ref_new (self, NULL, NULL));
268 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
270 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
275 TpConnectionManager *
276 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
279 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
282 for (l = priv->cms ; l != NULL; l = g_list_next (l))
284 TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
286 if (!tp_strdiff (tp_connection_manager_get_name (c), cm))
294 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
296 EmpathyConnectionManagersPriv *priv;
298 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self), 0);
300 priv = GET_PRIV (self);
302 return g_list_length (priv->cms);
306 notify_ready_cb (EmpathyConnectionManagers *self,
308 GSimpleAsyncResult *result)
310 g_simple_async_result_complete (result);
311 g_object_unref (result);
315 empathy_connection_managers_prepare_async (
316 EmpathyConnectionManagers *self,
317 GAsyncReadyCallback callback,
320 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
321 GSimpleAsyncResult *result;
323 result = g_simple_async_result_new (G_OBJECT (managers),
324 callback, user_data, empathy_connection_managers_prepare_finish);
328 g_simple_async_result_complete_in_idle (result);
329 g_object_unref (result);
333 g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
338 empathy_connection_managers_prepare_finish (
339 EmpathyConnectionManagers *self,
340 GAsyncResult *result,
343 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
345 g_return_val_if_fail (g_simple_async_result_is_valid (result,
346 G_OBJECT (self), empathy_connection_managers_prepare_finish), FALSE);
348 if (g_simple_async_result_propagate_error (simple, error))