Writing MIME Type association files

Autopackage will take your FreeDesktop.org XML MIME type file and automatically generate the KDE 3.x equivalent when the package is built if the FreeDesktop.org files are located in $build_root/share/mime/packages . 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 $PREFIX/mime/packages/applicationname.xml.

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

Autopackage extends this specification by adding the <icon> element. We added the element so that the KDE MIME Type files could be generated (they require an Icon key). 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/xdgmime-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/xdgmime-extensions as a namespace. If you add an <apkg:icon> entry (which we recommend) to the XML file, then you need to make sure that you have both namespaces listed in the file. If you don't, MIME Files will fail to be generated.

You can install FreeDesktop.org MIME type files using the installMime() function. Any automatically generated KDE 3.x definitions will automatically be installed as well. Following the above example, you could put this into the [Install] section of your specfile:

                    
installMime share/mime/packages/acroread.xml

Gnome 2.x

Versions of Gnome 2 prior to 2.8 used their own file format for MIME types. These are not automatically generated due to their complexity. A full documentation of the file format can be found at http://www.gnome.org/learn/admin-guide/2.6/ch05s02.html . The Gnome version of our PDF/PNG MIME type example would look like this:

                    
File acroread.mime:
application/pdf
        ext: pdf # note <tab>
image/png
        ext: png

File acroread.keys:
application/pdf
        description=PDF document # these lines must be indented with 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 # new MIME Type association
        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

The short_list_application_ids_for_*_user_level entry is used to link mime-types to the program to open them with. The value doesn't need to be the executable name (however that's the convention), but it needs to be the same as you use in the .applications file. You can specify multiple programs seperated by commas.

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/*