259 return configurationInterface.contains(id.toUInt()); |
269 return configurationInterface.contains(id.toUInt()); |
260 } |
270 } |
261 |
271 |
262 void QCoreWlanEngine::connectToId(const QString &id) |
272 void QCoreWlanEngine::connectToId(const QString &id) |
263 { |
273 { |
264 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
|
265 QString interfaceString = getInterfaceFromId(id); |
274 QString interfaceString = getInterfaceFromId(id); |
266 |
275 |
267 if(networkInterfaces.value(interfaceString) == "WLAN") { |
276 if(networkInterfaces.value(interfaceString) == "WLAN") { |
268 #if defined(MAC_SDK_10_6) |
277 #if defined(MAC_SDK_10_6) |
|
278 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
269 CWInterface *wifiInterface = [CWInterface interfaceWithName: qstringToNSString(interfaceString)]; |
279 CWInterface *wifiInterface = [CWInterface interfaceWithName: qstringToNSString(interfaceString)]; |
270 CWConfiguration *userConfig = [ wifiInterface configuration]; |
280 |
271 |
281 if([wifiInterface power]) { |
272 NSSet *remNets = [userConfig rememberedNetworks]; //CWWirelessProfile |
282 NSError *err = nil; |
273 |
283 NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0]; |
274 NSEnumerator *enumerator = [remNets objectEnumerator]; |
284 |
275 CWWirelessProfile *wProfile; |
285 NSString *wantedSsid = 0; |
276 NSUInteger index=0; |
286 bool okToProceed = true; |
277 |
287 |
278 NSDictionary *parametersDict; |
288 if(getNetworkNameFromSsid(id) != id) { |
279 NSArray* apArray; |
289 NSArray *array = [CW8021XProfile allUser8021XProfiles]; |
280 |
290 for (NSUInteger i=0; i<[array count]; ++i) { |
281 CW8021XProfile *user8021XProfile; |
291 if(id == nsstringToQString([[array objectAtIndex:i] userDefinedName]) |
282 NSError *err; |
292 || id == nsstringToQString([[array objectAtIndex:i] ssid]) ) { |
283 NSMutableDictionary *params; |
293 QString thisName = getSsidFromNetworkName(id); |
284 |
294 if(thisName.isEmpty()) { |
285 while ((wProfile = [enumerator nextObject])) { //CWWirelessProfile |
295 wantedSsid = qstringToNSString(id); |
286 |
296 } else { |
287 if(id == nsstringToQString([wProfile ssid])) { |
297 wantedSsid = qstringToNSString(thisName); |
288 user8021XProfile = nil; |
298 } |
289 user8021XProfile = [ wProfile user8021XProfile]; |
299 okToProceed = false; |
290 |
300 [params setValue: [array objectAtIndex:i] forKey:kCWAssocKey8021XProfile]; |
291 err = nil; |
301 break; |
292 params = [NSMutableDictionary dictionaryWithCapacity:0]; |
302 } |
293 |
|
294 if(user8021XProfile) { |
|
295 [params setValue: user8021XProfile forKey:kCWAssocKey8021XProfile]; |
|
296 } else { |
|
297 [params setValue: [wProfile passphrase] forKey: kCWAssocKeyPassphrase]; |
|
298 } |
303 } |
299 |
304 } |
300 parametersDict = nil; |
305 |
301 apArray = [NSMutableArray arrayWithArray:[wifiInterface scanForNetworksWithParameters:parametersDict error:&err]]; |
306 if(okToProceed) { |
302 |
307 NSUInteger index = 0; |
303 if(!err) { |
308 |
304 |
309 CWConfiguration *userConfig = [ wifiInterface configuration]; |
305 for(uint row=0; row < [apArray count]; row++ ) { |
310 NSSet *remNets = [userConfig rememberedNetworks]; |
306 CWNetwork *apNetwork = [apArray objectAtIndex:row]; |
311 NSEnumerator *enumerator = [remNets objectEnumerator]; |
307 if([[apNetwork ssid] compare:[wProfile ssid]] == NSOrderedSame) { |
312 CWWirelessProfile *wProfile; |
308 |
313 |
309 bool result = [wifiInterface associateToNetwork: apNetwork parameters:[NSDictionary dictionaryWithDictionary:params] error:&err]; |
314 while ((wProfile = [enumerator nextObject])) { |
310 |
315 if(id == nsstringToQString([wProfile ssid])) { |
311 if(!result) { |
316 |
312 emit connectionError(id, ConnectError); |
317 wantedSsid = [wProfile ssid]; |
313 } else { |
318 [params setValue: [wProfile passphrase] forKey: kCWAssocKeyPassphrase]; |
314 [autoreleasepool release]; |
319 break; |
315 return; |
320 } |
316 } |
321 index++; |
|
322 } |
|
323 } |
|
324 |
|
325 NSDictionary *parametersDict = nil; |
|
326 NSArray *apArray = [NSMutableArray arrayWithArray:[wifiInterface scanForNetworksWithParameters:parametersDict error:&err]]; |
|
327 |
|
328 if(!err) { |
|
329 for(uint row=0; row < [apArray count]; row++ ) { |
|
330 CWNetwork *apNetwork = [apArray objectAtIndex:row]; |
|
331 if([[apNetwork ssid] compare:wantedSsid] == NSOrderedSame) { |
|
332 bool result = [wifiInterface associateToNetwork: apNetwork parameters:[NSDictionary dictionaryWithDictionary:params] error:&err]; |
|
333 |
|
334 if(!result) { |
|
335 emit connectionError(id, ConnectError); |
|
336 } else { |
|
337 [autoreleasepool release]; |
|
338 return; |
317 } |
339 } |
318 } |
340 } |
319 } |
341 } |
320 } |
342 } else { |
321 index++; |
343 qDebug() <<"ERROR"<< nsstringToQString([err localizedDescription ]); |
322 } |
344 } |
323 |
345 |
324 emit connectionError(id, InterfaceLookupError); |
346 emit connectionError(id, InterfaceLookupError); |
325 #endif |
347 [autoreleasepool release]; |
326 } else { |
348 |
327 // not wifi |
349 } else { |
|
350 // not wifi |
|
351 } |
|
352 #endif |
328 } |
353 } |
329 emit connectionError(id, OperationNotSupported); |
354 emit connectionError(id, OperationNotSupported); |
330 [autoreleasepool release]; |
|
331 } |
355 } |
332 |
356 |
333 void QCoreWlanEngine::disconnectFromId(const QString &id) |
357 void QCoreWlanEngine::disconnectFromId(const QString &id) |
334 { |
358 { |
335 QString interfaceString = getInterfaceFromId(id); |
359 QString interfaceString = getInterfaceFromId(id); |
351 } |
375 } |
352 |
376 |
353 void QCoreWlanEngine::requestUpdate() |
377 void QCoreWlanEngine::requestUpdate() |
354 { |
378 { |
355 getAllScInterfaces(); |
379 getAllScInterfaces(); |
|
380 getUserConfigurations(); |
356 emit configurationsChanged(); |
381 emit configurationsChanged(); |
357 } |
382 } |
358 |
383 |
359 QCoreWlanEngine *QCoreWlanEngine::instance() |
384 QCoreWlanEngine *QCoreWlanEngine::instance() |
360 { |
385 { |
361 return coreWlanEngine(); |
386 return coreWlanEngine(); |
362 } |
387 } |
363 |
388 |
|
389 QString QCoreWlanEngine::getSsidFromNetworkName(const QString &name) |
|
390 { |
|
391 QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
|
392 while (i.hasNext()) { |
|
393 i.next(); |
|
394 QMap<QString,QString> map = i.value(); |
|
395 QMapIterator<QString, QString> ij(i.value()); |
|
396 while (ij.hasNext()) { |
|
397 ij.next(); |
|
398 if(name == i.key()) { |
|
399 return ij.key(); |
|
400 } |
|
401 } |
|
402 } |
|
403 return QString(); |
|
404 } |
|
405 |
|
406 QString QCoreWlanEngine::getNetworkNameFromSsid(const QString &ssid) |
|
407 { |
|
408 QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
|
409 while (i.hasNext()) { |
|
410 i.next(); |
|
411 QMap<QString,QString> map = i.value(); |
|
412 QMapIterator<QString, QString> ij(i.value()); |
|
413 while (ij.hasNext()) { |
|
414 ij.next(); |
|
415 if(ij.key() == ssid) { |
|
416 return i.key(); |
|
417 } |
|
418 |
|
419 } |
|
420 return map.key(ssid); |
|
421 } |
|
422 return QString(); |
|
423 } |
|
424 |
364 QList<QNetworkConfigurationPrivate *> QCoreWlanEngine::scanForSsids(const QString &interfaceName) |
425 QList<QNetworkConfigurationPrivate *> QCoreWlanEngine::scanForSsids(const QString &interfaceName) |
365 { |
426 { |
366 QList<QNetworkConfigurationPrivate *> foundConfigs; |
427 QList<QNetworkConfigurationPrivate *> foundConfigs; |
|
428 if(!hasWifi) { |
|
429 return foundConfigs; |
|
430 } |
367 #if defined(MAC_SDK_10_6) |
431 #if defined(MAC_SDK_10_6) |
368 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
432 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
369 |
|
370 CWInterface *currentInterface = [CWInterface interfaceWithName:qstringToNSString(interfaceName)]; |
433 CWInterface *currentInterface = [CWInterface interfaceWithName:qstringToNSString(interfaceName)]; |
|
434 QStringList addedConfigs; |
|
435 |
371 if([currentInterface power]) { |
436 if([currentInterface power]) { |
372 NSError *err = nil; |
437 NSError *err = nil; |
373 NSDictionary *parametersDict = nil; |
438 NSDictionary *parametersDict = nil; |
374 NSArray* apArray = [currentInterface scanForNetworksWithParameters:parametersDict error:&err]; |
439 NSArray* apArray = [currentInterface scanForNetworksWithParameters:parametersDict error:&err]; |
375 |
440 |
376 CWNetwork *apNetwork; |
441 CWNetwork *apNetwork; |
377 if(!err) { |
442 if(!err) { |
378 for(uint row=0; row < [apArray count]; row++ ) { |
443 for(uint row=0; row < [apArray count]; row++ ) { |
379 NSAutoreleasePool *looppool = [[NSAutoreleasePool alloc] init]; |
|
380 |
|
381 apNetwork = [apArray objectAtIndex:row]; |
444 apNetwork = [apArray objectAtIndex:row]; |
|
445 |
|
446 QString networkSsid = nsstringToQString([apNetwork ssid]); |
|
447 |
382 QNetworkConfigurationPrivate* cpPriv = new QNetworkConfigurationPrivate(); |
448 QNetworkConfigurationPrivate* cpPriv = new QNetworkConfigurationPrivate(); |
383 QString networkSsid = nsstringToQString([apNetwork ssid]); |
|
384 cpPriv->name = networkSsid; |
449 cpPriv->name = networkSsid; |
385 cpPriv->isValid = true; |
450 cpPriv->isValid = true; |
386 cpPriv->id = networkSsid; |
451 cpPriv->id = networkSsid; |
387 cpPriv->internet = true; |
452 cpPriv->internet = true; |
388 cpPriv->bearer = QLatin1String("WLAN"); |
453 cpPriv->bearer = QLatin1String("WLAN"); |
389 cpPriv->type = QNetworkConfiguration::InternetAccessPoint; |
454 cpPriv->type = QNetworkConfiguration::InternetAccessPoint; |
390 cpPriv->serviceInterface = QNetworkInterface::interfaceFromName(interfaceName); |
455 cpPriv->serviceInterface = QNetworkInterface::interfaceFromName(interfaceName); |
391 |
456 bool known = isKnownSsid(networkSsid); |
392 if( [currentInterface.interfaceState intValue] == kCWInterfaceStateRunning) { |
457 if( [currentInterface.interfaceState intValue] == kCWInterfaceStateRunning) { |
393 QString interfaceSsidString = nsstringToQString( [currentInterface ssid]); |
458 if( cpPriv->name == nsstringToQString( [currentInterface ssid])) { |
394 if( cpPriv->name == interfaceSsidString) { |
|
395 cpPriv->state |= QNetworkConfiguration::Active; |
459 cpPriv->state |= QNetworkConfiguration::Active; |
396 } |
460 } |
397 } |
461 } |
|
462 |
398 if(!cpPriv->state) { |
463 if(!cpPriv->state) { |
399 if(isKnownSsid(cpPriv->serviceInterface.name(), networkSsid)) { |
464 if(known) { |
400 cpPriv->state = QNetworkConfiguration::Discovered; |
465 cpPriv->state = QNetworkConfiguration::Discovered; |
401 } else { |
466 } else { |
402 cpPriv->state = QNetworkConfiguration::Defined; |
467 cpPriv->state = QNetworkConfiguration::Undefined; |
403 } |
468 } |
404 } |
|
405 if(!cpPriv->state) { |
|
406 cpPriv->state = QNetworkConfiguration::Undefined; |
|
407 } |
469 } |
408 if([[apNetwork securityMode ] intValue]== kCWSecurityModeOpen) |
470 if([[apNetwork securityMode ] intValue]== kCWSecurityModeOpen) |
409 cpPriv->purpose = QNetworkConfiguration::PublicPurpose; |
471 cpPriv->purpose = QNetworkConfiguration::PublicPurpose; |
410 else |
472 else |
411 cpPriv->purpose = QNetworkConfiguration::PrivatePurpose; |
473 cpPriv->purpose = QNetworkConfiguration::PrivatePurpose; |
|
474 |
412 foundConfigs.append(cpPriv); |
475 foundConfigs.append(cpPriv); |
413 [looppool release]; |
476 addedConfigs << networkSsid; |
414 } |
477 |
415 } |
478 } //end scanned row |
416 } |
479 } |
|
480 } //end power |
|
481 |
|
482 |
|
483 // add known configurations that are not around. |
|
484 QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
|
485 while (i.hasNext()) { |
|
486 i.next(); |
|
487 QString networkName = i.key(); |
|
488 |
|
489 if(!addedConfigs.contains(networkName)) { |
|
490 QString interfaceName; |
|
491 QMapIterator<QString, QString> ij(i.value()); |
|
492 while (ij.hasNext()) { |
|
493 ij.next(); |
|
494 interfaceName = ij.value(); |
|
495 } |
|
496 |
|
497 QNetworkConfigurationPrivate* cpPriv = new QNetworkConfigurationPrivate(); |
|
498 cpPriv->name = networkName; |
|
499 cpPriv->isValid = true; |
|
500 cpPriv->id = networkName; |
|
501 cpPriv->internet = true; |
|
502 cpPriv->bearer = QLatin1String("WLAN"); |
|
503 cpPriv->type = QNetworkConfiguration::InternetAccessPoint; |
|
504 cpPriv->serviceInterface = QNetworkInterface::interfaceFromName(interfaceName); |
|
505 QString ssid = getSsidFromNetworkName(networkName); |
|
506 if( [currentInterface.interfaceState intValue] == kCWInterfaceStateRunning) { |
|
507 if( ssid == nsstringToQString( [currentInterface ssid])) { |
|
508 cpPriv->state |= QNetworkConfiguration::Active; |
|
509 } |
|
510 } |
|
511 |
|
512 if( addedConfigs.contains(ssid)) { |
|
513 cpPriv->state |= QNetworkConfiguration::Discovered; |
|
514 } |
|
515 |
|
516 if(!cpPriv->state) { |
|
517 cpPriv->state = QNetworkConfiguration::Defined; |
|
518 } |
|
519 |
|
520 foundConfigs.append(cpPriv); |
|
521 } |
|
522 } |
|
523 |
417 [autoreleasepool drain]; |
524 [autoreleasepool drain]; |
418 #else |
525 #else |
419 Q_UNUSED(interfaceName); |
526 Q_UNUSED(interfaceName); |
420 #endif |
527 #endif |
421 return foundConfigs; |
528 return foundConfigs; |
422 } |
529 } |
423 |
530 |
424 bool QCoreWlanEngine::isWifiReady(const QString &wifiDeviceName) |
531 bool QCoreWlanEngine::isWifiReady(const QString &wifiDeviceName) |
425 { |
532 { |
426 #if defined(MAC_SDK_10_6) |
533 #if defined(MAC_SDK_10_6) |
427 CWInterface *defaultInterface = [CWInterface interfaceWithName: qstringToNSString(wifiDeviceName)]; |
534 if([[CWInterface supportedInterfaces] count] > 0 ) { |
428 if([defaultInterface power]) |
535 CWInterface *defaultInterface = [CWInterface interfaceWithName: qstringToNSString(wifiDeviceName)]; |
429 return true; |
536 if([defaultInterface power]) |
|
537 return true; |
|
538 } |
430 #else |
539 #else |
431 Q_UNUSED(wifiDeviceName); |
540 Q_UNUSED(wifiDeviceName); |
432 #endif |
541 #endif |
433 return false; |
542 return false; |
434 } |
543 } |
435 |
544 |
436 bool QCoreWlanEngine::isKnownSsid(const QString &interfaceName, const QString &ssid) |
545 bool QCoreWlanEngine::isKnownSsid( const QString &ssid) |
437 { |
546 { |
438 #if defined(MAC_SDK_10_6) |
547 #if defined(MAC_SDK_10_6) |
439 CWInterface *wifiInterface = [CWInterface interfaceWithName: qstringToNSString(interfaceName)]; |
548 QMapIterator<QString, QMap<QString,QString> > i(userProfiles); |
440 CWConfiguration *userConfig = [wifiInterface configuration]; |
549 while (i.hasNext()) { |
441 NSSet *remNets = [userConfig rememberedNetworks]; |
550 i.next(); |
442 for (CWWirelessProfile *wProfile in remNets) { |
551 QMap<QString,QString> map = i.value(); |
443 if(ssid == nsstringToQString([wProfile ssid])) |
552 if(map.keys().contains(ssid)) { |
444 return true; |
553 return true; |
|
554 } |
445 } |
555 } |
446 #else |
556 #else |
447 Q_UNUSED(interfaceName); |
|
448 Q_UNUSED(ssid); |
557 Q_UNUSED(ssid); |
449 #endif |
558 #endif |
450 return false; |
559 return false; |
451 } |
560 } |
452 |
561 |
542 |
651 |
543 CFRunLoopAddSource(CFRunLoopGetCurrent(), runloopSource, kCFRunLoopDefaultMode); |
652 CFRunLoopAddSource(CFRunLoopGetCurrent(), runloopSource, kCFRunLoopDefaultMode); |
544 return; |
653 return; |
545 } |
654 } |
546 |
655 |
|
656 void QCoreWlanEngine::getUserConfigurations() |
|
657 { |
|
658 #ifdef MAC_SDK_10_6 |
|
659 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
|
660 userProfiles.clear(); |
|
661 |
|
662 NSArray *wifiInterfaces = [CWInterface supportedInterfaces]; |
|
663 for(uint row=0; row < [wifiInterfaces count]; row++ ) { |
|
664 |
|
665 CWInterface *wifiInterface = [CWInterface interfaceWithName: [wifiInterfaces objectAtIndex:row]]; |
|
666 NSString *nsInterfaceName = [wifiInterface name]; |
|
667 // add user configured system networks |
|
668 SCDynamicStoreRef dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, (CFStringRef)@"Qt corewlan", nil, nil); |
|
669 CFDictionaryRef airportPlist = (const __CFDictionary*)SCDynamicStoreCopyValue(dynRef, (CFStringRef)[NSString stringWithFormat:@"Setup:/Network/Interface/%@/AirPort", nsInterfaceName]); |
|
670 CFRelease(dynRef); |
|
671 |
|
672 NSDictionary *prefNetDict = [airportPlist objectForKey:@"PreferredNetworks"]; |
|
673 |
|
674 NSArray *thisSsidarray = [prefNetDict valueForKey:@"SSID_STR"]; |
|
675 for(NSString *ssidkey in thisSsidarray) { |
|
676 QString thisSsid = nsstringToQString(ssidkey); |
|
677 if(!userProfiles.contains(thisSsid)) { |
|
678 QMap <QString,QString> map; |
|
679 map.insert(thisSsid, nsstringToQString(nsInterfaceName)); |
|
680 userProfiles.insert(thisSsid, map); |
|
681 } |
|
682 } |
|
683 CFRelease(airportPlist); |
|
684 |
|
685 // 802.1X user profiles |
|
686 QString userProfilePath = QDir::homePath() + "/Library/Preferences/com.apple.eap.profiles.plist"; |
|
687 NSDictionary* eapDict = [[NSMutableDictionary alloc] initWithContentsOfFile:qstringToNSString(userProfilePath)]; |
|
688 NSString *profileStr= @"Profiles"; |
|
689 NSString *nameStr = @"UserDefinedName"; |
|
690 NSString *networkSsidStr = @"Wireless Network"; |
|
691 for (id profileKey in eapDict) { |
|
692 if ([profileStr isEqualToString:profileKey]) { |
|
693 NSDictionary *itemDict = [eapDict objectForKey:profileKey]; |
|
694 for (id itemKey in itemDict) { |
|
695 |
|
696 NSInteger dictSize = [itemKey count]; |
|
697 id objects[dictSize]; |
|
698 id keys[dictSize]; |
|
699 |
|
700 [itemKey getObjects:objects andKeys:keys]; |
|
701 QString networkName; |
|
702 QString ssid; |
|
703 for(int i = 0; i < dictSize; i++) { |
|
704 if([nameStr isEqualToString:keys[i]]) { |
|
705 networkName = nsstringToQString(objects[i]); |
|
706 } |
|
707 if([networkSsidStr isEqualToString:keys[i]]) { |
|
708 ssid = nsstringToQString(objects[i]); |
|
709 } |
|
710 if(!userProfiles.contains(networkName) |
|
711 && !ssid.isEmpty()) { |
|
712 QMap<QString,QString> map; |
|
713 map.insert(ssid, nsstringToQString(nsInterfaceName)); |
|
714 userProfiles.insert(networkName, map); |
|
715 } |
|
716 } |
|
717 } |
|
718 } |
|
719 } |
|
720 } |
|
721 [autoreleasepool release]; |
|
722 #endif |
|
723 |
|
724 } |
547 |
725 |
548 #include "moc_qcorewlanengine_mac_p.cpp" |
726 #include "moc_qcorewlanengine_mac_p.cpp" |
549 |
727 |
550 QTM_END_NAMESPACE |
728 QTM_END_NAMESPACE |
551 |
|