Moved the update logic to the PackageManager

Contributes to CURA-8587
This commit is contained in:
Jelle Spijker 2021-12-10 11:14:40 +01:00
parent 4991c39535
commit d9f77d7ffd
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A
4 changed files with 29 additions and 59 deletions

View file

@ -1,5 +1,5 @@
# Copyright (c) 2021 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from typing import Any, Dict, List, Optional, TYPE_CHECKING from typing import Any, Dict, List, Optional, TYPE_CHECKING
from operator import attrgetter from operator import attrgetter
@ -39,6 +39,7 @@ class LocalPackageList(PackageList):
super().__init__(parent) super().__init__(parent)
self._has_footer = False self._has_footer = False
self._ongoing_requests["check_updates"] = None self._ongoing_requests["check_updates"] = None
self._manager.packagesWithUpdateChanged.connect(lambda: self.sort(attrgetter("sectionTitle", "_can_update", "displayName"), key = "package", reverse = True))
@pyqtSlot() @pyqtSlot()
def updatePackages(self) -> None: def updatePackages(self) -> None:
@ -92,16 +93,6 @@ class LocalPackageList(PackageList):
if len(response_data["data"]) == 0: if len(response_data["data"]) == 0:
return return
try: packages = response_data["data"]
for package_data in response_data["data"]: self._manager.setPackagesWithUpdate(dict(zip([p['package_id'] for p in packages], [p for p in packages])))
package = self.getPackageModel(package_data["package_id"]) self._ongoing_requests["check_updates"] = None
package.download_url = package_data.get("download_url", "")
package.setCanUpdate(True)
self.sort(attrgetter("sectionTitle", "_can_update", "displayName"), key = "package", reverse = True)
self._ongoing_requests["check_updates"] = None
except RuntimeError:
# Setting the ownership of this object to not qml can still result in a RuntimeError. Which can occur when quickly toggling
# between de-/constructing RemotePackageLists. This try-except is here to prevent a hard crash when the wrapped C++ object
# was deleted when it was still parsing the response
return

View file

@ -1,5 +1,5 @@
# Copyright (c) 2021 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import tempfile import tempfile
import json import json
import os.path import os.path
@ -268,13 +268,11 @@ class PackageList(ListModel):
package.uninstallPackageTriggered.connect(self.uninstallPackage) package.uninstallPackageTriggered.connect(self.uninstallPackage)
package.updatePackageTriggered.connect(self.updatePackage) package.updatePackageTriggered.connect(self.updatePackage)
def installPackage(self, package_id: str) -> None: def installPackage(self, package_id: str, url: str) -> None:
"""Install a package from the Marketplace """Install a package from the Marketplace
:param package_id: the package identification string :param package_id: the package identification string
""" """
package = self.getPackageModel(package_id)
url = package.download_url
self.download(package_id, url, False) self.download(package_id, url, False)
def uninstallPackage(self, package_id: str) -> None: def uninstallPackage(self, package_id: str) -> None:
@ -282,7 +280,6 @@ class PackageList(ListModel):
:param package_id: the package identification string :param package_id: the package identification string
""" """
package = self.getPackageModel(package_id)
self._manager.removePackage(package_id) self._manager.removePackage(package_id)
self.unsunscribeUserFromPackage(package_id) self.unsunscribeUserFromPackage(package_id)
@ -291,7 +288,6 @@ class PackageList(ListModel):
:param package_id: the package identification string :param package_id: the package identification string
""" """
package = self.getPackageModel(package_id)
self._manager.removePackage(package_id, force_add = True) self._manager.removePackage(package_id, force_add = True)
url = package.download_url url = self._manager.packagesWithUpdate[package_id]["download_url"]
self.download(package_id, url, True) self.download(package_id, url, True)

View file

