/pidgin/main: 06b19630bdb0: Validation for Request API fixes

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sat Aug 4 02:52:20 EDT 2012


Changeset: 06b19630bdb07d9457d2aa509cf5e13195a31829
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-08-04 08:52 +0200
Branch:	 default
URL: http://hg.pidgin.im/pidgin/main/rev/06b19630bdb0

Description:

Validation for Request API fixes

diffstat:

 libpurple/request.c |  37 +++++++++++++++++++++----------------
 libpurple/request.h |  19 ++++++++++++++-----
 pidgin/gtkdialogs.c |  11 +++++++++--
 3 files changed, 44 insertions(+), 23 deletions(-)

diffs (195 lines):

diff --git a/libpurple/request.c b/libpurple/request.c
--- a/libpurple/request.c
+++ b/libpurple/request.c
@@ -209,7 +209,7 @@ purple_request_fields_add_group(PurpleRe
 				g_list_append(fields->required_fields, field);
 		}
 
-		if (purple_request_field_is_validated(field)) {
+		if (purple_request_field_is_validatable(field)) {
 			fields->validated_fields =
 				g_list_append(fields->validated_fields, field);
 		}
@@ -234,7 +234,7 @@ purple_request_fields_exists(const Purpl
 	return (g_hash_table_lookup(fields->fields, id) != NULL);
 }
 
-GList *
+const GList *
 purple_request_fields_get_required(const PurpleRequestFields *fields)
 {
 	g_return_val_if_fail(fields != NULL, NULL);
@@ -242,8 +242,8 @@ purple_request_fields_get_required(const
 	return fields->required_fields;
 }
 
-GList *
-purple_request_fields_get_validated(const PurpleRequestFields *fields)
+const GList *
+purple_request_fields_get_validatable(const PurpleRequestFields *fields)
 {
 	g_return_val_if_fail(fields != NULL, NULL);
 
@@ -311,7 +311,7 @@ purple_request_fields_all_valid(const Pu
 	{
 		PurpleRequestField *field = (PurpleRequestField *)l->data;
 
-		if (!purple_request_field_is_valid(field))
+		if (!purple_request_field_is_valid(field, NULL))
 			return FALSE;
 	}
 
@@ -464,7 +464,7 @@ purple_request_field_group_add_field(Pur
 				g_list_append(group->fields_list->required_fields, field);
 		}
 		
-		if (purple_request_field_is_validated(field))
+		if (purple_request_field_is_validatable(field))
 		{
 			group->fields_list->validated_fields =
 				g_list_append(group->fields_list->validated_fields, field);
@@ -716,21 +716,19 @@ purple_request_field_set_validator(Purpl
 
 	if (field->group != NULL)
 	{
+		PurpleRequestFields *flist = field->group->fields_list;
+		flist->validated_fields = g_list_remove(flist->validated_fields,
+			field);
 		if (validator)
 		{
-			field->group->fields_list->validated_fields = g_list_append(
-				field->group->fields_list->validated_fields, field);
-		}
-		else
-		{
-			field->group->fields_list->validated_fields = g_list_remove(
-				field->group->fields_list->validated_fields, field);
+			flist->validated_fields = g_list_append(
+				flist->validated_fields, field);
 		}
 	}
 }
 
 gboolean
-purple_request_field_is_validated(PurpleRequestField *field)
+purple_request_field_is_validatable(PurpleRequestField *field)
 {
 	g_return_val_if_fail(field != NULL, FALSE);
 
@@ -738,8 +736,10 @@ purple_request_field_is_validated(Purple
 }
 
 gboolean
-purple_request_field_is_valid(PurpleRequestField *field)
+purple_request_field_is_valid(PurpleRequestField *field, gchar **errmsg)
 {
+	gboolean valid;
+
 	g_return_val_if_fail(field != NULL, FALSE);
 
 	if (!field->validator)
@@ -749,7 +749,12 @@ purple_request_field_is_valid(PurpleRequ
 		!purple_request_field_is_filled(field))
 		return TRUE;
 
-	return field->validator(field, field->validator_data);
+	valid = field->validator(field, errmsg, field->validator_data);
+
+	if (valid && errmsg)
+		*errmsg = NULL;
+
+	return valid;
 }
 
 PurpleRequestField *
diff --git a/libpurple/request.h b/libpurple/request.h
--- a/libpurple/request.h
+++ b/libpurple/request.h
@@ -150,7 +150,8 @@ typedef struct
 
 typedef void (*PurpleRequestInputCb)(void *, const char *);
 
-typedef gboolean (*PurpleRequestFieldValidator)(PurpleRequestField *field, void *user_data);
+typedef gboolean (*PurpleRequestFieldValidator)(PurpleRequestField *field,
+	gchar **errmsg, void *user_data);
 
 /** The type of callbacks passed to purple_request_action().  The first
  *  argument is the @a user_data parameter; the second is the index in the list
@@ -218,7 +219,8 @@ gboolean purple_request_fields_exists(co
  *
  * @constreturn The list of required fields.
  */
-GList *purple_request_fields_get_required(const PurpleRequestFields *fields);
+const GList *purple_request_fields_get_required(
+	const PurpleRequestFields *fields);
 
 /**
  * Returns a list of all validated fields.
@@ -227,7 +229,8 @@ GList *purple_request_fields_get_require
  *
  * @constreturn The list of validated fields.
  */
-GList *purple_request_fields_get_validated(const PurpleRequestFields *fields);
+const GList *purple_request_fields_get_validatable(
+	const PurpleRequestFields *fields);
 
 /**
  * Returns whether or not a field with the specified ID is required.
@@ -579,18 +582,24 @@ void purple_request_field_set_validator(
  *
  * @return TRUE if the field has validator, or FALSE.
  */
-gboolean purple_request_field_is_validated(PurpleRequestField *field);
+gboolean purple_request_field_is_validatable(PurpleRequestField *field);
 
 /**
  * Checks, if specified field is valid.
  *
+ * If detailed message about failure reason is needed, there is an option to
+ * return (via errmsg argument) pointer to newly allocated error message.
+ * It must be freed with g_free after use.
+ *
  * Note: empty, not required fields are valid.
  *
  * @param field The field.
+ * @param errmsg If non-NULL, the memory area, where the pointer to validation
+ *        failure message will be set.
  *
  * @return TRUE, if the field is valid, FALSE otherwise.
  */
-gboolean purple_request_field_is_valid(PurpleRequestField *field);
+gboolean purple_request_field_is_valid(PurpleRequestField *field, gchar **errmsg);
 
 /**
  * Returns the ui_data for a field.
diff --git a/pidgin/gtkdialogs.c b/pidgin/gtkdialogs.c
--- a/pidgin/gtkdialogs.c
+++ b/pidgin/gtkdialogs.c
@@ -880,18 +880,25 @@ pidgin_dialogs_im_cb(gpointer data, Purp
 }
 
 static gboolean
-pidgin_dialogs_im_name_validator(PurpleRequestField *field, void *_fields)
+pidgin_dialogs_im_name_validator(PurpleRequestField *field, gchar **errmsg,
+	void *_fields)
 {
 	PurpleRequestFields *fields = _fields;
 	PurpleAccount *account;
 	PurplePlugin *prpl;
 	const char *username;
+	gboolean valid;
 
 	account = purple_request_fields_get_account(fields, "account");
 	prpl = purple_find_prpl(purple_account_get_protocol_id(account));
 	username = purple_request_fields_get_string(fields, "screenname");
 
-	return purple_validate(prpl, username);
+	valid = purple_validate(prpl, username);
+
+	if (errmsg && !valid)
+		*errmsg = g_strdup(_("Invalid username"));
+
+	return valid;
 }
 
 void



More information about the Commits mailing list