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.
|
|
|
|
//
|
2014-12-04 05:36:55 +00: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 17:04:16 +00:00
|
|
|
|
|
|
|
#import "PlaybackEventController.h"
|
|
|
|
|
|
|
|
#import "AudioScrobbler.h"
|
2014-12-04 06:13:27 +00:00
|
|
|
|
2014-12-04 23:22:27 +00:00
|
|
|
NSString *TrackNotification = @"com.apple.iTunes.playerInfo";
|
|
|
|
|
|
|
|
NSString *TrackArtist = @"Artist";
|
|
|
|
NSString *TrackAlbum = @"Album";
|
|
|
|
NSString *TrackTitle = @"Name";
|
|
|
|
NSString *TrackGenre = @"Genre";
|
|
|
|
NSString *TrackNumber = @"Track Number";
|
|
|
|
NSString *TrackLength = @"Total Time";
|
|
|
|
NSString *TrackPath = @"Location";
|
|
|
|
NSString *TrackState = @"Player State";
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TrackPlaying,
|
|
|
|
TrackPaused,
|
|
|
|
TrackStopped
|
|
|
|
} TrackStatus;
|
2009-03-05 17:04:16 +00:00
|
|
|
|
|
|
|
@implementation PlaybackEventController
|
|
|
|
|
|
|
|
- (void)initDefaults
|
|
|
|
{
|
|
|
|
NSDictionary *defaultsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithBool:YES], @"enableAudioScrobbler",
|
|
|
|
[NSNumber numberWithBool:NO], @"automaticallyLaunchLastFM",
|
2014-12-04 05:36:55 +00:00
|
|
|
[NSNumber numberWithBool:YES], @"notifications.enable",
|
|
|
|
[NSNumber numberWithBool:YES], @"notifications.itunes-style",
|
|
|
|
[NSNumber numberWithBool:YES], @"notifications.show-album-art",
|
2009-03-05 17:04:16 +00:00
|
|
|
nil];
|
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDictionary];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
[self initDefaults];
|
2020-03-21 08:51:35 +00:00
|
|
|
|
|
|
|
didGainUN = NO;
|
|
|
|
|
|
|
|
if (@available(macOS 10.14,*)) {
|
|
|
|
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
|
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert
|
|
|
|
completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
|
|
self->didGainUN = granted;
|
|
|
|
|
|
|
|
if (granted) {
|
|
|
|
UNNotificationAction * skipAction = [UNNotificationAction actionWithIdentifier:@"skip" title:@"Skip" options:UNNotificationActionOptionNone];
|
|
|
|
|
|
|
|
UNNotificationCategory* playCategory = [UNNotificationCategory
|
|
|
|
categoryWithIdentifier:@"play"
|
|
|
|
actions:@[skipAction]
|
|
|
|
intentIdentifiers:@[]
|
|
|
|
options:UNNotificationCategoryOptionNone];
|
|
|
|
|
|
|
|
[center setNotificationCategories:[NSSet setWithObjects:playCategory, nil]];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
[center setDelegate:self];
|
|
|
|
}
|
2009-03-05 17:04:16 +00:00
|
|
|
|
|
|
|
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];
|
2014-12-04 05:36:55 +00:00
|
|
|
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
|
2014-12-04 06:13:27 +00:00
|
|
|
|
|
|
|
entry = nil;
|
2009-03-05 17:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-21 08:51:35 +00:00
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
|
willPresentNotification:(UNNotification *)notification
|
|
|
|
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(macos(10.14)){
|
|
|
|
UNNotificationPresentationOptions presentationOptions =
|
|
|
|
UNNotificationPresentationOptionAlert;
|
|
|
|
|
|
|
|
completionHandler(presentationOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
|
didReceiveNotificationResponse:(UNNotificationResponse *)response
|
|
|
|
withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(macos(10.14)){
|
|
|
|
if ([[response actionIdentifier] isEqualToString:@"skip"]) {
|
|
|
|
[playbackController next:self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:22:27 +00:00
|
|
|
- (NSDictionary *)fillNotificationDictionary:(PlaylistEntry *)pe status:(TrackStatus)status
|
2014-12-04 06:13:27 +00:00
|
|
|
{
|
2014-12-04 23:22:27 +00:00
|
|
|
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
|
2014-12-04 06:13:27 +00:00
|
|
|
|
2014-12-04 23:22:27 +00:00
|
|
|
[dict setObject:[[pe URL] absoluteString] forKey:TrackPath];
|
2014-12-04 06:13:27 +00:00
|
|
|
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];
|
2014-12-04 06:50:59 +00:00
|
|
|
if ([pe track]) [dict setObject:[NSString stringWithFormat:@"%@",[pe track]] forKey:TrackNumber];
|
2014-12-04 23:22:27 +00:00
|
|
|
if ([pe length]) [dict setObject:[NSNumber numberWithInteger:(NSInteger)([[pe length] doubleValue] * 1000.0)] forKey:TrackLength];
|
|
|
|
|
|
|
|
NSString * state = nil;
|
|
|
|
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case TrackPlaying: state = @"Playing"; break;
|
|
|
|
case TrackPaused: state = @"Paused"; break;
|
|
|
|
case TrackStopped: state = @"Stopped"; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
[dict setObject:state forKey:TrackState];
|
2014-12-04 06:13:27 +00:00
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:37:44 +00:00
|
|
|
- (void)performPlaybackDidBeginActions:(PlaylistEntry *)pe
|
2009-03-05 17:04:16 +00:00
|
|
|
{
|
2014-12-04 05:36:55 +00:00
|
|
|
if (NO == [pe error]) {
|
2016-05-05 20:05:39 +00:00
|
|
|
entry = pe;
|
2014-12-04 06:13:27 +00:00
|
|
|
|
2014-12-04 23:22:27 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackNotification object:nil userInfo:[self fillNotificationDictionary:pe status:TrackPlaying] deliverImmediately:YES];
|
2014-12-04 06:13:27 +00:00
|
|
|
|
2014-12-04 05:36:55 +00:00
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
if ([defaults boolForKey:@"notifications.enable"]) {
|
|
|
|
if([defaults boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler start:pe];
|
|
|
|
if ([AudioScrobbler isRunning]) return;
|
|
|
|
}
|
|
|
|
|
2020-03-21 08:51:35 +00:00
|
|
|
if (@available(macOS 10.14,*))
|
|
|
|
{
|
|
|
|
if (didGainUN) {
|
|
|
|
UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
|
|
|
|
|
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
|
|
|
|
|
|
|
|
content.title = @"Now Playing";
|
|
|
|
|
|
|
|
NSString *subtitle;
|
|
|
|
if ([pe artist] && [pe album]) {
|
|
|
|
subtitle = [NSString stringWithFormat:@"%@ - %@", [pe artist], [pe album]];
|
|
|
|
} else if ([pe artist]) {
|
|
|
|
subtitle = [pe artist];
|
|
|
|
} else if ([pe album]) {
|
|
|
|
subtitle = [pe album];
|
|
|
|
} else {
|
|
|
|
subtitle = @"";
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *body = [NSString stringWithFormat:@"%@\n%@", [pe title], subtitle];
|
|
|
|
content.body = body;
|
|
|
|
content.sound = nil;
|
|
|
|
content.categoryIdentifier = @"play";
|
|
|
|
|
|
|
|
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"PlayTrack" content:content trigger:nil];
|
|
|
|
|
|
|
|
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
|
|
|
|
NSLog(@"%@", error.localizedDescription);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-10-10 22:47:46 +00:00
|
|
|
{
|
2014-12-04 05:36:55 +00:00
|
|
|
NSUserNotification *notif = [[NSUserNotification alloc] init];
|
|
|
|
notif.title = [pe title];
|
|
|
|
|
2018-06-04 01:39:07 +00:00
|
|
|
NSString *subtitle;
|
|
|
|
if ([pe artist] && [pe album]) {
|
|
|
|
subtitle = [NSString stringWithFormat:@"%@ - %@", [pe artist], [pe album]];
|
|
|
|
} else if ([pe artist]) {
|
|
|
|
subtitle = [pe artist];
|
|
|
|
} else if ([pe album]) {
|
|
|
|
subtitle = [pe album];
|
|
|
|
} else {
|
|
|
|
subtitle = @"";
|
|
|
|
}
|
|
|
|
|
2014-12-04 05:36:55 +00:00
|
|
|
if ([defaults boolForKey:@"notifications.itunes-style"]) {
|
2018-06-04 01:39:07 +00:00
|
|
|
notif.subtitle = subtitle;
|
2014-12-04 05:36:55 +00:00
|
|
|
[notif setValue:@YES forKey:@"_showsButtons"];
|
|
|
|
}
|
|
|
|
else {
|
2018-06-04 01:39:07 +00:00
|
|
|
notif.informativeText = subtitle;
|
2014-12-04 05:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 17:04:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:37:44 +00:00
|
|
|
- (void)performPlaybackDidPauseActions
|
2009-03-05 17:04:16 +00:00
|
|
|
{
|
2014-12-04 23:22:27 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackNotification object:nil userInfo:[self fillNotificationDictionary:entry status:TrackPaused] deliverImmediately:YES];
|
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
|
|
|
{
|
2014-12-04 23:22:27 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackNotification object:nil userInfo:[self fillNotificationDictionary:entry status:TrackPlaying] deliverImmediately:YES];
|
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
|
|
|
{
|
2014-12-04 23:22:27 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:TrackNotification object:nil userInfo:[self fillNotificationDictionary:entry status:TrackStopped] deliverImmediately:YES];
|
2014-12-04 06:13:27 +00:00
|
|
|
entry = nil;
|
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];
|
2013-10-11 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) observeValueForKeyPath:(NSString *)keyPath
|
|
|
|
ofObject:(id)object
|
|
|
|
change:(NSDictionary *)change
|
|
|
|
context:(void *)context
|
|
|
|
{
|
2009-03-06 04:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackDidBegin:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidBeginActions:) object:[notification object]];
|
|
|
|
[queue addOperation:op];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackDidPause:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidPauseActions) object:nil];
|
|
|
|
[queue addOperation:op];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackDidResume:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidResumeActions) object:nil];
|
|
|
|
[queue addOperation:op];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackDidStop:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performPlaybackDidStopActions) object:nil];
|
|
|
|
[queue addOperation:op];
|
|
|
|
}
|
|
|
|
|
2014-12-04 05:36:55 +00: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 17:04:16 +00:00
|
|
|
@end
|