[network-manager-applet/NMA_0_8] editor: validate static IP/route values while editing cells in treeview



commit 042c1970bee00537ca3f599e7d19dfc042c149e6
Author: JiÅ?í KlimeÅ¡ <jklimes redhat com>
Date:   Fri Apr 29 09:01:26 2011 -0400

    editor: validate static IP/route values while editing cells in treeview
    
    And make that visible through changing cell backround colour to green/red.

 src/connection-editor/ip4-routes-dialog.c |   71 +++++++++++++++++++++
 src/connection-editor/ip6-routes-dialog.c |   61 ++++++++++++++++++
 src/connection-editor/page-ip4.c          |   98 ++++++++++++++++++++--------
 src/connection-editor/page-ip6.c          |   47 ++++++++++++++
 4 files changed, 249 insertions(+), 28 deletions(-)
---
diff --git a/src/connection-editor/ip4-routes-dialog.c b/src/connection-editor/ip4-routes-dialog.c
index 2e3850d..7583a71 100644
--- a/src/connection-editor/ip4-routes-dialog.c
+++ b/src/connection-editor/ip4-routes-dialog.c
@@ -408,6 +408,67 @@ delete_text_cb (GtkEditable *editable,
 }
 
 static gboolean
+cell_changed_cb (GtkEditable *editable,
+                 gpointer user_data)
+{
+	char *cell_text;
+	guint column;
+	GdkColor color;
+	gboolean value_valid = FALSE;
+
+	cell_text = gtk_editable_get_chars (editable, 0, -1);
+
+	/* The Netmask column can also contain prefix */
+	column = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (user_data), "column"));
+
+	if (column == COL_PREFIX) {
+		/* The COL_PREFIX can contain IP address or prefix */
+		guint32 tmp_prefix;
+
+		errno = 0;
+
+		/* Is it a prefix? */
+		if (!strchr (cell_text, '.')) {
+			tmp_prefix = strtol (cell_text, NULL, 10);
+			if (!errno && tmp_prefix >= 0 && tmp_prefix <= 32)
+				value_valid = TRUE;
+		} else {
+			struct in_addr tmp_addr;
+
+			/* Is it a netmask? */
+			if (inet_pton (AF_INET, cell_text, &tmp_addr) > 0)
+				value_valid = TRUE;
+		}
+	} else if (column == COL_METRIC) {
+		long int tmp_int;
+
+		errno = 0;
+		tmp_int = strtol (cell_text, NULL, 10);
+		if (errno || tmp_int < 0 || tmp_int > G_MAXUINT32)
+			value_valid = FALSE;
+		else
+			value_valid = TRUE;
+	} else {
+		struct in_addr tmp_addr;
+
+		if (inet_pton (AF_INET, cell_text, &tmp_addr) > 0)
+			value_valid = TRUE;
+	}
+
+	/* Change cell's background color while editing */
+	if (value_valid)
+		gdk_color_parse ("lightgreen", &color);
+	else
+		gdk_color_parse ("red", &color);
+
+	gtk_widget_modify_base (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color); /* works for GTK2 */
+	gtk_widget_modify_bg (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color);   /* works for GTK3 */
+
+	g_free (cell_text);
+	return FALSE;
+}
+
+static gboolean
 key_pressed_cb (GtkWidget *widget,
                 GdkEvent *event,
                 gpointer user_data)
@@ -450,6 +511,11 @@ ip4_cell_editing_started (GtkCellRenderer *cell,
 	                        (GCallback) delete_text_cb,
 	                        user_data);
 
