2009-03-05 17:04:16 +00:00
|
|
|
//
|
|
|
|
// PlaybackEventController.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 3/5/09.
|
|
|
|
// Copyright 2009 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "PlaybackEventController.h"
|
|
|
|
|
|
|
|
#import "AudioScrobbler.h"
|
|
|
|
#import "PlaybackController.h"
|
2009-03-06 04:37:44 +00:00
|
|
|
#import "PlaylistLoader.h"
|
|
|
|
#import "PlaylistEntry.h"
|
2009-03-05 17:04:16 +00:00
|
|
|
|
|
|
|
@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];
|
2009-03-06 04:37:44 +00:00
|
|
|
[queue setMaxConcurrentOperationCount:1];
|
|
|
|
|
2009-03-05 17:04:16 +00:00
|
|
|
scrobbler = [[AudioScrobbler alloc] init];
|
|
|
|
[GrowlApplicationBridge setGrowlDelegate:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[queue release];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:37:44 +00:00
|
|
|
- (void)performPlaybackDidBeginActions:(PlaylistEntry *)pe
|
2009-03-05 17:04:16 +00:00
|
|
|
{
|
2009-03-06 04:46:42 +00:00
|
|
|
// Race here, but the worst that could happen is we re-read the data
|
|
|
|
if ([pe metadataLoaded] != YES) {
|
|
|
|
[pe performSelectorOnMainThread:@selector(setMetadata:) withObject:[playlistLoader readEntryInfo:pe] waitUntilDone:YES];
|
|
|
|
}
|
2009-03-06 04:37:44 +00:00
|
|
|
|
2009-03-12 03:26:29 +00:00
|
|
|
if (NO == [pe error]) {
|
|
|
|
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:[[pe albumArt] TIFFRepresentation]
|
|
|
|
priority:0
|
|
|
|
isSticky:NO
|
|
|
|
clickContext:nil];
|
2009-03-05 17:04:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:37:44 +00:00
|
|
|
- (void)performPlaybackDidPauseActions
|
2009-03-05 17:04:16 +00:00
|
|
|
{
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler pause];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:37:44 +00:00
|
|
|
- (void)performPlaybackDidResumeActions
|
2009-03-05 17:04:16 +00:00
|
|
|
{
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler resume];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:37:44 +00:00
|
|
|
- (void)performPlaybackDidStopActions
|
2009-03-05 17:04:16 +00:00
|
|
|
{
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler stop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:37:44 +00:00
|
|
|
|
|
|
|
- (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
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidBeginActions:) object:[notification object]];
|
|
|
|
[queue addOperation:op];
|
|
|
|
[op release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackDidPause:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidPauseActions) object:nil];
|
|
|
|
[queue addOperation:op];
|
|
|
|
[op release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackDidResume:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidResumeActions) object:nil];
|
|
|
|
[queue addOperation:op];
|
|
|
|
[op release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackDidStop:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidStopActions) object:nil];
|
|
|
|
[queue addOperation:op];
|
|
|
|
[op release];
|
|
|
|
}
|
|
|
|
|
2009-03-05 17:04:16 +00:00
|
|
|
- (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
|