1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QList> |
|
19 #include <QString> |
|
20 #include "devicedelegate.h" |
|
21 #include "utilities.h" |
|
22 |
|
23 //! DeviceDelegate default constructor. |
|
24 DeviceDelegate::DeviceDelegate() |
|
25 : m_batteryCharging(false), m_updating(false) |
|
26 { |
|
27 // this will be the name javascript uses to access properties and signals |
|
28 // from this class |
|
29 setObjectName("deviceDelegate"); |
|
30 |
|
31 #ifdef QT_MOBILITY_BEARER_SYSINFO |
|
32 // create Qt Mobility API objects for device info |
|
33 m_deviceInfo = new QSystemDeviceInfo(this); |
|
34 m_networkInfo = new QSystemNetworkInfo(this); |
|
35 m_networkConfigManager = new QNetworkConfigurationManager(this); |
|
36 |
|
37 // Guess the network mode currently used for browser - |
|
38 // a guess is really the best we can do right now. |
|
39 m_currentMode = getInternetConfigurationMode(); |
|
40 |
|
41 // need the configuration manager to update its configuration list, when it |
|
42 // is done it will send us a signal |
|
43 safe_connect(m_networkConfigManager, SIGNAL(updateCompleted()), |
|
44 this, SLOT(handleUpdateComplete())); |
|
45 |
|
46 // tell configuration manager to update its configuration list |
|
47 m_updating = true; |
|
48 m_networkConfigManager->updateConfigurations(); |
|
49 |
|
50 // set up handlers for other network configuration manager signals |
|
51 safe_connect(m_networkConfigManager, SIGNAL(configurationAdded(const QNetworkConfiguration&)), |
|
52 this, SLOT(configurationAdded(const QNetworkConfiguration&))); |
|
53 safe_connect(m_networkConfigManager, SIGNAL(configurationRemoved(const QNetworkConfiguration&)), |
|
54 this, SLOT(configurationRemoved(const QNetworkConfiguration&))); |
|
55 safe_connect(m_networkConfigManager, SIGNAL(configurationChanged(const QNetworkConfiguration&)), |
|
56 this, SLOT(configurationChanged(const QNetworkConfiguration))); |
|
57 |
|
58 // When the m_deviceInfo signals battery level changed, |
|
59 // DeviceDelegate emits batteryLevelChanged |
|
60 safe_connect(m_deviceInfo, SIGNAL(batteryLevelChanged(int)), |
|
61 this, SIGNAL(batteryLevelChanged(int))); |
|
62 |
|
63 // set current charging state then keep it up to date with signal handler |
|
64 m_batteryCharging = (m_deviceInfo->currentPowerState() == |
|
65 QSystemDeviceInfo::WallPowerChargingBattery) ? true : false; |
|
66 //qDebug() << "DeviceDelegate: charging state = " << m_deviceInfo->currentPowerState(); |
|
67 //qDebug() << "DeviceDelegate: charging = " << m_batteryCharging; |
|
68 safe_connect(m_deviceInfo, SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState)), |
|
69 this, SLOT(handlePowerStateChanged(QSystemDeviceInfo::PowerState))); |
|
70 |
|
71 // set up handlers for system network info signals |
|
72 safe_connect(m_networkInfo, SIGNAL(networkSignalStrengthChanged( |
|
73 QSystemNetworkInfo::NetworkMode, int)), this, |
|
74 SLOT(handleNetworkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode, int))); |
|
75 |
|
76 safe_connect(m_networkInfo, SIGNAL(networkNameChanged( |
|
77 QSystemNetworkInfo::NetworkMode, const QString&)), this, |
|
78 SLOT(handleNetworkNameChanged(QSystemNetworkInfo::NetworkMode, const QString&))); |
|
79 #endif // QT_MOBILITY_BEARER_SYSINFO |
|
80 } |
|
81 |
|
82 //! DeviceDelegate destructor. |
|
83 DeviceDelegate::~DeviceDelegate() |
|
84 { |
|
85 // clean up |
|
86 #ifdef QT_MOBILITY_BEARER_SYSINFO |
|
87 delete m_deviceInfo; |
|
88 delete m_networkInfo; |
|
89 delete m_networkConfigManager; |
|
90 #endif |
|
91 } |
|
92 |
|
93 #ifdef QT_MOBILITY_BEARER_SYSINFO |
|
94 //! Converts bearer name string to network mode enumerator. |
|
95 //! The network configuration uses strings to represent the same info that |
|
96 //! the system network info uses an enumeration to represent |
|
97 /*! |
|
98 \param name bearer name to convert |
|
99 */ |
|
100 QSystemNetworkInfo::NetworkMode DeviceDelegate::bearerNameToMode(QString name) const |
|
101 { |
|
102 QSystemNetworkInfo::NetworkMode mode = QSystemNetworkInfo::UnknownMode; |
|
103 |
|
104 if (name == "Unknown") |
|
105 mode = QSystemNetworkInfo::UnknownMode; |
|
106 else if (name == "Ethernet") |
|
107 mode = QSystemNetworkInfo::EthernetMode; |
|
108 else if (name == "WLAN") |
|
109 mode = QSystemNetworkInfo::WlanMode; |
|
110 else if (name == "2G") { |
|
111 // there currently isn't a 2G in the enumeration but by trial and |
|
112 // error I found that it corresponds to either GSM or WCDMA mode |
|
113 // You can tell which mode to choose be getting the signal strength; |
|
114 // if you have the wrong mode it returns -1. |
|
115 if (m_networkInfo->networkSignalStrength(QSystemNetworkInfo::GsmMode) >= 0) |
|
116 mode = QSystemNetworkInfo::GsmMode; // T-Mobile uses this mode |
|
117 else |
|
118 mode = QSystemNetworkInfo::WcdmaMode; // AT&T uses this mode |
|
119 //qDebug() << "DeviceDelegate: 2G"; |
|
120 } else if (name == "CDMA2000") |
|
121 mode = QSystemNetworkInfo::CdmaMode; |
|
122 else if (name == "WCDMA") |
|
123 mode = QSystemNetworkInfo::WcdmaMode; |
|
124 else if (name == "HSPA") |
|
125 // HSPA isn't currently in the enumeration |
|
126 mode = QSystemNetworkInfo::UnknownMode; |
|
127 else if (name == "Bluetooth") |
|
128 mode = QSystemNetworkInfo::BluetoothMode; |
|
129 else if (name == "WiMAX") |
|
130 mode = QSystemNetworkInfo::WimaxMode; |
|
131 |
|
132 //qDebug() << "DeviceDelegate: Mode " << mode; |
|
133 |
|
134 return (mode); |
|
135 } |
|
136 #endif // QT_MOBILITY_BEARER_SYSINFO |
|
137 |
|
138 //! Gets the current battery level. |
|
139 int DeviceDelegate::getBatteryLevel() const |
|
140 { |
|
141 #ifdef QT_MOBILITY_BEARER_SYSINFO |
|
142 return (m_deviceInfo->batteryLevel()); |
|
143 #else |
|
144 return (100); // can't get real level, return full |
|
145 #endif |
|
146 } |
|
147 |
|
148 //! Returns true if the battery is charging. |
|
149 bool DeviceDelegate::isBatteryCharging() const |
|
150 { |
|
151 return (m_batteryCharging); |
|
152 } |
|
153 |
|
154 //! Gets the network signal strength for the current network mode. |
|
155 int DeviceDelegate::getNetworkSignalStrength() const |
|
156 { |
|
157 #ifdef QT_MOBILITY_BEARER_SYSINFO |
|
158 int strength = m_networkInfo->networkSignalStrength(m_currentMode); |
|
159 |
|
160 // Strength in WLAN mode is reported as -1 by QtMobility |
|
161 if ((strength == -1) && (m_currentMode == QSystemNetworkInfo::WlanMode)) { |
|
162 strength = 100; |
|
163 } |
|
164 |
|
165 return (strength); |
|
166 #else |
|
167 return (100); // can't get real level, return full |
|
168 #endif |
|
169 } |
|
170 |
|
171 //! Gets the network name for the current network mode. |
|
172 QString DeviceDelegate::getNetworkName() const |
|
173 { |
|
174 #ifdef QT_MOBILITY_BEARER_SYSINFO |
|
175 QString netName = m_networkInfo->networkName(m_currentMode); |
|
176 |
|
177 // if WLAN SSID name is unknown show "WiFi" |
|
178 if ((m_currentMode == QSystemNetworkInfo::WlanMode) && |
|
179 (netName == "")) { |
|
180 netName = "WiFi"; |
|
181 } |
|
182 |
|
183 //qDebug() << "DeviceDelegate: network name " << netName; |
|
184 return (netName); |
|
185 #else |
|
186 return (""); // can't get real name |
|
187 #endif |
|
188 } |
|
189 |
|
190 #ifdef QT_MOBILITY_BEARER_SYSINFO |
|
191 //! Emits a signal for the specified signal strength. |
|
192 /*! |
|
193 \param strength new signal strength |
|
194 */ |
|
195 void DeviceDelegate::updateSignalStrength(int strength) |
|
196 { |
|
197 //qDebug() << "DeviceDelegate: Signal Strength " << strength; |
|
198 // currently getting a -1 signal strength for WLAN, change to 100. |
|
199 if (strength == -1) { |
|
200 if (m_currentMode == QSystemNetworkInfo::WlanMode) { |
|
201 //qDebug() << "DeviceDelegate: In WLAN mode so use strength 100."; |
|
202 strength = 100; |
|
203 } //else if (m_currentMode != QSystemNetworkInfo::UnknownMode) { |
|
204 // qDebug() << "DeviceDelegate: Neg str in mode " << m_currentMode; |
|
205 //} |
|
206 } |
|
207 |
|
208 emit networkSignalStrengthChanged(strength); |
|
209 } |
|
210 |
|
211 |
|
212 //! Search for an active internet access point and return the network mode. |
|
213 //! Returns the network mode used by the browser (best guess anyway) |
|
214 //! My best guess is that the first active internet access point is the one |
|
215 //! used by the browser. |
|
216 QSystemNetworkInfo::NetworkMode DeviceDelegate::getInternetConfigurationMode() |
|
217 { |
|
218 // function return value |
|
219 QSystemNetworkInfo::NetworkMode mode = QSystemNetworkInfo::UnknownMode; |
|
220 QString bearerName; // network configuration bearer name |
|
221 bool found = false; // flag set when configuration found |
|
222 // active network configurations |
|
223 QList<QNetworkConfiguration> activeConfigurations = |
|
224 m_networkConfigManager->allConfigurations(QNetworkConfiguration::Active); |
|
225 |
|
226 // check each active configuration until we find one that fits the bill |
|
227 while (!found && !activeConfigurations.isEmpty()) { |
|
228 QNetworkConfiguration config = activeConfigurations.takeFirst(); |
|
229 |
|
230 //qDebug() << "DeviceDelegate: Have Active Config - type " << config.type(); |
|
231 // service networks contain children configurations which need to be explored |
|
232 if (config.type() == QNetworkConfiguration::ServiceNetwork) { |
|
233 //qDebug() << "DeviceDelegate: Found a ServiceNetwork!"; |
|
234 foreach (const QNetworkConfiguration &child, config.children()) { |
|
235 if ((child.type() == QNetworkConfiguration::InternetAccessPoint) && |
|
236 (child.state() == QNetworkConfiguration::Active)) { |
|
237 found = true; |
|
238 m_currentConfigIdentifier = child.identifier(); |
|
239 bearerName = child.bearerName(); |
|
240 //qDebug() << " Found InternetAccessPoint - " << bearerName; |
|
241 //qDebug() << " identifier: " << child.identifier(); |
|
242 break; // exit foreach loop |
|
243 } |
|
244 } |
|
245 } else if (config.type() == QNetworkConfiguration::InternetAccessPoint) { |
|
246 found = true; |
|
247 m_currentConfigIdentifier = config.identifier(); |
|
248 bearerName = config.bearerName(); |
|
249 //qDebug() << "DeviceDelegate: Found an InternetAccessPoint - " << bearerName; |
|
250 //qDebug() << " identifier: " << config.identifier(); |
|
251 } |
|
252 } |
|
253 |
|
254 // get the mode of the found network configuration |
|
255 if (found) { |
|
256 //qDebug() << "DeviceDelegate::getInternetConfigurationMode: use bearer " << bearerName; |
|
257 mode = bearerNameToMode(bearerName); |
|
258 } else { |
|
259 //qDebug() << "DeviceDelegate: Failed to find an active internet access point."; |
|
260 m_currentConfigIdentifier = ""; |
|
261 } |
|
262 |
|
263 return (mode); |
|
264 } |
|
265 |
|
266 //! Handles the updateCompleted signal from the configuration manager. |
|
267 void DeviceDelegate::handleUpdateComplete() |
|
268 { |
|
269 // search for appropriate network configuration mode again |
|
270 QSystemNetworkInfo::NetworkMode mode = getInternetConfigurationMode(); |
|
271 |
|
272 // if the mode has changed we need to update the signal strength and network name |
|
273 if (mode != m_currentMode) { |
|
274 m_currentMode = mode; |
|
275 updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode)); |
|
276 emit networkNameChanged(getNetworkName()); |
|
277 } |
|
278 |
|
279 m_updating = false; |
|
280 } |
|
281 |
|
282 //! Handles the configurationAdded signal from the configuration manager. |
|
283 /*! |
|
284 \param config added network configuration |
|
285 */ |
|
286 void DeviceDelegate::configurationAdded(const QNetworkConfiguration &config) |
|
287 { |
|
288 //qDebug() << "DeviceDelegate: Configuration Added."; |
|
289 if (!m_updating && (m_currentMode == QSystemNetworkInfo::UnknownMode) && |
|
290 (config.state() == QNetworkConfiguration::Active) && |
|
291 (config.type() == QNetworkConfiguration::InternetAccessPoint)) { |
|
292 // use this new configuration's mode, update the signal strength and network name |
|
293 //qDebug() << "DeviceDelegate: Use new configuration with bearer " << config.bearerName(); |
|
294 m_currentConfigIdentifier = config.identifier(); |
|
295 m_currentMode = bearerNameToMode(config.bearerName()); |
|
296 updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode)); |
|
297 emit networkNameChanged(getNetworkName()); |
|
298 } |
|
299 } |
|
300 |
|
301 //! Handles the configurationRemoved signal from the configuration manager. |
|
302 /*! |
|
303 \param config removed network configuration |
|
304 */ |
|
305 void DeviceDelegate::configurationRemoved(const QNetworkConfiguration &config) |
|
306 { |
|
307 //qDebug() << "DeviceDelegate: Configuration Removed."; |
|
308 if ((!m_updating) && (config.identifier() == m_currentConfigIdentifier)) { |
|
309 //qDebug() << "DeviceDelegate: Find new Configuration."; |
|
310 // search for appropriate network configuration mode again |
|
311 m_currentMode = getInternetConfigurationMode(); |
|
312 updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode)); |
|
313 emit networkNameChanged(getNetworkName()); |
|
314 } |
|
315 } |
|
316 |
|
317 //! Handles the configurationChanged signal from the configuration manager. |
|
318 /*! |
|
319 \param config changed network configuration |
|
320 */ |
|
321 void DeviceDelegate::configurationChanged(const QNetworkConfiguration &config) |
|
322 { |
|
323 //qDebug() << "DeviceDelegate: Configuration Changed."; |
|
324 if (!m_updating) { |
|
325 // if network mode currently unknown maybe this change will allow us to |
|
326 // identify the netork mode |
|
327 if (m_currentMode == QSystemNetworkInfo::UnknownMode) { |
|
328 // can we now identify the network mode? |
|
329 if ((config.state() == QNetworkConfiguration::Active) && |
|
330 (config.type() == QNetworkConfiguration::InternetAccessPoint)) { |
|
331 //qDebug() << "DeviceDelegate: Use new configuration with bearer " << config.bearerName(); |
|
332 m_currentConfigIdentifier = config.identifier(); |
|
333 m_currentMode = bearerNameToMode(config.bearerName()); |
|
334 updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode)); |
|
335 emit networkNameChanged(getNetworkName()); |
|
336 } |
|
337 // Did the configuration currently being used change in a notable way? |
|
338 } else if ((config.identifier() == m_currentConfigIdentifier) && |
|
339 ((config.state() != QNetworkConfiguration::Active) || |
|
340 (config.type() != QNetworkConfiguration::InternetAccessPoint))) { |
|
341 //qDebug() << "DeviceDelegate: Change configuration."; |
|
342 // search for appropriate network configuration mode again |
|
343 m_currentMode = getInternetConfigurationMode(); |
|
344 updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode)); |
|
345 emit networkNameChanged(getNetworkName()); |
|
346 } |
|
347 } |
|
348 } |
|
349 |
|
350 //! Handles the networkSignalStrengthChanged signal from system network info. |
|
351 /*! |
|
352 \param mode network mode of connection that changed |
|
353 \param strength new signal strength |
|
354 */ |
|
355 void DeviceDelegate::handleNetworkSignalStrengthChanged( |
|
356 QSystemNetworkInfo::NetworkMode mode, int strength) |
|
357 { |
|
358 // Only send signal strength changes for current mode. |
|
359 if (mode == m_currentMode) |
|
360 updateSignalStrength(strength); |
|
361 } |
|
362 |
|
363 //! Handles the networkNameChanged signal from system network info. |
|
364 /*! |
|
365 \param mode network mode of connection that changed |
|
366 \param name new network name |
|
367 */ |
|
368 void DeviceDelegate::handleNetworkNameChanged( |
|
369 QSystemNetworkInfo::NetworkMode mode, const QString& name) |
|
370 { |
|
371 // Only send network name changes for current mode. |
|
372 if (mode == m_currentMode) |
|
373 emit networkNameChanged(name); |
|
374 } |
|
375 |
|
376 //! Handles the powerStateChanged signal from system device info. |
|
377 /*! |
|
378 \param state new power state |
|
379 */ |
|
380 void DeviceDelegate::handlePowerStateChanged(QSystemDeviceInfo::PowerState state) |
|
381 { |
|
382 bool batteryCharging = |
|
383 (state == QSystemDeviceInfo::WallPowerChargingBattery) ? true : false; |
|
384 |
|
385 //qDebug() << "DeviceDelegate: new charging state = " << state; |
|
386 if (batteryCharging != m_batteryCharging) { |
|
387 m_batteryCharging = batteryCharging; |
|
388 //qDebug() << "DeviceDelegate: new charging = " << m_batteryCharging; |
|
389 // emit battery level - subscriber will get charging state if desired |
|
390 emit batteryLevelChanged(m_deviceInfo->batteryLevel()); |
|
391 } |
|
392 } |
|
393 |
|
394 #endif // QT_MOBILITY_BEARER_SYSINFO |
|
395 |
|
396 |
|