+	/* Set up handler for value verifying and changing cell background */
+	g_signal_connect (G_OBJECT (editable), "changed",
+	                  (GCallback) cell_changed_cb,
+	                  cell);
+
 	/* Set up key pressed handler - need to handle Tab key */
 	g_signal_connect (G_OBJECT (editable), "key-press-event",
 	                  (GCallback) key_pressed_cb,
@@ -522,6 +588,11 @@ uint_cell_editing_started (GtkCellRenderer *cell,
 	                        (GCallback) delete_text_cb,
 	                        user_data);
 
+	/* Set up handler for value verifying and changing cell background */
+	g_signal_connect (G_OBJECT (editable), "changed",
+	                  (GCallback) cell_changed_cb,
+	                  cell);
+
 	/* Set up key pressed handler - need to handle Tab key */
 	g_signal_connect (G_OBJECT (editable), "key-press-event",
 	                  (GCallback) key_pressed_cb,
diff --git a/src/connection-editor/ip6-routes-dialog.c b/src/connection-editor/ip6-routes-dialog.c
index e96d89b..53da84c 100644
--- a/src/connection-editor/ip6-routes-dialog.c
+++ b/src/connection-editor/ip6-routes-dialog.c
@@ -365,6 +365,57 @@ delete_text_cb (GtkEditable *editable,
 }
 
 static gboolean
+cell_changed_cb (GtkEditable *editable,
+                 gpointer user_data)
+{
+	char *cell_text;
+	guint column;
+	GdkColor color;
+	gboolean value_valid = FALSE;
+
+	cell_text = gtk_editable_get_chars (editable, 0, -1);
+
+	column = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (user_data), "column"));
+
+	if (column == COL_PREFIX) {
+		long int tmp_int;
+
+		errno = 0;
+		tmp_int = strtol (cell_text, NULL, 10);
+		if (errno || tmp_int < 0 || tmp_int > 128)
+			value_valid = FALSE;
+		else
+			value_valid = TRUE;
+	} else if (column == COL_METRIC) {
+		long int tmp_int;
+
+		errno = 0;
+		tmp_int = strtol (cell_text, NULL, 10);
+		if (errno || tmp_int < 0 || tmp_int > G_MAXUINT32)
+			value_valid = FALSE;
+		else
+			value_valid = TRUE;
+	} else {
+		struct in6_addr tmp_addr;
+
+		if (inet_pton (AF_INET6, cell_text, &tmp_addr) > 0)
+			value_valid = TRUE;
+	}
+
+	/* Change cell's background color while editing */
+	if (value_valid)
+		gdk_color_parse ("lightgreen", &color);
+	else
+		gdk_color_parse ("red", &color);
+
+	gtk_widget_modify_base (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color); /* works for GTK2 */
+	gtk_widget_modify_bg (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color);   /* works for GTK3 */
+
+	g_free (cell_text);
+	return FALSE;
+}
+
+static gboolean
 key_pressed_cb (GtkWidget *widget,
                 GdkEvent *event,
                 gpointer user_data)
@@ -407,6 +458,11 @@ ip6_cell_editing_started (GtkCellRenderer *cell,
 	                        (GCallback) delete_text_cb,
 	                        user_data);
 
