Rohit Banga
Rohit Banga

Reputation: 18918

How to find which program to use for a given mime type in linux?

I need to open a viewer for a given mime type. how to do that in Linux. I suppose the method would be specific to desktop environment in use. I want to do this through a C program.

Upvotes: 1

Views: 369

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881635

There are Gnome APIs that deal with mime-types for you (declared in headers such as <libgnomevfs/gnome-vfs-mime-handlers.h>) but I think that understanding what's happening under the covers is also useful.

The functions in question essentially read, parse and present certain configuration files' contents to you, and here is a clear explanation of the files that Gnome uses to associate mime types with files, i.e.:

extension .mime in the $gnome/share/mime-info directory or from the ~/.gnome/mime-info directory.

The file $gnome/share/mime-info/gnome.mime is special, as it contains the defaults for gnome, and is read first. In addition, the file ~/.gnome/mime-info/user.mime is read last.

As explain in the section "Default Keys" of this URL, keys such as open, view etc are what gives you the commands to use for various generic actions (and, via "tags", for specific named actions too).

For a C code example using the API, see e.g. the end of this thread.

Upvotes: 1

Related Questions