From a8b0125da1ba47742f21f734070d2d1cfc9cf46d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 3 Jul 2020 23:58:07 -0400 Subject: [PATCH] filefilter: Drop GtkFileFilterInfo We can just make the filter operate on GFileInfo. This is in preparation for making GtkFileFilter a GtkFilter. Update all users. --- docs/reference/gtk/gtk4-sections.txt | 1 - gtk/gtkfilechooserentry.c | 57 ++---------- gtk/gtkfilefilter.c | 131 ++++++++++++++------------- gtk/gtkfilefilter.h | 33 +------ gtk/gtkfilesystemmodel.c | 59 +----------- tests/testfilechooser.c | 12 ++- testsuite/gtk/builder.c | 10 -- 7 files changed, 91 insertions(+), 212 deletions(-) diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt index c6da63ac74..cb914a7703 100644 --- a/docs/reference/gtk/gtk4-sections.txt +++ b/docs/reference/gtk/gtk4-sections.txt @@ -1413,7 +1413,6 @@ GtkFileChooserButtonPrivate
gtkfilefilter GtkFileFilter -GtkFileFilterInfo GtkFileFilterFlags GtkFileFilterFunc gtk_file_filter_new diff --git a/gtk/gtkfilechooserentry.c b/gtk/gtkfilechooserentry.c index 60d6e3bff4..f4871344d3 100644 --- a/gtk/gtkfilechooserentry.c +++ b/gtk/gtkfilechooserentry.c @@ -194,65 +194,22 @@ match_func (GtkEntryCompletion *compl, * current file filter (e.g. just jpg files) here. */ if (chooser_entry->current_filter != NULL) { - char *mime_type = NULL; - gboolean matches; GFile *file; - GFileInfo *file_info; - GtkFileFilterInfo filter_info; - GtkFileFilterFlags needed_flags; + GFileInfo *info; file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store), iter); - file_info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store), - iter); + info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store), + iter); /* We always allow navigating into subfolders, so don't ever filter directories */ - if (g_file_info_get_file_type (file_info) != G_FILE_TYPE_REGULAR) + if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR) return TRUE; - needed_flags = gtk_file_filter_get_needed (chooser_entry->current_filter); + if (!g_file_info_has_attribute (info, "standard::file")) + g_file_info_set_attribute_object (info, "standard::file", G_OBJECT (file)); - filter_info.display_name = g_file_info_get_display_name (file_info); - filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME; - - if (needed_flags & GTK_FILE_FILTER_MIME_TYPE) - { - const char *s = g_file_info_get_content_type (file_info); - if (s != NULL) - { - mime_type = g_content_type_get_mime_type (s); - if (mime_type != NULL) - { - filter_info.mime_type = mime_type; - filter_info.contains |= GTK_FILE_FILTER_MIME_TYPE; - } - } - } - - if (needed_flags & GTK_FILE_FILTER_FILENAME) - { - const char *path = g_file_get_path (file); - if (path != NULL) - { - filter_info.filename = path; - filter_info.contains |= GTK_FILE_FILTER_FILENAME; - } - } - - if (needed_flags & GTK_FILE_FILTER_URI) - { - const char *uri = g_file_get_uri (file); - if (uri) - { - filter_info.uri = uri; - filter_info.contains |= GTK_FILE_FILTER_URI; - } - } - - matches = gtk_file_filter_filter (chooser_entry->current_filter, &filter_info); - - g_free (mime_type); - return matches; + return gtk_file_filter_filter (chooser_entry->current_filter, info); } return TRUE; diff --git a/gtk/gtkfilefilter.c b/gtk/gtkfilefilter.c index 204fc6f5bc..e0f112be39 100644 --- a/gtk/gtkfilefilter.c +++ b/gtk/gtkfilefilter.c @@ -73,6 +73,7 @@ #include "gtkbuilderprivate.h" #include "gtkintl.h" #include "gtkprivate.h" +#include "gtkfilter.h" typedef struct _GtkFileFilterClass GtkFileFilterClass; typedef struct _FilterRule FilterRule; @@ -107,7 +108,7 @@ struct _FilterRule { FilterRuleType type; GtkFileFilterFlags needed; - + union { gchar *pattern; gchar *mime_type; @@ -156,6 +157,7 @@ static void gtk_file_filter_buildable_custom_tag_end (GtkBuildable const gchar *tagname, gpointer data); + G_DEFINE_TYPE_WITH_CODE (GtkFileFilter, gtk_file_filter, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_file_filter_buildable_init)) @@ -436,7 +438,6 @@ gtk_file_filter_buildable_custom_tag_end (GtkBuildable *buildable, } } - /** * gtk_file_filter_new: * @@ -768,22 +769,19 @@ _gtk_file_filter_get_as_patterns (GtkFileFilter *filter) /** * gtk_file_filter_filter: * @filter: a #GtkFileFilter - * @filter_info: a #GtkFileFilterInfo containing information - * about a file. - * + * @info: the #GFileInfo to filter + * * Tests whether a file should be displayed according to @filter. - * The #GtkFileFilterInfo @filter_info should include - * the fields returned from gtk_file_filter_get_needed(). * * This function will not typically be used by applications; it * is intended principally for use in the implementation of * #GtkFileChooser. - * + * * Returns: %TRUE if the file should be displayed **/ gboolean -gtk_file_filter_filter (GtkFileFilter *filter, - const GtkFileFilterInfo *filter_info) +gtk_file_filter_filter (GtkFileFilter *filter, + GFileInfo *info) { GSList *tmp_list; @@ -791,69 +789,78 @@ gtk_file_filter_filter (GtkFileFilter *filter, { FilterRule *rule = tmp_list->data; - if ((filter_info->contains & rule->needed) != rule->needed) - continue; - switch (rule->type) - { - case FILTER_RULE_MIME_TYPE: - if (filter_info->mime_type != NULL) - { - gchar *filter_content_type, *rule_content_type; - gboolean match; + { + case FILTER_RULE_MIME_TYPE: + { + const char *filter_content_type; + char *rule_content_type; + gboolean match; - filter_content_type = g_content_type_from_mime_type (filter_info->mime_type); - rule_content_type = g_content_type_from_mime_type (rule->u.mime_type); - match = filter_content_type != NULL && - rule_content_type != NULL && - g_content_type_is_a (filter_content_type, rule_content_type); - g_free (filter_content_type); - g_free (rule_content_type); + filter_content_type = g_file_info_get_content_type (info); + if (filter_content_type) + { + rule_content_type = g_content_type_from_mime_type (rule->u.mime_type); + match = g_content_type_is_a (filter_content_type, rule_content_type); + g_free (rule_content_type); - if (match) - return TRUE; - } - break; - case FILTER_RULE_PATTERN: - if (filter_info->display_name != NULL && - _gtk_fnmatch (rule->u.pattern, filter_info->display_name, FALSE)) - return TRUE; - break; - case FILTER_RULE_PIXBUF_FORMATS: - { - GSList *list; + if (match) + return TRUE; + } + } + break; - if (!filter_info->mime_type) - break; + case FILTER_RULE_PATTERN: + { + const char *display_name; - for (list = rule->u.pixbuf_formats; list; list = list->next) - { - int i; - gchar **mime_types; + display_name = g_file_info_get_display_name (info); + if (display_name) + { + if (_gtk_fnmatch (rule->u.pattern, display_name, FALSE)) + return TRUE; + } + } + break; - mime_types = gdk_pixbuf_format_get_mime_types (list->data); + case FILTER_RULE_PIXBUF_FORMATS: + { + const char *filter_content_type; - for (i = 0; mime_types[i] != NULL; i++) - { - if (strcmp (mime_types[i], filter_info->mime_type) == 0) - { - g_strfreev (mime_types); - return TRUE; - } - } + filter_content_type = g_file_info_get_content_type (info); + if (filter_content_type) + { + GSList *list; - g_strfreev (mime_types); - } - break; - } - case FILTER_RULE_CUSTOM: - if (rule->u.custom.func (filter_info, rule->u.custom.data)) - return TRUE; - break; + for (list = rule->u.pixbuf_formats; list; list = list->next) + { + int i; + char **mime_types; + + mime_types = gdk_pixbuf_format_get_mime_types (list->data); + + for (i = 0; mime_types[i] != NULL; i++) + { + if (strcmp (mime_types[i], filter_content_type) == 0) + { + g_strfreev (mime_types); + return TRUE; + } + } + + g_strfreev (mime_types); + } + } + } + break; + case FILTER_RULE_CUSTOM: + if (rule->u.custom.func (info, rule->u.custom.data)) + return TRUE; + break; default: break; - } + } } return FALSE; diff --git a/gtk/gtkfilefilter.h b/gtk/gtkfilefilter.h index c3ee9eb3f1..0c968e111b 100644 --- a/gtk/gtkfilefilter.h +++ b/gtk/gtkfilefilter.h @@ -33,7 +33,6 @@ G_BEGIN_DECLS #define GTK_IS_FILE_FILTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_FILTER)) typedef struct _GtkFileFilter GtkFileFilter; -typedef struct _GtkFileFilterInfo GtkFileFilterInfo; /** * GtkFileFilterFlags: @@ -55,8 +54,7 @@ typedef enum { /** * GtkFileFilterFunc: - * @filter_info: a #GtkFileFilterInfo that is filled according - * to the @needed flags passed to gtk_file_filter_add_custom() + * @info: a #GFileInfo * @data: (closure): user data passed to gtk_file_filter_add_custom() * * The type of function that is used with custom filters, see @@ -64,31 +62,8 @@ typedef enum { * * Returns: %TRUE if the file should be displayed */ -typedef gboolean (*GtkFileFilterFunc) (const GtkFileFilterInfo *filter_info, - gpointer data); - -/** - * GtkFileFilterInfo: - * @contains: Flags indicating which of the following fields need - * are filled - * @filename: the filename of the file being tested - * @uri: the URI for the file being tested - * @display_name: the string that will be used to display the file - * in the file chooser - * @mime_type: the mime type of the file - * - * A #GtkFileFilterInfo is used to pass information about the - * tested file to gtk_file_filter_filter(). - */ -struct _GtkFileFilterInfo -{ - GtkFileFilterFlags contains; - - const gchar *filename; - const gchar *uri; - const gchar *display_name; - const gchar *mime_type; -}; +typedef gboolean (*GtkFileFilterFunc) (GFileInfo *info, + gpointer data); GDK_AVAILABLE_IN_ALL GType gtk_file_filter_get_type (void) G_GNUC_CONST; @@ -120,7 +95,7 @@ GDK_AVAILABLE_IN_ALL GtkFileFilterFlags gtk_file_filter_get_needed (GtkFileFilter *filter); GDK_AVAILABLE_IN_ALL gboolean gtk_file_filter_filter (GtkFileFilter *filter, - const GtkFileFilterInfo *filter_info); + GFileInfo *info); GDK_AVAILABLE_IN_ALL GVariant *gtk_file_filter_to_gvariant (GtkFileFilter *filter); diff --git a/gtk/gtkfilesystemmodel.c b/gtk/gtkfilesystemmodel.c index af9fce15d0..b816c464e9 100644 --- a/gtk/gtkfilesystemmodel.c +++ b/gtk/gtkfilesystemmodel.c @@ -375,12 +375,6 @@ static gboolean node_should_be_filtered_out (GtkFileSystemModel *model, guint id) { FileModelNode *node = get_node (model, id); - GtkFileFilterInfo filter_info = { 0, }; - GtkFileFilterFlags required; - gboolean result; - char *mime_type = NULL; - char *filename = NULL; - char *uri = NULL; if (node->info == NULL) return TRUE; @@ -388,57 +382,10 @@ node_should_be_filtered_out (GtkFileSystemModel *model, guint id) if (model->filter == NULL) return FALSE; - /* fill info */ - required = gtk_file_filter_get_needed (model->filter); + if (!g_file_info_has_attribute (node->info, "standard::file")) + g_file_info_set_attribute_object (node->info, "standard::file", G_OBJECT (node->file)); - filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME; - filter_info.display_name = g_file_info_get_display_name (node->info); - - if (required & GTK_FILE_FILTER_MIME_TYPE) - { - const char *s = g_file_info_get_content_type (node->info); - - if (!s) - s = g_file_info_get_attribute_string (node->info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE); - - if (s) - { - mime_type = g_content_type_get_mime_type (s); - if (mime_type) - { - filter_info.mime_type = mime_type; - filter_info.contains |= GTK_FILE_FILTER_MIME_TYPE; - } - } - } - - if (required & GTK_FILE_FILTER_FILENAME) - { - filename = g_file_get_path (node->file); - if (filename) - { - filter_info.filename = filename; - filter_info.contains |= GTK_FILE_FILTER_FILENAME; - } - } - - if (required & GTK_FILE_FILTER_URI) - { - uri = g_file_get_uri (node->file); - if (uri) - { - filter_info.uri = uri; - filter_info.contains |= GTK_FILE_FILTER_URI; - } - } - - result = !gtk_file_filter_filter (model->filter, &filter_info); - - g_free (mime_type); - g_free (filename); - g_free (uri); - - return result; + return !gtk_file_filter_filter (model->filter, node->info); } static gboolean diff --git a/tests/testfilechooser.c b/tests/testfilechooser.c index 1dca027ecc..4ff2c8ca96 100644 --- a/tests/testfilechooser.c +++ b/tests/testfilechooser.c @@ -119,11 +119,15 @@ response_cb (GtkDialog *dialog, } static gboolean -no_backup_files_filter (const GtkFileFilterInfo *filter_info, - gpointer data) +no_backup_files_filter (GFileInfo *info, + gpointer data) { - gsize len = filter_info->display_name ? strlen (filter_info->display_name) : 0; - if (len > 0 && filter_info->display_name[len - 1] == '~') + const char *display_name; + gsize len; + + display_name = g_file_info_get_display_name (info); + len = strlen (display_name); + if (len > 0 && display_name[len - 1] == '~') return 0; else return 1; diff --git a/testsuite/gtk/builder.c b/testsuite/gtk/builder.c index db1eb2dd04..38537a10f3 100644 --- a/testsuite/gtk/builder.c +++ b/testsuite/gtk/builder.c @@ -2387,7 +2387,6 @@ test_file_filter (void) GtkBuilder *builder; GObject *obj; GtkFileFilter *filter; - GtkFileFilterInfo info; const gchar buffer[] = "" @@ -2412,15 +2411,6 @@ test_file_filter (void) g_assert (gtk_file_filter_get_needed (filter) & GTK_FILE_FILTER_MIME_TYPE); g_assert (gtk_file_filter_get_needed (filter) & GTK_FILE_FILTER_DISPLAY_NAME); - info.filename = "test1.txt"; - info.display_name = "test1.txt"; - info.contains = GTK_FILE_FILTER_FILENAME | GTK_FILE_FILTER_DISPLAY_NAME; - g_assert (gtk_file_filter_filter (filter, &info)); - - info.mime_type = "application/x-pdf"; - info.contains = GTK_FILE_FILTER_MIME_TYPE; - g_assert (!gtk_file_filter_filter (filter, &info)); - g_object_unref (builder); }