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
25 #include <telepathy-glib/connection-manager.h>
26 #include <telepathy-glib/util.h>
28 #include "empathy-connection-managers.h"
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include <libempathy/empathy-debug.h>
34 static GObject
*managers
= NULL
;
36 G_DEFINE_TYPE(EmpathyConnectionManagers
, empathy_connection_managers
,
46 static guint signals
[LAST_SIGNAL
] = {0};
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
56 /* private structure */
57 typedef struct _EmpathyConnectionManagersPriv
58 EmpathyConnectionManagersPriv
;
60 struct _EmpathyConnectionManagersPriv
62 gboolean dispose_has_run
;
71 empathy_connection_managers_init (EmpathyConnectionManagers
*obj
)
73 EmpathyConnectionManagersPriv
*priv
=
74 G_TYPE_INSTANCE_GET_PRIVATE ((obj
), \
75 EMPATHY_TYPE_CONNECTION_MANAGERS
, EmpathyConnectionManagersPriv
);
79 priv
->dbus
= tp_dbus_daemon_dup (NULL
);
80 g_assert (priv
->dbus
!= NULL
);
82 empathy_connection_managers_update (obj
);
84 /* allocate any data required by the object here */
87 static void empathy_connection_managers_dispose (GObject
*object
);
90 empathy_connection_managers_constructor (GType type
,
91 guint n_construct_params
,
92 GObjectConstructParam
*construct_params
)
95 return g_object_ref (managers
);
98 G_OBJECT_CLASS (empathy_connection_managers_parent_class
)->constructor
99 (type
, n_construct_params
, construct_params
);
101 g_object_add_weak_pointer (managers
, (gpointer
) &managers
);
109 empathy_connection_managers_get_property (GObject
*object
,
114 EmpathyConnectionManagers
*self
= EMPATHY_CONNECTION_MANAGERS (object
);
115 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
120 g_value_set_boolean (value
, priv
->ready
);
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
129 empathy_connection_managers_class_init (
130 EmpathyConnectionManagersClass
*empathy_connection_managers_class
)
132 GObjectClass
*object_class
=
133 G_OBJECT_CLASS (empathy_connection_managers_class
);
135 g_type_class_add_private (empathy_connection_managers_class
, sizeof
136 (EmpathyConnectionManagersPriv
));
138 object_class
->constructor
= empathy_connection_managers_constructor
;
139 object_class
->dispose
= empathy_connection_managers_dispose
;
140 object_class
->get_property
= empathy_connection_managers_get_property
;
142 g_object_class_install_property (object_class
, PROP_READY
,
143 g_param_spec_boolean ("ready",
145 "Whether the connection manager information is ready to be used",
147 G_PARAM_STATIC_STRINGS
| G_PARAM_READABLE
));
149 signals
[UPDATED
] = g_signal_new ("updated",
150 G_TYPE_FROM_CLASS (object_class
),
153 g_cclosure_marshal_VOID__VOID
,
158 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers
*self
)
160 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
163 for (l
= priv
->cms
; l
!= NULL
; l
= g_list_next (l
))
165 g_object_unref (l
->data
);
167 g_list_free (priv
->cms
);
173 empathy_connection_managers_dispose (GObject
*object
)
175 EmpathyConnectionManagers
*self
= EMPATHY_CONNECTION_MANAGERS (object
);
176 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
178 if (priv
->dispose_has_run
)
181 priv
->dispose_has_run
= TRUE
;
183 if (priv
->dbus
!= NULL
)
184 g_object_unref (priv
->dbus
);
187 empathy_connection_managers_free_cm_list (self
);
189 /* release any references held by the object here */
191 if (G_OBJECT_CLASS (empathy_connection_managers_parent_class
)->dispose
)
192 G_OBJECT_CLASS (empathy_connection_managers_parent_class
)->dispose (object
);
195 EmpathyConnectionManagers
*
196 empathy_connection_managers_dup_singleton (void)
198 return EMPATHY_CONNECTION_MANAGERS (
199 g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS
, NULL
));
203 empathy_connection_managers_is_ready (EmpathyConnectionManagers
*self
)
205 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
210 empathy_connection_managers_listed_cb (TpConnectionManager
* const *cms
,
214 GObject
*weak_object
)
216 EmpathyConnectionManagers
*self
=
217 EMPATHY_CONNECTION_MANAGERS (weak_object
);
218 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
219 TpConnectionManager
* const *iter
;
221 empathy_connection_managers_free_cm_list (self
);
225 DEBUG ("Failed to get connection managers: %s", error
->message
);
229 for (iter
= cms
; iter
!= NULL
&& *iter
!= NULL
; iter
++)
231 /* only list cms that didn't hit errors */
232 if (tp_connection_manager_is_ready (*iter
))
233 priv
->cms
= g_list_prepend (priv
->cms
, g_object_ref (*iter
));
237 g_object_ref (weak_object
);
241 g_object_notify (weak_object
, "ready");
243 g_signal_emit (weak_object
, signals
[UPDATED
], 0);
244 g_object_unref (weak_object
);
248 empathy_connection_managers_update (EmpathyConnectionManagers
*self
)
250 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
252 tp_list_connection_managers (priv
->dbus
,
253 empathy_connection_managers_listed_cb
,
254 NULL
, NULL
, G_OBJECT (self
));
258 empathy_connection_managers_get_cms (EmpathyConnectionManagers
*self
)
260 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
265 TpConnectionManager
*
266 empathy_connection_managers_get_cm (EmpathyConnectionManagers
*self
,
269 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
272 for (l
= priv
->cms
; l
!= NULL
; l
= g_list_next (l
))
274 TpConnectionManager
*c
= TP_CONNECTION_MANAGER (l
->data
);
276 if (!tp_strdiff (c
->name
, cm
))
284 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers
*self
)
286 EmpathyConnectionManagersPriv
*priv
;
288 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (self
), 0);
290 priv
= GET_PRIV (self
);
292 return g_list_length (priv
->cms
);
296 notify_ready_cb (EmpathyConnectionManagers
*self
,
298 GSimpleAsyncResult
*result
)
300 g_simple_async_result_complete (result
);
301 g_object_unref (result
);
305 empathy_connection_managers_prepare_async (
306 EmpathyConnectionManagers
*self
,
307 GAsyncReadyCallback callback
,
310 EmpathyConnectionManagersPriv
*priv
= GET_PRIV (self
);
311 GSimpleAsyncResult
*result
;
313 result
= g_simple_async_result_new (G_OBJECT (managers
),
314 callback
, user_data
, empathy_connection_managers_prepare_finish
);
318 g_simple_async_result_complete_in_idle (result
);
319 g_object_unref (result
);
323 g_signal_connect (self
, "notify::ready", G_CALLBACK (notify_ready_cb
),
328 empathy_connection_managers_prepare_finish (
329 EmpathyConnectionManagers
*self
,
330 GAsyncResult
*result
,
333 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
335 g_return_val_if_fail (g_simple_async_result_is_valid (result
,
336 G_OBJECT (self
), empathy_connection_managers_prepare_finish
), FALSE
);
338 if (g_simple_async_result_propagate_error (simple
, error
))