#import <UIKit/UIKit.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIAlertSheet.h>
#import "EDGEServiceTableCell.h"
@implementation EDGEServiceTableCell
- (EDGEServiceTableCell *)initWithFrame:(CGRect)frame {
_prefs = SCPreferencesCreate(kCFAllocatorDefault, CFSTR("com.apple.preferences"), NULL);
NSDictionary *sbDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/NetServices.app/ProgramFiles/plists/settings.plist"];
NSString *EDGEavailability = [sbDictionary objectForKey:@"EDGEavailability"];
NSLog(@"EDGEavailability is currently %@ ", EDGEavailability);
if ([EDGEavailability intValue] == 1) {
[self _findEDGEPreferences];
[super initWithFrame:frame andValue: YES];
} else {
[self _findEDGEPreferences];
[super initWithFrame:frame andValue: NO];
}
NSLog(@"Initing the EDGE");
NSDictionary *cellDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/NetServices.app/ProgramFiles/plists/settings.plist"];
NSString *buttonLabel = [cellDictionary objectForKey:@"Label-EDGE"];
[self setControlChangedTarget:self withAction:@selector(buttonPushed:) withUserInfo:nil];
[self setTitle: buttonLabel];
[self setIcon: [[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"edge" ofType:@"png" inDirectory:@"/ProgramFiles/icons"]] autorelease]];
return self;
}
- (void)_findEDGEPreferences {
NSString *apn, *password, *username;
NSString *available;
NSDictionary *setup;
NSLog(@"Getting Network Interfaces");
/* This will get the current working set of network preferences */
SCNetworkSetRef nset = SCNetworkSetCopyCurrent(_prefs);
/* Here we get all the services available to us and iterate over it to find the EDGE one */
NSArray *services = (NSArray *)SCNetworkSetCopyServices(nset);
for (int i = [services count] - 1; i >= 0; i--) {
/* We get the interface that we are on and ask it for its type */
SCNetworkInterfaceRef face = SCNetworkServiceGetInterface((SCNetworkServiceRef)[services objectAtIndex:i]);
CFStringRef type = SCNetworkInterfaceGetInterfaceType(face);
NSLog(@"type: %@", type);
/* We compare its type to that of the EDGE service "com.apple.CommCenter" <- this is also the type for voicemail */
if (CFEqual(type, CFSTR("com.apple.CommCenter"))) {
/* Now we need to get the configuration elements of this interface */
NSDictionary *conf = (NSDictionary *)SCNetworkInterfaceGetConfiguration(face);
/* EDGE has AllowNetworkAccess=1 while Voicemail does not */
if ([[conf objectForKey:@"AllowNetworkAccess"] boolValue] && (setup = [conf objectForKey:@"Setup"]) && (available = [conf objectForKey:@"Available"])) {
/* Now grab the value for each of the necessary settings: apn, password, and username
This will weed out the erronious sections that seem to appear in some configurations */
if ((apn = [setup objectForKey:@"apn"]) && (password = [setup objectForKey:@"password"]) && (username = [setup objectForKey:@"username"])) {
/* Just double check to make sure this isn't voicemail! */
if ([apn rangeOfString:@"voicemail"].location == NSNotFound) {
/* Now that we have found edge save it for later use */
_edge = face;
CFRetain(_edge);
/* Make a copy of the Interface changes because that is what we will use to change configuration */
_edgeConfig = [conf mutableCopy];
[_edgeConfig setObject:[setup mutableCopy] forKey:@"Setup"];
//NSLog(@"%@", _edgeConfig);
/* Print a little debug about what we found */
CFStringRef serviceID = SCNetworkServiceGetServiceID((SCNetworkServiceRef)[services objectAtIndex:i]);
NSLog(@"%@ (%@) -> Setup -> apn: %@\tpassword:%@\tusername:%@\tAvailability:%@", serviceID, type, apn, password, username, available);
/* Exit the search, we found our goal */
break;
}
}
}
}
}
CFRelease(services);
CFRelease(nset);
}
- (BOOL)_setEDGEapn:(NSString *)newAPN {
NSNumber *newAPNint = [NSNumber numberWithInt:[newAPN intValue]];
[_edgeConfig setObject:newAPNint forKey:@"Available"];
// NSLog(@"%@", _edgeConfig);
if (!SCNetworkInterfaceSetConfiguration(_edge, (CFDictionaryRef)_edgeConfig)) {
NSLog(@"SCNetworkInterfaceSetConfiguration failed %s", SCErrorString(SCError()));
return NO;
}
if (!SCPreferencesCommitChanges(_prefs)) {
NSLog(@"SCPreferencesCommitChanges failed %s", SCErrorString(SCError()));
return NO;
}
if (!SCPreferencesApplyChanges(_prefs)) {
NSLog(@"SCPreferencesApplyChanges failed %s", SCErrorString(SCError()));
return NO;
}
return YES;
}
- (void)dealloc {
[_edgeConfig release];
CFRelease(_edge);
CFRelease(_prefs);
[super dealloc];
}
- (void)buttonPushed:(EDGEServiceTableCell *)stc {
/* Turn edge on/off appropriately */
if ([stc controlValue]) {
NSLog(@"Loading EDGE");
//NSLog(@"Activating Edge, Setting Available to 1");
/* Overwrite the settings in the dictionary */
[self _setEDGEapn:@"1"];
system([[NSString stringWithFormat:@"/Applications/NetServices.app/ProgramFiles/bin/plutil -i EDGEavailability -v 1 /Applications/NetServices.app/ProgramFiles/plists/settings.plist"] UTF8String]);
} else {
NSLog(@"Unloading EDGE");
//NSLog(@"Deactivating Edge, Setting Available to 0");
/* Overwrite the settings in the dictionary with a bogus entry to break EDGE */
[self _setEDGEapn:@"0"];
system([[NSString stringWithFormat:@"/Applications/NetServices.app/ProgramFiles/bin/plutil -i EDGEavailability -v 0 /Applications/NetServices.app/ProgramFiles/plists/settings.plist"] UTF8String]);
}
/* Restart the CommCenter to make the Edge setting take effect */
system("launchctl stop com.apple.CommCenter");
system("/Applications/NetServices.app/ProgramFiles/bin/alert -t 'EDGE' -b 'Adjusting EDGE settings often takes 10-15 Seconds. Please be patient.' -n -s 10");
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerDone:) userInfo:stc repeats:NO];
}
- (void)timerDone:(NSTimer *)timer {
EDGEServiceTableCell *stc = [timer userInfo];
[stc setLoading: ![stc loading]];
}
@end