SolarFM/plugins/thumbnailer/icons/mixins/desktopiconmixin.py

77 lines
2.4 KiB
Python
Raw Normal View History

# Python imports
import os
2021-10-10 06:45:55 +00:00
from os.path import isfile
import subprocess
import hashlib
2021-10-10 06:45:55 +00:00
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
2021-10-10 06:45:55 +00:00
# Application imports
from .xdg.DesktopEntry import DesktopEntry
2021-10-10 06:45:55 +00:00
class DesktopIconMixin:
def find_thumbnail_from_desktop_file(self, full_path):
2021-10-10 06:45:55 +00:00
try:
xdgObj = DesktopEntry(full_path)
icon = xdgObj.getIcon()
alt_icon_path = ""
if "steam" in icon:
2023-02-08 23:52:33 +00:00
return self.get_steam_img(xdgObj)
2021-10-10 06:45:55 +00:00
elif os.path.exists(icon):
return self.create_scaled_image(icon, self.sys_icon_wh)
2021-10-10 06:45:55 +00:00
else:
2023-02-08 23:52:33 +00:00
pixbuf, alt_icon_path = self.get_icon_from_traversal(icon)
if pixbuf:
return pixbuf
2021-10-10 06:45:55 +00:00
return self.create_scaled_image(alt_icon_path, self.sys_icon_wh)
2021-10-10 06:45:55 +00:00
except Exception as e:
print(".desktop icon generation issue:")
print( repr(e) )
2023-02-08 23:52:33 +00:00
return None
def get_steam_img(self, xdgObj):
name = xdgObj.getName()
file_hash = hashlib.sha256(str.encode(name)).hexdigest()
hash_img_pth = f"{self.STEAM_ICONS_PTH}/{file_hash}.jpg"
if not isfile(hash_img_pth):
exec_str = xdgObj.getExec()
parts = exec_str.split("steam://rungameid/")
id = parts[len(parts) - 1]
imageLink = f"{self.STEAM_CDN_URL}{id}/header.jpg"
proc = subprocess.Popen(["wget", "-O", hash_img_pth, imageLink])
proc.wait()
return self.create_scaled_image(hash_img_pth, self.video_icon_wh)
def get_icon_from_traversal(self, icon):
gio_icon = Gio.Icon.new_for_string(icon)
gicon = Gtk.Image.new_from_gicon(gio_icon, 32)
pixbuf = gicon.get_pixbuf()
alt_icon_path = ""
for dir in self.ICON_DIRS:
alt_icon_path = self.traverse_icons_folder(dir, icon)
if alt_icon_path != "":
break
return pixbuf, alt_icon_path
2021-10-10 06:45:55 +00:00
def traverse_icons_folder(self, path, icon):
for (dirpath, dirnames, filenames) in os.walk(path):
for file in filenames:
appNM = "application-x-" + icon
if icon in file or appNM in file:
return f"{dirpath}/{file}"