[network-manager-applet/danw/vlan: 2/5] connection-editor: further simplify MAC combos



commit 22c2adb352676cafe2e0ad4f3d380c83ac14fa79
Author: Dan Winship <danw gnome org>
Date:   Wed Aug 8 15:09:49 2012 -0400

    connection-editor: further simplify MAC combos
    
    Move more duplicated code into CEPage, for setting up the "Device MAC"
    combo boxes.

 src/connection-editor/ce-page.c         |   40 +++++++++++++++++++++++++++++++
 src/connection-editor/ce-page.h         |    2 +
 src/connection-editor/page-ethernet.c   |   29 ++--------------------
 src/connection-editor/page-infiniband.c |   33 +++----------------------
 src/connection-editor/page-wifi.c       |   29 ++--------------------
 src/connection-editor/page-wimax.c      |   29 ++--------------------
 6 files changed, 55 insertions(+), 107 deletions(-)
---
diff --git a/src/connection-editor/ce-page.c b/src/connection-editor/ce-page.c
index e0cae96..f363351 100644
--- a/src/connection-editor/ce-page.c
+++ b/src/connection-editor/ce-page.c
@@ -158,6 +158,46 @@ ce_page_get_mac_list (CEPage *self, GType device_type, const char *mac_property)
 }
 
 void
+ce_page_setup_mac_combo (CEPage *self, GtkComboBox *combo,
+                         const char *current_mac, char **mac_list)
+{
+	char **iter, *active_mac = NULL;
+	int current_mac_len;
+	GtkWidget *entry;
+
+	if (current_mac)
+		current_mac_len = strlen (current_mac);
+	else
+		current_mac_len = -1;
+
+	for (iter = mac_list; iter && *iter; iter++) {
+#if GTK_CHECK_VERSION (2,24,0)
+		gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), *iter);
+#else
+		gtk_combo_box_append_text (combo, *iter);
+#endif
+		if (   current_mac
+		    && g_ascii_strncasecmp (*iter, current_mac, current_mac_len) == 0
+		    && ((*iter)[current_mac_len] == '\0' || (*iter)[current_mac_len] == ' '))
+			active_mac = *iter;
+	}
+
+	if (current_mac) {
+		if (!active_mac) {
+#if GTK_CHECK_VERSION (2,24,0)
+			gtk_combo_box_text_prepend_text (GTK_COMBO_BOX_TEXT (combo), current_mac);
+#else
+			gtk_combo_box_prepend_text (combo, current_mac_str);
+#endif
+		}
+
+		entry = gtk_bin_get_child (GTK_BIN (combo));
+		if (entry)
+			gtk_entry_set_text (GTK_ENTRY (entry), active_mac ? active_mac : current_mac);
+	}
+}
+
+void
 ce_page_mac_to_entry (const GByteArray *mac, int type, GtkEntry *entry)
 {
 	char *str_addr;
diff --git a/src/connection-editor/ce-page.h b/src/connection-editor/ce-page.h
index b6a0d28..ed63f66 100644
--- a/src/connection-editor/ce-page.h
+++ b/src/connection-editor/ce-page.h
@@ -110,6 +110,8 @@ const char * ce_page_get_title (CEPage *self);
 gboolean ce_page_validate (CEPage *self, NMConnection *connection, GError **error);
 
 char **ce_page_get_mac_list (CEPage *self, GType device_type, const char *mac_property);
+void ce_page_setup_mac_combo (CEPage *self, GtkComboBox *combo,
+                              const char *current_mac, char **mac_list);
 
 void ce_page_changed (CEPage *self);
 
diff --git a/src/connection-editor/page-ethernet.c b/src/connection-editor/page-ethernet.c
index c6ac1a8..d0485c1 100644
--- a/src/connection-editor/page-ethernet.c
+++ b/src/connection-editor/page-ethernet.c
@@ -121,11 +121,9 @@ populate_ui (CEPageEthernet *self)
 	int port_idx = PORT_DEFAULT;
 	int speed_idx;
 	int mtu_def;
-	char **mac_list, **iter;
+	char **mac_list;
 	const GByteArray *s_mac;
 	char *s_mac_str;
-	char *active_mac = NULL;
-	GtkWidget *entry;
 
 	/* Port */
 	port = nm_setting_wired_get_port (setting);
@@ -177,29 +175,8 @@ populate_ui (CEPageEthernet *self)
 	                                 NM_DEVICE_ETHERNET_PERMANENT_HW_ADDRESS);
 	s_mac = nm_setting_wired_get_mac_address (setting);
 	s_mac_str = s_mac ? nm_utils_hwaddr_ntoa (s_mac->data, ARPHRD_ETHER) : NULL;
