1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2009 Collabora Ltd.
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.
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.
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
19 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
23 #include "empathy-connectivity.h"
26 #include <nm-client.h>
30 #include <dbus/dbus-glib.h>
33 #include <telepathy-glib/util.h>
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONNECTIVITY
38 #include "empathy-debug.h"
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectivity)
45 gulong state_change_signal_id;
54 } EmpathyConnectivityPriv;
66 static guint signals[LAST_SIGNAL];
67 static EmpathyConnectivity *connectivity_singleton = NULL;
69 G_DEFINE_TYPE (EmpathyConnectivity, empathy_connectivity, G_TYPE_OBJECT);
72 connectivity_change_state (EmpathyConnectivity *connectivity,
75 EmpathyConnectivityPriv *priv;
77 priv = GET_PRIV (connectivity);
79 if (priv->connected == new_state)
82 priv->connected = new_state;
84 g_signal_emit (connectivity, signals[STATE_CHANGE], 0,
90 #if !defined(NM_CHECK_VERSION)
91 #define NM_CHECK_VERSION(x,y,z) 0
95 connectivity_nm_state_change_cb (NMClient *client,
96 const GParamSpec *pspec,
97 EmpathyConnectivity *connectivity)
99 EmpathyConnectivityPriv *priv;
100 gboolean new_nm_connected;
103 priv = GET_PRIV (connectivity);
108 state = nm_client_get_state (priv->nm_client);
109 new_nm_connected = !(state == NM_STATE_CONNECTING
110 #if NM_CHECK_VERSION(0,8,992)
111 || state == NM_STATE_DISCONNECTING
113 || state == NM_STATE_ASLEEP
114 || state == NM_STATE_DISCONNECTED);
116 DEBUG ("New NetworkManager network state %d (connected: %s)", state,
117 new_nm_connected ? "true" : "false");
119 connectivity_change_state (connectivity, new_nm_connected);
125 connectivity_connman_state_changed_cb (DBusGProxy *proxy,
126 const gchar *new_state,
127 EmpathyConnectivity *connectivity)
129 EmpathyConnectivityPriv *priv;
130 gboolean new_connected;
132 priv = GET_PRIV (connectivity);
137 new_connected = !tp_strdiff (new_state, "online");
139 DEBUG ("New ConnMan network state %s", new_state);
141 connectivity_change_state (connectivity, new_connected);
145 connectivity_connman_check_state_cb (DBusGProxy *proxy,
146 DBusGProxyCall *call_id,
149 EmpathyConnectivity *connectivity = (EmpathyConnectivity *) user_data;
150 GError *error = NULL;
153 if (dbus_g_proxy_end_call (proxy, call_id, &error,
154 G_TYPE_STRING, &state, G_TYPE_INVALID))
156 connectivity_connman_state_changed_cb (proxy, state,
162 DEBUG ("Failed to call GetState: %s", error->message);
163 connectivity_connman_state_changed_cb (proxy, "offline",
169 connectivity_connman_check_state (EmpathyConnectivity *connectivity)
171 EmpathyConnectivityPriv *priv;
173 priv = GET_PRIV (connectivity);
175 dbus_g_proxy_begin_call (priv->proxy, "GetState",
176 connectivity_connman_check_state_cb, connectivity, NULL,
182 empathy_connectivity_init (EmpathyConnectivity *connectivity)
184 EmpathyConnectivityPriv *priv;
186 DBusGConnection *connection;
187 GError *error = NULL;
190 priv = G_TYPE_INSTANCE_GET_PRIVATE (connectivity,
191 EMPATHY_TYPE_CONNECTIVITY, EmpathyConnectivityPriv);
193 connectivity->priv = priv;
195 priv->use_conn = TRUE;
198 priv->nm_client = nm_client_new ();
199 if (priv->nm_client != NULL)
201 priv->state_change_signal_id = g_signal_connect (priv->nm_client,
202 "notify::" NM_CLIENT_STATE,
203 G_CALLBACK (connectivity_nm_state_change_cb), connectivity);
205 connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
209 DEBUG ("Failed to get NetworkManager proxy");
214 connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
215 if (connection != NULL)
217 priv->proxy = dbus_g_proxy_new_for_name (connection,
219 "net.connman.Manager");
221 dbus_g_object_register_marshaller (
222 g_cclosure_marshal_generic,
223 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
225 dbus_g_proxy_add_signal (priv->proxy, "StateChanged",
226 G_TYPE_STRING, G_TYPE_INVALID);
228 dbus_g_proxy_connect_signal (priv->proxy, "StateChanged",
229 G_CALLBACK (connectivity_connman_state_changed_cb),
232 connectivity_connman_check_state (connectivity);
236 DEBUG ("Failed to get system bus connection: %s", error->message);
237 g_error_free (error);
241 #if !defined(HAVE_NM) && !defined(HAVE_CONNMAN)
242 priv->connected = TRUE;
247 connectivity_finalize (GObject *object)
250 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
251 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
253 if (priv->nm_client != NULL)
255 g_signal_handler_disconnect (priv->nm_client,
256 priv->state_change_signal_id);
257 priv->state_change_signal_id = 0;
258 g_object_unref (priv->nm_client);
259 priv->nm_client = NULL;
264 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
265 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
267 if (priv->proxy != NULL)
269 dbus_g_proxy_disconnect_signal (priv->proxy, "StateChanged",
270 G_CALLBACK (connectivity_connman_state_changed_cb), connectivity);
272 g_object_unref (priv->proxy);
277 G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (object);
281 connectivity_dispose (GObject *object)
283 G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (object);
287 connectivity_constructor (GType type,
288 guint n_construct_params,
289 GObjectConstructParam *construct_params)
293 if (!connectivity_singleton)
295 retval = G_OBJECT_CLASS (empathy_connectivity_parent_class)->constructor
296 (type, n_construct_params, construct_params);
298 connectivity_singleton = EMPATHY_CONNECTIVITY (retval);
299 g_object_add_weak_pointer (retval, (gpointer) &connectivity_singleton);
303 retval = g_object_ref (connectivity_singleton);
310 connectivity_get_property (GObject *object,
315 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
320 g_value_set_boolean (value, empathy_connectivity_get_use_conn (
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
330 connectivity_set_property (GObject *object,
335 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
340 empathy_connectivity_set_use_conn (connectivity,
341 g_value_get_boolean (value));
344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
350 empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
352 GObjectClass *oclass = G_OBJECT_CLASS (klass);
354 oclass->finalize = connectivity_finalize;
355 oclass->dispose = connectivity_dispose;
356 oclass->constructor = connectivity_constructor;
357 oclass->get_property = connectivity_get_property;
358 oclass->set_property = connectivity_set_property;
360 signals[STATE_CHANGE] =
361 g_signal_new ("state-change",
362 G_TYPE_FROM_CLASS (klass),
366 g_cclosure_marshal_generic,
368 1, G_TYPE_BOOLEAN, NULL);
370 g_object_class_install_property (oclass,
372 g_param_spec_boolean ("use-conn",
373 "Use connectivity managers",
374 "Set presence according to connectivity managers",
376 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
378 g_type_class_add_private (oclass, sizeof (EmpathyConnectivityPriv));
383 EmpathyConnectivity *
384 empathy_connectivity_dup_singleton (void)
386 return g_object_new (EMPATHY_TYPE_CONNECTIVITY, NULL);
390 empathy_connectivity_is_online (EmpathyConnectivity *connectivity)
392 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
394 return priv->connected;
398 empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity)
400 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
402 return priv->use_conn;
406 empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
409 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
411 if (use_conn == priv->use_conn)
414 DEBUG ("use_conn GSetting key changed; new value = %s",
415 use_conn ? "true" : "false");
417 priv->use_conn = use_conn;
419 #if defined(HAVE_NM) || defined(HAVE_CONNMAN)
423 connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
424 #elif defined(HAVE_CONNMAN)
425 connectivity_connman_check_state (connectivity);
431 connectivity_change_state (connectivity, TRUE);
434 g_object_notify (G_OBJECT (connectivity), "use-conn");