Commit bce75bd4 authored by librebob's avatar librebob

Refactor global database functions into repository classes to better suit unit testing.

Fix detecting installed games on launch.
parent e248efbd
......@@ -17,7 +17,7 @@ from game import Game
from settings import Settings
from library import Library
from loader import Loader
from models import GameRecord, MetaRecord, SettingsRecord, initDatabase
from models import initDatabase, MetaRepository, SettingRepository, GameRepository
from systemtrayicon import SystemTrayIcon
......@@ -54,10 +54,14 @@ def main():
qmlRegisterType(Library, 'Athenaeum', 1, 0, 'Library')
qmlRegisterType(Loader, 'Athenaeum', 1, 0, 'Loader')
qmlRegisterType(Settings, 'Athenaeum', 1, 0, 'Settings')
metaRepository = MetaRepository()
settingRepository = SettingRepository()
gameRepository = GameRepository()
settings = Settings(parent=app)
loader = Loader(parent=app, flatpak=inFlatpak)
library = Library(parent=app, flatpak=inFlatpak)
settings = Settings(parent=app, settingRepository=settingRepository)
loader = Loader(parent=app, flatpak=inFlatpak, metaRepository=metaRepository, gameRepository=gameRepository)
library = Library(parent=app, flatpak=inFlatpak, metaRepository=metaRepository, gameRepository=gameRepository)
loader.started.connect(library.reset)
loader.finished.connect(library.load)
......
......@@ -6,7 +6,6 @@ from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, QProcess
from PyQt5.QtQml import QQmlListProperty
from game import Game
from models import setMeta, getMeta
class Library(QObject):
......@@ -18,13 +17,15 @@ class Library(QObject):
errorChanged = pyqtSignal()
displayNotification = pyqtSignal(int, str, arguments=['index', 'action'])
def __init__(self, flatpak=False, *args, **kwargs):
def __init__(self, flatpak=False, metaRepository=None, gameRepository=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self._flatpak = flatpak
self._metaRepository = metaRepository
self._gameRepository = gameRepository
self.reset()
def load(self):
self.filterValue = getMeta('filter')
self.filterValue = self._metaRepository.get('filter')
self.sortGames()
self.updateFilters(True)
......@@ -155,6 +156,7 @@ class Library(QObject):
installProcess.started.connect(self.updateFilters)
installProcess.finished.connect(partial(self.processCleanup, installProcess, idx, 'install'))
installProcess.finished.connect(partial(self._games[idx].finishInstall, installProcess))
installProcess.finished.connect(partial(self._gameRepository.set, self._games[idx]))
installProcess.finished.connect(self.updateFilters)
installProcess.readyReadStandardOutput.connect(partial(self._games[idx].appendLog, installProcess))
if self._flatpak:
......@@ -173,6 +175,7 @@ class Library(QObject):
uninstallProcess.started.connect(self.updateFilters)
uninstallProcess.finished.connect(partial(self.processCleanup, uninstallProcess, idx, 'uninstall'))
uninstallProcess.finished.connect(partial(self._games[idx].finishUninstall, uninstallProcess))
uninstallProcess.finished.connect(partial(self._gameRepository.set, self._games[idx]))
uninstallProcess.finished.connect(self.updateFilters)
uninstallProcess.readyReadStandardOutput.connect(partial(self._games[idx].appendLog, uninstallProcess))
if self._flatpak:
......@@ -190,6 +193,7 @@ class Library(QObject):
updateProcess.started.connect(self.updateFilters)
updateProcess.finished.connect(partial(self.processCleanup, updateProcess, idx, 'update'))
updateProcess.finished.connect(partial(self._games[idx].finishUpdate, updateProcess))
updateProcess.finished.connect(partial(self._gameRepository.set, self._games[idx]))
updateProcess.finished.connect(self.updateFilters)
updateProcess.readyReadStandardOutput.connect(partial(self._games[idx].appendLog, updateProcess))
if self._flatpak:
......@@ -206,6 +210,7 @@ class Library(QObject):
playProcess.started.connect(self.updateFilters)
playProcess.finished.connect(partial(self.processCleanup, playProcess, idx))
playProcess.finished.connect(partial(self._games[idx].stopGame, playProcess))
playProcess.finished.connect(partial(self._gameRepository.set, self._games[idx]))
playProcess.finished.connect(self.updateFilters)
playProcess.readyReadStandardOutput.connect(partial(self._games[idx].appendLog, playProcess))
playProcess.readyReadStandardError.connect(partial(self._games[idx].appendLog, playProcess))
......@@ -240,7 +245,7 @@ class Library(QObject):
self.filterValue = filter
self.filter = self._filters[filter]
setMeta(key='filter', value=filter)
self._metaRepository.set(key='filter', value=filter)
def updateFilters(self, new_load=False):
filters = {
......
......@@ -5,7 +5,7 @@ from functools import partial
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, QProcess, QTimer, QStandardPaths
import appstream
from models import getMeta, setMeta, getGame, setGame, createDatabase, initDatabase, eraseDatabase
from models import createDatabase, initDatabase, eraseDatabase
from game import Game, Screenshot, Release, Url
from lists import badLicenses, badCategories, loadingMessages
......@@ -26,9 +26,11 @@ class Loader(QObject):
flatHub = {'name':'flathub', 'url':'https://flathub.org/repo/flathub.flatpakrepo', 'git':'https://github.com/flathub'}
def __init__(self, flatpak=False, *args, **kwargs):
def __init__(self, flatpak=False, metaRepository=None, gameRepository=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self._flatpak = flatpak
self._metaRepository = metaRepository
self._gameRepository = gameRepository
self._loading = True
self._error = False
self._processes = []
......@@ -43,7 +45,7 @@ class Loader(QObject):
self._updates_list = ''
def load(self):
if getMeta(self.metaKey):
if self._metaRepository.get(self.metaKey):
self.loadAppstream()
else:
self.runUpdateCommands()
......@@ -89,15 +91,15 @@ class Loader(QObject):
commandProcess.finished.connect(partial(self.runListCommands, 1))
commandProcess.finished.connect(partial(self.loadListData, commandProcess, proc_number))
if self._flatpak:
commandProcess.start('flatpak-spawn', ['--host', 'flatpak', 'list', '--user'])
commandProcess.start('flatpak-spawn', ['--host', 'flatpak', 'list', '--user', '--app', '--columns=application'])
else:
commandProcess.start('flatpak', ['list', '--user'])
commandProcess.start('flatpak', ['list', '--user', '--app', '--columns=application'])
if proc_number == 1:
commandProcess.finished.connect(partial(self.loadListData, commandProcess, proc_number))
if self._flatpak:
commandProcess.start('flatpak-spawn', ['--host', 'flatpak', 'remote-ls', '--updates', '--user'])
commandProcess.start('flatpak-spawn', ['--host', 'flatpak', 'remote-ls', '--updates', '--user', '--app', '--columns=application'])
else:
commandProcess.start('flatpak', ['remote-ls', '--updates', '--user'])
commandProcess.start('flatpak', ['remote-ls', '--updates', '--user', '--app', '--columns=application'])
self._processes.append(commandProcess)
def loadListData(self, process, proc_number):
......@@ -120,13 +122,14 @@ class Loader(QObject):
created_date = None
if process:
name = component.bundle['value'][4:]
name = (component.id[:-8] if component.id.endswith('.desktop') else component.id)
installed = name in self._installed_list
has_update = name.split('/')[0] in self._updates_list
gr = getGame(component.id)
gr = self._gameRepository.get(component.id)
if gr:
installed = gr.installed
if not process:
installed = gr.installed
if not process:
has_update = gr.has_update
last_played_date = gr.last_played_date
......@@ -157,7 +160,7 @@ class Loader(QObject):
)
if process:
setGame(game=game)
self._gameRepository.set(game=game)
self.gameLoaded.emit(game)
self.finishLoading()
......@@ -266,7 +269,7 @@ class Loader(QObject):
self.started.emit()
def finishLoading(self):
setMeta(self.metaKey, 'y')
self._metaRepository.set(self.metaKey, 'y')
self.loading = False
self.finished.emit()
......
......@@ -31,39 +31,41 @@ class SettingsRecord(BaseModel):
key = CharField(unique=True)
value = TextField()
def getMeta(key):
try:
return (MetaRecord.get(MetaRecord.key == key)).value
except DoesNotExist:
return None
def setMeta(key, value):
MetaRecord.insert(key=key, value=value).on_conflict(action='REPLACE').execute()
def getSetting(key):
try:
return json.loads((SettingsRecord.get(SettingsRecord.key == key)).value)
except DoesNotExist:
return None
def setSetting(key, value):
SettingsRecord.insert(key=key, value=json.dumps(value)).on_conflict(action='REPLACE').execute()
def getGame(id):
try:
return GameRecord.get(GameRecord.id == id)
except DoesNotExist:
return None
def setGame(game):
GameRecord.insert(
id=game.id,
installed=game.installed,
has_update=game.hasUpdate,
created_date=game.createdDate,
last_played_date=game.lastPlayedDate
).on_conflict(action='REPLACE').execute()
class MetaRepository():
def get(self, key):
try:
return (MetaRecord.get(MetaRecord.key == key)).value
except DoesNotExist:
return None
def set(self, key, value):
MetaRecord.insert(key=key, value=value).on_conflict(action='REPLACE').execute()
class SettingRepository():
def get(self, key):
try:
return json.loads((SettingsRecord.get(SettingsRecord.key == key)).value)
except DoesNotExist:
return None
def set(self, key, value):
SettingsRecord.insert(key=key, value=json.dumps(value)).on_conflict(action='REPLACE').execute()
class GameRepository():
def get(self, id):
try:
return GameRecord.get(GameRecord.id == id)
except DoesNotExist:
return None
def set(self, game):
GameRecord.insert(
id=game.id,
installed=game.installed,
has_update=game.hasUpdate,
created_date=game.createdDate,
last_played_date=game.lastPlayedDate
).on_conflict(action='REPLACE').execute()
def createDatabase():
try:
......
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal
from models import setSetting, getSetting
class Settings(QObject):
showTrayIconChanged = pyqtSignal(bool)
closeToTrayChanged = pyqtSignal(bool)
......@@ -9,17 +7,18 @@ class Settings(QObject):
notificationsEnabledChanged = pyqtSignal(bool)
themeChanged = pyqtSignal(str)
def __init__(self, *args, **kwargs):
def __init__(self, *args, settingRepository=None, **kwargs):
super().__init__(*args, **kwargs)
showTrayIcon = getSetting('show_tray_icon')
self._settingRepository = settingRepository
showTrayIcon = self._settingRepository.get('show_tray_icon')
self._showTrayIcon = True if showTrayIcon is None else showTrayIcon
closeToTray = getSetting('close_to_tray')
closeToTray = self._settingRepository.get('close_to_tray')
self._closeToTray = True if closeToTray is None else closeToTray
alwaysShowLogs = getSetting('close_to_tray')
alwaysShowLogs = self._settingRepository.get('close_to_tray')
self._alwaysShowLogs = False if alwaysShowLogs is None else alwaysShowLogs
notificationsEnabled = getSetting('notifications_enabled')
notificationsEnabled = self._settingRepository.get('notifications_enabled')
self._notificationsEnabled = True if notificationsEnabled is None else notificationsEnabled
theme = getSetting('theme')
theme = self._settingRepository.get('theme')
self._theme = 'Dark' if theme is None else theme
@pyqtProperty(bool, notify=showTrayIconChanged)
......@@ -30,7 +29,7 @@ class Settings(QObject):
def showTrayIcon(self, showTrayIcon):
if showTrayIcon != self._showTrayIcon:
self._showTrayIcon = showTrayIcon
setSetting('show_tray_icon', showTrayIcon)
self._settingRepository.set('show_tray_icon', showTrayIcon)
self.showTrayIconChanged.emit(showTrayIcon)
@pyqtProperty(bool, notify=closeToTrayChanged)
......@@ -41,7 +40,7 @@ class Settings(QObject):
def closeToTray(self, closeToTray):
if closeToTray != self._closeToTray:
self._closeToTray = closeToTray
setSetting('close_to_tray', closeToTray)
self._settingRepository.set('close_to_tray', closeToTray)
print(not closeToTray)
self.closeToTrayChanged.emit(not closeToTray)
......@@ -53,7 +52,7 @@ class Settings(QObject):
def alwaysShowLogs(self, alwaysShowLogs):
if alwaysShowLogs != self._alwaysShowLogs:
self._alwaysShowLogs = alwaysShowLogs
setSetting('close_to_tray', alwaysShowLogs)
self._settingRepository.set('close_to_tray', alwaysShowLogs)
self.alwaysShowLogsChanged.emit(alwaysShowLogs)
@pyqtProperty(bool, notify=notificationsEnabledChanged)
......@@ -64,7 +63,7 @@ class Settings(QObject):
def notificationsEnabled(self, notificationsEnabled):
if notificationsEnabled != self._notificationsEnabled:
self._notificationsEnabled = notificationsEnabled
setSetting('notifications_enabled', notificationsEnabled)
self._settingRepository.set('notifications_enabled', notificationsEnabled)
self.notificationsEnabledChanged.emit(notificationsEnabled)
@pyqtProperty(str, notify=themeChanged)
......@@ -75,6 +74,6 @@ class Settings(QObject):
def theme(self, theme):
if theme != self._theme:
self._theme = theme
setSetting('theme', theme)
self._settingRepository.set('theme', theme)
self.themeChanged.emit(theme)
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