page-wired.c 9.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager Connection editor -- Connection editor for NetworkManager
 *
 * Dan Williams <dcbw@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
16 17 18
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22 23
 *
 * (C) Copyright 2008 Red Hat, Inc.
 */

#include <string.h>
24
#include <net/ethernet.h>
25 26 27 28 29 30 31 32 33

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include <nm-setting-connection.h>
#include <nm-setting-wired.h>

#include "page-wired.h"

34 35
G_DEFINE_TYPE (CEPageWired, ce_page_wired, CE_TYPE_PAGE)

Tambet Ingo's avatar
Tambet Ingo committed
36 37 38 39 40
#define CE_PAGE_WIRED_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CE_TYPE_PAGE_WIRED, CEPageWiredPrivate))

typedef struct {
	NMSettingWired *setting;

41
	GtkEntry *mac;
Tambet Ingo's avatar
Tambet Ingo committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	GtkComboBox *port;
	GtkComboBox *speed;
	GtkToggleButton *duplex;
	GtkToggleButton *autonegotiate;
	GtkSpinButton *mtu;

	gboolean disposed;
} CEPageWiredPrivate;

#define PORT_DEFAULT  0
#define PORT_TP       1
#define PORT_AUI      2
#define PORT_BNC      3
#define PORT_MII      4

#define SPEED_DEFAULT 0
#define SPEED_10      1
#define SPEED_100     2
#define SPEED_1000    3
#define SPEED_10000   4

static void
wired_private_init (CEPageWired *self)
{
	CEPageWiredPrivate *priv = CE_PAGE_WIRED_GET_PRIVATE (self);
	GladeXML *xml;

	xml = CE_PAGE (self)->xml;

71
	priv->mac = GTK_ENTRY (glade_xml_get_widget (xml, "wired_mac"));
Tambet Ingo's avatar
Tambet Ingo committed
72 73 74 75 76 77 78
	priv->port = GTK_COMBO_BOX (glade_xml_get_widget (xml, "wired_port"));
	priv->speed = GTK_COMBO_BOX (glade_xml_get_widget (xml, "wired_speed"));
	priv->duplex = GTK_TOGGLE_BUTTON (glade_xml_get_widget (xml, "wired_duplex"));
	priv->autonegotiate = GTK_TOGGLE_BUTTON (glade_xml_get_widget (xml, "wired_autonegotiate"));
	priv->mtu = GTK_SPIN_BUTTON (glade_xml_get_widget (xml, "wired_mtu"));
}

79 80 81 82 83 84
static void
stuff_changed (GtkWidget *w, gpointer user_data)
{
	ce_page_changed (CE_PAGE (user_data));
}

