2009-03-05 14:04:16 -03:00
|
|
|
//
|
|
|
|
// PlaybackEventController.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 3/5/09.
|
|
|
|
// Copyright 2009 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
2014-12-04 02:36:55 -03:00
|
|
|
// New Notification Center code shamelessly based off this:
|
|
|
|
// https://github.com/kbhomes/radiant-player-mac/tree/master/radiant-player-mac/Notifications
|
2009-03-05 14:04:16 -03:00
|
|
|
|
|
|
|
#import "PlaybackEventController.h"
|
|
|
|
|
|
|
|
#import "AudioScrobbler.h"
|
2009-03-06 01:37:44 -03:00
|
|
|
#import "PlaylistEntry.h"
|
2009-03-05 14:04:16 -03:00
|
|
|
|
|
|
|
@implementation PlaybackEventController
|
|
|
|
|
|
|
|
- (void)initDefaults
|
|
|
|
{
|
|
|
|
NSDictionary *defaultsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithBool:YES], @"enableAudioScrobbler",
|
|
|
|
[NSNumber numberWithBool:NO], @"automaticallyLaunchLastFM",
|
2014-12-04 02:36:55 -03:00
|
|
|
[NSNumber numberWithBool:YES], @"notifications.enable",
|
|
|
|
[NSNumber numberWithBool:NO], @"notifications.use-growl",
|
|
|
|
[NSNumber numberWithBool:YES], @"notifications.itunes-style",
|
|
|
|
[NSNumber numberWithBool:YES], @"notifications.show-album-art",
|
2009-03-05 14:04:16 -03:00
|
|
|
nil];
|
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDictionary];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
[self initDefaults];
|
|
|
|
|
|
|
|
queue = [[NSOperationQueue alloc] init];
|
2009-03-06 01:37:44 -03:00
|
|
|
[queue setMaxConcurrentOperationCount:1];
|
|
|
|
|
2009-03-05 14:04:16 -03:00
|
|
|
scrobbler = [[AudioScrobbler alloc] init];
|
2014-12-04 02:36:55 -03:00
|
|
|
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
|
2009-03-05 14:04:16 -03:00
|
|
|
[GrowlApplicationBridge setGrowlDelegate:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[queue release];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-03-06 01:37:44 -03:00
|
|
|
- (void)performPlaybackDidBeginActions:(PlaylistEntry *)pe
|
2009-03-05 14:04:16 -03:00
|
|
|
{
|
2014-12-04 02:36:55 -03:00
|
|
|
if (NO == [pe error]) {
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
if ([defaults boolForKey:@"notifications.enable"]) {
|
|
|
|
if([defaults boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler start:pe];
|
|
|
|
if ([AudioScrobbler isRunning]) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([defaults boolForKey:@"notifications.use-growl"]) {
|
|
|
|
// Note: We don't want to send a growl notification on resume.
|
|
|
|
[GrowlApplicationBridge notifyWithTitle:[pe title]
|
|
|
|
description:[pe artist]
|
|
|
|
notificationName:@"Stream Changed"
|
|
|
|
iconData:[pe albumArtInternal]
|
|
|
|
priority:0
|
|
|
|
isSticky:NO
|
|
|
|
clickContext:nil];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NSUserNotification *notif = [[NSUserNotification alloc] init];
|
|
|
|
notif.title = [pe title];
|
|
|
|
|
|
|
|
if ([defaults boolForKey:@"notifications.itunes-style"]) {
|
|
|
|
notif.subtitle = [NSString stringWithFormat:@"%@ - %@", [pe artist], [pe album]];
|
|
|
|
[notif setValue:@YES forKey:@"_showsButtons"];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
notif.informativeText = [NSString stringWithFormat:@"%@ - %@", [pe artist], [pe album]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([notif respondsToSelector:@selector(setContentImage:)]) {
|
|
|
|
if ([defaults boolForKey:@"notifications.show-album-art"] && [pe albumArtInternal]) {
|
|
|
|
NSImage *image = [pe albumArt];
|
|
|
|
|
|
|
|
if ([defaults boolForKey:@"notifications.itunes-style"]) {
|
|
|
|
[notif setValue:image forKey:@"_identityImage"];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
notif.contentImage = image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notif.actionButtonTitle = @"Skip";
|
|
|
|
|
|
|
|
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notif];
|
|
|
|
}
|
|
|
|
}
|
2009-03-05 14:04:16 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 01:37:44 -03:00
|
|
|
- (void)performPlaybackDidPauseActions
|
2009-03-05 14:04:16 -03:00
|
|
|
{
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler pause];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 01:37:44 -03:00
|
|
|
- (void)performPlaybackDidResumeActions
|
2009-03-05 14:04:16 -03:00
|
|
|
{
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler resume];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 01:37:44 -03:00
|
|
|
- (void)performPlaybackDidStopActions
|
2009-03-05 14:04:16 -03:00
|
|
|
{
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler stop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 01:37:44 -03: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];
|
2013-10-11 12:35:57 -03:00
|
|
|
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.enableGrowlMist" options:0 context:nil];
|
|
|
|
|
|
|
|
[self toggleGrowlMist];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) toggleGrowlMist
|
|
|
|
{
|
|
|
|
BOOL enableMist = [[NSUserDefaults standardUserDefaults] boolForKey:@"enableGrowlMist"];
|
|
|
|
[GrowlApplicationBridge setShouldUseBuiltInNotifications:enableMist];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) observeValueForKeyPath:(NSString *)keyPath
|
|
|
|
ofObject:(id)object
|
|
|
|
change:(NSDictionary *)change
|
|
|
|
context:(void *)context
|
|
|
|
{
|
|
|
|
if ([keyPath isEqualToString:@"values.enableGrowlMist"]) {
|
|
|
|
[self toggleGrowlMist];
|
|
|
|
}
|
2009-03-06 01:37:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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 14:04:16 -03: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];
|
|
|
|
}
|
|
|
|
|
2014-12-04 02:36:55 -03:00
|
|
|
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
|
|
|
|
{
|
|
|
|
switch (notification.activationType)
|
|
|
|
{
|
|
|
|
case NSUserNotificationActivationTypeActionButtonClicked:
|
|
|
|
[playbackController next:self];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSUserNotificationActivationTypeContentsClicked:
|
|
|
|
{
|
|
|
|
NSWindow *window = [[NSUserDefaults standardUserDefaults] boolForKey:@"miniMode"] ? miniWindow : mainWindow;
|
|
|
|
|
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
|
|
|
[window makeKeyAndOrderFront:self];
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 14:04:16 -03:00
|
|
|
@end
|