|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a statusbar icon tracking the network status. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot, pyqtSignal |
|
11 from PyQt6.QtNetwork import QNetworkInformation |
|
12 from PyQt6.QtWidgets import QLabel |
|
13 |
|
14 import UI.PixmapCache |
|
15 import Preferences |
|
16 |
|
17 |
|
18 class EricNetworkIcon(QLabel): |
|
19 """ |
|
20 Class implementing a statusbar icon tracking the network status. |
|
21 |
|
22 @signal onlineStateChanged(online) emitted to indicate a change of the |
|
23 network state |
|
24 @signal reachabilityStateChanged(reachability) emitted to indicate a |
|
25 change of the network reachability |
|
26 """ |
|
27 onlineStateChanged = pyqtSignal(bool) |
|
28 reachabilityStateChanged = pyqtSignal(QNetworkInformation.Reachability) |
|
29 |
|
30 def __init__(self, parent=None): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param parent reference to the parent widget |
|
35 @type QWidget |
|
36 """ |
|
37 super().__init__(parent) |
|
38 |
|
39 if ( |
|
40 Preferences.getUI("DynamicOnlineCheck") and |
|
41 QNetworkInformation.load(QNetworkInformation.Feature.Reachability) |
|
42 ): |
|
43 self.__online = ( |
|
44 QNetworkInformation.instance().reachability() == |
|
45 QNetworkInformation.Reachability.Online |
|
46 ) |
|
47 self.__reachabilityChanged( |
|
48 QNetworkInformation.instance().reachability()) |
|
49 |
|
50 # TODO: QNetworkInformation: re-enable once problem is clear |
|
51 ## QNetworkInformation.instance().reachabilityChanged.connect( |
|
52 ## self.__reachabilityChanged) |
|
53 else: |
|
54 # assume to be 'always online' if no backend could be loaded or |
|
55 # dynamic online check is switched of |
|
56 self.__online = True |
|
57 self.__reachabilityChanged(QNetworkInformation.Reachability.Online) |
|
58 |
|
59 @pyqtSlot(QNetworkInformation.Reachability) |
|
60 def __reachabilityChanged(self, reachability): |
|
61 """ |
|
62 Private slot handling reachability state changes. |
|
63 |
|
64 @param reachability new reachability state |
|
65 @type QNetworkInformation.Reachability |
|
66 """ |
|
67 online = reachability == QNetworkInformation.Reachability.Online |
|
68 tooltip = self.tr("<p>Shows the Internet reachability status<br/><br/>" |
|
69 "<b>Internet:</b> {0}</p>") |
|
70 |
|
71 if online: |
|
72 self.setPixmap(UI.PixmapCache.getPixmap("network-online")) |
|
73 tooltip = tooltip.format(self.tr("Reachable")) |
|
74 else: |
|
75 self.setPixmap(UI.PixmapCache.getPixmap("network-offline")) |
|
76 tooltip = tooltip.format(self.tr("Not Reachable")) |
|
77 |
|
78 self.setToolTip(tooltip) |
|
79 |
|
80 if online != self.__online: |
|
81 self.__online = online |
|
82 self.onlineStateChanged.emit(online) |
|
83 |
|
84 self.reachabilityStateChanged.emit(reachability) |
|
85 |
|
86 def isOnline(self): |
|
87 """ |
|
88 Public method to get the online state. |
|
89 |
|
90 @return online state |
|
91 @rtype bool |
|
92 """ |
|
93 return self.__online |