# MIME Types

This chapter describes how applications detect MIME types, how to register MIME types, and how to add applications to the GNOME desktop.

## Introduction

A Multipurpose Internet Mail Extension (MIME) type identifies the format of a file. The MIME type enables applications to read the file. Applications such as Internet browsers and email applications use the MIME type to handle files of different types. For example, an email application can use the MIME type to detect what type of file is in a file attached to an email.

In the GNOME desktop, the Nautilus file manager uses MIME types to identify the type of a file. The file manager needs to know the MIME type of a file to perform the following tasks:

- Open the file in an appropriate application.

- Display a string that describes the type of file.

- Display an appropriate icon to represent the file.

- Display a list of other applications that can open the file.

If you add a new application, you must ensure that other applications can recognize the files associated with the application. You must perform several tasks to enable other applications to detect the MIME type of the application files.

This section describes how applications detect the MIME types of files, and how applications are associated with MIME types. This chapter also describes the procedure that you must follow to add a new application.

## Detecting the MIME Type for a File

Applications can detect the MIME type of a file as follows:

1. The application uses file content sniffers to search for a particular pattern in the file. A file content sniffer associates a specific pattern in a file with a MIME type. If the application finds a match for the pattern, the MIME type associated with the pattern is the MIME type of the file.

2. If file content sniffers do not identify the MIME type, then the application can check the filename. The application checks the filename against the MIME type registry. The MIME type registry associates particular filename extensions and filename patterns, with particular MIME types. If a match for the filename is found, the MIME type associated with the extension or pattern is the MIME type of the file.

The following sections provide further information on file content sniffers and the MIME type registry.

### File Content Sniffers

File content sniffers are specified in the file `/usr/gnome/etc/gnome-vfs-mime-magic`. The following is an example of a file content sniffer:

    0	string		\x89PNG					image/png

The syntax for file content sniffers is as follows:

    offset_start[:offset_end] pattern_type pattern [&pattern_mask] type

describes the fields in a file content sniffer.

#### Fields in a File Content Sniffer

Field | Description
--- | ---
`offset_start` | Specifies the number of characters to ignore in the file before searching for a text pattern.
`pattern_type` | Specifies the type of pattern to search for. The `string` pattern type is the only pattern type that is supported at the time of publication of this guide.
`pattern` | Specifies the pattern to search for.
`pattern_mask` | Specifies a pattern mask, in hexadecimal format. For more information on pattern masks, see the next section. This field is optional. This field is not present in the example.
`type` | Specifies the MIME type to associate with files that match this entry.

#### Pattern Masks

A pattern mask identifies bits in the pattern to ignore when searching for a pattern in a file. The following is an example of a file content sniffer with a pattern mask:

    0     string          BMxxxx\000\000 &0xffff00000000ffff      image/bmp

The pattern and mask in the example are as follows:

Pattern | `B` | `M` | `x` | `x` | `x` | `x` | `\000` | `\000`
Mask | `ff` | `ff` | `00` | `00` | `00` | `00` | `ff` | `ff`

The pattern and mask specify a file with the following characteristics:

1. The file begins with `BM`.

2. `BM` is followed by four bytes with any values.

3. The four bytes are followed by `\000\000`.

The file content sniffer specifies that the MIME type of files that match the pattern and mask is `image/bmp`.

### MIME Type Registry

The MIME type registry is located in `/usr/gnome/share/mime-info`. The MIME type registry contains the following files:

File | Filename Extension
--- | ---
MIME information file | `.mime`
MIME keys file | `.keys`

The following sections describe MIME information files and MIME keys files.

#### MIME Information Files

MIME information files associate MIME types with one or both of the following:

- Filename extensions

- Filename patterns

When an application searches for the MIME type of a file, the application checks the filename against the MIME information files. If a match for the filename is found, the MIME type associated with the extension or pattern is the MIME type of the file.

In MIME information files, the filename pattern to search for is written as a regular expression.

The format of MIME type entries in MIME information files is as follows:

    MIME_type
    		ext[,priority]: list_of_extensions
    		regex[,priority]: list_of_regular_expressions

You can specify a priority value for the filename extension and the regular expression. You can use the priority value to differentiate composite filenames. For example, you can assign a priority of `1` to the `.gz` extension, and assign a higher priority of `2` to the `.tar.gz` extension. In this case, the file `abc.tar.gz` takes the MIME type for `.tar.gz`.

You must indent the `ext` field and the `regex` field with a tab character (`\t`).

The following MIME type entries are samples from the `gnome-vfs.mime` MIME information file:

    application/x-compressed-tar
    		regex,2: tar\.gz$
    		ext: tgz
    audio/x-real-audio
    		ext: rm ra ram
    image/jpeg
    		ext: jpe jpeg jpg
    image/png
    		ext: png
    text/html
    		ext: html htm HTML
    text/plain
    		ext: asc txt TXT
    text/x-readme
    		regex: README.*

