Marketplace.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 PyQt6.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. preferences = CuraApplication.getInstance().getPreferences()
  27. preferences.addPreference("info/automatic_plugin_update_check", True)
  28. self._local_package_list = LocalPackageList(self)
  29. if preferences.getValue("info/automatic_plugin_update_check"):
  30. self._local_package_list.checkForUpdates(self._package_manager.local_packages)
  31. self._package_manager.installedPackagesChanged.connect(self.checkIfRestartNeeded)
  32. self._tab_shown: int = 0
  33. self._restart_needed = False
  34. def getTabShown(self) -> int:
  35. return self._tab_shown
  36. def setTabShown(self, tab_shown: int) -> None:
  37. if tab_shown != self._tab_shown:
  38. self._tab_shown = tab_shown
  39. self.tabShownChanged.emit()
  40. tabShownChanged = pyqtSignal()
  41. tabShown = pyqtProperty(int, fget=getTabShown, fset=setTabShown, notify=tabShownChanged)
  42. @pyqtProperty(QObject, constant=True)
  43. def MaterialPackageList(self):
  44. if self._material_package_list is None:
  45. self._material_package_list = RemotePackageList()
  46. self._material_package_list.packageTypeFilter = "material"
  47. return self._material_package_list
  48. @pyqtProperty(QObject, constant=True)
  49. def PluginPackageList(self):
  50. if self._plugin_package_list is None:
  51. self._plugin_package_list = RemotePackageList()
  52. self._plugin_package_list.packageTypeFilter = "plugin"
  53. return self._plugin_package_list
  54. @pyqtProperty(QObject, constant=True)
  55. def LocalPackageList(self):
  56. return self._local_package_list
  57. @pyqtSlot()
  58. def show(self) -> None:
  59. """
  60. Opens the window of the Marketplace.
  61. If the window hadn't been loaded yet into Qt, it will be created lazily.
  62. """
  63. if self._window is None:
  64. self._plugin_registry = PluginRegistry.getInstance()
  65. self._plugin_registry.pluginsEnabledOrDisabledChanged.connect(self.checkIfRestartNeeded)
  66. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  67. if plugin_path is None:
  68. plugin_path = os.path.dirname(__file__)
  69. path = os.path.join(plugin_path, "resources", "qml", "Marketplace.qml")
  70. self._window = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  71. if self._window is None: # Still None? Failed to load the QML then.
  72. return
  73. if not self._window.isVisible():
  74. self.setTabShown(0)
  75. self._window.show()
  76. self._window.requestActivate() # Bring window into focus, if it was already open in the background.
  77. @pyqtSlot()
  78. def setVisibleTabToMaterials(self) -> None:
  79. """
  80. Set the tab shown to the remote materials one.
  81. Not implemented in a more generic way because it needs the ability to be called with 'callExtensionMethod'.
  82. """
  83. self.setTabShown(1)
  84. def checkIfRestartNeeded(self) -> None:
  85. if self._package_manager.hasPackagesToRemoveOrInstall or \
  86. cast(PluginRegistry, self._plugin_registry).getCurrentSessionActivationChangedPlugins():
  87. self._restart_needed = True
  88. else:
  89. self._restart_needed = False
  90. self.showRestartNotificationChanged.emit()
  91. showRestartNotificationChanged = pyqtSignal()
  92. @pyqtProperty(bool, notify=showRestartNotificationChanged)
  93. def showRestartNotification(self) -> bool:
  94. return self._restart_needed