X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=AppController.m;h=af247bdccd4e257aca362285d578090ccbfd3957;hb=e8d51183acdd2410a38dcf8f0efbf7c30cd6c581;hp=c8750c03b8ceec35d47e654dc9f716b8d61ef1f1;hpb=42cf7bbe564d70233a0d73baee613f209eb00eb6;p=chaz%2Fthecheat diff --git a/AppController.m b/AppController.m index c8750c0..af247bd 100644 --- a/AppController.m +++ b/AppController.m @@ -12,6 +12,7 @@ #include "cheat_shared.h" +#import "SessionController.h" #import "AboutBoxController.h" #import "PreferenceController.h" #import "NetTrafficController.h" @@ -24,11 +25,22 @@ @implementation AppController + +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark Initialization +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + + + (void)initialize { NSMutableDictionary *defaults = [NSMutableDictionary dictionary]; char temp[104]; + // set up logging +//#ifndef TC_SHOW_LOGS + CMLogDisable(); +//#endif + // change the socket path to reside in the home directory of the current user strncpy( temp, [NSHomeDirectory() lossyCString], 103 ); strncat( temp, TCDefaultListenPath, 103 - strlen(TCDefaultListenPath) ); @@ -36,16 +48,20 @@ [defaults setObject:[NSNumber numberWithBool:TCGlobalPlaySounds] forKey:TCPlaySoundsPref]; [defaults setObject:[NSNumber numberWithBool:TCGlobalWindowsOnTop] forKey:TCWindowsOnTopPref]; + [defaults setObject:[NSNumber numberWithBool:TCGlobalUpdateCheck] forKey:TCUpdateCheckPref]; [defaults setObject:[NSNumber numberWithBool:TCGlobalAllowRemote] forKey:TCAllowRemotePref]; [defaults setObject:[NSNumber numberWithInt:TCGlobalListenPort] forKey:TCListenPortPref]; [defaults setObject:[NSString stringWithFormat:@"%@'s Computer", NSFullUserName()] forKey:TCBroadcastNamePref]; + [defaults setObject:[NSNumber numberWithInt:TCGlobalHitsDisplayed] forKey:TCHitsDisplayedPref]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaults]; - TCGlobalPlaySounds = [[NSUserDefaults standardUserDefaults] integerForKey:TCPlaySoundsPref]; - TCGlobalWindowsOnTop = [[NSUserDefaults standardUserDefaults] integerForKey:TCWindowsOnTopPref]; - TCGlobalListenPort = [[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref]; + TCGlobalPlaySounds = [[NSUserDefaults standardUserDefaults] boolForKey:TCPlaySoundsPref]; + TCGlobalWindowsOnTop = [[NSUserDefaults standardUserDefaults] boolForKey:TCWindowsOnTopPref]; + TCGlobalUpdateCheck = [[NSUserDefaults standardUserDefaults] boolForKey:TCUpdateCheckPref]; TCGlobalAllowRemote = [[NSUserDefaults standardUserDefaults] boolForKey:TCAllowRemotePref]; + TCGlobalListenPort = [[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref]; + TCGlobalHitsDisplayed = [[NSUserDefaults standardUserDefaults] integerForKey:TCHitsDisplayedPref]; } - (id)init @@ -53,17 +69,70 @@ if ( self = [super init] ) { servers = [[NSMutableArray alloc] init]; - - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willQuit:) name:@"NSApplicationWillTerminateNotification" object:nil]; + // start the server with saved settings [self listenOnPort:TCGlobalListenPort remote:TCGlobalAllowRemote]; - [self broadcastWithName:TCGlobalBroadcastName]; + if ( TCGlobalAllowRemote ) [self broadcastWithName:TCGlobalBroadcastName]; + + // set up the network browser + browser = [[NSNetServiceBrowser alloc] init]; + [browser setDelegate:self]; + [browser searchForServicesOfType:@"_cheat._tcp." inDomain:@"local."]; + + serverList = [[NSMutableArray alloc] init]; + + [self setDelegate:self]; } return self; } +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [self stopListener]; + [self stopBroadcast]; + + [servers release]; + + [browser release]; + [serverList release]; + + [super dealloc]; +} + + +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark NSApplication Delegate +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + + +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification +{ + if ( TCGlobalUpdateCheck ) + { + [CMUpdateCheck checkWithURL:@"http://www.brokenzipper.com/software.plist" verbose:NO]; + } + + [self newSessionWindow:self]; +} + +- (void)applicationDidBecomeActive:(NSNotification *)aNotification +{ + /*if ( TCGlobalSessionCount == 0 ) + { + [self newSessionWindow:self]; + }*/ +} + + +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark Server Starting/Stopping +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + + - (void)listenOnPort:(int)port remote:(BOOL)remote { if ( connection ) @@ -94,23 +163,29 @@ - (void)broadcastWithName:(NSString *)name { - if ( TCGlobalAllowRemote ) - { - [self stopBroadcast]; + [self stopBroadcast]; - service = [[NSNetService alloc] initWithDomain:@"local." type:@"_cheat._tcp." name:name port:TCGlobalListenPort]; - [service setDelegate:self]; - [service publish]; - } + service = [[NSNetService alloc] initWithDomain:@"local." type:@"_cheat._tcp." name:name port:TCGlobalListenPort]; + [service setDelegate:self]; + [service publish]; } - (void)stopBroadcast { - oldService = service; - [oldService stop], service = nil; + [service stop], service = nil; } +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark Interface Activation +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + + +- (IBAction)newSessionWindow:(id)sender +{ + [[[SessionController alloc] init] showWindow:self]; +} + - (IBAction)showAboutBoxWindow:(id)sender { if ( !aboutBoxController ) @@ -145,7 +220,12 @@ - (IBAction)launchHelpFile:(id)sender { //[[NSWorkspace sharedWorkspace] openFile:[[NSBundle mainBundle] pathForResource:@"Read Me" ofType:@"html"] withApplication:@"Safari"]; - [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Read Me" ofType:@"pdf"]]]; + [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Read Me" ofType:@"rtf"]]]; +} + +- (IBAction)launchEmailMenu:(id)sender +{ + LaunchEmail(); } - (IBAction)launchWebsiteMenu:(id)sender @@ -153,30 +233,56 @@ LaunchWebsite(); } -- (IBAction)launchDebugEmailMenu:(id)sender + +- (IBAction)checkForUpdate:(id)sender { - [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"mailto:thecheat@brokenzipper.com"]]; + [CMUpdateCheck checkWithURL:@"http://www.brokenzipper.com/software.plist"]; } -- (void)dealloc +- (NSArray *)serverList { - [[NSNotificationCenter defaultCenter] removeObserver:self]; + return serverList; +} - [self stopListener]; - [self stopBroadcast]; - [servers release]; - - [super dealloc]; -} +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark Controlling Preferences +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ -/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%% PreferenceControlling -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +- (void)preferenceSetWindowsOnTop:(BOOL)windowsOnTop +{ + if ( TCGlobalWindowsOnTop != windowsOnTop ) + { + [[NSNotificationCenter defaultCenter] postNotificationName:@"TCWindowsOnTopChanged" object:[NSNumber numberWithBool:windowsOnTop]]; + } +} +- (void)preferenceSetAllowRemote:(BOOL)allow listenPort:(int)port broadcastName:(NSString *)name +{ + if ( TCGlobalAllowRemote != allow || TCGlobalListenPort != port ) + { + [self listenOnPort:port remote:allow]; + } + if ( allow ) + { + if ( !TCGlobalAllowRemote || ![TCGlobalBroadcastName isEqualToString:name] ) + { + [self broadcastWithName:name]; + } + } + else + { + [self stopBroadcast]; + } + [netTrafficController serverSetAllowRemote:allow listenPort:port broadcastName:name]; + //[netTrafficController allowRemoteChanged:allow]; + //[netTrafficController listenPortChanged:port]; + //[netTrafficController broadcastNameChanged:name]; +} +/* - (void)preferenceAllowRemoteChanged:(BOOL)allow { [self listenOnPort:TCGlobalListenPort remote:allow]; @@ -207,11 +313,12 @@ [netTrafficController broadcastNameChanged:name]; } +*/ -/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%% NetTrafficControlling -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark Controlling NetTraffic +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ - (int)netTrafficConnectionCount @@ -226,15 +333,15 @@ - (void)netTrafficKillConnection:(int)index { - NSLog( @"kill connection" ); + CMLog( @"kill connection" ); close( [[servers objectAtIndex:index] sockfd] ); } -/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%% ListenerDelegate -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark Controlling Listener +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ - (void)listenerListeningWithSocket:(int)sock @@ -272,9 +379,9 @@ } -/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%% ServerDelegate -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark Controlling Server +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ - (void)server:(CheatServer *)server connectedWithSocket:(int)sock @@ -352,24 +459,24 @@ } -/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%% NetService Delegate -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark NetService Delegate +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ - (void)netServiceWillPublish:(NSNetService *)sender { - NSLog( @"service will publish" ); + CMLog( @"service will publish" ); [sender resolve]; } - (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict { - NSLog( @"service did not publish" ); + CMLog( @"service did not publish" ); if ( [[errorDict objectForKey:@"NSNetServicesErrorCode"] intValue] == NSNetServicesCollisionError ) { - [self broadcastWithName:[NSString stringWithFormat:@"%@ %i", TCGlobalBroadcastName, TCGlobalAlternateBroadcastNameCount++]]; + [self broadcastWithName:[NSString stringWithFormat:@"%@ %i", [sender name], TCGlobalAlternateBroadcastNameCount++]]; } else { @@ -379,20 +486,63 @@ - (void)netServiceDidStop:(NSNetService *)sender { - NSLog( @"service stopped" ); - [oldService release], oldService = nil; + CMLog( @"service stopped" ); + [sender release]; } -/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%% NSApplication Notification -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#pragma mark NetServiceBrowser Delegate +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ -- (void)willQuit:(NSNotification *)note +- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didFindService:(NSNetService *)aService moreComing:(BOOL)more { - //[self listenPortTextField:self]; - //[self broadcastNameTextField:self]; + // a server has broadcast; not much use until it's resolved. + [aService setDelegate:self]; + [aService resolve]; +} + +- (void)netServiceDidResolveAddress:(NSNetService *)aService +{ + int i, top = [serverList count]; + + // ignore if this is the local server. + if ( TCGlobalAllowRemote && [[aService name] isEqualToString:TCGlobalBroadcastName] ) + { + return; + } + + // ignore if the server name is already in the list. + for ( i = 0; i < top; i++ ) + { + if ( [[aService name] isEqualToString:[(NSNetService *)[serverList objectAtIndex:i] name]] ) + { + return; + } + } + + [serverList addObject:aService]; + CMLog( @"server added: %i", [serverList count] ); + + [[NSNotificationCenter defaultCenter] postNotificationName:@"TCServerFound" object:aService]; +} + +- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didRemoveService:(NSNetService *)aService moreComing:(BOOL)more +{ + int i, top = [serverList count]; + + for ( i = 0; i < top; i++ ) + { + if ( [[aService name] isEqualToString:[(NSNetService *)[serverList objectAtIndex:i] name]] ) + { + [serverList removeObjectAtIndex:i]; + CMLog( @"server deleted: %i", [serverList count] ); + break; + } + } + + [[NSNotificationCenter defaultCenter] postNotificationName:@"TCServerLost" object:aService]; }