common: Allow using multiple words to filter languages

All words must match.
This commit is contained in:
Rui Matos 2013-02-05 01:44:27 +01:00
parent 0b2b0284fe
commit acbe609c94

View file

@ -47,6 +47,7 @@ typedef struct {
gboolean adding_languages;
gboolean showing_extra;
gchar *language;
gchar **filter_words;
} CcLanguageChooserPrivate;
#define GET_PRIVATE(chooser) ((CcLanguageChooserPrivate *) g_object_get_data (G_OBJECT (chooser), "private"))
@ -224,6 +225,19 @@ add_all_languages (GtkDialog *chooser)
g_strfreev (locale_ids);
}
static gboolean
match_all (gchar **words,
const gchar *str)
{
gchar **w;
for (w = words; *w; ++w)
if (!strstr (str, *w))
return FALSE;
return TRUE;
}
static gboolean
language_visible (GtkWidget *child,
gpointer user_data)
@ -233,7 +247,6 @@ language_visible (GtkWidget *child,
gchar *locale_name = NULL;
gchar *locale_current_name = NULL;
gchar *locale_untranslated_name = NULL;
gchar *filter_contents = NULL;
gboolean is_extra;
gboolean visible;
@ -249,43 +262,53 @@ language_visible (GtkWidget *child,
if (!priv->showing_extra && is_extra)
return FALSE;
filter_contents =
cc_util_normalize_casefold_and_unaccent (gtk_entry_get_text (GTK_ENTRY (priv->filter_entry)));
if (!filter_contents)
if (!priv->filter_words)
return TRUE;
visible = FALSE;
locale_name =
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (child), "locale-name"));
if (strstr (locale_name, filter_contents)) {
visible = TRUE;
visible = match_all (priv->filter_words, locale_name);
if (visible)
goto out;
}
locale_current_name =
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (child), "locale-current-name"));
if (strstr (locale_current_name, filter_contents)) {
visible = TRUE;
visible = match_all (priv->filter_words, locale_current_name);
if (visible)
goto out;
}
locale_untranslated_name =
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (child), "locale-untranslated-name"));
if (strstr (locale_untranslated_name, filter_contents)) {
visible = TRUE;
goto out;
}
visible = match_all (priv->filter_words, locale_untranslated_name);
out:
g_free (filter_contents);
g_free (locale_untranslated_name);
g_free (locale_current_name);
g_free (locale_name);
return visible;
}
static void
filter_changed (GtkDialog *chooser)
{
CcLanguageChooserPrivate *priv = GET_PRIVATE (chooser);
gchar *filter_contents = NULL;
g_clear_pointer (&priv->filter_words, g_strfreev);
filter_contents =
cc_util_normalize_casefold_and_unaccent (gtk_entry_get_text (GTK_ENTRY (priv->filter_entry)));
if (!filter_contents) {
egg_list_box_refilter (EGG_LIST_BOX (priv->language_list));
return;
}
priv->filter_words = g_strsplit_set (g_strstrip (filter_contents), " ", 0);
g_free (filter_contents);
egg_list_box_refilter (EGG_LIST_BOX (priv->language_list));
}
static void
show_more (GtkDialog *chooser)
{
@ -370,6 +393,7 @@ cc_language_chooser_private_free (gpointer data)
{
CcLanguageChooserPrivate *priv = data;
g_strfreev (priv->filter_words);
g_free (priv->language);
g_free (priv);
}
@ -418,8 +442,7 @@ cc_language_chooser_new (GtkWidget *parent)
add_all_languages (GTK_DIALOG (chooser));
g_signal_connect_swapped (priv->filter_entry, "changed",
G_CALLBACK (egg_list_box_refilter),
priv->language_list);
G_CALLBACK (filter_changed), chooser);
g_signal_connect (priv->language_list, "child-activated",
G_CALLBACK (child_activated), chooser);