empathy_contact_from_tpl_contact: check the accounts match
[empathy-mirror.git] / libempathy / empathy-connectivity.c
blob3c9805c542c05b380be8e809a5a0b3bc8d08ea2c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
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>
22 #include "config.h"
23 #include "empathy-connectivity.h"
25 #ifdef HAVE_NM
26 #include <nm-client.h>
27 #endif
29 #ifdef HAVE_CONNMAN
30 #include <dbus/dbus-glib.h>
31 #endif
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)
43 typedef struct {
44 #ifdef HAVE_NM
45 NMClient *nm_client;
46 gulong state_change_signal_id;
47 #endif
49 #ifdef HAVE_CONNMAN
50 DBusGProxy *proxy;
51 #endif
53 gboolean connected;
54 gboolean use_conn;
55 } EmpathyConnectivityPriv;
57 enum {
58 STATE_CHANGE,
59 LAST_SIGNAL
62 enum {
63 PROP_0,
64 PROP_USE_CONN,
67 static guint signals[LAST_SIGNAL];
68 static EmpathyConnectivity *connectivity_singleton = NULL;
70 G_DEFINE_TYPE (EmpathyConnectivity, empathy_connectivity, G_TYPE_OBJECT);
72 static void
73 connectivity_change_state (EmpathyConnectivity *connectivity,
74 gboolean new_state)
76 EmpathyConnectivityPriv *priv;
78 priv = GET_PRIV (connectivity);
80 if (priv->connected == new_state)
81 return;
83 priv->connected = new_state;
85 g_signal_emit (connectivity, signals[STATE_CHANGE], 0,
86 priv->connected);
89 #ifdef HAVE_NM
91 #if !defined(NM_CHECK_VERSION)
92 #define NM_CHECK_VERSION(x,y,z) 0
93 #endif
95 static void
96 connectivity_nm_state_change_cb (NMClient *client,
97 const GParamSpec *pspec,
98 EmpathyConnectivity *connectivity)
100 EmpathyConnectivityPriv *priv;
101 gboolean new_nm_connected;
102 NMState state;
104 priv = GET_PRIV (connectivity);
106 if (!priv->use_conn)
107 return;
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);
117 DEBUG ("New NetworkManager network state %d (connected: %s)", state,
118 new_nm_connected ? "true" : "false");
120 connectivity_change_state (connectivity, new_nm_connected);
122 #endif
124 #ifdef HAVE_CONNMAN
125 static void
126 connectivity_connman_state_changed_cb (DBusGProxy *proxy,
127 const gchar *new_state,
128 EmpathyConnectivity *connectivity)
130 EmpathyConnectivityPriv *priv;
131 gboolean new_connected;
133 priv = GET_PRIV (connectivity);
135 if (!priv->use_conn)
136 return;
138 new_connected = !tp_strdiff (new_state, "online");
140 DEBUG ("New ConnMan network state %s", new_state);
142 connectivity_change_state (connectivity, new_connected);
145 static void
146 connectivity_connman_check_state_cb (DBusGProxy *proxy,
147 DBusGProxyCall *call_id,
148 gpointer user_data)
150 EmpathyConnectivity *connectivity = (EmpathyConnectivity *) user_data;
151 GError *error = NULL;
152 gchar *state;
154 if (dbus_g_proxy_end_call (proxy, call_id, &error,
155 G_TYPE_STRING, &state, G_TYPE_INVALID))
157 connectivity_connman_state_changed_cb (proxy, state,
158 connectivity);
159 g_free (state);
161 else
163 DEBUG ("Failed to call GetState: %s", error->message);
164 connectivity_connman_state_changed_cb (proxy, "offline",
165 connectivity);
169 static void
170 connectivity_connman_check_state (EmpathyConnectivity *connectivity)
172 EmpathyConnectivityPriv *priv;
174 priv = GET_PRIV (connectivity);
176 dbus_g_proxy_begin_call (priv->proxy, "GetState",
177 connectivity_connman_check_state_cb, connectivity, NULL,
178 G_TYPE_INVALID);
180 #endif
182 static void
183 empathy_connectivity_init (EmpathyConnectivity *connectivity)
185 EmpathyConnectivityPriv *priv;
186 #ifdef HAVE_CONNMAN
187 DBusGConnection *connection;
188 GError *error = NULL;
189 #endif
191 priv = G_TYPE_INSTANCE_GET_PRIVATE (connectivity,
192 EMPATHY_TYPE_CONNECTIVITY, EmpathyConnectivityPriv);
194 connectivity->priv = priv;
196 priv->use_conn = TRUE;
198 #ifdef HAVE_NM
199 priv->nm_client = nm_client_new ();
200 if (priv->nm_client != NULL)
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);
206 connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
208 else
210 DEBUG ("Failed to get NetworkManager proxy");
212 #endif
214 #ifdef HAVE_CONNMAN
215 connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
216 if (connection != NULL)
218 priv->proxy = dbus_g_proxy_new_for_name (connection,
219 "net.connman", "/",
220 "net.connman.Manager");
222 dbus_g_object_register_marshaller (
223 _empathy_marshal_VOID__STRING,
224 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
226 dbus_g_proxy_add_signal (priv->proxy, "StateChanged",
227 G_TYPE_STRING, G_TYPE_INVALID);
229 dbus_g_proxy_connect_signal (priv->proxy, "StateChanged",
230 G_CALLBACK (connectivity_connman_state_changed_cb),
231 connectivity, NULL);
233 connectivity_connman_check_state (connectivity);
235 else
237 DEBUG ("Failed to get system bus connection: %s", error->message);
238 g_error_free (error);
240 #endif
242 #if !defined(HAVE_NM) && !defined(HAVE_CONNMAN)
243 priv->connected = TRUE;
244 #endif
247 static void
248 connectivity_finalize (GObject *object)
250 #ifdef HAVE_NM
251 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
252 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
254 if (priv->nm_client != NULL)
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;
262 #endif
264 #ifdef HAVE_CONNMAN
265 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
266 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
268 if (priv->proxy != NULL)
270 dbus_g_proxy_disconnect_signal (priv->proxy, "StateChanged",
271 G_CALLBACK (connectivity_connman_state_changed_cb), connectivity);
273 g_object_unref (priv->proxy);
274 priv->proxy = NULL;
276 #endif
278 G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (object);
281 static void
282 connectivity_dispose (GObject *object)
284 G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (object);
287 static GObject *
288 connectivity_constructor (GType type,
289 guint n_construct_params,
290 GObjectConstructParam *construct_params)
292 GObject *retval;
294 if (!connectivity_singleton)
296 retval = G_OBJECT_CLASS (empathy_connectivity_parent_class)->constructor
297 (type, n_construct_params, construct_params);
299 connectivity_singleton = EMPATHY_CONNECTIVITY (retval);
300 g_object_add_weak_pointer (retval, (gpointer) &connectivity_singleton);
302 else
304 retval = g_object_ref (connectivity_singleton);
307 return retval;
310 static void
311 connectivity_get_property (GObject *object,
312 guint param_id,
313 GValue *value,
314 GParamSpec *pspec)
316 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
318 switch (param_id)
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;
330 static void
331 connectivity_set_property (GObject *object,
332 guint param_id,
333 const GValue *value,
334 GParamSpec *pspec)
336 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
338 switch (param_id)
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;
350 static void
351 empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
353 GObjectClass *oclass = G_OBJECT_CLASS (klass);
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;
361 signals[STATE_CHANGE] =
362 g_signal_new ("state-change",
363 G_TYPE_FROM_CLASS (klass),
364 G_SIGNAL_RUN_LAST,
366 NULL, NULL,
367 _empathy_marshal_VOID__BOOLEAN,
368 G_TYPE_NONE,
369 1, G_TYPE_BOOLEAN, NULL);
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));
379 g_type_class_add_private (oclass, sizeof (EmpathyConnectivityPriv));
382 /* public methods */
384 EmpathyConnectivity *
385 empathy_connectivity_dup_singleton (void)
387 return g_object_new (EMPATHY_TYPE_CONNECTIVITY, NULL);
390 gboolean
391 empathy_connectivity_is_online (EmpathyConnectivity *connectivity)
393 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
395 return priv->connected;
398 gboolean
399 empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity)
401 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
403 return priv->use_conn;
406 void
407 empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
408 gboolean use_conn)
410 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
412 if (use_conn == priv->use_conn)
413 return;
415 DEBUG ("use_conn GSetting key changed; new value = %s",
416 use_conn ? "true" : "false");
418 priv->use_conn = use_conn;
420 #if defined(HAVE_NM) || defined(HAVE_CONNMAN)
421 if (use_conn)
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
429 else
430 #endif
432 connectivity_change_state (connectivity, TRUE);
435 g_object_notify (G_OBJECT (connectivity), "use-conn");