source: trunk/examples/network/bearermonitor/bearermonitor.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 13.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "bearermonitor.h"
42#include "sessionwidget.h"
43
44#include <QtCore/QDebug>
45
46#ifdef Q_OS_WIN
47#include <winsock2.h>
48#undef interface
49
50#ifndef NS_NLA
51#define NS_NLA 15
52#endif
53#endif
54
55BearerMonitor::BearerMonitor(QWidget *parent)
56: QWidget(parent)
57{
58 setupUi(this);
59#ifdef MAEMO_UI
60 newSessionButton->hide();
61 deleteSessionButton->hide();
62#else
63 delete tabWidget->currentWidget();
64 sessionGroup->hide();
65#endif
66#if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(MAEMO_UI)
67 setWindowState(Qt::WindowMaximized);
68#endif
69 updateConfigurations();
70 onlineStateChanged(!manager.allConfigurations(QNetworkConfiguration::Active).isEmpty());
71 QNetworkConfiguration defaultConfiguration = manager.defaultConfiguration();
72 for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
73 QTreeWidgetItem *item = treeWidget->topLevelItem(i);
74
75 if (item->data(0, Qt::UserRole).toString() == defaultConfiguration.identifier()) {
76 treeWidget->setCurrentItem(item);
77 showConfigurationFor(item);
78 break;
79 }
80 }
81 connect(&manager, SIGNAL(onlineStateChanged(bool)), this ,SLOT(onlineStateChanged(bool)));
82 connect(&manager, SIGNAL(configurationAdded(const QNetworkConfiguration&)),
83 this, SLOT(configurationAdded(const QNetworkConfiguration&)));
84 connect(&manager, SIGNAL(configurationRemoved(const QNetworkConfiguration&)),
85 this, SLOT(configurationRemoved(const QNetworkConfiguration&)));
86 connect(&manager, SIGNAL(configurationChanged(const QNetworkConfiguration&)),
87 this, SLOT(configurationChanged(const QNetworkConfiguration)));
88 connect(&manager, SIGNAL(updateCompleted()), this, SLOT(updateConfigurations()));
89
90#ifdef Q_OS_WIN
91 connect(registerButton, SIGNAL(clicked()), this, SLOT(registerNetwork()));
92 connect(unregisterButton, SIGNAL(clicked()), this, SLOT(unregisterNetwork()));
93#else
94 nlaGroup->hide();
95#endif
96
97 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
98 this, SLOT(createSessionFor(QTreeWidgetItem*)));
99
100 connect(treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
101 this, SLOT(showConfigurationFor(QTreeWidgetItem*)));
102
103 connect(newSessionButton, SIGNAL(clicked()),
104 this, SLOT(createNewSession()));
105#ifndef MAEMO_UI
106 connect(deleteSessionButton, SIGNAL(clicked()),
107 this, SLOT(deleteSession()));
108#endif
109 connect(scanButton, SIGNAL(clicked()),
110 this, SLOT(performScan()));
111
112 // Just in case update all configurations so that all
113 // configurations are up to date.
114 manager.updateConfigurations();
115}
116
117BearerMonitor::~BearerMonitor()
118{
119}
120
121static void updateItem(QTreeWidgetItem *item, const QNetworkConfiguration &config)
122{
123 item->setText(0, config.name());
124 item->setData(0, Qt::UserRole, config.identifier());
125
126 QFont font = item->font(1);
127 font.setBold((config.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active);
128 item->setFont(0, font);
129}
130
131void BearerMonitor::configurationAdded(const QNetworkConfiguration &config, QTreeWidgetItem *parent)
132{
133 QTreeWidgetItem *item = new QTreeWidgetItem;
134 updateItem(item, config);
135
136 if (parent)
137 parent->addChild(item);
138 else
139 treeWidget->addTopLevelItem(item);
140
141 if (config.type() == QNetworkConfiguration::ServiceNetwork) {
142 foreach (const QNetworkConfiguration &child, config.children())
143 configurationAdded(child, item);
144 }
145}
146
147void BearerMonitor::configurationRemoved(const QNetworkConfiguration &config)
148{
149 for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
150 QTreeWidgetItem *item = treeWidget->topLevelItem(i);
151
152 if (item->data(0, Qt::UserRole).toString() == config.identifier()) {
153 delete item;
154 break;
155 }
156 }
157}
158
159void BearerMonitor::configurationChanged(const QNetworkConfiguration &config)
160{
161 for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
162 QTreeWidgetItem *item = treeWidget->topLevelItem(i);
163
164 if (item->data(0, Qt::UserRole).toString() == config.identifier()) {
165 updateItem(item, config);
166
167 if (config.type() == QNetworkConfiguration::ServiceNetwork)
168 updateSnapConfiguration(item, config);
169
170 if (item == treeWidget->currentItem())
171 showConfigurationFor(item);
172
173 break;
174 }
175 }
176}
177
178void BearerMonitor::updateSnapConfiguration(QTreeWidgetItem *parent, const QNetworkConfiguration &snap)
179{
180 QMap<QString, QTreeWidgetItem *> itemMap;
181 foreach (QTreeWidgetItem *item, parent->takeChildren())
182 itemMap.insert(item->data(0, Qt::UserRole).toString(), item);
183
184 QList<QNetworkConfiguration> allConfigurations = snap.children();
185
186 while (!allConfigurations.isEmpty()) {
187 QNetworkConfiguration config = allConfigurations.takeFirst();
188
189 QTreeWidgetItem *item = itemMap.take(config.identifier());
190 if (item) {
191 updateItem(item, config);
192
193 parent->addChild(item);
194
195 if (config.type() == QNetworkConfiguration::ServiceNetwork)
196 updateSnapConfiguration(item, config);
197 } else {
198 configurationAdded(config, parent);
199 }
200 }
201
202 qDeleteAll(itemMap);
203}
204
205void BearerMonitor::updateConfigurations()
206{
207 progressBar->hide();
208 scanButton->show();
209
210 // Just in case update online state, on Symbian platform
211 // WLAN scan needs to be triggered initially to have their true state.
212 onlineStateChanged(manager.isOnline());
213
214 QList<QTreeWidgetItem *> items = treeWidget->findItems(QLatin1String("*"), Qt::MatchWildcard);
215 QMap<QString, QTreeWidgetItem *> itemMap;
216 while (!items.isEmpty()) {
217 QTreeWidgetItem *item = items.takeFirst();
218 itemMap.insert(item->data(0, Qt::UserRole).toString(), item);
219 }
220
221 QNetworkConfiguration defaultConfiguration = manager.defaultConfiguration();
222 QTreeWidgetItem *defaultItem = itemMap.take(defaultConfiguration.identifier());
223
224 if (defaultItem) {
225 updateItem(defaultItem, defaultConfiguration);
226
227 if (defaultConfiguration.type() == QNetworkConfiguration::ServiceNetwork)
228 updateSnapConfiguration(defaultItem, defaultConfiguration);
229 } else if (defaultConfiguration.isValid()) {
230 configurationAdded(defaultConfiguration);
231 }
232
233 QList<QNetworkConfiguration> allConfigurations = manager.allConfigurations();
234
235 while (!allConfigurations.isEmpty()) {
236 QNetworkConfiguration config = allConfigurations.takeFirst();
237
238 if (config.identifier() == defaultConfiguration.identifier())
239 continue;
240
241 QTreeWidgetItem *item = itemMap.take(config.identifier());
242 if (item) {
243 updateItem(item, config);
244
245 if (config.type() == QNetworkConfiguration::ServiceNetwork)
246 updateSnapConfiguration(item, config);
247 } else {
248 configurationAdded(config);
249 }
250 }
251
252 qDeleteAll(itemMap);
253}
254
255void BearerMonitor::onlineStateChanged(bool isOnline)
256{
257 if (isOnline)
258 onlineState->setText(tr("Online"));
259 else
260 onlineState->setText(tr("Offline"));
261}
262
263#ifdef Q_OS_WIN
264void BearerMonitor::registerNetwork()
265{
266 QTreeWidgetItem *item = treeWidget->currentItem();
267 if (!item) return;
268
269 QNetworkConfiguration configuration =
270 manager.configurationFromIdentifier(item->data(0, Qt::UserRole).toString());
271
272 const QString name = configuration.name();
273
274 qDebug() << "Registering" << name << "with system";
275
276 WSAQUERYSET networkInfo;
277 memset(&networkInfo, 0, sizeof(networkInfo));
278 networkInfo.dwSize = sizeof(networkInfo);
279 networkInfo.lpszServiceInstanceName = (LPWSTR)name.utf16();
280 networkInfo.dwNameSpace = NS_NLA;
281
282 if (WSASetService(&networkInfo, RNRSERVICE_REGISTER, 0) == SOCKET_ERROR)
283 qDebug() << "WSASetService(RNRSERVICE_REGISTER) returned" << WSAGetLastError();
284}
285
286void BearerMonitor::unregisterNetwork()
287{
288 QTreeWidgetItem *item = treeWidget->currentItem();
289 if (!item) return;
290
291 QNetworkConfiguration configuration =
292 manager.configurationFromIdentifier(item->data(0, Qt::UserRole).toString());
293
294 const QString name = configuration.name();
295
296 qDebug() << "Unregistering" << name << "with system";
297
298 WSAQUERYSET networkInfo;
299 memset(&networkInfo, 0, sizeof(networkInfo));
300 networkInfo.dwSize = sizeof(networkInfo);
301 networkInfo.lpszServiceInstanceName = (LPWSTR)name.utf16();
302 networkInfo.dwNameSpace = NS_NLA;
303
304 if (WSASetService(&networkInfo, RNRSERVICE_DELETE, 0) == SOCKET_ERROR)
305 qDebug() << "WSASetService(RNRSERVICE_DELETE) returned" << WSAGetLastError();
306}
307#endif
308
309void BearerMonitor::showConfigurationFor(QTreeWidgetItem *item)
310{
311 QString identifier;
312
313 if (item)
314 identifier = item->data(0, Qt::UserRole).toString();
315
316 QNetworkConfiguration conf = manager.configurationFromIdentifier(identifier);
317
318 switch (conf.state()) {
319 case QNetworkConfiguration::Active:
320 configurationState->setText(tr("Active"));
321 break;
322 case QNetworkConfiguration::Discovered:
323 configurationState->setText(tr("Discovered"));
324 break;
325 case QNetworkConfiguration::Defined:
326 configurationState->setText(tr("Defined"));
327 break;
328 case QNetworkConfiguration::Undefined:
329 configurationState->setText(tr("Undefined"));
330 break;
331 default:
332 configurationState->setText(QString());
333 }
334
335 switch (conf.type()) {
336 case QNetworkConfiguration::InternetAccessPoint:
337 configurationType->setText(tr("Internet Access Point"));
338 break;
339 case QNetworkConfiguration::ServiceNetwork:
340 configurationType->setText(tr("Service Network"));
341 break;
342 case QNetworkConfiguration::UserChoice:
343 configurationType->setText(tr("User Choice"));
344 break;
345 case QNetworkConfiguration::Invalid:
346 configurationType->setText(tr("Invalid"));
347 break;
348 default:
349 configurationType->setText(QString());
350 }
351
352 switch (conf.purpose()) {
353 case QNetworkConfiguration::UnknownPurpose:
354 configurationPurpose->setText(tr("Unknown"));
355 break;
356 case QNetworkConfiguration::PublicPurpose:
357 configurationPurpose->setText(tr("Public"));
358 break;
359 case QNetworkConfiguration::PrivatePurpose:
360 configurationPurpose->setText(tr("Private"));
361 break;
362 case QNetworkConfiguration::ServiceSpecificPurpose:
363 configurationPurpose->setText(tr("Service Specific"));
364 break;
365 default:
366 configurationPurpose->setText(QString());
367 }
368
369 configurationIdentifier->setText(conf.identifier());
370
371 configurationRoaming->setText(conf.isRoamingAvailable() ? tr("Available") : tr("Not available"));
372
373 configurationChildren->setText(QString::number(conf.children().count()));
374
375 configurationName->setText(conf.name());
376}
377
378void BearerMonitor::createSessionFor(QTreeWidgetItem *item)
379{
380 const QString identifier = item->data(0, Qt::UserRole).toString();
381
382 QNetworkConfiguration conf = manager.configurationFromIdentifier(identifier);
383
384 SessionWidget *session = new SessionWidget(conf);
385
386 tabWidget->addTab(session, conf.name());
387
388#ifndef MAEMO_UI
389 sessionGroup->show();
390#endif
391
392 sessionWidgets.append(session);
393}
394
395void BearerMonitor::createNewSession()
396{
397 QTreeWidgetItem *item = treeWidget->currentItem();
398 if (!item) return;
399
400 createSessionFor(item);
401}
402
403#ifndef MAEMO_UI
404void BearerMonitor::deleteSession()
405{
406 SessionWidget *session = qobject_cast<SessionWidget *>(tabWidget->currentWidget());
407 if (session) {
408 sessionWidgets.removeAll(session);
409
410 delete session;
411
412 if (tabWidget->count() == 0)
413 sessionGroup->hide();
414 }
415}
416#endif
417
418void BearerMonitor::performScan()
419{
420 scanButton->hide();
421 progressBar->show();
422 manager.updateConfigurations();
423}
Note: See TracBrowser for help on using the repository browser.