FirmwareUpdateChecker.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (c) 2017 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 UM.Extension import Extension
  6. from UM.Preferences import Preferences
  7. from UM.Logger import Logger
  8. from UM.i18n import i18nCatalog
  9. from UM.Settings.ContainerRegistry import ContainerRegistry
  10. from cura.Settings.GlobalStack import GlobalStack
  11. from .FirmwareUpdateCheckerJob import FirmwareUpdateCheckerJob
  12. i18n_catalog = i18nCatalog("cura")
  13. ## This Extension checks for new versions of the firmware based on the latest checked version number.
  14. # The plugin is currently only usable for applications maintained by Ultimaker. But it should be relatively easy
  15. # to change it to work for other applications.
  16. class FirmwareUpdateChecker(Extension):
  17. JEDI_VERSION_URL = "http://software.ultimaker.com/jedi/releases/latest.version?utm_source=cura&utm_medium=software&utm_campaign=resources"
  18. def __init__(self):
  19. super().__init__()
  20. # Initialize the Preference called `latest_checked_firmware` that stores the last version
  21. # checked for the UM3. In the future if we need to check other printers' firmware
  22. Preferences.getInstance().addPreference("info/latest_checked_firmware", "")
  23. # Listen to a Signal that indicates a change in the list of printers, just if the user has enabled the
  24. # 'check for updates' option
  25. Preferences.getInstance().addPreference("info/automatic_update_check", True)
  26. if Preferences.getInstance().getValue("info/automatic_update_check"):
  27. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)
  28. self._download_url = None
  29. self._check_job = None
  30. ## Callback for the message that is spawned when there is a new version.
  31. def _onActionTriggered(self, message, action):
  32. if action == "download":
  33. if self._download_url is not None:
  34. QDesktopServices.openUrl(QUrl(self._download_url))
  35. def _onSetDownloadUrl(self, download_url):
  36. self._download_url = download_url
  37. def _onContainerAdded(self, container):
  38. # Only take care when a new GlobalStack was added
  39. if isinstance(container, GlobalStack):
  40. self.checkFirmwareVersion(container, True)
  41. def _onJobFinished(self, *args, **kwargs):
  42. self._check_job = None
  43. ## Connect with software.ultimaker.com, load latest.version and check version info.
  44. # If the version info is different from the current version, spawn a message to
  45. # allow the user to download it.
  46. #
  47. # \param silent type(boolean) Suppresses messages other than "new version found" messages.
  48. # This is used when checking for a new firmware version at startup.
  49. def checkFirmwareVersion(self, container = None, silent = False):
  50. # Do not run multiple check jobs in parallel
  51. if self._check_job is not None:
  52. Logger.log("i", "A firmware update check is already running, do nothing.")
  53. return
  54. self._check_job = FirmwareUpdateCheckerJob(container = container, silent = silent, url = self.JEDI_VERSION_URL,
  55. callback = self._onActionTriggered,
  56. set_download_url_callback = self._onSetDownloadUrl)
  57. self._check_job.start()
  58. self._check_job.finished.connect(self._onJobFinished)