Chapter 8. Using MimeTypes

Table of Contents

Mime Type specs
Writing MimeType association files
FreeDesktop.org/KDE
Gnome 2.x
Associating Applications with mime-types
.desktop file
Gnome .applications file

Mime Types are somewhat of a mess on the Linux desktop. KDE and Gnome each have their own ways of defining mime-types, along with FreeDesktop.org who has introduced another standard trying to bring some organization to this madness. Autopackage has API calls for installing the files for each standard.

Mime Type specs

Writing MimeType association files

Autopackage can take your FreeDesktop.org XML mime-type file and turn it into a KDE version using the generateMimeFiles() API call. Gnome mime-type files need to be written separately.

FreeDesktop.org/KDE

A typical FreeDesktop.org mime-type file looks like this:

                    
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/pdf">
    <comment>PDF document</comment>
    <comment xml:lang="nn">PDF-dokument</comment>
    <magic priority="50">
      <match offset="0" type="string" value="%PDF-" />
    </magic>
    <glob pattern="*.pdf" />
  </mime-type>

  <mime-type type="image/png">
    <comment>PNG image</comment>
    <comment xml:lang="fi">PNG-kuva</comment>
    <magic priority="50">
      <match offset="0" type="string" value="\x89PNG" />
    </magic>
    <glob pattern="*.png" />
  </mime-type>
</mime-info>

This file is installed into $XDG_DATA_DIRS/mime/packages/applicationname.xml.

For documentation on all the gory details of specification, read the documentation on FreeDesktop.org.

Autopackage extends this specification by adding an <icon> tag. The FDO specification can be extended, given that new tags are properly namespaced. To add an icon association, change the entry to this:

                    
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info" 
xmlns:apkg="http://autopackage.org/fdo-extensions">
  <mime-type type="application/pdf">
    <comment>PDF document</comment>
    <comment xml:lang="nn">PDF-dokument</comment>
        
    <magic priority="50">
      <match offset="0" type="string" value="%PDF-" />
    </magic>
        
    <glob pattern="*.pdf" />
    <apkg:icon>pdf.png</apkg:icon>
  </mime-type>
  <!-- [...] -->
</mime-info>

Note how we added http://autopackage.org/fdo-extensions as a namespace.

This file needs to be available when the Autopackage is generated. For example you could have acroread.xml stored in the source root of the program your packaging. You could also create the file inside the Autopackage specfile (you can use echo '<?xml...?>[...]' > acroread.xml.

Once the XML file has been created you can use the generateMimeFiles() API call to generate mime-type files for KDE. You could have this in the [BuildPrepare] section of your specfile:

                    
echo '<?xml version="1.0"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info" 
xmlns:apkg="http://autopackage.org/fdo-extensions">
  <mime-type type="application/pdf">
    <comment>PDF document</comment>
    <comment xml:lang="nn">PDF-dokument</comment>
    <magic priority="50">
      <match offset="0" type="string" value="%PDF-" />
    </magic>

    <glob pattern="*.pdf" />
    <apkg:icon>pdf.png</apkg:icon>
  </mime-type>
</mime-info>' > acroread.xml

generateMimeFiles acroread.xml

generateMimeFiles() uses xsltproc to generate the KDE mime-type files and copies them to $build_root/mime-storage/kde/<media type>/<subtype>.desktop. The PDF mime-type file would end up in $build_root/mime-storage/kde/application/pdf.desktop. The FDO XML file is copied to $build_root/mime-storage/fdo/<name>. Remember to import the $build_root/mime-storage directory if you are manually importing files and directories.

You can install these files using the installMime() and installKDEMime() functions. Following the above example, you could put this into the [Install] section of your specfile:

                    
installKDEMime mime-storage/kde/application/pdf.desktop
installMime mime-storage/fdo/acroread.xml

Gnome 2.x

Versions of Gnome 2 prior to 2.8 used their own file format for mime-types. These are not generated by generateMimeFiles, due to their complexity. A full documentation can be found at http://www.gnome.org/learn/admin-guide/latest/ch05s02.html. The following is the Gnome version of our PDF/PNG mime-type example:

                    
File acroread.mime:
application/pdf
ext: pdf
        image/png # note <tab>
        ext: png
        
File acroread.keys:
application/pdf
        description=PDF document # note tabs!
        category=Documents
        icon_filename=pdf # note! No file extention or path on filename
        default_action_type=application
        short_list_application_ids_for_novice_user_level=acroread
        short_list_application_ids_for_intermediate_user_level=acroread
        short_list_application_ids_for_advanced_user_level=acroread
        image/png
        description=PNG Image
        category=Documents/Images
        icon_filename=png
        default_action_type=application
        short_list_application_ids_for_novice_user_level=acroread
        short_list_application_ids_for_intermediate_user_level=acroread
        short_list_application_ids_for_advanced_user_level=acroread

These files are installed into <gnome-prefix>/share/mime-info/. You could import these files into $build_root/mime-storage/gnome if you wanted to put all the Mime-type files in one place. Then, add this API calls to the [Install] section:

                    
installGnome2Mime mime-storage/gnome/*

Associating Applications with mime-types

After you have associated files with mime-types, you need to associate those mime-types with the applications who can open them. KDE and Gnome 2.8 and above use .desktop files to achieve this, and Gnome prior to 2.8 use .applications files.

.desktop file

The .desktop way is the eaisest--simply add a MimeType key to the existing .desktop file. For example, acroread.desktop could become:

                    
[Desktop Entry]
Version=1.0
Type=Application
Encoding=UTF-8
Name=Acrobat Reader
Comment=Read PDF files
Exec=acroread %f
Icon=acroread.png
MimeType=application/pdf;image/png; # note final semi-colon
Categories=Viewer;Office;
                     

A list of valid Exec variablesis available from FreeDesktop.org.

You can install this file with the installDesktop() API call.

Gnome .applications file

Gnome uses their own file format for associating mime-types with applications. It is fully documented here.

The first line ("acroread" in this case), is called the "Application Identifier". It does not need to be the same at the executable name, but that is the convention. This is used to map short_list_application_ids_for_*_user_level to its entry in a .applications file.

                    
acroread
        command=acroread # note tabs!
        name=Acrobat Reader
        can_open_multiple_files=false
        expects_uris=false
        requires_terminal=false
        startup_notify=true
        mime_types=application/pdf,image/png

This file can be called acroread.applications and can be installed with installGnome2AppEntry().