/pidgin/main: e740bbb8350c: Request API: fix integer fields, add...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sun May 12 06:46:02 EDT 2013


Changeset: e740bbb8350c2be2e23e950cce0b8f4f316c3b84
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-05-12 12:45 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/e740bbb8350c

Description:

Request API: fix integer fields, add bounds to them

diffstat:

 finch/gntblist.c                         |    2 +-
 finch/gntplugin.c                        |    2 +-
 finch/gntprefs.c                         |    2 +-
 libpurple/plugins/idle.c                 |    4 +-
 libpurple/plugins/perl/common/Request.xs |    6 +-
 libpurple/request.c                      |  104 ++++++++++++++----------------
 libpurple/request.h                      |   54 +++++++++++-----
 pidgin/gtkblist.c                        |    2 +-
 pidgin/gtkrequest.c                      |   33 ++------
 9 files changed, 106 insertions(+), 103 deletions(-)

diffs (truncated from 395 to 300 lines):

diff --git a/finch/gntblist.c b/finch/gntblist.c
--- a/finch/gntblist.c
+++ b/finch/gntblist.c
@@ -1149,7 +1149,7 @@ chat_components_edit(PurpleBlistNode *se
 			const char *str = g_hash_table_lookup(purple_chat_get_components(chat), pce->identifier);
 			if (!str || sscanf(str, "%d", &val) != 1)
 				val = pce->min;
-			field = purple_request_field_int_new(pce->identifier, pce->label, val);
+			field = purple_request_field_int_new(pce->identifier, pce->label, val, INT_MIN, INT_MAX);
 		} else {
 			field = purple_request_field_string_new(pce->identifier, pce->label,
 					g_hash_table_lookup(purple_chat_get_components(chat), pce->identifier), FALSE);
diff --git a/finch/gntplugin.c b/finch/gntplugin.c
--- a/finch/gntplugin.c
+++ b/finch/gntplugin.c
@@ -510,7 +510,7 @@ process_pref_frame(PurplePluginPrefFrame
 					field = purple_request_field_bool_new(name, label, purple_prefs_get_bool(name));
 					break;
 				case PURPLE_PREF_INT:
-					field = purple_request_field_int_new(name, label, purple_prefs_get_int(name));
+					field = purple_request_field_int_new(name, label, purple_prefs_get_int(name), INT_MIN, INT_MAX);
 					break;
 				case PURPLE_PREF_STRING:
 					field = purple_request_field_string_new(name, label, purple_prefs_get_string(name),
diff --git a/finch/gntprefs.c b/finch/gntprefs.c
--- a/finch/gntprefs.c
+++ b/finch/gntprefs.c
@@ -123,7 +123,7 @@ get_pref_field(Prefs *prefs)
 				break;
 			case PURPLE_PREF_INT:
 				field = purple_request_field_int_new(prefs->pref, _(prefs->label),
-						purple_prefs_get_int(prefs->pref));
+						purple_prefs_get_int(prefs->pref), INT_MIN, INT_MAX);
 				break;
 			case PURPLE_PREF_STRING:
 				field = purple_request_field_string_new(prefs->pref, _(prefs->label),
diff --git a/libpurple/plugins/idle.c b/libpurple/plugins/idle.c
--- a/libpurple/plugins/idle.c
+++ b/libpurple/plugins/idle.c
@@ -151,7 +151,7 @@ idle_action(PurplePluginAction *action)
 	purple_request_field_account_set_show_all(field, FALSE);
 	purple_request_field_group_add_field(group, field);
 
-	field = purple_request_field_int_new("mins", _("Minutes"), 10);
+	field = purple_request_field_int_new("mins", _("Minutes"), 10, 0, 9999);
 	purple_request_field_group_add_field(group, field);
 
 	request = purple_request_fields_new();
@@ -211,7 +211,7 @@ idle_all_action(PurplePluginAction *acti
 
 	group = purple_request_field_group_new(NULL);
 
-	field = purple_request_field_int_new("mins", _("Minutes"), 10);
+	field = purple_request_field_int_new("mins", _("Minutes"), 10, 0, 9999);
 	purple_request_field_group_add_field(group, field);
 
 	request = purple_request_fields_new();
diff --git a/libpurple/plugins/perl/common/Request.xs b/libpurple/plugins/perl/common/Request.xs
--- a/libpurple/plugins/perl/common/Request.xs
+++ b/libpurple/plugins/perl/common/Request.xs
@@ -327,11 +327,13 @@ MODULE = Purple::Request  PACKAGE = Purp
 PROTOTYPES: ENABLE
 
 Purple::Request::Field
-purple_request_field_int_new(clas, id, text, default_value = 0)
+purple_request_field_int_new(clas, id, text, default_value = 0, lower_bound = INT_MIN, upper_bound = INT_MAX)
 	const char *id
 	const char *text
 	int default_value
-	C_ARGS: id, text, default_value
+	int lower_bound
+	int upper_bound
+	C_ARGS: id, text, default_value, lower_bound, upper_bound
 
 int
 purple_request_field_int_get_default_value(field)
diff --git a/libpurple/request.c b/libpurple/request.c
--- a/libpurple/request.c
+++ b/libpurple/request.c
@@ -70,7 +70,8 @@ struct _PurpleRequestField
 		{
 			int default_value;
 			int value;
-
+			int lower_bound;
+			int upper_bound;
 		} integer;
 
 		struct
@@ -864,7 +865,7 @@ purple_request_field_string_is_editable(
 
 PurpleRequestField *
 purple_request_field_int_new(const char *id, const char *text,
-						   int default_value)
+	int default_value, int lower_bound, int upper_bound)
 {
 	PurpleRequestField *field;
 
@@ -873,6 +874,8 @@ purple_request_field_int_new(const char 
 
 	field = purple_request_field_new(id, text, PURPLE_REQUEST_FIELD_INTEGER);
 
+	purple_request_field_int_set_lower_bound(field, lower_bound);
+	purple_request_field_int_set_upper_bound(field, upper_bound);
 	purple_request_field_int_set_default_value(field, default_value);
 	purple_request_field_int_set_value(field, default_value);
 
@@ -890,11 +893,37 @@ purple_request_field_int_set_default_val
 }
 
 void
+purple_request_field_int_set_lower_bound(PurpleRequestField *field,
+	int lower_bound)
+{
+	g_return_if_fail(field != NULL);
+	g_return_if_fail(field->type == PURPLE_REQUEST_FIELD_INTEGER);
+
+	field->u.integer.lower_bound = lower_bound;
+}
+
+void
+purple_request_field_int_set_upper_bound(PurpleRequestField *field,
+	int upper_bound)
+{
+	g_return_if_fail(field != NULL);
+	g_return_if_fail(field->type == PURPLE_REQUEST_FIELD_INTEGER);
+
+	field->u.integer.upper_bound = upper_bound;
+}
+
+void
 purple_request_field_int_set_value(PurpleRequestField *field, int value)
 {
 	g_return_if_fail(field != NULL);
 	g_return_if_fail(field->type == PURPLE_REQUEST_FIELD_INTEGER);
 
+	if (field->u.integer.value < field->u.integer.lower_bound ||
+		field->u.integer.value > field->u.integer.upper_bound) {
+		purple_debug_error("request", "Int value out of bounds\n");
+		return;
+	}
+
 	field->u.integer.value = value;
 }
 
@@ -908,6 +937,24 @@ purple_request_field_int_get_default_val
 }
 
 int
+purple_request_field_int_get_lower_bound(const PurpleRequestField *field)
+{
+	g_return_val_if_fail(field != NULL, 0);
+	g_return_val_if_fail(field->type == PURPLE_REQUEST_FIELD_INTEGER, 0);
+
+	return field->u.integer.lower_bound;
+}
+
+int
+purple_request_field_int_get_upper_bound(const PurpleRequestField *field)
+{
+	g_return_val_if_fail(field != NULL, 0);
+	g_return_val_if_fail(field->type == PURPLE_REQUEST_FIELD_INTEGER, 0);
+
+	return field->u.integer.upper_bound;
+}
+
+int
 purple_request_field_int_get_value(const PurpleRequestField *field)
 {
 	g_return_val_if_fail(field != NULL, 0);
@@ -1522,59 +1569,6 @@ purple_request_field_alphanumeric_valida
 	return FALSE;
 }
 
-gboolean purple_request_field_numeric_validator(PurpleRequestField *field,
-	gchar **errmsg, void *range_p)
-{
-	gboolean succ = TRUE;
-	int value = 0;
-	int *range = range_p;
-	
-	g_return_val_if_fail(field != NULL, FALSE);
-	g_return_val_if_fail(field->type == PURPLE_REQUEST_FIELD_STRING ||
-		field->type == PURPLE_REQUEST_FIELD_INTEGER, FALSE);
-
-	if (field->type == PURPLE_REQUEST_FIELD_STRING)
-	{
-		const gchar *svalue, *it;
-		svalue = purple_request_field_string_get_value(field);
-		if (svalue == NULL || svalue[0] == '\0')
-			succ = FALSE;
-		it = svalue;
-		if (it[0] == '-')
-			it++;
-		while (succ && *it)
-		{
-			if (!g_ascii_isdigit(*it))
-				succ = FALSE;
-			it++;
-		}
-		if (succ)
-		{
-			char *endptr;
-			value = strtol(svalue, &endptr, 10);
-			succ = (errno != ERANGE && endptr[0] == '\0');
-		}
-	}
-	// TODO: integer fields doesn't seems to work, so this one needs testing
-	else if (field->type == PURPLE_REQUEST_FIELD_INTEGER)
-		value = purple_request_field_int_get_value(field);
-	else
-		g_return_val_if_reached(FALSE);
-
-	if (succ && range)
-		succ = (value >= range[0] && value <= range[1]);
-
-	if (succ)
-		return TRUE;
-
-	if (errmsg && !range)
-		*errmsg = g_strdup(_("Invalid number"));
-	if (errmsg && range)
-		*errmsg = g_strdup_printf(_("Value is not between %d and %d"),
-			range[0], range[1]);
-	return FALSE;
-}
-
 /* -- */
 
 void *
diff --git a/libpurple/request.h b/libpurple/request.h
--- a/libpurple/request.h
+++ b/libpurple/request.h
@@ -739,12 +739,13 @@ gboolean purple_request_field_string_is_
  * @param id            The field ID.
  * @param text          The text label of the field.
  * @param default_value The default value.
+ * @param lower_bound   The lower bound.
+ * @param upper_bound   The upper bound.
  *
  * @return The new field.
  */
 PurpleRequestField *purple_request_field_int_new(const char *id,
-											 const char *text,
-											 int default_value);
+	const char *text, int default_value, int lower_bound, int upper_bound);
 
 /**
  * Sets the default value in an integer field.
@@ -756,6 +757,22 @@ void purple_request_field_int_set_defaul
 											  int default_value);
 
 /**
+ * Sets the lower bound in an integer field.
+ *
+ * @param field       The field.
+ * @param lower_bound The lower bound.
+ */
+void purple_request_field_int_set_lower_bound(PurpleRequestField *field, int lower_bound);
+
+/**
+ * Sets the upper bound in an integer field.
+ *
+ * @param field       The field.
+ * @param upper_bound The upper bound.
+ */
+void purple_request_field_int_set_upper_bound(PurpleRequestField *field, int lower_bound);
+
+/**
  * Sets the value in an integer field.
  *
  * @param field The field.
@@ -773,6 +790,24 @@ void purple_request_field_int_set_value(
 int purple_request_field_int_get_default_value(const PurpleRequestField *field);
 
 /**
+ * Returns the lower bound in an integer field.
+ *
+ * @param field The field.
+ *
+ * @return The lower bound.
+ */
+int purple_request_field_int_get_lower_bound(const PurpleRequestField *field);
+
+/**
+ * Returns the upper bound in an integer field.
+ *
+ * @param field The field.
+ *
+ * @return The upper bound.
+ */
+int purple_request_field_int_get_upper_bound(const PurpleRequestField *field);
+
+/**
  * Returns the user-entered value in an integer field.
  *
  * @param field The field.
@@ -1301,21 +1336,6 @@ gboolean purple_request_field_email_vali
 gboolean purple_request_field_alphanumeric_validator(PurpleRequestField *field,
 	gchar **errmsg, void *allowed_characters);



More information about the Commits mailing list