From 20c7b2776133a9b3531ac711dd4f4be2d0523ddd Mon Sep 17 00:00:00 2001
From: Lorn Potter <lorn.potter@nokia.com>
Date: Fri, 5 Mar 2010 15:28:31 +1000
Subject: add wifi power notifications. more Qt way for autorelease. and change
 to better way of getting wifi interfaces.

---
 src/plugins/bearer/corewlan/qcorewlanengine.h  |   2 +-
 src/plugins/bearer/corewlan/qcorewlanengine.mm | 114 ++++++++++++++++++-------
 2 files changed, 83 insertions(+), 33 deletions(-)

diff --git a/src/plugins/bearer/corewlan/qcorewlanengine.h b/src/plugins/bearer/corewlan/qcorewlanengine.h
index 3289ffb..cbaa9d6 100644
--- a/src/plugins/bearer/corewlan/qcorewlanengine.h
+++ b/src/plugins/bearer/corewlan/qcorewlanengine.h
@@ -78,7 +78,7 @@ public:
 
     QNetworkConfigurationPrivatePointer defaultConfiguration();
 
-    bool getAllScInterfaces();
+    bool getWifiInterfaces();
 
     bool requiresPolling() const;
 
diff --git a/src/plugins/bearer/corewlan/qcorewlanengine.mm b/src/plugins/bearer/corewlan/qcorewlanengine.mm
index ec7d92b..8f174db 100644
--- a/src/plugins/bearer/corewlan/qcorewlanengine.mm
+++ b/src/plugins/bearer/corewlan/qcorewlanengine.mm
@@ -61,14 +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>
+
 QMap <QString, QString> networkInterfaces;
 
+#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;
+
+@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;
+}
+
+-(void)dealloc
+{
+    [autoreleasepool release];
+    [super dealloc];
+}
+
+-(void)setEngine:(QCoreWlanEngine *)coreEngine
+{
+    [locker lock];
+    if(!engine)
+        engine = coreEngine;
+    [locker unlock];
+}
+
+-(void)remove
+{
+    [locker lock];
+    [center removeObserver:self];
+    [locker unlock];
+}
+
+- (void)notificationHandler:(NSNotification *)notification
+{
+    engine->requestUpdate();
+}
+@end
+
+QNSListener *listener = 0;
+
+#endif
+
 QT_BEGIN_NAMESPACE
 
 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];
@@ -122,12 +186,22 @@ 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();
+
+    [listener remove];
+    [listener release];
 }
 
 QString QCoreWlanEngine::getInterfaceFromId(const QString &id)
@@ -249,7 +323,7 @@ void QCoreWlanEngine::doRequestUpdate()
 {
     QMutexLocker locker(&mutex);
 
-    getAllScInterfaces();
+    getWifiInterfaces();
 
     QStringList previous = accessPointConfigurations.keys();
 
@@ -337,7 +411,7 @@ 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)];
     if([currentInterface power]) {
@@ -348,7 +422,6 @@ 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];
 
@@ -411,11 +484,9 @@ QStringList QCoreWlanEngine::scanForSsids(const QString &interfaceName)
 
                     emit configurationAdded(ptr);
                 }
-                [looppool release];
             }
         }
     }
-    [autoreleasepool drain];
 #else
     Q_UNUSED(interfaceName);
 #endif
@@ -455,39 +526,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( nsstringToQString([wifiInterfaces objectAtIndex:row]),"WLAN");
     }
-    CFRelease(interfaces);
 
-    [autoreleasepool drain];
     return true;
 }
 
-- 
cgit v0.12