88 lines
3.1 KiB
Python
88 lines
3.1 KiB
Python
|
# Python Imports
|
||
|
import os, subprocess, threading, hashlib
|
||
|
from os.path import isfile
|
||
|
|
||
|
# Gtk imports
|
||
|
import gi
|
||
|
gi.require_version('Gtk', '3.0')
|
||
|
|
||
|
from gi.repository import Gtk
|
||
|
|
||
|
# Application imports
|
||
|
from .mixins import *
|
||
|
|
||
|
|
||
|
|
||
|
def threaded(fn):
|
||
|
def wrapper(*args, **kwargs):
|
||
|
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
||
|
return wrapper
|
||
|
|
||
|
|
||
|
class Icon(DesktopIconMixin, VideoIconMixin):
|
||
|
def create_icon(self, dir, file):
|
||
|
full_path = dir + "/" + file
|
||
|
return self.get_icon_image(dir, file, full_path)
|
||
|
|
||
|
def get_icon_image(self, dir, file, full_path):
|
||
|
try:
|
||
|
thumbnl = None
|
||
|
|
||
|
if file.lower().endswith(self.fvideos): # Video icon
|
||
|
thumbnl = self.create_thumbnail(dir, file)
|
||
|
elif file.lower().endswith(self.fimages): # Image Icon
|
||
|
thumbnl = self.create_scaled_image(full_path, self.VIDEO_ICON_WH)
|
||
|
elif full_path.lower().endswith( ('.desktop',) ): # .desktop file parsing
|
||
|
thumbnl = self.parse_desktop_files(full_path)
|
||
|
else: # System icons
|
||
|
thumbnl = self.get_system_thumbnail(full_path, self.SYS_ICON_WH[0])
|
||
|
|
||
|
if thumbnl == None: # If no icon whatsoever, return internal default
|
||
|
thumbnl = Gtk.Image.new_from_file(self.DEFAULT_ICON)
|
||
|
|
||
|
return thumbnl
|
||
|
except Exception as e:
|
||
|
print("Icon generation issue:")
|
||
|
print( repr(e) )
|
||
|
return Gtk.Image.new_from_file(self.DEFAULT_ICON)
|
||
|
|
||
|
def create_thumbnail(self, dir, file):
|
||
|
full_path = dir + "/" + file
|
||
|
try:
|
||
|
file_hash = hashlib.sha256(str.encode(full_path)).hexdigest()
|
||
|
hash_img_pth = self.ABS_THUMBS_PTH + "/" + file_hash + ".jpg"
|
||
|
if isfile(hash_img_pth) == False:
|
||
|
self.generate_video_thumbnail(full_path, hash_img_pth)
|
||
|
|
||
|
thumbnl = self.create_scaled_image(hash_img_pth, self.VIDEO_ICON_WH)
|
||
|
if thumbnl == None: # If no icon whatsoever, return internal default
|
||
|
thumbnl = Gtk.Image.new_from_file(self.DEFAULT_ICONS + "/video.png")
|
||
|
|
||
|
return thumbnl
|
||
|
except Exception as e:
|
||
|
print("Thumbnail generation issue:")
|
||
|
print( repr(e) )
|
||
|
return Gtk.Image.new_from_file(self.DEFAULT_ICONS + "/video.png")
|
||
|
|
||
|
|
||
|
def create_scaled_image(self, path, wxh):
|
||
|
try:
|
||
|
pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
|
||
|
scaled_pixbuf = pixbuf.scale_simple(wxh[0], wxh[1], 2) # 2 = BILINEAR and is best by default
|
||
|
return Gtk.Image.new_from_pixbuf(scaled_pixbuf)
|
||
|
except Exception as e:
|
||
|
print("Image Scaling Issue:")
|
||
|
print( repr(e) )
|
||
|
return None
|
||
|
|
||
|
def create_from_file(self, path):
|
||
|
try:
|
||
|
return Gtk.Image.new_from_file(path)
|
||
|
except Exception as e:
|
||
|
print("Image from file Issue:")
|
||
|
print( repr(e) )
|
||
|
return None
|
||
|
|
||
|
def return_generic_icon(self):
|
||
|
return Gtk.Image.new_from_file(self.DEFAULT_ICON)
|