diff --git a/Application/PlaybackEventController.h b/Application/PlaybackEventController.h new file mode 100644 index 000000000..2d94ade4a --- /dev/null +++ b/Application/PlaybackEventController.h @@ -0,0 +1,21 @@ +// +// PlaybackEventController.h +// Cog +// +// Created by Vincent Spader on 3/5/09. +// Copyright 2009 __MyCompanyName__. All rights reserved. +// + +#import +#import + +@class PlaybackController; +@class AudioScrobbler; +@interface PlaybackEventController : NSObject { + NSOperationQueue *queue; + + AudioScrobbler *scrobbler; + IBOutlet PlaybackController *playbackController; +} + +@end diff --git a/Application/PlaybackEventController.m b/Application/PlaybackEventController.m new file mode 100644 index 000000000..513c47a0b --- /dev/null +++ b/Application/PlaybackEventController.m @@ -0,0 +1,106 @@ +// +// PlaybackEventController.m +// Cog +// +// Created by Vincent Spader on 3/5/09. +// Copyright 2009 __MyCompanyName__. All rights reserved. +// + +#import "PlaybackEventController.h" + +#import "PlaylistEntry.h" +#import "AudioScrobbler.h" +#import "PlaybackController.h" + +@implementation PlaybackEventController + +- (void)initDefaults +{ + NSDictionary *defaultsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithBool:YES], @"enableAudioScrobbler", + [NSNumber numberWithBool:NO], @"automaticallyLaunchLastFM", + nil]; + + [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDictionary]; +} + +- (id)init +{ + self = [super init]; + if (self) + { + [self initDefaults]; + + queue = [[NSOperationQueue alloc] init]; + scrobbler = [[AudioScrobbler alloc] init]; + [GrowlApplicationBridge setGrowlDelegate:self]; + } + + return self; +} + +- (void)dealloc +{ + [queue release]; + + [super dealloc]; +} + +- (void)awakeFromNib +{ + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidBegin:) name:CogPlaybackDidBeginNotficiation object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidPause:) name:CogPlaybackDidPauseNotficiation object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidResume:) name:CogPlaybackDidResumeNotficiation object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidStop:) name:CogPlaybackDidStopNotficiation object:nil]; +} + +- (void)playbackDidBegin:(NSNotification *)notification +{ + PlaylistEntry *pe = [notification object]; + if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) { + [scrobbler start:pe]; + } + + // Note: We don't want to send a growl notification on resume. + [GrowlApplicationBridge notifyWithTitle:[pe title] + description:[pe artist] + notificationName:@"Stream Changed" + iconData:nil + priority:0 + isSticky:NO + clickContext:nil]; +} + +- (void)playbackDidPause:(NSNotification *)notification +{ + if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) { + [scrobbler pause]; + } +} + +- (void)playbackDidResume:(NSNotification *)notification +{ + if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) { + [scrobbler resume]; + } +} + +- (void)playbackDidStop:(NSNotification *)notification +{ + if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) { + [scrobbler stop]; + } +} + +- (NSDictionary *) registrationDictionaryForGrowl +{ + NSArray *notifications = [NSArray arrayWithObjects:@"Stream Changed", nil]; + + return [NSDictionary dictionaryWithObjectsAndKeys: + @"Cog", GROWL_APP_NAME, + notifications, GROWL_NOTIFICATIONS_ALL, + notifications, GROWL_NOTIFICATIONS_DEFAULT, + nil]; +} + +@end