-	for (iter = mac_list; iter && *iter; iter++) {
-#if GTK_CHECK_VERSION (2,24,0)
-		gtk_combo_box_text_append_text (priv->device_mac, *iter);
-#else
-		gtk_combo_box_append_text (GTK_COMBO_BOX (priv->device_mac), *iter);
-#endif
-		if (s_mac_str && g_ascii_strncasecmp (*iter, s_mac_str, 17) == 0)
-			active_mac = *iter;
-	}
-
-	if (s_mac_str) {
-		if (!active_mac) {
-#if GTK_CHECK_VERSION (2,24,0)
-			gtk_combo_box_text_prepend_text (priv->device_mac, s_mac_str);
-#else
-			gtk_combo_box_prepend_text (GTK_COMBO_BOX (priv->device_mac), s_mac_str);
-#endif
-		}
-
-		entry = gtk_bin_get_child (GTK_BIN (priv->device_mac));
-		if (entry)
-			gtk_entry_set_text (GTK_ENTRY (entry), active_mac ? active_mac : s_mac_str);
-	}
+	ce_page_setup_mac_combo (CE_PAGE (self), GTK_COMBO_BOX (priv->device_mac),
+	                         s_mac_str, mac_list);
 	g_free (s_mac_str);
 	g_strfreev (mac_list);
 	g_signal_connect (priv->device_mac, "changed", G_CALLBACK (stuff_changed), self);
diff --git a/src/connection-editor/page-infiniband.c b/src/connection-editor/page-infiniband.c
index 190a12c..f298e27 100644
--- a/src/connection-editor/page-infiniband.c
+++ b/src/connection-editor/page-infiniband.c
@@ -99,11 +99,9 @@ populate_ui (CEPageInfiniband *self)
 	const char *mode;
 	int mode_idx = TRANSPORT_MODE_DATAGRAM;
 	int mtu_def;
-	char **mac_list, **iter;
+	char **mac_list;
 	const GByteArray *s_mac;
 	char *s_mac_str;
-	char *active_mac = NULL;
-	GtkWidget *entry;
 
 	/* Port */
 	mode = nm_setting_infiniband_get_transport_mode (setting);
@@ -119,32 +117,9 @@ populate_ui (CEPageInfiniband *self)
 	mac_list = ce_page_get_mac_list (CE_PAGE (self), NM_TYPE_DEVICE_INFINIBAND,
 	                                 NM_DEVICE_INFINIBAND_HW_ADDRESS);
 	s_mac = nm_setting_infiniband_get_mac_address (setting);
-	s_mac_str = s_mac ? nm_utils_hwaddr_ntoa (s_mac->data, ARPHRD_INFINIBAND):
-	                    NULL;
-
-	for (iter = mac_list; iter && *iter; iter++) {
-#if GTK_CHECK_VERSION (2,24,0)
-		gtk_combo_box_text_append_text (priv->device_mac, *iter);
-#else
-		gtk_combo_box_append_text (GTK_COMBO_BOX (priv->device_mac), *iter);
-#endif
-		if (s_mac_str && g_ascii_strncasecmp (*iter, s_mac_str, 59) == 0)
-			active_mac = *iter;
-	}
-
-	if (s_mac_str) {
-		if (!active_mac) {
-#if GTK_CHECK_VERSION (2,24,0)
-			gtk_combo_box_text_prepend_text (priv->device_mac, s_mac_str);
-#else
-			gtk_combo_box_prepend_text (GTK_COMBO_BOX (priv->device_mac), s_mac_str);
-#endif
-		}
-
-		entry = gtk_bin_get_child (GTK_BIN (priv->device_mac));
-		if (entry)
-			gtk_entry_set_text (GTK_ENTRY (entry), active_mac ? active_mac : s_mac_str);
-	}
+	s_mac_str = s_mac ? nm_utils_hwaddr_ntoa (s_mac->data, ARPHRD_INFINIBAND) : NULL;
+	ce_page_setup_mac_combo (CE_PAGE (self), GTK_COMBO_BOX (priv->device_mac),
+	                         s_mac_str, mac_list);
 	g_free (s_mac_str);
 	g_strfreev (mac_list);
 	g_signal_connect (priv->device_mac, "changed", G_CALLBACK (stuff_changed), self);
diff --git a/src/connection-editor/page-wifi.c b/src/connection-editor/page-wifi.c
index 0c11feb..0c8d6ce 100644
--- a/src/connection-editor/page-wifi.c
+++ b/src/connection-editor/page-wifi.c
@@ -293,11 +293,9 @@ populate_ui (CEPageWifi *self)
 	int tx_power_def;
 	int mtu_def;
 	char *utf8_ssid;
-	char **mac_list, **iter;
+	char **mac_list;
 	const GByteArray *s_mac;
 	char *s_mac_str;
