[network-manager-applet] editor: enable band&channel selection for Ad-Hoc connections



commit aedf921d973c7b8bc8da66d9d0ff24558a9bf234
Author: JiÅ?í KlimeÅ¡ <jklimes redhat com>
Date:   Mon May 31 15:05:33 2010 +0200

    editor: enable band&channel selection for Ad-Hoc connections
    
    Ad-Hoc connections support selection of band and frequency via wpa-supplicant.
    So we can enable these bits in nm-connection-editor.
    Utility functions for frequency/channel manipulation moved to libnm-util.

 src/connection-editor/page-wireless.c |   88 ++++++++++++-----
 src/utils/utils.c                     |  172 +++------------------------------
 2 files changed, 81 insertions(+), 179 deletions(-)
---
diff --git a/src/connection-editor/page-wireless.c b/src/connection-editor/page-wireless.c
index 078108c..4a9d7f3 100644
--- a/src/connection-editor/page-wireless.c
+++ b/src/connection-editor/page-wireless.c
@@ -17,7 +17,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * (C) Copyright 2008 Red Hat, Inc.
+ * (C) Copyright 2008 - 2010 Red Hat, Inc.
  */
 
 #include <string.h>
@@ -122,11 +122,11 @@ channel_spin_input_cb (GtkSpinButton *spin, gdouble *new_val, gpointer user_data
 	else
 		int_channel = ceil (channel);
 
-	if (utils_channel_to_freq (int_channel, aband ? "a" : "bg") == -1)
+	if (nm_utils_wifi_channel_to_freq (int_channel, aband ? "a" : "bg") == -1)
 		return GTK_INPUT_ERROR;
 
 	*new_val = channel;
-	return TRUE;
+	return 1;
 }
 
 static gint
@@ -147,23 +147,30 @@ channel_spin_output_cb (GtkSpinButton *spin, gpointer user_data)
 		if (channel == 0)
 			buf = g_strdup (_("default"));
 		else {
-			freq = utils_channel_to_freq (channel, aband ? "a" : "bg");
+			int direction = 0;
+			freq = nm_utils_wifi_channel_to_freq (channel, aband ? "a" : "bg");
 			if (freq == -1) {
-				int direction = 0;
-
 				if (priv->last_channel < channel)
 					direction = 1;
 				else if (priv->last_channel > channel)
 					direction = -1;
-				channel = utils_find_next_channel (channel, direction, aband ? "a" : "bg");
-				freq = utils_channel_to_freq (channel, aband ? "a" : "bg");
+				channel = nm_utils_wifi_find_next_channel (channel, direction, aband ? "a" : "bg");
+				gtk_spin_button_set_value (spin, channel);
+				freq = nm_utils_wifi_channel_to_freq (channel, aband ? "a" : "bg");
 				if (freq == -1) {
 					g_warning ("%s: invalid channel %d!", __func__, channel);
 					gtk_spin_button_set_value (spin, 0);
 					goto out;
 				}
+
 			}
-			buf = g_strdup_printf (_("%u (%u MHz)"), channel, freq);
+			/* Set spin button to zero to go to "default" from the lowest channel */
+			if (direction == -1 && priv->last_channel == channel) {
+				buf = g_strdup_printf (_("default"));
+				gtk_spin_button_set_value (spin, 0);
+				channel = 0;
+			} else
+				buf = g_strdup_printf (_("%u (%u MHz)"), channel, freq);
 		}
 		priv->last_channel = channel;
 	}
@@ -173,7 +180,7 @@ channel_spin_output_cb (GtkSpinButton *spin, gpointer user_data)
 
 out:
 	g_free (buf);
-	return TRUE;
+	return 1;
 }
 
 static void
@@ -195,13 +202,58 @@ band_value_changed_cb (GtkComboBox *box, gpointer user_data)
 		sensitive = FALSE;
  		break;
  	}
-	
+
 	gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), sensitive);
 
 	ce_page_changed (CE_PAGE (self));
 }
 
 static void
