Skip to content

Commit

Permalink
Actual error handling
Browse files Browse the repository at this point in the history
Also tidied stuff up more.
  • Loading branch information
mxcl committed Jun 19, 2010
1 parent b1cb357 commit 6d779ff
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 178 deletions.
4 changes: 1 addition & 3 deletions HistoryMenuController.m
Expand Up @@ -41,11 +41,9 @@ -(void)insert:(NSDictionary*)track
if([item isEnabled] == false)
[menu removeItem:item];

NSURL* url = [Lastfm urlForTrack:[track objectForKey:@"Name"] by:[track objectForKey:@"Artist"]];

item = [[NSMenuItem alloc] initWithTitle:track.prettyTitle action:@selector(clicked:) keyEquivalent:@""];
[item setTarget:self];
[item setRepresentedObject:url];
[item setRepresentedObject:track.url];
[menu insertItem:item atIndex:0];
[item release];

Expand Down
9 changes: 2 additions & 7 deletions ITunesListener.m
Expand Up @@ -153,11 +153,6 @@ -(void)submit
[lastfm scrobble:track startTime:start_time];
}

-(void)updateNowPlaying:(NSDictionary*)dict
{
[lastfm updateNowPlaying:dict];
}

-(void)start
{
[self announce:TrackStarted];
Expand All @@ -169,8 +164,8 @@ -(void)start

// we wait a second so that we don't spam Last.fm and so that stuff like
// Growl (for auth) doesn't fill the screen when you skip-skip-skip
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(updateNowPlaying:) withObject:track afterDelay:1];
[NSObject cancelPreviousPerformRequestsWithTarget:lastfm];
[lastfm performSelector:@selector(updateNowPlaying:) withObject:track afterDelay:1];
}

-(void)load_album_art
Expand Down
8 changes: 2 additions & 6 deletions NSDictionary+Track.h
Expand Up @@ -2,7 +2,6 @@
#import <Cocoa/Cocoa.h>

@interface NSDictionary (mxcl)

@property(readonly) bool unrated;
@property(readonly) int64_t pid;
@property(readonly) int rating;
Expand All @@ -11,16 +10,13 @@
@property(readonly) NSString* album;
@property(readonly) int playerState;
@property(readonly) NSString* prettyTitle;

@property(readonly) NSURL* url;
@property(readonly) unsigned int duration;
@end


@interface NSMutableDictionary (mxcl)

-(void)setArtist:(NSString*)artist;
-(void)setTitle:(NSString*)title;
-(void)setAlbum:(NSString*)album;

-(bool)isEqualToTrack:(NSDictionary*)track;

@end
18 changes: 16 additions & 2 deletions NSDictionary+Track.m
@@ -1,5 +1,7 @@
// Created by Max Howell on 05/06/2010.
#import "NSDictionary+Track.h"
#import "lastfm.h"


@implementation NSDictionary (mxcl)

Expand All @@ -8,7 +10,7 @@ -(NSString*)title { return [self objectForKey:@"Name"]; }
-(NSString*)album { return [self objectForKey:@"Album"]; }
-(int64_t)pid { return [[self objectForKey:@"PersistentID"] longLongValue]; }
-(int)rating { return [[self objectForKey:@"Rating"] intValue]; }

-(uint)duration { return [[self objectForKey:@"Total Time"] longLongValue] / 1000; }
-(int)playerState
{
NSString* s = [self objectForKey:@"Player State"];
Expand All @@ -34,6 +36,16 @@ -(NSString*)prettyTitle
return [s autorelease];
}

-(NSURL*)url
{
//TODO localise URL, maybe auth ws gives that? otherwise OS level locale
NSMutableString* path = [[@"http://www.last.fm/music/" mutableCopy] autorelease];
[path appendString:[Lastfm urlEncode:self.artist]];
[path appendString:@"/_/"];
[path appendString:[Lastfm urlEncode:self.title]];
return [NSURL URLWithString:path];
}

@end


Expand All @@ -46,13 +58,15 @@ -(void)setAlbum:(NSString*)album { [self setObject:album forKey:@"Album"]; }
-(bool)isEqualToTrack:(NSDictionary*)track
{
id o = [self objectForKey:@"Album Art"];
bool b = false;
@try {
[self removeObjectForKey:@"Album Art"];
return [self isEqualToDictionary:track];
b = [self isEqualToDictionary:track];
}
@finally {
if (o) [self setObject:o forKey:@"Album Art"];
}
return b;
}