The file manager reads the MIME information files alphabetically. The alphabetical order determines the order in which MIME types are assigned to filename extensions or regular expressions. For example, if the same file extension is assigned to different MIME types in the files `abc.mime` and `def.mime`, the MIME type in `abc.mime` is used.

#### MIME Keys Files

MIME keys file provide information about a MIME type that is used in the user interface. For example, the MIME keys file provides a description of a MIME type, and specifies an icon to represent files of that MIME type.

The following is a sample from a MIME keys file:

    text/html
    		description=HTML page
    		icon_filename=gnome-text-html
    		default_action_type=application
    		short_list_application_ids_for_novice_user_level=mozilla,netscape,galeon
    		category=Documents/World Wide Web

You must indent the keys in a MIME keys file with a tab character (`\t`).

describes the most important keys in MIME keys files. Typically, the `description` key and the `category` key are localized.

##### Keys in MIME Keys Files

Key | Description
--- | ---
`can_be_executable` | Specifies whether files of this MIME type can be executed.
`description` | Describes the MIME type. This description can be displayed in the file manager and other applications.
`icon_filename` | Specifies the filename of an icon to represent the MIME type. Does not specify the path to the filename, or the filename extension. This icon can be displayed in the file manager and other applications.
`default_action_type` | Specifies the category of action to take when a file of this MIME type is opened by the user. Enter `application` for this MIME type for most applications.
`short_list_application_ids` `_for_novice_user_level` | Specifies the application to use when a file of this MIME type is opened by a user. Specify one or more applications, in order of priority. The applications must also be registered in the application registry.
`category` | Specifies a category for the MIME type. The value of this key determines the location of the MIME type in the File Types and Programs preference tool.

## Registering Applications for MIME Types

The application registry contains text files that register applications. The application registration files contain a series of key-value pairs that specify details for applications. For example, the application registration files contain the following information:

- The command to use to start the application.

- MIME types to associate with the application.

An application registration file can contain one or more application registrations. Application registration files have a `.applications` extension.

The location of the application registry is `/usr/share/gnome/application-registry`. This directory contains a default application registration file that is called `gnome-vfs.applications`.

To register an application, add a registration file for the application to the application registry.

The following is an example of an application registration:

    eog
    		command=eog
    		name=Eye of Gnome
    		can_open_multiple_files=true
    		expects_uris=false
    		requires_terminal=false
    		mime_types=image/bmp,image/gif,image/jpeg,image/png,image/tiff,
    image/x-xpixmap,image/x-bmp,image/x-png,image/x-portable-anymap,
    image/x-portable-bitmap,image/x-portable-graymap,
    image/x-portable-pixmap

describes the keys in application registration files.

### Keys for an Application Registration

Key | Description
--- | ---
Application identifier | Specifies a unique identifier for the application. This identifier must be the same as the identifier in the short_list_application_ids_for_novice_user_level key in the MIME keys file for the application.
`command` | Specifies the command to use to start the application, and any options to use with the command.
`name` | Specifies a name for the application. The name is used in the user interface. For example, the name is used in the Open With submenu in the file manager.
`can_open_multiple_files` | Specifies whether the application can open several files at the same time.
`expects_uris` | Specifies whether the application can process URIs. If the value of this key is `true`, the application registration entry must also contain a `supported_uri_schemes` key.
`supported_uri_schemes` | Specifies the URI schemes that the application can process.
`requires_terminal` | Specifies whether to run the application in a terminal window. Enter `true` for this field for an application that does not create a window in which to run.
`mime_types` | Specifies the MIME types that the application can use.

## Adding an Application to the GNOME Desktop

To add an application to the GNOME desktop, perform the following steps:

1. Add a menu item for the application. For more information on how to add an item to a menu, see .

2. Add an icon for the application to `/usr/gnome/share/icons/theme_name/icon_size/apps`. For more information on icons and themes, see Installing Themes.

3. If the application uses a new MIME type, add a file content sniffer for the new MIME type. For more information on file content sniffers, see .

4. If the application uses a new MIME type, add a MIME information file for the application to the MIME type registry. For more information on MIME information files, see .

5. Add a MIME keys file for the application to the MIME type registry. For more information on MIME keys files, see .

6. If the application uses a new MIME type, add an icon for the MIME type to `/usr/gnome/share/icons/theme_name/icon_size/mimetypes`. For more information on icons and themes, see Installing Themes.

7. To associate the application with a MIME type, add an application registration file to the application registry. For more information on the application registry, see .