-	char *active_mac = NULL;
-	GtkWidget *entry;
 
 	rate_def = ce_get_property_default (NM_SETTING (setting), NM_SETTING_WIRELESS_RATE);
 	g_signal_connect (priv->rate, "output",
@@ -380,29 +378,8 @@ populate_ui (CEPageWifi *self)
 	                                 NM_DEVICE_WIFI_PERMANENT_HW_ADDRESS);
 	s_mac = nm_setting_wireless_get_mac_address (setting);
 	s_mac_str = s_mac ? nm_utils_hwaddr_ntoa (s_mac->data, ARPHRD_ETHER) : NULL;
-	for (iter = mac_list; iter && *iter; iter++) {
-#if GTK_CHECK_VERSION (2,24,0)
-		gtk_combo_box_text_append_text (priv->device_mac, *iter);
-#else
-		gtk_combo_box_append_text (GTK_COMBO_BOX (priv->device_mac), *iter);
-#endif
-		if (s_mac_str && g_ascii_strncasecmp (*iter, s_mac_str, 17) == 0)
-			active_mac = *iter;
-	}
-
-	if (s_mac_str) {
-		if (!active_mac) {
-#if GTK_CHECK_VERSION (2,24,0)
-			gtk_combo_box_text_prepend_text (priv->device_mac, s_mac_str);
-#else
-			gtk_combo_box_prepend_text (GTK_COMBO_BOX (priv->device_mac), s_mac_str);
-#endif
-		}
-
-		entry = gtk_bin_get_child (GTK_BIN (priv->device_mac));
-		if (entry)
-			gtk_entry_set_text (GTK_ENTRY (entry), active_mac ? active_mac : s_mac_str);
-	}
+	ce_page_setup_mac_combo (CE_PAGE (self), GTK_COMBO_BOX (priv->device_mac),
+	                         s_mac_str, mac_list);
 	g_free (s_mac_str);
 	g_strfreev (mac_list);
 	g_signal_connect_swapped (priv->device_mac, "changed", G_CALLBACK (ce_page_changed), self);
diff --git a/src/connection-editor/page-wimax.c b/src/connection-editor/page-wimax.c
index e3308bf..9d99ae8 100644
--- a/src/connection-editor/page-wimax.c
+++ b/src/connection-editor/page-wimax.c
@@ -83,11 +83,9 @@ populate_ui (CEPageWimax *self)
 {
 	CEPageWimaxPrivate *priv = CE_PAGE_WIMAX_GET_PRIVATE (self);
 	NMSettingWimax *setting = priv->setting;
-	char **mac_list, **iter;
+	char **mac_list;
 	const GByteArray *s_mac;
 	char *s_mac_str;
-	char *active_mac = NULL;
-	GtkWidget *entry;
 
 	gtk_entry_set_text (priv->name, nm_setting_wimax_get_network_name (setting));
 	g_signal_connect_swapped (priv->name, "changed", G_CALLBACK (ce_page_changed), self);
@@ -97,29 +95,8 @@ populate_ui (CEPageWimax *self)
 	                                 NM_DEVICE_WIMAX_HW_ADDRESS);
 	s_mac = nm_setting_wimax_get_mac_address (setting);
 	s_mac_str = s_mac ? nm_utils_hwaddr_ntoa (s_mac->data, ARPHRD_ETHER) : NULL;
-	for (iter = mac_list; iter && *iter; iter++) {
-#if GTK_CHECK_VERSION (2,24,0)
-		gtk_combo_box_text_append_text (priv->device_mac, *iter);
-#else
-		gtk_combo_box_append_text (GTK_COMBO_BOX (priv->device_mac), *iter);
-#endif
-		if (s_mac_str && g_ascii_strncasecmp (*iter, s_mac_str, 17) == 0)
-			active_mac = *iter;
-	}
-
-	if (s_mac_str) {
-		if (!active_mac) {
-#if GTK_CHECK_VERSION (2,24,0)
-			gtk_combo_box_text_prepend_text (priv->device_mac, s_mac_str);
-#else
-			gtk_combo_box_prepend_text (GTK_COMBO_BOX (priv->device_mac), s_mac_str);
-#endif
-		}
-
-		entry = gtk_bin_get_child (GTK_BIN (priv->device_mac));
-		if (entry)
-			gtk_entry_set_text (GTK_ENTRY (entry), active_mac ? active_mac : s_mac_str);
-	}
+	ce_page_setup_mac_combo (CE_PAGE (self), GTK_COMBO_BOX (priv->device_mac),
+	                         s_mac_str, mac_list);
 	g_free (s_mac_str);
 	g_strfreev (mac_list);
 	g_signal_connect_swapped (priv->device_mac, "changed", G_CALLBACK (ce_page_changed), self);



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