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



commit 4cad3c27ebd939ea3d7fd5782c19b921b50463a1
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 42376c1..53a3b7a 100644
--- a/src/connection-editor/ip4-routes-dialog.c
+++ b/src/connection-editor/ip4-routes-dialog.c
@@ -410,6 +410,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)
@@ -452,6 +513,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,
@@ -524,6 +590,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 a24de4d..2742b82 100644
--- a/src/connection-editor/ip6-routes-dialog.c
+++ b/src/connection-editor/ip6-routes-dialog.c
@@ -367,6 +367,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)
@@ -409,6 +460,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,
@@ -481,6 +537,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 1140093..469a59a 100644
--- a/src/connection-editor/page-ip4.c
+++ b/src/connection-editor/page-ip4.c
@@ -653,6 +653,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)
@@ -698,6 +761,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,
@@ -935,32 +1003,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);
@@ -1017,7 +1059,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);
@@ -1042,7 +1084,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 d153c62..64beaa1 100644
--- a/src/connection-editor/page-ip6.c
+++ b/src/connection-editor/page-ip6.c
@@ -633,6 +633,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)
@@ -683,6 +725,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]