summaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk_autocomplete.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-12-30 00:06:10 +0100
committerChristian Grothoff <christian@grothoff.org>2021-12-30 00:06:10 +0100
commitd77e7cab65c416f72ff1b52fa590b24ede512e75 (patch)
treef69e8c038d4723c60c348342b03e11ecb1611401 /src/anastasis/anastasis-gtk_autocomplete.c
parent987f3f2f2d4f7fd61c115323fb17e6889f1e1120 (diff)
downloadanastasis-gtk-d77e7cab65c416f72ff1b52fa590b24ede512e75.tar.gz
anastasis-gtk-d77e7cab65c416f72ff1b52fa590b24ede512e75.tar.bz2
anastasis-gtk-d77e7cab65c416f72ff1b52fa590b24ede512e75.zip
simplify pin entry by breaking up into groups and auto-completion (#7088)
Diffstat (limited to 'src/anastasis/anastasis-gtk_autocomplete.c')
-rw-r--r--src/anastasis/anastasis-gtk_autocomplete.c100
1 files changed, 93 insertions, 7 deletions
diff --git a/src/anastasis/anastasis-gtk_autocomplete.c b/src/anastasis/anastasis-gtk_autocomplete.c
index 1e3da34..c941d25 100644
--- a/src/anastasis/anastasis-gtk_autocomplete.c
+++ b/src/anastasis/anastasis-gtk_autocomplete.c
@@ -50,12 +50,41 @@ lookup_autocomplete (GtkEditable *editable)
}
json_array_foreach (ra, index, a)
{
- const char *widget_name = json_string_value (json_object_get (a,
- "widget"));
- if (editable ==
- GTK_EDITABLE (GCG_get_main_window_object (ai[i].widget_name)))
- return json_string_value (json_object_get (a,
- "autocomplete"));
+ const char *autocomplete = NULL;
+ const char *widget_name = NULL;
+ const char *type = NULL;
+ char *data_name;
+ bool match;
+ struct GNUNET_JSON_Specification spec[] = {
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("widget",
+ &widget_name)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("autocomplete",
+ &autocomplete)),
+ GNUNET_JSON_spec_string ("type",
+ &type),
+ GNUNET_JSON_spec_end ()
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_JSON_parse (a,
+ spec,
+ NULL, NULL))
+ {
+ GNUNET_break (0);
+ continue;
+ }
+ if ( (NULL == autocomplete) ||
+ (NULL == widget_name) )
+ continue;
+ data_name = AG_expand_name (widget_name,
+ type);
+ match = (editable ==
+ GTK_EDITABLE (GCG_get_main_window_object (data_name)));
+ GNUNET_free (data_name);
+ if (match)
+ return autocomplete;
}
GNUNET_break (0);
return NULL;
@@ -115,10 +144,10 @@ anastasis_gtk_autocomplete_insert_text (GtkEditable *editable,
void *user_data)
{
const char *ac;
- (void) user_data;
gint pos;
char *replacement;
+ (void) user_data;
ac = lookup_autocomplete (editable);
if (NULL == ac)
return;
@@ -141,3 +170,60 @@ anastasis_gtk_autocomplete_insert_text (GtkEditable *editable,
GNUNET_free (replacement);
}
}
+
+
+/**
+ * Function called from an auto-completed PIN widget upon text insertion.
+ *
+ * @param editable the widget
+ * @param new_text inserted text
+ * @param new_text_length number of bytes in @a new_text
+ * @param position insertion position
+ * @param user_data unused
+ */
+void
+anastasis_gtk_pin_autocomplete_insert_text (GtkEditable *editable,
+ const char *new_text,
+ int new_text_length,
+ gpointer position,
+ void *user_data)
+{
+ /* Use \? to break up trigraphs */
+ const char *acn = "????\?-??\?-???\?-??\?";
+ const char *aca = "A-????\?-??\?-???\?-??\?";
+ const char *ac;
+ gint pos;
+ char *replacement;
+ gchar *pfx;
+
+ (void) user_data;
+ pfx = gtk_editable_get_chars (editable,
+ 0,
+ 2);
+ if (0 == strncasecmp (pfx,
+ "A-",
+ 2))
+ ac = aca;
+ else
+ ac = acn;
+ g_free (pfx);
+
+ pos = gtk_editable_get_position (editable);
+ if (strlen (ac) <= pos + new_text_length)
+ return;
+ if (ac[pos+new_text_length] != '?')
+ {
+ g_signal_stop_emission_by_name (editable,
+ "insert-text");
+ GNUNET_asprintf (&replacement,
+ "%.*s%c",
+ new_text_length,
+ new_text,
+ ac[pos+new_text_length]);
+ gtk_editable_insert_text (editable,
+ replacement,
+ -1,
+ position);
+ GNUNET_free (replacement);
+ }
+}