Better thumbnailer setup. Garunteed no crashes due to Gio out of thread

This commit is contained in:
itdominator 2021-11-20 03:26:14 -06:00
parent a668bf991b
commit ab9fca8c33
2 changed files with 54 additions and 73 deletions

View File

@ -9,9 +9,10 @@ import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
from gi.repository import Gtk as gtk from gi.repository import Gtk
from gi.repository import Gdk as gdk from gi.repository import Gdk
from gi.repository import GLib as glib from gi.repository import GLib
from gi.repository import Gio
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf
@ -32,7 +33,7 @@ class Grid:
self.settings = settings self.settings = settings
self.fileHandler = FileHandler(self.settings) self.fileHandler = FileHandler(self.settings)
self.store = gtk.ListStore(GdkPixbuf.Pixbuf, str) self.store = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
self.usrHome = settings.returnUserHome() self.usrHome = settings.returnUserHome()
self.hideHiddenFiles = settings.isHideHiddenFiles() self.hideHiddenFiles = settings.isHideHiddenFiles()
self.builder = settings.returnBuilder() self.builder = settings.returnBuilder()
@ -86,45 +87,54 @@ class Grid:
files = dirPaths + vids + images + desktop + files files = dirPaths + vids + images + desktop + files
self.generateGridIcons(path, files) self.generateGridIcons(path, files)
self.fillVideoIcons(path, vids, len(dirPaths))
def generateGridIcons(self, dir, files):
icon = GdkPixbuf.Pixbuf.new_from_file(self.iconFactory.INTERNAL_ICON_PTH)
for i, file in enumerate(files):
self.store.append([icon, file])
self.create_icon(i, dir, file)
@threaded @threaded
def generateGridIcons(self, dirPath, files): def create_icon(self, i, dir, file):
for file in files: icon = self.iconFactory.createIcon(dir, file)
image = self.iconFactory.createIcon(dirPath, file).get_pixbuf() fpath = dir + "/" + file
glib.idle_add(self.addToGrid, (image, file,)) GLib.idle_add(self.update_store, (i, icon, fpath,))
def update_store(self, item):
i, icon, fpath = item
itr = self.store.get_iter(i)
@threaded if not icon:
def fillVideoIcons(self, dirPath, files, start): icon = self.get_system_thumbnail(fpath, self.iconFactory.systemIconImageWH[0])
model = self.grid.get_model() if not icon:
if fpath.endswith(".gif"):
icon = GdkPixbuf.PixbufAnimation.get_static_image(fpath)
else:
icon = GdkPixbuf.Pixbuf.new_from_file(self.iconFactory.INTERNAL_ICON_PTH)
# Wait till we have a proper index... self.store.set_value(itr, 0, icon)
while len(self.store) < (start + 1):
time.sleep(.650)
i = start def get_system_thumbnail(self, filename, size):
for file in files:
self.updateGrid(model, dirPath, file, i)
i += 1
@threaded
def updateGrid(self, model, dirPath, file, i):
try: try:
image = self.iconFactory.createThumbnail(dirPath, file).get_pixbuf() if os.path.exists(filename):
iter = model.get_iter_from_string(str(i)) gioFile = Gio.File.new_for_path(filename)
glib.idle_add(self.replaceInGrid, (iter, image,)) info = gioFile.query_info('standard::icon' , 0, Gio.Cancellable())
icon = info.get_icon().get_names()[0]
iconTheme = Gtk.IconTheme.get_default()
iconData = iconTheme.lookup_icon(icon , size , 0)
if iconData:
iconPath = iconData.get_filename()
return GdkPixbuf.Pixbuf.new_from_file(iconPath)
else:
return None
else:
return None
except Exception as e: except Exception as e:
# Errors seem to happen when fillVideoIcons index wait check is to low print("System icon generation issue:")
print("widgets/Grid.py sinking errors on updateGrid method...") print( repr(e) )
return None
def addToGrid(self, dataSet):
self.store.append([dataSet[0], dataSet[1]])
def replaceInGrid(self, dataSet):
# Iter, row column, new pixbuf...
self.store.set_value(dataSet[0], 0 , dataSet[1])
def iconDblLeftClick(self, widget, item): def iconDblLeftClick(self, widget, item):

View File

