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"
36 #include "empathy-marshal.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_CONNECTIVITY
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectivity)
46 gulong state_change_signal_id;
55 } EmpathyConnectivityPriv;
67 static guint signals[LAST_SIGNAL];
68 static EmpathyConnectivity *connectivity_singleton = NULL;
70 G_DEFINE_TYPE (EmpathyConnectivity, empathy_connectivity, G_TYPE_OBJECT);
73 connectivity_change_state (EmpathyConnectivity *connectivity,
76 EmpathyConnectivityPriv *priv;
78 priv = GET_PRIV (connectivity);
80 if (priv->connected == new_state)
83 priv->connected = new_state;
85 g_signal_emit (connectivity, signals[STATE_CHANGE], 0,
91 connectivity_nm_state_change_cb (NMClient *client,
92 const GParamSpec *pspec,
93 EmpathyConnectivity *connectivity)
95 EmpathyConnectivityPriv *priv;
96 gboolean new_nm_connected;
99 priv = GET_PRIV (connectivity);
104 state = nm_client_get_state (priv->nm_client);
105 new_nm_connected = !(state == NM_STATE_CONNECTING
106 || state == NM_STATE_DISCONNECTED);
108 DEBUG ("New NetworkManager network state %d", state);
110 connectivity_change_state (connectivity, new_nm_connected);
116 connectivity_connman_state_changed_cb (DBusGProxy *proxy,
117 const gchar *new_state,
118 EmpathyConnectivity *connectivity)
120 EmpathyConnectivityPriv *priv;
121 gboolean new_connected;
123 priv = GET_PRIV (connectivity);
128 new_connected = !tp_strdiff (new_state, "online");
130 DEBUG ("New ConnMan network state %s", new_state);
132 connectivity_change_state (connectivity, new_connected);
136 connectivity_connman_check_state_cb (DBusGProxy *proxy,
137 DBusGProxyCall *call_id,
140 EmpathyConnectivity *connectivity = (EmpathyConnectivity *) user_data;
141 GError *error = NULL;
144 if (dbus_g_proxy_end_call (proxy, call_id, &error,
145 G_TYPE_STRING, &state, G_TYPE_INVALID))
147 connectivity_connman_state_changed_cb (proxy, state,
153 DEBUG ("Failed to call GetState: %s", error->message);
154 connectivity_connman_state_changed_cb (proxy, "offline",
160 connectivity_connman_check_state (EmpathyConnectivity *connectivity)
162 EmpathyConnectivityPriv *priv;
164 priv = GET_PRIV (connectivity);
166 dbus_g_proxy_begin_call (priv->proxy, "GetState",
167 connectivity_connman_check_state_cb, connectivity, NULL,
173 empathy_connectivity_init (EmpathyConnectivity *connectivity)
175 EmpathyConnectivityPriv *priv;
177 DBusGConnection *connection;
178 GError *error = NULL;
181 priv = G_TYPE_INSTANCE_GET_PRIVATE (connectivity,
182 EMPATHY_TYPE_CONNECTIVITY, EmpathyConnectivityPriv);
184 connectivity->priv = priv;
186 priv->use_conn = TRUE;
189 priv->nm_client = nm_client_new ();
190 if (priv->nm_client != NULL)
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);
196 connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
200 DEBUG ("Failed to get NetworkManager proxy");
205 connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
206 if (connection != NULL)
208 priv->proxy = dbus_g_proxy_new_for_name (connection,
209 "org.moblin.connman", "/",
210 "org.moblin.connman.Manager");
212 dbus_g_object_register_marshaller (
213 _empathy_marshal_VOID__STRING,
214 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
216 dbus_g_proxy_add_signal (priv->proxy, "StateChanged",
217 G_TYPE_STRING, G_TYPE_INVALID);
219 dbus_g_proxy_connect_signal (priv->proxy, "StateChanged",
220 G_CALLBACK (connectivity_connman_state_changed_cb),
223 connectivity_connman_check_state (connectivity);
227 DEBUG ("Failed to get system bus connection: %s", error->message);
228 g_error_free (error);
232 #if !defined(HAVE_NM) || !defined(HAVE_CONNMAN)
233 priv->connected = TRUE;
238 connectivity_finalize (GObject *object)
241 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
242 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
244 if (priv->nm_client != NULL)
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;
255 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
256 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
258 if (priv->proxy != NULL)
260 dbus_g_proxy_disconnect_signal (priv->proxy, "StateChanged",
261 G_CALLBACK (connectivity_connman_state_changed_cb), connectivity);
263 g_object_unref (priv->proxy);
268 G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (object);
272 connectivity_dispose (GObject *object)
274 G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (object);
278 connectivity_constructor (GType type,
279 guint n_construct_params,
280 GObjectConstructParam *construct_params)
284 if (!connectivity_singleton)
286 retval = G_OBJECT_CLASS (empathy_connectivity_parent_class)->constructor
287 (type, n_construct_params, construct_params);
289 connectivity_singleton = EMPATHY_CONNECTIVITY (retval);
290 g_object_add_weak_pointer (retval, (gpointer) &connectivity_singleton);
294 retval = g_object_ref (connectivity_singleton);
301 connectivity_get_property (GObject *object,
306 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
311 g_value_set_boolean (value, empathy_connectivity_get_use_conn (
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
321 connectivity_set_property (GObject *object,
326 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
331 empathy_connectivity_set_use_conn (connectivity,
332 g_value_get_boolean (value));
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
341 empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
343 GObjectClass *oclass = G_OBJECT_CLASS (klass);
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;
351 signals[STATE_CHANGE] =
352 g_signal_new ("state-change",
353 G_TYPE_FROM_CLASS (klass),
357 _empathy_marshal_VOID__BOOLEAN,
359 1, G_TYPE_BOOLEAN, NULL);
361 g_object_class_install_property (oclass,
363 g_param_spec_boolean ("use-conn",
364 "Use connectivity managers",
365 "Set presence according to connectivity managers",
367 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
369 g_type_class_add_private (oclass, sizeof (EmpathyConnectivityPriv));
374 EmpathyConnectivity *
375 empathy_connectivity_dup_singleton (void)
377 return g_object_new (EMPATHY_TYPE_CONNECTIVITY, NULL);
381 empathy_connectivity_is_online (EmpathyConnectivity *connectivity)
383 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
387 return priv->connected;
396 empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity)
398 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
400 return priv->use_conn;
404 empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
407 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
409 if (use_conn == priv->use_conn)
412 DEBUG ("use_conn gconf key changed; new value = %s",
413 use_conn ? "true" : "false");
415 priv->use_conn = use_conn;
417 #if defined(HAVE_NM) || defined(HAVE_CONNMAN)
421 connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
422 #elif defined(HAVE_CONNMAN)
423 connectivity_connman_check_state (connectivity);
429 connectivity_change_state (connectivity, TRUE);
432 g_object_notify (G_OBJECT (connectivity), "use-conn");