Marketplace.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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._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. self.missingPackageDialog = None
  35. def getTabShown(self) -> int:
  36. return self._tab_shown
  37. def setTabShown(self, tab_shown: int) -> None:
  38. if tab_shown != self._tab_shown:
  39. self._tab_shown = tab_shown
  40. self.tabShownChanged.emit()
  41. tabShownChanged = pyqtSignal()
  42. tabShown = pyqtProperty(int, fget=getTabShown, fset=setTabShown, notify=tabShownChanged)
  43. @pyqtProperty(QObject, constant=True)
  44. def MaterialPackageList(self):
  45. if self._material_package_list is None:
  46. self._material_package_list = RemotePackageList()
  47. self._material_package_list.packageTypeFilter = "material"
  48. return self._material_package_list
  49. @pyqtProperty(QObject, constant=True)
  50. def PluginPackageList(self):
  51. if self._plugin_package_list is None:
  52. self._plugin_package_list = RemotePackageList()
  53. self._plugin_package_list.packageTypeFilter = "plugin"
  54. return self._plugin_package_list
  55. @pyqtProperty(QObject, constant=True)
  56. def LocalPackageList(self):
  57. return self._local_package_list
  58. @pyqtSlot()
  59. def show(self) -> None:
  60. """
  61. Opens the window of the Marketplace.
  62. If the window hadn't been loaded yet into Qt, it will be created lazily.
  63. """
  64. if self._window is None:
  65. plugin_registry = PluginRegistry.getInstance()
  66. plugin_registry.pluginsEnabledOrDisabledChanged.connect(self.checkIfRestartNeeded)
  67. plugin_path = plugin_registry.getPluginPath(self.getPluginId())
  68. if plugin_path is None:
  69. plugin_path = os.path.dirname(__file__)
  70. path = os.path.join(plugin_path, "resources", "qml", "Marketplace.qml")
  71. self._window = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  72. if self._window is None: # Still None? Failed to load the QML then.
  73. return
  74. if not self._window.isVisible():
  75. self.setTabShown(0)
  76. self._window.show()
  77. self._window.requestActivate() # Bring window into focus, if it was already open in the background.
  78. @pyqtSlot()
  79. def setVisibleTabToMaterials(self) -> None:
  80. """
  81. Set the tab shown to the remote materials one.
  82. Not implemented in a more generic way because it needs the ability to be called with 'callExtensionMethod'.
  83. """
  84. self.setTabShown(1)
  85. def checkIfRestartNeeded(self) -> None:
  86. if self._window is None:
  87. return
  88. if self._package_manager.hasPackagesToRemoveOrInstall or \
  89. PluginRegistry.getInstance().getCurrentSessionActivationChangedPlugins():
  90. self._restart_needed = True
  91. else:
  92. self._restart_needed = False
  93. self.showRestartNotificationChanged.emit()
  94. showRestartNotificationChanged = pyqtSignal()
  95. @pyqtProperty(bool, notify = showRestartNotificationChanged)
  96. def showRestartNotification(self) -> bool:
  97. return self._restart_needed
  98. def showInstallMissingPackageDialog(self, packages_metadata: List[Dict[str, str]], ignore_warning_callback: Callable[[], None]) -> None:
  99. """
  100. Show a dialog that prompts the user to install certain packages.
  101. The dialog is worded for packages that are missing and required for a certain operation.
  102. :param packages_metadata: The metadata of the packages that are missing.
  103. :param ignore_warning_callback: A callback that gets executed when the user ignores the pop-up, to show them a
  104. warning.
  105. """
  106. self.missingPackageDialog = InstallMissingPackageDialog(packages_metadata, ignore_warning_callback)
  107. self.missingPackageDialog.show()