pidgin/gtkprefs.c

changeset 27501
a9b9d025395b
parent 27493
96dc1b8abdf9
child 27505
485b02eec2be
--- a/pidgin/gtkprefs.c	Fri Jul 03 02:21:42 2009 +0000
+++ b/pidgin/gtkprefs.c	Fri Jul 03 03:30:08 2009 +0000
@@ -1636,15 +1636,82 @@
 	return ret;
 }
 
+/* This isn't a very strict check, but should give the user a clue. */
+static gboolean
+verify_ip_address(const gchar *text)
+{
+	char *tmp;
+	long octet;
+
+	if (!text && !isdigit(*text))
+		return FALSE;
+
+	tmp = NULL;
+	octet = strtol(text, &tmp, 10);
+	if (octet < 0 || octet > 255)
+		return FALSE;
+
+	if (!tmp || *tmp != '.')
+		return FALSE;
+
+	text = tmp + 1;
+	if (!isdigit(*text))
+		return FALSE;
+
+	tmp = NULL;
+	octet = strtol(text, &tmp, 10);
+	if (octet < 0 || octet > 255)
+		return FALSE;
+
+	if (!tmp || *tmp != '.')
+		return FALSE;
+
+	text = tmp + 1;
+	if (!isdigit(*text))
+		return FALSE;
+
+	tmp = NULL;
+	octet = strtol(text, &tmp, 10);
+	if (octet < 0 || octet > 255)
+		return FALSE;
+
+	text = tmp + 1;
+	if (!isdigit(*text))
+		return FALSE;
+
+	tmp = NULL;
+	octet = strtol(text, &tmp, 10);
+	if (octet < 0 || octet > 255)
+		return FALSE;
+
+	if (!tmp || *tmp != '\0')
+		return FALSE;
+
+	return TRUE;
+}
+
 static void
 network_ip_changed(GtkEntry *entry, gpointer data)
 {
-	/*
-	 * TODO: It would be nice if we could validate this and show a
-	 *       red background in the box when the IP address is invalid
-	 *       and a green background when the IP address is valid.
-	 */
-	purple_network_set_public_ip(gtk_entry_get_text(entry));
+	const gchar *text = gtk_entry_get_text(entry);
+	GdkColor color;
+
+	if (verify_ip_address(text))
+	{
+		color.red = 0xAFFF;
+		color.green = 0xFFFF;
+		color.blue = 0xAFFF;
+
+		purple_network_set_public_ip(text);
+	}
+	else
+	{
+		color.red = 0xFFFF;
+		color.green = 0xAFFF;
+		color.blue = 0xAFFF;
+	}
+
+	gtk_widget_modify_base(GTK_WIDGET(entry), GTK_STATE_NORMAL, &color);
 }
 
 static gboolean

mercurial