/pidgin/main: 2574432da068: Validation for Request API: common v...
Tomasz Wasilczyk
tomkiewicz at cpw.pidgin.im
Mon Aug 6 09:25:09 EDT 2012
Changeset: 2574432da0686ff333f100286bb4824f960ed1ff
Author: Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date: 2012-08-06 15:24 +0200
Branch: default
URL: http://hg.pidgin.im/pidgin/main/rev/2574432da068
Description:
Validation for Request API: common validators
diffstat:
libpurple/request.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++
libpurple/request.h | 50 ++++++++++++++++++++++
2 files changed, 166 insertions(+), 0 deletions(-)
diffs (186 lines):
diff --git a/libpurple/request.c b/libpurple/request.c
--- a/libpurple/request.c
+++ b/libpurple/request.c
@@ -1461,6 +1461,122 @@ purple_request_field_certificate_get_val
/* -- */
+gboolean
+purple_request_field_email_validator(PurpleRequestField *field, gchar **errmsg,
+ void *user_data)
+{
+ const char *value;
+
+ g_return_val_if_fail(field != NULL, FALSE);
+ g_return_val_if_fail(field->type == PURPLE_REQUEST_FIELD_STRING, FALSE);
+
+ value = purple_request_field_string_get_value(field);
+
+ if (value != NULL && purple_email_is_valid(value))
+ return TRUE;
+
+ if (errmsg)
+ *errmsg = g_strdup(_("Invalid email address"));
+ return FALSE;
+}
+
+gboolean
+purple_request_field_alphanumeric_validator(PurpleRequestField *field,
+ gchar **errmsg, void *allowed_characters)
+{
+ const char *value;
+ gchar invalid_char = '\0';
+
+ g_return_val_if_fail(field != NULL, FALSE);
+ g_return_val_if_fail(field->type == PURPLE_REQUEST_FIELD_STRING, FALSE);
+
+ value = purple_request_field_string_get_value(field);
+
+ g_return_val_if_fail(value != NULL, FALSE);
+
+ if (allowed_characters)
+ {
+ gchar *value_r = g_strdup(value);
+ g_strcanon(value_r, allowed_characters, '\0');
+ invalid_char = value[strlen(value_r)];
+ g_free(value_r);
+ }
+ else
+ {
+ while (value)
+ {
+ if (!g_ascii_isalnum(*value))
+ {
+ invalid_char = *value;
+ break;
+ }
+ value++;
+ }
+ }
+ if (!invalid_char)
+ return TRUE;
+
+ if (errmsg)
+ *errmsg = g_strdup_printf(_("Invalid character '%c'"),
+ invalid_char);
+ 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 *
purple_request_input(void *handle, const char *title, const char *primary,
const char *secondary, const char *default_value,
diff --git a/libpurple/request.h b/libpurple/request.h
--- a/libpurple/request.h
+++ b/libpurple/request.h
@@ -1269,6 +1269,56 @@ PurpleCertificate *purple_request_field_
/*@}*/
/**************************************************************************/
+/** @name Validators for request fields. */
+/**************************************************************************/
+/*@{*/
+
+/**
+ * Validates a field which should contain an email address.
+ *
+ * @see purple_request_field_set_validator
+ *
+ * @param field The field.
+ * @param errmsg (Optional) destination for error message.
+ * @param user_data Ignored.
+ *
+ * @return TRUE, if field contains valid email address.
+ */
+gboolean purple_request_field_email_validator(PurpleRequestField *field,
+ gchar **errmsg, void *user_data);
+
+/**
+ * Validates a field which should contain alphanumeric content.
+ *
+ * @see purple_request_field_set_validator
+ *
+ * @param field The field.
+ * @param errmsg (Optional) destination for error message.
+ * @param user_data (Optional) allowed character list (NULL-terminated string).
+ *
+ * @return TRUE, if field contains only alphanumeric characters.
+ */
+gboolean purple_request_field_alphanumeric_validator(PurpleRequestField *field,
+ gchar **errmsg, void *allowed_characters);
+
+/**
+ * Validates a field which should contain numeric content, within (optional)
+ * range.
+ *
+ * @see purple_request_field_set_validator
+ *
+ * @param field The field.
+ * @param errmsg (Optional) destination for error message.
+ * @param user_data (Optional) an int[2] array containing specified range.
+ *
+ * @return TRUE, if field contains only alphanumeric characters.
+ */
+gboolean purple_request_field_numeric_validator(PurpleRequestField *field,
+ gchar **errmsg, void *range);
+
+/*@}*/
+
+/**************************************************************************/
/** @name Request API */
/**************************************************************************/
/*@{*/
More information about the Commits
mailing list