qtmobility/src/bearer/qcorewlanengine_mac.mm
branchRCL_3
changeset 2 87be51aa5b5b
parent 1 5822d84012fb
child 5 4ea83c148e84
--- a/qtmobility/src/bearer/qcorewlanengine_mac.mm	Thu Apr 15 08:16:03 2010 +0300
+++ b/qtmobility/src/bearer/qcorewlanengine_mac.mm	Tue Apr 27 17:29:55 2010 +0300
@@ -81,7 +81,7 @@
 {
     [super init];
     center = [NSNotificationCenter defaultCenter];
-    currentInterface = [CWInterface interface];
+    currentInterface = [CWInterface interfaceWithName:nil];
     [center addObserver:self selector:@selector(notificationHandler:) name:kCWLinkDidChangeNotification object:nil];
     [center addObserver:self selector:@selector(notificationHandler:) name:kCWPowerDidChangeNotification object:nil];
 
@@ -282,53 +282,109 @@
             NSError *err = nil;
             NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
 
-            NSString *wantedSsid = 0;
-            bool okToProceed = true;
+            QString wantedSsid = 0;
+            bool using8021X = false;
 
             if(getNetworkNameFromSsid(id) != id) {
                 NSArray *array = [CW8021XProfile allUser8021XProfiles];
                 for (NSUInteger i=0; i<[array count]; ++i) {
+
                     if(id == nsstringToQString([[array objectAtIndex:i] userDefinedName])
                         || id == nsstringToQString([[array objectAtIndex:i] ssid]) ) {
                         QString thisName = getSsidFromNetworkName(id);
                         if(thisName.isEmpty()) {
-                            wantedSsid = qstringToNSString(id);
+                            wantedSsid = id;
                         } else {
-                            wantedSsid = qstringToNSString(thisName);
+                            wantedSsid = thisName;
                         }
-                        okToProceed = false;
                         [params setValue: [array objectAtIndex:i] forKey:kCWAssocKey8021XProfile];
+                        using8021X = true;
+                        break;
+                    }
+                }
+            }
+
+            if(!using8021X) {
+                QString wantedNetwork;
+                QMapIterator<QString, QMap<QString,QString> > i(userProfiles);
+                while (i.hasNext()) {
+                    i.next();
+                    wantedNetwork = i.key();
+                    if(id == wantedNetwork) {
+                        wantedSsid = getSsidFromNetworkName(wantedNetwork);
                         break;
                     }
                 }
             }
 
-            if(okToProceed) {
-                NSUInteger index = 0;
+            NSDictionary *parametersDict = [NSDictionary dictionaryWithObjectsAndKeys:
+                                       [NSNumber numberWithBool:YES], kCWScanKeyMerge,
+                                       [NSNumber numberWithInteger:100], kCWScanKeyRestTime,
+                                       qstringToNSString(wantedSsid), kCWScanKeySSID,
+                                       nil];
+
+
+            NSArray *scanArray = [NSMutableArray arrayWithArray:[wifiInterface scanForNetworksWithParameters:parametersDict error:&err]];
+            if(!err) {
+                for(uint row=0; row < [scanArray count]; row++ ) {
+                    CWNetwork *apNetwork = [scanArray objectAtIndex:row];
+                    if(wantedSsid == nsstringToQString([apNetwork ssid])) {
+
+                        if(!using8021X) {
+                            SecKeychainAttribute attributes[3];
 
-                CWConfiguration *userConfig = [ wifiInterface configuration];
-                NSSet *remNets = [userConfig rememberedNetworks];
-                NSEnumerator *enumerator = [remNets objectEnumerator];
-                CWWirelessProfile *wProfile;
+                            NSString *account = [apNetwork ssid];
+                            NSString *keyKind = @"AirPort network password";
+                            NSString *keyName = account;
+
+                            attributes[0].tag = kSecAccountItemAttr;
+                            attributes[0].data = (void *)[account UTF8String];
+                            attributes[0].length = [account length];
 
-                while ((wProfile = [enumerator nextObject])) {
-                    if(id == nsstringToQString([wProfile ssid])) {
+                            attributes[1].tag = kSecDescriptionItemAttr;
+                            attributes[1].data = (void *)[keyKind UTF8String];
+                            attributes[1].length = [keyKind length];
+
+                            attributes[2].tag = kSecLabelItemAttr;
+                            attributes[2].data = (void *)[keyName UTF8String];
+                            attributes[2].length = [keyName length];
+
+                            SecKeychainAttributeList attributeList = {3,attributes};
 
-                        wantedSsid = [wProfile ssid];
-                        [params setValue: [wProfile passphrase] forKey: kCWAssocKeyPassphrase];
-                        break;
-                    }
-                    index++;
-                }
-            }
+                            SecKeychainSearchRef searchRef;
+                            OSErr result = SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &attributeList, &searchRef);
+
+                            NSString *password = @"";
+                            SecKeychainItemRef searchItem;
+
+                            if (SecKeychainSearchCopyNext(searchRef, &searchItem) == noErr) {
+                                UInt32 realPasswordLength;
+                                SecKeychainAttribute attributesW[8];
+                                attributesW[0].tag = kSecAccountItemAttr;
+                                SecKeychainAttributeList listW = {1,attributesW};
+                                char *realPassword;
+                                OSStatus status = SecKeychainItemCopyContent(searchItem, NULL, &listW, &realPasswordLength,(void **)&realPassword);
+
+                                if (status == noErr) {
+                                    if (realPassword != NULL) {
 
-            NSDictionary *parametersDict = nil;
-            NSArray *apArray = [NSMutableArray arrayWithArray:[wifiInterface scanForNetworksWithParameters:parametersDict error:&err]];
+                                        QByteArray pBuf;
+                                        pBuf.resize(realPasswordLength);
+                                        pBuf.prepend(realPassword);
+                                        pBuf.insert(realPasswordLength,'\0');
+
+                                        password = [NSString stringWithUTF8String:pBuf];
+                                    }
+                                }
 
-            if(!err) {
-                for(uint row=0; row < [apArray count]; row++ ) {
-                    CWNetwork *apNetwork = [apArray objectAtIndex:row];
-                    if([[apNetwork ssid] compare:wantedSsid] == NSOrderedSame) {
+                                CFRelease(searchItem);
+                                SecKeychainItemFreeContent(&listW, realPassword);
+                            } else {
+                                qDebug() << "SecKeychainSearchCopyNext error";
+                            }
+                            [params setValue: password forKey: kCWAssocKeyPassphrase];
+                        } // end using8021X
+
                         bool result = [wifiInterface associateToNetwork: apNetwork parameters:[NSDictionary dictionaryWithDictionary:params] error:&err];
 
                         if(!result) {
@@ -415,9 +471,7 @@
              if(ij.key() == ssid) {
                  return i.key();
              }
-
          }
-            return map.key(ssid);
     }
     return QString();
 }
@@ -435,9 +489,11 @@
 
     if([currentInterface power]) {
         NSError *err = nil;
-        NSDictionary *parametersDict = nil;
+        NSDictionary *parametersDict =  [NSDictionary dictionaryWithObjectsAndKeys:
+                                   [NSNumber numberWithBool:YES], kCWScanKeyMerge,
+                                   [NSNumber numberWithInt:kCWScanTypeFast], kCWScanKeyScanType, // get the networks in the scan cache
+                                   [NSNumber numberWithInteger:100], kCWScanKeyRestTime, nil];
         NSArray* apArray = [currentInterface scanForNetworksWithParameters:parametersDict error:&err];
-
         CWNetwork *apNetwork;
         if(!err) {
             for(uint row=0; row < [apArray count]; row++ ) {
@@ -666,7 +722,7 @@
         NSString *nsInterfaceName = [wifiInterface name];
 // add user configured system networks
         SCDynamicStoreRef dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, (CFStringRef)@"Qt corewlan", nil, nil);
-        CFDictionaryRef airportPlist = (const __CFDictionary*)SCDynamicStoreCopyValue(dynRef, (CFStringRef)[NSString stringWithFormat:@"Setup:/Network/Interface/%@/AirPort", nsInterfaceName]);
+        NSDictionary *airportPlist = (NSDictionary *)SCDynamicStoreCopyValue(dynRef, (CFStringRef)[[NSString stringWithFormat:@"Setup:/Network/Interface/%@/AirPort", nsInterfaceName] autorelease]);
         CFRelease(dynRef);
 
         NSDictionary *prefNetDict = [airportPlist objectForKey:@"PreferredNetworks"];
@@ -684,7 +740,7 @@
 
         // 802.1X user profiles
         QString userProfilePath = QDir::homePath() + "/Library/Preferences/com.apple.eap.profiles.plist";
-        NSDictionary* eapDict = [[NSMutableDictionary alloc] initWithContentsOfFile:qstringToNSString(userProfilePath)];
+        NSDictionary* eapDict = [[NSDictionary alloc] initWithContentsOfFile:qstringToNSString(userProfilePath)];
         NSString *profileStr= @"Profiles";
         NSString *nameStr = @"UserDefinedName";
         NSString *networkSsidStr = @"Wireless Network";
@@ -715,8 +771,10 @@
                         }
                     }
                 }
+                [itemDict release];
             }
         }
+        [eapDict release];
     }
     [autoreleasepool release];
 #endif