@ -1,5 +1,5 @@
# Copyright (c) 2021 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import re import re
from enum import Enum from enum import Enum
@ -45,7 +45,7 @@ class PackageModel(QObject):
self._description = package_data.get("description", "") self._description = package_data.get("description", "")
self._formatted_description = self._format(self._description) self._formatted_description = self._format(self._description)
self.download_url = package_data.get("download_url", "") self._download_url = package_data.get("download_url", "")
self._release_notes = package_data.get("release_notes", "") # Not used yet, propose to add to description? self._release_notes = package_data.get("release_notes", "") # Not used yet, propose to add to description?
subdata = package_data.get("data", {}) subdata = package_data.get("data", {})
@ -72,32 +72,21 @@ class PackageModel(QObject):
self.sdk_version = package_data.get("sdk_version_semver", "") self.sdk_version = package_data.get("sdk_version_semver", "")
# Note that there's a lot more info in the package_data than just these specified here. # Note that there's a lot more info in the package_data than just these specified here.
def install_clicked(package_id): self.enablePackageTriggered.connect(self._plugin_registry.enablePlugin)
self._install_status_changing = True self.disablePackageTriggered.connect(self._plugin_registry.disablePlugin)
self.setIsInstalling(True) self.installPackageTriggered.connect(lambda pkg_id: self.setIsInstalling(True))
self.uninstallPackageTriggered.connect(lambda pkg_id: self.setIsInstalling(True))
self.installPackageTriggered.connect(install_clicked) self.updatePackageTriggered.connect(lambda pkg_id: self.setIsUpdating(True))
def uninstall_clicked(package_id):
self._install_status_changing = False
self.setIsInstalling(True)
self.uninstallPackageTriggered.connect(uninstall_clicked)
def update_clicked(package_id):
self.setIsUpdating(True)
self.updatePackageTriggered.connect(update_clicked)
def finished_installed(): def finished_installed():
self.setIsUpdating(False) self.setIsUpdating(False)
self.setIsInstalling(False) self.setIsInstalling(False)
self._package_manager.installedPackagesChanged.connect(finished_installed) self._package_manager.installedPackagesChanged.connect(finished_installed)
self.enablePackageTriggered.connect(self._plugin_registry.enablePlugin)
self.disablePackageTriggered.connect(self._plugin_registry.disablePlugin)
self._plugin_registry.hasPluginsEnabledOrDisabledChanged.connect(self.stateManageButtonChanged) self._plugin_registry.hasPluginsEnabledOrDisabledChanged.connect(self.stateManageButtonChanged)
self._package_manager.packagesWithUpdateChanged.connect(lambda : self.setCanUpdate(self._package_id in self._package_manager.packagesWithUpdate))
def __eq__(self, other: object): def __eq__(self, other: object):
if isinstance(other, PackageModel): if isinstance(other, PackageModel):
return other == self return other == self
@ -298,11 +287,15 @@ class PackageModel(QObject):
def isBundled(self) -> bool: def isBundled(self) -> bool:
return self._is_bundled return self._is_bundled
@pyqtProperty(str, constant = True)
def downloadURL(self) -> str:
return self._download_url
# --- manage buttons signals --- # --- manage buttons signals ---
stateManageButtonChanged = pyqtSignal() stateManageButtonChanged = pyqtSignal()
installPackageTriggered = pyqtSignal(str) installPackageTriggered = pyqtSignal(str, str)
uninstallPackageTriggered = pyqtSignal(str) uninstallPackageTriggered = pyqtSignal(str)
@ -314,7 +307,6 @@ class PackageModel(QObject):
@pyqtProperty(bool, notify = stateManageButtonChanged) @pyqtProperty(bool, notify = stateManageButtonChanged)
def isActive(self): def isActive(self):
Logger.debug(f"getDisabledPlugins = {self._plugin_registry.getDisabledPlugins()}")
return not self._package_id in self._plugin_registry.getDisabledPlugins() return not self._package_id in self._plugin_registry.getDisabledPlugins()
def setIsInstalling(self, value: bool) -> None: def setIsInstalling(self, value: bool) -> None:
@ -326,15 +318,6 @@ class PackageModel(QObject):
def isInstalling(self) -> bool: def isInstalling(self) -> bool:
return self._is_installing return self._is_installing
def setInstallStatusChanging(self, value: bool) -> None:
if value != self._install_status_changing:
self._install_status_changing = value
self.stateManageButtonChanged.emit()
@pyqtProperty(bool, fset = setInstallStatusChanging, notify = stateManageButtonChanged)
def installStatusChanging(self) -> bool:
return self._install_status_changing
@pyqtProperty(bool, notify = stateManageButtonChanged) @pyqtProperty(bool, notify = stateManageButtonChanged)
def isInstalled(self) -> bool: def isInstalled(self) -> bool:
return self._package_id in self._package_manager.local_packages_id return self._package_id in self._package_manager.local_packages_id

View file

@ -254,7 +254,7 @@ Item
ManageButton ManageButton
{ {
id: updateManageButton id: updateManageButton
visible: (showManageButtons || confirmed) && packageData.canUpdate && !installManageButton.confirmed visible: (showManageButtons || confirmed) && (packageData.canUpdate || confirmed) && !installManageButton.confirmed
enabled: !installManageButton.busy enabled: !installManageButton.busy
busy: packageData.isUpdating busy: packageData.isUpdating
@ -265,12 +265,12 @@ Item
text: text:
{ {
if (packageData.isUpdating) { return catalog.i18nc("@button", "Updating..."); } if (busy) { return catalog.i18nc("@button", "Updating..."); }
else if (packageData.isRecentlyUpdated) { return catalog.i18nc("@button", "Updated"); } else if (confirmed) { return catalog.i18nc("@button", "Updated"); }
else { return catalog.i18nc("@button", "Update"); } else { return catalog.i18nc("@button", "Update"); }
} }
onClicked: packageData.updatePackageTriggered(packageData.packageId) onClicked: packageData.updatePackageTriggered(packageData.packageId, packageData.downloadURL)
} }
} }
} }