Also check for package updates automatically

Moves the 30-second sync timer from CloudOutputDeviceManager to
Account and subscribes CloudPackageChecker to the timer.

CURA-7290
This commit is contained in:
Nino van Hooff 2020-05-04 16:56:09 +02:00
parent 637a241d99
commit 88ff68e40c
3 changed files with 37 additions and 29 deletions

View file

@ -3,9 +3,8 @@
from datetime import datetime
from typing import Optional, Dict, TYPE_CHECKING
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty, QTimer
from UM.Logger import Logger
from UM.Message import Message
from UM.i18n import i18nCatalog
from cura.OAuth2.AuthorizationService import AuthorizationService
@ -28,11 +27,14 @@ i18n_catalog = i18nCatalog("cura")
# api.account.userProfile # Who is logged in``
#
class Account(QObject):
# The interval with which the remote clusters are checked
SYNC_INTERVAL = 30.0 # seconds
# Signal emitted when user logged in or out.
loginStateChanged = pyqtSignal(bool)
accessTokenChanged = pyqtSignal()
cloudPrintersDetectedChanged = pyqtSignal(bool)
manualSyncRequested = pyqtSignal()
syncRequested = pyqtSignal()
lastSyncDateTimeChanged = pyqtSignal()
syncStateChanged = pyqtSignal(str)
@ -66,6 +68,13 @@ class Account(QObject):
self._authorization_service = AuthorizationService(self._oauth_settings)
# Create a timer for automatic account sync
self._update_timer = QTimer()
self._update_timer.setInterval(int(self.SYNC_INTERVAL * 1000))
# The timer is restarted explicitly after an update was processed. This prevents 2 concurrent updates
self._update_timer.setSingleShot(True)
self._update_timer.timeout.connect(self.syncRequested)
self._sync_clients = {}
"""contains entries "client_name" : "state["success"|"error|"syncing"]"""
@ -77,9 +86,9 @@ class Account(QObject):
self._authorization_service.loadAuthDataFromPreferences()
def setSyncState(self, service_name: str, state: str) -> None:
""" Can be used to register and update account sync states
""" Can be used to register sync services and update account sync states
Example: `setSyncState("packages", "syncing")`
Example: `setSyncState("PluginSyncService", "syncing")`
:param service_name: A unique name for your service, such as `plugins` or `backups`
:param state: One of Account.SYNC_STATES
"""
@ -105,6 +114,11 @@ class Account(QObject):
self._last_sync_str = datetime.now().strftime("%d/%m/%Y %H:%M")
self.lastSyncDateTimeChanged.emit()
if self._sync_state != "syncing":
# schedule new auto update after syncing completed (for whatever reason)
if not self._update_timer.isActive():
self._update_timer.start()
def _onAccessTokenChanged(self):
self.accessTokenChanged.emit()
@ -181,7 +195,7 @@ class Account(QObject):
def sync(self) -> None:
"""Checks for new cloud printers"""
self.manualSyncRequested.emit()
self.syncRequested.emit()
@pyqtSlot()
def logout(self) -> None: