Skip to content

Commit

Permalink
we don't need to choose the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Lebel committed Sep 28, 2010
1 parent 6b7b2d4 commit aeab078
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 919 deletions.
3 changes: 0 additions & 3 deletions iProxyMacSetup/Classes/PMUIController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
{
IBOutlet iProxyMacSetupAppDelegate *appDelegate;
IBOutlet NSPopUpButton *proxyPopUpButton;
IBOutlet NSPopUpButton *interfacePopUpButton;
IBOutlet NSButton *automaticButton;
IBOutlet NSProgressIndicator *progressIndicator;
IBOutlet NSButton *startButton;
Expand All @@ -24,10 +23,8 @@

- (void)updateProgressIndicator;
- (void)updateProxyPopUpButton;
- (void)updateInterfacePopUpButton;
- (void)updateStartButton;
- (IBAction)startButtonAction:(id)sender;
- (IBAction)interfacePopUpButtonAction:(id)sender;
- (IBAction)proxyPopUpButtonAction:(id)sender;

@end
41 changes: 4 additions & 37 deletions iProxyMacSetup/Classes/PMUIController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ - (void)awakeFromNib
[appDelegate addObserver:self forKeyPath:@"browsing" options:NSKeyValueObservingOptionNew context:nil];
[appDelegate addObserver:self forKeyPath:@"resolvingServiceCount" options:NSKeyValueObservingOptionNew context:nil];
[appDelegate addObserver:self forKeyPath:@"proxyServiceList" options:NSKeyValueObservingOptionNew context:nil];
[appDelegate addObserver:self forKeyPath:@"interfaceList" options:NSKeyValueObservingOptionNew context:nil];
[appDelegate addObserver:self forKeyPath:@"proxyEnabled" options:NSKeyValueObservingOptionNew context:nil];
[appDelegate addObserver:self forKeyPath:@"automatic" options:NSKeyValueObservingOptionNew context:nil];
}
Expand All @@ -36,16 +35,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
} else if ([keyPath isEqualToString:@"proxyServiceList"]) {
[self updateStartButton];
[self updateProxyPopUpButton];
} else if ([keyPath isEqualToString:@"interfaceList"]) {
[self updateStartButton];
[self updateInterfacePopUpButton];
} else if ([keyPath isEqualToString:@"proxyEnabled"]) {
[self updateStartButton];
[self updateInterfacePopUpButton];
[self updateProxyPopUpButton];
} else if ([keyPath isEqualToString:@"automatic"]) {
[self updateStartButton];
[self updateInterfacePopUpButton];
[self updateProxyPopUpButton];
}
}
Expand All @@ -62,7 +56,7 @@ - (void)updateProgressIndicator

- (void)updateStartButton
{
if ([appDelegate.proxyServiceList count] > 0 && [appDelegate.interfaceList count] > 0) {
if ([appDelegate.proxyServiceList count] > 0) {
[startButton setEnabled:!appDelegate.automatic];
} else {
[startButton setEnabled:NO];
Expand All @@ -83,7 +77,7 @@ - (void)updateProxyPopUpButton

proxyService = [proxy objectForKey:PROXY_SERVICE_KEY];
title = [[NSString alloc] initWithFormat:@"%@.%@", [proxyService name], [proxyService domain]];
if ([iProxyMacSetupAppDelegate isProxyEnabled:proxy]) {
if ([[appDelegate class] isProxyReady:proxy]) {
[proxyPopUpButton addItemWithTitle:title];
} else {
[proxyPopUpButton addItemWithTitle:[NSString stringWithFormat:@"%@ (disabled)", title]];
Expand All @@ -94,46 +88,19 @@ - (void)updateProxyPopUpButton
[self updateStartButton];
}

- (void)updateInterfacePopUpButton
{
NSString *defaultInterface = appDelegate.defaultInterface;

[interfacePopUpButton removeAllItems];
for (NSDictionary *service in appDelegate.interfaceList) {
if ([[service objectForKey:INTERFACE_ENABLED] boolValue]) {
[interfacePopUpButton addItemWithTitle:[service objectForKey:INTERFACE_NAME]];
} else {
[interfacePopUpButton addItemWithTitle:[NSString stringWithFormat:@"%@ (disabled)", [service objectForKey:INTERFACE_NAME]]];
}
if ([defaultInterface isEqualToString:[service objectForKey:INTERFACE_NAME]]) {
[interfacePopUpButton selectItem:[interfacePopUpButton lastItem]];
}
}
[interfacePopUpButton setEnabled:!appDelegate.proxyEnabled && !appDelegate.automatic];
[self updateStartButton];
}

- (IBAction)startButtonAction:(id)sender
{
NSDictionary *interfaceInfo;
NSDictionary *proxy;

[self updateProxyPopUpButton];
[self updateInterfacePopUpButton];
interfaceInfo = [appDelegate.interfaceList objectAtIndex:[interfacePopUpButton indexOfSelectedItem]];
proxy = [appDelegate.proxyServiceList objectAtIndex:[proxyPopUpButton indexOfSelectedItem]];
if (appDelegate.proxyEnabled) {
[appDelegate disableProxyForInterface:[interfaceInfo objectForKey:INTERFACE_NAME]];
[appDelegate disableCurrentProxy];
} else {
[appDelegate enableForInterface:[interfaceInfo objectForKey:INTERFACE_NAME] withProxy:proxy];
[appDelegate enableProxy:proxy];
}
}

- (IBAction)interfacePopUpButtonAction:(id)sender
{
appDelegate.defaultInterface = [[appDelegate.interfaceList objectAtIndex:[interfacePopUpButton indexOfSelectedItem]] objectForKey:INTERFACE_NAME];
}

- (IBAction)proxyPopUpButtonAction:(id)sender
{
}
Expand Down
13 changes: 3 additions & 10 deletions iProxyMacSetup/Classes/iProxyMacSetupAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#import <Cocoa/Cocoa.h>

#define INTERFACE_NAME @"name"
#define INTERFACE_ENABLED @"enabled"

#define PROXY_SERVICE_KEY @"service"
#define PROXY_IP_KEY @"ip"
#define PROXY_INTERFACE_KEY @"interface"
Expand All @@ -19,13 +16,11 @@
@interface iProxyMacSetupAppDelegate : NSObject <NSApplicationDelegate, NSNetServiceBrowserDelegate, NSNetServiceDelegate>
{
NSMutableArray *proxyServiceList;
NSMutableArray *interfaceList;

BOOL browsing;
BOOL automatic;
NSUInteger resolvingServiceCount;

NSString *defaultInterface;
NSString *proxyEnabledInterfaceName;
BOOL proxyEnabled;
}
Expand All @@ -35,13 +30,11 @@
@property(readonly) BOOL proxyEnabled;
@property(readonly) NSUInteger resolvingServiceCount;
@property(readonly) NSArray *proxyServiceList;
@property(readonly) NSArray *interfaceList;
@property(retain, nonatomic) NSString *defaultInterface;

- (void)startBrowsingServices;
- (void)enableForInterface:(NSString *)interfaceName withProxy:(NSDictionary *)proxy;
- (void)disableProxyForInterface:(NSString *)interface;
- (void)enableProxy:(NSDictionary *)proxy;
- (void)disableCurrentProxy;

+ (BOOL)isProxyEnabled:(NSDictionary *)proxy;
+ (BOOL)isProxyReady:(NSDictionary *)proxy;

@end
123 changes: 16 additions & 107 deletions iProxyMacSetup/Classes/iProxyMacSetupAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,57 @@

@interface iProxyMacSetupAppDelegate ()

- (void)fetchInterfaceList;
- (NSString *)_getInterfaceNameForIP:(NSString *)ip;

@end

@implementation iProxyMacSetupAppDelegate

@synthesize browsing, resolvingServiceCount, proxyServiceList, interfaceList, proxyEnabled;
@synthesize browsing, resolvingServiceCount, proxyServiceList, proxyEnabled;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
proxyServiceList = [[NSMutableArray alloc] init];
interfaceList = [[NSMutableArray alloc] init];
browsing = NO;
resolvingServiceCount = 0;
self.automatic = [[[NSUserDefaults standardUserDefaults] valueForKey:@"AUTOMATIC"] boolValue];
[self fetchInterfaceList];
[self startBrowsingServices];
[self addObserver:self forKeyPath:@"proxyServiceList" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"interfaceList" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"resolvingServiceCount" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
if (proxyEnabledInterfaceName) {
[self disableProxyForInterface:proxyEnabledInterfaceName];
[self disableCurrentProxy];
}
}

- (void)_updateAutomatic
{
if (automatic) {
NSDictionary *proxy = nil;
NSDictionary *currentInterface = nil;
NSUInteger ii, count = [proxyServiceList count];

for (ii = 0; ii < count; ii++) {
if ([iProxyMacSetupAppDelegate isProxyEnabled:[proxyServiceList objectAtIndex:ii]]) {
if ([[self class] isProxyReady:[proxyServiceList objectAtIndex:ii]]) {
proxy = [proxyServiceList objectAtIndex:ii];
break;
}
}
for (NSDictionary *interface in interfaceList) {
if ([self.defaultInterface isEqualToString:[interface objectForKey:INTERFACE_NAME]]) {
currentInterface = interface;
break;
}
}
if ((!proxy || !currentInterface) && self.proxyEnabled) {
[self disableProxyForInterface:proxyEnabledInterfaceName];
if (!proxy && self.proxyEnabled) {
[self disableCurrentProxy];
}
if (proxy && currentInterface && !self.proxyEnabled) {
[self enableForInterface:[currentInterface objectForKey:INTERFACE_NAME] withProxy:proxy];
if (proxy && !self.proxyEnabled) {
[self enableProxy:proxy];
}
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self) {
if ([keyPath isEqualToString:@"proxyServiceList"] || [keyPath isEqualToString:@"interfaceList"] || [keyPath isEqualToString:@"resolvingServiceCount"]) {
if ([keyPath isEqualToString:@"proxyServiceList"] || [keyPath isEqualToString:@"resolvingServiceCount"]) {
[self _updateAutomatic];
}
}
Expand All @@ -97,7 +86,7 @@ - (void)setAutomatic:(BOOL)value
[self didChangeValueForKey:@"automatic"];

if (shouldStop && self.proxyEnabled) {
[self disableProxyForInterface:proxyEnabledInterfaceName];
[self disableCurrentProxy];
}
[self _updateAutomatic];
}
Expand All @@ -117,67 +106,6 @@ - (NSTask *)taskWithLaunchPath:(NSString *)launchPath arguments:(NSArray *)argum
return task;
}

- (void)fetchInterfaceList
{
NSTask *task;

task = [self taskWithLaunchPath:NETWORKSETUP_PATH arguments:[NSArray arrayWithObjects:@"-listnetworkserviceorder", nil]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(servicesFromTask:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[[task standardOutput] fileHandleForReading]];
[task launch];
}

NSString *parseInterface(NSString *line, BOOL *enabled)
{
assert(enabled != NULL);
NSUInteger ii, charCount = [line length];

if ([line characterAtIndex:0] != '(') {
return NULL;
}
for (ii = 1; ii < charCount; ii++) {
if ([line characterAtIndex:ii] == ')') {
break;
} else if (([line characterAtIndex:ii] < '0' || [line characterAtIndex:ii] > '9') && [line characterAtIndex:ii] != '*') {
return NULL;
}
}
if (ii == charCount) {
return NULL;
}
*enabled = ![[line substringWithRange:NSMakeRange(1, ii - 1)] isEqualToString:@"*"];
return [[line substringFromIndex:ii + 1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

- (void)servicesFromTask:(NSNotification *)notification
{
NSData *data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

[self willChangeValueForKey:@"interfaceList"];
NSUInteger index = 0;
NSUInteger endOfData = 0;
NSUInteger endLine = 0;
while (index < [result length]) {
BOOL enabled;
NSString *interfaceName;

[result getLineStart:&index end:&endLine contentsEnd:&endOfData forRange:NSMakeRange(index, 1)];
interfaceName = parseInterface([result substringWithRange:NSMakeRange(index, endOfData - index)], &enabled);
if (interfaceName) {
NSMutableDictionary *interfaceInfo;

interfaceInfo = [[NSMutableDictionary alloc] init];
[interfaceInfo setObject:interfaceName forKey:INTERFACE_NAME];
[interfaceInfo setObject:[NSNumber numberWithBool:enabled] forKey:INTERFACE_ENABLED];
[interfaceList addObject:interfaceInfo];
[interfaceInfo release];
}
index = endLine + 1;
};
[self didChangeValueForKey:@"interfaceList"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:[notification object]];
}

- (void)_setBrowsing:(BOOL)value
{
if (browsing != value) {
Expand Down Expand Up @@ -280,25 +208,6 @@ - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDic
[self _changeResolvingServiceWithOffset:-1];
}

- (NSString *)defaultInterface
{
if (!defaultInterface) {
defaultInterface = [[[NSUserDefaults standardUserDefaults] objectForKey:@"DEFAULT_INTERFACE"] retain];
if (!defaultInterface) {
defaultInterface = [@"AirPort" retain];
}
}
return defaultInterface;
}

- (void)setDefaultInterface:(NSString *)interface
{
[defaultInterface release];
defaultInterface = [interface retain];
[[NSUserDefaults standardUserDefaults] setObject:defaultInterface forKey:@"DEFAULT_INTERFACE"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)_enableProxyForInterface:(NSString *)interface server:(NSString *)server port:(NSUInteger)port
{
NSTask *task;
Expand All @@ -311,17 +220,17 @@ - (void)_enableProxyForInterface:(NSString *)interface server:(NSString *)server
[task waitUntilExit];
}

- (void)enableForInterface:(NSString *)interfaceName withProxy:(NSDictionary *)proxy
- (void)enableProxy:(NSDictionary *)proxy
{
if (!proxyEnabled) {
if (!proxyEnabled && [[self class] isProxyReady:proxy]) {
NSNetService *proxyService;

[self willChangeValueForKey:@"proxyEnabled"];
proxyService = [proxy objectForKey:PROXY_SERVICE_KEY];
if ([proxyService port] != -1) {
[self _enableProxyForInterface:interfaceName server:[proxy objectForKey:PROXY_IP_KEY] port:[proxyService port]];
[self _enableProxyForInterface:[proxy objectForKey:PROXY_INTERFACE_KEY] server:[proxy objectForKey:PROXY_IP_KEY] port:[proxyService port]];
proxyEnabled = YES;
proxyEnabledInterfaceName = [interfaceName retain];
proxyEnabledInterfaceName = [[proxy objectForKey:PROXY_INTERFACE_KEY] retain];
}
[self didChangeValueForKey:@"proxyEnabled"];
}
Expand All @@ -336,11 +245,11 @@ - (void)_disableProxyForInterface:(NSString *)interface
[task waitUntilExit];
}

- (void)disableProxyForInterface:(NSString *)interface
- (void)disableCurrentProxy
{
if (proxyEnabled) {
[self willChangeValueForKey:@"proxyEnabled"];
[self _disableProxyForInterface:interface];
[self _disableProxyForInterface:proxyEnabledInterfaceName];
proxyEnabled = NO;
[proxyEnabledInterfaceName release];
proxyEnabledInterfaceName = nil;
Expand Down Expand Up @@ -379,7 +288,7 @@ - (NSString *)_getInterfaceNameForIP:(NSString *)ip
return result;
}

+ (BOOL)isProxyEnabled:(NSDictionary *)proxy
+ (BOOL)isProxyReady:(NSDictionary *)proxy
{
NSNetService *proxyService;

Expand Down
Loading

0 comments on commit aeab078

Please sign in to comment.