FirmwareUpdateCheckerJob.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 UM.i18n import i18nCatalog
  11. i18n_catalog = i18nCatalog("cura")
  12. ## This job checks if there is an update available on the provided URL.
  13. class FirmwareUpdateCheckerJob(Job):
  14. def __init__(self, container = None, silent = False, url = None, callback = None, set_download_url_callback = None):
  15. super().__init__()
  16. self._container = container
  17. self.silent = silent
  18. self._url = url
  19. self._callback = callback
  20. self._set_download_url_callback = set_download_url_callback
  21. def run(self):
  22. if not self._url:
  23. Logger.log("e", "Can not check for a new release. URL not set!")
  24. return
  25. try:
  26. application_name = Application.getInstance().getApplicationName()
  27. headers = {"User-Agent": "%s - %s" % (application_name, Application.getInstance().getVersion())}
  28. request = urllib.request.Request(self._url, headers = headers)
  29. current_version_file = urllib.request.urlopen(request)
  30. reader = codecs.getreader("utf-8")
  31. # get machine name from the definition container
  32. machine_name = self._container.definition.getName()
  33. machine_name_parts = machine_name.lower().split(" ")
  34. # If it is not None, then we compare between the checked_version and the current_version
  35. # Now we just do that if the active printer is Ultimaker 3 or Ultimaker 3 Extended or any
  36. # other Ultimaker 3 that will come in the future
  37. if len(machine_name_parts) >= 2 and machine_name_parts[:2] == ["ultimaker", "3"]:
  38. Logger.log("i", "You have a UM3 in printer list. Let's check the firmware!")
  39. # Nothing to parse, just get the string
  40. # TODO: In the future may be done by parsing a JSON file with diferent version for each printer model
  41. current_version = reader(current_version_file).readline().rstrip()
  42. # If it is the first time the version is checked, the checked_version is ''
  43. checked_version = Preferences.getInstance().getValue("info/latest_checked_firmware")
  44. # If the checked_version is '', it's because is the first time we check firmware and in this case
  45. # we will not show the notification, but we will store it for the next time
  46. Preferences.getInstance().setValue("info/latest_checked_firmware", current_version)
  47. Logger.log("i", "Reading firmware version of %s: checked = %s - latest = %s", machine_name, checked_version, current_version)
  48. # The first time we want to store the current version, the notification will not be shown,
  49. # because the new version of Cura will be release before the firmware and we don't want to
  50. # notify the user when no new firmware version is available.
  51. if (checked_version != "") and (checked_version != current_version):
  52. Logger.log("i", "SHOWING FIRMWARE UPDATE MESSAGE")
  53. message = Message(i18n_catalog.i18nc("@info Don't translate {machine_name}, since it gets replaced by a printer name!", "New features are available for your {machine_name}! It is recommended to update the firmware on your printer.").format(machine_name = machine_name),
  54. title = i18n_catalog.i18nc("@info:title The %s gets replaced with the printer name.", "New %s firmware available") % machine_name)
  55. message.addAction("download", i18n_catalog.i18nc("@action:button", "How to update"), "[no_icon]", "[no_description]")
  56. # If we do this in a cool way, the download url should be available in the JSON file
  57. if self._set_download_url_callback:
  58. self._set_download_url_callback("https://ultimaker.com/en/resources/23129-updating-the-firmware?utm_source=cura&utm_medium=software&utm_campaign=hw-update")
  59. message.actionTriggered.connect(self._callback)
  60. message.show()
  61. except Exception as e:
  62. Logger.log("w", "Failed to check for new version: %s", e)
  63. if not self.silent:
  64. Message(i18n_catalog.i18nc("@info", "Could not access update information.")).show()
  65. return