/dev/tomkiewicz/e2ee: eee5ad689415: Request API: fields sensitivity

Tomasz Wasilczyk twasilczyk at pidgin.im
Wed Oct 23 14:33:24 EDT 2013


Changeset: eee5ad689415ae4c2276c2a64a77f0407eb0805d
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2013-10-23 20:33 +0200
Branch:	 default
URL: https://hg.pidgin.im/dev/tomkiewicz/e2ee/rev/eee5ad689415

Description:

Request API: fields sensitivity

diffstat:

 libpurple/request.c |  111 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 libpurple/request.h |   13 ++++++
 pidgin/gtkrequest.c |   14 ++++++
 3 files changed, 137 insertions(+), 1 deletions(-)

diffs (truncated from 306 to 300 lines):

diff --git a/libpurple/request.c b/libpurple/request.c
--- a/libpurple/request.c
+++ b/libpurple/request.c
@@ -53,6 +53,8 @@ struct _PurpleRequestField
 
 	gboolean visible;
 	gboolean required;
+	gboolean sensitive;
+	PurpleRequestFieldSensitivityCb sensitivity_cb;
 
 	union
 	{
@@ -144,6 +146,8 @@ struct _PurpleRequestFields
 
 	GList *validated_fields;
 
+	GList *autosensitive_fields;
+
 	void *ui_data;
 };
 
@@ -179,6 +183,9 @@ struct _PurpleRequestCommonParameters
 	gpointer parent_from;
 };
 
+static void
+purple_request_fields_check_others_sensitivity(PurpleRequestField *field);
+
 PurpleRequestCommonParameters *
 purple_request_cpar_new(void)
 {
@@ -469,6 +476,7 @@ purple_request_fields_destroy(PurpleRequ
 	g_list_free(fields->groups);
 	g_list_free(fields->required_fields);
 	g_list_free(fields->validated_fields);
+	g_list_free(fields->autosensitive_fields);
 	g_hash_table_destroy(fields->fields);
 	g_free(fields);
 }
@@ -506,6 +514,10 @@ purple_request_fields_add_group(PurpleRe
 				g_list_append(fields->validated_fields, field);
 		}
 
+		if (field->sensitivity_cb != NULL) {
+			fields->autosensitive_fields =
+				g_list_append(fields->autosensitive_fields, field);
+		}
 	}
 }
 
@@ -542,6 +554,14 @@ purple_request_fields_get_validatable(co
 	return fields->validated_fields;
 }
 
+const GList *
+purple_request_fields_get_autosensitive(const PurpleRequestFields *fields)
+{
+	g_return_val_if_fail(fields != NULL, NULL);
+
+	return fields->autosensitive_fields;
+}
+
 gboolean
 purple_request_fields_is_field_required(const PurpleRequestFields *fields,
 									  const char *id)
@@ -610,6 +630,37 @@ purple_request_fields_all_valid(const Pu
 	return TRUE;
 }
 
+static void
+purple_request_fields_check_sensitivity(PurpleRequestFields *fields)
+{
+	GList *it;
+
+	g_return_if_fail(fields != NULL);
+
+	for (it = fields->autosensitive_fields; it; it = g_list_next(it)) {
+		PurpleRequestField *field = it->data;
+
+		if (field->sensitivity_cb == NULL) {
+			g_warn_if_reached();
+			continue;
+		}
+
+		purple_request_field_set_sensitive(field,
+			field->sensitivity_cb(field));
+	}
+}
+
+static void
+purple_request_fields_check_others_sensitivity(PurpleRequestField *field)
+{
+	g_return_if_fail(field != NULL);
+
+	if (field->group == NULL || field->group->fields_list == NULL)
+		return;
+
+	purple_request_fields_check_sensitivity(field->group->fields_list);
+}
+
 PurpleRequestField *
 purple_request_fields_get_field(const PurpleRequestFields *fields, const char *id)
 {
@@ -756,12 +807,18 @@ purple_request_field_group_add_field(Pur
 			group->fields_list->required_fields =
 				g_list_append(group->fields_list->required_fields, field);
 		}
-		
+
 		if (purple_request_field_is_validatable(field))
 		{
 			group->fields_list->validated_fields =
 				g_list_append(group->fields_list->validated_fields, field);
 		}
+
+		if (field->sensitivity_cb != NULL)
+		{
+			group->fields_list->autosensitive_fields =
+				g_list_append(group->fields_list->autosensitive_fields, field);
+		}
 	}
 
 	field->group = group;
@@ -808,6 +865,7 @@ purple_request_field_new(const char *id,
 
 	purple_request_field_set_label(field, text);
 	purple_request_field_set_visible(field, TRUE);
+	purple_request_field_set_sensitive(field, TRUE);
 
 	return field;
 }
@@ -1059,6 +1117,45 @@ purple_request_field_is_valid(PurpleRequ
 	return valid;
 }
 
