Creating new mime types in a shortcut Debian packaging

A short tutorial about installing your proprietary app’s own, new mimetype via a shortcut Debian packaging (on Linux.)

This is not about registering your application for existing mimetypes that other applications also create and understand.

Wiki on mimetypes

About Automating

This note is about automating:  making the Debian package install your mimetype when a user installs.

This is for a shortcut packaging done by you for limited distribution.  If your app is in the main distribution channels (Debian or Ubuntu) than a packager person might be doing this for you, in a different way.

The web is clogged with posts telling user’s how to fix mimetype icons manually (when installing an app fails to do it automatically.)  Those posts are relevant.

Adding Mime Types on Ubuntu

Gnome Adding Mime Types

About Registering with the IANA

I don’t know the benefits of registering your mimetype with an international standards organization.  I presume you may just start using a new mimetype, and register it when it becomes popular.  You might want to search the IANA site to see whether your mimetype is truly new (not registered by someone else.)

About mimetypes and file extensions on Linux

A mimetype is a Type in the object oriented sense.  A file instance has the type, meaning the file behaves a certain way under certain operations (mainly, reading by the owning application.)

The fact that the file has a certain type is not necessarily:

  • labeled on the outside of the file,  in the filename extension e.g.  ‘.bmp’
  • nor  in a header in the contents of the file (e.g. first three bytes ‘BMP’)
  • nor in separate metadata (resource fork?) associated with a file.

Linux actually uses a combination of those to determine a file’s mimetype.  Other OS’s differ.

Relationship between file extension and mimetype on Linux

The process

Overview:

  • define a mimetype: tell the OS how to recognize files having the mimetype
  • associate an icon: tell the OS what icon to display for said mimetype
  • register your application for the mimetype: tell the OS desktop what application understands the mimetype (what app to launch when a file is clicked.)

For this example:

  • application name: ‘helloworld’
  •  file extension: ‘.hlw’
  • mimetype name: ‘application/hlw’

Define a mimetype

In your Debian template directory create:  /usr/share/mime/packages/helloworld.xml.  (This will be copied to the computers’s filesystem at install time.)

(The name need not be helloworld.xml, as long as it is unique.  The OS reads all the XML files in this directory, no matter what they are named.  The contents of the XML file must match the mimetype.  It might be that the suffix of the file need not even be .xml, since the mimetype of the file: text/xml, might be determinable by the function in the OS that is reading them even without the suffix.)

<?xml version="1.0" encoding="utf-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/hlw">
     <glob pattern="*.hlw"/>
  </mime-type>
</mime-info>

Here:

  • ‘application/hlw’ is the name of the mimetype.
  • ‘.hlw’ is a filename extension that helps the OS recognize files having the mimetype

The second half of the mimetype name need not match the filename extension, but it usually does?  There might be other ways to get the OS to recognize files having a mimetype, for example, patching the algorithms used by the OS.  (Beyond the scope of this tutorial.)

Here, you might be able to substitute for ‘application’, for example ‘image’ or ‘text’.  The word ‘application’ might imply that the file is binary versus text?  If the OS thinks your file (before creating the mimetype) is of mimetype ‘application/octet-stream’?  (Beyond the scope of this tutorial.)

If you are doing this manually, create this file then run the command ‘sudo update-mime-database /usr/share/mime’.  But the installer should trigger this automatically at install time (or at computer restart.)
 

Associate an icon

Now  associate an icon with the MIME type. Create an SVG icon.   Name it “application-hlw.svg”  Note that the name of the file matches the mimetype name with slashes replaced by “-” and there are no capital letters.

Put the file in this directory in your Debian template directory:

/usr/share/icons/hicolor/scalable/mimetypes/application-hlw.svg

Here, hicolor is an icon theme, the default one for both Gnome and KDE. (Beyond the scope of this tutorial.)

Register your application for the mimetype

In your application’s desktop file at the following location in your Debian template directory:

/usr/share/applications/helloworld.desktop

Add this line:

MimeType=application/hlw

Testing

Rebuild your debian package, click on it, and choose ‘Reinstall’ (or similar.)

Test that:

  • in a file browser, a file created by your app displays your icon
  • when you click on the icon or file name, your app starts
Advertisements

4 thoughts on “Creating new mime types in a shortcut Debian packaging

  1. Pingback: A short Debian packaging case: GUI apps, GPL, PyInstaller | plashless

    • Sorry, I can’t really help. I don’t use dh. I read the man page also. It seems to me that dh_installmime must be called by some other part of dh, when you are creating a package. It creates files in your build directory? I don’t understand what its inputs are. I don’t understand how it also creates text to insert in the postinst scripts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s