FirmwareUpdateChecker.py 3.7 KB

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