diff options
Diffstat (limited to 'src/plugins/bearer/corewlan/qcorewlanengine.mm')
-rw-r--r-- | src/plugins/bearer/corewlan/qcorewlanengine.mm | 163 |
1 files changed, 93 insertions, 70 deletions
diff --git a/src/plugins/bearer/corewlan/qcorewlanengine.mm b/src/plugins/bearer/corewlan/qcorewlanengine.mm index 2d33d36..cb278d9 100644 --- a/src/plugins/bearer/corewlan/qcorewlanengine.mm +++ b/src/plugins/bearer/corewlan/qcorewlanengine.mm @@ -61,44 +61,78 @@ #include <Foundation/NSEnumerator.h> #include <Foundation/NSKeyValueObserving.h> #include <Foundation/NSAutoreleasePool.h> +#include <Foundation/NSLock.h> #include <SystemConfiguration/SCNetworkConfiguration.h> +#include <private/qt_cocoa_helpers_mac_p.h> +#include "private/qcore_mac_p.h" + QMap <QString, QString> networkInterfaces; -QT_BEGIN_NAMESPACE +#ifdef MAC_SDK_10_6 +@interface QNSListener : NSObject +{ + NSNotificationCenter *center; + CWInterface *currentInterface; + QCoreWlanEngine *engine; + NSAutoreleasePool *autoreleasepool; + NSLock *locker; +} +- (void)notificationHandler:(NSNotification *)notification; +- (void)remove; +- (void)setEngine:(QCoreWlanEngine *)coreEngine; +- (void)dealloc; -inline QString cfstringRefToQstring(CFStringRef cfStringRef) { -// return QString([cfStringRef UTF8String]); - QString retVal; - CFIndex maxLength = 2 * CFStringGetLength(cfStringRef) + 1/*zero term*/; // max UTF8 - char *cstring = new char[maxLength]; - if (CFStringGetCString(CFStringRef(cfStringRef), cstring, maxLength, kCFStringEncodingUTF8)) { - retVal = QString::fromUtf8(cstring); - } - delete[] cstring; - return retVal; +@property (assign) QCoreWlanEngine* engine; + +@end + +@implementation QNSListener +- (id) init +{ + [locker lock]; + autoreleasepool = [[NSAutoreleasePool alloc] init]; + center = [NSNotificationCenter defaultCenter]; + currentInterface = [CWInterface interface]; +// [center addObserver:self selector:@selector(notificationHandler:) name:kCWLinkDidChangeNotification object:nil]; + [center addObserver:self selector:@selector(notificationHandler:) name:kCWPowerDidChangeNotification object:nil]; + [locker unlock]; + return self; } -inline CFStringRef qstringToCFStringRef(const QString &string) +-(void)dealloc { - return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(string.unicode()), - string.length()); + [autoreleasepool release]; + [super dealloc]; } -inline NSString *qstringToNSString(const QString &qstr) -{ return [reinterpret_cast<const NSString *>(qstringToCFStringRef(qstr)) autorelease]; } +-(void)setEngine:(QCoreWlanEngine *)coreEngine +{ + [locker lock]; + if(!engine) + engine = coreEngine; + [locker unlock]; +} -inline QString nsstringToQString(const NSString *nsstr) -{ return cfstringRefToQstring(reinterpret_cast<const CFStringRef>(nsstr)); } +-(void)remove +{ + [locker lock]; + [center removeObserver:self]; + [locker unlock]; +} -inline QStringList nsarrayToQStringList(void *nsarray) +- (void)notificationHandler:(NSNotification *)notification { - QStringList result; - NSArray *array = static_cast<NSArray *>(nsarray); - for (NSUInteger i=0; i<[array count]; ++i) - result << nsstringToQString([array objectAtIndex:i]); - return result; + engine->requestUpdate(); } +@end + +QNSListener *listener = 0; + +#endif + +QT_BEGIN_NAMESPACE + static QString qGetInterfaceType(const QString &interfaceString) { @@ -109,8 +143,8 @@ void networkChangeCallback(SCDynamicStoreRef/* store*/, CFArrayRef changedKeys, { for ( long i = 0; i < CFArrayGetCount(changedKeys); i++) { - CFStringRef changed = (CFStringRef)CFArrayGetValueAtIndex(changedKeys, i); - if( cfstringRefToQstring(changed).contains("/Network/Global/IPv4")) { + QString changed = QCFString::toQString((CFStringRef)CFArrayGetValueAtIndex(changedKeys, i)); + if( changed.contains("/Network/Global/IPv4")) { QCoreWlanEngine* wlanEngine = static_cast<QCoreWlanEngine*>(info); wlanEngine->requestUpdate(); } @@ -122,12 +156,23 @@ QCoreWlanEngine::QCoreWlanEngine(QObject *parent) : QBearerEngineImpl(parent) { startNetworkChangeLoop(); + +#if defined(MAC_SDK_10_6) + if(!listener) { + listener = [[QNSListener alloc] init]; + listener.engine = this; + } +#endif } QCoreWlanEngine::~QCoreWlanEngine() { while (!foundConfigurations.isEmpty()) delete foundConfigurations.takeFirst(); +#if defined(MAC_SDK_10_6) + [listener remove]; + [listener release]; +#endif } QString QCoreWlanEngine::getInterfaceFromId(const QString &id) @@ -153,7 +198,7 @@ void QCoreWlanEngine::connectToId(const QString &id) if(networkInterfaces.value(interfaceString) == "WLAN") { #if defined(MAC_SDK_10_6) - CWInterface *wifiInterface = [CWInterface interfaceWithName: qstringToNSString(interfaceString)]; + CWInterface *wifiInterface = [CWInterface interfaceWithName: qt_mac_QStringToNSString(interfaceString)]; CWConfiguration *userConfig = [ wifiInterface configuration]; NSSet *remNets = [userConfig rememberedNetworks]; //CWWirelessProfile @@ -170,7 +215,7 @@ void QCoreWlanEngine::connectToId(const QString &id) while ((wProfile = [enumerator nextObject])) { //CWWirelessProfile - if(id == QString::number(qHash(QLatin1String("corewlan:") + nsstringToQString([wProfile ssid])))) { + if(id == QString::number(qHash(QLatin1String("corewlan:") + qt_mac_NSStringToQString([wProfile ssid])))) { user8021XProfile = nil; user8021XProfile = [ wProfile user8021XProfile]; @@ -224,7 +269,7 @@ void QCoreWlanEngine::disconnectFromId(const QString &id) if(networkInterfaces.value(getInterfaceFromId(id)) == "WLAN") { //wifi only for now #if defined(MAC_SDK_10_6) NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; - CWInterface *wifiInterface = [CWInterface interfaceWithName: qstringToNSString(interfaceString)]; + CWInterface *wifiInterface = [CWInterface interfaceWithName: qt_mac_QStringToNSString(interfaceString)]; [wifiInterface disassociate]; if([[wifiInterface interfaceState]intValue] != kCWInterfaceStateInactive) { emit connectionError(id, DisconnectionError); @@ -242,15 +287,14 @@ void QCoreWlanEngine::requestUpdate() { QMutexLocker locker(&mutex); - pollTimer.stop(); - QTimer::singleShot(0, this, SLOT(doRequestUpdate())); + doRequestUpdate(); } void QCoreWlanEngine::doRequestUpdate() { QMutexLocker locker(&mutex); - getAllScInterfaces(); + getWifiInterfaces(); QStringList previous = accessPointConfigurations.keys(); @@ -328,8 +372,6 @@ void QCoreWlanEngine::doRequestUpdate() emit configurationRemoved(ptr); } - pollTimer.start(); - emit updateCompleted(); } @@ -340,9 +382,9 @@ QStringList QCoreWlanEngine::scanForSsids(const QString &interfaceName) QStringList found; #if defined(MAC_SDK_10_6) - NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; + QMacCocoaAutoReleasePool pool; - CWInterface *currentInterface = [CWInterface interfaceWithName:qstringToNSString(interfaceName)]; + CWInterface *currentInterface = [CWInterface interfaceWithName:qt_mac_QStringToNSString(interfaceName)]; if([currentInterface power]) { NSError *err = nil; NSDictionary *parametersDict = nil; @@ -351,11 +393,10 @@ QStringList QCoreWlanEngine::scanForSsids(const QString &interfaceName) CWNetwork *apNetwork; if (!err) { for(uint row=0; row < [apArray count]; row++ ) { - NSAutoreleasePool *looppool = [[NSAutoreleasePool alloc] init]; apNetwork = [apArray objectAtIndex:row]; - const QString networkSsid = nsstringToQString([apNetwork ssid]); + const QString networkSsid = qt_mac_NSStringToQString([apNetwork ssid]); const QString id = QString::number(qHash(QLatin1String("corewlan:") + networkSsid)); found.append(id); @@ -363,7 +404,7 @@ QStringList QCoreWlanEngine::scanForSsids(const QString &interfaceName) QNetworkConfiguration::StateFlags state = QNetworkConfiguration::Undefined; if ([currentInterface.interfaceState intValue] == kCWInterfaceStateRunning) { - if (networkSsid == nsstringToQString([currentInterface ssid])) + if (networkSsid == qt_mac_NSStringToQString([currentInterface ssid])) state = QNetworkConfiguration::Active; } else { if (isKnownSsid(interfaceName, networkSsid)) @@ -414,11 +455,9 @@ QStringList QCoreWlanEngine::scanForSsids(const QString &interfaceName) emit configurationAdded(ptr); } - [looppool release]; } } } - [autoreleasepool drain]; #else Q_UNUSED(interfaceName); #endif @@ -430,7 +469,7 @@ bool QCoreWlanEngine::isWifiReady(const QString &wifiDeviceName) QMutexLocker locker(&mutex); #if defined(MAC_SDK_10_6) - CWInterface *defaultInterface = [CWInterface interfaceWithName: qstringToNSString(wifiDeviceName)]; + CWInterface *defaultInterface = [CWInterface interfaceWithName: qt_mac_QStringToNSString(wifiDeviceName)]; if([defaultInterface power]) return true; #else @@ -444,11 +483,11 @@ bool QCoreWlanEngine::isKnownSsid(const QString &interfaceName, const QString &s QMutexLocker locker(&mutex); #if defined(MAC_SDK_10_6) - CWInterface *wifiInterface = [CWInterface interfaceWithName: qstringToNSString(interfaceName)]; + CWInterface *wifiInterface = [CWInterface interfaceWithName: qt_mac_QStringToNSString(interfaceName)]; CWConfiguration *userConfig = [wifiInterface configuration]; NSSet *remNets = [userConfig rememberedNetworks]; for (CWWirelessProfile *wProfile in remNets) { - if(ssid == nsstringToQString([wProfile ssid])) + if(ssid == qt_mac_NSStringToQString([wProfile ssid])) return true; } #else @@ -458,39 +497,18 @@ bool QCoreWlanEngine::isKnownSsid(const QString &interfaceName, const QString &s return false; } -bool QCoreWlanEngine::getAllScInterfaces() +bool QCoreWlanEngine::getWifiInterfaces() { QMutexLocker locker(&mutex); networkInterfaces.clear(); - NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; + QMacCocoaAutoReleasePool pool; - CFArrayRef interfaces = SCNetworkInterfaceCopyAll(); - if (interfaces != NULL) { - CFIndex interfaceCount; - CFIndex interfaceIndex; - interfaceCount = CFArrayGetCount(interfaces); - for (interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++) { - NSAutoreleasePool *looppool = [[NSAutoreleasePool alloc] init]; - - CFStringRef bsdName; - CFTypeRef thisInterface = CFArrayGetValueAtIndex(interfaces, interfaceIndex); - bsdName = SCNetworkInterfaceGetBSDName((SCNetworkInterfaceRef)thisInterface); - QString interfaceName = cfstringRefToQstring(bsdName); - QString typeStr; - CFStringRef type = SCNetworkInterfaceGetInterfaceType((SCNetworkInterfaceRef)thisInterface); - if ( CFEqual(type, kSCNetworkInterfaceTypeIEEE80211)) { - typeStr = "WLAN"; - } - if(!networkInterfaces.contains(interfaceName) && !typeStr.isEmpty()) { - networkInterfaces.insert(interfaceName,typeStr); - } - [looppool release]; - } + NSArray *wifiInterfaces = [CWInterface supportedInterfaces]; + for(uint row=0; row < [wifiInterfaces count]; row++ ) { + networkInterfaces.insert( qt_mac_NSStringToQString([wifiInterfaces objectAtIndex:row]),"WLAN"); } - CFRelease(interfaces); - [autoreleasepool drain]; return true; } @@ -589,4 +607,9 @@ QNetworkConfigurationPrivatePointer QCoreWlanEngine::defaultConfiguration() return QNetworkConfigurationPrivatePointer(); } +bool QCoreWlanEngine::requiresPolling() const +{ + return true; +} + QT_END_NAMESPACE |