FirmwareUpdateChecker.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QUrl
  4. from PyQt5.QtGui import QDesktopServices
  5. from typing import Set
  6. from UM.Extension import Extension
  7. from UM.Application import Application
  8. from UM.Logger import Logger
  9. from UM.i18n import i18nCatalog
  10. from UM.Settings.ContainerRegistry import ContainerRegistry
  11. from .FirmwareUpdateCheckerJob import FirmwareUpdateCheckerJob
  12. from .FirmwareUpdateCheckerMessage import FirmwareUpdateCheckerMessage
  13. i18n_catalog = i18nCatalog("cura")
  14. ## This Extension checks for new versions of the firmware based on the latest checked version number.
  15. # The plugin is currently only usable for applications maintained by Ultimaker. But it should be relatively easy
  16. # to change it to work for other applications.
  17. class FirmwareUpdateChecker(Extension):
  18. def __init__(self) -> None:
  19. super().__init__()
  20. # Listen to a Signal that indicates a change in the list of printers, just if the user has enabled the
  21. # "check for updates" option
  22. Application.getInstance().getPreferences().addPreference("info/automatic_update_check", True)
  23. if Application.getInstance().getPreferences().getValue("info/automatic_update_check"):
  24. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)
  25. self._check_job = None
  26. self._checked_printer_names = set() # type: Set[str]
  27. ## Callback for the message that is spawned when there is a new version.
  28. def _onActionTriggered(self, message, action):
  29. if action == FirmwareUpdateCheckerMessage.STR_ACTION_DOWNLOAD:
  30. machine_id = message.getMachineId()
  31. download_url = message.getDownloadUrl()
  32. if download_url is not None:
  33. if QDesktopServices.openUrl(QUrl(download_url)):
  34. Logger.log("i", "Redirected browser to {0} to show newly available firmware.".format(download_url))
  35. else:
  36. Logger.log("e", "Can't reach URL: {0}".format(download_url))
  37. else:
  38. Logger.log("e", "Can't find URL for {0}".format(machine_id))
  39. def _onContainerAdded(self, container):
  40. # Only take care when a new GlobalStack was added
  41. from cura.Settings.GlobalStack import GlobalStack # otherwise circular imports
  42. if isinstance(container, GlobalStack):
  43. self.checkFirmwareVersion(container, True)
  44. def _onJobFinished(self, *args, **kwargs):
  45. self._check_job = None
  46. ## Connect with software.ultimaker.com, load latest.version and check version info.
  47. # If the version info is different from the current version, spawn a message to
  48. # allow the user to download it.
  49. #
  50. # \param silent type(boolean) Suppresses messages other than "new version found" messages.
  51. # This is used when checking for a new firmware version at startup.
  52. def checkFirmwareVersion(self, container = None, silent = False):
  53. container_name = container.definition.getName()
  54. if container_name in self._checked_printer_names:
  55. return
  56. self._checked_printer_names.add(container_name)
  57. metadata = container.definition.getMetaData().get("firmware_update_info")
  58. if metadata is None:
  59. Logger.log("i", "No machine with name {0} in list of firmware to check.".format(container_name))
  60. return
  61. self._check_job = FirmwareUpdateCheckerJob(silent = silent,
  62. machine_name = container_name, metadata = metadata,
  63. callback = self._onActionTriggered)
  64. self._check_job.start()
  65. self._check_job.finished.connect(self._onJobFinished)