]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connectivity.c
Merge branch 'log-window-webview'
[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
91 #if !defined(NM_CHECK_VERSION)
92 #define NM_CHECK_VERSION(x,y,z) 0
93 #endif
94
95 static void
96 connectivity_nm_state_change_cb (NMClient *client,
97     const GParamSpec *pspec,
98     EmpathyConnectivity *connectivity)
99 {
100   EmpathyConnectivityPriv *priv;
101   gboolean new_nm_connected;
102   NMState state;
103
104   priv = GET_PRIV (connectivity);
105
106   if (!priv->use_conn)
107     return;
108
109   state = nm_client_get_state (priv->nm_client);
110   new_nm_connected = !(state == NM_STATE_CONNECTING
111 #if NM_CHECK_VERSION(0,8,992)
112       || state == NM_STATE_DISCONNECTING
113 #endif
114       || state == NM_STATE_ASLEEP
115       || state == NM_STATE_DISCONNECTED);
116
117   DEBUG ("New NetworkManager network state %d (connected: %s)", state,
118       new_nm_connected ? "true" : "false");
119
120   connectivity_change_state (connectivity, new_nm_connected);
121 }
122 #endif
123
124 #ifdef HAVE_CONNMAN
125 static void
126 connectivity_connman_state_changed_cb (DBusGProxy *proxy,
127     const gchar *new_state,
128     EmpathyConnectivity *connectivity)
129 {
130   EmpathyConnectivityPriv *priv;
131   gboolean new_connected;
132
133   priv = GET_PRIV (connectivity);
134
135   if (!priv->use_conn)
136     return;
137
138   new_connected = !tp_strdiff (new_state, "online");
139
140   DEBUG ("New ConnMan network state %s", new_state);
141
142   connectivity_change_state (connectivity, new_connected);
143 }
144
145 static void
146 connectivity_connman_check_state_cb (DBusGProxy *proxy,
147     DBusGProxyCall *call_id,
148     gpointer user_data)
149 {
150   EmpathyConnectivity *connectivity = (EmpathyConnectivity *) user_data;
151   GError *error = NULL;
152   gchar *state;
153
154   if (dbus_g_proxy_end_call (proxy, call_id, &error,
155           G_TYPE_STRING, &state, G_TYPE_INVALID))
156     {
157       connectivity_connman_state_changed_cb (proxy, state,
158           connectivity);
159       g_free (state);
160     }
161   else
162     {
163       DEBUG ("Failed to call GetState: %s", error->message);
164       connectivity_connman_state_changed_cb (proxy, "offline",
165           connectivity);
166     }
167 }
168
169 static void
170 connectivity_connman_check_state (EmpathyConnectivity *connectivity)
171 {
172   EmpathyConnectivityPriv *priv;
173
174   priv = GET_PRIV (connectivity);
175
176   dbus_g_proxy_begin_call (priv->proxy, "GetState",
177       connectivity_connman_check_state_cb, connectivity, NULL,
178       G_TYPE_INVALID);
179 }
180 #endif
181
182 static void
183 empathy_connectivity_init (EmpathyConnectivity *connectivity)
184 {
185   EmpathyConnectivityPriv *priv;
186 #ifdef HAVE_CONNMAN
187   DBusGConnection *connection;
188   GError *error = NULL;
189 #endif
190
191   priv = G_TYPE_INSTANCE_GET_PRIVATE (connectivity,
192       EMPATHY_TYPE_CONNECTIVITY, EmpathyConnectivityPriv);
193
194   connectivity->priv = priv;
195
196   priv->use_conn = TRUE;
197
198 #ifdef HAVE_NM
199   priv->nm_client = nm_client_new ();
200   if (priv->nm_client != NULL)
201     {
202       priv->state_change_signal_id = g_signal_connect (priv->nm_client,
203           "notify::" NM_CLIENT_STATE,
204           G_CALLBACK (connectivity_nm_state_change_cb), connectivity);
205
206       connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
207     }
208   else
209     {
210       DEBUG ("Failed to get NetworkManager proxy");
211     }
212 #endif
213
214 #ifdef HAVE_CONNMAN
215   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
216   if (connection != NULL)
217     {
218       priv->proxy = dbus_g_proxy_new_for_name (connection,
219           "net.connman", "/",
220           "net.connman.Manager");
221
222       dbus_g_object_register_marshaller (
223           _empathy_marshal_VOID__STRING,
224           G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
225
226       dbus_g_proxy_add_signal (priv->proxy, "StateChanged",
227           G_TYPE_STRING, G_TYPE_INVALID);
228
229       dbus_g_proxy_connect_signal (priv->proxy, "StateChanged",
230           G_CALLBACK (connectivity_connman_state_changed_cb),
231           connectivity, NULL);
232
233       connectivity_connman_check_state (connectivity);
234     }
235   else
236     {
237       DEBUG ("Failed to get system bus connection: %s", error->message);
238       g_error_free (error);
239     }
240 #endif
241
242 #if !defined(HAVE_NM) && !defined(HAVE_CONNMAN)
243   priv->connected = TRUE;
244 #endif
245 }
246
247 static void
248 connectivity_finalize (GObject *object)
249 {
250 #ifdef HAVE_NM
251   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
252   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
253
254   if (priv->nm_client != NULL)
255     {
256       g_signal_handler_disconnect (priv->nm_client,
257           priv->state_change_signal_id);
258       priv->state_change_signal_id = 0;
259       g_object_unref (priv->nm_client);
260       priv->nm_client = NULL;
261     }
262 #endif
263
264 #ifdef HAVE_CONNMAN
265   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
266   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
267
268   if (priv->proxy != NULL)
269     {
270       dbus_g_proxy_disconnect_signal (priv->proxy, "StateChanged",
271           G_CALLBACK (connectivity_connman_state_changed_cb), connectivity);
272
273       g_object_unref (priv->proxy);
274       priv->proxy = NULL;
275     }
276 #endif
277
278   G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (object);
279 }
280
281 static void
282 connectivity_dispose (GObject *object)
283 {
284   G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (object);
285 }
286
287 static GObject *
288 connectivity_constructor (GType type,
289     guint n_construct_params,
290     GObjectConstructParam *construct_params)
291 {
292   GObject *retval;
293
294   if (!connectivity_singleton)
295     {
296       retval = G_OBJECT_CLASS (empathy_connectivity_parent_class)->constructor
297         (type, n_construct_params, construct_params);
298
299       connectivity_singleton = EMPATHY_CONNECTIVITY (retval);
300       g_object_add_weak_pointer (retval, (gpointer) &connectivity_singleton);
301     }
302   else
303     {
304       retval = g_object_ref (connectivity_singleton);
305     }
306
307   return retval;
308 }
309
310 static void
311 connectivity_get_property (GObject *object,
312     guint param_id,
313     GValue *value,
314     GParamSpec *pspec)
315 {
316   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
317
318   switch (param_id)
319     {
320     case PROP_USE_CONN:
321       g_value_set_boolean (value, empathy_connectivity_get_use_conn (
322               connectivity));
323       break;
324     default:
325       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
326       break;
327     };
328 }
329
330 static void
331 connectivity_set_property (GObject *object,
332     guint param_id,
333     const GValue *value,
334     GParamSpec *pspec)
335 {
336   EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
337
338   switch (param_id)
339     {
340     case PROP_USE_CONN:
341       empathy_connectivity_set_use_conn (connectivity,
342           g_value_get_boolean (value));
343       break;
344     default:
345       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
346       break;
347     };
348 }
349
350 static void
351 empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
352 {
353   GObjectClass *oclass = G_OBJECT_CLASS (klass);
354
355   oclass->finalize = connectivity_finalize;
356   oclass->dispose = connectivity_dispose;
357   oclass->constructor = connectivity_constructor;
358   oclass->get_property = connectivity_get_property;
359   oclass->set_property = connectivity_set_property;
360
361   signals[STATE_CHANGE] =
362     g_signal_new ("state-change",
363         G_TYPE_FROM_CLASS (klass),
364         G_SIGNAL_RUN_LAST,
365         0,
366         NULL, NULL,
367         _empathy_marshal_VOID__BOOLEAN,
368         G_TYPE_NONE,
369         1, G_TYPE_BOOLEAN, NULL);
370
371   g_object_class_install_property (oclass,
372       PROP_USE_CONN,
373       g_param_spec_boolean ("use-conn",
374           "Use connectivity managers",
375           "Set presence according to connectivity managers",
376           TRUE,
377           G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
378
379   g_type_class_add_private (oclass, sizeof (EmpathyConnectivityPriv));
380 }
381
382 /* public methods */
383
384 EmpathyConnectivity *
385 empathy_connectivity_dup_singleton (void)
386 {
387   return g_object_new (EMPATHY_TYPE_CONNECTIVITY, NULL);
388 }
389
390 gboolean
391 empathy_connectivity_is_online (EmpathyConnectivity *connectivity)
392 {
393   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
394
395   return priv->connected;
396 }
397
398 gboolean
399 empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity)
400 {
401   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
402
403   return priv->use_conn;
404 }
405
406 void
407 empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
408     gboolean use_conn)
409 {
410   EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
411
412   if (use_conn == priv->use_conn)
413     return;
414
415   DEBUG ("use_conn GSetting key changed; new value = %s",
416       use_conn ? "true" : "false");
417
418   priv->use_conn = use_conn;
419
420 #if defined(HAVE_NM) || defined(HAVE_CONNMAN)
421   if (use_conn)
422     {
423 #if defined(HAVE_NM)
424       connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
425 #elif defined(HAVE_CONNMAN)
426       connectivity_connman_check_state (connectivity);
427 #endif
428     }
429   else
430 #endif
431     {
432       connectivity_change_state (connectivity, TRUE);
433     }
434
435   g_object_notify (G_OBJECT (connectivity), "use-conn");
436 }