+mode_combo_changed_cb (GtkComboBox *combo,
+                       gpointer user_data)
+{
+	CEPageWireless *self = CE_PAGE_WIRELESS (user_data);
+	CEPageWirelessPrivate *priv = CE_PAGE_WIRELESS_GET_PRIVATE (self);
+	CEPage *parent = CE_PAGE (self);
+	GtkWidget *widget;
+	gboolean show;
+
+ 	switch (gtk_combo_box_get_active (GTK_COMBO_BOX (combo))) {
+ 	case 1: /* adhoc */
+		show = TRUE;
+ 		break;
+ 	default: /* infrastructure */
+		show = FALSE;
+ 		break;
+ 	}
+
+	if (show) {
+		widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
+		gtk_widget_show (widget);
+		gtk_widget_show (GTK_WIDGET (priv->band));
+		widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
+		gtk_widget_show (widget);
+		gtk_widget_show (GTK_WIDGET (priv->channel));
+	} else {
+		widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
+		gtk_widget_hide (widget);
+		gtk_widget_hide (GTK_WIDGET (priv->band));
+		widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
+		gtk_widget_hide (widget);
+		gtk_widget_hide (GTK_WIDGET (priv->channel));
+	}
+
+	widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
+	gtk_widget_set_sensitive (GTK_WIDGET (widget), show);
+	gtk_widget_set_sensitive (GTK_WIDGET (priv->band), show);
+	widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
+	gtk_widget_set_sensitive (GTK_WIDGET (widget), show);
+	gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), show);
+
+	ce_page_changed (CE_PAGE (self));
+}
+
+static void
 populate_ui (CEPageWireless *self)
 {
 	CEPageWirelessPrivate *priv = CE_PAGE_WIRELESS_GET_PRIVATE (self);
@@ -251,7 +303,8 @@ populate_ui (CEPageWireless *self)
 	gtk_combo_box_set_active (priv->mode, 0);
 	if (mode && !strcmp (mode, "adhoc"))
 		gtk_combo_box_set_active (priv->mode, 1);
-	g_signal_connect_swapped (priv->mode, "changed", G_CALLBACK (ce_page_changed), self);
+	mode_combo_changed_cb (priv->mode, self);
+	g_signal_connect (priv->mode, "changed", G_CALLBACK (mode_combo_changed_cb), self);
 
 	g_signal_connect (priv->channel, "output",
 	                  G_CALLBACK (channel_spin_output_cb),
@@ -306,17 +359,6 @@ finish_setup (CEPageWireless *self, gpointer unused, GError *error, gpointer use
 
 	populate_ui (self);
 
-	/* Hide widgets we don't yet support */
-	widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
-	gtk_widget_hide (widget);
-	widget = glade_xml_get_widget (parent->xml, "wireless_band");
-	gtk_widget_hide (widget);
-
-	widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
-	gtk_widget_hide (widget);
-	widget = glade_xml_get_widget (parent->xml, "wireless_channel");
-	gtk_widget_hide (widget);
-
 	widget = glade_xml_get_widget (parent->xml, "wireless_tx_power_label");
 	gtk_widget_hide (widget);
 	widget = glade_xml_get_widget (parent->xml, "wireless_tx_power_hbox");
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 227c382..6d4f33e 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -196,152 +196,6 @@ utils_get_device_description (NMDevice *device)
 	return description;
 }
 
-struct cf_pair {
-	guint32 chan;
-	guint32 freq;
-};
-
-static struct cf_pair a_table[] = {
-	/* A band */
-	{  7, 5035 },
-	{  8, 5040 },
-	{  9, 5045 },
-	{ 11, 5055 },
-	{ 12, 5060 },
-	{ 16, 5080 },
-	{ 34, 5170 },
-	{ 36, 5180 },
-	{ 38, 5190 },
-	{ 40, 5200 },
-	{ 42, 5210 },
-	{ 44, 5220 },
-	{ 46, 5230 },
-	{ 48, 5240 },
-	{ 50, 5250 },
-	{ 52, 5260 },
-	{ 56, 5280 },
-	{ 58, 5290 },
-	{ 60, 5300 },
-	{ 64, 5320 },
-	{ 100, 5500 },
-	{ 104, 5520 },
-	{ 108, 5540 },
-	{ 112, 5560 },
-	{ 116, 5580 },
-	{ 120, 5600 },
-	{ 124, 5620 },
-	{ 128, 5640 },
-	{ 132, 5660 },
-	{ 136, 5680 },
-	{ 140, 5700 },
-	{ 149, 5745 },
-	{ 152, 5760 },
-	{ 153, 5765 },
-	{ 157, 5785 },
-	{ 160, 5800 },
-	{ 161, 5805 },
-	{ 165, 5825 },
-	{ 183, 4915 },
-	{ 184, 4920 },
-	{ 185, 4925 },
-	{ 187, 4935 },
-	{ 188, 4945 },
-	{ 192, 4960 },
-	{ 196, 4980 },
-	{ 0, -1 }
-};
-
-static struct cf_pair bg_table[] = {
-	/* B/G band */
-	{ 1, 2412 },
-	{ 2, 2417 },
-	{ 3, 2422 },
-	{ 4, 2427 },
-	{ 5, 2432 },
-	{ 6, 2437 },
-	{ 7, 2442 },
-	{ 8, 2447 },
-	{ 9, 2452 },
-	{ 10, 2457 },
-	{ 11, 2462 },
-	{ 12, 2467 },
-	{ 13, 2472 },
-	{ 14, 2484 },
-	{ 0, -1 }
-};
-
-guint32
-utils_freq_to_channel (guint32 freq)
-{
-	int i = 0;
-
-	while (a_table[i].chan && (a_table[i].freq != freq))
-		i++;
-	if (a_table[i].chan)
-		return a_table[i].chan;
-
-	i = 0;
-	while (bg_table[i].chan && (bg_table[i].freq != freq))
-		i++;
-	return bg_table[i].chan;
-}
-
-guint32
-utils_channel_to_freq (guint32 channel, char *band)
-{
-	int i = 0;
-
-	if (!strcmp (band, "a")) {
-		while (a_table[i].chan && (a_table[i].chan != channel))
-			i++;
-		return a_table[i].freq;
-	} else if (!strcmp (band, "bg")) {
-		while (bg_table[i].chan && (bg_table[i].chan != channel))
-			i++;
-		return bg_table[i].freq;
-	}
-
-	return 0;
-}
-
-guint32
-utils_find_next_channel (guint32 channel, int direction, char *band)
-{
-	size_t a_size = sizeof (a_table) / sizeof (struct cf_pair);
-	size_t bg_size = sizeof (bg_table) / sizeof (struct cf_pair);
-	struct cf_pair *pair = NULL;
-
-	if (!strcmp (band, "a")) {
-		if (channel < a_table[0].chan)
-			return a_table[0].chan;
-		if (channel > a_table[a_size - 2].chan)
-			return a_table[a_size - 2].chan;
-		pair = &a_table[0];
-	} else if (!strcmp (band, "bg")) {
-		if (channel < bg_table[0].chan)
-			return bg_table[0].chan;
-		if (channel > bg_table[bg_size - 2].chan)
-			return bg_table[bg_size - 2].chan;
-		pair = &bg_table[0];
-	} else {
-		g_assert_not_reached ();
-		return 0;
-	}
-
-	while (pair->chan) {
-		if (channel == pair->chan)
-			return channel;
-		if ((channel < (pair+1)->chan) && (channel > pair->chan)) {
-			if (direction > 0)	
-				return (pair+1)->chan;
-			else
-				return pair->chan;
-		}
-		pair++;
-	}
-	return 0;
-}
-
 /*
  * utils_ether_addr_valid
  *
@@ -387,7 +241,7 @@ utils_check_ap_compatible (NMAccessPoint *ap,
 	const char *setting_mode;
 	const char *setting_band;
 	NM80211Mode ap_mode;
-	guint32 freq;
+	guint32 freq, channel;
 
 	g_return_val_if_fail (NM_IS_ACCESS_POINT (ap), FALSE);
 	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
@@ -424,25 +278,31 @@ utils_check_ap_compatible (NMAccessPoint *ap,
 	setting_band = nm_setting_wireless_get_band (s_wireless);
 	if (setting_band) {
 		if (!strcmp (setting_band, "a")) {
-			if (freq < 5170 || freq > 5825)
+			if (freq < 4915 || freq > 5825)
 				return FALSE;
 		} else if (!strcmp (setting_band, "bg")) {
-			if (freq < 2412 || freq > 2472)
+			if (freq < 2412 || freq > 2484)
 				return FALSE;
 		}
 	}
 
-	// FIXME: channel check
+	channel = nm_setting_wireless_get_channel (s_wireless);
+	if (channel) {
+		guint32 ap_chan = nm_utils_wifi_freq_to_channel (freq);
+
+		if (channel != ap_chan)
+			return FALSE;
+	}
 
 	s_wireless_sec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection,
-															    NM_TYPE_SETTING_WIRELESS_SECURITY);
+	                                                                          NM_TYPE_SETTING_WIRELESS_SECURITY);
 
 	return nm_setting_wireless_ap_security_compatible (s_wireless,
-											 s_wireless_sec,
-											 nm_access_point_get_flags (ap),
-											 nm_access_point_get_wpa_flags (ap),
-											 nm_access_point_get_rsn_flags (ap),
-											 nm_access_point_get_mode (ap));
+	                                                   s_wireless_sec,
+	                                                   nm_access_point_get_flags (ap),
+	                                                   nm_access_point_get_wpa_flags (ap),
+	                                                   nm_access_point_get_rsn_flags (ap),
+	                                                   nm_access_point_get_mode (ap));
 }
 
 static gboolean



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]