#import "AppController.h" @implementation AppController - (id)init { [super init]; NSString *file = [[NSBundle mainBundle] pathForResource:@"halo3bg" ofType:@"jpg"]; bgImage = [[[NSImage alloc] initWithContentsOfFile:file] autorelease]; return self; } - (void) awakeFromNib { NSSize size = [gamerTag frame].size; [gamerTag setBoundsSize:NSMakeSize(size.width/2, size.height/2)]; size = [playerSnap frame].size; [playerSnap setBoundsSize:NSMakeSize(size.width/1.2, size.height/1.2)]; size = [callSign frame].size; [callSign setBoundsSize:NSMakeSize(size.width/1.7, size.height/1.7)]; [window setBackgroundColor:[NSColor colorWithPatternImage:bgImage]]; [window display]; } - (int)indexOf:(NSString *)key inside:(NSString *)whole from:(int) start { int len1 = [whole length]; int len2 = [key length]; int count, count2; for (count = start; count < len1-len2; count++) { if ([whole characterAtIndex:count] == [key characterAtIndex:0]) { BOOL matches = YES; for (count2 = count+1; count2 < count+len2; count2++) { if ([whole characterAtIndex:count2] != [key characterAtIndex:count2-count]) { matches = NO; break; } } if (matches) return count; } } return -1; } - (NSString*)findAndReplace:(NSString *)key inside:(NSString *)whole with:(NSString *)replacement { int len1 = [whole length]; int len2 = [key length]; int count, count2; for (count = 0; count < len1-len2; count++) { if ([whole characterAtIndex:count] == [key characterAtIndex:0]) { BOOL matches = YES; for (count2 = count+1; count2 < count+len2; count2++) { if ([whole characterAtIndex:count2] != [key characterAtIndex:count2-count]) { matches = NO; break; } } if (matches) { whole = [[[whole substringWithRange:NSMakeRange(0, count)] stringByAppendingString: replacement] stringByAppendingString: [whole substringFromIndex:count+len2]]; len1 = [whole length]; count -= len2; } } } return whole; } - (void)parseData:(NSString *)htmlPage { if ([self indexOf:@"Halo 3 Service Record Not Found" inside:htmlPage from:0] > -1) { [notFound setStringValue:@"Gamertag not found."]; [progressIndicator stopAnimation:self]; [progressIndicator displayIfNeeded]; [userInput setEditable:YES]; return; } [notFound setStringValue:@"Parsing..."]; int index1 = [self indexOf:@"imgModel" inside:htmlPage from:0]+15; int index2 = [self indexOf:@"style" inside:htmlPage from:index1]-2; NSString *imageURL = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; imageURL = [@"http://www.bungie.net" stringByAppendingString:imageURL]; imageURL = [self findAndReplace:@"&" inside:imageURL with:@"&"]; gamerImage = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:imageURL]]; [gamerImageView setImage:gamerImage]; [gamerImage release]; index1 = [self indexOf:@"imgEmblem" inside:htmlPage from:0]+16; index2 = [self indexOf:@"style" inside:htmlPage from:index1]-2; NSString *emblemURL = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; emblemURL = [@"http://www.bungie.net" stringByAppendingString:emblemURL]; emblemURL = [self findAndReplace:@"&" inside:emblemURL with:@"&"]; emblemImage = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:emblemURL]]; [emblemView setImage:emblemImage]; [emblemImage release]; NSString *gamerName = [userInput stringValue]; [gamerTag setStringValue:gamerName]; index1 = [self indexOf:@"Strip_lblRank" inside:htmlPage from:0]+15; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *rankStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [rank setStringValue:rankStr]; index1 = [self indexOf:@"hypNextRank" inside:htmlPage from:0]+56; index1 = [self indexOf:@"\"" inside:htmlPage from:index1]+2; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *promoStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [nextPromo setStringValue:promoStr]; index1 = [self indexOf:@"identityStrip_imgRank" inside:htmlPage from:0]; index1 = [self indexOf:@"src=" inside:htmlPage from:index1]+5; index2 = [self indexOf:@"gif" inside:htmlPage from:index1]+3; NSString *rankImageStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; rankImageStr = [@"http://www.bungie.net" stringByAppendingString:rankImageStr]; rankImage = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:rankImageStr]]; NSImage *bgRankImage = [[NSImage alloc] initWithSize:[rankImageView frame].size]; NSRect rect = NSMakeRect(0, 0, [rankImageView frame].size.width, [rankImageView frame].size.height); [bgRankImage lockFocus]; [[NSColor grayColor] set]; NSRectFill(rect); [rankImage drawAtPoint:NSMakePoint(0,0) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; [bgRankImage unlockFocus]; [rankImageView setImage:bgRankImage]; [rankImage release]; [bgRankImage release]; index1 = [self indexOf:@"lblSkill" inside:htmlPage from:0]+10; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *skillStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [hiSkill setStringValue:skillStr]; index1 = [self indexOf:@"Total Games" inside:htmlPage from:0]+57; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *totStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [totalGames setStringValue:totStr]; index1 = [self indexOf:@"Matchmade Games" inside:htmlPage from:0]+61; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *mmgStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [matchGames setStringValue:mmgStr]; index1 = [self indexOf:@"Custom Games" inside:htmlPage from:0]+58; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *custStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [custGames setStringValue:custStr]; index1 = [self indexOf:@"Campaign Missions" inside:htmlPage from:0]+63; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *campStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [campMiss setStringValue:campStr]; index1 = [self indexOf:@"Total EXP" inside:htmlPage from:index2]+56; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *expStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [totEXP setStringValue:expStr]; index1 = [self indexOf:@"lblServiceTag" inside:htmlPage from:0]+15; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *callStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [callSign setStringValue:callStr]; index1 = [self indexOf:@"Player Since" inside:htmlPage from:0]+44; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *pSinceStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [playSince setStringValue:pSinceStr]; index1 = [self indexOf:@"Last Played" inside:htmlPage from:0]+44; index2 = [self indexOf:@"" inside:htmlPage from:index1]; NSString *pLastStr = [htmlPage substringWithRange:NSMakeRange(index1, index2-index1)]; [lastPlay setStringValue:pLastStr]; [notFound setStringValue:@"Done"]; [progressIndicator stopAnimation:self]; [progressIndicator displayIfNeeded]; [userInput setEditable:YES]; } - (IBAction)okay:(id)sender { [progressIndicator startAnimation:self]; [progressIndicator displayIfNeeded]; [userInput setEditable:NO]; [notFound setStringValue:@"Downloading..."]; NSString *urlStr = [@"http://www.bungie.net/Stats/Halo3/Default.aspx?player=" stringByAppendingString:[userInput stringValue]]; urlStr = [self findAndReplace:@" " inside:urlStr with:@"+"]; NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { receivedData=[[NSMutableData data] retain]; } else { [notFound setStringValue:@"Could not connect to bungie.net"]; [progressIndicator stopAnimation:self]; [progressIndicator displayIfNeeded]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [receivedData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [progressIndicator stopAnimation:self]; [progressIndicator displayIfNeeded]; [userInput setEditable:YES]; [connection release]; [receivedData release]; [notFound setStringValue:@"Could not connect to bungie.net"]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *tempStr = [NSString stringWithCString:[receivedData bytes] length:[receivedData length]]; [self parseData:tempStr]; [receivedData release]; [connection release]; } @end