1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QNETWORKINFORMATION_P_H
5#define QNETWORKINFORMATION_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of the Network Information API. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19
20#include <QtNetwork/qnetworkinformation.h>
21
22#include <QtCore/qloggingcategory.h>
23
24QT_BEGIN_NAMESPACE
25
26class Q_NETWORK_EXPORT QNetworkInformationBackend : public QObject
27{
28 Q_OBJECT
29
30 using Reachability = QNetworkInformation::Reachability;
31 using TransportMedium = QNetworkInformation::TransportMedium;
32
33public:
34 static inline const char16_t PluginNames[4][22] = {
35 { u"networklistmanager" },
36 { u"scnetworkreachability" },
37 { u"android" },
38 { u"networkmanager" },
39 };
40 static constexpr int PluginNamesWindowsIndex = 0;
41 static constexpr int PluginNamesAppleIndex = 1;
42 static constexpr int PluginNamesAndroidIndex = 2;
43 static constexpr int PluginNamesLinuxIndex = 3;
44
45 QNetworkInformationBackend() = default;
46 ~QNetworkInformationBackend() override;
47
48 virtual QString name() const = 0;
49 virtual QNetworkInformation::Features featuresSupported() const = 0;
50
51 Reachability reachability() const { return m_reachability; }
52 bool behindCaptivePortal() const { return m_behindCaptivePortal; }
53 TransportMedium transportMedium() const { return m_transportMedium; }
54 bool isMetered() const { return m_metered; }
55
56Q_SIGNALS:
57 void reachabilityChanged(Reachability reachability);
58 void behindCaptivePortalChanged(bool behindPortal);
59 void transportMediumChanged(TransportMedium medium);
60 void isMeteredChanged(bool isMetered);
61
62protected:
63 void setReachability(QNetworkInformation::Reachability reachability)
64 {
65 if (m_reachability != reachability) {
66 m_reachability = reachability;
67 emit reachabilityChanged(reachability);
68 }
69 }
70
71 void setBehindCaptivePortal(bool behindPortal)
72 {
73 if (m_behindCaptivePortal != behindPortal) {
74 m_behindCaptivePortal = behindPortal;
75 emit behindCaptivePortalChanged(behindPortal);
76 }
77 }
78
79 void setTransportMedium(TransportMedium medium)
80 {
81 if (m_transportMedium != medium) {
82 m_transportMedium = medium;
83 emit transportMediumChanged(medium);
84 }
85 }
86
87 void setMetered(bool isMetered)
88 {
89 if (m_metered != isMetered) {
90 m_metered = isMetered;
91 emit isMeteredChanged(isMetered);
92 }
93 }
94
95private:
96 Reachability m_reachability = Reachability::Unknown;
97 TransportMedium m_transportMedium = TransportMedium::Unknown;
98 bool m_behindCaptivePortal = false;
99 bool m_metered = false;
100
101 Q_DISABLE_COPY_MOVE(QNetworkInformationBackend)
102 friend class QNetworkInformation;
103 friend class QNetworkInformationPrivate;
104};
105
106class Q_NETWORK_EXPORT QNetworkInformationBackendFactory : public QObject
107{
108 Q_OBJECT
109
110 using Features = QNetworkInformation::Features;
111
112public:
113 QNetworkInformationBackendFactory();
114 virtual ~QNetworkInformationBackendFactory();
115 virtual QString name() const = 0;
116 virtual QNetworkInformationBackend *create(Features requiredFeatures) const = 0;
117 virtual Features featuresSupported() const = 0;
118
119private:
120 Q_DISABLE_COPY_MOVE(QNetworkInformationBackendFactory)
121};
122#define QNetworkInformationBackendFactory_iid "org.qt-project.Qt.NetworkInformationBackendFactory"
123Q_DECLARE_INTERFACE(QNetworkInformationBackendFactory, QNetworkInformationBackendFactory_iid);
124
125QT_END_NAMESPACE
126
127#endif
128

source code of qtbase/src/network/kernel/qnetworkinformation_p.h