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
|
|
|
|
/**
|
|
|
|
|
* SECTION:gtkfilefilter
|
|
|
|
|
* @Short_description: A filter for selecting a file subset
|
|
|
|
|
* @Title: GtkFileFilter
|
2012-04-13 00:07:28 +00:00
|
|
|
|
* @see_also: #GtkFileChooser
|
2011-01-15 13:50:50 +00:00
|
|
|
|
*
|
|
|
|
|
* A GtkFileFilter can be used to restrict the files being shown in a
|
|
|
|
|
* #GtkFileChooser. Files can be filtered based on their name (with
|
|
|
|
|
* gtk_file_filter_add_pattern()), on their mime type (with
|
|
|
|
|
* gtk_file_filter_add_mime_type()), or by a custom filter function
|
|
|
|
|
* (with gtk_file_filter_add_custom()).
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
|
* Normally, filters are used by adding them to a #GtkFileChooser,
|
|
|
|
|
* see gtk_file_chooser_add_filter(), but it is also possible
|
|
|
|
|
* to manually use a filter on a file with gtk_file_filter_filter().
|
|
|
|
|
*
|
2014-02-05 02:00:58 +00:00
|
|
|
|
* # GtkFileFilter as GtkBuildable
|
2014-02-02 05:29:00 +00:00
|
|
|
|
*
|
2011-01-27 07:30:15 +00:00
|
|
|
|
* The GtkFileFilter implementation of the GtkBuildable interface
|
2014-02-09 22:24:06 +00:00
|
|
|
|
* supports adding rules using the <mime-types>, <patterns> and
|
|
|
|
|
* <applications> elements and listing the rules within. Specifying
|
|
|
|
|
* a <mime-type> or <pattern> has the same effect as as calling
|
2014-05-13 15:55:57 +00:00
|
|
|
|
* gtk_file_filter_add_mime_type() or gtk_file_filter_add_pattern().
|
2011-01-27 07:30:15 +00:00
|
|
|
|
*
|
2014-02-02 05:29:00 +00:00
|
|
|
|
* An example of a UI definition fragment specifying GtkFileFilter
|
|
|
|
|
* rules:
|
2014-01-27 17:12:55 +00:00
|
|
|
|
* |[
|
2011-01-27 07:30:15 +00:00
|
|
|
|
* <object class="GtkFileFilter">
|
|
|
|
|
* <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>
|
|
|
|
|
* <pattern>*.png</pattern>
|
|
|
|
|
* </patterns>
|
|
|
|
|
* </object>
|
2014-01-27 17:12:55 +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>
|
|
|
|
|
|
2003-10-23 04:22:32 +00:00
|
|
|
|
#include "gtkfilefilter.h"
|
2011-01-27 07:30:15 +00:00
|
|
|
|
#include "gtkbuildable.h"
|
2005-09-01 05:11:46 +00:00
|
|
|
|
#include "gtkintl.h"
|
2003-10-27 23:50:34 +00:00
|
|
|
|
#include "gtkprivate.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,
|
2004-11-07 05:42:01 +00:00
|
|
|
|
FILTER_RULE_PIXBUF_FORMATS,
|
2004-01-22 02:39:31 +00:00
|
|
|
|
FILTER_RULE_CUSTOM
|
2003-07-23 15:31:10 +00:00
|
|
|
|
} FilterRuleType;
|
|
|
|
|
|
|
|
|
|
struct _GtkFileFilterClass
|
|
|
|
|
{
|
2010-09-18 23:54:31 +00:00
|
|
|
|
GInitiallyUnownedClass parent_class;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _GtkFileFilter
|
|
|
|
|
{
|
2010-09-18 23:54:31 +00:00
|
|
|
|
GInitiallyUnowned parent_instance;
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
gchar *name;
|
|
|
|
|
GSList *rules;
|
|
|
|
|
|
|
|
|
|
GtkFileFilterFlags needed;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _FilterRule
|
|
|
|
|
{
|
|
|
|
|
FilterRuleType type;
|
|
|
|
|
GtkFileFilterFlags needed;
|
|
|
|
|
|
|
|
|
|
union {
|
|
|
|
|
gchar *pattern;
|
|
|
|
|
gchar *mime_type;
|
2004-11-07 05:42:01 +00:00
|
|
|
|
GSList *pixbuf_formats;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
struct {
|
|
|
|
|
GtkFileFilterFunc func;
|
|
|
|
|
gpointer data;
|
|
|
|
|
GDestroyNotify notify;
|
|
|
|
|
} custom;
|
|
|
|
|
} u;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void gtk_file_filter_finalize (GObject *object);
|
|
|
|
|
|
|
|
|
|
|
2011-01-27 07:30:15 +00:00
|
|
|
|
static void gtk_file_filter_buildable_init (GtkBuildableIface *iface);
|
2015-03-08 13:10:58 +00:00
|
|
|
|
static void gtk_file_filter_buildable_set_name (GtkBuildable *buildable,
|
|
|
|
|
const gchar *name);
|
|
|
|
|
static const gchar* gtk_file_filter_buildable_get_name (GtkBuildable *buildable);
|
|
|
|
|
|
|
|
|
|
|
2011-01-27 07:30:15 +00:00
|
|
|
|
static gboolean gtk_file_filter_buildable_custom_tag_start (GtkBuildable *buildable,
|
|
|
|
|
GtkBuilder *builder,
|
|
|
|
|
GObject *child,
|
|
|
|
|
const gchar *tagname,
|
|
|
|
|
GMarkupParser *parser,
|
|
|
|
|
gpointer *data);
|
|
|
|
|
static void gtk_file_filter_buildable_custom_tag_end (GtkBuildable *buildable,
|
|
|
|
|
GtkBuilder *builder,
|
|
|
|
|
GObject *child,
|
|
|
|
|
const gchar *tagname,
|
|
|
|
|
gpointer *data);
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkFileFilter, gtk_file_filter, G_TYPE_INITIALLY_UNOWNED,
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_class_init (GtkFileFilterClass *class)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
|
|
gobject_class->finalize = gtk_file_filter_finalize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
filter_rule_free (FilterRule *rule)
|
|
|
|
|
{
|
|
|
|
|
switch (rule->type)
|
|
|
|
|
{
|
|
|
|
|
case FILTER_RULE_MIME_TYPE:
|
|
|
|
|
g_free (rule->u.mime_type);
|
|
|
|
|
break;
|
|
|
|
|
case FILTER_RULE_PATTERN:
|
|
|
|
|
g_free (rule->u.pattern);
|
|
|
|
|
break;
|
|
|
|
|
case FILTER_RULE_CUSTOM:
|
|
|
|
|
if (rule->u.custom.notify)
|
|
|
|
|
rule->u.custom.notify (rule->u.custom.data);
|
|
|
|
|
break;
|
2004-11-07 05:42:01 +00:00
|
|
|
|
case FILTER_RULE_PIXBUF_FORMATS:
|
|
|
|
|
g_slist_free (rule->u.pixbuf_formats);
|
|
|
|
|
break;
|
2004-01-22 02:39:31 +00:00
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-07-04 14:27:40 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
g_slist_foreach (filter->rules, (GFunc)filter_rule_free, NULL);
|
2004-01-22 02:39:31 +00:00
|
|
|
|
g_slist_free (filter->rules);
|
|
|
|
|
|
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
|
|
|
|
/*
|
|
|
|
|
* GtkBuildable implementation
|
|
|
|
|
*/
|
|
|
|
|
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;
|
2015-03-08 13:10:58 +00:00
|
|
|
|
iface->set_name = gtk_file_filter_buildable_set_name;
|
|
|
|
|
iface->get_name = gtk_file_filter_buildable_get_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_buildable_set_name (GtkBuildable *buildable,
|
|
|
|
|
const gchar *name)
|
|
|
|
|
{
|
|
|
|
|
gtk_file_filter_set_name (GTK_FILE_FILTER (buildable), name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
|
gtk_file_filter_buildable_get_name (GtkBuildable *buildable)
|
|
|
|
|
{
|
|
|
|
|
return gtk_file_filter_get_name (GTK_FILE_FILTER (buildable));
|
2011-01-27 07:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
PARSE_MIME_TYPES,
|
|
|
|
|
PARSE_PATTERNS
|
|
|
|
|
} ParserType;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
GtkFileFilter *filter;
|
|
|
|
|
ParserType type;
|
|
|
|
|
GString *string;
|
|
|
|
|
gboolean parsing;
|
|
|
|
|
} SubParserData;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
parser_start_element (GMarkupParseContext *context,
|
|
|
|
|
const gchar *element_name,
|
|
|
|
|
const gchar **names,
|
|
|
|
|
const gchar **values,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
SubParserData *parser_data = (SubParserData*)user_data;
|
|
|
|
|
|
|
|
|
|
if (strcmp (element_name, "mime-types") == 0)
|
|
|
|
|
return;
|
|
|
|
|
else if (strcmp (element_name, "mime-type") == 0)
|
|
|
|
|
{
|
|
|
|
|
parser_data->parsing = TRUE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (element_name, "patterns") == 0)
|
|
|
|
|
return;
|
|
|
|
|
else if (strcmp (element_name, "pattern") == 0)
|
|
|
|
|
{
|
|
|
|
|
parser_data->parsing = TRUE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
g_warning ("Unsupported tag for GtkFileFilter: %s\n", element_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
parser_text_element (GMarkupParseContext *context,
|
|
|
|
|
const gchar *text,
|
|
|
|
|
gsize text_len,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
SubParserData *parser_data = (SubParserData*)user_data;
|
|
|
|
|
|
|
|
|
|
if (parser_data->parsing)
|
|
|
|
|
g_string_append_len (parser_data->string, text, text_len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
parser_end_element (GMarkupParseContext *context,
|
|
|
|
|
const gchar *element_name,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
SubParserData *parser_data = (SubParserData*)user_data;
|
|
|
|
|
|
|
|
|
|
if (parser_data->string)
|
|
|
|
|
{
|
|
|
|
|
switch (parser_data->type)
|
|
|
|
|
{
|
|
|
|
|
case PARSE_MIME_TYPES:
|
|
|
|
|
gtk_file_filter_add_mime_type (parser_data->filter, parser_data->string->str);
|
|
|
|
|
break;
|
|
|
|
|
case PARSE_PATTERNS:
|
|
|
|
|
gtk_file_filter_add_pattern (parser_data->filter, parser_data->string->str);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_string_set_size (parser_data->string, 0);
|
|
|
|
|
parser_data->parsing = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const GMarkupParser sub_parser =
|
|
|
|
|
{
|
|
|
|
|
parser_start_element,
|
|
|
|
|
parser_end_element,
|
|
|
|
|
parser_text_element,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_filter_buildable_custom_tag_start (GtkBuildable *buildable,
|
|
|
|
|
GtkBuilder *builder,
|
|
|
|
|
GObject *child,
|
|
|
|
|
const gchar *tagname,
|
|
|
|
|
GMarkupParser *parser,
|
|
|
|
|
gpointer *data)
|
|
|
|
|
{
|
|
|
|
|
SubParserData *parser_data = NULL;
|
|
|
|
|
|
|
|
|
|
if (strcmp (tagname, "mime-types") == 0)
|
|
|
|
|
{
|
|
|
|
|
parser_data = g_slice_new0 (SubParserData);
|
|
|
|
|
parser_data->string = g_string_new ("");
|
|
|
|
|
parser_data->type = PARSE_MIME_TYPES;
|
|
|
|
|
parser_data->filter = GTK_FILE_FILTER (buildable);
|
|
|
|
|
|
|
|
|
|
*parser = sub_parser;
|
|
|
|
|
*data = parser_data;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp (tagname, "patterns") == 0)
|
|
|
|
|
{
|
|
|
|
|
parser_data = g_slice_new0 (SubParserData);
|
|
|
|
|
parser_data->string = g_string_new ("");
|
|
|
|
|
parser_data->type = PARSE_PATTERNS;
|
|
|
|
|
parser_data->filter = GTK_FILE_FILTER (buildable);
|
|
|
|
|
|
|
|
|
|
*parser = sub_parser;
|
|
|
|
|
*data = parser_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parser_data != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_filter_buildable_custom_tag_end (GtkBuildable *buildable,
|
|
|
|
|
GtkBuilder *builder,
|
|
|
|
|
GObject *child,
|
|
|
|
|
const gchar *tagname,
|
|
|
|
|
gpointer *data)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (tagname, "mime-types") == 0 ||
|
|
|
|
|
strcmp (tagname, "patterns") == 0)
|
|
|
|
|
{
|
|
|
|
|
SubParserData *parser_data = (SubParserData*)data;
|
|
|
|
|
|
|
|
|
|
g_string_free (parser_data->string, TRUE);
|
|
|
|
|
g_slice_free (SubParserData, parser_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_new:
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GtkFileFilter with no rules added to it.
|
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
|
|
|
|
|
* gtk_file_filter_add_mime_type(), gtk_file_filter_add_pattern(),
|
|
|
|
|
* or gtk_file_filter_add_custom(). To create a filter
|
|
|
|
|
* that accepts any file, use:
|
2014-01-27 19:55:18 +00:00
|
|
|
|
* |[<!-- language="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, "*");
|
2007-11-25 06:51:19 +00:00
|
|
|
|
* ]|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: a new #GtkFileFilter
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
|
|
|
|
GtkFileFilter *
|
|
|
|
|
gtk_file_filter_new (void)
|
|
|
|
|
{
|
|
|
|
|
return g_object_new (GTK_TYPE_FILE_FILTER, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_set_name:
|
|
|
|
|
* @filter: a #GtkFileFilter
|
2010-02-19 16:53:17 +00:00
|
|
|
|
* @name: (allow-none): the human-readable-name for the filter, or %NULL
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* to remove any existing name.
|
|
|
|
|
*
|
|
|
|
|
* Sets the human-readable name of the filter; this is the string
|
|
|
|
|
* that will be displayed in the file selector user interface if
|
|
|
|
|
* there is a selectable list of filters.
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_set_name (GtkFileFilter *filter,
|
|
|
|
|
const gchar *name)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_FILTER (filter));
|
|
|
|
|
|
2007-03-09 21:57:37 +00:00
|
|
|
|
g_free (filter->name);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
|
|
|
|
filter->name = g_strdup (name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_get_name:
|
|
|
|
|
* @filter: a #GtkFileFilter
|
|
|
|
|
*
|
|
|
|
|
* Gets the human-readable name for the filter. See gtk_file_filter_set_name().
|
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: The human-readable name of the filter,
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* or %NULL. This value is owned by GTK+ and must not
|
|
|
|
|
* be modified or freed.
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
2011-06-06 18:13:44 +00:00
|
|
|
|
const gchar *
|
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);
|
|
|
|
|
|
|
|
|
|
return filter->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
file_filter_add_rule (GtkFileFilter *filter,
|
|
|
|
|
FilterRule *rule)
|
|
|
|
|
{
|
|
|
|
|
filter->needed |= rule->needed;
|
|
|
|
|
filter->rules = g_slist_append (filter->rules, rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_mime_type:
|
|
|
|
|
* @filter: A #GtkFileFilter
|
|
|
|
|
* @mime_type: name of a MIME type
|
|
|
|
|
*
|
|
|
|
|
* Adds a rule allowing a given mime type to @filter.
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_mime_type (GtkFileFilter *filter,
|
|
|
|
|
const gchar *mime_type)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
rule->needed = GTK_FILE_FILTER_MIME_TYPE;
|
|
|
|
|
rule->u.mime_type = g_strdup (mime_type);
|
|
|
|
|
|
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_pattern:
|
|
|
|
|
* @filter: a #GtkFileFilter
|
|
|
|
|
* @pattern: a shell style glob
|
|
|
|
|
*
|
|
|
|
|
* Adds a rule allowing a shell style glob to a filter.
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_pattern (GtkFileFilter *filter,
|
|
|
|
|
const gchar *pattern)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
|
|
|
|
|
|
|
|
|
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->needed = GTK_FILE_FILTER_DISPLAY_NAME;
|
|
|
|
|
rule->u.pattern = g_strdup (pattern);
|
|
|
|
|
|
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-07 05:42:01 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_pixbuf_formats:
|
|
|
|
|
* @filter: a #GtkFileFilter
|
|
|
|
|
*
|
|
|
|
|
* Adds a rule allowing image files in the formats supported
|
|
|
|
|
* by GdkPixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.6
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_pixbuf_formats (GtkFileFilter *filter)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
rule->needed = GTK_FILE_FILTER_MIME_TYPE;
|
|
|
|
|
rule->u.pixbuf_formats = gdk_pixbuf_get_formats ();
|
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_add_custom:
|
|
|
|
|
* @filter: a #GtkFileFilter
|
|
|
|
|
* @needed: bitfield of flags indicating the information that the custom
|
|
|
|
|
* filter function needs.
|
|
|
|
|
* @func: callback function; if the function returns %TRUE, then
|
|
|
|
|
* the file will be displayed.
|
|
|
|
|
* @data: data to pass to @func
|
|
|
|
|
* @notify: function to call to free @data when it is no longer needed.
|
|
|
|
|
*
|
|
|
|
|
* Adds rule to a filter that allows files based on a custom callback
|
|
|
|
|
* function. The bitfield @needed which is passed in provides information
|
|
|
|
|
* about what sorts of information that the filter function needs;
|
|
|
|
|
* this allows GTK+ to avoid retrieving expensive information when
|
2014-02-07 18:32:47 +00:00
|
|
|
|
* it isn’t needed by the filter.
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
gtk_file_filter_add_custom (GtkFileFilter *filter,
|
|
|
|
|
GtkFileFilterFlags needed,
|
|
|
|
|
GtkFileFilterFunc func,
|
|
|
|
|
gpointer data,
|
|
|
|
|
GDestroyNotify notify)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_FILTER (filter));
|
|
|
|
|
g_return_if_fail (func != 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_CUSTOM;
|
|
|
|
|
rule->needed = needed;
|
|
|
|
|
rule->u.custom.func = func;
|
|
|
|
|
rule->u.custom.data = data;
|
|
|
|
|
rule->u.custom.notify = notify;
|
|
|
|
|
|
|
|
|
|
file_filter_add_rule (filter, rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_get_needed:
|
|
|
|
|
* @filter: a #GtkFileFilter
|
|
|
|
|
*
|
2014-01-27 22:04:46 +00:00
|
|
|
|
* Gets the fields that need to be filled in for the #GtkFileFilterInfo
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* passed to gtk_file_filter_filter()
|
|
|
|
|
*
|
|
|
|
|
* This function will not typically be used by applications; it
|
|
|
|
|
* is intended principally for use in the implementation of
|
|
|
|
|
* #GtkFileChooser.
|
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: bitfield of flags indicating needed fields when
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* calling gtk_file_filter_filter()
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
|
|
|
|
GtkFileFilterFlags
|
|
|
|
|
gtk_file_filter_get_needed (GtkFileFilter *filter)
|
|
|
|
|
{
|
|
|
|
|
return filter->needed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_file_filter_filter:
|
|
|
|
|
* @filter: a #GtkFileFilter
|
2014-01-27 22:04:46 +00:00
|
|
|
|
* @filter_info: a #GtkFileFilterInfo containing information
|
2003-07-23 15:31:10 +00:00
|
|
|
|
* about a file.
|
|
|
|
|
*
|
|
|
|
|
* Tests whether a file should be displayed according to @filter.
|
2014-01-27 22:04:46 +00:00
|
|
|
|
* The #GtkFileFilterInfo @filter_info should include
|
2004-11-12 22:18:04 +00:00
|
|
|
|
* the fields returned from gtk_file_filter_get_needed().
|
2003-07-23 15:31:10 +00:00
|
|
|
|
*
|
|
|
|
|
* This function will not typically be used by applications; it
|
|
|
|
|
* is intended principally for use in the implementation of
|
|
|
|
|
* #GtkFileChooser.
|
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: %TRUE if the file should be displayed
|
2003-10-23 21:22:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2003-07-23 15:31:10 +00:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
gtk_file_filter_filter (GtkFileFilter *filter,
|
|
|
|
|
const GtkFileFilterInfo *filter_info)
|
|
|
|
|
{
|
|
|
|
|
GSList *tmp_list;
|
|
|
|
|
|
|
|
|
|
for (tmp_list = filter->rules; tmp_list; tmp_list = tmp_list->next)
|
|
|
|
|
{
|
|
|
|
|
FilterRule *rule = tmp_list->data;
|
|
|
|
|
|
|
|
|
|
if ((filter_info->contains & rule->needed) != rule->needed)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
switch (rule->type)
|
|
|
|
|
{
|
|
|
|
|
case FILTER_RULE_MIME_TYPE:
|
2008-06-08 05:50:46 +00:00
|
|
|
|
if (filter_info->mime_type != NULL &&
|
|
|
|
|
g_content_type_is_a (filter_info->mime_type, rule->u.mime_type))
|
2003-07-23 15:31:10 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
break;
|
|
|
|
|
case FILTER_RULE_PATTERN:
|
2004-03-01 19:48:28 +00:00
|
|
|
|
if (filter_info->display_name != NULL &&
|
2004-06-02 17:46:21 +00:00
|
|
|
|
_gtk_fnmatch (rule->u.pattern, filter_info->display_name, FALSE))
|
2003-07-23 15:31:10 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
break;
|
2004-11-07 05:42:01 +00:00
|
|
|
|
case FILTER_RULE_PIXBUF_FORMATS:
|
|
|
|
|
{
|
|
|
|
|
GSList *list;
|
|
|
|
|
|
2005-12-27 18:38:52 +00:00
|
|
|
|
if (!filter_info->mime_type)
|
|
|
|
|
break;
|
|
|
|
|
|
2004-11-07 05:42:01 +00:00
|
|
|
|
for (list = rule->u.pixbuf_formats; list; list = list->next)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
gchar **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_info->mime_type) == 0)
|
|
|
|
|
{
|
|
|
|
|
g_strfreev (mime_types);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_strfreev (mime_types);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-07-23 15:31:10 +00:00
|
|
|
|
case FILTER_RULE_CUSTOM:
|
|
|
|
|
if (rule->u.custom.func (filter_info, rule->u.custom.data))
|
|
|
|
|
return TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|