~ubuntu-branches/ubuntu/precise/gnome-main-menu/precise

« back to all changes in this revision

Viewing changes to .pc/0004-Drop-the-dbus-glib-1-requirement.patch/main-menu/src/network-status-agent.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode, Julian Andres Klode, Josselin Mouette
  • Date: 2011-03-14 19:09:51 UTC
  • mfrom: (1.1.7 upstream) (2.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20110314190951-kfrk4hocrw1pk0ow
Tags: 0.9.16-1
[ Julian Andres Klode ]
* New upstream release:
  - All patches merged upstream (except for default-applications.diff)
  - Correctly links against libxml2 (Closes: #611550)
* default-applications.diff: remove the firefox change (upstream now)

[ Josselin Mouette ]
* Drop type-handling usage. Closes: #587880.
* Bump standards version accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of the Main Menu.
3
 
 *
4
 
 * Copyright (c) 2006 Novell, Inc.
5
 
 *
6
 
 * The Main Menu is free software; you can redistribute it and/or modify it
7
 
 * under the terms of the GNU General Public License as published by the Free
8
 
 * Software Foundation; either version 2 of the License, or (at your option)
9
 
 * any later version.
10
 
 *
11
 
 * The Main Menu is distributed in the hope that it will be useful, but WITHOUT
12
 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14
 
 * more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License along with
17
 
 * the Main Menu; if not, write to the Free Software Foundation, Inc., 51
18
 
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include "network-status-agent.h"
22
 
 
23
 
#include <string.h>
24
 
#include <nm-client.h>
25
 
#include <NetworkManager.h>
26
 
#include <nm-device-wifi.h>
27
 
#include <nm-device-ethernet.h>
28
 
#include <nm-gsm-device.h>
29
 
#include <nm-cdma-device.h>
30
 
#include <nm-setting-ip4-config.h>
31
 
#include <nm-utils.h>
32
 
#include <arpa/inet.h>
33
 
#include <dbus/dbus.h>
34
 
#include <dbus/dbus-glib.h>
35
 
#include <dbus/dbus-glib-lowlevel.h>
36
 
#include <iwlib.h>
37
 
#include <glibtop/netlist.h>
38
 
#include <glibtop/netload.h>
39
 
#include <libslab/slab.h>
40
 
 
41
 
typedef struct
42
 
{
43
 
        NMClient * nm_client;
44
 
        guint state_curr;
45
 
} NetworkStatusAgentPrivate;
46
 
 
47
 
#define NETWORK_STATUS_AGENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NETWORK_STATUS_AGENT_TYPE, NetworkStatusAgentPrivate))
48
 
 
49
 
static void network_status_agent_dispose (GObject *);
50
 
 
51
 
static void init_nm_connection (NetworkStatusAgent *);
52
 
 
53
 
static NetworkStatusInfo *nm_get_first_active_device_info (NetworkStatusAgent *);
54
 
static NetworkStatusInfo *nm_get_device_info (NetworkStatusAgent *, NMDevice *);
55
 
 
56
 
static void nm_state_change_cb (NMDevice *device, GParamSpec *pspec, gpointer user_data);
57
 
 
58
 
static NetworkStatusInfo *gtop_get_first_active_device_info (void);
59
 
 
60
 
enum
61
 
{
62
 
        STATUS_CHANGED,
63
 
        LAST_SIGNAL
64
 
};
65
 
 
66
 
static guint network_status_agent_signals[LAST_SIGNAL] = { 0 };
67
 
 
68
 
G_DEFINE_TYPE (NetworkStatusAgent, network_status_agent, G_TYPE_OBJECT)
69
 
 
70
 
static void network_status_agent_class_init (NetworkStatusAgentClass * this_class)
71
 
{
72
 
        GObjectClass *g_obj_class = (GObjectClass *) this_class;
73
 
 
74
 
        g_obj_class->dispose = network_status_agent_dispose;
75
 
 
76
 
        network_status_agent_signals[STATUS_CHANGED] = g_signal_new ("status-changed",
77
 
                G_TYPE_FROM_CLASS (this_class),
78
 
                G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
79
 
                G_STRUCT_OFFSET (NetworkStatusAgentClass, status_changed),
80
 
                NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
81
 
 
82
 
        g_type_class_add_private (this_class, sizeof (NetworkStatusAgentPrivate));
83
 
}
84
 
 
85
 
static void
86
 
network_status_agent_init (NetworkStatusAgent * agent)
87
 
{
88
 
        NetworkStatusAgentPrivate *priv = NETWORK_STATUS_AGENT_GET_PRIVATE (agent);
89
 
 
90
 
        agent->nm_present = FALSE;
91
 
 
92
 
        priv->nm_client = NULL;
93
 
        priv->state_curr = 0;
94
 
}
95
 
 
96
 
NetworkStatusAgent *
97
 
network_status_agent_new ()
98
 
{
99
 
        return g_object_new (NETWORK_STATUS_AGENT_TYPE, NULL);
100
 
}
101
 
 
102
 
NetworkStatusInfo *
103
 
network_status_agent_get_first_active_device_info (NetworkStatusAgent * agent)
104
 
{
105
 
        NetworkStatusAgentPrivate *priv = NETWORK_STATUS_AGENT_GET_PRIVATE (agent);
106
 
        NetworkStatusInfo *info = NULL;
107
 
 
108
 
        if (!priv->nm_client)
109
 
                init_nm_connection (agent);
110
 
 
111
 
        if (agent->nm_present)
112
 
                info = nm_get_first_active_device_info (agent);
113
 
        else
114
 
                info = gtop_get_first_active_device_info ();
115
 
 
116
 
        return info;
117
 
}
118
 
 
119
 
static void
120
 
network_status_agent_dispose (GObject * obj)
121
 
{
122
 
        NetworkStatusAgentPrivate *priv = NETWORK_STATUS_AGENT_GET_PRIVATE (obj);
123
 
        if (priv->nm_client) {
124
 
                g_object_unref (priv->nm_client);
125
 
                priv->nm_client = NULL;
126
 
        }
127
 
}
128
 
 
129
 
static void
130
 
init_nm_connection (NetworkStatusAgent * agent)
131
 
{
132
 
        NetworkStatusAgentPrivate *priv = NETWORK_STATUS_AGENT_GET_PRIVATE (agent);
133
 
 
134
 
        priv->nm_client = nm_client_new();
135
 
 
136
 
        if (!(priv->nm_client && nm_client_get_manager_running (priv->nm_client)))
137
 
        {
138
 
                g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "nm_client_new failed");
139
 
 
140
 
                agent->nm_present = FALSE;
141
 
 
142
 
                return;
143
 
        }
144
 
 
145
 
        agent->nm_present = TRUE;
146
 
}
147
 
 
148
 
static NetworkStatusInfo *
149
 
nm_get_first_active_device_info (NetworkStatusAgent * agent)
150
 
{
151
 
        NetworkStatusAgentPrivate *priv = NETWORK_STATUS_AGENT_GET_PRIVATE (agent);
152
 
 
153
 
        NetworkStatusInfo *info = NULL;
154
 
 
155
 
        const GPtrArray *devices;
156
 
        gint i;
157
 
 
158
 
        if (!priv->nm_client)
159
 
                return NULL;
160
 
 
161
 
        devices = nm_client_get_devices (priv->nm_client);
162
 
 
163
 
        for (i = 0; devices && i < devices->len; i++)
164
 
        {
165
 
                NMDevice *nm_device;
166
 
 
167
 
                nm_device = NM_DEVICE (g_ptr_array_index (devices, i));
168
 
                info = nm_get_device_info (agent, nm_device);
169
 
 
170
 
                if (info)
171
 
                {
172
 
                        g_signal_connect (nm_device, "notify::state", G_CALLBACK (nm_state_change_cb), agent);
173
 
                        if (info->active)
174
 
                                break;
175
 
 
176
 
                        g_object_unref (info);
177
 
 
178
 
                        info = NULL;
179
 
                }
180
 
        }
181
 
 
182
 
        return info;
183
 
}
184
 
 
185
 
static gchar *
186
 
ip4_address_as_string (guint32 ip)
187
 
{
188
 
        struct in_addr tmp_addr;
189
 
        gchar *ip_string;
190
 
 
191
 
        tmp_addr.s_addr = ip;
192
 
        ip_string = inet_ntoa (tmp_addr);
193
 
 
194
 
        return g_strdup (ip_string);
195
 
}
196
 
 
197
 
static NetworkStatusInfo *
198
 
nm_get_device_info (NetworkStatusAgent * agent, NMDevice * device)
199
 
{
200
 
        NetworkStatusInfo *info = g_object_new (NETWORK_STATUS_INFO_TYPE, NULL);
201
 
        const GArray *array;
202
 
        const GSList *addresses;
203
 
        NMIP4Address *def_addr;
204
 
        guint32 address, netmask, hostmask, network, bcast;
205
 
 
206
 
        info->iface = g_strdup (nm_device_get_iface (device));
207
 
        info->driver = g_strdup (nm_device_get_driver (device));
208
 
        info->active = (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) ? TRUE : FALSE;
209
 
        if (! info->active)
210
 
                return info;
211
 
        NMIP4Config * cfg = nm_device_get_ip4_config (device);
212
 
        //According to the documentation this should not be NULL if the device is active but on resume from suspend it is (BNC#463712), so check for now
213
 
        if(cfg)
214
 
        {
215
 
                addresses = nm_ip4_config_get_addresses (cfg);
216
 
                if (addresses) {
217
 
                        def_addr = addresses->data;
218
 
 
219
 
                        address = nm_ip4_address_get_address (def_addr);
220
 
                        netmask = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (def_addr));
221
 
                        network = ntohl (address) & ntohl (netmask);
222
 
                        hostmask = ~ntohl (netmask);
223
 
                        bcast = htonl (network | hostmask);
224
 
 
225
 
                        info->ip4_addr = ip4_address_as_string (address);
226
 
                        info->subnet_mask = ip4_address_as_string (netmask);
227
 
                        info->route = ip4_address_as_string (nm_ip4_address_get_gateway (def_addr));
228
 
                                        info->broadcast = ip4_address_as_string (bcast);
229
 
                }
230
 
 
231
 
                info->primary_dns = NULL;
232
 
                info->secondary_dns = NULL;
233
 
                array = nm_ip4_config_get_nameservers (cfg);
234
 
                if (array)
235
 
                {
236
 
                        if (array->len > 0)
237
 
                                info->primary_dns = ip4_address_as_string (g_array_index (array, guint32, 0));
238
 
                        if (array->len > 1)
239
 
                                info->secondary_dns = ip4_address_as_string (g_array_index (array, guint32, 1));
240
 
                }
241
 
        }
242
 
 
243
 
        if (NM_IS_DEVICE_WIFI(device))
244
 
        {
245
 
                NMAccessPoint * activeap = NULL;
246
 
                const GByteArray * ssid;
247
 
 
248
 
                info->type = DEVICE_TYPE_802_11_WIRELESS;
249
 
                info->speed_mbs = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI(device));
250
 
                info->hw_addr = g_strdup (nm_device_wifi_get_hw_address (NM_DEVICE_WIFI(device)));
251
 
 
252
 
                activeap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI(device));
253
 
                if (activeap)
254
 
                {
255
 
                        ssid = nm_access_point_get_ssid (NM_ACCESS_POINT (activeap));
256
 
                        if (ssid)
257
 
                                info->essid = g_strdup (nm_utils_escape_ssid (ssid->data, ssid->len));
258
 
                }
259
 
 
260
 
                if (! info->essid)
261
 
                        info->essid = g_strdup ("(none)");
262
 
        }
