summaryrefslogtreecommitdiffstats
path: root/src/systeminfo/qsystemnetworkinfo.cpp
blob: 55088c0b46285a3a83b67eb44c149baecf279247 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qsystemnetworkinfo.h"
#include "qsysteminfocommon_p.h"
#include <QMetaType>

QTM_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(QSystemNetworkInfoPrivate, netInfoPrivate)

#ifdef QT_SIMULATOR
QSystemNetworkInfoPrivate *getSystemNetworkInfoPrivate() { return netInfoPrivate(); }
#endif // QT_SIMULATOR

/*!
    \class QSystemNetworkInfo
    \ingroup systeminfo
    \inmodule QtSystemInfo
    \brief The QSystemNetworkInfo class provides access to various networking status and signals.
*/

/*!
    \enum QSystemNetworkInfo::NetworkStatus
    This enum describes the status of the network connection:

    \value UndefinedStatus        There is no network device, or on error.
    \value NoNetworkAvailable     There is no network available.
    \value EmergencyOnly          Emergency calls only.
    \value Searching              Searching for or connecting with the network.
    \value Busy                   Network is busy.
    \value Connected              Connected to network.
    \value HomeNetwork            On Home Network.
    \value Denied                 Network access denied.
    \value Roaming                On Roaming network.
*/

/*!
    \enum QSystemNetworkInfo::NetworkMode
    This enum describes the type of network:

    \value UnknownMode             Unknown network, or network error.
    \value GsmMode                 Global System for Mobile (GSM) network.
    \value CdmaMode                Code Division Multiple Access (CDMA) network.
    \value WcdmaMode               Wideband Code Division Multiple Access (W-CDMA) network.
    \value WlanMode                Wireless Local Area Network (WLAN) network.
    \value EthernetMode            Wired Local Area network.
    \value BluetoothMode           Bluetooth network.
    \value WimaxMode               Wimax network.
    \value LteMode                 Lte network.
*/

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

    \value UnknownDataTechnology   Unknown cellular technology, or error.
    \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.
*/

/*!
    \fn void QSystemNetworkInfo::networkStatusChanged(QSystemNetworkInfo::NetworkMode mode, QSystemNetworkInfo::NetworkStatus status)

    This signal is emitted whenever the network status of \a mode changes, specified by \a status.
*/

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

    This signal is emitted whenever the network \a mode signal strength changes, specified by \a strength.
*/

/*!
    \fn void QSystemNetworkInfo::currentMobileCountryCodeChanged(const QString &mcc)

    This signal is emitted whenever the Mobile Country Code changes, specified by \a mcc.
*/

/*!
    \fn void QSystemNetworkInfo::currentMobileNetworkCodeChanged(const QString &mnc)

    This signal is emitted whenever the network Mobile Network Code changes, specified by \a mnc.
*/

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

    This signal is emitted whenever the network \a mode name changes, specified by \a name.
*/

/*!
    \fn void QSystemNetworkInfo::cellIdChanged(int cellId)

    This signal is emitted whenever the network cell changes, specified by \a cellId.
*/

/*!
    \fn void QSystemNetworkInfo::networkModeChanged(QSystemNetworkInfo::NetworkMode mode)

    This signal is emitted whenever the network mode changes, specified by \a mode.
*/

/*!
    \fn void QSystemNetworkInfo::cellDataTechnologyChanged(QSystemNetworkInfo::CellDataTechnology cellTech)

    This signal is emitted whenever the cellular technology changes, specified by \a cellTech.
*/

/*!
    Constructs a QSystemNetworkInfo with the given \a parent.
*/
QSystemNetworkInfo::QSystemNetworkInfo(QObject *parent)
   : QObject(parent)
   , d(netInfoPrivate())
{
    qRegisterMetaType<QSystemNetworkInfo::NetworkMode>("QSystemNetworkInfo::NetworkMode");
    qRegisterMetaType<QSystemNetworkInfo::NetworkStatus>("QSystemNetworkInfo::NetworkStatus");
}

/*!
    Destroys the QSystemNetworkInfo object.
*/
QSystemNetworkInfo::~QSystemNetworkInfo()
{
}

/*!
    Returns the status of the network \a mode.
*/
QSystemNetworkInfo::NetworkStatus QSystemNetworkInfo::networkStatus(QSystemNetworkInfo::NetworkMode mode)
{
    return netInfoPrivate()->networkStatus(mode);
}

