Reputation: 931
Core Question:
How to turn mimetype - image/png
in to actual path to an icon file - /usr/share/icons/Menda-Circle/mimetypes/24x24/application-image-png.svg
while respecting linux distros DE's icon theme set?
Own Progress:
mimetypes.guess_type() will get me easily mimetype of a file based on the filename which is OKish.
Whats not so easy is translating that mimetype in to a path that leads to an icon
so far I had no luck progressing with linux own xdg-utils, or pyxdg in detecting icon theme or moving beyond that. PyQt5 seems to have trouble as well, which is understandable when most DEs are not Qt based. Maybe on KDE it will get something.
So with some googling I can use this to detect icon theme through Gtk
from gi.repository import Gtk
print(Gtk.Settings.get_default().get_property("gtk-icon-theme-name"))
Well thats where I am now, and I guess with some work and adjusting for all cases I could come up with some function to get the icons on most common distros/DE and for most common icon themes. Assuming I figure out the patern translating mimetype in to an icon file name.
But this all feels like it should be done and done already. That its some freedesktop standard and every file manager or any program with a file picker or displaying of files and folders is using this functionality no?
Is there some nice elegant way to go about this?
Upvotes: 3
Views: 1702
Reputation: 210
Once you use TingPing's link to https://lazka.github.io/pgi-docs/#Gio-2.0/functions.html#Gio.content_type_get_icon, you can get a list of possible icons. You then have to split this list and find the most-specific matching icon file.
import gi
gi.require('Gtk','3.0')
from gi.repository import Gtk, Gio # Debian package python3-gi
icon_theme = Gtk.IconTheme.get_default()
mimetype = "image/png"
image_file = None
icon = Gio.content_type_get_icon(mimetype)
for entry in icon.to_string().split():
if entry != "." and entry != "GThemedIcon":
try:
image_file = icon_theme.lookup_icon(entry,32,0).get_filename()
except:
# file must not exist for this entry
pass
if image_file:
break
print("Mimetype {0} can use icon file {1}".format(mimetype,image_file))
References
Upvotes: 1
Reputation: 2337
Gio's ContentType handles getting a files type and its icon for you:
https://lazka.github.io/pgi-docs/#Gio-2.0/functions.html#Gio.content_type_get_icon
Upvotes: 2