CtkFileFilter

CtkFileFilter — A filter for selecting a file subset

Functions

Types and Values

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── CtkFileFilter

Implemented Interfaces

CtkFileFilter implements CtkBuildable.

Includes

#include <ctk/ctk.h>

Description

A CtkFileFilter can be used to restrict the files being shown in a CtkFileChooser. Files can be filtered based on their name (with ctk_file_filter_add_pattern()), on their mime type (with ctk_file_filter_add_mime_type()), or by a custom filter function (with ctk_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 CtkFileFilter allows wildcards for the subtype of a mime type, so you can e.g. filter for image/*.

Normally, filters are used by adding them to a CtkFileChooser, see ctk_file_chooser_add_filter(), but it is also possible to manually use a filter on a file with ctk_file_filter_filter().

CtkFileFilter as CtkBuildable

The CtkFileFilter implementation of the CtkBuildable interface 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 ctk_file_filter_add_mime_type() or ctk_file_filter_add_pattern().

An example of a UI definition fragment specifying CtkFileFilter rules:

1
2
3
4
5
6
7
8
9
10
<object class="CtkFileFilter">
  <mime-types>
    <mime-type>text/plain</mime-type>
    <mime-type>image/ *</mime-type>
  </mime-types>
  <patterns>
    <pattern>*.txt</pattern>
    <pattern>*.png</pattern>
  </patterns>
</object>

Functions

CtkFileFilterFunc ()

gboolean
(*CtkFileFilterFunc) (const CtkFileFilterInfo *filter_info,
                      gpointer data);

The type of function that is used with custom filters, see ctk_file_filter_add_custom().

Parameters

filter_info

a CtkFileFilterInfo that is filled according to the needed flags passed to ctk_file_filter_add_custom()

 

data

user data passed to ctk_file_filter_add_custom().

[closure]

Returns

TRUE if the file should be displayed


ctk_file_filter_new ()

CtkFileFilter *
ctk_file_filter_new (void);

Creates a new CtkFileFilter with no rules added to it. Such a filter doesn’t accept any files, so is not particularly useful until you add rules with ctk_file_filter_add_mime_type(), ctk_file_filter_add_pattern(), or ctk_file_filter_add_custom(). To create a filter that accepts any file, use:

1
2
CtkFileFilter *filter = ctk_file_filter_new ();
ctk_file_filter_add_pattern (filter, "*");

Returns

a new CtkFileFilter

Since: 2.4


ctk_file_filter_set_name ()

void
ctk_file_filter_set_name (CtkFileFilter *filter,
                          const gchar *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.

Parameters

filter

a CtkFileFilter

 

name

the human-readable-name for the filter, or NULL to remove any existing name.

[allow-none]

Since: 2.4


ctk_file_filter_get_name ()

const gchar *
ctk_file_filter_get_name (CtkFileFilter *filter);

Gets the human-readable name for the filter. See ctk_file_filter_set_name().

Parameters

filter

a CtkFileFilter

 

Returns

The human-readable name of the filter, or NULL. This value is owned by CTK+ and must not be modified or freed.

[nullable]

Since: 2.4


ctk_file_filter_add_mime_type ()

void
ctk_file_filter_add_mime_type (CtkFileFilter *filter,
                               const gchar *mime_type);

Adds a rule allowing a given mime type to filter .

Parameters

filter

A CtkFileFilter

 

mime_type

name of a MIME type

 

Since: 2.4


ctk_file_filter_add_pattern ()

void
ctk_file_filter_add_pattern (CtkFileFilter *filter,
                             const gchar *pattern);

Adds a rule allowing a shell style glob to a filter.

Parameters

filter

a CtkFileFilter

 

pattern

a shell style glob

 

Since: 2.4


ctk_file_filter_add_pixbuf_formats ()

void
ctk_file_filter_add_pixbuf_formats (CtkFileFilter *filter);

Adds a rule allowing image files in the formats supported by GdkPixbuf.

Parameters

filter

a CtkFileFilter

 

Since: 2.6


ctk_file_filter_add_custom ()

void
ctk_file_filter_add_custom (CtkFileFilter *filter,
                            CtkFileFilterFlags needed,
                            CtkFileFilterFunc func,
                            gpointer data,
                            GDestroyNotify notify);

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 CTK+ to avoid retrieving expensive information when it isn’t needed by the filter.

Parameters

filter

a CtkFileFilter

 

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.

 

Since: 2.4


ctk_file_filter_get_needed ()

CtkFileFilterFlags
ctk_file_filter_get_needed (CtkFileFilter *filter);

Gets the fields that need to be filled in for the CtkFileFilterInfo passed to ctk_file_filter_filter()

This function will not typically be used by applications; it is intended principally for use in the implementation of CtkFileChooser.

Parameters

filter

a CtkFileFilter

 

Returns

bitfield of flags indicating needed fields when calling ctk_file_filter_filter()

Since: 2.4


ctk_file_filter_filter ()

gboolean
ctk_file_filter_filter (CtkFileFilter *filter,
                        const CtkFileFilterInfo *filter_info);

Tests whether a file should be displayed according to filter . The CtkFileFilterInfo filter_info should include the fields returned from ctk_file_filter_get_needed().

This function will not typically be used by applications; it is intended principally for use in the implementation of CtkFileChooser.

Parameters

filter

a CtkFileFilter

 

filter_info

a CtkFileFilterInfo containing information about a file.

 

Returns

TRUE if the file should be displayed

Since: 2.4


ctk_file_filter_new_from_gvariant ()

CtkFileFilter *
ctk_file_filter_new_from_gvariant (GVariant *variant);

Deserialize a file filter from an a{sv} variant in the format produced by ctk_file_filter_to_gvariant().

Parameters

variant

an a{sv} GVariant

 

Returns

a new CtkFileFilter object.

[transfer full]

Since: 3.22


ctk_file_filter_to_gvariant ()

GVariant *
ctk_file_filter_to_gvariant (CtkFileFilter *filter);

Serialize a file filter to an a{sv} variant.

Parameters

filter

a CtkFileFilter

 

Returns

a new, floating, GVariant.

[transfer none]

Since: 3.22

Types and Values

CtkFileFilter

typedef struct _CtkFileFilter CtkFileFilter;

struct CtkFileFilterInfo

struct CtkFileFilterInfo {
  CtkFileFilterFlags contains;

  const gchar *filename;
  const gchar *uri;
  const gchar *display_name;
  const gchar *mime_type;
};

A CtkFileFilterInfo is used to pass information about the tested file to ctk_file_filter_filter().

Members

CtkFileFilterFlags contains;

Flags indicating which of the following fields need are filled

 

const gchar *filename;

the filename of the file being tested

 

const gchar *uri;

the URI for the file being tested

 

const gchar *display_name;

the string that will be used to display the file in the file chooser

 

const gchar *mime_type;

the mime type of the file

 

enum CtkFileFilterFlags

These flags indicate what parts of a CtkFileFilterInfo struct are filled or need to be filled.

Members

CTK_FILE_FILTER_FILENAME

the filename of the file being tested

 

CTK_FILE_FILTER_URI

the URI for the file being tested

 

CTK_FILE_FILTER_DISPLAY_NAME

the string that will be used to display the file in the file chooser

 

CTK_FILE_FILTER_MIME_TYPE

the mime type of the file

 

See Also

CtkFileChooser