/*!
    Returns the strength of the network signal, per network \a mode , 0 - 100 linear scaling. -1 is returned
    if not available or on error.

    In the case of QSystemNetworkInfo::EthernetMode, it will either be 100 for carrier active, or 0 for when
    there is no carrier or cable connected.
*/
int QSystemNetworkInfo::networkSignalStrength(QSystemNetworkInfo::NetworkMode mode)
{
    QSystemNetworkInfo::NetworkStatus info = netInfoPrivate()->networkStatus(mode);
    if (info == QSystemNetworkInfo::UndefinedStatus || info == QSystemNetworkInfo::NoNetworkAvailable)
        return -1;

    return netInfoPrivate()->networkSignalStrength(mode);
}

/*!
    \property QSystemNetworkInfo::cellId
    \brief The devices Cell ID

    Returns the Cell ID of the connected tower or based station. -1 is returned if not available or on error.
*/
int QSystemNetworkInfo::cellId()
{
    return netInfoPrivate()->cellId();
}

/*!
    \property QSystemNetworkInfo::locationAreaCode
    \brief The LAC.

    Returns the location area code of the current cellular radio network. -1 is returned if not available
    or on error.
*/
int QSystemNetworkInfo::locationAreaCode()
{
    return netInfoPrivate()->locationAreaCode();
}

/*!
    \property QSystemNetworkInfo::currentMobileCountryCode
    \brief The current MCC.

    Returns the current Mobile Country Code. An empty string is returned if not available or on error.
*/
QString QSystemNetworkInfo::currentMobileCountryCode()
{
    return netInfoPrivate()->currentMobileCountryCode();
}

/*!
    \property QSystemNetworkInfo::currentMobileNetworkCode
    \brief The current MNC.

    Returns the current Mobile Network Code. An empty string is returned if not available or on error.
*/
QString QSystemNetworkInfo::currentMobileNetworkCode()
{
    return netInfoPrivate()->currentMobileNetworkCode();
}

/*!
    \property QSystemNetworkInfo::homeMobileCountryCode
    \brief The home MNC.

    Returns the home Mobile Country Code. An empty string is returned if not available or on error.
*/
QString QSystemNetworkInfo::homeMobileCountryCode()
{
    return netInfoPrivate()->homeMobileCountryCode();
}

/*!
    \property QSystemNetworkInfo::homeMobileNetworkCode
    \brief The home MCC.

    Returns the home Mobile Network Code. An empty string is returned if not available or on error.
*/
QString QSystemNetworkInfo::homeMobileNetworkCode()
{
    return netInfoPrivate()->homeMobileNetworkCode();
}

/*!
    Returns the name of the operator for the network \a mode. An empty string is returned if not
    available or on error.

    For WLAN this returns the network's current SSID.
*/
QString QSystemNetworkInfo::networkName(QSystemNetworkInfo::NetworkMode mode)
{
    return netInfoPrivate()->networkName(mode);
}

/*!
    Returns the MAC address for the interface servicing the network \a mode. An empty string is
    returned if not available or on error.
*/
QString QSystemNetworkInfo::macAddress(QSystemNetworkInfo::NetworkMode mode)
{
    return netInfoPrivate()->macAddress(mode);
}

/*!
    Returns the first found QNetworkInterface for type \a mode. If none is found, or it can't be represented
    by QNetworkInterface (e.g. Bluetooth), an invalid QNetworkInterface object is returned.
*/
QNetworkInterface QSystemNetworkInfo::interfaceForMode(QSystemNetworkInfo::NetworkMode mode)
{
    return netInfoPrivate()->interfaceForMode(mode);
}

/*!
    \property QSystemNetworkInfo::currentMode

    Returns the current active network mode. If more than one mode is active, returns the
    default or preferred mode. If no modes are active, returns UnknownMode.
*/
QSystemNetworkInfo::NetworkMode QSystemNetworkInfo::currentMode()
{
    return netInfoPrivate()->currentMode();
}

/*!
    \internal
*/
void QSystemNetworkInfo::connectNotify(const char *signal)
{
    // connect only once
    if (receivers(signal) > 1)
        return;

    //check for networkSignalStrengthChanged() signal connect notification
    //This is not required on all platforms
#if defined(Q_WS_MAEMO_5)
    if (QLatin1String(signal) == SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int))) {
        netInfoPrivate()->setWlanSignalStrengthCheckEnabled(true);
    } else
