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