263
 
        else if (NM_IS_DEVICE_ETHERNET (device))
264
 
        {
265
 
                info->type = DEVICE_TYPE_802_3_ETHERNET;
266
 
                info->speed_mbs = nm_device_ethernet_get_speed (NM_DEVICE_ETHERNET(device));
267
 
                info->hw_addr = g_strdup (nm_device_ethernet_get_hw_address (NM_DEVICE_ETHERNET(device)));
268
 
        }
269
 
        else if (NM_IS_GSM_DEVICE (device))
270
 
        {
271
 
                info->type = DEVICE_TYPE_GSM;
272
 
                info->speed_mbs = 0;
273
 
                info->hw_addr = NULL;
274
 
        }
275
 
        else if (NM_IS_CDMA_DEVICE (device))
276
 
        {
277
 
                info->type = DEVICE_TYPE_CDMA;
278
 
                info->speed_mbs = 0;
279
 
                info->hw_addr = NULL;
280
 
        }
281
 
 
282
 
        return info;
283
 
}
284
 
 
285
 
static void
286
 
nm_state_change_cb (NMDevice *device, GParamSpec *pspec, gpointer user_data)
287
 
{
288
 
        NetworkStatusAgent        *this = NETWORK_STATUS_AGENT (user_data);
289
 
        NetworkStatusAgentPrivate *priv = NETWORK_STATUS_AGENT_GET_PRIVATE (this);
290
 
        NMDeviceState              state;
291
 
 
292
 
        state = nm_device_get_state (device);
293
 
 
294
 
        if (priv->state_curr == state)
295
 
                return;
296
 
 
297
 
        priv->state_curr = state;
298
 
 
299
 
        g_signal_emit (this, network_status_agent_signals [STATUS_CHANGED], 0);
300
 
}
301
 
 
302
 
#define CHECK_FLAG(flags, offset) (((flags) & (1 << (offset))) ? TRUE : FALSE)
303
 
 
304
 
static NetworkStatusInfo *
305
 
gtop_get_first_active_device_info ()
306
 
{
307
 
        NetworkStatusInfo *info = NULL;
308
 
 
309
 
        glibtop_netlist net_list;
310
 
        gchar **networks;
311
 
 
312
 
        glibtop_netload net_load;
313
 
 
314
 
        gint sock_fd;
315
 
        wireless_config wl_cfg;
316
 
 
317
 
        gint ret;
318
 
        gint i;
319
 
 
320
 
        networks = glibtop_get_netlist (&net_list);
321
 
 
322
 
        for (i = 0; i < net_list.number; ++i)
323
 
        {
324
 
                glibtop_get_netload (&net_load, networks[i]);
325
 
 
326
 
                if (CHECK_FLAG (net_load.if_flags, GLIBTOP_IF_FLAGS_RUNNING)
327
 
                        && !CHECK_FLAG (net_load.if_flags, GLIBTOP_IF_FLAGS_LOOPBACK)
328
 
                        && net_load.address)
329
 
                {
330
 
                        sock_fd = iw_sockets_open ();
331
 
 
332
 
                        if (sock_fd <= 0)
333
 
                        {
334
 
                                g_warning ("error opening socket\n");
335
 
 
336
 
                                info = NULL;
337
 
                                break;
338
 
                        }
339
 
 
340
 
                        info = g_object_new (NETWORK_STATUS_INFO_TYPE, NULL);
341
 
 
342
 
                        ret = iw_get_basic_config (sock_fd, networks[i], &wl_cfg);
343
 
 
344
 
                        if (ret >= 0)
345
 
                        {
346
 
                                info->type = DEVICE_TYPE_802_11_WIRELESS;
347
 
                                info->essid = g_strdup (wl_cfg.essid);
348
 
                                info->iface = g_strdup (networks[i]);
349
 
 
350
 
                                break;
351
 
                        }
352
 
                        else
353
 
                        {
354
 
                                info->type = DEVICE_TYPE_802_3_ETHERNET;
355
 
                                info->essid = NULL;
356
 
                                info->iface = g_strdup (networks[i]);
357
 
 
358
 
                                break;
359
 
                        }
360
 
                }
361
 
        }
362
 
 
363
 
        g_strfreev (networks);
364
 
        return info;
365
 
}
366