Permalink
Browse files

Actual error handling

Also tidied stuff up more.
  • Loading branch information...
1 parent b1cb357 commit 6d779ff966a9cc140205733283424829e1e8faa3 @mxcl committed Jun 19, 2010
Showing with 245 additions and 178 deletions.
  1. +1 −3 HistoryMenuController.m
  2. +2 −7 ITunesListener.m
  3. +2 −6 NSDictionary+Track.h
  4. +16 −2 NSDictionary+Track.m
  5. +9 −9 StatusItemController.m
  6. +4 −9 lastfm.h
  7. +211 −142 lastfm.m
View
@@ -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];
View
@@ -153,11 +153,6 @@ -(void)submit
[lastfm scrobble:track startTime:start_time];
}
--(void)updateNowPlaying:(NSDictionary*)dict
-{
- [lastfm updateNowPlaying:dict];
-}
-
-(void)start
{
[self announce:TrackStarted];
@@ -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
View
@@ -2,7 +2,6 @@
#import <Cocoa/Cocoa.h>
@interface NSDictionary (mxcl)
-
@property(readonly) bool unrated;
@property(readonly) int64_t pid;
@property(readonly) int rating;
@@ -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
View
@@ -1,5 +1,7 @@
// Created by Max Howell on 05/06/2010.
#import "NSDictionary+Track.h"
+#import "lastfm.h"
+
@implementation NSDictionary (mxcl)
@@ -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"];
@@ -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
@@ -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
View
@@ -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 \
@@ -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
@@ -304,6 +305,7 @@ -(void)lastfm:(Lastfm*)lastfm metadata:(NSDictionary*)metadata betterdata:(NSDic
clickContext:nil];
}
+
-(void)growlNotificationWasClicked:(id)dict
{
if ([dict isKindOfClass:[NSString class]])
@@ -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
@@ -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]];
}
View
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit 6d779ff

Please sign in to comment.