~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/network/net-device-wired.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2011-2012 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <glib-object.h>
 
25
#include <glib/gi18n.h>
 
26
 
 
27
#include <nm-client.h>
 
28
#include <nm-device.h>
 
29
#include <nm-device-ethernet.h>
 
30
#include <nm-remote-connection.h>
 
31
 
 
32
#include "panel-common.h"
 
33
 
 
34
#include "net-device-wired.h"
 
35
 
 
36
#define NET_DEVICE_WIRED_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_DEVICE_WIRED, NetDeviceWiredPrivate))
 
37
 
 
38
struct _NetDeviceWiredPrivate
 
39
{
 
40
        GtkBuilder              *builder;
 
41
        gboolean                 updating_device;
 
42
};
 
43
 
 
44
G_DEFINE_TYPE (NetDeviceWired, net_device_wired, NET_TYPE_DEVICE)
 
45
 
 
46
static GtkWidget *
 
47
device_wired_proxy_add_to_notebook (NetObject *object,
 
48
                                    GtkNotebook *notebook,
 
49
                                    GtkSizeGroup *heading_size_group)
 
50
{
 
51
        GtkWidget *widget;
 
52
        GtkWindow *window;
 
53
        NetDeviceWired *device_wired = NET_DEVICE_WIRED (object);
 
54
 
 
55
        /* add widgets to size group */
 
56
        widget = GTK_WIDGET (gtk_builder_get_object (device_wired->priv->builder,
 
57
                                                     "heading_ipv4"));
 
58
        gtk_size_group_add_widget (heading_size_group, widget);
 
59
 
 
60
        /* reparent */
 
61
        window = GTK_WINDOW (gtk_builder_get_object (device_wired->priv->builder,
 
62
                                                     "window_tmp"));
 
63
        widget = GTK_WIDGET (gtk_builder_get_object (device_wired->priv->builder,
 
64
                                                     "vbox6"));
 
65
        g_object_ref (widget);
 
66
        gtk_container_remove (GTK_CONTAINER (window), widget);
 
67
        gtk_notebook_append_page (notebook, widget, NULL);
 
68
        g_object_unref (widget);
 
69
        return widget;
 
70
}
 
71
 
 
72
static void
 
73
update_off_switch_from_device_state (GtkSwitch *sw,
 
74
                                     NMDeviceState state,
 
75
                                     NetDeviceWired *device_wired)
 
76
{
 
77
        device_wired->priv->updating_device = TRUE;
 
78
        switch (state) {
 
79
                case NM_DEVICE_STATE_UNMANAGED:
 
80
                case NM_DEVICE_STATE_UNAVAILABLE:
 
81
                case NM_DEVICE_STATE_DISCONNECTED:
 
82
                case NM_DEVICE_STATE_DEACTIVATING:
 
83
                case NM_DEVICE_STATE_FAILED:
 
84
                        gtk_switch_set_active (sw, FALSE);
 
85
                        break;
 
86
                default:
 
87
                        gtk_switch_set_active (sw, TRUE);
 
88
                        break;
 
89
        }
 
90
        device_wired->priv->updating_device = FALSE;
 
91
}
 
92
 
 
93
static void
 
94
nm_device_wired_refresh_ui (NetDeviceWired *device_wired)
 
95
{
 
96
        const char *str;
 
97
        GString *status;
 
98
        GtkWidget *widget;
 
99
        guint speed = 0;
 
100
        NMDevice *nm_device;
 
101
        NMDeviceState state;
 
102
        NetDeviceWiredPrivate *priv = device_wired->priv;
 
103
 
 
104
        /* set device kind */
 
105
        nm_device = net_device_get_nm_device (NET_DEVICE (device_wired));
 
106
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_device"));
 
107
        gtk_label_set_label (GTK_LABEL (widget),
 
108
                             panel_device_to_localized_string (nm_device));
 
109
 
 
110
        /* set up the device on/off switch */
 
111
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "device_off_switch"));
 
112
        state = nm_device_get_state (nm_device);
 
113
        gtk_widget_set_visible (widget,
 
114
                                state != NM_DEVICE_STATE_UNAVAILABLE
 
115
                                && state != NM_DEVICE_STATE_UNMANAGED);
 
116
        update_off_switch_from_device_state (GTK_SWITCH (widget), state, device_wired);
 
117
 
 
118
        /* set device state, with status and optionally speed */
 
119
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_status"));
 
120
        status = g_string_new (panel_device_state_to_localized_string (nm_device));
 
121
        if (state != NM_DEVICE_STATE_UNAVAILABLE)
 
122
                speed = nm_device_ethernet_get_speed (NM_DEVICE_ETHERNET (nm_device));
 
123
        if (speed  > 0) {
 
124
                g_string_append (status, " - ");
 
125
                /* Translators: network device speed */
 
126
                g_string_append_printf (status, _("%d Mb/s"), speed);
 
127
        }
 
128
        gtk_label_set_label (GTK_LABEL (widget), status->str);
 
129
        g_string_free (status, TRUE);
 
130
        gtk_widget_set_tooltip_text (widget,
 
131
                                     panel_device_state_reason_to_localized_string (nm_device));
 
132
 
 
133
        /* device MAC */
 
134
        str = nm_device_ethernet_get_hw_address (NM_DEVICE_ETHERNET (nm_device));
 
