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>
29 #include "empathy-utils.h"
30 #include "empathy-marshal.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_CONNECTIVITY
33 #include "empathy-debug.h"
35 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectivity)
45 } EmpathyConnectivityPriv;
57 static guint signals[LAST_SIGNAL];
58 static EmpathyConnectivity *connectivity_singleton = NULL;
60 G_DEFINE_TYPE (EmpathyConnectivity, empathy_connectivity, G_TYPE_OBJECT);
64 connectivity_nm_state_change_cb (NMClient *client,
65 const GParamSpec *pspec,
66 EmpathyConnectivity *connectivity)
68 EmpathyConnectivityPriv *priv;
69 gboolean old_nm_connected;
70 gboolean new_nm_connected;
73 priv = GET_PRIV (connectivity);
78 state = nm_client_get_state (priv->nm_client);
79 old_nm_connected = priv->connected;
80 new_nm_connected = !(state == NM_STATE_CONNECTING
81 || state == NM_STATE_DISCONNECTED);
83 DEBUG ("New NetworkManager network state %d", state);
85 priv->connected = new_nm_connected;
87 g_signal_emit (connectivity, signals[STATE_CHANGE], 0,
88 old_nm_connected, new_nm_connected);
93 empathy_connectivity_init (EmpathyConnectivity *connectivity)
95 EmpathyConnectivityPriv *priv;
97 priv = G_TYPE_INSTANCE_GET_PRIVATE (connectivity,
98 EMPATHY_TYPE_CONNECTIVITY, EmpathyConnectivityPriv);
100 connectivity->priv = priv;
101 priv->dispose_run = FALSE;
103 priv->use_conn = TRUE;
106 priv->nm_client = nm_client_new ();
107 if (priv->nm_client != NULL)
109 g_signal_connect (priv->nm_client, "notify::" NM_CLIENT_STATE,
110 G_CALLBACK (connectivity_nm_state_change_cb), connectivity);
112 connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
116 DEBUG ("Failed to get NetworkManager proxy");
119 priv->connected = TRUE;
124 connectivity_finalize (GObject *object)
127 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
128 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
130 if (priv->nm_client != NULL)
132 g_signal_handlers_disconnect_by_func (priv->nm_client,
133 connectivity_nm_state_change_cb, connectivity);
134 g_object_unref (priv->nm_client);
135 priv->nm_client = NULL;
139 G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (object);
143 connectivity_dispose (GObject *object)
145 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
146 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
148 if (priv->dispose_run)
151 priv->dispose_run = TRUE;
153 G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (object);
157 connectivity_constructor (GType type,
158 guint n_construct_params,
159 GObjectConstructParam *construct_params)
163 if (!connectivity_singleton)
165 retval = G_OBJECT_CLASS (empathy_connectivity_parent_class)->constructor
166 (type, n_construct_params, construct_params);
168 connectivity_singleton = EMPATHY_CONNECTIVITY (retval);
169 g_object_add_weak_pointer (retval, (gpointer) &connectivity_singleton);
173 retval = g_object_ref (connectivity_singleton);
180 connectivity_get_property (GObject *object,
185 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
190 g_value_set_boolean (value, empathy_connectivity_get_use_conn (
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
200 connectivity_set_property (GObject *object,
205 EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
210 empathy_connectivity_set_use_conn (connectivity,
211 g_value_get_boolean (value));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
220 empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
222 GObjectClass *oclass = G_OBJECT_CLASS (klass);
224 oclass->finalize = connectivity_finalize;
225 oclass->dispose = connectivity_dispose;
226 oclass->constructor = connectivity_constructor;
227 oclass->get_property = connectivity_get_property;
228 oclass->set_property = connectivity_set_property;
230 signals[STATE_CHANGE] =
231 g_signal_new ("state-change",
232 G_TYPE_FROM_CLASS (klass),
236 _empathy_marshal_VOID__BOOLEAN_BOOLEAN,
238 2, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, NULL);
240 g_object_class_install_property (oclass,
242 g_param_spec_boolean ("use-conn",
243 "Use connectivity managers",
244 "Set presence according to connectivity managers",
246 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
248 g_type_class_add_private (oclass, sizeof (EmpathyConnectivityPriv));
253 EmpathyConnectivity *
254 empathy_connectivity_dup_singleton (void)
256 return g_object_new (EMPATHY_TYPE_CONNECTIVITY, NULL);
260 empathy_connectivity_is_online (EmpathyConnectivity *connectivity)
262 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
266 return priv->connected;
275 empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity)
277 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
279 return priv->use_conn;
283 empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
286 EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
288 if (use_conn == priv->use_conn)
291 DEBUG ("use_conn gconf key changed; new value = %s",
292 use_conn ? "true" : "false");
294 priv->use_conn = use_conn;
299 connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
307 g_signal_emit (connectivity, signals[STATE_CHANGE], 0,
311 g_object_notify (G_OBJECT (connectivity), "use-conn");