[gedit] FileChooser: factor out setup_filters()



commit e8d1ee95a33ced95e946f19ff3559eedbb9fd02e
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Jun 8 18:28:36 2020 +0200

    FileChooser: factor out setup_filters()
    
    _gedit_file_chooser_setup_filters() will also be used by the new
    GeditFileChooserOpen, but since GeditFileChooserOpen uses a
    GtkFileChooserNative, the custom filter will need to be modified to
    become a filter based on mimetypes or glob patterns.
    
    I think it's possible to no longer use a custom filter, without
    regression.

 gedit/gedit-file-chooser-dialog-gtk.c | 167 +------------------------------
 gedit/gedit-file-chooser.c            | 180 ++++++++++++++++++++++++++++++++++
 gedit/gedit-file-chooser.h            |  32 ++++++
 gedit/meson.build                     |   2 +
 po/POTFILES.in                        |   1 +
 5 files changed, 217 insertions(+), 165 deletions(-)
---
diff --git a/gedit/gedit-file-chooser-dialog-gtk.c b/gedit/gedit-file-chooser-dialog-gtk.c
index 1e1265a94..cbab77949 100644
--- a/gedit/gedit-file-chooser-dialog-gtk.c
+++ b/gedit/gedit-file-chooser-dialog-gtk.c
@@ -23,28 +23,20 @@
 /* TODO: add encoding property */
 
 #include "config.h"
-
 #include "gedit-file-chooser-dialog-gtk.h"
-
 #include <string.h>
-
 #include <glib/gi18n.h>
-
 #include "gedit-encodings-combo-box.h"
 #include "gedit-debug.h"
 #include "gedit-enum-types.h"
 #include "gedit-settings.h"
 #include "gedit-utils.h"
-
-#define ALL_FILES              _("All Files")
-#define ALL_TEXT_FILES         _("All Text Files")
+#include "gedit-file-chooser.h"
 
 struct _GeditFileChooserDialogGtk
 {
        GtkFileChooserDialog parent_instance;
 
-       GSettings *filter_settings;
-
        GtkWidget *option_menu;
        GtkWidget *extra_widget;
 
@@ -248,22 +240,9 @@ gedit_file_chooser_dialog_gtk_chooser_init (gpointer g_iface,
        iface->get_window = chooser_get_window;
 }
 
-static void
-gedit_file_chooser_dialog_gtk_dispose (GObject *object)
-{
-       GeditFileChooserDialogGtk *dialog_gtk = GEDIT_FILE_CHOOSER_DIALOG_GTK (object);
-
-       g_clear_object (&dialog_gtk->filter_settings);
-
-       G_OBJECT_CLASS (gedit_file_chooser_dialog_gtk_parent_class)->dispose (object);
-}
-
 static void
 gedit_file_chooser_dialog_gtk_class_init (GeditFileChooserDialogGtkClass *klass)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-       object_class->dispose = gedit_file_chooser_dialog_gtk_dispose;
 }
 
 static void
@@ -435,115 +414,9 @@ action_changed (GeditFileChooserDialogGtk *dialog,
        update_newline_visibility (dialog);
 }
 
-static void
-filter_changed (GeditFileChooserDialogGtk *dialog,
-               GParamSpec             *pspec,
-               gpointer                data)
-{
-       GtkFileFilter *filter;
-
-       filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));
-       if (filter != NULL)
-       {
-               /* Store ID of selected filter */
-               const gchar *name;
-               gint id = 0;
-
-               name = gtk_file_filter_get_name (filter);
-               g_return_if_fail (name != NULL);
-
-               if (strcmp (name, ALL_FILES) == 0)
-                       id = 1;
-
-               gedit_debug_message (DEBUG_COMMANDS, "Active filter: %s (%d)", name, id);
-
-               g_settings_set_int (dialog->filter_settings,
-                                   GEDIT_SETTINGS_ACTIVE_FILE_FILTER, id);
-       }
-}
-
-/* FIXME: use globs too - Paolo (Aug. 27, 2007) */
-static gboolean
-all_text_files_filter (const GtkFileFilterInfo *filter_info,
-                      gpointer                 data)
-{
-       static GSList *known_mime_types = NULL;
-       GSList *mime_types;
-
-       if (known_mime_types == NULL)
-       {
-               GtkSourceLanguageManager *lm;
-               const gchar * const *languages;
-
-               lm = gtk_source_language_manager_get_default ();
-               languages = gtk_source_language_manager_get_language_ids (lm);
-
-               while ((languages != NULL) && (*languages != NULL))
-               {
-                       gchar **mime_types;
-                       gint i;
-                       GtkSourceLanguage *lang;
-
-                       lang = gtk_source_language_manager_get_language (lm, *languages);
-                       g_return_val_if_fail (GTK_SOURCE_IS_LANGUAGE (lang), FALSE);
-                       ++languages;
-
-                       mime_types = gtk_source_language_get_mime_types (lang);
-                       if (mime_types == NULL)
-                               continue;
-
-                       for (i = 0; mime_types[i] != NULL; i++)
-                       {
-                               if (!g_content_type_is_a (mime_types[i], "text/plain"))
-                               {
-                                       gedit_debug_message (DEBUG_COMMANDS,
-                                                            "Mime-type %s is not related to text/plain",
-                                                            mime_types[i]);
-
-                                       known_mime_types = g_slist_prepend (known_mime_types,
-                                                                           g_strdup (mime_types[i]));
-                               }
-                       }
-
-                       g_strfreev (mime_types);
-               }
-
-               /* known_mime_types always has "text/plain" as first item" */
-               known_mime_types = g_slist_prepend (known_mime_types, g_strdup ("text/plain"));
-       }
-
-       /* known mime_types contains "text/plain" and then the list of mime-types unrelated to "text/plain"
-        * that gedit recognizes */
-
-       if (filter_info->mime_type == NULL)
-               return FALSE;
-
-       /*
-        * The filter is matching:
-        * - the mime-types beginning with "text/"
-        * - the mime-types inheriting from a known mime-type (note the text/plain is
-        *   the first known mime-type)
-        */
-
-       if (strncmp (filter_info->mime_type, "text/", 5) == 0)
-               return TRUE;
-
-       mime_types = known_mime_types;
-       while (mime_types != NULL)
-       {
-               if (g_content_type_is_a (filter_info->mime_type, (const gchar*)mime_types->data))
-                       return TRUE;
-
-               mime_types = g_slist_next (mime_types);
-       }
-
-       return FALSE;
-}
-
 static void
 gedit_file_chooser_dialog_gtk_init (GeditFileChooserDialogGtk *dialog)
 {
-       dialog->filter_settings = g_settings_new ("org.gnome.gedit.state.file-chooser");
 }
 
 GeditFileChooserDialog *