@ -5,11 +5,9 @@ from os.path import isdir, isfile, join
# Gtk imports # Gtk imports
import gi import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
from gi.repository import Gtk as gtk from gi.repository import GdkPixbuf
from gi.repository import Gio as gio
from xdg.DesktopEntry import DesktopEntry from xdg.DesktopEntry import DesktopEntry
@ -55,13 +53,13 @@ class Icon:
thumbnl = self.createScaledImage(hashImgPth, self.viIconWH) thumbnl = self.createScaledImage(hashImgPth, self.viIconWH)
if thumbnl == None: # If no icon whatsoever, return internal default if thumbnl == None: # If no icon whatsoever, return internal default
thumbnl = gtk.Image.new_from_file(self.SCRIPT_PTH + "../resources/icons/video.png") thumbnl = GdkPixbuf.Pixbuf.new_from_file(self.SCRIPT_PTH + "../resources/icons/video.png")
return thumbnl return thumbnl
except Exception as e: except Exception as e:
print("Thumbnail generation issue:") print("Thumbnail generation issue:")
print( repr(e) ) print( repr(e) )
return gtk.Image.new_from_file(self.SCRIPT_PTH + "../resources/icons/video.png") return GdkPixbuf.Pixbuf.new_from_file(self.SCRIPT_PTH + "../resources/icons/video.png")
def getIconImage(self, file, fullPath): def getIconImage(self, file, fullPath):
@ -70,25 +68,19 @@ class Icon:
# Video icon # Video icon
if file.lower().endswith(self.vidsList): if file.lower().endswith(self.vidsList):
thumbnl = gtk.Image.new_from_file(self.SCRIPT_PTH + "../resources/icons/video.png") thumbnl = GdkPixbuf.Pixbuf.new_from_file(self.SCRIPT_PTH + "../resources/icons/video.png")
# Image Icon # Image Icon
elif file.lower().endswith(self.imagesList): elif file.lower().endswith(self.imagesList):
thumbnl = self.createScaledImage(fullPath, self.viIconWH) thumbnl = self.createScaledImage(fullPath, self.viIconWH)
# .desktop file parsing # .desktop file parsing
elif fullPath.lower().endswith( ('.desktop',) ): elif fullPath.lower().endswith( ('.desktop',) ):
thumbnl = self.parseDesktopFiles(fullPath) thumbnl = self.parseDesktopFiles(fullPath)
# System icons
else:
thumbnl = self.getSystemThumbnail(fullPath, self.systemIconImageWH[0])
if thumbnl == None: # If no icon whatsoever, return internal default
thumbnl = gtk.Image.new_from_file(self.INTERNAL_ICON_PTH)
return thumbnl return thumbnl
except Exception as e: except Exception as e:
print("Icon generation issue:") print("Icon generation issue:")
print( repr(e) ) print( repr(e) )
return gtk.Image.new_from_file(self.INTERNAL_ICON_PTH) return GdkPixbuf.Pixbuf.new_from_file(self.INTERNAL_ICON_PTH)
def parseDesktopFiles(self, fullPath): def parseDesktopFiles(self, fullPath):
try: try:
@ -126,7 +118,7 @@ class Icon:
for iconsDir in iconsDirs: for iconsDir in iconsDirs:
altIconPath = self.traverseIconsFolder(iconsDir, icon) altIconPath = self.traverseIconsFolder(iconsDir, icon)
if altIconPath is not "": if altIconPath != "":
break break
return self.createScaledImage(altIconPath, self.systemIconImageWH) return self.createScaledImage(altIconPath, self.systemIconImageWH)
@ -149,32 +141,11 @@ class Icon:
return altIconPath return altIconPath
def getSystemThumbnail(self, filename, size):
try:
if os.path.exists(filename):
gioFile = gio.File.new_for_path(filename)
info = gioFile.query_info('standard::icon' , 0, gio.Cancellable())
icon = info.get_icon().get_names()[0]
iconTheme = gtk.IconTheme.get_default()
iconData = iconTheme.lookup_icon(icon , size , 0)
if iconData:
iconPath = iconData.get_filename()
return gtk.Image.new_from_file(iconPath) # This seems to cause a lot of core dump issues...
else:
return None
else:
return None
except Exception as e:
print("system icon generation issue:")
print( repr(e) )
return None
def createScaledImage(self, path, wxh): def createScaledImage(self, path, wxh):
try: try:
pixbuf = gtk.Image.new_from_file(path).get_pixbuf() pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
scaledPixBuf = pixbuf.scale_simple(wxh[0], wxh[1], 2) # 2 = BILINEAR and is best by default scaledPixBuf = pixbuf.scale_simple(wxh[0], wxh[1], 2) # 2 = BILINEAR and is best by default
return gtk.Image.new_from_pixbuf(scaledPixBuf) return scaledPixBuf
except Exception as e: except Exception as e:
print("Image Scaling Issue:") print("Image Scaling Issue:")
print( repr(e) ) print( repr(e) )
@ -182,14 +153,14 @@ class Icon:
def createFromFile(self, path): def createFromFile(self, path):
try: try:
return gtk.Image.new_from_file(path) return GdkPixbuf.Pixbuf.new_from_file(path)
except Exception as e: except Exception as e:
print("Image from file Issue:") print("Image from file Issue:")
print( repr(e) ) print( repr(e) )
return None return None
def returnGenericIcon(self): def returnGenericIcon(self):
return gtk.Image.new_from_file(self.INTERNAL_ICON_PTH) return GdkPixbuf.Pixbuf.new_from_file(self.INTERNAL_ICON_PTH)
def generateVideoThumbnail(self, fullPath, hashImgPth): def generateVideoThumbnail(self, fullPath, hashImgPth):