+	/* Set up handler for value verifying and changing cell background */
+	g_signal_connect (G_OBJECT (editable), "changed",
+	                  (GCallback) cell_changed_cb,
+	                  cell);
+
 	/* Set up key pressed handler - need to handle Tab key */
 	g_signal_connect (G_OBJECT (editable), "key-press-event",
 	                  (GCallback) key_pressed_cb,
@@ -479,6 +535,11 @@ uint_cell_editing_started (GtkCellRenderer *cell,
 	                        (GCallback) delete_text_cb,
 	                        user_data);
 
+	/* Set up handler for value verifying and changing cell background */
+	g_signal_connect (G_OBJECT (editable), "changed",
+	                  (GCallback) cell_changed_cb,
+	                  cell);
+
 	/* Set up key pressed handler - need to handle Tab key */
 	g_signal_connect (G_OBJECT (editable), "key-press-event",
 	                  (GCallback) key_pressed_cb,
diff --git a/src/connection-editor/page-ip4.c b/src/connection-editor/page-ip4.c
index 925c0ca..81a9d7b 100644
--- a/src/connection-editor/page-ip4.c
+++ b/src/connection-editor/page-ip4.c
@@ -654,6 +654,69 @@ delete_text_cb (GtkEditable *editable,
 }
 
 static gboolean
+parse_netmask (const char *str, guint32 *prefix)
+{
+	struct in_addr tmp_addr;
+	glong tmp_prefix;
+
+	errno = 0;
+
+	/* Is it a prefix? */
+	if (!strchr (str, '.')) {
+		tmp_prefix = strtol (str, NULL, 10);
+		if (!errno && tmp_prefix >= 0 && tmp_prefix <= 32) {
+			*prefix = tmp_prefix;
+			return TRUE;
+		}
+	}
+
+	/* Is it a netmask? */
+	if (inet_pton (AF_INET, str, &tmp_addr) > 0) {
+		*prefix = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr);
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
+static gboolean
+cell_changed_cb (GtkEditable *editable,
+                 gpointer user_data)
+{
+	char *cell_text;
+	guint column;
+	GdkColor color;
+	gboolean value_valid = FALSE;
+
+	cell_text = gtk_editable_get_chars (editable, 0, -1);
+
+	/* The COL_PREFIX can contain IP address or prefix */
+	column = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (user_data), "column"));
+	if (column == COL_PREFIX) {
+		guint32 tmp_prefix;
+
+		value_valid = parse_netmask (cell_text, &tmp_prefix);
+	} else {
+		struct in_addr tmp_addr;
+
+		if (inet_pton (AF_INET, cell_text, &tmp_addr) > 0)
+			value_valid = TRUE;
+	}
+
+	/* Change cell's background color while editing */
+	if (value_valid)
+		gdk_color_parse ("lightgreen", &color);
+	else
+		gdk_color_parse ("red", &color);
+
+	gtk_widget_modify_base (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color); /* works for GTK2 */
+	gtk_widget_modify_bg (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color);   /* works for GTK3 */
+
+	g_free (cell_text);
+	return FALSE;
+}
+
+static gboolean
 key_pressed_cb (GtkWidget *widget,
                 GdkEvent *event,
                 gpointer user_data)
@@ -699,6 +762,11 @@ cell_editing_started (GtkCellRenderer *cell,
 	                        (GCallback) delete_text_cb,
 	                        user_data);
 
+	/* Set up handler for IP verifying and changing cell background */
+	g_signal_connect (G_OBJECT (editable), "changed",
+	                  (GCallback) cell_changed_cb,
+	                  cell);
+
 	/* Set up key pressed handler - need to handle Tab key */
 	g_signal_connect (G_OBJECT (editable), "key-press-event",
 	                  (GCallback) key_pressed_cb,
@@ -940,32 +1008,6 @@ free_one_addr (gpointer data)
 }
 
 static gboolean
-parse_netmask (const char *str, guint32 *prefix)
-{
-	struct in_addr tmp_addr;
-	glong tmp_prefix;
-
-	errno = 0;
-
-	/* Is it a prefix? */
-	if (!strchr (str, '.')) {
-		tmp_prefix = strtol (str, NULL, 10);
-		if (!errno && tmp_prefix >= 0 && tmp_prefix <= 32) {
-			*prefix = tmp_prefix;
-			return TRUE;
-		}
-	}
-
-	/* Is it a netmask? */
-	if (inet_pton (AF_INET, str, &tmp_addr) > 0) {
-		*prefix = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr);
-		return TRUE;
-	}
-
-	return FALSE;
-}
-
-static gboolean
 ui_to_setting (CEPageIP4 *self)
 {
 	CEPageIP4Private *priv = CE_PAGE_IP4_GET_PRIVATE (self);
@@ -1022,7 +1064,7 @@ ui_to_setting (CEPageIP4 *self)
 		guint32 empty_val = 0, prefix;
 
 		gtk_tree_model_get (model, &tree_iter, COL_ADDRESS, &item, -1);
-		if (!item || !inet_aton (item, &tmp_addr)) {
+		if (!item || inet_pton (AF_INET, item, &tmp_addr) <= 0) {
 			g_warning ("%s: IPv4 address '%s' missing or invalid!",
 			           __func__, item ? item : "<none>");
 			g_free (item);
@@ -1047,7 +1089,7 @@ ui_to_setting (CEPageIP4 *self)
 
 		/* Gateway is optional... */
 		gtk_tree_model_get (model, &tree_iter, COL_GATEWAY, &item, -1);
-		if (item && !inet_aton (item, &tmp_gateway)) {
+		if (item && inet_pton (AF_INET, item, &tmp_gateway) <= 0) {
 			g_warning ("%s: IPv4 gateway '%s' invalid!",
 			           __func__, item ? item : "<none>");
 			g_free (item);
diff --git a/src/connection-editor/page-ip6.c b/src/connection-editor/page-ip6.c
index 7b2f01d..5cc01d5 100644
--- a/src/connection-editor/page-ip6.c
+++ b/src/connection-editor/page-ip6.c
@@ -634,6 +634,48 @@ delete_text_cb (GtkEditable *editable,
 }
 
 static gboolean
+cell_changed_cb (GtkEditable *editable,
+                 gpointer user_data)
+{
+	char *cell_text;
+	guint column;
+	GdkColor color;
+	gboolean value_valid = FALSE;
+
+	cell_text = gtk_editable_get_chars (editable, 0, -1);
+
+	/* The Prefix column is 0..128 */
+	column = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (user_data), "column"));
+	if (column == COL_PREFIX) {
+		guint32 prefix;
+		char *end;
+
+		prefix = strtoul (cell_text, &end, 10);
+		if (!end || *end || prefix == 0 || prefix > 128)
+			value_valid = FALSE;
+		else
+			value_valid = TRUE;
+	} else {
+		struct in6_addr tmp_addr;
+
+		if (inet_pton (AF_INET6, cell_text, &tmp_addr))
+			value_valid = TRUE;
+	}
+
+	/* Change cell's background color while editing */
+	if (value_valid)
+		gdk_color_parse ("lightgreen", &color);
+	else
+		gdk_color_parse ("red", &color);
+
+	gtk_widget_modify_base (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color); /* works for GTK2 */
+	gtk_widget_modify_bg (GTK_WIDGET (editable), GTK_STATE_NORMAL, &color);   /* works for GTK3 */
+
+	g_free (cell_text);
+	return FALSE;
+}
+
+static gboolean
 key_pressed_cb (GtkWidget *widget,
                 GdkEvent *event,
                 gpointer user_data)
@@ -684,6 +726,11 @@ cell_editing_started (GtkCellRenderer *cell,
 	                        (GCallback) delete_text_cb,
 	                        user_data);
 
+	/* Set up handler for value verifying and changing cell background */
+	g_signal_connect (G_OBJECT (editable), "changed",
+	                  (GCallback) cell_changed_cb,
+	                  cell);
+
 	/* Set up key pressed handler - need to handle Tab key */
 	g_signal_connect (G_OBJECT (editable), "key-press-event",
 	                  (GCallback) key_pressed_cb,



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