// // JSRunIpodHelperController.m // JSRunIpodHelperApp // // Created by Jonathan Saggau on 10/30/06. // Copyright 2006 Jonathan Saggau. All rights reserved. // #import "JSRunIpodHelperController.h" #import "JSIpodDetector.h" #import "Growl-WithInstaller/Growl.h" @interface JSRunIpodHelperController (PrivateAPI) - (void) handleIpodsOnStartup; //JSIpodDetector Delegate methods - (void) ipodDidMountAtPath: (NSString *)path; - (void) ipodDidUnmountAtPath: (NSString *)path; - (void) ipodWithRunsDidMountAtPath: (NSString *)path; //official growl optional delegate methods - (NSString *) applicationNameForGrowl; - (void) growlIsReady; //internal growl notification methods - (void) notifyGrowldetectedIpodAtPath:(NSString *)path; - (void) notifyGrowlipodDidUnmountAtPath:(NSString *)path; - (void) notifyGrowldetectedIpodWithRunsAtPath:(NSString *)path; //internal growl helper methods - (void) loadGrowl; - (void) unloadGrowl; @end @implementation JSRunIpodHelperController // init - (id)init { if (self = [super init]) { ipodDetector = Nil; growlIsReady = NO; notifyGrowl = NO; //define all growl notifications NSArray *notifications = [NSArray arrayWithObjects: @"detectedIpodAtPath", @"ipodDidUnmountAtPath", @"detectedIpodWithRunsAtPath", NULL]; //make dictionary for growl growlNotifications = [[NSDictionary alloc] initWithObjectsAndKeys: notifications, //obj GROWL_NOTIFICATIONS_ALL, //key for obj ^ notifications, //etc GROWL_NOTIFICATIONS_DEFAULT, NULL]; } return self; } - (void) awakeFromNib { ipodDetector = [[JSIpodDetector alloc] init]; [ipodDetector setDelegate:self]; [self setNotifyGrowl:YES]; [self handleIpodsOnStartup]; } - (void) dealloc { [ipodDetector setDelegate:nil]; [ipodDetector release]; ipodDetector = nil; [growlNotifications release]; growlNotifications = nil; [self unloadGrowl]; [super dealloc]; } - (void) handleIpodsOnStartup { NSArray *mountedIpodPaths = [ipodDetector mountedIpodPaths]; NSArray *mountedIpodWithRunsPaths = [ipodDetector mountedIpodWithRunsPaths]; // NSDictionary *pedometersFiles = [ipodDetector pedometersFiles]; NSEnumerator *ipods = [mountedIpodPaths objectEnumerator]; NSEnumerator *ipodsWithRuns = [mountedIpodWithRunsPaths objectEnumerator]; // NSEnumerator *pedometersFiles = [mountedIpodWithRunsPaths objectEnumerator]; NSString *path; while (path = [ipods nextObject]) { [self notifyGrowldetectedIpodAtPath:path]; } while (path = [ipodsWithRuns nextObject]) { [self notifyGrowldetectedIpodWithRunsAtPath:path]; } } #pragma mark - #pragma mark JSIpodDetector Delegate methods - (void) ipodDidMountAtPath: (NSString *)path { [self notifyGrowldetectedIpodAtPath:path]; } - (void) ipodDidUnmountAtPath: (NSString *)path { [self notifyGrowlipodDidUnmountAtPath:path]; } - (void) ipodWithRunsDidMountAtPath: (NSString *)path; { [self notifyGrowldetectedIpodWithRunsAtPath:path]; } #pragma mark - #pragma mark growl #pragma mark protocol - (NSDictionary *) registrationDictionaryForGrowl; { return growlNotifications; } #pragma mark delegate methods - (NSString *) applicationNameForGrowl; { return @"JSRun Ipod Notifier"; } - (void) growlIsReady; { if (!growlIsReady) { //handle growl state change [self loadGrowl]; } growlIsReady = YES; } #pragma mark notification methods - (void) notifyGrowldetectedIpodAtPath:(NSString *)path { if (notifyGrowl && growlIsReady) { [GrowlApplicationBridge notifyWithTitle:@"Ipod Event" description:[NSString stringWithFormat:@"An ipod was detected at %@", path] notificationName:@"detectedIpodAtPath" iconData:nil //default app icon. Should change it to the ipod's icon (NSData *) priority:0.0 isSticky:NO //NO causes it to fade out, YES will keep it on the screen until clicked clickContext:nil]; /* If the delegate implements the growlNotificationWasClicked: method (see above), this context will be passed back to the delegate if the notification is clicked. (Optional; pass nil to not receive click notifications for this notification.) http://growl.info/documentation/developer/implementing-growl.php?lang=cocoa */ } } /* growl notifications from init @"detectedIpodAtPath", @"ipodDidUnmountAtPath", @"detectedIpodWithRunsAtPath", */ - (void) notifyGrowlipodDidUnmountAtPath:(NSString *)path { if (notifyGrowl && growlIsReady) { [GrowlApplicationBridge notifyWithTitle:@"Ipod Event" description:[NSString stringWithFormat:@"An ipod was removed from %@", path] notificationName:@"ipodDidUnmountAtPath" iconData:nil //default app icon. Should change it to the ipod's icon (NSData *) priority:0.0 isSticky:NO //NO causes it to fade out, YES will keep it on the screen until clicked clickContext:nil]; } } - (void) notifyGrowldetectedIpodWithRunsAtPath:(NSString *)path { if (notifyGrowl && growlIsReady) { [GrowlApplicationBridge notifyWithTitle:@"Ipod Event" description:[NSString stringWithFormat:@"An ipod containing runs was detected at %@", path] notificationName:@"detectedIpodWithRunsAtPath" iconData:nil //default app icon. Should change it to the ipod's icon (NSData *) priority:0.0 isSticky:NO //NO causes it to fade out, YES will keep it on the screen until clicked clickContext:nil]; } } #pragma mark growl helper methods - (void) loadGrowl; { NSBundle *myBundle = [NSBundle bundleForClass:[self class]]; NSString *growlPath = [[myBundle privateFrameworksPath] stringByAppendingPathComponent:@"Growl-WithInstaller.framework"]; NSBundle *growlBundle = [NSBundle bundleWithPath:growlPath]; if (growlBundle && [growlBundle load]) { // Register ourselves as a Growl delegate [GrowlApplicationBridge setGrowlDelegate:self]; if ([GrowlApplicationBridge isGrowlRunning]) { growlIsReady = YES; } else { //fortunately, growl just "does the right thing" when it changes on/off state //it will let us know when it's ready and won't break our app if we send it a msg //when it is not running. NSLog(@"Growl is not running but Growl-WithInstaller.framework was loaded."); } } else { NSLog(@"Could not load Growl-WithInstaller.framework"); } } - (void) unloadGrowl; { [GrowlApplicationBridge setGrowlDelegate:nil]; //bad idea? } #pragma mark - #pragma mark accessors //=========================================================== // ipodDetector //=========================================================== - (JSIpodDetector *)ipodDetector { return ipodDetector; } - (void)setIpodDetector:(JSIpodDetector *)anIpodDetector { if (ipodDetector != anIpodDetector) { [anIpodDetector retain]; [ipodDetector release]; ipodDetector = anIpodDetector; } } //=========================================================== // notifyGrowl //=========================================================== - (BOOL)notifyGrowl { return notifyGrowl; } - (void)setNotifyGrowl:(BOOL)flag { notifyGrowl = flag; if (notifyGrowl) [self loadGrowl]; else [self unloadGrowl]; } @end