1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtSystems module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL21$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34#include "qnetworkinfo.h"
35
36#if defined(QT_SIMULATOR)
37# include "simulator/qsysteminfo_simulator_p.h"
38#elif defined(Q_OS_LINUX)
39# include "linux/qnetworkinfo_linux_p.h"
40#elif defined(Q_OS_WIN)
41# include "windows/qnetworkinfo_win_p.h"
42#elif defined(Q_OS_MAC)
43# include "mac/qnetworkinfo_mac_p.h"
44#else
45QT_BEGIN_NAMESPACE
46class QNetworkInfoPrivate
47{
48public:
49 QNetworkInfoPrivate(QNetworkInfo *) {}
50
51 int networkInterfaceCount(QNetworkInfo::NetworkMode) { return -1; }
52 int networkSignalStrength(QNetworkInfo::NetworkMode, int) { return -1; }
53 QNetworkInfo::CellDataTechnology currentCellDataTechnology(int) { return QNetworkInfo::UnknownDataTechnology; }
54 QNetworkInfo::NetworkMode currentNetworkMode() { return QNetworkInfo::UnknownMode; }
55 QNetworkInfo::NetworkStatus networkStatus(QNetworkInfo::NetworkMode, int) { return QNetworkInfo::UnknownStatus; }
56#ifndef QT_NO_NETWORKINTERFACE
57 QNetworkInterface interfaceForMode(QNetworkInfo::NetworkMode, int) { return QNetworkInterface(); }
58#endif // QT_NO_NETWORKINTERFACE
59 QString cellId(int) { return QString(); }
60 QString currentMobileCountryCode(int) { return QString(); }
61 QString currentMobileNetworkCode(int) { return QString(); }
62 QString homeMobileCountryCode(int) { return QString(); }
63 QString homeMobileNetworkCode(int) { return QString(); }
64 QString imsi(int) { return QString(); }
65 QString locationAreaCode(int) { return QString(); }
66 QString macAddress(QNetworkInfo::NetworkMode, int) { return QString(); }
67 QString networkName(QNetworkInfo::NetworkMode, int) { return QString(); }
68};
69QT_END_NAMESPACE
70#endif
71
72#include <QtCore/qmetaobject.h>
73
74QT_BEGIN_NAMESPACE
75
76/*!
77 \class QNetworkInfo
78 \inmodule QtSystemInfo
79 \brief The QNetworkInfo class provides various information about the network status.
80
81 \ingroup systeminfo
82
83 To support the cases where one has multiple interfaces / modems for the same network mode, you
84 can specify which interface you refer to. For those cases, the 'interface' parameter is the index
85 of the interface, starting from 0.
86*/
87
88/*!
89 \enum QNetworkInfo::CellDataTechnology
90 This enum describes the type of cellular technology.
91
92 \value UnknownDataTechnology The cellular technology is unknown or an error occured.
93 \value GprsDataTechnology General Packet Radio Service (GPRS) data service.
94 \value EdgeDataTechnology Enhanced Data Rates for GSM Evolution (EDGE) data service.
95 \value UmtsDataTechnology Universal Mobile Telecommunications System (UMTS) data service.
96 \value HspaDataTechnology High Speed Packet Access (HSPA) data service.
97*/
98
99/*!
100 \enum QNetworkInfo::NetworkMode
101 This enumeration describes the type of the network.
102
103 \value UnknownMode The network is unknown or an error occured.
104 \value GsmMode Global System for Mobile (GSM) network.
105 \value CdmaMode Code Division Multiple Access (CDMA) network.
106 \value WcdmaMode Wideband Code Division Multiple Access (WCDMA) network.
107 \value WlanMode Wireless local area network (WLAN) network.
108 \value EthernetMode Local area network (LAN), or Ethernet network.
109 \value BluetoothMode Bluetooth network.
110 \value WimaxMode Worldwide Interoperability for Microwave Access (WiMAX) network.
111 \value LteMode 3GPP Long Term Evolution (LTE) network.
112 \value TdscdmaMode Time Division Synchronous Code Division Multiple Access (TD-SCDMA) network.
113*/
114
115/*!
116 \enum QNetworkInfo::NetworkStatus
117 This enumeration describes the status of the network.
118
119 \value UnknownStatus The status is unknown or an error occured.
120 \value NoNetworkAvailable There is no network available.
121 \value EmergencyOnly The network only allows emergency calls.
122 \value Searching The device is searching or connecting to the network.
123 \value Busy The network is too busy to be connected.
124 \value Denied The connection to the network has been denied.
125 \value HomeNetwork The device is connected to the home network.
126 \value Roaming The device is connected to some roaming network.
127*/
128
129/*!
130 \fn void QNetworkInfo::cellIdChanged(int interface, const QString &id)
131
132 This signal is emitted whenever the cell ID for \a interface has changed to \a id.
133*/
134
135/*!
136 \fn void QNetworkInfo::currentCellDataTechnologyChanged(int interface, QNetworkInfo::CellDataTechnology tech)
137
138 This signal is emitted whenever the current cell data technology for \a interface has changed to \a tech.
139*/
140
141/*!
142 \fn void QNetworkInfo::currentMobileCountryCodeChanged(int interface, const QString &mcc)
143
144 This signal is emitted whenever the current Mobile Country Code (MCC) for \a interface has changed
145 to \a mcc.
146*/
147
148/*!
149 \fn void QNetworkInfo::currentMobileNetworkCodeChanged(int interface, const QString &mnc)
150
151 This signal is emitted whenever the current Mobile Network Code (MNC) for \a interface has changed
152 to \a mnc.
153*/
154
155/*!
156 \fn void QNetworkInfo::currentNetworkModeChanged(QNetworkInfo::NetworkMode mode)
157
158 This signal is emitted whenever the current network has changed to \a mode.
159*/
160
161/*!
162 \fn void QNetworkInfo::locationAreaCodeChanged(int interface, const QString &lac)
163
164 This signal is emitted whenever the location area code for \a interface has changed to \a lac.
165*/
166
167/*!
168 \fn void QNetworkInfo::networkInterfaceCountChanged(QNetworkInfo::NetworkMode mode, int count)
169
170 This signal is emitted whenever the number of interfaces for the \a mode has changed to \a count.
171*/
172
173/*!
174 \fn void QNetworkInfo::networkNameChanged(QNetworkInfo::NetworkMode mode, int interface, const QString &name)
175
176 This signal is emitted whenever the name for the \a interface of \a mode has changed to \a name.
177*/
178
179/*!
180 \fn void QNetworkInfo::networkSignalStrengthChanged(QNetworkInfo::NetworkMode mode, int interface, int strength)
181
182 This signal is emitted whenever the signal strength for the \a interface of \a mode has changed
183 to \a strength.
184*/
185
186/*!
187 \fn void QNetworkInfo::networkStatusChanged(QNetworkInfo::NetworkMode mode, int interface, QNetworkInfo::NetworkStatus status)
188
189 This signal is emitted whenever the status for the \a interface of \a mode has changed to \a status.
190*/
191
192/*!
193 Constructs a QNetworkInfo object with the given \a parent.
194*/
195QNetworkInfo::QNetworkInfo(QObject *parent)
196 : QObject(parent)
197#if !defined(QT_SIMULATOR)
198 , d_ptr(new QNetworkInfoPrivate(this))
199#else
200 , d_ptr(new QNetworkInfoSimulator(this))
201#endif // QT_SIMULATOR
202{
203}
204
205/*!
206 Destroys the object
207*/
208QNetworkInfo::~QNetworkInfo()
209{
210 delete d_ptr;
211}
212
213/*!
214 Returns the number of interfaces for the \a mode. If the information is not available, or error
215 occurs, -1 is returned.
216*/
217int QNetworkInfo::networkInterfaceCount(QNetworkInfo::NetworkMode mode) const
218{
219 return d_ptr->networkInterfaceCount(mode);
220}
221
222/*!
223 Returns the signal strength for \a interfaceDevice of \a mode, in 0 - 100 scale. If the information
224 is not available, or error occurs, -1 is returned.
225*/
226int QNetworkInfo::networkSignalStrength(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
227{
228 return d_ptr->networkSignalStrength(mode, interface: interfaceDevice);
229}
230
231/*!
232 Returns the current cell data technology used for \a interfaceDevice.
233*/
234QNetworkInfo::CellDataTechnology QNetworkInfo::currentCellDataTechnology(int interfaceDevice) const
235{
236 return d_ptr->currentCellDataTechnology(interface: interfaceDevice);
237}
238
239/*!
240 Returns the current active network mode. If there are more than one modes activated, the preferred
241 one is returned.
242*/
243QNetworkInfo::NetworkMode QNetworkInfo::currentNetworkMode() const
244{
245 return d_ptr->currentNetworkMode();
246}
247
248/*!
249 Returns the current status for \a interfaceDevice of \a mode.
250*/
251QNetworkInfo::NetworkStatus QNetworkInfo::networkStatus(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
252{
253 return d_ptr->networkStatus(mode, interface: interfaceDevice);
254}
255
256#ifndef QT_NO_NETWORKINTERFACE
257/*!
258 Returns the first found interface for \a interfaceDevice of \a mode. If none is found, or it can't be
259 represented by QNetworkInterface (e.g. Bluetooth), and empty object is returned.
260*/
261QNetworkInterface QNetworkInfo::interfaceForMode(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
262{
263 return d_ptr->interfaceForMode(mode, interface: interfaceDevice);
264}
265#endif // QT_NO_NETWORKINTERFACE
266
267/*!
268 Returns the cell ID of the connected tower or based station for \a interfaceDevice. If this information
269 is not available or error occurs, an empty string is returned.
270*/
271QString QNetworkInfo::cellId(int interfaceDevice) const
272{
273 return d_ptr->cellId(interface: interfaceDevice);
274}
275
276/*!
277 Returns the current Mobile Country Code (MCC) for \a interfaceDevice. An empty string is returned if the
278 information is not available or an error occurs.
279*/
280QString QNetworkInfo::currentMobileCountryCode(int interfaceDevice) const
281{
282 return d_ptr->currentMobileCountryCode(interface: interfaceDevice);
283}
284
285/*!
286 Returns the current Mobile Network Code (MNC) for \a interfaceDevice. An empty string is returned if the
287 information is not available or an error occurs.
288*/
289QString QNetworkInfo::currentMobileNetworkCode(int interfaceDevice) const
290{
291 return d_ptr->currentMobileNetworkCode(interface: interfaceDevice);
292}
293
294/*!
295 Returns the home Mobile Country Code (MCC) for \a interfaceDevice. An empty string is returned if the
296 information is not available or an error occurs.
297*/
298QString QNetworkInfo::homeMobileCountryCode(int interfaceDevice) const
299{
300 return d_ptr->homeMobileCountryCode(interface: interfaceDevice);
301}
302
303/*!
304 Returns the home Mobile Network Code (MNC) for \a interfaceDevice. An empty string is returned if the
305 information is not available or an error occurs.
306*/
307QString QNetworkInfo::homeMobileNetworkCode(int interfaceDevice) const
308{
309 return d_ptr->homeMobileNetworkCode(interface: interfaceDevice);
310}
311
312/*!
313 Returns the International Mobile Subscriber Identity (IMSI) for \a interfaceDevice. If this information is
314 not available, or error occurs, an empty string is returned.
315*/
316QString QNetworkInfo::imsi(int interfaceDevice) const
317{
318 return d_ptr->imsi(interface: interfaceDevice);
319}
320
321/*!
322 Returns the location area code of the current cellular radio network for \a interfaceDevice. If this information
323 is not available or error occurs, an empty string is returned.
324*/
325QString QNetworkInfo::locationAreaCode(int interfaceDevice) const
326{
327 return d_ptr->locationAreaCode(interface: interfaceDevice);
328}
329
330/*!
331 Returns the MAC address for \a interfaceDevice of \a mode. If the MAC address is not available or error
332 occurs, an empty string is returned.
333*/
334QString QNetworkInfo::macAddress(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
335{
336 return d_ptr->macAddress(mode, interface: interfaceDevice);
337}
338
339/*!
340 Returns the name of the operator for \a interfaceDevice of \a mode. If the information is not available,
341 or an error occurs, an empty string is returned.
342
343 In case of WLAN, the SSID is returned; for Ethernet, the domain name is returned if available.
344*/
345QString QNetworkInfo::networkName(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
346{
347 return d_ptr->networkName(mode, interface: interfaceDevice);
348}
349
350extern QMetaMethod proxyToSourceSignal(const QMetaMethod &, QObject *);
351
352/*!
353 \internal
354*/
355void QNetworkInfo::connectNotify(const QMetaMethod &signal)
356{
357#if defined(Q_OS_LINUX) || defined(Q_OS_WIN) || defined(QT_SIMULATOR) || defined(Q_OS_MAC)
358 QMetaMethod sourceSignal = proxyToSourceSignal(signal, d_ptr);
359 connect(sender: d_ptr, signal: sourceSignal, receiver: this, method: signal, type: Qt::UniqueConnection);
360#else
361 Q_UNUSED(signal)
362#endif
363}
364
365/*!
366 \internal
367*/
368void QNetworkInfo::disconnectNotify(const QMetaMethod &signal)
369{
370#if defined(Q_OS_LINUX) || defined(Q_OS_WIN) || defined(QT_SIMULATOR) || defined(Q_OS_MAC)
371 // We can only disconnect with the private implementation, when there is no receivers for the signal.
372 if (isSignalConnected(signal))
373 return;
374
375 QMetaMethod sourceSignal = proxyToSourceSignal(signal, d_ptr);
376 disconnect(sender: d_ptr, signal: sourceSignal, receiver: this, member: signal);
377#else
378 Q_UNUSED(signal)
379#endif
380}
381
382QT_END_NAMESPACE
383

source code of qtsystems/src/systeminfo/qnetworkinfo.cpp