Marketplace.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os.path
  4. from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
  5. from typing import Optional, cast
  6. from cura.CuraApplication import CuraApplication # Creating QML objects and managing packages.
  7. from UM.Extension import Extension # We are implementing the main object of an extension here.
  8. from UM.PluginRegistry import PluginRegistry # To find out where we are stored (the proper way).
  9. from .RemotePackageList import RemotePackageList # To register this type with QML.
  10. from .LocalPackageList import LocalPackageList # To register this type with QML.
  11. class Marketplace(Extension, QObject):
  12. """
  13. The main managing object for the Marketplace plug-in.
  14. """
  15. def __init__(self, parent: Optional[QObject] = None) -> None:
  16. QObject.__init__(self, parent)
  17. Extension.__init__(self)
  18. self._window: Optional["QObject"] = None # If the window has been loaded yet, it'll be cached in here.
  19. self._plugin_registry: Optional[PluginRegistry] = None
  20. self._package_manager = CuraApplication.getInstance().getPackageManager()
  21. self._material_package_list: Optional[RemotePackageList] = None
  22. self._plugin_package_list: Optional[RemotePackageList] = None
  23. # Not entirely the cleanest code, since the localPackage list also checks the server if there are updates
  24. # Since that in turn will trigger notifications to be shown, we do need to construct it here and make sure
  25. # that it checks for updates...
  26. self._local_package_list = LocalPackageList(self)
  27. self._local_package_list.checkForUpdates(self._package_manager.local_packages)
  28. self._package_manager.installedPackagesChanged.connect(self.checkIfRestartNeeded)
  29. self._tab_shown: int = 0
  30. self._restart_needed = False
  31. def getTabShown(self) -> int:
  32. return self._tab_shown
  33. def setTabShown(self, tab_shown: int) -> None:
  34. if tab_shown != self._tab_shown:
  35. self._tab_shown = tab_shown
  36. self.tabShownChanged.emit()
  37. tabShownChanged = pyqtSignal()
  38. tabShown = pyqtProperty(int, fget=getTabShown, fset=setTabShown, notify=tabShownChanged)
  39. @pyqtProperty(QObject, constant=True)
  40. def MaterialPackageList(self):
  41. if self._material_package_list is None:
  42. self._material_package_list = RemotePackageList()
  43. self._material_package_list.packageTypeFilter = "material"
  44. return self._material_package_list
  45. @pyqtProperty(QObject, constant=True)
  46. def PluginPackageList(self):
  47. if self._plugin_package_list is None:
  48. self._plugin_package_list = RemotePackageList()
  49. self._plugin_package_list.packageTypeFilter = "plugin"
  50. return self._plugin_package_list
  51. @pyqtProperty(QObject, constant=True)
  52. def LocalPackageList(self):
  53. return self._local_package_list
  54. @pyqtSlot()
  55. def show(self) -> None:
  56. """
  57. Opens the window of the Marketplace.
  58. If the window hadn't been loaded yet into Qt, it will be created lazily.
  59. """
  60. if self._window is None:
  61. self._plugin_registry = PluginRegistry.getInstance()
  62. self._plugin_registry.pluginsEnabledOrDisabledChanged.connect(self.checkIfRestartNeeded)
  63. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  64. if plugin_path is None:
  65. plugin_path = os.path.dirname(__file__)
  66. path = os.path.join(plugin_path, "resources", "qml", "Marketplace.qml")
  67. self._window = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  68. if self._window is None: # Still None? Failed to load the QML then.
  69. return
  70. if not self._window.isVisible():
  71. self.setTabShown(0)
  72. self._window.show()
  73. self._window.requestActivate() # Bring window into focus, if it was already open in the background.
  74. @pyqtSlot()
  75. def setVisibleTabToMaterials(self) -> None:
  76. """
  77. Set the tab shown to the remote materials one.
  78. Not implemented in a more generic way because it needs the ability to be called with 'callExtensionMethod'.
  79. """
  80. self.setTabShown(1)
  81. def checkIfRestartNeeded(self) -> None:
  82. if self._package_manager.hasPackagesToRemoveOrInstall or \
  83. cast(PluginRegistry, self._plugin_registry).getCurrentSessionActivationChangedPlugins():
  84. self._restart_needed = True
  85. else:
  86. self._restart_needed = False
  87. self.showRestartNotificationChanged.emit()
  88. showRestartNotificationChanged = pyqtSignal()
  89. @pyqtProperty(bool, notify=showRestartNotificationChanged)
  90. def showRestartNotification(self) -> bool:
  91. return self._restart_needed