Marketplace.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Callable, cast, Dict, List, Optional
  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 .InstallMissingPackagesDialog import InstallMissingPackageDialog # To allow creating this dialogue from outside of the plug-in.
  10. from .LocalPackageList import LocalPackageList # To register this type with QML.
  11. from .RemotePackageList import RemotePackageList # To register this type with QML.
  12. class Marketplace(Extension, QObject):
  13. """
  14. The main managing object for the Marketplace plug-in.
  15. """
  16. def __init__(self, parent: Optional[QObject] = None) -> None:
  17. QObject.__init__(self, parent)
  18. Extension.__init__(self)
  19. self._window: Optional["QObject"] = None # If the window has been loaded yet, it'll be cached in here.
  20. self._plugin_registry: Optional[PluginRegistry] = None
  21. self._package_manager = CuraApplication.getInstance().getPackageManager()
  22. self._material_package_list: Optional[RemotePackageList] = None
  23. self._plugin_package_list: Optional[RemotePackageList] = None
  24. # Not entirely the cleanest code, since the localPackage list also checks the server if there are updates
  25. # Since that in turn will trigger notifications to be shown, we do need to construct it here and make sure
  26. # that it checks for updates...
  27. preferences = CuraApplication.getInstance().getPreferences()
  28. preferences.addPreference("info/automatic_plugin_update_check", True)
  29. self._local_package_list = LocalPackageList(self)
  30. if preferences.getValue("info/automatic_plugin_update_check"):
  31. self._local_package_list.checkForUpdates(self._package_manager.local_packages)
  32. self._package_manager.installedPackagesChanged.connect(self.checkIfRestartNeeded)
  33. self._tab_shown: int = 0
  34. self._restart_needed = False
  35. self.missingPackageDialog = None
  36. def getTabShown(self) -> int:
  37. return self._tab_shown
  38. def setTabShown(self, tab_shown: int) -> None:
  39. if tab_shown != self._tab_shown:
  40. self._tab_shown = tab_shown
  41. self.tabShownChanged.emit()
  42. tabShownChanged = pyqtSignal()
  43. tabShown = pyqtProperty(int, fget=getTabShown, fset=setTabShown, notify=tabShownChanged)
  44. @pyqtProperty(QObject, constant=True)
  45. def MaterialPackageList(self):
  46. if self._material_package_list is None:
  47. self._material_package_list = RemotePackageList()
  48. self._material_package_list.packageTypeFilter = "material"
  49. return self._material_package_list
  50. @pyqtProperty(QObject, constant=True)
  51. def PluginPackageList(self):
  52. if self._plugin_package_list is None:
  53. self._plugin_package_list = RemotePackageList()
  54. self._plugin_package_list.packageTypeFilter = "plugin"
  55. return self._plugin_package_list
  56. @pyqtProperty(QObject, constant=True)
  57. def LocalPackageList(self):
  58. return self._local_package_list
  59. @pyqtSlot()
  60. def show(self) -> None:
  61. """
  62. Opens the window of the Marketplace.
  63. If the window hadn't been loaded yet into Qt, it will be created lazily.
  64. """
  65. if self._window is None:
  66. self._plugin_registry = PluginRegistry.getInstance()
  67. self._plugin_registry.pluginsEnabledOrDisabledChanged.connect(self.checkIfRestartNeeded)
  68. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  69. if plugin_path is None:
  70. plugin_path = os.path.dirname(__file__)
  71. path = os.path.join(plugin_path, "resources", "qml", "Marketplace.qml")
  72. self._window = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  73. if self._window is None: # Still None? Failed to load the QML then.
  74. return
  75. if not self._window.isVisible():
  76. self.setTabShown(0)
  77. self._window.show()
  78. self._window.requestActivate() # Bring window into focus, if it was already open in the background.
  79. @pyqtSlot()
  80. def setVisibleTabToMaterials(self) -> None:
  81. """
  82. Set the tab shown to the remote materials one.
  83. Not implemented in a more generic way because it needs the ability to be called with 'callExtensionMethod'.
  84. """
  85. self.setTabShown(1)
  86. def checkIfRestartNeeded(self) -> None:
  87. if self._window is None:
  88. return
  89. if self._package_manager.hasPackagesToRemoveOrInstall or \
  90. cast(PluginRegistry, self._plugin_registry).getCurrentSessionActivationChangedPlugins():
  91. self._restart_needed = True
  92. else:
  93. self._restart_needed = False
  94. self.showRestartNotificationChanged.emit()
  95. showRestartNotificationChanged = pyqtSignal()
  96. @pyqtProperty(bool, notify=showRestartNotificationChanged)
  97. def showRestartNotification(self) -> bool:
  98. return self._restart_needed
  99. def showInstallMissingPackageDialog(self, packages_metadata: List[Dict[str, str]], ignore_warning_callback: Callable[[], None]) -> None:
  100. """
  101. Show a dialog that prompts the user to install certain packages.
  102. The dialog is worded for packages that are missing and required for a certain operation.
  103. :param packages_metadata: The metadata of the packages that are missing.
  104. :param ignore_warning_callback: A callback that gets executed when the user ignores the pop-up, to show them a
  105. warning.
  106. """
  107. self.missingPackageDialog = InstallMissingPackageDialog(packages_metadata, ignore_warning_callback)
  108. self.missingPackageDialog.show()