#endif // Q_WS_MAEMO_5
    if (QLatin1String(signal) == SIGNAL(currentMobileCountryCodeChanged(QString))) {
        connect(d, SIGNAL(currentMobileCountryCodeChanged(QString)),
                this, SIGNAL(currentMobileCountryCodeChanged(QString)));
    } else if (QLatin1String(signal) == SIGNAL(currentMobileNetworkCodeChanged(QString))) {
        connect(d, SIGNAL(currentMobileNetworkCodeChanged(QString)),
                this, SIGNAL(currentMobileNetworkCodeChanged(QString)));
    } else if (QLatin1String(signal) == SIGNAL(networkModeChanged(QSystemNetworkInfo::NetworkMode))) {
        connect(d, SIGNAL(networkModeChanged(QSystemNetworkInfo::NetworkMode)),
                this, SIGNAL(networkModeChanged(QSystemNetworkInfo::NetworkMode)));
    } else if (QLatin1String(signal) == SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString))) {
        connect(d, SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)),
                this, SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)));
    } else if (QLatin1String(signal) == SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int))) {
        connect(d, SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)),
                this, SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)));
    } else if (QLatin1String(signal) == SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus))) {
        connect(d, SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)),
                this, SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)));
    } else if (QLatin1String(signal) == SIGNAL(cellIdChanged(int))) {
        connect(d, SIGNAL(cellIdChanged(int)),
                this, SIGNAL(cellIdChanged(int)));
    } else if (QLatin1String(signal) == SIGNAL(cellIdChanged(int))) {
        connect(d, SIGNAL(cellDataTechnologyChanged(QSystemNetworkInfo::CellDataTechnology)),
                this, SIGNAL(cellDataTechnologyChanged(QSystemNetworkInfo::CellDataTechnology)));
    }
}

/*!
    \internal
*/
void QSystemNetworkInfo::disconnectNotify(const char *signal)
{
    // disconnect only when there's no connections
    if (receivers(signal) == 0)
        return;

    //check for networkSignalStrengthChanged() signal disconnect notification
    //This is not required on all platforms
#if defined(Q_WS_MAEMO_5)
    if (QLatin1String(signal) == SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int))) {
        netInfoPrivate()->setWlanSignalStrengthCheckEnabled(false);
    } else
#endif // Q_WS_MAEMO_5
    if (QLatin1String(signal) == SIGNAL(currentMobileCountryCodeChanged(QString))) {
        disconnect(d, SIGNAL(currentMobileCountryCodeChanged(QString)),
                   this, SIGNAL(currentMobileCountryCodeChanged(QString)));
    } else if (QLatin1String(signal) == SIGNAL(currentMobileNetworkCodeChanged(QString))) {
        disconnect(d, SIGNAL(currentMobileNetworkCodeChanged(QString)),
                   this, SIGNAL(currentMobileNetworkCodeChanged(QString)));
    } else if (QLatin1String(signal) == SIGNAL(networkModeChanged(QSystemNetworkInfo::NetworkMode))) {
        disconnect(d, SIGNAL(networkModeChanged(QSystemNetworkInfo::NetworkMode)),
                   this, SIGNAL(networkModeChanged(QSystemNetworkInfo::NetworkMode)));
    } else if (QLatin1String(signal) == SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString))) {
        disconnect(d, SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)),
                   this, SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)));
    } else if (QLatin1String(signal) == SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int))) {
        disconnect(d, SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)),
                   this, SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)));
    } else if (QLatin1String(signal) == SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus))) {
        disconnect(d, SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)),
                   this, SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)));
    } else if (QLatin1String(signal) == SIGNAL(cellIdChanged(int))) {
        disconnect(d, SIGNAL(cellIdChanged(int)),
                   this, SIGNAL(cellIdChanged(int)));
    } else if (QLatin1String(signal) == SIGNAL(cellIdChanged(int))) {
        disconnect(d, SIGNAL(cellDataTechnologyChanged(QSystemNetworkInfo::CellDataTechnology)),
                   this, SIGNAL(cellDataTechnologyChanged(QSystemNetworkInfo::CellDataTechnology)));
    }
}

/*!
    \property QSystemNetworkInfo::cellDataTechnology

    Returns the current active cell data technology.

    If no data technology is active, or data technology is not supported, QSystemNetworkInfo::UnknownDataTechnology
    is returned.
*/
QSystemNetworkInfo::CellDataTechnology QSystemNetworkInfo::cellDataTechnology()
{
    return netInfoPrivate()->cellDataTechnology();
}

#include "moc_qsystemnetworkinfo.cpp"

QTM_END_NAMESPACE