-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddonmanager_connection_checker.py
125 lines (104 loc) · 5.68 KB
/
addonmanager_connection_checker.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2022 FreeCAD Project Association *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, but *
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
"""System for checking the network connection status asynchronously."""
import FreeCAD
from PySide import QtCore, QtWidgets
import NetworkManager
from addonmanager_workers_utility import ConnectionChecker
translate = FreeCAD.Qt.translate
class ConnectionCheckerGUI(QtCore.QObject):
"""Determine whether there is an active network connection, showing a progress message if it
starts to take too long, and an error message if the network cannot be accessed."""
connection_available = QtCore.Signal()
check_complete = QtCore.Signal()
def __init__(self):
super().__init__()
# Check the connection in a new thread, so FreeCAD stays responsive
self.connection_checker = ConnectionChecker()
self.signals_connected = False
self.connection_message_timer = None
self.connection_check_message = None
def start(self):
"""Start the connection check"""
self.connection_checker.start()
self.connection_checker.success.connect(self._check_succeeded)
self.connection_checker.failure.connect(self._network_connection_failed)
self.signals_connected = True
# If it takes longer than a half second to check the connection, show a message:
self.connection_message_timer = QtCore.QTimer.singleShot(
500, self._show_connection_check_message
)
def _show_connection_check_message(self):
"""Display a message informing the user that the check is in process"""
if not self.connection_checker.isFinished():
self.connection_check_message = QtWidgets.QMessageBox(
QtWidgets.QMessageBox.Information,
translate("AddonsInstaller", "Checking connection"),
translate("AddonsInstaller", "Checking for connection to GitHub..."),
QtWidgets.QMessageBox.Cancel,
)
self.connection_check_message.buttonClicked.connect(self.cancel_network_check)
self.connection_check_message.show()
def cancel_network_check(self, _):
"""Cancel the check"""
if not self.connection_checker.isFinished():
self._disconnect_signals()
self.connection_checker.requestInterruption()
self.connection_checker.wait(500)
self.connection_check_message.close()
self.check_complete.emit()
def _network_connection_failed(self, message: str) -> None:
"""Callback for failed connection check. Displays an error message, then emits the
check_complete signal (but not the connection available signal)."""
# This must run on the main GUI thread
if hasattr(self, "connection_check_message") and self.connection_check_message:
self.connection_check_message.close()
if NetworkManager.HAVE_QTNETWORK:
QtWidgets.QMessageBox.critical(
None, translate("AddonsInstaller", "Connection failed"), message
)
else:
# pylint: disable=line-too-long
QtWidgets.QMessageBox.critical(
None,
translate("AddonsInstaller", "Missing dependency"),
translate(
"AddonsInstaller",
"Could not import QtNetwork -- see Report View for details. Addon Manager "
"unavailable.",
),
)
self._disconnect_signals()
self.check_complete.emit()
def _check_succeeded(self):
"""Emit both the connection_available and the check_complete signals, in that order."""
if hasattr(self, "connection_check_message") and self.connection_check_message:
self.connection_check_message.close()
self.connection_available.emit()
self._disconnect_signals()
self.check_complete.emit()
def _disconnect_signals(self):
if self.signals_connected:
self.connection_checker.success.disconnect(self._check_succeeded)
self.connection_checker.failure.disconnect(self._network_connection_failed)
self.signals_connected = False