+void
+purple_request_field_set_sensitive(PurpleRequestField *field,
+	gboolean sensitive)
+{
+	g_return_if_fail(field != NULL);
+
+	field->sensitive = sensitive;
+}
+
+gboolean
+purple_request_field_is_sensitive(PurpleRequestField *field)
+{
+	g_return_val_if_fail(field != NULL, FALSE);
+
+	return field->sensitive;
+}
+
+void
+purple_request_field_set_sensitivity_cb(PurpleRequestField *field,
+	PurpleRequestFieldSensitivityCb cb)
+{
+	PurpleRequestFields *flist;
+
+	g_return_if_fail(field != NULL);
+
+	field->sensitivity_cb = cb;
+
+	if (!field->group || !field->group->fields_list)
+		return;
+	flist = field->group->fields_list;
+	flist->autosensitive_fields = g_list_remove(flist->autosensitive_fields,
+		field);
+	if (cb != NULL)
+	{
+		flist->autosensitive_fields = g_list_append(
+			flist->autosensitive_fields, field);
+	}
+}
+
 PurpleRequestField *
 purple_request_field_string_new(const char *id, const char *text,
 							  const char *default_value, gboolean multiline)
@@ -1098,6 +1195,8 @@ purple_request_field_string_set_value(Pu
 
 	g_free(field->u.string.value);
 	field->u.string.value = g_strdup(value);
+
+	purple_request_fields_check_others_sensitivity(field);
 }
 
 void
@@ -1226,6 +1325,8 @@ purple_request_field_int_set_value(Purpl
 	}
 
 	field->u.integer.value = value;
+
+	purple_request_fields_check_others_sensitivity(field);
 }
 
 int
@@ -1298,6 +1399,8 @@ purple_request_field_bool_set_value(Purp
 	g_return_if_fail(field->type == PURPLE_REQUEST_FIELD_BOOLEAN);
 
 	field->u.boolean.value = value;
+
+	purple_request_fields_check_others_sensitivity(field);
 }
 
 gboolean
@@ -1366,6 +1469,8 @@ purple_request_field_choice_set_value(Pu
 	g_return_if_fail(field->type == PURPLE_REQUEST_FIELD_CHOICE);
 
 	field->u.choice.value = value;
+
+	purple_request_fields_check_others_sensitivity(field);
 }
 
 gpointer
@@ -1718,6 +1823,8 @@ purple_request_field_account_set_value(P
 	g_return_if_fail(field->type == PURPLE_REQUEST_FIELD_ACCOUNT);
 
 	field->u.account.account = value;
+
+	purple_request_fields_check_others_sensitivity(field);
 }
 
 void
@@ -2208,6 +2315,8 @@ purple_request_fields(void *handle, cons
 		purple_request_fields_strip_html(fields);
 	}
 
+	purple_request_fields_check_sensitivity(fields);
+
 	if (ops != NULL && ops->request_fields != NULL) {
 		PurpleRequestInfo *info;
 		gchar **tmp;
diff --git a/libpurple/request.h b/libpurple/request.h
--- a/libpurple/request.h
+++ b/libpurple/request.h
@@ -178,6 +178,8 @@ typedef void (*PurpleRequestInputCb)(voi
 typedef gboolean (*PurpleRequestFieldValidator)(PurpleRequestField *field,
 	gchar **errmsg, gpointer user_data);
 
+typedef gboolean (*PurpleRequestFieldSensitivityCb)(PurpleRequestField *field);
+
 /** 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
  *  of actions of the one chosen.
@@ -514,6 +516,9 @@ const GList *purple_request_fields_get_r
 const GList *purple_request_fields_get_validatable(
 	const PurpleRequestFields *fields);
 
+const GList *
+purple_request_fields_get_autosensitive(const PurpleRequestFields *fields);
+
 /**
  * Returns whether or not a field with the specified ID is required.
  *
@@ -884,6 +889,14 @@ gboolean purple_request_field_is_validat
  */
 gboolean purple_request_field_is_valid(PurpleRequestField *field, gchar **errmsg);
 
+void purple_request_field_set_sensitive(PurpleRequestField *field,
+	gboolean sensitive);
+
+gboolean purple_request_field_is_sensitive(PurpleRequestField *field);
+
+void purple_request_field_set_sensitivity_cb(PurpleRequestField *field,
+	PurpleRequestFieldSensitivityCb cb);
+
 /**
  * Returns the ui_data for a field.
  *
diff --git a/pidgin/gtkrequest.c b/pidgin/gtkrequest.c
--- a/pidgin/gtkrequest.c
+++ b/pidgin/gtkrequest.c
@@ -1073,6 +1073,7 @@ req_field_changed_cb(GtkWidget *widget, 
 	PurpleRequestFieldGroup *group;
 	PurpleRequestFields *fields;
 	PidginRequestData *req_data;
+	const GList *it;
 
 	group = purple_request_field_get_group(field);
 	fields = purple_request_field_group_get_fields_list(group);
@@ -1081,6 +1082,16 @@ req_field_changed_cb(GtkWidget *widget, 
 	gtk_widget_set_sensitive(req_data->ok_button,
 		purple_request_fields_all_required_filled(fields) &&
 		purple_request_fields_all_valid(fields));
+
+	it = purple_request_fields_get_autosensitive(fields);
+	for (; it != NULL; it = g_list_next(it)) {
+		PurpleRequestField *field = it->data;
+		GtkWidget *widget = purple_request_field_get_ui_data(field);
+
+		gtk_widget_set_sensitive(widget,
+			purple_request_field_is_sensitive(field));
+		/* TODO: string fields and set_editable */
+	}
 }
 
 static void
@@ -1876,6 +1887,9 @@ pidgin_request_fields(const char *title,
 						continue;
 				}
 



More information about the Commits mailing list