SoundNote/Classes/Watchers/MGMITunesWatcher.m
2011-03-03 10:29:24 -06:00

59 lines
3.1 KiB
Objective-C

//
// MGMITunesWatcher.m
// SoundNote
//
// Created by Mr. Gecko on 2/15/11.
// Copyright (c) 2011 Mr. Gecko's Media (James Coleman). All rights reserved. http://mrgeckosmedia.com/
//
#import "MGMITunesWatcher.h"
#import "MGMController.h"
#import <EyeTunes/EyeTunes.h>
NSString * const MGMTName = @"Name";
NSString * const MGMTArtist = @"Artist";
@implementation MGMITunesWatcher
- (id)init {
if ((self = [super init])) {
NSDistributedNotificationCenter *notificationCenter = [NSDistributedNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(itunesActivity:) name:@"com.apple.iTunes.playerInfo" object:nil];
[notificationCenter addObserver:self selector:@selector(itunesSaved:) name:@"com.apple.iTunes.sourceSaved" object:nil];
}
return self;
}
- (void)dealloc {
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)itunesActivity:(NSNotification *)theNotification {
NSString *playerState = [[theNotification userInfo] objectForKey:@"Player State"];
ETTrack *currentTrack = [[EyeTunes sharedInstance] currentTrack];
NSImage *image = nil;
NSArray *artwork = [currentTrack artwork];
if ([artwork count]>0)
image = [artwork objectAtIndex:0];
else
image = [[NSWorkspace sharedWorkspace] iconForFile:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes"]];
NSString *trackName = @"";
if ([[theNotification userInfo] objectForKey:MGMTName]!=nil && ![[[theNotification userInfo] objectForKey:MGMTName] isEqual:@""])
trackName = [[theNotification userInfo] objectForKey:MGMTName];
if ([[theNotification userInfo] objectForKey:MGMTArtist]!=nil && ![[[theNotification userInfo] objectForKey:MGMTArtist] isEqual:@""]) {
trackName = [trackName stringByAppendingFormat:@" by %@", [[theNotification userInfo] objectForKey:MGMTArtist]];
}
if ([playerState isEqualToString:@"Playing"]) {
[[MGMController sharedController] startNotificationWithInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"itunesplaying", MGMNName, @"Now Playing", MGMNTitle, trackName, MGMNDescription, image, MGMNIcon, nil]];
} else if ([playerState isEqualToString:@"Paused"]) {
[[MGMController sharedController] startNotificationWithInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"itunespaused", MGMNName, @"Paused", MGMNTitle, trackName, MGMNDescription, image, MGMNIcon, nil]];
} else {
[[MGMController sharedController] startNotificationWithInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"itunesstopped", MGMNName, @"Stopped", MGMNTitle, @"iTunes is no longer playing.", MGMNDescription, image, MGMNIcon, nil]];
}
}
- (void)itunesSaved:(NSNotification *)theNotification {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
[[MGMController sharedController] startNotificationWithInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"itunessaved", MGMNName, @"iTunes Saved", MGMNTitle, [NSString stringWithFormat:@"iTunes saved %@.", [[theNotification userInfo] objectForKey:MGMTName]], MGMNDescription, [[NSWorkspace sharedWorkspace] iconForFile:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes"]], MGMNIcon, nil]];
[pool drain];
}
@end