Skip to content

Commit

Permalink
Do not create empty file filters
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
b4n committed Jun 11, 2024
1 parent f1a2870 commit 3106a93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/filetypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down

0 comments on commit 3106a93

Please sign in to comment.