]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connectivity.c
Merge branch 'mc5', fixes bug #590165
[empathy.git] / libempathy / empathy-connectivity.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /*
3  * Copyright (C) 2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  */
21
22 #include "config.h"
23 #include "empathy-connectivity.h"
24
25 #ifdef HAVE_NM
26 #include <nm-client.h>
27 #endif
28
29 #ifdef HAVE_CONNMAN
30 #include <dbus/dbus-glib.h>
31 #endif
32
33 #include <telepathy-glib/util.h>
34
35 #include "empathy-utils.h"
36 #include "empathy-marshal.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_CONNECTIVITY
39 #include "empathy-debug.h"
40
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectivity)
42
43 typedef struct {
44 #ifdef HAVE_NM
45   NMClient *nm_client;
46   gulong state_change_signal_id;
47 #endif
48
49 #ifdef HAVE_CONNMAN
50   DBusGProxy *proxy;
51 #endif
52
53   gboolean connected;
54   gboolean use_conn;
55 } EmpathyConnectivityPriv;
56
57 enum {
58   STATE_CHANGE,
59   LAST_SIGNAL
60 };
61
62 enum {
63   PROP_0,
64   PROP_USE_CONN,
65 };
66
67 static guint signals[LAST_SIGNAL];
68 static EmpathyConnectivity *connectivity_singleton = NULL;
69
70 G_DEFINE_TYPE (EmpathyConnectivity, empathy_connectivity, G_TYPE_OBJECT);
71
72 static void
73 connectivity_change_state (EmpathyConnectivity *connectivity,
74     gboolean new_state)
75 {
76   EmpathyConnectivityPriv *priv;
77
78   priv = GET_PRIV (connectivity);
79
80   if (priv->connected == new_state)
81     return;
82
83   priv->connected = new_state;
84
85   g_signal_emit (connectivity, signals[STATE_CHANGE], 0,
86       priv->connected);
87 }
88
89 #ifdef HAVE_NM
90 static void
91 connectivity_nm_state_change_cb (NMClient *client,
92     const GParamSpec *pspec,
93     EmpathyConnectivity *connectivity)
94 {
95   EmpathyConnectivityPriv *priv;
96   gboolean new_nm_connected;
97   NMState state;
98
99   priv = GET_PRIV (connectivity);
100
101   if (!priv->use_conn)
102     return;
103
104   state = nm_client_get_state (priv->nm_client);
105   new_nm_connected = !(state == NM_STATE_CONNECTING
106       || state == NM_STATE_DISCONNECTED);
107
108   DEBUG ("New NetworkManager network state %d", state);
109
110   connectivity_change_state (connectivity, new_nm_connected);
111 }
112 #endif
113
114 #ifdef HAVE_CONNMAN
115 static void
116 connectivity_connman_state_changed_cb (DBusGProxy *proxy,
117     const gchar *new_state,
118     EmpathyConnectivity *connectivity)
119 {
120   EmpathyConnectivityPriv *priv;
121   gboolean new_connected;
122
123   priv = GET_PRIV (connectivity);
124
125   if (!priv->use_conn)
126     return;
127
128   new_connected = !tp_strdiff (new_state, "online");
129
130   DEBUG ("New ConnMan network state %s", new_state);
131
132   connectivity_change_state (connectivity, new_connected);
133 }
134
135 static void
136 connectivity_connman_check_state_cb (DBusGProxy *proxy,
137     DBusGProxyCall *call_id,
138     gpointer user_data)
139 {
140   EmpathyConnectivity *connectivity = (EmpathyConnectivity *) user_data;
141   GError *error = NULL;
142   gchar *state;
143
144   if (dbus_g_proxy_end_call (proxy, call_id, &error,
145           G_TYPE_STRING, &state, G_TYPE_INVALID))
146     {
147       connectivity_connman_state_changed_cb (proxy, state,
148           connectivity);
149       g_free (state);
150     }
151   else
152     {
153       DEBUG ("Failed to call GetState: %s", error->message);
154       connectivity_connman_state_changed_cb (proxy, "offline",
155           connectivity);
156     }
157 }
158
159 static void
160 connectivity_connman_check_state (EmpathyConnectivity *connectivity)
161 {
162   EmpathyConnectivityPriv *priv;
163
164   priv = GET_PRIV (connectivity);
165
166   dbus_g_proxy_begin_call (priv->proxy, "GetState",
167       connectivity_connman_check_state_cb, connectivity, NULL,
168       G_TYPE_INVALID);
169 }
170 #endif
171
172 static void
173 empathy_connectivity_init (EmpathyConnectivity *connectivity)
174 {
175   EmpathyConnectivityPriv *priv;
176 #ifdef HAVE_CONNMAN
177   DBusGConnection *connection;
178   GError *error = NULL;
179 #endif
180
181   priv = G_TYPE_INSTANCE_GET_PRIVATE (connectivity,
182       EMPATHY_TYPE_CONNECTIVITY, EmpathyConnectivityPriv);
183
184   connectivity->priv = priv;
185
186   priv->use_conn = TRUE;
187
188 #ifdef HAVE_NM
189   priv->nm_client = nm_client_new ();
190   if (priv->nm_client != NULL)
191     {
192       priv->state_change_signal_id = g_signal_connect (priv->nm_client,
193           "notify::" NM_CLIENT_STATE,
194           G_CALLBACK (connectivity_nm_state_change_cb), connectivity);
195
196       connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
197     }
198   else
199     {
200       DEBUG ("Failed to get NetworkManager proxy");
201     }
202 #endif
203
204 #ifdef HAVE_CONNMAN
205   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
206   if (connection != NULL)
207     {
208       priv->proxy = dbus_g_proxy_new_for_name (connection,
209           "org.moblin.connman", "/",
210           "org.moblin.connman.Manager");
211
212       dbus_g_object_register_marshaller (
213           _empathy_marshal_VOID__STRING,
214           G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
215
216       dbus_g_proxy_add_signal (priv->proxy, "StateChanged",
217           G_TYPE_STRING, G_TYPE_INVALID);
218
219       dbus_g_proxy_connect_signal (priv->proxy, "StateChanged",
220           G_CALLBACK (connectivity_connman_state_changed_cb),
221           connectivity, NULL);
222
223       connectivity_connman_check_state (connectivity);
224     }
225   else
226     {
227       DEBUG ("Failed to get system bus connection: %s", error->message);
228       g_error_free (error);
229     }
230 #endif
231
232 #if !defined(HAVE_NM) || !defined(HAVE_CONNMAN)
233   priv->connected = TRUE;
234 #endif
235 }
236
237 static void
238 connectivity_finalize (GObject *object)
239 {
240 #ifdef HAVE_NM
241   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
242   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
243
244   if (priv->nm_client != NULL)
245     {
246       g_signal_handler_disconnect (priv->nm_client,
247           priv->state_change_signal_id);
248       priv->state_change_signal_id = 0;
249       g_object_unref (priv->nm_client);
250       priv->nm_client = NULL;
251     }
252 #endif
253
254 #ifdef HAVE_CONNMAN
255   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
256   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
257
258   if (priv->proxy != NULL)
259     {
260       dbus_g_proxy_disconnect_signal (priv->proxy, "StateChanged",
261           G_CALLBACK (connectivity_connman_state_changed_cb), connectivity);
262
263       g_object_unref (priv->proxy);
264       priv->proxy = NULL;
265     }
266 #endif
267
268   G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (object);
269 }
270
271 static void
272 connectivity_dispose (GObject *object)
273 {
274   G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (object);
275 }
276
277 static GObject *
278 connectivity_constructor (GType type,
279     guint n_construct_params,
280     GObjectConstructParam *construct_params)
281 {
282   GObject *retval;
283
284   if (!connectivity_singleton)
285     {
286       retval = G_OBJECT_CLASS (empathy_connectivity_parent_class)->constructor
287         (type, n_construct_params, construct_params);
288
289       connectivity_singleton = EMPATHY_CONNECTIVITY (retval);
290       g_object_add_weak_pointer (retval, (gpointer) &connectivity_singleton);
291     }
292   else
293     {
294       retval = g_object_ref (connectivity_singleton);
295     }
296
297   return retval;
298 }
299
300 static void
301 connectivity_get_property (GObject *object,
302     guint param_id,
303     GValue *value,
304     GParamSpec *pspec)
305 {
306   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
307
308   switch (param_id)
309     {
310     case PROP_USE_CONN:
311       g_value_set_boolean (value, empathy_connectivity_get_use_conn (
312               connectivity));
313       break;
314     default:
315       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
316       break;
317     };
318 }
319
320 static void
321 connectivity_set_property (GObject *object,
322     guint param_id,
323     const GValue *value,
324     GParamSpec *pspec)
325 {
326   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
327
328   switch (param_id)
329     {
330     case PROP_USE_CONN:
331       empathy_connectivity_set_use_conn (connectivity,
332           g_value_get_boolean (value));
333       break;
334     default:
335       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
336       break;
337     };
338 }
339
340 static void
341 empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
342 {
343   GObjectClass *oclass = G_OBJECT_CLASS (klass);
344
345   oclass->finalize = connectivity_finalize;
346   oclass->dispose = connectivity_dispose;
347   oclass->constructor = connectivity_constructor;
348   oclass->get_property = connectivity_get_property;
349   oclass->set_property = connectivity_set_property;
350
351   signals[STATE_CHANGE] =
352     g_signal_new ("state-change",
353         G_TYPE_FROM_CLASS (klass),
354         G_SIGNAL_RUN_LAST,
355         0,
356         NULL, NULL,
357         _empathy_marshal_VOID__BOOLEAN,
358         G_TYPE_NONE,
359         1, G_TYPE_BOOLEAN, NULL);
360
361   g_object_class_install_property (oclass,
362       PROP_USE_CONN,
363       g_param_spec_boolean ("use-conn",
364           "Use connectivity managers",
365           "Set presence according to connectivity managers",
366           TRUE,
367           G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
368
369   g_type_class_add_private (oclass, sizeof (EmpathyConnectivityPriv));
370 }
371
372 /* public methods */
373
374 EmpathyConnectivity *
375 empathy_connectivity_dup_singleton (void)
376 {
377   return g_object_new (EMPATHY_TYPE_CONNECTIVITY, NULL);
378 }
379
380 gboolean
381 empathy_connectivity_is_online (EmpathyConnectivity *connectivity)
382 {
383   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
384
385   if (priv->use_conn)
386     {
387       return priv->connected;
388     }
389   else
390     {
391       return TRUE;
392     }
393 }
394
395 gboolean
396 empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity)
397 {
398   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
399
400   return priv->use_conn;
401 }
402
403 void
404 empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
405     gboolean use_conn)
406 {
407   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
408
409   if (use_conn == priv->use_conn)
410     return;
411
412   DEBUG ("use_conn gconf key changed; new value = %s",
413       use_conn ? "true" : "false");
414
415   priv->use_conn = use_conn;
416
417 #if defined(HAVE_NM) || defined(HAVE_CONNMAN)
418   if (use_conn)
419     {
420 #if defined(HAVE_NM)
421       connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
422 #elif defined(HAVE_CONNMAN)
423       connectivity_connman_check_state (connectivity);
424 #endif
425     }
426   else
427 #endif
428     {
429       connectivity_change_state (connectivity, TRUE);
430     }
431
432   g_object_notify (G_OBJECT (connectivity), "use-conn");
433 }