Skip to content

Commit

Permalink
the default proxy is kind of useless feature (who will use more than …
Browse files Browse the repository at this point in the history
…one proxy on the same network), and implementing the automatic button
  • Loading branch information
Jérôme Lebel committed Sep 24, 2010
1 parent a192f34 commit 17d1443
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 42 deletions.
44 changes: 20 additions & 24 deletions iProxyMacSetup/Classes/PMUIController.m
Expand Up @@ -28,24 +28,26 @@ - (void)awakeFromNib

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"browsing"]) {
[self updateProgressIndicator];
} else if ([keyPath isEqualToString:@"resolvingServiceCount"]) {
[self updateProgressIndicator];
} 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];
if (object == appDelegate) {
if ([keyPath isEqualToString:@"browsing"]) {
[self updateProgressIndicator];
} else if ([keyPath isEqualToString:@"resolvingServiceCount"]) {
[self updateProgressIndicator];
} 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 Down Expand Up @@ -74,8 +76,6 @@ - (void)updateStartButton

- (void)updateProxyPopUpButton
{
NSString *defaultProxy = appDelegate.defaultProxy;

[proxyPopUpButton removeAllItems];
for (NSNetService *service in appDelegate.proxyServiceList) {
NSString *title;
Expand All @@ -86,9 +86,6 @@ - (void)updateProxyPopUpButton
} else {
[proxyPopUpButton addItemWithTitle:[NSString stringWithFormat:@"%@ (disabled)", title]];
}
if ([defaultProxy isEqualToString:title]) {
[proxyPopUpButton selectItem:[proxyPopUpButton lastItem]];
}
[title release];
}
[proxyPopUpButton setEnabled:!appDelegate.proxyEnabled && !appDelegate.automatic];
Expand Down Expand Up @@ -137,7 +134,6 @@ - (IBAction)interfacePopUpButtonAction:(id)sender

- (IBAction)proxyPopUpButtonAction:(id)sender
{
appDelegate.defaultProxy = [appDelegate.proxyServiceList objectAtIndex:[proxyPopUpButton indexOfSelectedItem]];
}

@end
3 changes: 1 addition & 2 deletions iProxyMacSetup/Classes/iProxyMacSetupAppDelegate.h
Expand Up @@ -21,8 +21,8 @@
NSUInteger resolvingServiceCount;

NSString *defaultInterface;
NSString *defaultProxy;
NSString *proxyEnabledInterfaceName;
BOOL proxyEnabled;
}

@property(readonly) BOOL browsing;
Expand All @@ -32,7 +32,6 @@
@property(readonly) NSArray *proxyServiceList;
@property(readonly) NSArray *interfaceList;
@property(retain, nonatomic) NSString *defaultInterface;
@property(retain, nonatomic) NSString *defaultProxy;

- (void)startBrowsingServices;
- (void)enableForInterface:(NSString *)interfaceName withProxy:(NSNetService *)proxy;
Expand Down
62 changes: 46 additions & 16 deletions iProxyMacSetup/Classes/iProxyMacSetupAppDelegate.m
Expand Up @@ -29,6 +29,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
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];
}

- (void)applicationWillTerminate:(NSNotification *)notification
Expand All @@ -38,6 +40,43 @@ - (void)applicationWillTerminate:(NSNotification *)notification
}
}

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

for (ii = 0; ii < count; ii++) {
if ([(NSNetService *)[proxyServiceList objectAtIndex:ii] port] != -1) {
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 && currentInterface && !self.proxyEnabled) {
[self enableForInterface:[currentInterface objectForKey:INTERFACE_NAME] withProxy:proxy];
}
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self) {
if ([keyPath isEqualToString:@"proxyServiceList"] || [keyPath isEqualToString:@"interfaceList"]) {
[self _updateAutomatic];
}
}
}

- (BOOL)automatic
{
return automatic;
Expand All @@ -46,11 +85,18 @@ - (BOOL)automatic
- (void)setAutomatic:(BOOL)value
{
if (automatic != value) {
BOOL shouldStop = automatic;

[self willChangeValueForKey:@"automatic"];
automatic = value;
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:automatic] forKey:@"AUTOMATIC"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self didChangeValueForKey:@"automatic"];

if (shouldStop && self.proxyEnabled) {
[self disableProxyForInterface:proxyEnabledInterfaceName];
}
[self _updateAutomatic];
}
}

Expand Down Expand Up @@ -225,22 +271,6 @@ - (void)setDefaultInterface:(NSString *)interface
[[NSUserDefaults standardUserDefaults] synchronize];
}

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

- (void)setDefaultProxy:(NSString *)proxy
{
[defaultProxy release];
defaultProxy = [proxy retain];
[[NSUserDefaults standardUserDefaults] setObject:defaultProxy forKey:@"DEFAULT_PROXY"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)_enableProxyForInterface:(NSString *)interface server:(NSString *)server port:(NSUInteger)port
{
NSTask *task;
Expand Down

0 comments on commit 17d1443

Please sign in to comment.