Commit 6969d3de authored by Roman Alifanov's avatar Roman Alifanov

applicationrow: implemented downloading icons from Etersoft servers and caching

parent f24534bf
from gi.repository import Adw, Gtk import os
import urllib.request
import threading
from gi.repository import Adw, Gtk, GdkPixbuf, Gio, GLib, Gdk
class IconLoader:
BASE_URL = "https://download.etersoft.ru/pub/Etersoft/XimperLinux/eepm_play_gui/icons/"
SUPPORTED_EXTENSIONS = ['png', 'jpg', 'svg']
CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "ru.eepm.PlayGUI", "apps_icons")
@classmethod
def ensure_cache_dir(cls):
if not os.path.exists(cls.CACHE_DIR):
os.makedirs(cls.CACHE_DIR)
@classmethod
def get_cached_icon_path(cls, icon_name):
return os.path.join(cls.CACHE_DIR, f"{icon_name}.png") # Cache as PNG
@classmethod
def load_icon_async(cls, icon_name, callback):
cls.ensure_cache_dir() # Ensure the cache directory exists
cached_icon_path = cls.get_cached_icon_path(icon_name)
if os.path.exists(cached_icon_path):
pixbuf = GdkPixbuf.Pixbuf.new_from_file(cached_icon_path)
GLib.idle_add(callback, pixbuf)
return
def load_icon():
for ext in cls.SUPPORTED_EXTENSIONS:
icon_url = f"{cls.BASE_URL}{icon_name}.{ext}"
try:
with urllib.request.urlopen(icon_url) as response:
input_stream = Gio.MemoryInputStream.new_from_data(response.read(), None)
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(input_stream, None)
# Save the pixbuf to the local cache
pixbuf.savev(cached_icon_path, "png", [], [])
GLib.idle_add(callback, pixbuf)
return
except Exception as e:
print(f"Icon for {icon_name} not found in {ext}: {e}")
threading.Thread(target=load_icon, daemon=True).start()
class ApplicationRow(Adw.ActionRow): class ApplicationRow(Adw.ActionRow):
__gtype_name__ = 'ApplicationRow' __gtype_name__ = 'ApplicationRow'
...@@ -11,6 +58,12 @@ class ApplicationRow(Adw.ActionRow): ...@@ -11,6 +58,12 @@ class ApplicationRow(Adw.ActionRow):
self.set_selectable(False) self.set_selectable(False)
self.set_activatable(True) self.set_activatable(True)
self.icon_image = Gtk.Image.new_from_icon_name("image-missing")
self.icon_image.set_icon_size(Gtk.IconSize.LARGE)
self.add_prefix(self.icon_image)
IconLoader.load_icon_async(self.app['name'], self.set_icon)
self.checkbox = Gtk.CheckButton() self.checkbox = Gtk.CheckButton()
self.checkbox.add_css_class("selection-mode") self.checkbox.add_css_class("selection-mode")
self.checkbox.set_valign(Gtk.Align.CENTER) self.checkbox.set_valign(Gtk.Align.CENTER)
...@@ -23,6 +76,9 @@ class ApplicationRow(Adw.ActionRow): ...@@ -23,6 +76,9 @@ class ApplicationRow(Adw.ActionRow):
self.on_toggle = on_toggle # Callback for when the checkbox is toggled self.on_toggle = on_toggle # Callback for when the checkbox is toggled
def set_icon(self, pixbuf):
self.icon_image.set_from_pixbuf(pixbuf)
def on_checkbox_toggled(self, checkbox): def on_checkbox_toggled(self, checkbox):
self.on_toggle(self.app['name'], checkbox.get_active()) self.on_toggle(self.app['name'], checkbox.get_active())
self.update_row_css() # Update the row's style when toggled self.update_row_css() # Update the row's style when toggled
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment