From 3106a938506c3b73e775d055025fb774aa828b80 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Tue, 11 Jun 2024 22:21:26 +0200 Subject: [PATCH] Do not create empty file filters The xdg-desktop-portal file chooser implementation (that can be forced with `GTK_USE_PORTAL=1`) doesn't allow those and fails to create the dialog if there are any such filter. This seems like a potential xdg-desktop-portal bug, but it's easy enough to workaround, and there's no guarantee yet it will change. It's also unlikely to actually happen that we create such an empty filter, but it is possible if a filetype doesn't have a corresponding filetype_extensions.conf entry. --- src/dialogs.c | 12 +++++++----- src/filetypes.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/dialogs.c b/src/dialogs.c index fe254c51f8..d1fa65a54b 100644 --- a/src/dialogs.c +++ b/src/dialogs.c @@ -372,6 +372,7 @@ static GtkWidget *add_file_open_extra_widget(GtkWidget *dialog) static GtkFileChooser *create_open_file_dialog(void) { GtkFileChooser *dialog; + GtkFileFilter *filter; GtkWidget *viewbtn; GSList *node; @@ -408,18 +409,19 @@ static GtkFileChooser *create_open_file_dialog(void) } /* add FileFilters(start with "All Files") */ - gtk_file_chooser_add_filter(dialog, - filetypes_create_file_filter(filetypes[GEANY_FILETYPES_NONE])); + if ((filter = filetypes_create_file_filter(filetypes[GEANY_FILETYPES_NONE]))) + gtk_file_chooser_add_filter(dialog, filter); /* now create meta filter "All Source" */ - gtk_file_chooser_add_filter(dialog, - filetypes_create_file_filter_all_source()); + if ((filter = filetypes_create_file_filter_all_source())) + gtk_file_chooser_add_filter(dialog, filter); foreach_slist(node, filetypes_by_title) { GeanyFiletype *ft = node->data; if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE)) continue; - gtk_file_chooser_add_filter(dialog, filetypes_create_file_filter(ft)); + if ((filter = filetypes_create_file_filter(ft))) + gtk_file_chooser_add_filter(dialog, filter); } gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); diff --git a/src/filetypes.c b/src/filetypes.c index b1913e3689..883201352e 100644 --- a/src/filetypes.c +++ b/src/filetypes.c @@ -1187,6 +1187,7 @@ GtkFileFilter *filetypes_create_file_filter_all_source(void) { GtkFileFilter *new_filter; guint i, j; + guint n_patterns = 0; new_filter = gtk_file_filter_new(); gtk_file_filter_set_name(new_filter, _("All Source")); @@ -1200,7 +1201,16 @@ GtkFileFilter *filetypes_create_file_filter_all_source(void) { gtk_file_filter_add_pattern(new_filter, filetypes[i]->pattern[j]); } + n_patterns += j; } + + /* very unlikely case where there is *no* patterns at all */ + if (n_patterns == 0) + { + g_object_unref(new_filter); + new_filter = NULL; + } + return new_filter; } @@ -1213,6 +1223,13 @@ GtkFileFilter *filetypes_create_file_filter(const GeanyFiletype *ft) g_return_val_if_fail(ft != NULL, NULL); + /* unlikely case where the ft has no patterns */ + if (! ft->pattern[0]) + { + g_debug("Not creating filter for filetype %s that has no pattern", ft->name); + return NULL; + } + new_filter = gtk_file_filter_new(); title = ft->id == GEANY_FILETYPES_NONE ? _("All files") : ft->title; gtk_file_filter_set_name(new_filter, title);