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