loading flatpak db in a different thread

This commit is contained in:
Vinicius Moreira
2019-06-13 15:20:57 -03:00
parent d5142662eb
commit 05a33c9d63
5 changed files with 38 additions and 14 deletions

View File

@@ -4,7 +4,6 @@ Wriiten in Python for QT5.
### Roadmap
- Test installer for Ubuntu
- Load the database in a thread when the application starts. It will prevent the delay of showing the icon.
- Locales
- System notification whe new updates
- Search and install applications.

7
app.py
View File

@@ -1,4 +1,5 @@
#!env/bin/python
import argparse
import sys
from PyQt5.QtWidgets import QApplication, QWidget
@@ -6,9 +7,15 @@ from PyQt5.QtWidgets import QApplication, QWidget
from core.controller import FlatpakController
from core.model import FlatpakManager
from view.qt.systray import TrayIcon
from core import __version__
parser = argparse.ArgumentParser(prog='fpakman', description="GUI for Flatpak applications management")
parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(__version__))
args = parser.parse_args()
app = QApplication(sys.argv)
manager = FlatpakManager()
manager.load_database_async()
controller = FlatpakController(manager)
hidden_widget = QWidget()
trayIcon = TrayIcon(parent=hidden_widget, controller=controller, check_interval=30)

View File

@@ -1,3 +1,4 @@
from threading import Lock, Thread
from typing import List
import requests
@@ -17,15 +18,23 @@ class FlatpakManager:
self.apps = []
self.apps_db = {}
self.http_session = requests.Session()
self._load_database()
self.lock_db_read = Lock()
def _load_database(self):
def load_database_async(self):
Thread(target=self.load_database).start()
res = self.http_session.get(__FLATHUB_API_URL__ + '/apps')
def load_database(self):
if res.status_code == 200:
for app in res.json():
self.apps_db[app['flatpakAppId']] = app
self.lock_db_read.acquire()
try:
res = self.http_session.get(__FLATHUB_API_URL__ + '/apps')
if res.status_code == 200:
for app in res.json():
self.apps_db[app['flatpakAppId']] = app
finally:
self.lock_db_read.release()
def get_version(self):
return flatpak.get_version()
@@ -41,6 +50,9 @@ class FlatpakManager:
for app in installed:
if not self.apps_db:
self.load_database()
if self.apps_db:
app_data = self.apps_db.get(app['id'], None)
else:

2
run.sh
View File

@@ -1,2 +0,0 @@
#!/bin/bash
/home/dafiti/workspace/fpakman/env/bin/python /home/dafiti/workspace/fpakman/app.py

View File

@@ -3,8 +3,8 @@ from functools import reduce
from threading import Lock
from typing import List
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QUrl
from PyQt5.QtGui import QIcon, QColor, QPixmap
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QUrl, QEvent
from PyQt5.QtGui import QIcon, QColor, QPixmap, QWindowStateChangeEvent
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QTableWidget, \
QTableWidgetItem, QTableView, QCheckBox, QHeaderView, QToolButton, QToolBar, \
@@ -105,9 +105,7 @@ class ManageWindow(QWidget):
self.table_apps.setHorizontalHeaderLabels(ManageWindow.__COLUMNS__)
self.table_apps.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
header_horizontal = self.table_apps.horizontalHeader()
for i in range(0, len(ManageWindow.__COLUMNS__)):
header_horizontal.setSectionResizeMode(i, QHeaderView.Stretch + QHeaderView.AdjustToContents)
self._change_table_headers_policy()
self.layout.addWidget(self.table_apps)
@@ -130,6 +128,16 @@ class ManageWindow(QWidget):
self.centralize()
def _change_table_headers_policy(self, policy: QHeaderView = QHeaderView.ResizeToContents):
header_horizontal = self.table_apps.horizontalHeader()
for i in range(0, len(ManageWindow.__COLUMNS__)):
header_horizontal.setSectionResizeMode(i, policy)
def changeEvent(self, e: QEvent):
if isinstance(e, QWindowStateChangeEvent):
self._change_table_headers_policy(QHeaderView.Stretch if self.isMaximized() else QHeaderView.ResizeToContents)
def closeEvent(self, event):
if self.tray_icon: