2008-07-01 22:57:50 +00:00
|
|
|
|
/* GTK - The GIMP Toolkit
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* gtkfilefilter.c: Filters for selecting a file subset
|
|
|
|
|
* Copyright (C) 2003, Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2003-07-23 15:31:10 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2011-01-15 13:50:50 +00:00
|
|
|
|
/**
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* GtkFileFilter:
|
2011-01-15 13:50:50 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* `GtkFileFilter` filters files by name or mime type.
|
|
|
|
|
*
|
|
|
|
|
* `GtkFileFilter` can be used to restrict the files being shown in a
|
|
|
|
|
* `GtkFileChooser`. Files can be filtered based on their name (with
|
2021-06-04 03:55:48 +00:00
|
|
|
|
* [method@Gtk.FileFilter.add_pattern] or [method@Gtk.FileFilter.add_suffix])
|
|
|
|
|
* or on their mime type (with [method@Gtk.FileFilter.add_mime_type]).
|
2011-01-15 13:50:50 +00:00
|
|
|
|
*
|
|
|
|
|
* Filtering by mime types handles aliasing and subclassing of mime
|
|
|
|
|
* types; e.g. a filter for text/plain also matches a file with mime
|
|
|
|
|
* type application/rtf, since application/rtf is a subclass of
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* text/plain. Note that `GtkFileFilter` allows wildcards for the
|
2014-02-15 04:34:22 +00:00
|
|
|
|
* subtype of a mime type, so you can e.g. filter for image/\*.
|
2011-01-15 13:50:50 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Normally, file filters are used by adding them to a `GtkFileChooser`
|
|
|
|
|
* (see [method@Gtk.FileChooser.add_filter]), but it is also possible to
|
|
|
|
|
* manually use a file filter on any [class@Gtk.FilterListModel] containing
|
|
|
|
|
* `GFileInfo` objects.
|
2011-01-15 13:50:50 +00:00
|
|
|
|
*
|
2014-02-05 02:00:58 +00:00
|
|
|
|
* # GtkFileFilter as GtkBuildable
|
2014-02-02 05:29:00 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* The `GtkFileFilter` implementation of the `GtkBuildable` interface
|
2021-06-04 03:55:48 +00:00
|
|
|
|
* supports adding rules using the `<mime-types>` and `<patterns>` and
|
|
|
|
|
* `<suffixes>` elements and listing the rules within. Specifying a
|
|
|
|
|
* `<mime-type>` or `<pattern>` or `<suffix>` has the same effect as
|
|
|
|
|
* as calling
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* [method@Gtk.FileFilter.add_mime_type] or
|
2021-06-04 03:55:48 +00:00
|
|
|
|
* [method@Gtk.FileFilter.add_pattern] or
|
|
|
|
|
* [method@Gtk.FileFilter.add_suffix].
|
2011-01-27 07:30:15 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* An example of a UI definition fragment specifying `GtkFileFilter`
|
2014-02-02 05:29:00 +00:00
|
|
|
|
* rules:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* ```xml
|
2011-01-27 07:30:15 +00:00
|
|
|
|
* <object class="GtkFileFilter">
|
2018-10-07 21:22:04 +00:00
|
|
|
|
* <property name="name" translatable="yes">Text and Images</property>
|
2011-01-27 07:30:15 +00:00
|
|
|
|
* <mime-types>
|
|
|
|
|
* <mime-type>text/plain</mime-type>
|
2014-02-15 04:34:22 +00:00
|
|
|
|
* <mime-type>image/ *</mime-type>
|
2011-01-27 07:30:15 +00:00
|
|
|
|
* </mime-types>
|
|
|
|
|
* <patterns>
|
|
|
|
|
* <pattern>*.txt</pattern>
|
|
|
|
|
* </patterns>
|
2021-06-04 03:55:48 +00:00
|
|
|
|
* <suffixes>
|
|
|
|
|
* <suffix>png</suffix>
|
|
|
|
|
* </suffixes>
|
2011-01-27 07:30:15 +00:00
|
|
|
|
* </object>
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* ```
|
2011-01-15 13:50:50 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
|
#include "config.h"
|
2003-07-23 15:31:10 +00:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2010-10-19 17:14:46 +00:00
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
|
|
2015-11-03 13:52:42 +00:00
|
|
|
|
#include "gtkfilefilterprivate.h"
|
2011-01-27 07:30:15 +00:00
|
|
|
|
#include "gtkbuildable.h"
|
2015-04-25 23:34:34 +00:00
|
|
|
|
#include "gtkbuilderprivate.h"
|
2005-09-01 05:11:46 +00:00
|
|
|
|
#include "gtkintl.h"
|
2003-10-27 23:50:34 +00:00
|
|
|
|
#include "gtkprivate.h"
|
2020-07-04 03:58:07 +00:00
|
|
|
|
#include "gtkfilter.h"
|
2003-10-23 04:22:32 +00:00
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
typedef struct _GtkFileFilterClass GtkFileFilterClass;
|
|
|
|
|
typedef struct _FilterRule FilterRule;
|
|
|
|
|
|
|
|
|
|
#define GTK_FILE_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_FILTER, GtkFileFilterClass))
|
|
|
|
|
#define GTK_IS_FILE_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_FILTER))
|
|
|
|
|
#define GTK_FILE_FILTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_FILTER, GtkFileFilterClass))
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
FILTER_RULE_PATTERN,
|
|
|
|
|
FILTER_RULE_MIME_TYPE,
|
2021-06-04 03:55:48 +00:00
|
|
|
|
FILTER_RULE_SUFFIX,
|
2020-07-05 17:51:42 +00:00
|
|
|
|
FILTER_RULE_PIXBUF_FORMATS
|
2003-07-23 15:31:10 +00:00
|
|
|
|
} FilterRuleType;
|
|
|
|
|
|
|
|
|
|
struct _GtkFileFilterClass
|
|
|
|
|
{
|
2020-07-04 14:57:07 +00:00
|
|
|
|
GtkFilterClass parent_class;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _GtkFileFilter
|
|
|
|
|
{
|
2020-07-04 14:57:07 +00:00
|
|
|
|
GtkFilter parent_instance;
|
2010-09-18 23:54:31 +00:00
|
|
|
|
|
2020-07-05 23:36:45 +00:00
|
|
|
|
char *name;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
GSList *rules;
|
|
|
|
|
|
2020-07-05 18:24:21 +00:00
|
|
|
|
char **attributes;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _FilterRule
|
|
|
|
|
{
|
|
|
|
|
FilterRuleType type;
|
2020-07-04 03:58:07 +00:00
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
union {
|
2020-07-05 19:07:36 +00:00
|
|
|
|
char *pattern;
|
|
|
|
|
char **content_types;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
} u;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-07 21:22:04 +00:00
|
|
|
|
enum {
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_NAME,
|
|
|
|
|
NUM_PROPERTIES
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static GParamSpec *props[NUM_PROPERTIES] = { NULL, };
|
|
|
|
|
|
|
|
|
|
|
2020-07-04 14:57:07 +00:00
|
|
|
|
static gboolean gtk_file_filter_match (GtkFilter *filter,
|
|
|
|
|
gpointer item);
|
|
|
|
|
static GtkFilterMatch gtk_file_filter_get_strictness (GtkFilter *filter);
|
2020-07-04 03:58:07 +00:00
|
|
|
|
|
2020-07-05 23:36:45 +00:00
|
|
|
|
static void gtk_file_filter_buildable_init (GtkBuildableIface *iface);
|
|
|
|
|
|
2020-07-04 14:57:07 +00:00
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkFileFilter, gtk_file_filter, GTK_TYPE_FILTER,
|
2011-01-27 07:30:15 +00:00
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
|
|
|
|
|
gtk_file_filter_buildable_init))
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
2006-05-03 17:07:09 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_init (GtkFileFilter *object)
|
|
|
|
|
{
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 21:22:04 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_set_property (GObject *object,
|
|
|
|
|
guint prop_id,
|
|
|
|
|
const GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
|
|
|
|
GtkFileFilter *filter = GTK_FILE_FILTER (object);
|
|
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_NAME:
|
|
|
|
|
gtk_file_filter_set_name (filter, g_value_get_string (value));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_get_property (GObject *object,
|
|
|
|
|
guint prop_id,
|
|
|
|
|
GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
|
|
|
|
GtkFileFilter *filter = GTK_FILE_FILTER (object);
|
|
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_NAME:
|
|
|
|
|
g_value_set_string (value, filter->name);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 23:36:45 +00:00
|
|
|
|
static void
|
|
|
|
|
filter_rule_free (FilterRule *rule)
|
|
|
|
|
{
|
|
|
|
|
switch (rule->type)
|
|
|
|
|
{
|
|
|
|
|
case FILTER_RULE_PATTERN:
|
2021-06-04 03:55:48 +00:00
|
|
|
|
case FILTER_RULE_SUFFIX:
|
2020-07-05 23:36:45 +00:00
|
|
|
|
g_free (rule->u.pattern);
|
|
|
|
|
break;
|
|
|
|
|
case FILTER_RULE_MIME_TYPE:
|
|
|
|
|
case FILTER_RULE_PIXBUF_FORMATS:
|
|
|
|
|
g_strfreev (rule->u.content_types);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
g_slice_free (FilterRule, rule);
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_finalize (GObject *object)
|
|
|
|
|
{
|
2003-11-15 20:35:55 +00:00
|
|
|
|
GtkFileFilter *filter = GTK_FILE_FILTER (object);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
2015-07-31 21:57:04 +00:00
|
|
|
|
g_slist_free_full (filter->rules, (GDestroyNotify)filter_rule_free);
|
2020-07-05 18:24:21 +00:00
|
|
|
|
g_strfreev (filter->attributes);
|
2004-01-22 02:39:31 +00:00
|
|
|
|
|
2007-03-09 21:57:37 +00:00
|
|
|
|
g_free (filter->name);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
2006-05-03 17:07:09 +00:00
|
|
|
|
G_OBJECT_CLASS (gtk_file_filter_parent_class)->finalize (object);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-27 07:30:15 +00:00
|
|
|
|
static void
|
2020-07-05 23:36:45 +00:00
|
|
|
|
gtk_file_filter_class_init (GtkFileFilterClass *class)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2020-07-05 23:36:45 +00:00
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
GtkFilterClass *filter_class = GTK_FILTER_CLASS (class);
|
|
|
|
|
|
|
|
|
|
gobject_class->set_property = gtk_file_filter_set_property;
|
|
|
|
|
gobject_class->get_property = gtk_file_filter_get_property;
|
|
|
|
|
gobject_class->finalize = gtk_file_filter_finalize;
|
|
|
|
|
|
|
|
|
|
filter_class->get_strictness = gtk_file_filter_get_strictness;
|
|
|
|
|
filter_class->match = gtk_file_filter_match;
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* GtkFileFilter:name: (attributes org.gtk.Property.get=gtk_file_filter_get_name org.gtk.Property.set=gtk_file_filter_set_name)
|
2020-07-05 23:36:45 +00:00
|
|
|
|
*
|
|
|
|
|
* The human-readable name of the filter.
|
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* This is the string that will be displayed in the file chooser
|
|
|
|
|
* user interface if there is a selectable list of filters.
|
2020-07-05 23:36:45 +00:00
|
|
|
|
*/
|
|
|
|
|
props[PROP_NAME] =
|
|
|
|
|
g_param_spec_string ("name",
|
|
|
|
|
P_("Name"),
|
|
|
|
|
P_("The human-readable name for this filter"),
|
|
|
|
|
NULL,
|
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, props);
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 23:36:45 +00:00
|
|
|
|
/*
|
|
|
|
|
* GtkBuildable implementation
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
2011-01-27 07:30:15 +00:00
|
|
|
|
PARSE_MIME_TYPES,
|
2021-06-04 03:55:48 +00:00
|
|
|
|
PARSE_PATTERNS,
|
|
|
|
|
PARSE_SUFFIXES
|
2011-01-27 07:30:15 +00:00
|
|
|
|
} ParserType;
|
|
|
|
|
|
2020-07-05 23:36:45 +00:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2011-01-27 07:30:15 +00:00
|
|
|
|
GtkFileFilter *filter;
|
2015-04-25 23:34:34 +00:00
|
|
|
|
GtkBuilder *builder;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
ParserType type;
|
|
|
|
|
GString *string;
|
|
|
|
|
gboolean parsing;
|
|
|
|
|
} SubParserData;
|
|
|
|
|
|
|
|
|
|
static void
|
2019-08-29 13:21:20 +00:00
|
|
|
|
parser_start_element (GtkBuildableParseContext *context,
|
2020-07-05 23:36:45 +00:00
|
|
|
|
const char *element_name,
|
|
|
|
|
const char **names,
|
|
|
|
|
const char **values,
|
2019-08-29 13:21:20 +00:00
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
SubParserData *data = (SubParserData*)user_data;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
|
2015-04-25 23:34:34 +00:00
|
|
|
|
if (!g_markup_collect_attributes (element_name, names, values, error,
|
|
|
|
|
G_MARKUP_COLLECT_INVALID, NULL, NULL,
|
|
|
|
|
G_MARKUP_COLLECT_INVALID))
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
_gtk_builder_prefix_error (data->builder, context, error);
|
2011-01-27 07:30:15 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2015-04-25 23:34:34 +00:00
|
|
|
|
|
|
|
|
|
if (strcmp (element_name, "mime-types") == 0 ||
|
2022-03-17 15:49:39 +00:00
|
|
|
|
strcmp (element_name, "patterns") == 0 ||
|
|
|
|
|
strcmp (element_name, "suffixes") == 0)
|
2015-04-25 23:34:34 +00:00
|
|
|
|
{
|
|
|
|
|
if (!_gtk_builder_check_parent (data->builder, context, "object", error))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "mime-type") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!_gtk_builder_check_parent (data->builder, context, "mime-types", error))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
data->parsing = TRUE;
|
|
|
|
|
}
|
2011-01-27 07:30:15 +00:00
|
|
|
|
else if (strcmp (element_name, "pattern") == 0)
|
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
if (!_gtk_builder_check_parent (data->builder, context, "patterns", error))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-06-04 03:55:48 +00:00
|
|
|
|
data->parsing = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "suffix") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!_gtk_builder_check_parent (data->builder, context, "suffixes", error))
|
|
|
|
|
return;
|
|
|
|
|
|
2015-04-25 23:34:34 +00:00
|
|
|
|
data->parsing = TRUE;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2015-04-25 23:34:34 +00:00
|
|
|
|
{
|
|
|
|
|
_gtk_builder_error_unhandled_tag (data->builder, context,
|
|
|
|
|
"GtkFileFilter", element_name,
|
|
|
|
|
error);
|
|
|
|
|
}
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-07-05 23:36:45 +00:00
|
|
|
|
parser_text_element (GtkBuildableParseContext *context,
|
|
|
|
|
const char *text,
|
|
|
|
|
gsize text_len,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
SubParserData *data = (SubParserData*)user_data;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
|
2015-04-25 23:34:34 +00:00
|
|
|
|
if (data->parsing)
|
|
|
|
|
g_string_append_len (data->string, text, text_len);
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-07-05 23:36:45 +00:00
|
|
|
|
parser_end_element (GtkBuildableParseContext *context,
|
|
|
|
|
const char *element_name,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
SubParserData *data = (SubParserData*)user_data;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
|
2022-04-19 03:03:15 +00:00
|
|
|
|
if (data->string && data->parsing)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
switch (data->type)
|
|
|
|
|
{
|
|
|
|
|
case PARSE_MIME_TYPES:
|
|
|
|
|
gtk_file_filter_add_mime_type (data->filter, data->string->str);
|
|
|
|
|
break;
|
|
|
|
|
case PARSE_PATTERNS:
|
|
|
|
|
gtk_file_filter_add_pattern (data->filter, data->string->str);
|
|
|
|
|
break;
|
2021-06-04 03:55:48 +00:00
|
|
|
|
case PARSE_SUFFIXES:
|
|
|
|
|
gtk_file_filter_add_suffix (data->filter, data->string->str);
|
|
|
|
|
break;
|
2015-04-25 23:34:34 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-25 23:34:34 +00:00
|
|
|
|
g_string_set_size (data->string, 0);
|
|
|
|
|
data->parsing = FALSE;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 13:21:20 +00:00
|
|
|
|
static const GtkBuildableParser sub_parser =
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
|
|
|
|
parser_start_element,
|
|
|
|
|
parser_end_element,
|
|
|
|
|
parser_text_element,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2019-08-29 13:21:20 +00:00
|
|
|
|
gtk_file_filter_buildable_custom_tag_start (GtkBuildable *buildable,
|
|
|
|
|
GtkBuilder *builder,
|
|
|
|
|
GObject *child,
|
2020-07-05 23:36:45 +00:00
|
|
|
|
const char *tagname,
|
2019-08-29 13:21:20 +00:00
|
|
|
|
GtkBuildableParser *parser,
|
|
|
|
|
gpointer *parser_data)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
SubParserData *data = NULL;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
|
|
|
|
|
if (strcmp (tagname, "mime-types") == 0)
|
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
data = g_slice_new0 (SubParserData);
|
|
|
|
|
data->string = g_string_new ("");
|
|
|
|
|
data->type = PARSE_MIME_TYPES;
|
|
|
|
|
data->filter = GTK_FILE_FILTER (buildable);
|
|
|
|
|
data->builder = builder;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
|
|
|
|
|
*parser = sub_parser;
|
2015-04-25 23:34:34 +00:00
|
|
|
|
*parser_data = data;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
else if (strcmp (tagname, "patterns") == 0)
|
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
data = g_slice_new0 (SubParserData);
|
|
|
|
|
data->string = g_string_new ("");
|
|
|
|
|
data->type = PARSE_PATTERNS;
|
|
|
|
|
data->filter = GTK_FILE_FILTER (buildable);
|
|
|
|
|
data->builder = builder;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
|
2021-06-04 03:55:48 +00:00
|
|
|
|
*parser = sub_parser;
|
|
|
|
|
*parser_data = data;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (tagname, "suffixes") == 0)
|
|
|
|
|
{
|
|
|
|
|
data = g_slice_new0 (SubParserData);
|
|
|
|
|
data->string = g_string_new ("");
|
|
|
|
|
data->type = PARSE_SUFFIXES;
|
|
|
|
|
data->filter = GTK_FILE_FILTER (buildable);
|
|
|
|
|
data->builder = builder;
|
|
|
|
|
|
2011-01-27 07:30:15 +00:00
|
|
|
|
*parser = sub_parser;
|
2015-04-25 23:34:34 +00:00
|
|
|
|
*parser_data = data;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-25 23:34:34 +00:00
|
|
|
|
return data != NULL;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_buildable_custom_tag_end (GtkBuildable *buildable,
|
2015-04-25 23:34:34 +00:00
|
|
|
|
GtkBuilder *builder,
|
|
|
|
|
GObject *child,
|
2020-07-05 23:36:45 +00:00
|
|
|
|
const char *tagname,
|
2018-10-07 22:34:43 +00:00
|
|
|
|
gpointer user_data)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
|
|
|
|
if (strcmp (tagname, "mime-types") == 0 ||
|
2022-03-17 15:49:39 +00:00
|
|
|
|
strcmp (tagname, "patterns") == 0 ||
|
|
|
|
|
strcmp (tagname, "suffixes") == 0)
|
2011-01-27 07:30:15 +00:00
|
|
|
|
{
|
2015-04-25 23:34:34 +00:00
|
|
|
|
SubParserData *data = (SubParserData*)user_data;
|
2011-01-27 07:30:15 +00:00
|
|
|
|
|
2015-04-25 23:34:34 +00:00
|
|
|
|
g_string_free (data->string, TRUE);
|
|
|
|
|
g_slice_free (SubParserData, data);
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 23:36:45 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_buildable_init (GtkBuildableIface *iface)
|
|
|
|
|
{
|
|
|
|
|
iface->custom_tag_start = gtk_file_filter_buildable_custom_tag_start;
|
|
|
|
|
iface->custom_tag_end = gtk_file_filter_buildable_custom_tag_end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Public api
|
|
|
|
|
*/
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_new:
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Creates a new `GtkFileFilter` with no rules added to it.
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2014-02-07 18:32:47 +00:00
|
|
|
|
* Such a filter doesn’t accept any files, so is not
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* particularly useful until you add rules with
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* [method@Gtk.FileFilter.add_mime_type],
|
2021-06-04 03:55:48 +00:00
|
|
|
|
* [method@Gtk.FileFilter.add_pattern],
|
|
|
|
|
* [method@Gtk.FileFilter.add_suffix] or
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* [method@Gtk.FileFilter.add_pixbuf_formats].
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
|
|
|
|
* To create a filter that accepts any file, use:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* ```c
|
2007-11-25 06:51:19 +00:00
|
|
|
|
* GtkFileFilter *filter = gtk_file_filter_new ();
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* gtk_file_filter_add_pattern (filter, "*");
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* ```
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Returns: a new `GtkFileFilter`
|
|
|
|
|
*/
|
2003-07-23 15:31:10 +00:00
|
|
|
|
GtkFileFilter *
|
|
|
|
|
gtk_file_filter_new (void)
|
|
|
|
|
{
|
|
|
|
|
return g_object_new (GTK_TYPE_FILE_FILTER, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* gtk_file_filter_set_name: (attributes org.gtk.Method.set_property=name)
|
|
|
|
|
* @filter: a `GtkFileFilter`
|
2021-05-19 11:24:34 +00:00
|
|
|
|
* @name: (nullable): the human-readable-name for the filter, or %NULL
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* to remove any existing name.
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Sets a human-readable name of the filter.
|
|
|
|
|
*
|
|
|
|
|
* This is the string that will be displayed in the file chooser
|
|
|
|
|
* if there is a selectable list of filters.
|
|
|
|
|
*/
|
2003-07-23 15:31:10 +00:00
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_set_name (GtkFileFilter *filter,
|
2020-07-05 23:36:45 +00:00
|
|
|
|
const char *name)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_FILTER (filter));
|
|
|
|
|
|
2018-10-07 21:22:04 +00:00
|
|
|
|
if (g_strcmp0 (filter->name, name) == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_free (filter->name);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
filter->name = g_strdup (name);
|
2018-10-07 21:22:04 +00:00
|
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (filter), props[PROP_NAME]);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* gtk_file_filter_get_name: (attributes org.gtk.Method.get_property=name)
|
|
|
|
|
* @filter: a `GtkFileFilter`
|
|
|
|
|
*
|
|
|
|
|
* Gets the human-readable name for the filter.
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* See [method@Gtk.FileFilter.set_name].
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2021-05-21 00:45:06 +00:00
|
|
|
|
* Returns: (nullable): The human-readable name of the filter
|
2021-03-01 06:37:23 +00:00
|
|
|
|
*/
|
2020-07-05 23:36:45 +00:00
|
|
|
|
const char *
|
2003-07-23 15:31:10 +00:00
|
|
|
|
gtk_file_filter_get_name (GtkFileFilter *filter)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (GTK_IS_FILE_FILTER (filter), NULL);
|
2020-07-04 14:24:09 +00:00
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
return filter->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
file_filter_add_rule (GtkFileFilter *filter,
|
2020-07-05 23:36:45 +00:00
|
|
|
|
FilterRule *rule)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
|
|
|
|
filter->rules = g_slist_append (filter->rules, rule);
|
2020-07-04 14:57:07 +00:00
|
|
|
|
|
|
|
|
|
gtk_filter_changed (GTK_FILTER (filter), GTK_FILTER_CHANGE_LESS_STRICT);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 18:24:21 +00:00
|
|
|
|
static void
|
|
|
|
|
file_filter_add_attribute (GtkFileFilter *filter,
|
|
|
|
|
const char *attribute)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (filter->attributes)
|
|
|
|
|
for (i = 0; filter->attributes[i]; i++)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (filter->attributes[i], attribute) == 0)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
|
|
filter->attributes = (char **)g_renew (char **, filter->attributes, i + 2);
|
|
|
|
|
filter->attributes[i] = g_strdup (attribute);
|
|
|
|
|
filter->attributes[i + 1] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_mime_type:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* @filter: A `GtkFileFilter`
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* @mime_type: name of a MIME type
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* Adds a rule allowing a given mime type to @filter.
|
2021-03-01 06:37:23 +00:00
|
|
|
|
*/
|
2003-07-23 15:31:10 +00:00
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_mime_type (GtkFileFilter *filter,
|
2020-07-05 23:36:45 +00:00
|
|
|
|
const char *mime_type)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_FILTER (filter));
|
|
|
|
|
g_return_if_fail (mime_type != NULL);
|
|
|
|
|
|
2008-07-04 14:27:40 +00:00
|
|
|
|
rule = g_slice_new (FilterRule);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
rule->type = FILTER_RULE_MIME_TYPE;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
rule->u.content_types = g_new0 (char *, 2);
|
|
|
|
|
rule->u.content_types[0] = g_content_type_from_mime_type (mime_type);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
2020-07-05 18:24:21 +00:00
|
|
|
|
file_filter_add_attribute (filter, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_pattern:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* @filter: a `GtkFileFilter`
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* @pattern: a shell style glob
|
2020-07-05 23:36:45 +00:00
|
|
|
|
*
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* Adds a rule allowing a shell style glob to a filter.
|
2021-06-04 03:55:48 +00:00
|
|
|
|
*
|
|
|
|
|
* Note that it depends on the platform whether pattern
|
|
|
|
|
* matching ignores case or not. On Windows, it does, on
|
|
|
|
|
* other platforms, it doesn't.
|
2021-03-01 06:37:23 +00:00
|
|
|
|
*/
|
2003-07-23 15:31:10 +00:00
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_pattern (GtkFileFilter *filter,
|
2020-07-05 23:36:45 +00:00
|
|
|
|
const char *pattern)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
2020-07-05 23:36:45 +00:00
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_FILTER (filter));
|
|
|
|
|
g_return_if_fail (pattern != NULL);
|
|
|
|
|
|
2008-07-04 14:27:40 +00:00
|
|
|
|
rule = g_slice_new (FilterRule);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
rule->type = FILTER_RULE_PATTERN;
|
|
|
|
|
rule->u.pattern = g_strdup (pattern);
|
|
|
|
|
|
2020-07-05 18:24:21 +00:00
|
|
|
|
file_filter_add_attribute (filter, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 03:55:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_suffix:
|
|
|
|
|
* @filter: a `GtkFileFilter`
|
|
|
|
|
* @suffix: filename suffix to match
|
|
|
|
|
*
|
|
|
|
|
* Adds a suffix match rule to a filter.
|
|
|
|
|
*
|
|
|
|
|
* This is similar to adding a match for the pattern
|
|
|
|
|
* "*.@suffix".
|
|
|
|
|
*
|
|
|
|
|
* In contrast to pattern matches, suffix matches
|
|
|
|
|
* are *always* case-insensitive.
|
2021-07-07 16:18:28 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 4.4
|
2021-06-04 03:55:48 +00:00
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_suffix (GtkFileFilter *filter,
|
|
|
|
|
const char *suffix)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_FILTER (filter));
|
|
|
|
|
g_return_if_fail (suffix != NULL);
|
|
|
|
|
|
|
|
|
|
rule = g_slice_new (FilterRule);
|
|
|
|
|
rule->type = FILTER_RULE_SUFFIX;
|
|
|
|
|
rule->u.pattern = g_strconcat ("*.", suffix, NULL);
|
|
|
|
|
|
|
|
|
|
file_filter_add_attribute (filter, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
|
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-07 05:42:01 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_pixbuf_formats:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* @filter: a `GtkFileFilter`
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2004-11-07 05:42:01 +00:00
|
|
|
|
* Adds a rule allowing image files in the formats supported
|
|
|
|
|
* by GdkPixbuf.
|
2020-07-04 14:24:09 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* This is equivalent to calling [method@Gtk.FileFilter.add_mime_type]
|
2020-07-04 14:24:09 +00:00
|
|
|
|
* for all the supported mime types.
|
2021-03-01 06:37:23 +00:00
|
|
|
|
*/
|
2004-11-07 05:42:01 +00:00
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_pixbuf_formats (GtkFileFilter *filter)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
GPtrArray *array;
|
|
|
|
|
GSList *formats, *l;
|
|
|
|
|
|
2004-11-07 05:42:01 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_FILTER (filter));
|
|
|
|
|
|
2008-07-04 14:27:40 +00:00
|
|
|
|
rule = g_slice_new (FilterRule);
|
2004-11-07 05:42:01 +00:00
|
|
|
|
rule->type = FILTER_RULE_PIXBUF_FORMATS;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
|
|
|
|
|
array = g_ptr_array_new ();
|
|
|
|
|
|
|
|
|
|
formats = gdk_pixbuf_get_formats ();
|
|
|
|
|
for (l = formats; l; l = l->next)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char **mime_types;
|
|
|
|
|
|
|
|
|
|
mime_types = gdk_pixbuf_format_get_mime_types (l->data);
|
|
|
|
|
|
|
|
|
|
for (i = 0; mime_types[i] != NULL; i++)
|
|
|
|
|
{
|
|
|
|
|
g_ptr_array_add (array, g_content_type_from_mime_type (mime_types[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_slist_free (formats);
|
|
|
|
|
|
|
|
|
|
g_ptr_array_add (array, NULL);
|
|
|
|
|
|
|
|
|
|
rule->u.content_types = (char **)g_ptr_array_free (array, FALSE);
|
2020-07-05 18:24:21 +00:00
|
|
|
|
|
|
|
|
|
file_filter_add_attribute (filter, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
|
2004-11-07 05:42:01 +00:00
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
/**
|
2020-07-05 18:24:21 +00:00
|
|
|
|
* gtk_file_filter_get_attributes:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* @filter: a `GtkFileFilter`
|
2020-07-05 23:36:45 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Gets the attributes that need to be filled in for the `GFileInfo`
|
2020-08-05 01:17:49 +00:00
|
|
|
|
* passed to this filter.
|
2020-07-05 23:36:45 +00:00
|
|
|
|
*
|
|
|
|
|
* This function will not typically be used by applications;
|
|
|
|
|
* it is intended principally for use in the implementation
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* of `GtkFileChooser`.
|
2020-07-05 23:36:45 +00:00
|
|
|
|
*
|
2020-07-05 18:24:21 +00:00
|
|
|
|
* Returns: (transfer none): the attributes
|
2021-03-01 06:37:23 +00:00
|
|
|
|
*/
|
2020-07-05 18:24:21 +00:00
|
|
|
|
const char **
|
|
|
|
|
gtk_file_filter_get_attributes (GtkFileFilter *filter)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
2020-07-05 18:24:21 +00:00
|
|
|
|
return (const char **)filter->attributes;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-10 03:08:23 +00:00
|
|
|
|
#ifdef GDK_WINDOWING_MACOS
|
2017-07-09 16:06:12 +00:00
|
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
2017-07-23 08:37:26 +00:00
|
|
|
|
NSArray * _gtk_file_filter_get_as_pattern_nsstrings (GtkFileFilter *filter)
|
2017-07-09 16:06:12 +00:00
|
|
|
|
{
|
2017-07-23 08:37:26 +00:00
|
|
|
|
NSMutableArray *array = [[NSMutableArray alloc] init];
|
2017-07-09 16:06:12 +00:00
|
|
|
|
GSList *tmp_list;
|
|
|
|
|
|
|
|
|
|
for (tmp_list = filter->rules; tmp_list; tmp_list = tmp_list->next)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule = tmp_list->data;
|
|
|
|
|
|
|
|
|
|
switch (rule->type)
|
2020-07-05 19:07:36 +00:00
|
|
|
|
{
|
|
|
|
|
case FILTER_RULE_MIME_TYPE:
|
|
|
|
|
{
|
|
|
|
|
// convert mime-types to UTI
|
|
|
|
|
NSString *mime_type_nsstring = [NSString stringWithUTF8String: rule->u.content_types[0]];
|
|
|
|
|
NSString *uti_nsstring = (NSString *) UTTypeCreatePreferredIdentifierForTag (kUTTagClassMIMEType, (CFStringRef) mime_type_nsstring, NULL);
|
|
|
|
|
if (uti_nsstring == NULL)
|
|
|
|
|
{
|
|
|
|
|
[array release];
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
[array addObject:uti_nsstring];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FILTER_RULE_PATTERN:
|
2021-06-04 03:55:48 +00:00
|
|
|
|
case FILTER_RULE_SUFFIX:
|
2020-07-05 19:07:36 +00:00
|
|
|
|
{
|
|
|
|
|
// patterns will need to be stripped of their leading *.
|
|
|
|
|
GString *pattern = g_string_new (rule->u.pattern);
|
|
|
|
|
if (strncmp (pattern->str, "*.", 2) == 0)
|
|
|
|
|
{
|
|
|
|
|
pattern = g_string_erase (pattern, 0, 2);
|
|
|
|
|
}
|
|
|
|
|
else if (strncmp (pattern->str, "*", 1) == 0)
|
|
|
|
|
{
|
|
|
|
|
pattern = g_string_erase (pattern, 0, 1);
|
|
|
|
|
}
|
2020-07-24 18:40:36 +00:00
|
|
|
|
char *pattern_c = g_string_free (pattern, FALSE);
|
2020-07-05 19:07:36 +00:00
|
|
|
|
NSString *pattern_nsstring = [NSString stringWithUTF8String:pattern_c];
|
|
|
|
|
g_free (pattern_c);
|
|
|
|
|
[pattern_nsstring retain];
|
|
|
|
|
[array addObject:pattern_nsstring];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FILTER_RULE_PIXBUF_FORMATS:
|
|
|
|
|
{
|
|
|
|
|
GSList *formats, *l;
|
|
|
|
|
|
|
|
|
|
formats = gdk_pixbuf_get_formats ();
|
|
|
|
|
for (l = formats; l; l = l->next)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2020-07-24 18:40:36 +00:00
|
|
|
|
char **extensions;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
|
|
|
|
|
extensions = gdk_pixbuf_format_get_extensions (l->data);
|
|
|
|
|
|
|
|
|
|
for (i = 0; extensions[i] != NULL; i++)
|
|
|
|
|
{
|
|
|
|
|
NSString *extension = [NSString stringWithUTF8String: extensions[i]];
|
|
|
|
|
[extension retain];
|
|
|
|
|
[array addObject:extension];
|
|
|
|
|
}
|
|
|
|
|
g_strfreev (extensions);
|
|
|
|
|
}
|
|
|
|
|
g_slist_free (formats);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-09 16:06:12 +00:00
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-11-03 13:52:42 +00:00
|
|
|
|
char **
|
2020-07-05 23:36:45 +00:00
|
|
|
|
_gtk_file_filter_get_as_patterns (GtkFileFilter *filter)
|
2015-11-03 13:52:42 +00:00
|
|
|
|
{
|
|
|
|
|
GPtrArray *array;
|
|
|
|
|
GSList *tmp_list;
|
|
|
|
|
|
|
|
|
|
array = g_ptr_array_new_with_free_func (g_free);
|
|
|
|
|
|
|
|
|
|
for (tmp_list = filter->rules; tmp_list; tmp_list = tmp_list->next)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule = tmp_list->data;
|
|
|
|
|
|
|
|
|
|
switch (rule->type)
|
2020-07-05 19:07:36 +00:00
|
|
|
|
{
|
|
|
|
|
case FILTER_RULE_MIME_TYPE:
|
2015-11-03 13:52:42 +00:00
|
|
|
|
g_ptr_array_free (array, TRUE);
|
|
|
|
|
return NULL;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FILTER_RULE_PATTERN:
|
2021-06-04 03:55:48 +00:00
|
|
|
|
case FILTER_RULE_SUFFIX:
|
|
|
|
|
/* Note: we don't make the suffix pattern explicitly
|
|
|
|
|
* case-insensitive, since this is only used on Windows
|
|
|
|
|
*/
|
2015-11-03 13:52:42 +00:00
|
|
|
|
g_ptr_array_add (array, g_strdup (rule->u.pattern));
|
2020-07-05 19:07:36 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FILTER_RULE_PIXBUF_FORMATS:
|
|
|
|
|
{
|
|
|
|
|
GSList *formats, *l;
|
2015-11-03 13:52:42 +00:00
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
formats = gdk_pixbuf_get_formats ();
|
|
|
|
|
for (l = formats; l; l = l->next)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char **extensions;
|
2015-11-03 13:52:42 +00:00
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
extensions = gdk_pixbuf_format_get_extensions (l->data);
|
2015-11-03 13:52:42 +00:00
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
for (i = 0; extensions[i] != NULL; i++)
|
2015-11-03 13:52:42 +00:00
|
|
|
|
g_ptr_array_add (array, g_strdup_printf ("*.%s", extensions[i]));
|
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
g_strfreev (extensions);
|
|
|
|
|
}
|
|
|
|
|
g_slist_free (formats);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-10-06 19:19:42 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
}
|
2015-11-03 13:52:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_ptr_array_add (array, NULL); /* Null terminate */
|
|
|
|
|
return (char **)g_ptr_array_free (array, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 14:57:07 +00:00
|
|
|
|
static GtkFilterMatch
|
|
|
|
|
gtk_file_filter_get_strictness (GtkFilter *filter)
|
|
|
|
|
{
|
|
|
|
|
GtkFileFilter *file_filter = GTK_FILE_FILTER (filter);
|
|
|
|
|
|
|
|
|
|
/* Handle only the documented cases for 'match all'
|
|
|
|
|
* and match none. There are of course other ways to
|
|
|
|
|
* make filters that do this.
|
|
|
|
|
*/
|
|
|
|
|
if (file_filter->rules == NULL)
|
|
|
|
|
return GTK_FILTER_MATCH_NONE;
|
|
|
|
|
|
|
|
|
|
if (file_filter->rules->next == NULL)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule = file_filter->rules->data;
|
|
|
|
|
if (rule->type == FILTER_RULE_PATTERN &&
|
|
|
|
|
strcmp (rule->u.pattern, "*") == 0)
|
|
|
|
|
return GTK_FILTER_MATCH_ALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GTK_FILTER_MATCH_SOME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_filter_match (GtkFilter *filter,
|
|
|
|
|
gpointer item)
|
|
|
|
|
{
|
2020-07-04 15:06:36 +00:00
|
|
|
|
GtkFileFilter *file_filter = GTK_FILE_FILTER (filter);
|
|
|
|
|
GFileInfo *info = item;
|
|
|
|
|
GSList *tmp_list;
|
|
|
|
|
|
2020-07-04 14:57:07 +00:00
|
|
|
|
if (!G_IS_FILE_INFO (item))
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
2020-07-04 15:06:36 +00:00
|
|
|
|
for (tmp_list = file_filter->rules; tmp_list; tmp_list = tmp_list->next)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
|
|
|
|
FilterRule *rule = tmp_list->data;
|
2021-06-04 03:55:48 +00:00
|
|
|
|
gboolean ignore_case = FALSE;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
|
|
|
|
switch (rule->type)
|
2020-07-04 03:58:07 +00:00
|
|
|
|
{
|
2021-06-04 03:55:48 +00:00
|
|
|
|
case FILTER_RULE_SUFFIX:
|
|
|
|
|
ignore_case = TRUE;
|
|
|
|
|
G_GNUC_FALLTHROUGH;
|
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
case FILTER_RULE_PATTERN:
|
2020-07-04 03:58:07 +00:00
|
|
|
|
{
|
2020-07-05 19:07:36 +00:00
|
|
|
|
const char *display_name;
|
2020-07-04 03:58:07 +00:00
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
display_name = g_file_info_get_display_name (info);
|
|
|
|
|
if (display_name)
|
2020-07-04 03:58:07 +00:00
|
|
|
|
{
|
2021-06-04 03:55:48 +00:00
|
|
|
|
if (_gtk_fnmatch (rule->u.pattern, display_name, FALSE, ignore_case))
|
2020-07-04 03:58:07 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2004-11-07 05:42:01 +00:00
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
case FILTER_RULE_MIME_TYPE:
|
|
|
|
|
case FILTER_RULE_PIXBUF_FORMATS:
|
2020-07-04 03:58:07 +00:00
|
|
|
|
{
|
2020-07-05 19:07:36 +00:00
|
|
|
|
const char *filter_content_type;
|
2020-07-04 03:58:07 +00:00
|
|
|
|
|
2020-07-05 19:07:36 +00:00
|
|
|
|
filter_content_type = g_file_info_get_content_type (info);
|
|
|
|
|
if (filter_content_type)
|
2020-07-04 03:58:07 +00:00
|
|
|
|
{
|
2020-07-05 19:07:36 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; rule->u.content_types[i]; i++)
|
|
|
|
|
{
|
|
|
|
|
if (g_content_type_is_a (filter_content_type, rule->u.content_types[i]))
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2020-07-04 03:58:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2004-11-07 05:42:01 +00:00
|
|
|
|
|
2017-10-06 19:19:42 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2020-07-04 03:58:07 +00:00
|
|
|
|
}
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2016-06-11 04:45:27 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_to_gvariant:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* @filter: a `GtkFileFilter`
|
2016-06-11 04:45:27 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Serialize a file filter to an `a{sv}` variant.
|
2016-06-11 04:45:27 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Returns: (transfer none): a new, floating, `GVariant`
|
2016-06-11 04:45:27 +00:00
|
|
|
|
*/
|
|
|
|
|
GVariant *
|
|
|
|
|
gtk_file_filter_to_gvariant (GtkFileFilter *filter)
|
|
|
|
|
{
|
|
|
|
|
GVariantBuilder builder;
|
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
|
|
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(us)"));
|
|
|
|
|
for (l = filter->rules; l; l = l->next)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule = l->data;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
int i;
|
2016-06-11 04:45:27 +00:00
|
|
|
|
|
|
|
|
|
switch (rule->type)
|
|
|
|
|
{
|
|
|
|
|
case FILTER_RULE_PATTERN:
|
|
|
|
|
g_variant_builder_add (&builder, "(us)", 0, rule->u.pattern);
|
|
|
|
|
break;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
|
2021-06-04 03:55:48 +00:00
|
|
|
|
case FILTER_RULE_SUFFIX:
|
|
|
|
|
{
|
|
|
|
|
/* Tweak the glob, since the filechooser portal has no api
|
|
|
|
|
* for case-insensitive globs
|
|
|
|
|
*/
|
|
|
|
|
char *pattern = _gtk_make_ci_glob_pattern (rule->u.pattern);
|
|
|
|
|
g_variant_builder_add (&builder, "(us)", 0, pattern);
|
|
|
|
|
g_free (pattern);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2016-06-11 04:45:27 +00:00
|
|
|
|
case FILTER_RULE_MIME_TYPE:
|
|
|
|
|
case FILTER_RULE_PIXBUF_FORMATS:
|
2020-07-05 19:07:36 +00:00
|
|
|
|
for (i = 0; rule->u.content_types[i]; i++)
|
|
|
|
|
g_variant_builder_add (&builder, "(us)", 1, rule->u.content_types[i]);
|
2016-06-11 04:45:27 +00:00
|
|
|
|
break;
|
2020-07-05 19:07:36 +00:00
|
|
|
|
|
2016-06-11 04:45:27 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return g_variant_new ("(s@a(us))", filter->name, g_variant_builder_end (&builder));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_new_from_gvariant:
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* @variant: an `a{sv}` `GVariant`
|
|
|
|
|
*
|
|
|
|
|
* Deserialize a file filter from a `GVariant`.
|
2016-06-11 04:45:27 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* The variant must be in the format produced by
|
|
|
|
|
* [method@Gtk.FileFilter.to_gvariant].
|
2016-06-11 04:45:27 +00:00
|
|
|
|
*
|
2021-03-01 06:37:23 +00:00
|
|
|
|
* Returns: (transfer full): a new `GtkFileFilter` object
|
2016-06-11 04:45:27 +00:00
|
|
|
|
*/
|
|
|
|
|
GtkFileFilter *
|
|
|
|
|
gtk_file_filter_new_from_gvariant (GVariant *variant)
|
|
|
|
|
{
|
|
|
|
|
GtkFileFilter *filter;
|
|
|
|
|
GVariantIter *iter;
|
|
|
|
|
const char *name;
|
|
|
|
|
int type;
|
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
|
|
|
|
|
|
g_variant_get (variant, "(&sa(us))", &name, &iter);
|
|
|
|
|
|
|
|
|
|
gtk_file_filter_set_name (filter, name);
|
|
|
|
|
|
|
|
|
|
while (g_variant_iter_next (iter, "(u&s)", &type, &tmp))
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
gtk_file_filter_add_pattern (filter, tmp);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
gtk_file_filter_add_mime_type (filter, tmp);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_variant_iter_free (iter);
|
|
|
|
|
|
|
|
|
|
return filter;
|
|
|
|
|
}
|