Tambet Ingo's avatar
Tambet Ingo committed
85 86 87 88 89
static void
populate_ui (CEPageWired *self)
{
	CEPageWiredPrivate *priv = CE_PAGE_WIRED_GET_PRIVATE (self);
	NMSettingWired *setting = priv->setting;
90 91
	const char *port;
	const char *duplex;
Tambet Ingo's avatar
Tambet Ingo committed
92 93 94 95 96
	int port_idx = PORT_DEFAULT;
	int speed_idx;
	int mtu_def;

	/* Port */
97 98 99
	port = nm_setting_wired_get_port (setting);
	if (port) {
		if (!strcmp (port, "tp"))
Tambet Ingo's avatar
Tambet Ingo committed
100
			port_idx = PORT_TP;
101
		else if (!strcmp (port, "aui"))
Tambet Ingo's avatar
Tambet Ingo committed
102
			port_idx = PORT_AUI;
103
		else if (!strcmp (port, "bnc"))
Tambet Ingo's avatar
Tambet Ingo committed
104
			port_idx = PORT_BNC;
105
		else if (!strcmp (port, "mii"))
Tambet Ingo's avatar
Tambet Ingo committed
106 107 108 109 110
			port_idx = PORT_MII;
	}
	gtk_combo_box_set_active (priv->port, port_idx);

	/* Speed */
111
	switch (nm_setting_wired_get_speed (setting)) {
Tambet Ingo's avatar
Tambet Ingo committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	case 10:
		speed_idx = SPEED_10;
		break;
	case 100:
		speed_idx = SPEED_100;
		break;
	case 1000:
		speed_idx = SPEED_1000;
		break;
	case 10000:
		speed_idx = SPEED_10000;
		break;
	default:
		speed_idx = SPEED_DEFAULT;
		break;
	}
	gtk_combo_box_set_active (priv->speed, speed_idx);

	/* Duplex */
131 132
	duplex = nm_setting_wired_get_duplex (setting);
	if (duplex && !strcmp (duplex, "half"))
Tambet Ingo's avatar
Tambet Ingo committed
133 134 135 136 137
		gtk_toggle_button_set_active (priv->duplex, FALSE);
	else
		gtk_toggle_button_set_active (priv->duplex, TRUE);

	/* Autonegotiate */
138
	gtk_toggle_button_set_active (priv->autonegotiate, 
139
	                              nm_setting_wired_get_auto_negotiate (setting));
Tambet Ingo's avatar
Tambet Ingo committed
140

141
	/* MAC address */
142
	ce_page_mac_to_entry (nm_setting_wired_get_mac_address (setting), priv->mac);
143
	g_signal_connect (priv->mac, "changed", G_CALLBACK (stuff_changed), self);
Tambet Ingo's avatar
Tambet Ingo committed
144 145 146 147 148 149 150

	/* MTU */
	mtu_def = ce_get_property_default (NM_SETTING (setting), NM_SETTING_WIRED_MTU);
	g_signal_connect (priv->mtu, "output",
	                  G_CALLBACK (ce_spin_output_with_default),
	                  GINT_TO_POINTER (mtu_def));

151
	gtk_spin_button_set_value (priv->mtu, (gdouble) nm_setting_wired_get_mtu (setting));
Tambet Ingo's avatar
Tambet Ingo committed
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
static void
finish_setup (CEPageWired *self, gpointer unused, GError *error, gpointer user_data)
{
	CEPage *parent = CE_PAGE (self);
	CEPageWiredPrivate *priv = CE_PAGE_WIRED_GET_PRIVATE (self);
	GtkWidget *widget;

	if (error)
		return;

	populate_ui (self);

	g_signal_connect (priv->port, "changed", G_CALLBACK (stuff_changed), self);
	g_signal_connect (priv->speed, "changed", G_CALLBACK (stuff_changed), self);
	g_signal_connect (priv->duplex, "toggled", G_CALLBACK (stuff_changed), self);
	g_signal_connect (priv->autonegotiate, "toggled", G_CALLBACK (stuff_changed), self);
	g_signal_connect (priv->mtu, "value-changed", G_CALLBACK (stuff_changed), self);

	/* Hide widgets we don't yet support */
	widget = glade_xml_get_widget (parent->xml, "wired_port_label");
	gtk_widget_hide (widget);
	widget = glade_xml_get_widget (parent->xml, "wired_port");
	gtk_widget_hide (widget);

	widget = glade_xml_get_widget (parent->xml, "wired_speed_label");
	gtk_widget_hide (widget);
	widget = glade_xml_get_widget (parent->xml, "wired_speed");
	gtk_widget_hide (widget);

	widget = glade_xml_get_widget (parent->xml, "wired_duplex");
	gtk_widget_hide (widget);
	widget = glade_xml_get_widget (parent->xml, "wired_autonegotiate");
	gtk_widget_hide (widget);
}

CEPage *
190 191 192 193
ce_page_wired_new (NMConnection *connection,
                   GtkWindow *parent_window,
                   const char **out_secrets_setting_name,
                   GError **error)
194
{
195
	CEPageWired *self;
Tambet Ingo's avatar
Tambet Ingo committed
196
	CEPageWiredPrivate *priv;
197
	CEPage *parent;
198

199 200 201 202
	self = CE_PAGE_WIRED (g_object_new (CE_TYPE_PAGE_WIRED,
	                                    CE_PAGE_CONNECTION, connection,
	                                    CE_PAGE_PARENT_WINDOW, parent_window,
	                                    NULL));
203 204 205 206
	parent = CE_PAGE (self);

	parent->xml = glade_xml_new (GLADEDIR "/ce-page-wired.glade", "WiredPage", NULL);
	if (!parent->xml) {
207
		g_set_error (error, 0, 0, "%s", _("Could not load wired user interface."));
208 209 210 211 212 213
		g_object_unref (self);
		return NULL;
	}

	parent->page = glade_xml_get_widget (parent->xml, "WiredPage");
	if (!parent->page) {
214
		g_set_error (error, 0, 0, "%s", _("Could not load wired user interface."));
215 216 217 218
		g_object_unref (self);
		return NULL;
	}
	g_object_ref_sink (parent->page);
219

220
	parent->title = g_strdup (_("Wired"));
221

Tambet Ingo's avatar
Tambet Ingo committed
222 223
	wired_private_init (self);
	priv = CE_PAGE_WIRED_GET_PRIVATE (self);
224

225 226
	priv->setting = (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
	if (!priv->setting) {
Tambet Ingo's avatar
Tambet Ingo committed
227
		priv->setting = NM_SETTING_WIRED (nm_setting_wired_new ());
228 229
		nm_connection_add_setting (connection, NM_SETTING (priv->setting));
	}
230

231
	g_signal_connect (self, "initialized", G_CALLBACK (finish_setup), NULL);
232

233
	return CE_PAGE (self);
Tambet Ingo's avatar
Tambet Ingo committed
234 235 236 237 238 239 240 241
}

static void
ui_to_setting (CEPageWired *self)
{
	CEPageWiredPrivate *priv = CE_PAGE_WIRED_GET_PRIVATE (self);
	const char *port;
	guint32 speed;
242
	GByteArray *mac = NULL;
Tambet Ingo's avatar
Tambet Ingo committed
243 244 245 246 247

	/* Port */
	switch (gtk_combo_box_get_active (priv->port)) {
	case PORT_TP:
		port = "tp";
248
		break;
Tambet Ingo's avatar
Tambet Ingo committed
249 250
	case PORT_AUI:
		port = "aui";
251
		break;
Tambet Ingo's avatar
Tambet Ingo committed
252 253
	case PORT_BNC:
		port = "bnc";
254
		break;
Tambet Ingo's avatar
Tambet Ingo committed
255 256
	case PORT_MII:
		port = "mii";
257 258
		break;
	default:
Tambet Ingo's avatar
Tambet Ingo committed
259
		port = NULL;
260 261 262
		break;
	}

Tambet Ingo's avatar
Tambet Ingo committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	/* Speed */
	switch (gtk_combo_box_get_active (priv->speed)) {
	case SPEED_10:
		speed = 10;
		break;
	case SPEED_100:
		speed = 100;
		break;
	case SPEED_1000:
		speed = 1000;
		break;
	case SPEED_10000:
		speed = 10000;
		break;
	default:
		speed = 0;
		break;
	}
281

282 283
	mac = ce_page_entry_to_mac (priv->mac, NULL);

Tambet Ingo's avatar
Tambet Ingo committed
284
	g_object_set (priv->setting,
285
				  NM_SETTING_WIRED_MAC_ADDRESS, mac,
Tambet Ingo's avatar
Tambet Ingo committed
286 287 288 289 290 291
				  NM_SETTING_WIRED_PORT, port,
				  NM_SETTING_WIRED_SPEED, speed,
				  NM_SETTING_WIRED_DUPLEX, gtk_toggle_button_get_active (priv->duplex) ? "full" : "half",
				  NM_SETTING_WIRED_AUTO_NEGOTIATE, gtk_toggle_button_get_active (priv->autonegotiate),
				  NM_SETTING_WIRED_MTU, (guint32) gtk_spin_button_get_value_as_int (priv->mtu),
				  NULL);
292 293 294

	if (mac)
		g_byte_array_free (mac, TRUE);
Tambet Ingo's avatar
Tambet Ingo committed
295
}
296

Tambet Ingo's avatar
Tambet Ingo committed
297
static gboolean
298
validate (CEPage *page, NMConnection *connection, GError **error)
Tambet Ingo's avatar
Tambet Ingo committed
299 300 301
{
	CEPageWired *self = CE_PAGE_WIRED (page);
	CEPageWiredPrivate *priv = CE_PAGE_WIRED_GET_PRIVATE (self);
302 303 304 305 306 307
	gboolean invalid = FALSE;
	GByteArray *ignore;

	ignore = ce_page_entry_to_mac (priv->mac, &invalid);
	if (invalid)
		return FALSE;
308

Tambet Ingo's avatar
Tambet Ingo committed
309
	ui_to_setting (self);
310
	return nm_setting_verify (NM_SETTING (priv->setting), NULL, error);
311 312 313 314 315
}

static void
ce_page_wired_init (CEPageWired *self)
{
316 317
}

318 319 320
static void
ce_page_wired_class_init (CEPageWiredClass *wired_class)
{
Tambet Ingo's avatar
Tambet Ingo committed
321
	GObjectClass *object_class = G_OBJECT_CLASS (wired_class);
322
	CEPageClass *parent_class = CE_PAGE_CLASS (wired_class);
323

Tambet Ingo's avatar
Tambet Ingo committed
324 325
	g_type_class_add_private (object_class, sizeof (CEPageWiredPrivate));

326
	/* virtual methods */
Tambet Ingo's avatar
Tambet Ingo committed
327
	parent_class->validate = validate;
328
}
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349


void
wired_connection_new (GtkWindow *parent,
                      PageNewConnectionResultFunc result_func,
                      PageGetConnectionsFunc get_connections_func,
                      gpointer user_data)
{
	NMConnection *connection;

	connection = ce_page_new_connection (_("Wired connection %d"),
	                                     NM_SETTING_WIRED_SETTING_NAME,
	                                     TRUE,
	                                     get_connections_func,
	                                     user_data);
	nm_connection_add_setting (connection, nm_setting_wired_new ());

	(*result_func) (connection, FALSE, NULL, user_data);
}