Added FirmwareUpdateCheckerMessage, so no variables have to be hidden in the action of a plain Message.

This commit is contained in:
Remco Burema 2018-10-13 20:05:20 +02:00
parent 8c71a8855c
commit 931143ceaa
4 changed files with 40 additions and 22 deletions

View file

@ -19,6 +19,7 @@ from cura.Settings.GlobalStack import GlobalStack
from .FirmwareUpdateCheckerJob import FirmwareUpdateCheckerJob
from .FirmwareUpdateCheckerLookup import FirmwareUpdateCheckerLookup, getSettingsKeyForMachine
from .FirmwareUpdateCheckerMessage import FirmwareUpdateCheckerMessage
i18n_catalog = i18nCatalog("cura")
@ -40,20 +41,22 @@ class FirmwareUpdateChecker(Extension):
# Partly initialize after creation, since we need our own path from the plugin-manager.
self._download_url = None
self._check_job = None
self._checked_printer_names = [] # type: Set[str]
self._checked_printer_names = set() # type: Set[str]
self._lookups = None
QtApplication.pluginsLoaded.connect(self._onPluginsLoaded)
## Callback for the message that is spawned when there is a new version.
def _onActionTriggered(self, message, action):
download_url = self._lookups.getRedirectUserFor(int(action))
if action == FirmwareUpdateCheckerMessage.STR_ACTION_DOWNLOAD:
machine_id = message.getMachineId()
download_url = self._lookups.getRedirectUserFor(machine_id)
if download_url is not None:
if QDesktopServices.openUrl(QUrl(download_url)):
Logger.log("i", "Redirected browser to {0} to show newly available firmware.".format(download_url))
else:
Logger.log("e", "Can't reach URL: {0}".format(download_url))
else:
Logger.log("e", "Can't find URL for {0}".format(action))
Logger.log("e", "Can't find URL for {0}".format(machine_id))
def _onContainerAdded(self, container):
# Only take care when a new GlobalStack was added
@ -88,7 +91,7 @@ class FirmwareUpdateChecker(Extension):
container_name = container.definition.getName()
if container_name in self._checked_printer_names:
return
self._checked_printer_names.append(container_name)
self._checked_printer_names.add(container_name)
self._check_job = FirmwareUpdateCheckerJob(container = container, silent = silent,
lookups = self._lookups,

View file

@ -10,9 +10,9 @@ from UM.Version import Version
import urllib.request
from urllib.error import URLError
from typing import Dict
import codecs
from .FirmwareUpdateCheckerLookup import FirmwareUpdateCheckerLookup, getSettingsKeyForMachine
from .FirmwareUpdateCheckerMessage import FirmwareUpdateCheckerMessage
from UM.i18n import i18nCatalog
i18n_catalog = i18nCatalog("cura")
@ -99,22 +99,7 @@ class FirmwareUpdateCheckerJob(Job):
# notify the user when no new firmware version is available.
if (checked_version != "") and (checked_version != current_version):
Logger.log("i", "SHOWING FIRMWARE UPDATE MESSAGE")
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),
title = i18n_catalog.i18nc(
"@info:title The %s gets replaced with the printer name.",
"New %s firmware available") % machine_name)
message.addAction(machine_id,
i18n_catalog.i18nc("@action:button", "How to update"),
"[no_icon]",
"[no_description]",
button_style = Message.ActionButtonStyle.LINK,
button_align = Message.ActionButtonStyle.BUTTON_ALIGN_LEFT)
message = FirmwareUpdateCheckerMessage(machine_id, machine_name)
message.actionTriggered.connect(self._callback)
message.show()
else:

View file

@ -6,7 +6,6 @@ import json
from typing import Callable, Dict, List, Optional
from UM.Logger import Logger
from UM.Version import Version
from UM.i18n import i18nCatalog
i18n_catalog = i18nCatalog("cura")

View file

@ -0,0 +1,31 @@
from UM.i18n import i18nCatalog
from UM.Message import Message
i18n_catalog = i18nCatalog("cura")
# Make a separate class, since we need an extra field: The machine-id that this message is about.
class FirmwareUpdateCheckerMessage(Message):
STR_ACTION_DOWNLOAD = "download"
def __init__(self, machine_id: int, machine_name: str) -> None:
super().__init__(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),
title=i18n_catalog.i18nc(
"@info:title The %s gets replaced with the printer name.",
"New %s firmware available") % machine_name)
self._machine_id = machine_id
self.addAction(self.STR_ACTION_DOWNLOAD,
i18n_catalog.i18nc("@action:button", "How to update"),
"[no_icon]",
"[no_description]",
button_style=Message.ActionButtonStyle.LINK,
button_align=Message.ActionButtonStyle.BUTTON_ALIGN_LEFT)
def getMachineId(self) -> int:
return self._machine_id