]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connection-managers.c
Updated Basque language
[empathy.git] / libempathy / empathy-connection-managers.c
1 /*
2  * empathy-connection-managers.c - Source for EmpathyConnectionManagers
3  * Copyright (C) 2009 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <telepathy-glib/connection-manager.h>
26 #include <telepathy-glib/util.h>
27
28 #include "empathy-connection-managers.h"
29 #include "empathy-utils.h"
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include <libempathy/empathy-debug.h>
33
34 static GObject *managers = NULL;
35
36 G_DEFINE_TYPE(EmpathyConnectionManagers, empathy_connection_managers,
37     G_TYPE_OBJECT)
38
39 /* signal enum */
40 enum
41 {
42     UPDATED,
43     LAST_SIGNAL
44 };
45
46 static guint signals[LAST_SIGNAL] = {0};
47
48 /* properties */
49 enum {
50   PROP_READY = 1
51 };
52
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectionManagers)
54
55
56 /* private structure */
57 typedef struct _EmpathyConnectionManagersPriv
58   EmpathyConnectionManagersPriv;
59
60 struct _EmpathyConnectionManagersPriv
61 {
62   gboolean dispose_has_run;
63   gboolean ready;
64
65   GList *cms;
66
67   TpDBusDaemon *dbus;
68 };
69
70 static void
71 empathy_connection_managers_init (EmpathyConnectionManagers *obj)
72 {
73   EmpathyConnectionManagersPriv *priv =
74     G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
75       EMPATHY_TYPE_CONNECTION_MANAGERS, EmpathyConnectionManagersPriv);
76
77   obj->priv = priv;
78
79   priv->dbus = tp_dbus_daemon_dup (NULL);
80   g_assert (priv->dbus != NULL);
81
82   empathy_connection_managers_update (obj);
83
84   /* allocate any data required by the object here */
85 }
86
87 static void empathy_connection_managers_dispose (GObject *object);
88
89 static GObject *
90 empathy_connection_managers_constructor (GType type,
91                         guint n_construct_params,
92                         GObjectConstructParam *construct_params)
93 {
94   if (managers != NULL)
95     return g_object_ref (managers);
96
97   managers =
98       G_OBJECT_CLASS (empathy_connection_managers_parent_class)->constructor
99           (type, n_construct_params, construct_params);
100
101   g_object_add_weak_pointer (managers, (gpointer) &managers);
102
103   return managers;
104 }
105
106
107
108 static void
109 empathy_connection_managers_get_property (GObject *object,
110     guint prop_id,
111     GValue *value,
112     GParamSpec *pspec)
113 {
114   EmpathyConnectionManagers *managers = EMPATHY_CONNECTION_MANAGERS (object);
115   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
116
117   switch (prop_id)
118     {
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;
125     }
126 }
127
128 static void
129 empathy_connection_managers_class_init (
130     EmpathyConnectionManagersClass *empathy_connection_managers_class)
131 {
132   GObjectClass *object_class =
133       G_OBJECT_CLASS (empathy_connection_managers_class);
134
135   g_type_class_add_private (empathy_connection_managers_class, sizeof
136       (EmpathyConnectionManagersPriv));
137
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;
141
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));
148
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);
155 }
156
157 static void
158 empathy_connection_managers_free_cm_list (EmpathyConnectionManagers *self)
159 {
160   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
161   GList *l;
162
163   for (l = priv->cms ; l != NULL ; l = g_list_next (l))
164     {
165       g_object_unref (l->data);
166     }
167   g_list_free (priv->cms);
168
169   priv->cms = NULL;
170 }
171
172 static void
173 empathy_connection_managers_dispose (GObject *object)
174 {
175   EmpathyConnectionManagers *self = EMPATHY_CONNECTION_MANAGERS (object);
176   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
177
178   if (priv->dispose_has_run)
179     return;
180
181   priv->dispose_has_run = TRUE;
182
183   if (priv->dbus != NULL)
184     g_object_unref (priv->dbus);
185   priv->dbus = NULL;
186
187   empathy_connection_managers_free_cm_list (self);
188
189   /* release any references held by the object here */
190
191   if (G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose)
192     G_OBJECT_CLASS (empathy_connection_managers_parent_class)->dispose (object);
193 }
194
195 EmpathyConnectionManagers *
196 empathy_connection_managers_dup_singleton (void)
197 {
198   return EMPATHY_CONNECTION_MANAGERS (
199       g_object_new (EMPATHY_TYPE_CONNECTION_MANAGERS, NULL));
200 }
201
202 gboolean
203 empathy_connection_managers_is_ready (EmpathyConnectionManagers *managers)
204 {
205   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
206   return priv->ready;
207 }
208
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)
215 {
216   EmpathyConnectionManagers *self =
217     EMPATHY_CONNECTION_MANAGERS (weak_object);
218   EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
219   TpConnectionManager * const *iter;
220
221   empathy_connection_managers_free_cm_list (self);
222
223   if (error != NULL)
224     {
225       DEBUG ("Failed to get connection managers: %s", error->message);
226       goto out;
227     }
228
229   for (iter = cms ; iter != NULL && *iter != NULL; iter++)
230     {
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));
234     }
235
236 out:
237   g_object_ref (weak_object);
238   if (!priv->ready)
239     {
240       priv->ready = TRUE;
241       g_object_notify (weak_object, "ready");
242     }
243   g_signal_emit (weak_object, signals[UPDATED], 0);
244   g_object_unref (weak_object);
245 }
246
247 void
248 empathy_connection_managers_update (EmpathyConnectionManagers *managers)
249 {
250   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
251
252   tp_list_connection_managers (priv->dbus,
253     empathy_connection_managers_listed_cb,
254     NULL, NULL, G_OBJECT (managers));
255 }
256
257 GList *
258 empathy_connection_managers_get_cms (EmpathyConnectionManagers *managers)
259 {
260   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
261
262   return priv->cms;
263 }
264
265 TpConnectionManager *
266 empathy_connection_managers_get_cm (EmpathyConnectionManagers *managers,
267   const gchar *cm)
268 {
269   EmpathyConnectionManagersPriv *priv = GET_PRIV (managers);
270   GList *l;
271
272   for (l = priv->cms ; l != NULL; l = g_list_next (l))
273     {
274       TpConnectionManager *c = TP_CONNECTION_MANAGER (l->data);
275
276       if (!tp_strdiff (c->name, cm))
277         return c;
278     }
279
280   return NULL;
281 }
282
283 guint
284 empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *managers)
285 {
286   EmpathyConnectionManagersPriv *priv;
287
288   g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (managers), 0);
289
290   priv = GET_PRIV (managers);
291
292   return g_list_length (priv->cms);
293 }