Development/Howto/Mimetypes

From Mandriva Community Wiki

Jump to: navigation, search
MIME Type

Contents


[edit] What is MIME ?

Multipurpose Internet Mail Extensions (MIME) is an Internet Standard that extends the format of e-mail to support:

  • text in character sets other than US-ASCII;
  • non-text attachments;
  • multi-part message bodies; and
  • header information in non-ASCII character sets.

More informations in wikipedia/MIME.

The use of MIME type has been generalized in Unix and other free OS. When the header of the file giving the MIME type is missing, the system can guess the type of file with the (unreliable) suffix of its name.

MIME type is a subject often view as a complex subject. But in fact, it is not. It is often used to designate a lot of different things under the same umbrella.

On Linux systems, MIME type covers two differents problems :

  • associating a file to an identifier describing it type ; this is called a MIME type.
  • associating a MIME type to a application which will handle files from this type.

[edit] Associating file to MIME type

Currently, this action is not yet completely standardised between GNOME and KDE. It will be when KDE 4 is released and replace KDE 3.

This part is taking of detecting which mimetype should be assigned to a particular file on disk (it is not relevant for network stream, see below for this).

  • GNOME 2.x is using Freedesktop.org specification. KDE 4 will use this same specification. File used for this specification are packaged in shared-mime-info package. If a file type is not recognized, a small XML file can be written like the following example :
<?xml version="1.0"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-foobar">
    <comment>foobar file</comment>
    <glob pattern="*.foobar"/>
     <magic priority="60">
       <match value="foobar-file" type="string" offset="20:140"/>
     </magic>
  </mime-type>
</mime-info>

This file must be installed in /usr/share/mime/packages/ and %update_mime_database / %clean_mime_database macro should be called in package %post / %postun.

This file will detect all files with .foobar as extension as all as file containing string "foobar-file" as offset 20 to 140 (this is often called magic).

It is highly recommended to submit bugs on Freedesktop.org bugzilla for file type not recognized.

  • KDE 3.x is still using an old format, which is based on .desktop files. Those files should be installed in /usr/share/mimelnk, and packaged in kdelibs-common. File example :
[Desktop Entry]
Type=MimeType
Comment=foobar file
MimeType=application/x-foobar
Patterns=*.foobar;

magic handling must be done in /usr/share/mimelnk/magic (which is packaged in kdelibs-common).

[edit] Associating MIME type to applications

This part is standardised between GNOME and KDE (3.x and 4.x) and specified on freedesktop.org, but there is still some slight differences between implementations.

This part is also relevant for files downloaded through network (for instance a web browser). When a file is requested through a web browser, a mimetype is sent by web server, before the real file and web browser can then decide which application will be used to handle this file. It can happen incorrect mimetype is sent but this particular case is a web server problem and can't be fixed on the client side.

To associate an application to mimetypes, .desktop file for the particular application should have the following line (list must always end with a semicolon): MimeType=application/x-foobar;application/x-foobar2;

To add or remove mimetypes to .desktop file when doing packaging, it is best to use desktop-file-install utility. For instance :

desktop-file-install --add-mime-type="foo/bar" --remove-mime-type="broken/mime" --vendor="" --dir %{buildroot}%{_datadir}/applications foobar.desktop

Differences between GNOME and KDE are only to handle how applications are priorited when several applications claim to support the same mimetype.

For GNOME (and according to freedesktop.org specification), /usr/share/applications/defaults.list file (packaged in share-mime-info) must be modified. Each mimetype specified there lists, in order or preference, the .desktop file corresponding to application to use.

For KDE, a key named InitialPreference is added in application .desktop file and application order is based on this key value : higher InitialPreference value will determine default application.

Exec line of .desktop file can be modified when command-line need to be tuned for specific applications (see here for complete list),

  • if only one file can be added as argument : %f
  • if several files can be added as argument : %F
  • if one url can be added as argument : %u
  • if several urls can be added as argument : %U
Personal tools
Ad (via La Vignette)
Looking for a job?