ConnectionStatus.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from typing import Optional
  2. from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty
  3. from UM.TaskManagement.HttpRequestManager import HttpRequestManager
  4. class ConnectionStatus(QObject):
  5. """Provides an estimation of whether internet is reachable
  6. Estimation is updated with every request through HttpRequestManager.
  7. Acts as a proxy to HttpRequestManager.internetReachableChanged without
  8. exposing the HttpRequestManager in its entirety.
  9. """
  10. __instance = None # type: Optional[ConnectionStatus]
  11. internetReachableChanged = pyqtSignal()
  12. @classmethod
  13. def getInstance(cls, *args, **kwargs) -> "ConnectionStatus":
  14. if cls.__instance is None:
  15. cls.__instance = cls(*args, **kwargs)
  16. return cls.__instance
  17. def __init__(self) -> None:
  18. manager = HttpRequestManager.getInstance()
  19. super(ConnectionStatus, self).__init__(parent = manager)
  20. self._is_internet_reachable = manager.isInternetReachable # type: bool
  21. manager.internetReachableChanged.connect(self._onInternetReachableChanged)
  22. @pyqtProperty(bool, notify = internetReachableChanged)
  23. def isInternetReachable(self) -> bool:
  24. return self._is_internet_reachable
  25. def _onInternetReachableChanged(self, reachable: bool):
  26. if reachable != self._is_internet_reachable:
  27. self._is_internet_reachable = reachable
  28. self.internetReachableChanged.emit()