From cc68591a5cc0b8d9b58061d56d4553c520fd988e Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Fri, 3 Jun 2011 09:54:38 -0400 Subject: [PATCH] Added some of the NSURL delegate methods --- DDWRT_MonitorAppDelegate.h | 7 +- DDWRT_MonitorAppDelegate.m | 38 ++-- English.lproj/MainMenu.xib | 396 +++++++++++++++++++++++++++++++++++-- WRTStatusClient.h | 25 ++- WRTStatusClient.m | 148 +++++++++++--- 5 files changed, 539 insertions(+), 75 deletions(-) diff --git a/DDWRT_MonitorAppDelegate.h b/DDWRT_MonitorAppDelegate.h index bf43e43..b66360c 100644 --- a/DDWRT_MonitorAppDelegate.h +++ b/DDWRT_MonitorAppDelegate.h @@ -19,9 +19,9 @@ IBOutlet WebView* BandwidthViewerWebView; IBOutlet id configFieldHostname; - IBOutlet id configFieldProtocol; - IBOutlet id configFieldProtocolHTTP; - IBOutlet id configFieldProtocolHTTPS; + IBOutlet NSMatrix *configFieldProtocol; + IBOutlet NSButtonCell *configFieldProtocolHTTP; + IBOutlet NSButtonCell *configFieldProtocolHTTPS; IBOutlet id configFieldPort; IBOutlet id configFieldRefresh; IBOutlet id configLabelRefresh; @@ -55,6 +55,7 @@ // application prefs: NSString *hostname; + NSString *protocol; int port; NSString *username; NSString *password; diff --git a/DDWRT_MonitorAppDelegate.m b/DDWRT_MonitorAppDelegate.m index 15fa9d7..3479add 100644 --- a/DDWRT_MonitorAppDelegate.m +++ b/DDWRT_MonitorAppDelegate.m @@ -33,7 +33,7 @@ [self readDefaults]; // initialize the client - wrt_client = [[WRTStatusClient alloc] initWithHostname:hostname port:port username:username password:password]; + wrt_client = [[WRTStatusClient alloc] initWithHostname:hostname port:port protocol:protocol username:username password:password]; //_readTimer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] integerForKey:@"refreshTime"] target:self selector:@selector(updateThroughput:) userInfo:nil repeats:YES]; //[_readTimer fire]; @@ -59,7 +59,8 @@ { NSLog(@"Creating new user defaults"); - // do any other initialization you want to do here - e.g. the starting default values. + // do any other initialization you want to do here - e.g. the starting default values. + [defaults setValue:@"http" forKey:@"protocol"]; [defaults setValue:@"192.168.1.1" forKey:@"hostname"]; [defaults setInteger:80 forKey:@"port"]; [defaults setValue:@"" forKey:@"username"]; @@ -82,6 +83,7 @@ - (void) readDefaults { hostname = [defaults valueForKey:@"hostname"]; + protocol = [defaults valueForKey:@"protocol"]; port = [defaults integerForKey:@"port"]; username = [defaults valueForKey:@"username"]; password = [defaults valueForKey:@"password"]; @@ -95,6 +97,7 @@ { // do any other initialization you want to do here - e.g. the starting default values. [defaults setValue:hostname forKey:@"hostname"]; + [defaults setValue:protocol forKey:@"protocol"]; [defaults setInteger:port forKey:@"port"]; [defaults setValue:username forKey:@"username"]; [defaults setValue:password forKey:@"password"]; @@ -157,7 +160,6 @@ - (void) setMenubarText:(NSString *)menubarText { - // Todo set proper line height NSFontManager *fontManager = [NSFontManager sharedFontManager]; NSFont *menuFont = [fontManager fontWithFamily:@"Lucida Grande" traits:NSBoldFontMask weight:10 size:8]; @@ -182,6 +184,8 @@ [configFieldHostname setStringValue:hostname]; } + // TODO + [configFieldPort setIntValue:[defaults integerForKey:@"port"]]; [configFieldRefresh setIntegerValue:[defaults integerForKey:@"refreshTime"]]; @@ -204,26 +208,23 @@ } - (void) showBandwidthViewer:(id)sender -{ - - - +{ // NSString *urlAddress = [NSString stringWithFormat:@"https://%@:%d/graph_if.svg?ppp1", // [[NSUserDefaults standardUserDefaults] stringForKey:@"hostname"], // [[NSUserDefaults standardUserDefaults] integerForKey:@"port"]]; // NSLog(@"%@", urlAddress); // - - [BandwidthViewer makeKeyAndOrderFront:nil]; - - NSURLRequest *request = [[wrt_client requestForBandwidthViewerForInterface:@"ppp1"] retain]; - - [ [BandwidthViewerWebView mainFrame] loadRequest: request ]; + [BandwidthViewer makeKeyAndOrderFront:nil]; + + NSURLRequest *request = [[wrt_client requestForBandwidthViewerForInterface:@"ppp0"] retain]; + + [ [BandwidthViewerWebView mainFrame] loadRequest: request ]; } - (void) hideBandwidthViewer:(id)sender { [BandwidthViewer orderOut:nil]; + [BandwidthViewer release]; } - (void) setConfigHostname:(id)sender @@ -314,9 +315,9 @@ port = [configFieldPort intValue]; [defaults setInteger:port forKey:@"port"]; - NSLog(@"Got %@ for %@", hostname, [configFieldHostname stringValue]); - - //NSLog(@"%@", [configFieldProtocol str]); + //NSLog(@"%d", [configFieldProtocol state]); + //NSLog(@"%d", [configFieldProtocolHTTP state]); + //NSLog(@"%d", [configFieldProtocolHTTPS state]); refreshTime = [configFieldRefresh integerValue]; [defaults setInteger:refreshTime forKey:@"refreshTime"]; @@ -408,11 +409,14 @@ //[wrtr doRequest:self requestSelector:@selector(wanRequestCallback:) uri:@"Status_Internet.live.asp"]; - //[wrt_client getStatusUpdate:@"Status_Bandwidth.asp" delegate:wanRequestCallback]; + [wrt_client getStatusUpdate:@"Status_Bandwidth.asp" delegate:self callback:@selector(wanRequestCallback:)]; } - (void) wanRequestCallback:(NSData *)data { + NSLog(@"Received callback for request"); + NSLog(@"Length: %lu", [data length]); + NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"%@", stringData); /* diff --git a/English.lproj/MainMenu.xib b/English.lproj/MainMenu.xib index b5bb4bf..7fb17d7 100644 --- a/English.lproj/MainMenu.xib +++ b/English.lproj/MainMenu.xib @@ -558,6 +558,7 @@ 268 {{129, 55}, {125, 17}} + YES @@ -585,6 +586,7 @@ 268 {{15, 56}, {62, 14}} + YES @@ -618,6 +620,7 @@ 268 {{79, 56}, {45, 14}} + 1 YES @@ -637,6 +640,7 @@ 268 {{82, 125}, {171, 16}} + YES @@ -673,6 +677,7 @@ 268 {{15, 125}, {62, 14}} + YES @@ -690,6 +695,7 @@ 268 {{15, 105}, {62, 14}} + YES @@ -707,6 +713,7 @@ 268 {{82, 103}, {126, 18}} + YES 1 @@ -721,7 +728,11 @@ 1 1211912703 - 0 + 2 + + NSImage + NSRadioButton + NSRadioButton @@ -743,7 +754,7 @@ 75 - {61, 18} + {56, 18} {4, 2} 1151868928 NSActionCell @@ -816,6 +827,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 268 {{82, 82}, {39, 16}} + YES @@ -835,6 +847,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 268 {{15, 82}, {62, 14}} + YES @@ -852,6 +865,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 268 {{15, 32}, {74, 18}} + YES @@ -880,6 +894,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 268 {{15, 12}, {76, 18}} + YES @@ -901,11 +916,13 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA {{1, 1}, {272, 152}} + {{17, 41}, {274, 168}} + {0, 0} @@ -930,6 +947,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 268 {{156, 195}, {135, 17}} + YES @@ -950,6 +968,8 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 289 {{213, 20}, {75, 17}} + + YES -2080244224 @@ -970,6 +990,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 268 {{130, 20}, {75, 17}} + YES @@ -992,6 +1013,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA {{20, 20}, {16, 16}} + 2 20746 @@ -1000,6 +1022,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA {{7, 11}, {308, 232}} + {{0, 0}, {2048, 1130}} @@ -1050,26 +1073,31 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA {555, 275} + YES 2 - + BandwidthViewer YES YES - WebKitDefaultFixedFontSize - WebKitDefaultFontSize - WebKitMinimumFontSize + BandwidthViewerWebKitDefaultFixedFontSize + BandwidthViewerWebKitDefaultFontSize + BandwidthViewerWebKitJavaEnabled + BandwidthViewerWebKitJavaScriptCanOpenWindowsAutomatically + BandwidthViewerWebKitJavaScriptEnabled YES - - + + + + @@ -1079,6 +1107,8 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA {{7, 11}, {555, 275}} + + YES 2 @@ -1444,14 +1474,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 805 - - - delegate - - - - 806 - menuWanTrafficIn @@ -1484,6 +1506,38 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 810 + + + configFieldProtocol + + + + 811 + + + + delegate + + + + 812 + + + + delegate + + + + 813 + + + + delegate + + + + 814 + @@ -2536,9 +2590,313 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - 810 + 814 + + + + YES + + DDWRT_MonitorAppDelegate + NSObject + + YES + + YES + refreshClientsMenu: + refreshMenu: + refreshSystemMenu: + refreshWanMenu: + + + YES + id + id + id + id + + + + YES + + YES + refreshClientsMenu: + refreshMenu: + refreshSystemMenu: + refreshWanMenu: + + + YES + + refreshClientsMenu: + id + + + refreshMenu: + id + + + refreshSystemMenu: + id + + + refreshWanMenu: + id + + + + + YES + + YES + AppMenu + BandwidthViewer + BandwidthViewerWebView + ConfigPanel + configButtonSave + configFieldHostname + configFieldPort + configFieldProtocol + configFieldProtocolHTTP + configFieldProtocolHTTPS + configFieldRefresh + configFieldShowIcon + configFieldUseBytes + configLabelRefresh + configLabelStatus + configStateStatus + menuConnections + menuLoad + menuUptime + menuWanBandwidthGraph + menuWanDNS + menuWanDNSItem0 + menuWanDNSItem1 + menuWanDNSItem2 + menuWanIP + menuWanSeparator + menuWanStatus + menuWanTrafficIn + menuWanTrafficOut + menuWanUptime + + + YES + NSMenu + NSPanel + WebView + NSPanel + id + id + id + NSMatrix + NSButtonCell + NSButtonCell + id + id + id + id + id + NSProgressIndicator + id + id + id + id + id + id + id + id + id + id + id + id + id + id + + + + YES + + YES + AppMenu + BandwidthViewer + BandwidthViewerWebView + ConfigPanel + configButtonSave + configFieldHostname + configFieldPort + configFieldProtocol + configFieldProtocolHTTP + configFieldProtocolHTTPS + configFieldRefresh + configFieldShowIcon + configFieldUseBytes + configLabelRefresh + configLabelStatus + configStateStatus + menuConnections + menuLoad + menuUptime + menuWanBandwidthGraph + menuWanDNS + menuWanDNSItem0 + menuWanDNSItem1 + menuWanDNSItem2 + menuWanIP + menuWanSeparator + menuWanStatus + menuWanTrafficIn + menuWanTrafficOut + menuWanUptime + + + YES + + AppMenu + NSMenu + + + BandwidthViewer + NSPanel + + + BandwidthViewerWebView + WebView + + + ConfigPanel + NSPanel + + + configButtonSave + id + + + configFieldHostname + id + + + configFieldPort + id + + + configFieldProtocol + NSMatrix + + + configFieldProtocolHTTP + NSButtonCell + + + configFieldProtocolHTTPS + NSButtonCell + + + configFieldRefresh + id + + + configFieldShowIcon + id + + + configFieldUseBytes + id + + + configLabelRefresh + id + + + configLabelStatus + id + + + configStateStatus + NSProgressIndicator + + + menuConnections + id + + + menuLoad + id + + + menuUptime + id + + + menuWanBandwidthGraph + id + + + menuWanDNS + id + + + menuWanDNSItem0 + id + + + menuWanDNSItem1 + id + + + menuWanDNSItem2 + id + + + menuWanIP + id + + + menuWanSeparator + id + + + menuWanStatus + id + + + menuWanTrafficIn + id + + + menuWanTrafficOut + id + + + menuWanUptime + id + + + + + IBProjectSource + ./Classes/DDWRT_MonitorAppDelegate.h + + + + WebView + + reloadFromOrigin: + id + + + reloadFromOrigin: + + reloadFromOrigin: + id + + + + IBProjectSource + ./Classes/WebView.h + + + - 0 IBCocoaFramework @@ -2557,12 +2915,14 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA YES NSMenuCheckmark NSMenuMixedState + NSRadioButton NSSwitch YES {9, 8} {7, 2} + {16, 15} {15, 15} diff --git a/WRTStatusClient.h b/WRTStatusClient.h index 7f0ac06..9f1bc33 100644 --- a/WRTStatusClient.h +++ b/WRTStatusClient.h @@ -13,28 +13,37 @@ BOOL wrtReachable; @private NSString *hostname; + NSString *protocol; int port; NSString *username; NSString *password; - //NSMutableData *receivedData; - //NSURLRequest *theRequest; - //NSURLConnection *theConnection; + NSMutableData *receivedData; + NSURLRequest *theRequest; + NSURLConnection *theConnection; - //id delegate; - //SEL callback; - //SEL errorCallback; + id delegate; + SEL callback; + SEL errorCallback; } -- (id) initWithHostname:(NSString*)new_hostname port:(int)new_port username:(NSString*)new_username password:(NSString*)new_password; +@property(nonatomic, retain) NSMutableData *receivedData; +@property(nonatomic, retain) id delegate; +@property(nonatomic) SEL callback; +@property(nonatomic) SEL errorCallback; + +- (id) initWithHostname:(NSString*)new_hostname port:(int)new_port protocol:(NSString*)new_protocol username:(NSString*)new_username password:(NSString*)new_password; - (NSMutableURLRequest*) buildRequest:(NSString*)uri; - (NSMutableURLRequest*) requestForBandwidthViewerForInterface:(NSString*)interface; -- (void) getStatusUpdate:(NSString*)uri delegate:(id)delegate; +- (void) getStatusUpdate:(NSString*)uri delegate:(id)requestDelegate callback:(SEL)requestSelector; - (NSString*) getHostname; - (void) setHostname:(NSString*)new_hostname; +- (NSString*) getProtocol; +- (void) setProtocol:(NSString*)new_protocol; + - (int) getPort; - (void) setPort:(int)new_port; diff --git a/WRTStatusClient.m b/WRTStatusClient.m index e3639f9..eda29df 100644 --- a/WRTStatusClient.m +++ b/WRTStatusClient.m @@ -11,39 +11,45 @@ @implementation WRTStatusClient -- (id) initWithHostname:(NSString*)new_hostname port:(int)new_port username:(NSString*)new_username password:(NSString*)new_password +@synthesize receivedData; +@synthesize delegate; +@synthesize callback; +@synthesize errorCallback; + +- (id) initWithHostname:(NSString*)new_hostname port:(int)new_port protocol:(NSString*)new_protocol username:(NSString*)new_username password:(NSString*)new_password { - self = [super init]; - - [self setHostname: new_hostname]; - [self setPort: new_port]; - [self setUsername: new_username]; - [self setPassword: new_password]; - - return self; + self = [super init]; + + [self setHostname: new_hostname]; + [self setProtocol: new_protocol]; + [self setPort: new_port]; + [self setUsername: new_username]; + [self setPassword: new_password]; + + return self; } #pragma mark - - (NSMutableURLRequest*) buildRequest:(NSString*)uri { - NSString *urlAddress = [NSString stringWithFormat:@"http://%@:%@@%@:%d/%@", - username, - password, - hostname, - port, - uri]; + NSString *urlAddress = [NSString stringWithFormat:@"%@://%@:%@@%@:%d/%@", + protocol, + username, + password, + hostname, + port, + uri]; + NSLog(@"Building request form %@", urlAddress); NSURL *url = [NSURL URLWithString:urlAddress]; //[self request:url]; // ---V + + theRequest = [[NSMutableURLRequest alloc] initWithURL: url]; - NSURLRequest *theRequest; - - theRequest = [[NSMutableURLRequest alloc] initWithURL: url]; - - return [theRequest autorelease]; + return [theRequest autorelease]; } - (NSMutableURLRequest*) requestForBandwidthViewerForInterface:(NSString*)interface @@ -53,17 +59,22 @@ return [self buildRequest:endpoint]; } -- (void) getStatusUpdate:(NSString*)uri delegate:(id)delegate +- (void) getStatusUpdate:(NSString*)uri delegate:(id)requestDelegate callback:(SEL)requestSelector { - NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:[self buildRequest: uri] delegate:delegate]; - + self.delegate = requestDelegate; + self.callback = requestSelector; + + theConnection = [[NSURLConnection alloc] initWithRequest:[self buildRequest: uri] delegate:self]; + + if (theConnection) { + receivedData = [[NSMutableData data] retain]; + } + [theConnection autorelease]; - - - // this function returns immediately - // the delegate will receive a function call with the following signature upon completion: - // - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data - + + // this function returns immediately + // the delegate will receive a function call with the following signature upon completion: + // - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data } @@ -82,6 +93,18 @@ [old_hostname autorelease]; } +- (NSString*) getProtocol +{ + return protocol; +} + +- (void) setProtocol:(NSString *)new_protocol +{ + NSString *old_protocol = protocol; + protocol = [new_protocol retain]; + [old_protocol autorelease]; +} + - (int) getPort { return port; @@ -116,5 +139,72 @@ [old_password autorelease]; } +#pragma mark - +#pragma mark NSURLConnection Delegate Methods: + +-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge +{ + NSLog(@"1"); + if ([challenge previousFailureCount] == 0) { + NSLog(@"1.1.1"); + NSURLCredential *newCredential; + newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]; + + NSLog(@"1.1.2"); + [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; + } else { + NSLog(@"1.2"); + [[challenge sender] cancelAuthenticationChallenge:challenge]; + // TODO Pop up authentication error + NSLog(@"Invalid Username or Password"); + } +} + +-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response +{ + NSLog(@"2"); + [receivedData setLength:0]; +} + +-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data +{ + NSLog(@"3 %lu", [data length]); + [receivedData appendData:data]; + NSLog(@"3 %lu", [receivedData length]); +} + +-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error +{ + NSLog(@"4"); + //[connection release]; + //[receivedData release]; + //[theRequest release]; + + //TODO Return alert + NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); + + if (errorCallback) { + [delegate performSelector:errorCallback withObject:error]; + } +} +-(void)connectionDidFinishLoading:(NSURLConnection *)connection +{ + NSLog(@"5"); + // TODO: Return the data + if (delegate && callback) { + if ([delegate respondsToSelector:self.callback]) { + [delegate performSelector:self.callback withObject:receivedData]; + } else { + // TODO: Return no data + NSLog(@"No response data from delegate"); + } + } else { + NSLog(@"Invalid delegate callback"); + } + + //[theConnection release]; + //[receivedData release]; + //[theRequest release]; +} @end