@end
18 changes: 9 additions & 9 deletions StatusItemController.m
Expand Up @@ -188,7 +188,7 @@ -(void)onPlayerInfo:(NSNotification*)userData
NSDictionary* dict = [userData userInfo];
uint transition = [[dict objectForKey:@"Transition"] unsignedIntValue];
NSString* name = [dict objectForKey:@"Name"];
uint const duration = [(NSNumber*)[dict objectForKey:@"Total Time"] longLongValue] / 1000;
uint const duration = [[dict objectForKey:@"Total Time"] longLongValue] / 1000;
NSString* notificationName = ASGrowlTrackResumed;

#define UPDATE_TITLE_MENU \
Expand Down Expand Up @@ -282,15 +282,16 @@ -(void)lastfm:(Lastfm*)lastfm requiresAuth:(NSURL*)url
identifier:ASGrowlAuthenticationRequired];
}

-(void)lastfm:(Lastfm*)lastfm error:(NSString*)message
-(void)lastfm:(Lastfm*)lastfm errorCode:(int)code errorMessage:(NSString*)message
{
[GrowlApplicationBridge notifyWithTitle:@"Error Occurred :("
[GrowlApplicationBridge notifyWithTitle:[NSString stringWithFormat:@"Error Code %d", code]
description:message
notificationName:ASGrowlErrorCommunication
iconData:nil
priority:2
isSticky:false
clickContext:nil];
clickContext:nil
identifier:[message stringByAppendingString:ASGrowlErrorCommunication]];
}

-(void)lastfm:(Lastfm*)lastfm metadata:(NSDictionary*)metadata betterdata:(NSDictionary*)betterdata
Expand All @@ -304,6 +305,7 @@ -(void)lastfm:(Lastfm*)lastfm metadata:(NSDictionary*)metadata betterdata:(NSDic
clickContext:nil];
}


-(void)growlNotificationWasClicked:(id)dict
{
if ([dict isKindOfClass:[NSString class]])
Expand All @@ -316,11 +318,10 @@ -(void)growlNotificationWasClicked:(id)dict
[self love:self];
else
[lastfm love:dict];
// need some kind of feedback
// TODO need some kind of feedback
}
else
[[NSWorkspace sharedWorkspace] openURL:[Lastfm urlForTrack:[dict objectForKey:@"Name"]
by:[dict objectForKey:@"Artist"]]];
[[NSWorkspace sharedWorkspace] openURL:[dict url]];
}

-(IBAction)love:(id)sender
Expand All @@ -332,8 +333,7 @@ -(IBAction)love:(id)sender

-(IBAction)tag:(id)sender
{
NSDictionary* t = listener.track;
NSURL* url = [Lastfm urlForTrack:t.title by:t.artist];
NSURL* url = listener.track.url;
NSString* path = [[url path] stringByAppendingPathComponent:@"+tags"];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:path relativeToURL:url]];
}
Expand Down
13 changes: 4 additions & 9 deletions lastfm.h
Expand Up @@ -25,8 +25,8 @@
@protocol LastfmDelegate <NSObject>
@optional
-(void)lastfm:(Lastfm*)lastfm requiresAuth:(NSURL*)url; // the user needs to visit this URL to auth
-(void)lastfm:(Lastfm*)lastfm error:(NSString*)message;
-(void)lastfm:(Lastfm*)lastfm metadata:(NSDictionary*)metadata betterdata:(NSDictionary*)betterdata;
-(void)lastfm:(Lastfm*)lastfm errorCode:(int)code errorMessage:(NSString*)message;
@end


Expand All @@ -39,19 +39,14 @@

@property(readonly) NSString* username;

// generates lastfm URLs
+(NSURL*)urlForTrack:(NSString*)track by:(NSString*)artist;
+(NSURL*)urlForUser:(NSString*)username;
+(NSString*)urlEncode:(NSString*)url_component; // Last.fm has special URL encoding rules
+(NSURL*)urlForUser:(NSString*)username; // the user's profile page

// pretty string functions
+(NSString*)durationString:(NSTimeInterval)durationInSeconds;
-(id)initWithDelegate:(id)delegate;

// lastfm API
-(void)love:(NSDictionary*)track;
-(void)share:(NSDictionary*)track with:(NSString*)username;
-(void)updateNowPlaying:(NSDictionary*)track;
-(void)scrobble:(NSDictionary*)track startTime:(time_t)start_time;

-(id)initWithDelegate:(id)delegate;

@end

0 comments on commit 6d779ff

Please sign in to comment.