2021-10-10 06:45:55 +00:00
|
|
|
# Python Imports
|
|
|
|
import os, subprocess, threading, hashlib
|
|
|
|
from os.path import isfile
|
|
|
|
|
|
|
|
# Gtk imports
|
2021-11-17 03:41:24 +00:00
|
|
|
from gi.repository import GdkPixbuf
|
2021-10-10 06:45:55 +00:00
|
|
|
|
|
|
|
# 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):
|
2022-01-19 22:10:43 +00:00
|
|
|
full_path = f"{dir}/{file}"
|
2021-10-10 06:45:55 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
return thumbnl
|
|
|
|
except Exception as e:
|
2021-11-17 03:41:24 +00:00
|
|
|
return None
|
2021-10-10 06:45:55 +00:00
|
|
|
|
|
|
|
def create_thumbnail(self, dir, file):
|
2022-01-19 22:10:43 +00:00
|
|
|
full_path = f"{dir}/{file}"
|
2021-10-10 06:45:55 +00:00
|
|
|
try:
|
|
|
|
file_hash = hashlib.sha256(str.encode(full_path)).hexdigest()
|
2022-01-19 22:10:43 +00:00
|
|
|
hash_img_pth = f"{self.ABS_THUMBS_PTH}/{file_hash}.jpg"
|
2021-10-10 06:45:55 +00:00
|
|
|
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
|
2022-01-19 22:10:43 +00:00
|
|
|
thumbnl = GdkPixbuf.Pixbuf.new_from_file(f"{self.DEFAULT_ICONS}/video.png")
|
2021-10-10 06:45:55 +00:00
|
|
|
|
|
|
|
return thumbnl
|
|
|
|
except Exception as e:
|
|
|
|
print("Thumbnail generation issue:")
|
|
|
|
print( repr(e) )
|
2022-01-19 22:10:43 +00:00
|
|
|
return GdkPixbuf.Pixbuf.new_from_file(f"{self.DEFAULT_ICONS}/video.png")
|
2021-10-10 06:45:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_scaled_image(self, path, wxh):
|
|
|
|
try:
|
2022-01-19 18:09:01 +00:00
|
|
|
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], True)
|
2021-10-10 06:45:55 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Image Scaling Issue:")
|
|
|
|
print( repr(e) )
|
|
|
|
return None
|
|
|
|
|
|
|
|
def create_from_file(self, path):
|
|
|
|
try:
|
2021-11-17 03:41:24 +00:00
|
|
|
return GdkPixbuf.Pixbuf.new_from_file(path)
|
2021-10-10 06:45:55 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Image from file Issue:")
|
|
|
|
print( repr(e) )
|
|
|
|
return None
|
|
|
|
|
|
|
|
def return_generic_icon(self):
|
2021-11-17 03:41:24 +00:00
|
|
|
return GdkPixbuf.Pixbuf.new_from_file(self.DEFAULT_ICON)
|