Monday, April 18, 2011

Freedesktop / Linux MimeTypes, File Extensions, MimeType-Application Associations

This is a capture of Linux KDE/Gnome/Freedesktop files that define how MIME types such as "html"are managed by the system, and used by Applications such as Firefox. This includes File Associations and Default actions.


1. Related Links:


http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#mime-types
http://www.freedesktop.org/wiki/Specifications/mime-actions-spec
http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html
http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec

2. Overview of What is involved:














3. Directories and Files Managing MIME (database) and Application (Database) 

3.1 Shared MIME-Info Database

Is a file-based database of all known MIME types. The database's files are located in the following directories (further down overrides) /usr/share/mime/*/*.xml/usr/local/share/mime/*/*.xml, and~/.local/share/mime/*/*.xml (if they exist, and in this order).
Each has maintains an application which manipulates this database - for example, in KDE, Configure Desktop -> File Association

The database starts in directory named "mime" - for example /usr/share/mime - which has following files


all
application
audio
...
packages
...
text
(etc)


As an example, /usr/share/mime/text directory has following files:



css.xml
html.xml
plain.xml
richtext.xml
sgml.xml
vnd.graphviz.xml
x-cobol.xml
x-erlang.xml
x-fortran.xml

(etc)

As an example, html.xml defines MIME type "html", this way:



So html.xml defines:
The MIME type name (html)
The MIME Parent Type (text/plain)
Description (in all languages)
List of File Extensions which will assume this MIME Type

Application section of the the Shared MIME-Info database

this is in the mime/packages directory.

 For example, doing ./configure && make install with Firefox should create /usr/share/mime/packages/firefox.xml (but it does NOT).


 KDE and GNOME desktop environments have adopted a similar format for "desktop entries", or configuration files describing how a particular program is to be launched, how it appears in menus, etc. These files end with extension .desktopThe suffix "desktop" seems a misnomer - it seems .desktop files simply describe Applications

Desktop Entries Database (=Application Definitions Database) (NOT Part of MIME-Info Database, but uses it)

This database is located in /usr/share/applications/*/usr/local/share/applications/*, and ~/.local/share/applications/* (if they exist, and in this order). Further, if desktop=KDE, ~.kde4/share/apps/* is also part of the database.

The /usr/share/applications directory has following files
kde4
YaST2
google-chrome.desktop
firefox.desktop
gimp.desktop
emacs.desktop
....etc

for example, /usr/share/applications/firefox.desktop contains

[Desktop Entry]
X-SuSE-translate=true
Categories=Network;WebBrowser;GTK;
Encoding=UTF-8
Name=Firefox
GenericName=Web Browser
Comment=Web Browser
TryExec=firefox
Exec=firefox %u
Icon=firefox
Terminal=false
StartupNotify=true
MimeType=text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;application/x-xpinstall;x-scheme-handler/http;x-scheme-handler/https;
Type=Application


Note: The MIME Type list above can be expanded to list of file extensions. This is displayed in "File Association" settings.


Clearly some component scrounges through the applications/*.desktop, and if the *.desktop lists the MIME under MimeType, it will add the application.



Note: To add a new application, such as emacs-client, that appears in popups of right click open-with, simply
  • create /usr/share/applications/emacs-client.desktop
  • copy contents from emacs.desktop
  • modify Exec=run-emacs-client.sh
  • modify Name=Emacs Client

3.3 ~/.local/share/applications/mimeapp.list - user defined order of application preference when open

  • maintains ORDER of Applications Which Understand a certain MimeType
  • Format
[Added Associations]
mimetype1=foo1.desktop;foo2.desktop;foo3.desktop;
mimetype2=foo4.desktop;
[Removed Associations]
mimetype1=foo5.desktop;
  • Sample:


[Added Associations]
application/x-shellscript=run-emacs-client.sh.desktop;
text/x-emacs-lisp=run-emacs-client.sh.desktop;

[Added KDE Service Associations]
application/xhtml+xml=kwebkitpart.desktop;khtml.desktop;
application/xml=kwebkitpart.desktop;khtml.desktop;
text/html=kwebkitpart.desktop;khtml.desktop;

No comments: