diff --git a/Application/PlaybackEventController.h b/Application/PlaybackEventController.h index 9ef8b2200..d322198b4 100644 --- a/Application/PlaybackEventController.h +++ b/Application/PlaybackEventController.h @@ -10,10 +10,13 @@ #import #import "PlaybackController.h" +#import "PlaylistEntry.h" @class AudioScrobbler; @interface PlaybackEventController : NSObject { NSOperationQueue *queue; + + PlaylistEntry *entry; AudioScrobbler *scrobbler; diff --git a/Application/PlaybackEventController.m b/Application/PlaybackEventController.m index d3bde5957..850ecfc33 100644 --- a/Application/PlaybackEventController.m +++ b/Application/PlaybackEventController.m @@ -11,7 +11,19 @@ #import "PlaybackEventController.h" #import "AudioScrobbler.h" -#import "PlaylistEntry.h" + +NSString *TrackPlaying = @"org.cogx.Cog-Track-Playing"; +NSString *TrackStopped = @"org.cogx.Cog-Track-Stopped"; +NSString *TrackPaused = @"org.cogx.Cog-Track-Paused"; + +NSString *TrackArtist = @"artist"; +NSString *TrackAlbum = @"album"; +NSString *TrackTitle = @"title"; +NSString *TrackGenre = @"genre"; +NSString *TrackComposer = @"composer"; +NSString *TrackNumber = @"trackNumber"; +NSString *TrackLength = @"length"; +NSString *TrackCurrentTime = @"currentTime"; @implementation PlaybackEventController @@ -42,6 +54,8 @@ scrobbler = [[AudioScrobbler alloc] init]; [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; [GrowlApplicationBridge setGrowlDelegate:self]; + + entry = nil; } return self; @@ -50,13 +64,35 @@ - (void)dealloc { [queue release]; + + [entry release]; [super dealloc]; } +- (NSDictionary *)fillNotificationDictionary:(PlaylistEntry *)pe +{ + NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; + + if ([pe title]) [dict setObject:[pe title] forKey:TrackTitle]; + if ([pe artist]) [dict setObject:[pe artist] forKey:TrackArtist]; + if ([pe album]) [dict setObject:[pe album] forKey:TrackAlbum]; + if ([pe genre]) [dict setObject:[pe genre] forKey:TrackGenre]; + if ([pe track]) [dict setObject:[pe track] forKey:TrackNumber]; + if ([pe lengthText]) [dict setObject:[pe lengthText] forKey:TrackLength]; + if ([pe positionText]) [dict setObject:[pe positionText] forKey:TrackCurrentTime]; + + return dict; +} + - (void)performPlaybackDidBeginActions:(PlaylistEntry *)pe { if (NO == [pe error]) { + [entry release]; + entry = [pe retain]; + + [[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackPlaying object:nil userInfo:[self fillNotificationDictionary:pe]]; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ([defaults boolForKey:@"notifications.enable"]) { @@ -110,6 +146,7 @@ - (void)performPlaybackDidPauseActions { + [[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackPaused object:nil userInfo:[self fillNotificationDictionary:entry]]; if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) { [scrobbler pause]; } @@ -117,6 +154,7 @@ - (void)performPlaybackDidResumeActions { + [[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackPlaying object:nil userInfo:[self fillNotificationDictionary:entry]]; if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) { [scrobbler resume]; } @@ -124,6 +162,9 @@ - (void)performPlaybackDidStopActions { + [[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackStopped object:nil userInfo:[self fillNotificationDictionary:entry]]; + [entry release]; + entry = nil; if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) { [scrobbler stop]; } diff --git a/Playlist/PlaylistEntry.h b/Playlist/PlaylistEntry.h index 52a370183..143a9cb7c 100644 --- a/Playlist/PlaylistEntry.h +++ b/Playlist/PlaylistEntry.h @@ -73,6 +73,10 @@ @property(readonly) NSString *spam; +@property(readonly) NSString *positionText; + +@property(readonly) NSString *lengthText; + @property int index; @property int shuffleIndex; diff --git a/Playlist/PlaylistEntry.m b/Playlist/PlaylistEntry.m index dff80cb23..7086c08d2 100644 --- a/Playlist/PlaylistEntry.m +++ b/Playlist/PlaylistEntry.m @@ -92,6 +92,16 @@ return [NSSet setWithObjects:@"artist", @"title", @"album", @"track", @"totalFrames", @"currentPosition", @"bitrate", nil]; } ++ (NSSet *)keyPathsForValuesAffectingPositionText +{ + return [NSSet setWithObject:@"currentPosition"]; +} + ++ (NSSet *)keyPathsForValuesAffectingLengthText +{ + return [NSSet setWithObject:@"length"]; +} + + (NSSet *)keyPathsForValuesAffectingAlbumArt { return [NSSet setWithObject:@"albumArtInternal"]; @@ -234,6 +244,24 @@ return [elements componentsJoinedByString:@""]; } +@dynamic positionText; +- (NSString *)positionText +{ + SecondsFormatter *secondsFormatter = [[SecondsFormatter alloc] init]; + NSString *time = [secondsFormatter stringForObjectValue:[NSNumber numberWithFloat:currentPosition]]; + [secondsFormatter release]; + return time; +} + +@dynamic lengthText; +- (NSString *)lengthText +{ + SecondsFormatter *secondsFormatter = [[SecondsFormatter alloc] init]; + NSString *time = [secondsFormatter stringForObjectValue:[self length]]; + [secondsFormatter release]; + return time; +} + @synthesize albumArtInternal; @dynamic albumArt;