|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "bearermonitor.h" |
|
43 #include "sessionwidget.h" |
|
44 |
|
45 #include <QDebug> |
|
46 |
|
47 #ifdef Q_OS_WIN |
|
48 #include <winsock2.h> |
|
49 #undef interface |
|
50 |
|
51 #ifndef NS_NLA |
|
52 #define NS_NLA 15 |
|
53 #endif |
|
54 #endif |
|
55 |
|
56 BearerMonitor::BearerMonitor(QWidget *parent) |
|
57 : QWidget(parent) |
|
58 { |
|
59 setupUi(this); |
|
60 delete tabWidget->currentWidget(); |
|
61 sessionGroup->hide(); |
|
62 #if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) |
|
63 setWindowState(Qt::WindowMaximized); |
|
64 #endif |
|
65 updateConfigurations(); |
|
66 |
|
67 onlineStateChanged(!manager.allConfigurations(QNetworkConfiguration::Active).isEmpty()); |
|
68 |
|
69 QNetworkConfiguration defaultConfiguration = manager.defaultConfiguration(); |
|
70 for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) { |
|
71 QTreeWidgetItem *item = treeWidget->topLevelItem(i); |
|
72 |
|
73 if (item->data(0, Qt::UserRole).toString() == defaultConfiguration.identifier()) { |
|
74 treeWidget->setCurrentItem(item); |
|
75 showConfigurationFor(item); |
|
76 break; |
|
77 } |
|
78 } |
|
79 |
|
80 connect(&manager, SIGNAL(configurationAdded(const QNetworkConfiguration&)), |
|
81 this, SLOT(configurationAdded(const QNetworkConfiguration&))); |
|
82 connect(&manager, SIGNAL(configurationRemoved(const QNetworkConfiguration&)), |
|
83 this, SLOT(configurationRemoved(const QNetworkConfiguration&))); |
|
84 connect(&manager, SIGNAL(configurationChanged(const QNetworkConfiguration&)), |
|
85 this, SLOT(configurationChanged(const QNetworkConfiguration))); |
|
86 connect(&manager, SIGNAL(updateCompleted()), this, SLOT(updateConfigurations())); |
|
87 connect(&manager, SIGNAL(onlineStateChanged(bool)), this ,SLOT(onlineStateChanged(bool))); |
|
88 |
|
89 #ifdef Q_OS_WIN |
|
90 connect(registerButton, SIGNAL(clicked()), this, SLOT(registerNetwork())); |
|
91 connect(unregisterButton, SIGNAL(clicked()), this, SLOT(unregisterNetwork())); |
|
92 #else |
|
93 nlaGroup->hide(); |
|
94 #endif |
|
95 |
|
96 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)), |
|
97 this, SLOT(createSessionFor(QTreeWidgetItem*))); |
|
98 |
|
99 connect(treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), |
|
100 this, SLOT(showConfigurationFor(QTreeWidgetItem*))); |
|
101 |
|
102 connect(newSessionButton, SIGNAL(clicked()), |
|
103 this, SLOT(createNewSession())); |
|
104 connect(deleteSessionButton, SIGNAL(clicked()), |
|
105 this, SLOT(deleteSession())); |
|
106 |
|
107 connect(scanButton, SIGNAL(clicked()), |
|
108 this, SLOT(performScan())); |
|
109 } |
|
110 |
|
111 BearerMonitor::~BearerMonitor() |
|
112 { |
|
113 } |
|
114 |
|
115 static void updateItem(QTreeWidgetItem *item, const QNetworkConfiguration &config) |
|
116 { |
|
117 item->setText(0, config.name()); |
|
118 item->setData(0, Qt::UserRole, config.identifier()); |
|
119 |
|
120 QFont font = item->font(1); |
|
121 font.setBold((config.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active); |
|
122 item->setFont(0, font); |
|
123 } |
|
124 |
|
125 void BearerMonitor::configurationAdded(const QNetworkConfiguration &config, QTreeWidgetItem *parent) |
|
126 { |
|
127 QTreeWidgetItem *item = new QTreeWidgetItem; |
|
128 updateItem(item, config); |
|
129 |
|
130 if (parent) |
|
131 parent->addChild(item); |
|
132 else |
|
133 treeWidget->addTopLevelItem(item); |
|
134 |
|
135 if (config.type() == QNetworkConfiguration::ServiceNetwork) { |
|
136 foreach (const QNetworkConfiguration &child, config.children()) |
|
137 configurationAdded(child, item); |
|
138 } |
|
139 } |
|
140 |
|
141 void BearerMonitor::configurationRemoved(const QNetworkConfiguration &config) |
|
142 { |
|
143 for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) { |
|
144 QTreeWidgetItem *item = treeWidget->topLevelItem(i); |
|
145 |
|
146 if (item->data(0, Qt::UserRole).toString() == config.identifier()) { |
|
147 delete item; |
|
148 break; |
|
149 } |
|
150 } |
|
151 } |
|
152 |
|
153 void BearerMonitor::configurationChanged(const QNetworkConfiguration &config) |
|
154 { |
|
155 for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) { |
|
156 QTreeWidgetItem *item = treeWidget->topLevelItem(i); |
|
157 |
|
158 if (item->data(0, Qt::UserRole).toString() == config.identifier()) { |
|
159 updateItem(item, config); |
|
160 |
|
161 if (config.type() == QNetworkConfiguration::ServiceNetwork) |
|
162 updateSnapConfiguration(item, config); |
|
163 |
|
164 if (item == treeWidget->currentItem()) |
|
165 showConfigurationFor(item); |
|
166 |
|
167 break; |
|
168 } |
|
169 } |
|
170 } |
|
171 |
|
172 void BearerMonitor::updateSnapConfiguration(QTreeWidgetItem *parent, const QNetworkConfiguration &snap) |
|
173 { |
|
174 QMap<QString, QTreeWidgetItem *> itemMap; |
|
175 for (int i = 0; i < parent->childCount(); ++i) { |
|
176 QTreeWidgetItem *item = parent->child(i); |
|
177 itemMap.insert(item->data(0, Qt::UserRole).toString(), item); |
|
178 } |
|
179 |
|
180 QList<QNetworkConfiguration> allConfigurations = snap.children(); |
|
181 |
|
182 while (!allConfigurations.isEmpty()) { |
|
183 QNetworkConfiguration config = allConfigurations.takeFirst(); |
|
184 |
|
185 QTreeWidgetItem *item = itemMap.take(config.identifier()); |
|
186 if (item) { |
|
187 updateItem(item, config); |
|
188 |
|
189 if (config.type() == QNetworkConfiguration::ServiceNetwork) |
|
190 updateSnapConfiguration(item, config); |
|
191 } else { |
|
192 configurationAdded(config, parent); |
|
193 } |
|
194 } |
|
195 |
|
196 foreach (const QString &id, itemMap.keys()) |
|
197 delete itemMap.value(id); |
|
198 |
|
199 itemMap.clear(); |
|
200 } |
|
201 |
|
202 void BearerMonitor::updateConfigurations() |
|
203 { |
|
204 progressBar->hide(); |
|
205 scanButton->show(); |
|
206 |
|
207 QList<QTreeWidgetItem *> items = treeWidget->findItems(QLatin1String("*"), Qt::MatchWildcard); |
|
208 QMap<QString, QTreeWidgetItem *> itemMap; |
|
209 while (!items.isEmpty()) { |
|
210 QTreeWidgetItem *item = items.takeFirst(); |
|
211 itemMap.insert(item->data(0, Qt::UserRole).toString(), item); |
|
212 } |
|
213 |
|
214 QList<QNetworkConfiguration> allConfigurations = manager.allConfigurations(); |
|
215 |
|
216 while (!allConfigurations.isEmpty()) { |
|
217 QNetworkConfiguration config = allConfigurations.takeFirst(); |
|
218 |
|
219 QTreeWidgetItem *item = itemMap.take(config.identifier()); |
|
220 if (item) { |
|
221 updateItem(item, config); |
|
222 |
|
223 if (config.type() == QNetworkConfiguration::ServiceNetwork) |
|
224 updateSnapConfiguration(item, config); |
|
225 } else { |
|
226 configurationAdded(config); |
|
227 } |
|
228 } |
|
229 |
|
230 foreach (const QString &id, itemMap.keys()) |
|
231 delete itemMap.value(id); |
|
232 } |
|
233 |
|
234 void BearerMonitor::onlineStateChanged(bool isOnline) |
|
235 { |
|
236 if (isOnline) |
|
237 onlineState->setText(tr("Online")); |
|
238 else |
|
239 onlineState->setText(tr("Offline")); |
|
240 } |
|
241 |
|
242 #ifdef Q_OS_WIN |
|
243 void BearerMonitor::registerNetwork() |
|
244 { |
|
245 QTreeWidgetItem *item = treeWidget->currentItem(); |
|
246 |
|
247 QNetworkConfiguration configuration = |
|
248 manager.configurationFromIdentifier(item->data(0, Qt::UserRole).toString()); |
|
249 |
|
250 const QString name = configuration.name(); |
|
251 |
|
252 qDebug() << "Registering" << name << "with system"; |
|
253 |
|
254 WSAQUERYSET networkInfo; |
|
255 memset(&networkInfo, 0, sizeof(networkInfo)); |
|
256 networkInfo.dwSize = sizeof(networkInfo); |
|
257 networkInfo.lpszServiceInstanceName = (LPWSTR)name.utf16(); |
|
258 networkInfo.dwNameSpace = NS_NLA; |
|
259 |
|
260 if (WSASetService(&networkInfo, RNRSERVICE_REGISTER, 0) == SOCKET_ERROR) |
|
261 qDebug() << "WSASetService(RNRSERVICE_REGISTER) returned" << WSAGetLastError(); |
|
262 } |
|
263 |
|
264 void BearerMonitor::unregisterNetwork() |
|
265 { |
|
266 QTreeWidgetItem *item = treeWidget->currentItem(); |
|
267 |
|
268 QNetworkConfiguration configuration = |
|
269 manager.configurationFromIdentifier(item->data(0, Qt::UserRole).toString()); |
|
270 |
|
271 const QString name = configuration.name(); |
|
272 |
|
273 qDebug() << "Unregistering" << name << "with system"; |
|
274 |
|
275 WSAQUERYSET networkInfo; |
|
276 memset(&networkInfo, 0, sizeof(networkInfo)); |
|
277 networkInfo.dwSize = sizeof(networkInfo); |
|
278 networkInfo.lpszServiceInstanceName = (LPWSTR)name.utf16(); |
|
279 networkInfo.dwNameSpace = NS_NLA; |
|
280 |
|
281 if (WSASetService(&networkInfo, RNRSERVICE_DELETE, 0) == SOCKET_ERROR) |
|
282 qDebug() << "WSASetService(RNRSERVICE_DELETE) returned" << WSAGetLastError(); |
|
283 } |
|
284 #endif |
|
285 |
|
286 void BearerMonitor::showConfigurationFor(QTreeWidgetItem *item) |
|
287 { |
|
288 QString identifier; |
|
289 |
|
290 if (item) |
|
291 identifier = item->data(0, Qt::UserRole).toString(); |
|
292 |
|
293 QNetworkConfiguration conf = manager.configurationFromIdentifier(identifier); |
|
294 |
|
295 switch (conf.state()) { |
|
296 case QNetworkConfiguration::Active: |
|
297 configurationState->setText(tr("Active")); |
|
298 break; |
|
299 case QNetworkConfiguration::Discovered: |
|
300 configurationState->setText(tr("Discovered")); |
|
301 break; |
|
302 case QNetworkConfiguration::Defined: |
|
303 configurationState->setText(tr("Defined")); |
|
304 break; |
|
305 case QNetworkConfiguration::Undefined: |
|
306 configurationState->setText(tr("Undefined")); |
|
307 break; |
|
308 default: |
|
309 configurationState->setText(QString()); |
|
310 } |
|
311 |
|
312 switch (conf.type()) { |
|
313 case QNetworkConfiguration::InternetAccessPoint: |
|
314 configurationType->setText(tr("Internet Access Point")); |
|
315 break; |
|
316 case QNetworkConfiguration::ServiceNetwork: |
|
317 configurationType->setText(tr("Service Network")); |
|
318 break; |
|
319 case QNetworkConfiguration::UserChoice: |
|
320 configurationType->setText(tr("User Choice")); |
|
321 break; |
|
322 case QNetworkConfiguration::Invalid: |
|
323 configurationType->setText(tr("Invalid")); |
|
324 break; |
|
325 default: |
|
326 configurationType->setText(QString()); |
|
327 } |
|
328 |
|
329 switch (conf.purpose()) { |
|
330 case QNetworkConfiguration::UnknownPurpose: |
|
331 configurationPurpose->setText(tr("Unknown")); |
|
332 break; |
|
333 case QNetworkConfiguration::PublicPurpose: |
|
334 configurationPurpose->setText(tr("Public")); |
|
335 break; |
|
336 case QNetworkConfiguration::PrivatePurpose: |
|
337 configurationPurpose->setText(tr("Private")); |
|
338 break; |
|
339 case QNetworkConfiguration::ServiceSpecificPurpose: |
|
340 configurationPurpose->setText(tr("Service Specific")); |
|
341 break; |
|
342 default: |
|
343 configurationPurpose->setText(QString()); |
|
344 } |
|
345 |
|
346 configurationIdentifier->setText(conf.identifier()); |
|
347 |
|
348 configurationRoaming->setText(conf.isRoamingAvailable() ? tr("Available") : tr("Not available")); |
|
349 |
|
350 configurationChildren->setText(QString::number(conf.children().count())); |
|
351 |
|
352 configurationName->setText(conf.name()); |
|
353 } |
|
354 |
|
355 void BearerMonitor::createSessionFor(QTreeWidgetItem *item) |
|
356 { |
|
357 const QString identifier = item->data(0, Qt::UserRole).toString(); |
|
358 |
|
359 QNetworkConfiguration conf = manager.configurationFromIdentifier(identifier); |
|
360 |
|
361 SessionWidget *session = new SessionWidget(conf); |
|
362 |
|
363 tabWidget->addTab(session, conf.name()); |
|
364 |
|
365 sessionGroup->show(); |
|
366 |
|
367 sessionWidgets.append(session); |
|
368 } |
|
369 |
|
370 void BearerMonitor::createNewSession() |
|
371 { |
|
372 QTreeWidgetItem *item = treeWidget->currentItem(); |
|
373 |
|
374 createSessionFor(item); |
|
375 } |
|
376 |
|
377 void BearerMonitor::deleteSession() |
|
378 { |
|
379 SessionWidget *session = qobject_cast<SessionWidget *>(tabWidget->currentWidget()); |
|
380 if (session) { |
|
381 sessionWidgets.removeAll(session); |
|
382 |
|
383 delete session; |
|
384 |
|
385 if (tabWidget->count() == 0) |
|
386 sessionGroup->hide(); |
|
387 } |
|
388 } |
|
389 |
|
390 void BearerMonitor::performScan() |
|
391 { |
|
392 scanButton->hide(); |
|
393 progressBar->show(); |
|
394 manager.updateConfigurations(); |
|
395 } |