ConnectionStatus.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from typing import Optional
  2. from PyQt6.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, parent: Optional["QObject"] = None) -> None:
  18. super().__init__(parent)
  19. manager = HttpRequestManager.getInstance()
  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()