135
        panel_set_device_widget_details (priv->builder, "mac", str);
 
136
 
 
137
        /* set IP entries */
 
138
        panel_set_device_widgets (priv->builder, nm_device);
 
139
}
 
140
 
 
141
static void
 
142
device_wired_refresh (NetObject *object)
 
143
{
 
144
        NetDeviceWired *device_wired = NET_DEVICE_WIRED (object);
 
145
        nm_device_wired_refresh_ui (device_wired);
 
146
}
 
147
 
 
148
static void
 
149
device_off_toggled (GtkSwitch *sw,
 
150
                    GParamSpec *pspec,
 
151
                    NetDeviceWired *device_wired)
 
152
{
 
153
        const gchar *path;
 
154
        const GPtrArray *acs;
 
155
        gboolean active;
 
156
        gint i;
 
157
        NMActiveConnection *a;
 
158
        NMConnection *connection;
 
159
        NMClient *client;
 
160
 
 
161
        if (device_wired->priv->updating_device)
 
162
                return;
 
163
 
 
164
        active = gtk_switch_get_active (sw);
 
165
        if (active) {
 
166
                client = net_object_get_client (NET_OBJECT (device_wired));
 
167
                connection = net_device_get_find_connection (NET_DEVICE (device_wired));
 
168
                if (connection == NULL)
 
169
                        return;
 
170
                nm_client_activate_connection (client,
 
171
                                               connection,
 
172
                                               net_device_get_nm_device (NET_DEVICE (device_wired)),
 
173
                                               NULL, NULL, NULL);
 
174
        } else {
 
175
                connection = net_device_get_find_connection (NET_DEVICE (device_wired));
 
176
                if (connection == NULL)
 
177
                        return;
 
178
                path = nm_connection_get_path (connection);
 
179
                client = net_object_get_client (NET_OBJECT (device_wired));
 
180
                acs = nm_client_get_active_connections (client);
 
181
                for (i = 0; i < acs->len; i++) {
 
182
                        a = (NMActiveConnection*)acs->pdata[i];
 
183
                        if (strcmp (nm_active_connection_get_connection (a), path) == 0) {
 
184
                                nm_client_deactivate_connection (client, a);
 
185
                                break;
 
186
                        }
 
187
                }
 
188
        }
 
189
}
 
190
 
 
191
static void
 
192
edit_connection (GtkButton *button, NetDeviceWired *device_wired)
 
193
{
 
194
        net_object_edit (NET_OBJECT (device_wired));
 
195
}
 
196
 
 
197
static void
 
198
net_device_wired_constructed (GObject *object)
 
199
{
 
200
        NetDeviceWired *device_wired = NET_DEVICE_WIRED (object);
 
201
 
 
202
        G_OBJECT_CLASS (net_device_wired_parent_class)->constructed (object);
 
203
 
 
204
        nm_device_wired_refresh_ui (device_wired);
 
205
}
 
206
 
 
207
static void
 
208
net_device_wired_finalize (GObject *object)
 
209
{
 
210
        NetDeviceWired *device_wired = NET_DEVICE_WIRED (object);
 
211
        NetDeviceWiredPrivate *priv = device_wired->priv;
 
212
 
 
213
        g_object_unref (priv->builder);
 
214
 
 
215
        G_OBJECT_CLASS (net_device_wired_parent_class)->finalize (object);
 
216
}
 
217
 
 
218
static void
 
219
net_device_wired_class_init (NetDeviceWiredClass *klass)
 
220
{
 
221
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
222
        NetObjectClass *parent_class = NET_OBJECT_CLASS (klass);
 
223
 
 
224
        object_class->finalize = net_device_wired_finalize;
 
225
        object_class->constructed = net_device_wired_constructed;
 
226
        parent_class->add_to_notebook = device_wired_proxy_add_to_notebook;
 
227
        parent_class->refresh = device_wired_refresh;
 
228
        g_type_class_add_private (klass, sizeof (NetDeviceWiredPrivate));
 
229
}
 
230
 
 
231
static void
 
232
net_device_wired_init (NetDeviceWired *device_wired)
 
233
{
 
234
        GError *error = NULL;
 
235
        GtkWidget *widget;
 
236
 
 
237
        device_wired->priv = NET_DEVICE_WIRED_GET_PRIVATE (device_wired);
 
238
 
 
239
        device_wired->priv->builder = gtk_builder_new ();
 
240
        gtk_builder_add_from_file (device_wired->priv->builder,
 
241
                                   GNOMECC_UI_DIR "/network-wired.ui",
 
242
                                   &error);
 
243
        if (error != NULL) {
 
244
                g_warning ("Could not load interface file: %s", error->message);
 
245
                g_error_free (error);
 
246
                return;
 
247
        }
 
248
 
 
249
        /* setup wired combobox model */
 
250
        widget = GTK_WIDGET (gtk_builder_get_object (device_wired->priv->builder,
 
251
                                                     "device_off_switch"));
 
252
        g_signal_connect (widget, "notify::active",
 
253
                          G_CALLBACK (device_off_toggled), device_wired);
 
254
 
 
255
        widget = GTK_WIDGET (gtk_builder_get_object (device_wired->priv->builder,
 
256
                                                     "button_options"));
 
257
        g_signal_connect (widget, "clicked",
 
258
                          G_CALLBACK (edit_connection), device_wired);
 
259
}