build: remove -enumtypes rules
[empathy-mirror.git] / libempathy / empathy-connection-managers.c
blob30a9b8471ea412c6d6c46b90d44a030c02019d8d
1 /*
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 <stdio.h>
23 #include <stdlib.h>
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,
37 G_TYPE_OBJECT)
39 /* signal enum */
40 enum
42 UPDATED,
43 LAST_SIGNAL
46 static guint signals[LAST_SIGNAL] = {0};
48 /* properties */
49 enum {
50 PROP_READY = 1
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;
63 gboolean ready;
65 GList *cms;
67 TpDBusDaemon *dbus;
70 static void
71 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
73 EmpathyConnectionManagersPriv *priv =
74 G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
75 EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
77 obj->priv = priv;
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);
89 static GObject *
90 empathy_connection_managers_constructor (GType type,
91 guint n_construct_params,
92 GObjectConstructParam *construct_params)
94 if (managers != NULL)
95 return g_object_ref (managers);
97 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);
103 return managers;
108 static void
109 empathy_connection_managers_get_property (GObject *object,
110 guint prop_id,
111 GValue *value,
112 GParamSpec *pspec)
114 EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
115 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
117 switch (prop_id)
119 case PROP_READY:
120 g_value_set_boolean (value, priv->ready);
121 break;
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 break;
128 static void
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",
144 "Ready",
145 "Whether the connection manager information is ready to be used",
146 FALSE,
147 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
149 signals[UPDATED] = g_signal_new ("updated",
150 G_TYPE_FROM_CLASS (object_class),
151 G_SIGNAL_RUN_LAST,
152 0, NULL, NULL,
153 g_cclosure_marshal_VOID__VOID,
154 G_TYPE_NONE, 0);
157 static void
158 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
160 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
161 GList *l;
163 for (l = priv->cms ; l != NULL ; l = g_list_next (l))
165 g_object_unref (l->data);
167 g_list_free (priv->cms);
169 priv->cms = NULL;
172 static void
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)
179 return;
181 priv->dispose_has_run = TRUE;
183 if (priv->dbus != NULL)
184 g_object_unref (priv->dbus);
185 priv->dbus = NULL;
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));
202 gboolean
203 empathy_connection_managers_is_ready (EmpathyConnectionManagers *self)
205 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
206 return priv->ready;
209 static void
210 empathy_connection_managers_listed_cb (TpConnectionManager * const *cms,
211 gsize n_cms,
212 const GError *error,
213 gpointer user_data,
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);
223 if (error != NULL)
225 DEBUG ("Failed to get connection managers: %s", error->message);
226 goto out;
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));
236 out:
237 g_object_ref (weak_object);
238 if (!priv->ready)
240 priv->ready = TRUE;
241 g_object_notify (weak_object, "ready");
243 g_signal_emit (weak_object, signals[UPDATED], 0);
244 g_object_unref (weak_object);
247 void
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));
257 GList *
258 empathy_connection_managers_get_cms (EmpathyConnectionManagers *self)
260 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
262 return priv->cms;
265 TpConnectionManager *
266 empathy_connection_managers_get_cm (EmpathyConnectionManagers *self,
267 const gchar *cm)
269 EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
270 GList *l;
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))
277 return c;
280 return NULL;
283 guint
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);
295 static void
296 notify_ready_cb (EmpathyConnectionManagers *self,
297 GParamSpec *spec,
298 GSimpleAsyncResult *result)
300 g_simple_async_result_complete (result);
301 g_object_unref (result);
304 void
305 empathy_connection_managers_prepare_async (
306 EmpathyConnectionManagers *self,
307 GAsyncReadyCallback callback,
308 gpointer user_data)
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);
316 if (priv->ready)
318 g_simple_async_result_complete_in_idle (result);
319 g_object_unref (result);
320 return;
323 g_signal_connect (self, "notify::ready", G_CALLBACK (notify_ready_cb),
324 result);
327 gboolean
328 empathy_connection_managers_prepare_finish (
329 EmpathyConnectionManagers *self,
330 GAsyncResult *result,
331 GError **error)
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))
339 return FALSE;
341 return TRUE;