PyGTK: Get GNOME icon associated with a file
If in your application you need to display a list of files/folders it would be nice to display icons near filenames.
Icons are associated with files using mime types. For a file you need to get it’s mime and the get the icon associated with this mime.
The module gnomevfs provides almost all you need:
- def gnomevfs.get_mime_type(uri)
Returns a string containing the mime type (ex: for the file “file.tar.gz” the mime is “application/x-compressed-tar”).
- def gnomevfs.mime_get_icon(mime_type)
Returns a string with an icon name.
I said almost because gnomevfs.mime_get_icon always returns None (at least in my version).
Since the gnomevfs.get_mime_type works fine I decide to write a little class to associate the mime with the icons.
Gnome use icon names like “gnome-mime-<mime>”. Ex: for the mime “application/x-compressed-tar” the gnome icon name is “gnome-mime-application-x-compressed-tar”.
My class try to associate the mime with an icon, by searching for icons with the name:
- if the path is a directory it will try the icon name “folder” and if “folder” don’t exists just returns gtk.STOCK_DIRECTORY
- returns gtk.STOCK_FILE
In order to optimize mime to icon association the class use a cache.
<br /> <b>Warning</b>: include(/home2/macadanc/public_html/le-web.org/wp-content/uploads/2008/11/gnomefileicons.py) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in <b>/home2/macadanc/public_html/le-web.org/wp-content/plugins/include-it/plugin.php</b> on line <b>86</b><br /> <br /> <b>Warning</b>: include() [<a href='function.include'>function.include</a>]: Failed opening '/home2/macadanc/public_html/le-web.org/wp-content/uploads/2008/11/gnomefileicons.py' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in <b>/home2/macadanc/public_html/le-web.org/wp-content/plugins/include-it/plugin.php</b> on line <b>86</b><br />
Using it is very simple:
import gnomefileicons fileicons = gnomefileicons.GnomeFileIcons() print fileicons.getIcon('/path/file.tar.gz')