43 #include "../qnetworksession_impl.h" 45 #include <QtNetwork/private/qnetworkconfiguration_p.h> 47 #include <QtCore/qthread.h> 48 #include <QtCore/qmutex.h> 49 #include <QtCore/qcoreapplication.h> 50 #include <QtCore/qstringlist.h> 52 #include <QtCore/qdebug.h> 57 #import <CoreWLAN/CoreWLAN.h> 60 #include "private/qcore_mac_p.h" 65 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 67 @interface QT_MANGLE_NAMESPACE(QNSListener) : NSObject
69 NSNotificationCenter *notificationCenter;
70 CWInterface *currentInterface;
74 - (void)notificationHandler:(NSNotification *)notification;
84 @implementation QT_MANGLE_NAMESPACE(QNSListener)
89 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
90 notificationCenter = [NSNotificationCenter defaultCenter];
91 currentInterface = [CWInterface interfaceWithName:nil];
92 [notificationCenter addObserver:self selector:@selector(notificationHandler:) name:CWPowerDidChangeNotification object:nil];
94 [autoreleasepool release];
119 [notificationCenter removeObserver:self];
123 - (void)notificationHandler:(NSNotification *)notification
126 engine->requestUpdate();
136 for (
long i = 0; i < CFArrayGetCount(changedKeys); i++) {
139 if( changed.
contains(
"/Network/Global/IPv4")) {
164 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
167 CWInterface *currentInterface = [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(interfaceName)];
170 if (currentInterface.powerOn) {
173 NSSet* apSet = [currentInterface scanForNetworksWithName:nil error:&err];
176 for (CWNetwork *apNetwork in apSet) {
183 if (currentInterface.serviceActive) {
196 if ([apNetwork supportsSecurity:kCWSecurityNone]) {
208 QMapIterator<QString, QMap<QString,QString> > i(
userProfiles);
209 while (i.hasNext()) {
220 QMapIterator<QString, QString> ij(i.value());
221 while (ij.hasNext()) {
223 interfaceName = ij.value();
226 if (currentInterface.serviceActive) {
246 [autoreleasepool release];
279 return foundConfigurations;
286 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
289 NSSet *wifiInterfaces = [CWInterface interfaceNames];
290 for (NSString *ifName in wifiInterfaces) {
292 CWInterface *wifiInterface = [CWInterface interfaceWithName: ifName];
293 if (!wifiInterface.powerOn)
296 NSString *nsInterfaceName = wifiInterface.ssid;
298 SCDynamicStoreRef dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, (
CFStringRef)
@"Qt corewlan", nil, nil);
299 NSDictionary * airportPlist = (NSDictionary *)SCDynamicStoreCopyValue(dynRef, (
CFStringRef)[NSString stringWithFormat:@"Setup:/Network/Interface/%@/AirPort", nsInterfaceName]);
301 if(airportPlist != nil) {
302 NSDictionary *prefNetDict = [airportPlist objectForKey:@"PreferredNetworks"];
304 NSArray *thisSsidarray = [prefNetDict valueForKey:@"SSID_STR"];
305 for (NSString *ssidkey in thisSsidarray) {
313 CFRelease(airportPlist);
318 NSDictionary* eapDict = [[[NSDictionary alloc] initWithContentsOfFile: (NSString *)QCFString::toCFStringRef(userProfilePath)] autorelease];
320 NSString *profileStr=
@"Profiles";
321 NSString *nameStr =
@"UserDefinedName";
322 NSString *networkSsidStr =
@"Wireless Network";
323 for (
id profileKey in eapDict) {
324 if ([profileStr isEqualToString:profileKey]) {
325 NSDictionary *itemDict = [eapDict objectForKey:profileKey];
326 for (
id itemKey in itemDict) {
328 NSInteger dictSize = [itemKey count];
329 id objects[dictSize];
332 [itemKey getObjects:objects andKeys:keys];
335 for(
int i = 0; i < dictSize; i++) {
336 if([nameStr isEqualToString:keys[i]]) {
339 if([networkSsidStr isEqualToString:keys[i]]) {
354 [autoreleasepool release];
361 QMapIterator<QString, QMap<QString,QString> > i(
userProfiles);
362 while (i.hasNext()) {
365 QMapIterator<QString, QString> ij(i.value());
366 while (ij.hasNext()) {
369 if(name == i.key() || name == networkNameHash) {
381 QMapIterator<QString, QMap<QString,QString> > i(
userProfiles);
382 while (i.hasNext()) {
385 QMapIterator<QString, QString> ij(i.value());
386 while (ij.hasNext()) {
388 if(ij.key() == ssid) {
400 QMapIterator<QString, QMap<QString,QString> > i(
userProfiles);
401 while (i.hasNext()) {
422 while (!foundConfigurations.
isEmpty())
431 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
433 if ([[CWInterface interfaceNames] count] > 0 && !
listener) {
434 listener = [[QT_MANGLE_NAMESPACE(QNSListener) alloc] init];
442 startNetworkChangeLoop();
443 [autoreleasepool release];
451 return scanThread->configurationInterface.value(
id);
458 return scanThread->configurationInterface.contains(
id);
464 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
465 QString interfaceString = getInterfaceFromId(
id);
467 CWInterface *wifiInterface =
468 [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(interfaceString)];
470 if (wifiInterface.powerOn) {
479 QMapIterator<QString, QMap<QString, QString> > i(scanThread->userProfiles);
480 while (i.hasNext()) {
482 wantedNetwork = i.key();
484 if (
id == networkNameHash) {
485 wantedSsid = scanThread->getSsidFromNetworkName(wantedNetwork);
490 NSSet *scanSet = [wifiInterface scanForNetworksWithName:(NSString *)QCFString::toCFStringRef(wantedSsid) error:&err];
493 for (CWNetwork *apNetwork in scanSet) {
494 CFDataRef ssidData = (CFDataRef)[apNetwork ssidData];
497 SecIdentityRef identity = 0;
499 if (CWKeychainCopyEAPIdentity(ssidData, &identity) == errSecSuccess) {
502 if (CWKeychainCopyEAPUsernameAndPassword(ssidData, &username, &password) == errSecSuccess) {
503 result = [wifiInterface associateToEnterpriseNetwork:apNetwork
504 identity:identity username:(NSString *)username password:(NSString *)password
512 if (CWKeychainCopyPassword(ssidData, &password) == errSecSuccess) {
513 result = [wifiInterface associateToNetwork:apNetwork password:(NSString *)password error:&err];
520 emit connectionError(
id, ConnectError);
531 emit connectionError(
id, InterfaceLookupError);
535 emit connectionError(
id, InterfaceLookupError);
536 [autoreleasepool release];
543 QString interfaceString = getInterfaceFromId(
id);
544 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
546 CWInterface *wifiInterface =
547 [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(interfaceString)];
549 [wifiInterface disassociate];
550 if (wifiInterface.serviceActive) {
552 emit connectionError(
id, DisconnectionError);
555 [autoreleasepool release];
560 scanThread->getUserConfigurations();
568 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
570 NSSet *wifiInterfaces = [CWInterface interfaceNames];
571 for (NSString *ifName in wifiInterfaces) {
576 if ([wifiInterfaces count] == 0)
578 [autoreleasepool release];
584 bool haswifi =
false;
586 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
587 CWInterface *defaultInterface = [CWInterface interfaceWithName: (NSString *)QCFString::toCFStringRef(wifiDeviceName)];
588 if (defaultInterface.powerOn) {
591 [autoreleasepool release];
630 SCDynamicStoreContext dynStoreContext = { 0,
this, NULL, NULL, NULL };
631 storeSession = SCDynamicStoreCreate(NULL,
632 CFSTR(
"networkChangeCallback"),
635 if (!storeSession ) {
636 qWarning() <<
"could not open dynamic store: error:" << SCErrorString(SCError());
640 CFMutableArrayRef notificationKeys;
641 notificationKeys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
642 CFMutableArrayRef patternsArray;
643 patternsArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
646 storeKey = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL,
647 kSCDynamicStoreDomainState,
649 CFArrayAppendValue(notificationKeys, storeKey);
652 storeKey = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL,
653 kSCDynamicStoreDomainState,
656 CFArrayAppendValue(patternsArray, storeKey);
659 if (!SCDynamicStoreSetNotificationKeys(storeSession , notificationKeys, patternsArray)) {
660 qWarning() <<
"register notification error:"<< SCErrorString(SCError());
661 CFRelease(storeSession );
662 CFRelease(notificationKeys);
663 CFRelease(patternsArray);
666 CFRelease(notificationKeys);
667 CFRelease(patternsArray);
669 runloopSource = SCDynamicStoreCreateRunLoopSource(NULL, storeSession , 0);
670 if (!runloopSource) {
671 qWarning() <<
"runloop source error:"<< SCErrorString(SCError());
672 CFRelease(storeSession );
676 CFRunLoopAddSource(CFRunLoopGetCurrent(), runloopSource, kCFRunLoopDefaultMode);
699 QStringList previous = accessPointConfigurations.keys();
702 while (!foundConfigurations.
isEmpty()) {
707 if (accessPointConfigurations.contains(cpPriv->
id)) {
710 bool changed =
false;
738 emit configurationChanged(ptr);
746 accessPointConfigurations.insert(ptr->
id, ptr);
749 emit configurationAdded(ptr);
756 accessPointConfigurations.take(previous.
takeFirst());
759 emit configurationRemoved(ptr);
764 emit updateCompleted();
771 const QString interfaceStr = getInterfaceFromId(
id);
772 return getBytes(interfaceStr,
false);
778 const QString interfaceStr = getInterfaceFromId(
id);
779 return getBytes(interfaceStr,
true);
785 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
788 NSString *filePath =
@"/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist";
789 NSDictionary* plistDict = [[[NSDictionary alloc] initWithContentsOfFile:filePath] autorelease];
792 NSString *input =
@"KnownNetworks";
793 NSString *timeStampStr =
@"_timeStamp";
795 NSString *ssidStr =
@"SSID_STR";
797 for (
id key in plistDict) {
798 if ([input isEqualToString:
key]) {
800 NSDictionary *knownNetworksDict = [plistDict objectForKey:key];
801 if(knownNetworksDict == nil)
803 for (
id networkKey in knownNetworksDict) {
804 bool isFound =
false;
805 NSDictionary *itemDict = [knownNetworksDict objectForKey:networkKey];
808 NSInteger dictSize = [itemDict count];
809 id objects[dictSize];
812 [itemDict getObjects:objects andKeys:keys];
814 for(
int i = 0; i < dictSize; i++) {
815 if([ssidStr isEqualToString:keys[i]]) {
817 if(ident == identifier) {
821 if(ok && [timeStampStr isEqualToString:keys[i]]) {
822 timestamp = (
quint64)[objects[i] timeIntervalSince1970];
832 [autoreleasepool release];
838 struct ifaddrs *ifAddressList, *ifAddress;
839 struct if_data *if_data;
843 if(getifaddrs(&ifAddressList) == 0) {
844 for(ifAddress = ifAddressList; ifAddress; ifAddress = ifAddress->ifa_next) {
845 if(interfaceName == ifAddress->ifa_name) {
846 if_data = (
struct if_data*)ifAddress->ifa_data;
848 bytes = if_data->ifi_ibytes;
851 bytes = if_data->ifi_obytes;
856 freeifaddrs(ifAddressList);
863 #else // QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
bool requiresPolling() const
uint qHash(const QProcEnvKey &key)
const struct __CFString * CFStringRef
Purpose
Specifies the purpose of the configuration.
static mach_timebase_info_data_t info
#define QT_END_NAMESPACE
This macro expands to.
void lock()
Locks the mutex.
void disconnectFromId(const QString &id)
QList< QNetworkConfigurationPrivate * > getConfigurations()
bool hasIdentifier(const QString &id)
QNetworkConfiguration::StateFlags state
QScanThread(QObject *parent=0)
Q_INVOKABLE void initialize()
quint64 bytesWritten(const QString &id)
static QString toQString(CFStringRef cfstr)
State
This enum describes the connectivity state of the session.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QString getSsidFromNetworkName(const QString &name)
quint64 bytesReceived(const QString &id)
The QString class provides a Unicode character string.
quint64 startTime(const QString &id)
The QObject class is the base class of all Qt objects.
QNetworkConfiguration::BearerType bearerType
QCoreWlanEngine(QObject *parent=0)
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
void getUserConfigurations()
Q_CORE_EXPORT void qDebug(const char *,...)
void append(const T &t)
Inserts value at the end of the list.
QList< QNetworkConfigurationPrivate * > fetchedConfigurations
QFuture< void > map(Sequence &sequence, MapFunction function)
#define QT_BEGIN_NAMESPACE
This macro expands to.
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
QNetworkConfigurationPrivatePointer defaultConfiguration()
T takeFirst()
Removes the first item in the list and returns it.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
QMap< QString, QMap< QString, QString > > userProfiles
const T value(const Key &key) const
Returns the value associated with the key key.
The QStringList class provides a list of strings.
void startNetworkChangeLoop()
Q_CORE_EXPORT void qWarning(const char *,...)
QNetworkSessionPrivate * createSessionBackend()
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
void connectToId(const QString &id)
const T * ptr(const T &t)
QList< Key > keys() const
Returns a list containing all the keys in the map in ascending order.
void clear()
Removes all items from the list.
QString getInterfaceFromId(const QString &id)
void unlock()
Unlocks the mutex.
QMap< QString, QString > configurationInterface
QString getNetworkNameFromSsid(const QString &ssid)
bool isWifiReady(const QString &dev)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
QNetworkConfiguration::Purpose purpose
void run()
The starting point for the thread.
QExplicitlySharedDataPointer< QNetworkConfigurationPrivate > QNetworkConfigurationPrivatePointer
Q_INVOKABLE void requestUpdate()
QNetworkConfiguration::Type type
#define QT_MANGLE_NAMESPACE(name)
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
bool isKnownSsid(const QString &ssid)
QObject * parent() const
Returns a pointer to the parent object.
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
quint64 getBytes(const QString &interfaceName, bool b)
void networkChangeCallback(SCDynamicStoreRef, CFArrayRef changedKeys, void *info)
bool contains(const Key &key) const
Returns true if the map contains an item with key key; otherwise returns false.
QNetworkSession::State sessionStateForId(const QString &id)
QStringList foundNetwork(const QString &id, const QString &ssid, const QNetworkConfiguration::StateFlags state, const QString &interfaceName, const QNetworkConfiguration::Purpose purpose)
static QNSListener * listener
QNetworkConfigurationManager::Capabilities capabilities() const
const struct __CFArray * CFArrayRef
The QThread class provides a platform-independent way to manage threads.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
void clear()
Removes all items from the map.
The QList class is a template class that provides lists.
int removeAll(const T &t)
Removes all occurrences of value in the list and returns the number of entries removed.
static QString homePath()
Returns the absolute path of the user's home directory.