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"
|
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";
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
typedef NS_ENUM(NSInteger, TrackStatus) { TrackPlaying,
|
|
|
|
TrackPaused,
|
|
|
|
TrackStopped };
|
2021-02-26 20:01:48 +00:00
|
|
|
|
|
|
|
@implementation PlaybackEventController {
|
2022-02-07 05:49:27 +00:00
|
|
|
AudioScrobbler *scrobbler;
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
NSOperationQueue *queue;
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
PlaylistEntry *entry;
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
Boolean didGainUN API_AVAILABLE(macosx(10.14));
|
2021-02-26 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initDefaults {
|
2022-02-07 05:49:27 +00:00
|
|
|
NSDictionary *defaultsDictionary = @{
|
|
|
|
@"enableAudioScrobbler": @YES,
|
|
|
|
@"automaticallyLaunchLastFM": @NO,
|
|
|
|
@"notifications.enable": @YES,
|
|
|
|
@"notifications.itunes-style": @YES,
|
|
|
|
@"notifications.show-album-art": @YES
|
|
|
|
};
|
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDictionary];
|
2009-03-05 17:04:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (id)init {
|
2022-02-07 05:49:27 +00:00
|
|
|
self = [super init];
|
|
|
|
if(self) {
|
|
|
|
[self initDefaults];
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
didGainUN = NO;
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(@available(macOS 10.14, *)) {
|
|
|
|
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
|
[center
|
|
|
|
requestAuthorizationWithOptions:UNAuthorizationOptionAlert
|
|
|
|
completionHandler:^(BOOL granted, NSError *_Nullable error) {
|
|
|
|
self->didGainUN = granted;
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(granted) {
|
|
|
|
UNNotificationAction *skipAction = [UNNotificationAction
|
|
|
|
actionWithIdentifier:@"skip"
|
|
|
|
title:@"Skip"
|
|
|
|
options:UNNotificationActionOptionNone];
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
UNNotificationCategory *playCategory = [UNNotificationCategory
|
|
|
|
categoryWithIdentifier:@"play"
|
|
|
|
actions:@[skipAction]
|
|
|
|
intentIdentifiers:@[]
|
|
|
|
options:UNNotificationCategoryOptionNone];
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[center setNotificationCategories:
|
|
|
|
[NSSet setWithObject:playCategory]];
|
|
|
|
}
|
|
|
|
}];
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[center setDelegate:self];
|
|
|
|
}
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
queue = [[NSOperationQueue alloc] init];
|
|
|
|
[queue setMaxConcurrentOperationCount:1];
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
scrobbler = [[AudioScrobbler alloc] init];
|
|
|
|
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
entry = nil;
|
|
|
|
}
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
return self;
|
2009-03-05 17:04:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 08:51:35 +00:00
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
|
willPresentNotification:(UNNotification *)notification
|
2021-02-26 20:01:48 +00:00
|
|
|
withCompletionHandler:
|
2022-02-07 05:49:27 +00:00
|
|
|
(void (^)(UNNotificationPresentationOptions options))completionHandler
|
|
|
|
API_AVAILABLE(macos(10.14)) {
|
|
|
|
UNNotificationPresentationOptions presentationOptions = UNNotificationPresentationOptionAlert;
|
2021-02-26 20:01:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
completionHandler(presentationOptions);
|
2020-03-21 08:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
2022-02-07 05:49:27 +00:00
|
|
|
didReceiveNotificationResponse:(UNNotificationResponse *)response
|
|
|
|
withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(macos(10.14)) {
|
|
|
|
if([[response actionIdentifier] isEqualToString:@"skip"]) {
|
|
|
|
[playbackController next:self];
|
|
|
|
}
|
2020-03-21 08:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (NSDictionary *)fillNotificationDictionary:(PlaylistEntry *)pe status:(TrackStatus)status {
|
2022-02-07 05:49:27 +00:00
|
|
|
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
|
|
|
|
if(pe == nil) return dict;
|
|
|
|
|
|
|
|
[dict setObject:[[pe URL] absoluteString] forKey:TrackPath];
|
|
|
|
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 trackText] forKey:TrackNumber];
|
|
|
|
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];
|
|
|
|
|
|
|
|
return dict;
|
2014-12-04 06:13:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)performPlaybackDidBeginActions:(PlaylistEntry *)pe {
|
2022-02-07 05:49:27 +00:00
|
|
|
if(NO == [pe error]) {
|
|
|
|
entry = pe;
|
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName:TrackNotification
|
|
|
|
object:nil
|
|
|
|
userInfo:[self fillNotificationDictionary:pe status:TrackPlaying]
|
|
|
|
deliverImmediately:YES];
|
|
|
|
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
if([defaults boolForKey:@"notifications.enable"]) {
|
|
|
|
if([defaults boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler start:pe];
|
|
|
|
if([AudioScrobbler isRunning]) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
if([defaults boolForKey:@"notifications.show-album-art"] &&
|
|
|
|
[pe albumArt]) {
|
|
|
|
NSError *error = nil;
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
NSURL *tmpSubFolderURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()]
|
|
|
|
URLByAppendingPathComponent:@"cog-artworks-cache"
|
|
|
|
isDirectory:true];
|
|
|
|
if([fileManager createDirectoryAtPath:[tmpSubFolderURL path]
|
|
|
|
withIntermediateDirectories:true
|
|
|
|
attributes:nil
|
|
|
|
error:&error]) {
|
|
|
|
NSString *tmpFileName =
|
|
|
|
[[NSProcessInfo.processInfo globallyUniqueString]
|
|
|
|
stringByAppendingString:@".jpg"];
|
|
|
|
NSURL *fileURL =
|
|
|
|
[tmpSubFolderURL URLByAppendingPathComponent:tmpFileName];
|
|
|
|
NSImage *image = [pe albumArt];
|
|
|
|
CGImageRef cgRef = [image CGImageForProposedRect:NULL
|
|
|
|
context:nil
|
|
|
|
hints:nil];
|
|
|
|
|
|
|
|
if(cgRef) {
|
|
|
|
NSBitmapImageRep *newRep =
|
|
|
|
[[NSBitmapImageRep alloc] initWithCGImage:cgRef];
|
|
|
|
NSData *jpgData = [newRep
|
|
|
|
representationUsingType:NSBitmapImageFileTypeJPEG
|
|
|
|
properties:@{ NSImageCompressionFactor: @0.5f }];
|
|
|
|
[jpgData writeToURL:fileURL atomically:YES];
|
|
|
|
|
|
|
|
UNNotificationAttachment *icon =
|
|
|
|
[UNNotificationAttachment attachmentWithIdentifier:@"art"
|
|
|
|
URL:fileURL
|
|
|
|
options:nil
|
|
|
|
error:&error];
|
|
|
|
if(error) {
|
|
|
|
// We have size limit of 10MB per image attachment.
|
|
|
|
NSLog(@"%@", error.localizedDescription);
|
|
|
|
} else {
|
|
|
|
content.attachments = @[icon];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UNNotificationRequest *request =
|
|
|
|
[UNNotificationRequest requestWithIdentifier:@"PlayTrack"
|
|
|
|
content:content
|
|
|
|
trigger:nil];
|
|
|
|
|
|
|
|
[center addNotificationRequest:request
|
|
|
|
withCompletionHandler:^(NSError *_Nullable error) {
|
|
|
|
NSLog(@"%@", error.localizedDescription);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NSUserNotification *notif = [[NSUserNotification alloc] init];
|
|
|
|
notif.title = [pe title];
|
|
|
|
|
|
|
|
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 = @"";
|
|
|
|
}
|
|
|
|
|
|
|
|
if([defaults boolForKey:@"notifications.itunes-style"]) {
|
|
|
|
notif.subtitle = subtitle;
|
|
|
|
[notif setValue:@YES forKey:@"_showsButtons"];
|
|
|
|
} else {
|
|
|
|
notif.informativeText = subtitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)performPlaybackDidPauseActions {
|
2022-02-07 05:49:27 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName:TrackNotification
|
|
|
|
object:nil
|
|
|
|
userInfo:[self fillNotificationDictionary:entry status:TrackPaused]
|
|
|
|
deliverImmediately:YES];
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler pause];
|
|
|
|
}
|
2009-03-05 17:04:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)performPlaybackDidResumeActions {
|
2022-02-07 05:49:27 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName:TrackNotification
|
|
|
|
object:nil
|
|
|
|
userInfo:[self fillNotificationDictionary:entry status:TrackPlaying]
|
|
|
|
deliverImmediately:YES];
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler resume];
|
|
|
|
}
|
2009-03-05 17:04:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)performPlaybackDidStopActions {
|
2022-02-07 05:49:27 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName:TrackNotification
|
|
|
|
object:nil
|
|
|
|
userInfo:[self fillNotificationDictionary:entry status:TrackStopped]
|
|
|
|
deliverImmediately:YES];
|
|
|
|
entry = nil;
|
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
|
|
|
|
[scrobbler stop];
|
|
|
|
}
|
2013-10-11 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)awakeFromNib {
|
2022-02-07 05:49:27 +00:00
|
|
|
[[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];
|
2009-03-06 04:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)playbackDidBegin:(NSNotification *)notification {
|
2022-02-07 05:49:27 +00:00
|
|
|
NSOperation *op = [NSBlockOperation blockOperationWithBlock:^{
|
|
|
|
[self performPlaybackDidBeginActions:(PlaylistEntry *)[notification object]];
|
|
|
|
}];
|
|
|
|
[queue addOperation:op];
|
2009-03-06 04:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)playbackDidPause:(NSNotification *)notification {
|
2022-02-07 05:49:27 +00:00
|
|
|
NSOperation *op = [NSBlockOperation blockOperationWithBlock:^{
|
|
|
|
[self performPlaybackDidPauseActions];
|
|
|
|
}];
|
|
|
|
[queue addOperation:op];
|
2009-03-06 04:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)playbackDidResume:(NSNotification *)notification {
|
2022-02-07 05:49:27 +00:00
|
|
|
NSOperation *op = [NSBlockOperation blockOperationWithBlock:^{
|
|
|
|
[self performPlaybackDidResumeActions];
|
|
|
|
}];
|
|
|
|
[queue addOperation:op];
|
2009-03-06 04:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)playbackDidStop:(NSNotification *)notification {
|
2022-02-07 05:49:27 +00:00
|
|
|
NSOperation *op = [NSBlockOperation blockOperationWithBlock:^{
|
|
|
|
[self performPlaybackDidStopActions];
|
|
|
|
}];
|
|
|
|
[queue addOperation:op];
|
2009-03-06 04:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:01:48 +00:00
|
|
|
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
|
|
|
|
didActivateNotification:(NSUserNotification *)notification {
|
2022-02-07 05:49:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-12-04 05:36:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 17:04:16 +00:00
|
|
|
@end
|