mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-09 12:04:15 +02:00
improving initialization time and reducing async overload
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import time
|
||||
from threading import Lock
|
||||
from typing import List
|
||||
|
||||
from PyQt5.QtCore import QThread, pyqtSignal, QCoreApplication, Qt
|
||||
@@ -7,6 +8,8 @@ from PyQt5.QtWidgets import QSystemTrayIcon, QMenu
|
||||
|
||||
from fpakman.core import resource, system
|
||||
from fpakman.core.controller import FlatpakManager
|
||||
from fpakman.core.model import Application
|
||||
from fpakman.util.cache import Cache
|
||||
from fpakman.view.qt.about import AboutDialog
|
||||
from fpakman.view.qt.window import ManageWindow
|
||||
|
||||
@@ -26,7 +29,7 @@ class UpdateCheck(QThread):
|
||||
|
||||
apps = self.manager.read_installed()
|
||||
|
||||
updates = [app for app in apps if app['update']]
|
||||
updates = [app for app in apps if app.update]
|
||||
|
||||
if updates:
|
||||
self.signal.emit(updates)
|
||||
@@ -34,25 +37,13 @@ class UpdateCheck(QThread):
|
||||
time.sleep(self.check_interval)
|
||||
|
||||
|
||||
class LoadDatabase(QThread):
|
||||
|
||||
signal_finished = pyqtSignal()
|
||||
|
||||
def __init__(self, manager: FlatpakManager, parent=None):
|
||||
super(LoadDatabase, self).__init__(parent)
|
||||
self.manager = manager
|
||||
|
||||
def run(self):
|
||||
self.manager.load_full_database()
|
||||
self.signal_finished.emit()
|
||||
|
||||
|
||||
class TrayIcon(QSystemTrayIcon):
|
||||
|
||||
def __init__(self, locale_keys: dict, manager: FlatpakManager, check_interval: int = 60, update_notification: bool = True):
|
||||
def __init__(self, locale_keys: dict, manager: FlatpakManager, icon_cache: Cache, check_interval: int = 60, update_notification: bool = True):
|
||||
super(TrayIcon, self).__init__()
|
||||
self.locale_keys = locale_keys
|
||||
self.manager = manager
|
||||
self.icon_cache = icon_cache
|
||||
|
||||
self.icon_default = QIcon(resource.get_path('img/logo.png'))
|
||||
self.icon_update = QIcon(resource.get_path('img/logo_update.png'))
|
||||
@@ -60,12 +51,8 @@ class TrayIcon(QSystemTrayIcon):
|
||||
|
||||
self.menu = QMenu()
|
||||
|
||||
self.action_refreshing = self.menu.addAction(self.locale_keys['tray.action.refreshing'] + '...')
|
||||
self.action_refreshing.setEnabled(False)
|
||||
|
||||
self.action_manage = self.menu.addAction(self.locale_keys['tray.action.manage'])
|
||||
self.action_manage.triggered.connect(self.show_manage_window)
|
||||
self.action_manage.setVisible(False)
|
||||
|
||||
self.action_about = self.menu.addAction(self.locale_keys['tray.action.about'])
|
||||
self.action_about.triggered.connect(self.show_about)
|
||||
@@ -82,46 +69,45 @@ class TrayIcon(QSystemTrayIcon):
|
||||
|
||||
self.dialog_about = None
|
||||
|
||||
self.thread_database = LoadDatabase(manager)
|
||||
self.thread_database.signal_finished.connect(self._update_menu)
|
||||
self.last_updates = set()
|
||||
self.update_notification = update_notification
|
||||
self.lock_notify = Lock()
|
||||
|
||||
def load_database(self):
|
||||
self.thread_database.start()
|
||||
def notify_updates(self, updates: List[Application]):
|
||||
|
||||
def _update_menu(self):
|
||||
self.action_refreshing.setVisible(False)
|
||||
self.action_manage.setVisible(True)
|
||||
self.lock_notify.acquire()
|
||||
|
||||
def notify_updates(self, updates: List[dict]):
|
||||
try:
|
||||
if len(updates) > 0:
|
||||
|
||||
if len(updates) > 0:
|
||||
update_keys = {'{}:{}'.format(app.base_data.id, app.base_data.version) for app in updates}
|
||||
|
||||
update_keys = {'{}:{}'.format(app['id'], app['latest_version']) for app in updates}
|
||||
new_icon = self.icon_update
|
||||
|
||||
new_icon = self.icon_update
|
||||
if update_keys.difference(self.last_updates):
|
||||
self.last_updates = update_keys
|
||||
msg = '{}: {}'.format(self.locale_keys['notification.new_updates'].format('Flatpak'), len(updates))
|
||||
self.setToolTip(msg)
|
||||
|
||||
if update_keys.difference(self.last_updates):
|
||||
self.last_updates = update_keys
|
||||
msg = '{}: {}'.format(self.locale_keys['notification.new_updates'].format('Flatpak'), len(updates))
|
||||
self.setToolTip(msg)
|
||||
if self.update_notification:
|
||||
system.notify_user(msg)
|
||||
|
||||
if self.update_notification:
|
||||
system.notify_user(msg)
|
||||
else:
|
||||
new_icon = self.icon_default
|
||||
self.setToolTip(None)
|
||||
|
||||
else:
|
||||
new_icon = self.icon_default
|
||||
self.setToolTip(None)
|
||||
if self.icon().cacheKey() != new_icon.cacheKey(): # changes the icon if needed
|
||||
self.setIcon(new_icon)
|
||||
|
||||
if self.icon().cacheKey() != new_icon.cacheKey(): # changes the icon if needed
|
||||
self.setIcon(new_icon)
|
||||
finally:
|
||||
self.lock_notify.release()
|
||||
|
||||
def show_manage_window(self):
|
||||
|
||||
if self.manage_window is None:
|
||||
self.manage_window = ManageWindow(locale_keys=self.locale_keys,
|
||||
manager=self.manager,
|
||||
icon_cache=self.icon_cache,
|
||||
tray_icon=self)
|
||||
|
||||
if self.manage_window.isMinimized():
|
||||
|
||||
Reference in New Issue
Block a user