mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-21 02:01:03 +07:00
277b295fd8
* Feat: Add MtProxy (Telegram) * add path files * refactor: move logic from ui to core --------- Co-authored-by: vkamn <vk@amnezia.org>
47 lines
1.5 KiB
C++
47 lines
1.5 KiB
C++
#include "networkReachabilityController.h"
|
|
|
|
#include <QNetworkInformation>
|
|
|
|
namespace {
|
|
|
|
bool reachabilityAllowsRemoteOperations(QNetworkInformation::Reachability r) {
|
|
using R = QNetworkInformation::Reachability;
|
|
// Unknown: no backend or not yet determined — do not block UI.
|
|
return r == R::Online || r == R::Unknown;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NetworkReachabilityController::NetworkReachabilityController(QObject *parent) : QObject(parent) {
|
|
attachToNetworkInformation();
|
|
}
|
|
|
|
bool NetworkReachabilityController::hasInternetAccess() const {
|
|
return m_hasInternetAccess;
|
|
}
|
|
|
|
void NetworkReachabilityController::attachToNetworkInformation() {
|
|
if (!QNetworkInformation::loadDefaultBackend()) {
|
|
return;
|
|
}
|
|
QNetworkInformation *ni = QNetworkInformation::instance();
|
|
if (!ni) {
|
|
return;
|
|
}
|
|
const bool initial = reachabilityAllowsRemoteOperations(ni->reachability());
|
|
const bool previous = m_hasInternetAccess;
|
|
m_hasInternetAccess = initial;
|
|
if (previous != m_hasInternetAccess) {
|
|
emit hasInternetAccessChanged();
|
|
}
|
|
connect(ni, &QNetworkInformation::reachabilityChanged, this,
|
|
[this](QNetworkInformation::Reachability r) {
|
|
const bool ok = reachabilityAllowsRemoteOperations(r);
|
|
if (ok == m_hasInternetAccess) {
|
|
return;
|
|
}
|
|
m_hasInternetAccess = ok;
|
|
emit hasInternetAccessChanged();
|
|
});
|
|
}
|