@@ -554,8 +427,6 @@ gedit_file_chooser_dialog_gtk_create (const gchar           *title,
                                      const gchar           *cancel_label)
 {
        GeditFileChooserDialogGtk *result;
-       GtkFileFilter *filter;
-       gint active_filter;
        GtkFileChooserAction action;
        gboolean select_multiple;
 
@@ -584,41 +455,7 @@ gedit_file_chooser_dialog_gtk_create (const gchar           *title,
                          G_CALLBACK (action_changed),
                          NULL);
 
-       active_filter = g_settings_get_int (result->filter_settings, GEDIT_SETTINGS_ACTIVE_FILE_FILTER);
-       gedit_debug_message (DEBUG_COMMANDS, "Active filter: %d", active_filter);
-
-       /* "All Text Files" filter */
-       filter = gtk_file_filter_new ();
-       gtk_file_filter_set_name (filter, ALL_TEXT_FILES);
-       gtk_file_filter_add_custom (filter,
-                                   GTK_FILE_FILTER_MIME_TYPE,
-                                   all_text_files_filter,
-                                   NULL,
-                                   NULL);
-       gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (result), filter);
-
-       if (active_filter != 1)
-       {
-               /* Use this filter if set by user and as default */
-               gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (result), filter);
-       }
-
-       /* "All Files" filter */
-       filter = gtk_file_filter_new ();
-       gtk_file_filter_set_name (filter, ALL_FILES);
-       gtk_file_filter_add_pattern (filter, "*");
-       gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (result), filter);
-
-       if (active_filter == 1)
-       {
-               /* Use this filter if set by user */
-               gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (result), filter);
-       }
-
-       g_signal_connect (result,
-                         "notify::filter",
-                         G_CALLBACK (filter_changed),
-                         NULL);
+       _gedit_file_chooser_setup_filters (GTK_FILE_CHOOSER (result));
 
        if (parent != NULL)
        {
diff --git a/gedit/gedit-file-chooser.c b/gedit/gedit-file-chooser.c
new file mode 100644
index 000000000..342f8d901
--- /dev/null
+++ b/gedit/gedit-file-chooser.c
@@ -0,0 +1,180 @@
+/*
+ * This file is part of gedit
+ *
+ * Copyright (C) 2020 Sébastien Wilmet <swilmet gnome org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gedit-file-chooser.h"
+#include <glib/gi18n.h>
+#include <gtksourceview/gtksource.h>
+#include "gedit-settings.h"
+
+/* Common code between the different GeditFileChooser's. */
+
+#define ALL_FILES              _("All Files")
+#define ALL_TEXT_FILES         _("All Text Files")
+
+/* FIXME: use globs too - Paolo (Aug. 27, 2007) */
+static gboolean
+all_text_files_filter (const GtkFileFilterInfo *filter_info,
+                      gpointer                 data)
+{
+       static GSList *known_mime_types = NULL;
+       GSList *mime_types;
+
+       if (known_mime_types == NULL)
+       {
+               GtkSourceLanguageManager *lm;
+               const gchar * const *languages;
+
+               lm = gtk_source_language_manager_get_default ();
+               languages = gtk_source_language_manager_get_language_ids (lm);
+
+               while ((languages != NULL) && (*languages != NULL))
+               {
+                       gchar **mime_types;
+                       gint i;
+                       GtkSourceLanguage *lang;
+
+                       lang = gtk_source_language_manager_get_language (lm, *languages);
+                       g_return_val_if_fail (GTK_SOURCE_IS_LANGUAGE (lang), FALSE);
+                       ++languages;
+
+                       mime_types = gtk_source_language_get_mime_types (lang);
+                       if (mime_types == NULL)
+                               continue;
+
+                       for (i = 0; mime_types[i] != NULL; i++)
+                       {
+                               if (!g_content_type_is_a (mime_types[i], "text/plain"))
+                               {
+                                       //g_message ("Mime-type '%s' is not related to 'text/plain'", 
mime_types[i]);
+
+                                       known_mime_types = g_slist_prepend (known_mime_types,
+                                                                           g_strdup (mime_types[i]));
+                               }
+                       }
+
+                       g_strfreev (mime_types);
+               }
+
+               /* known_mime_types always has "text/plain" as first item" */
+               known_mime_types = g_slist_prepend (known_mime_types, g_strdup ("text/plain"));
+       }
+
+       /* known mime_types contains "text/plain" and then the list of mime-types unrelated to "text/plain"
+        * that gedit recognizes */
+
+       if (filter_info->mime_type == NULL)
+               return FALSE;
+
+       /*
+        * The filter is matching:
+        * - the mime-types beginning with "text/"
+        * - the mime-types inheriting from a known mime-type (note the text/plain is
+        *   the first known mime-type)
+        */
+
+       if (strncmp (filter_info->mime_type, "text/", 5) == 0)
+               return TRUE;
+
+       mime_types = known_mime_types;
+       while (mime_types != NULL)
+       {
+               if (g_content_type_is_a (filter_info->mime_type, (const gchar*)mime_types->data))
+                       return TRUE;
+
+               mime_types = g_slist_next (mime_types);
+       }
+
+       return FALSE;
+}
+
+static void
+notify_filter_cb (GtkFileChooser *chooser,
+                 GParamSpec     *pspec,
+                 gpointer        user_data)
+{
+       GtkFileFilter *filter;
+
+       filter = gtk_file_chooser_get_filter (chooser);
+       if (filter != NULL)
+       {
+               /* Store ID of selected filter */
+               const gchar *name;
+               gint id = 0;
+               GeditSettings *settings;
+               GSettings *file_chooser_state_settings;
+
+               name = gtk_file_filter_get_name (filter);
+               g_return_if_fail (name != NULL);
+
+               if (strcmp (name, ALL_FILES) == 0)
+                       id = 1;
+
+               settings = _gedit_settings_get_singleton ();
+               file_chooser_state_settings = _gedit_settings_peek_file_chooser_state_settings (settings);
+               g_settings_set_int (file_chooser_state_settings, GEDIT_SETTINGS_ACTIVE_FILE_FILTER, id);
+       }
+}
+
+void
+_gedit_file_chooser_setup_filters (GtkFileChooser *chooser)
+{
+       GeditSettings *settings;
+       GSettings *file_chooser_state_settings;
+       gint active_filter;
+       GtkFileFilter *filter;
+
+       g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
+
+       settings = _gedit_settings_get_singleton ();
+       file_chooser_state_settings = _gedit_settings_peek_file_chooser_state_settings (settings);
+       active_filter = g_settings_get_int (file_chooser_state_settings, GEDIT_SETTINGS_ACTIVE_FILE_FILTER);
+
+       /* "All Text Files" filter */
+       filter = gtk_file_filter_new ();
+       gtk_file_filter_set_name (filter, ALL_TEXT_FILES);
+       gtk_file_filter_add_custom (filter,
+                                   GTK_FILE_FILTER_MIME_TYPE,
+                                   all_text_files_filter,
+                                   NULL,
+                                   NULL);
+       gtk_file_chooser_add_filter (chooser, filter);
+
+       if (active_filter != 1)
+       {
+               /* Use this filter if set by user and as default. */
+               gtk_file_chooser_set_filter (chooser, filter);
+       }
+
+       /* "All Files" filter */
+       filter = gtk_file_filter_new ();
+       gtk_file_filter_set_name (filter, ALL_FILES);
+       gtk_file_filter_add_pattern (filter, "*");
+       gtk_file_chooser_add_filter (chooser, filter);
+
+       if (active_filter == 1)
+       {
+               /* Use this filter if set by user. */
+               gtk_file_chooser_set_filter (chooser, filter);
+       }
+
+       g_signal_connect (chooser,
+                         "notify::filter",
+                         G_CALLBACK (notify_filter_cb),
+                         NULL);
+}
diff --git a/gedit/gedit-file-chooser.h b/gedit/gedit-file-chooser.h
new file mode 100644
index 000000000..f1366f9ed
--- /dev/null
+++ b/gedit/gedit-file-chooser.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of gedit
+ *
+ * Copyright (C) 2020 Sébastien Wilmet <swilmet gnome org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GEDIT_FILE_CHOOSER_H
+#define GEDIT_FILE_CHOOSER_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL
+void   _gedit_file_chooser_setup_filters       (GtkFileChooser *chooser);
+
+G_END_DECLS
+
+#endif /* GEDIT_FILE_CHOOSER_H */
diff --git a/gedit/meson.build b/gedit/meson.build
index 14a7ec0eb..4a3dec2b6 100644
--- a/gedit/meson.build
+++ b/gedit/meson.build
@@ -51,6 +51,7 @@ libgedit_private_headers = [
   'gedit-file-chooser-dialog-gtk.h',
   'gedit-file-chooser-dialog.h',
   'gedit-file-chooser-dialog-osx.h',
+  'gedit-file-chooser.h',
   'gedit-file-chooser-open.h',
   'gedit-highlight-mode-dialog.h',
   'gedit-highlight-mode-selector.h',
@@ -88,6 +89,7 @@ libgedit_private_sources = [
   'gedit-encoding-items.c',
   'gedit-encodings-dialog.c',
   'gedit-factory.c',
+  'gedit-file-chooser.c',
   'gedit-file-chooser-dialog.c',
   'gedit-file-chooser-dialog-gtk.c',
   'gedit-highlight-mode-dialog.c',
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 6c10bd271..edbd9c196 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -17,6 +17,7 @@ gedit/gedit-encodings-combo-box.c
 gedit/gedit-encoding-items.c
 gedit/gedit-encodings-dialog.c
 gedit/gedit-factory.c
+gedit/gedit-file-chooser.c
 gedit/gedit-file-chooser-dialog-gtk.c
 gedit/gedit-file-chooser-dialog-osx.m
 gedit/gedit-file-chooser-open-adapter.c


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]