summaryrefslogtreecommitdiffstats
path: root/src/systeminfo/qnetworkinfo.cpp
blob: ea994331cc34e293af6f4133b12de4ac9ec5fb4e (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtSystems module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qnetworkinfo.h"

#if defined(QT_SIMULATOR)
#  include "simulator/qsysteminfo_simulator_p.h"
#elif defined(Q_OS_LINUX)
#  include "linux/qnetworkinfo_linux_p.h"
#elif defined(Q_OS_WIN)
#  include "windows/qnetworkinfo_win_p.h"
#elif defined(Q_OS_MAC)
#  include "mac/qnetworkinfo_mac_p.h"
#else
QT_BEGIN_NAMESPACE
class QNetworkInfoPrivate
{
public:
    QNetworkInfoPrivate(QNetworkInfo *) {}

    int networkInterfaceCount(QNetworkInfo::NetworkMode) { return -1; }
    int networkSignalStrength(QNetworkInfo::NetworkMode, int) { return -1; }
    QNetworkInfo::CellDataTechnology currentCellDataTechnology(int) { return QNetworkInfo::UnknownDataTechnology; }
    QNetworkInfo::NetworkMode currentNetworkMode() { return QNetworkInfo::UnknownMode; }
    QNetworkInfo::NetworkStatus networkStatus(QNetworkInfo::NetworkMode, int) { return QNetworkInfo::UnknownStatus; }
#ifndef QT_NO_NETWORKINTERFACE
    QNetworkInterface interfaceForMode(QNetworkInfo::NetworkMode, int) { return QNetworkInterface(); }
#endif // QT_NO_NETWORKINTERFACE
    QString cellId(int) { return QString(); }
    QString currentMobileCountryCode(int) { return QString(); }
    QString currentMobileNetworkCode(int) { return QString(); }
    QString homeMobileCountryCode(int) { return QString(); }
    QString homeMobileNetworkCode(int) { return QString(); }
    QString imsi(int) { return QString(); }
    QString locationAreaCode(int) { return QString(); }
    QString macAddress(QNetworkInfo::NetworkMode, int) { return QString(); }
    QString networkName(QNetworkInfo::NetworkMode, int) { return QString(); }
};
QT_END_NAMESPACE
#endif

#include <QtCore/qmetaobject.h>

QT_BEGIN_NAMESPACE

/*!
    \class QNetworkInfo
    \inmodule QtSystemInfo
    \brief The QNetworkInfo class provides various information about the network status.

    \ingroup systeminfo

    To support the cases where one has multiple interfaces / modems for the same network mode, you
    can specify which interface you refer to. For those cases, the 'interface' parameter is the index
    of the interface, starting from 0.
*/

/*!
    \enum QNetworkInfo::CellDataTechnology
    This enum describes the type of cellular technology.

    \value UnknownDataTechnology   The cellular technology is unknown or an error occured.
    \value GprsDataTechnology      General Packet Radio Service (GPRS) data service.
    \value EdgeDataTechnology      Enhanced Data Rates for GSM Evolution (EDGE) data service.
    \value UmtsDataTechnology      Universal Mobile Telecommunications System (UMTS) data service.
    \value HspaDataTechnology      High Speed Packet Access (HSPA) data service.
*/

/*!
    \enum QNetworkInfo::NetworkMode
    This enumeration describes the type of the network.

    \value UnknownMode     The network is unknown or an error occured.
    \value GsmMode         Global System for Mobile (GSM) network.
    \value CdmaMode        Code Division Multiple Access (CDMA) network.
    \value WcdmaMode       Wideband Code Division Multiple Access (WCDMA) network.
    \value WlanMode        Wireless local area network (WLAN) network.
    \value EthernetMode    Local area network (LAN), or Ethernet network.
    \value BluetoothMode   Bluetooth network.
    \value WimaxMode       Worldwide Interoperability for Microwave Access (WiMAX) network.
    \value LteMode         3GPP Long Term Evolution (LTE) network.
    \value TdscdmaMode     Time Division Synchronous Code Division Multiple Access (TD-SCDMA) network.
*/

/*!
    \enum QNetworkInfo::NetworkStatus
    This enumeration describes the status of the network.

    \value UnknownStatus        The status is unknown or an error occured.
    \value NoNetworkAvailable   There is no network available.
    \value EmergencyOnly        The network only allows emergency calls.
    \value Searching            The device is searching or connecting to the network.
    \value Busy                 The network is too busy to be connected.
    \value Denied               The connection to the network has been denied.
    \value HomeNetwork          The device is connected to the home network.
    \value Roaming              The device is connected to some roaming network.
*/

/*!
    \fn void QNetworkInfo::cellIdChanged(int interface, const QString &id)

    This signal is emitted whenever the cell ID for \a interface has changed to \a id.
*/

/*!
    \fn void QNetworkInfo::currentCellDataTechnologyChanged(int interface, QNetworkInfo::CellDataTechnology tech)

    This signal is emitted whenever the current cell data technology for \a interface has changed to \a tech.
*/

/*!
    \fn void QNetworkInfo::currentMobileCountryCodeChanged(int interface, const QString &mcc)

    This signal is emitted whenever the current Mobile Country Code (MCC) for \a interface has changed
    to \a mcc.
*/

/*!
    \fn void QNetworkInfo::currentMobileNetworkCodeChanged(int interface, const QString &mnc)

    This signal is emitted whenever the current Mobile Network Code (MNC) for \a interface has changed
    to \a mnc.
*/

/*!
    \fn void QNetworkInfo::currentNetworkModeChanged(QNetworkInfo::NetworkMode mode)

    This signal is emitted whenever the current network has changed to \a mode.
*/

/*!
    \fn void QNetworkInfo::locationAreaCodeChanged(int interface, const QString &lac)

    This signal is emitted whenever the location area code for \a interface has changed to \a lac.
*/

/*!
    \fn void QNetworkInfo::networkInterfaceCountChanged(QNetworkInfo::NetworkMode mode, int count)

    This signal is emitted whenever the number of interfaces for the \a mode has changed to \a count.
*/

/*!
    \fn void QNetworkInfo::networkNameChanged(QNetworkInfo::NetworkMode mode, int interface, const QString &name)

    This signal is emitted whenever the name for the \a interface of \a mode has changed to \a name.
*/

/*!
    \fn void QNetworkInfo::networkSignalStrengthChanged(QNetworkInfo::NetworkMode mode, int interface, int strength)

    This signal is emitted whenever the signal strength for the \a interface of \a mode has changed
    to \a strength.
*/

/*!
    \fn void QNetworkInfo::networkStatusChanged(QNetworkInfo::NetworkMode mode, int interface, QNetworkInfo::NetworkStatus status)

    This signal is emitted whenever the status for the \a interface of \a mode has changed to \a status.
*/

/*!
    Constructs a QNetworkInfo object with the given \a parent.
*/
QNetworkInfo::QNetworkInfo(QObject *parent)
    : QObject(parent)
#if !defined(QT_SIMULATOR)
    , d_ptr(new QNetworkInfoPrivate(this))
#else
    , d_ptr(new QNetworkInfoSimulator(this))
#endif // QT_SIMULATOR
{
}

/*!
    Destroys the object
*/
QNetworkInfo::~QNetworkInfo()
{
    delete d_ptr;
}

/*!
    Returns the number of interfaces for the \a mode. If the information is not available, or error
    occurs, -1 is returned.
*/
int QNetworkInfo::networkInterfaceCount(QNetworkInfo::NetworkMode mode) const
{
    return d_ptr->networkInterfaceCount(mode);
}

/*!
    Returns the signal strength for \a interfaceDevice of \a mode, in 0 - 100 scale. If the information
    is not available, or error occurs, -1 is returned.
*/
int QNetworkInfo::networkSignalStrength(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
{
    return d_ptr->networkSignalStrength(mode, interfaceDevice);
}

/*!
    Returns the current cell data technology used for \a interfaceDevice.
*/
QNetworkInfo::CellDataTechnology QNetworkInfo::currentCellDataTechnology(int interfaceDevice) const
{
    return d_ptr->currentCellDataTechnology(interfaceDevice);
}

/*!
    Returns the current active network mode. If there are more than one modes activated, the preferred
    one is returned.
*/
QNetworkInfo::NetworkMode QNetworkInfo::currentNetworkMode() const
{
    return d_ptr->currentNetworkMode();
}

/*!
    Returns the current status for \a interfaceDevice of \a mode.
*/
QNetworkInfo::NetworkStatus QNetworkInfo::networkStatus(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
{
    return d_ptr->networkStatus(mode, interfaceDevice);
}

#ifndef QT_NO_NETWORKINTERFACE
/*!
    Returns the first found interface for \a interfaceDevice of \a mode. If none is found, or it can't be
    represented by QNetworkInterface (e.g. Bluetooth), and empty object is returned.
*/
QNetworkInterface QNetworkInfo::interfaceForMode(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
{
    return d_ptr->interfaceForMode(mode, interfaceDevice);
}
#endif // QT_NO_NETWORKINTERFACE

/*!
    Returns the cell ID of the connected tower or based station for \a interfaceDevice. If this information
    is not available or error occurs, an empty string is returned.
*/
QString QNetworkInfo::cellId(int interfaceDevice) const
{
    return d_ptr->cellId(interfaceDevice);
}

/*!
    Returns the current Mobile Country Code (MCC) for \a interfaceDevice. An empty string is returned if the
    information is not available or an error occurs.
*/
QString QNetworkInfo::currentMobileCountryCode(int interfaceDevice) const
{
    return d_ptr->currentMobileCountryCode(interfaceDevice);
}

/*!
    Returns the current Mobile Network Code (MNC) for \a interfaceDevice. An empty string is returned if the
    information is not available or an error occurs.
*/
QString QNetworkInfo::currentMobileNetworkCode(int interfaceDevice) const
{
    return d_ptr->currentMobileNetworkCode(interfaceDevice);
}

/*!
    Returns the home Mobile Country Code (MCC) for \a interfaceDevice. An empty string is returned if the
    information is not available or an error occurs.
*/
QString QNetworkInfo::homeMobileCountryCode(int interfaceDevice) const
{
    return d_ptr->homeMobileCountryCode(interfaceDevice);
}

/*!
    Returns the home Mobile Network Code (MNC) for \a interfaceDevice. An empty string is returned if the
    information is not available or an error occurs.
*/
QString QNetworkInfo::homeMobileNetworkCode(int interfaceDevice) const
{
    return d_ptr->homeMobileNetworkCode(interfaceDevice);
}

/*!
    Returns the International Mobile Subscriber Identity (IMSI) for \a interfaceDevice. If this information is
    not available, or error occurs, an empty string is returned.
*/
QString QNetworkInfo::imsi(int interfaceDevice) const
{
    return d_ptr->imsi(interfaceDevice);
}

/*!
    Returns the location area code of the current cellular radio network for \a interfaceDevice. If this information
    is not available or error occurs, an empty string is returned.
*/
QString QNetworkInfo::locationAreaCode(int interfaceDevice) const
{
    return d_ptr->locationAreaCode(interfaceDevice);
}

/*!
    Returns the MAC address for \a interfaceDevice of \a mode. If the MAC address is not available or error
    occurs, an empty string is returned.
*/
QString QNetworkInfo::macAddress(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
{
    return d_ptr->macAddress(mode, interfaceDevice);
}

/*!
    Returns the name of the operator for \a interfaceDevice of \a mode. If the information is not available,
    or an error occurs, an empty string is returned.

    In case of WLAN, the SSID is returned; for Ethernet, the domain name is returned if available.
*/
QString QNetworkInfo::networkName(QNetworkInfo::NetworkMode mode, int interfaceDevice) const
{
    return d_ptr->networkName(mode, interfaceDevice);
}

extern QMetaMethod proxyToSourceSignal(const QMetaMethod &, QObject *);

/*!
    \internal
*/
void QNetworkInfo::connectNotify(const QMetaMethod &signal)
{
#if defined(Q_OS_LINUX) || defined(Q_OS_WIN) || defined(QT_SIMULATOR) || defined(Q_OS_MAC)
    QMetaMethod sourceSignal = proxyToSourceSignal(signal, d_ptr);
    connect(d_ptr, sourceSignal, this, signal, Qt::UniqueConnection);
#else
    Q_UNUSED(signal)
#endif
}

/*!
    \internal
*/
void QNetworkInfo::disconnectNotify(const QMetaMethod &signal)
{
#if defined(Q_OS_LINUX) || defined(Q_OS_WIN) || defined(QT_SIMULATOR) || defined(Q_OS_MAC)
    // We can only disconnect with the private implementation, when there is no receivers for the signal.
    if (isSignalConnected(signal))
        return;

    QMetaMethod sourceSignal = proxyToSourceSignal(signal, d_ptr);
    disconnect(d_ptr, sourceSignal, this, signal);
#else
    Q_UNUSED(signal)
#endif
}

QT_END_NAMESPACE