Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FirmwareUpdateChecker.py « FirmwareUpdateChecker « plugins - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7b1aef6d1bec01265932c61b80aa976d1e8e63b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.

from PyQt6.QtCore import QUrl
from PyQt6.QtGui import QDesktopServices

from typing import Set

from UM.Extension import Extension
from UM.Application import Application
from UM.Logger import Logger
from UM.i18n import i18nCatalog
from UM.Settings.ContainerRegistry import ContainerRegistry

from .FirmwareUpdateCheckerJob import FirmwareUpdateCheckerJob
from .FirmwareUpdateCheckerMessage import FirmwareUpdateCheckerMessage

i18n_catalog = i18nCatalog("cura")


class FirmwareUpdateChecker(Extension):
    """This Extension checks for new versions of the firmware based on the latest checked version number.

    The plugin is currently only usable for applications maintained by Ultimaker. But it should be relatively easy
    to change it to work for other applications.
    """

    def __init__(self) -> None:
        super().__init__()

        # Listen to a Signal that indicates a change in the list of printers, just if the user has enabled the
        # "check for updates" option
        Application.getInstance().getPreferences().addPreference("info/automatic_update_check", True)
        if Application.getInstance().getPreferences().getValue("info/automatic_update_check"):
            ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)

        self._check_job = None
        self._checked_printer_names = set()  # type: Set[str]

    def _onActionTriggered(self, message, action):
        """Callback for the message that is spawned when there is a new version."""

        if action == FirmwareUpdateCheckerMessage.STR_ACTION_DOWNLOAD:
            machine_id = message.getMachineId()
            download_url = message.getDownloadUrl()
            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(machine_id))

    def _onContainerAdded(self, container):
        # Only take care when a new GlobalStack was added
        from cura.Settings.GlobalStack import GlobalStack  # otherwise circular imports
        if isinstance(container, GlobalStack):
            self.checkFirmwareVersion(container, True)

    def _onJobFinished(self, *args, **kwargs):
        self._check_job = None

    def checkFirmwareVersion(self, container = None, silent = False):
        """Connect with software.ultimaker.com, load latest.version and check version info.

        If the version info is different from the current version, spawn a message to
        allow the user to download it.

        :param silent: type(boolean) Suppresses messages other than "new version found" messages.
            This is used when checking for a new firmware version at startup.
        """
        container_name = container.definition.getName()
        if container_name in self._checked_printer_names:
            return
        self._checked_printer_names.add(container_name)

        metadata = container.definition.getMetaData().get("firmware_update_info")
        if metadata is None:
            Logger.log("i", "No machine with name {0} in list of firmware to check.".format(container_name))
            return

        self._check_job = FirmwareUpdateCheckerJob(silent = silent,
                                                   machine_name = container_name, metadata = metadata,
                                                   callback = self._onActionTriggered)
        self._check_job.start()
        self._check_job.finished.connect(self._onJobFinished)