Commit be01aa8d authored by librebob's avatar librebob

Add support for updating flatpak repo and refreshing db.

parent 3fb01674
......@@ -83,6 +83,7 @@ Page {
}
MenuItem {
text: "Update All"
onTriggered: window.updateAll()
}
MenuItem {
text: "Exit"
......
......@@ -28,39 +28,9 @@ class Library(QObject):
self._processes = []
def load(self):
print('load')
stream = appstream.Store()
stream.from_file('/var/lib/flatpak/appstream/flathub/x86_64/active/appstream.xml.gz')
for component in stream.get_components():
if component.project_license and not 'LicenseRef-proprietary' in component.project_license and not 'CC-BY-NC-SA' in component.project_license:
if 'Game' in component.categories:
try:
gr = GameRecord.get(GameRecord.id == component.id)
except DoesNotExist:
gr = None
self.appendGame(Game(component.id, component.name, self.getIconSmall(component.icons), self.getIconLarge(component.icons), component.bundle['value'], gr.installed if gr else False))
self.filter = self.games
self.indexUpdated(0)
def getIconSmall(self, icons):
path = '/var/lib/flatpak/appstream/flathub/x86_64/active/icons'
if icons['cached'][0]['height'] == '64':
return path + '/64x64/' + icons['cached'][0]['value']
elif icons['cached'][1]['height'] == '64':
return path + '/64x64/' + icons['cached'][0]['value']
else:
return path + '/128x128/' + icons['cached'][0]['height']['value']
def getIconLarge(self, icons):
path = '/var/lib/flatpak/appstream/flathub/x86_64/active/icons'
cached_icon = icons['cached'][0]
if cached_icon['height'] == '128':
return path + '/128x128/' + cached_icon['value']
elif cached_icon['height'] == '64':
return path + '/64x64/' + cached_icon['value']
def findById(self, game_id):
for index, game in enumerate(self.games):
if game.id == game_id:
......
......@@ -3,16 +3,25 @@ from functools import partial
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, QProcess, QTimer
from models import getMeta, setMeta
import appstream
from models import getMeta, setMeta, getGame, setGame, DoesNotExist
from game import Game
class Loader(QObject):
finished = pyqtSignal()
stateChanged = pyqtSignal()
messageChanged = pyqtSignal()
errorChanged = pyqtSignal()
gameLoaded = pyqtSignal(Game)
arch = 'x86_64'
metaKey = 'flathub_added'
flatHub = {'name':'flathub', 'url':'https://flathub.org/repo/flathub.flatpakrepo'}
appsteamPath = '/var/lib/flatpak/appstream/{repo}/{arch}/active/appstream.xml.gz'
messages = [
'Mining Mese blocks...',
'Peeling bananas...',
......@@ -33,7 +42,7 @@ class Loader(QObject):
def load(self):
if getMeta(self.metaKey):
self.finishLoading()
self.loadAppstream()
else:
self.runCommands()
......@@ -43,15 +52,68 @@ class Loader(QObject):
if proc_number == 0:
commandProcess.started.connect(self.startLoading)
commandProcess.finished.connect(partial(self.runCommands, 1))
commandProcess.start('flatpak', ['remote-add', '--if-not-exists', 'flathub', 'https://flathub.org/repo/flathub.flatpakrepo'])
commandProcess.start('flatpak', ['remote-add', '--if-not-exists', self.flatHub['name'], self.flatHub['url']])
elif proc_number == 1:
commandProcess.finished.connect(partial(self.runCommands, 2))
commandProcess.start('flatpak', ['remote-ls', '--updates'])
elif proc_number == 2:
commandProcess.finished.connect(self.finishLoading)
commandProcess.finished.connect(partial(self.runCommands, 3))
commandProcess.start('flatpak', ['update', '--appstream'])
elif proc_number == 3:
commandProcess.finished.connect(partial(self.loadAppstream, commandProcess))
commandProcess.start('flatpak', ['list'])
self._processes.append(commandProcess)
def loadAppstream(self, process=None):
if process:
installed_list = str(process.readAllStandardOutput(), 'utf-8')
stream = appstream.Store()
stream.from_file(self.appsteamPath.format(repo=self.flatHub['name'], arch=self.arch))
for component in stream.get_components():
if component.project_license:
if not 'LicenseRef-proprietary' in component.project_license:
if not 'CC-BY-NC-SA' in component.project_license:
if 'Game' in component.categories:
if process:
installed = component.bundle['value'][4:] in installed_list
setGame(component.id, installed)
else:
try:
gr = getGame(component.id)
installed = gr.installed
except DoesNotExist:
installed = False
self.gameLoaded.emit(
Game(
id=component.id,
name=component.name,
iconSmall=self.getIconSmall(component.icons),
iconLarge=self.getIconLarge(component.icons),
ref=component.bundle['value'],
installed=installed
)
)
self.finishLoading()
def getIconSmall(self, icons):
path = '/var/lib/flatpak/appstream/flathub/x86_64/active/icons'
if icons['cached'][0]['height'] == '64':
return path + '/64x64/' + icons['cached'][0]['value']
elif icons['cached'][1]['height'] == '64':
return path + '/64x64/' + icons['cached'][0]['value']
else:
return path + '/128x128/' + icons['cached'][0]['height']['value']
def getIconLarge(self, icons):
path = '/var/lib/flatpak/appstream/flathub/x86_64/active/icons'
cached_icon = icons['cached'][0]
if cached_icon['height'] == '128':
return path + '/128x128/' + cached_icon['value']
elif cached_icon['height'] == '64':
return path + '/64x64/' + cached_icon['value']
@pyqtProperty(bool, notify=stateChanged)
def loading(self):
return self._loading
......
......@@ -4,8 +4,6 @@ from sys import argv
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType
from peewee import SqliteDatabase
from game import Game
from library import Library
from loader import Loader
......@@ -29,6 +27,7 @@ def main():
library = Library()
loader.finished.connect(library.load)
loader.gameLoaded.connect(library.appendGame)
loader.load()
......@@ -44,6 +43,7 @@ def main():
engine.rootObjects()[0].updateGame.connect(library.updateGame)
engine.rootObjects()[0].playGame.connect(library.playGame)
engine.rootObjects()[0].search.connect(library.search)
engine.rootObjects()[0].updateAll.connect(loader.runCommands)
exit(app.exec_())
......
......@@ -14,6 +14,7 @@ ApplicationWindow {
signal updateGame(string id)
signal playGame(string id)
signal search(string query)
signal updateAll()
width: 1000
height: 700
......
from peewee import *
import datetime
from peewee import DoesNotExist
db = SqliteDatabase('store.db')
class BaseModel(Model):
......@@ -31,3 +29,16 @@ def getMeta(key):
def setMeta(key, value):
MetaRecord.replace(key=key, value=value).execute()
def getGame(id):
try:
return GameRecord.get(GameRecord.id == id)
except DoesNotExist:
return None
def setGame(id, installed):
GameRecord.replace(
id=id,
installed=installed,
modified_date=datetime.datetime.now()
).execute()
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