FirmwareUpdateCheckerJob.py 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Preferences import Preferences
  4. from UM.Application import Application
  5. from UM.Message import Message
  6. from UM.Logger import Logger
  7. from UM.Job import Job
  8. import urllib.request
  9. import codecs
  10. from PyQt5.QtCore import QUrl
  11. from PyQt5.QtGui import QDesktopServices
  12. from UM.i18n import i18nCatalog
  13. i18n_catalog = i18nCatalog("cura")
  14. ## This job checks if there is an update available on the provided URL.
  15. class FirmwareUpdateCheckerJob(Job):
  16. def __init__(self, container = None, silent = False, url = None):
  17. super().__init__()
  18. self._container = container
  19. self.silent = silent
  20. self._url = url
  21. self._download_url = None # If an update was found, the download_url will be set to the location of the new version.
  22. ## Callback for the message that is spawned when there is a new version.
  23. def actionTriggered(self, message, action):
  24. if action == "download":
  25. if self._download_url is not None:
  26. QDesktopServices.openUrl(QUrl(self._download_url))
  27. def run(self):
  28. self._download_url = None # Reset download ur.
  29. if not self._url:
  30. Logger.log("e", "Can not check for a new release. URL not set!")
  31. return
  32. try:
  33. application_name = Application.getInstance().getApplicationName()
  34. headers = {"User-Agent": "%s - %s" % (application_name, Application.getInstance().getVersion())}
  35. request = urllib.request.Request(self._url, headers = headers)
  36. current_version_file = urllib.request.urlopen(request)
  37. reader = codecs.getreader("utf-8")
  38. # get machine name from the definition container
  39. machine_name = self._container.definition.getName()
  40. machine_name_parts = machine_name.lower().split(" ")
  41. # If it is not None, then we compare between the checked_version and the current_version
  42. # Now we just do that if the active printer is Ultimaker 3 or Ultimaker 3 Extended or any
  43. # other Ultimaker 3 that will come in the future
  44. if len(machine_name_parts) >= 2 and machine_name_parts[:2] == ["ultimaker", "3"]:
  45. # Nothing to parse, just get the string
  46. # TODO: In the future may be done by parsing a JSON file with diferent version for each printer model
  47. current_version = reader(current_version_file).readline().rstrip()
  48. # If it is the first time the version is checked, the checked_version is ''
  49. checked_version = Preferences.getInstance().getValue("info/latest_checked_firmware")
  50. # If the checked_version is '', it's because is the first time we check firmware and in this case
  51. # we will not show the notification, but we will store it for the next time
  52. Preferences.getInstance().setValue("info/latest_checked_firmware", current_version)
  53. Logger.log("i", "Reading firmware version of %s: checked = %s - latest = %s", machine_name, checked_version, current_version)
  54. # The first time we want to store the current version, the notification will not be shown,
  55. # because the new version of Cura will be release before the firmware and we don't want to
  56. # notify the user when no new firmware version is available.
  57. if (checked_version != "") and (checked_version != current_version):
  58. Logger.log("i", "SHOWING FIRMWARE UPDATE MESSAGE")
  59. message = Message(i18n_catalog.i18nc("@info Don't translate {machine_name}, since it gets replaced by a printer name!", "To ensure that your {machine_name} is equipped with the latest features it is recommended to update the firmware regularly. This can be done on the {machine_name} (when connected to the network) or via USB.").format(machine_name = machine_name),
  60. title = i18n_catalog.i18nc("@info:title The %s gets replaced with the printer name.", "New %s firmware available") % machine_name)
  61. message.addAction("download", i18n_catalog.i18nc("@action:button", "Download"), "[no_icon]", "[no_description]")
  62. # If we do this in a cool way, the download url should be available in the JSON file
  63. self._download_url = "https://ultimaker.com/en/resources/20500-upgrade-firmware"
  64. message.actionTriggered.connect(self.actionTriggered)
  65. message.show()
  66. except Exception as e:
  67. Logger.log("w", "Failed to check for new version: %s", e)
  68. if not self.silent:
  69. Message(i18n_catalog.i18nc("@info", "Could not access update information.")).show()
  70. return