~urbanape/ubuntuone-ios-client/2.0-13-prerelease

« back to all changes in this revision

Viewing changes to musicstreaming/models/Artist.m

  • Committer: Jason Foreman
  • Date: 2011-06-16 18:33:42 UTC
  • mfrom: (191.1.60 master)
  • Revision ID: jason.foreman@canonical.com-20110616183342-vl7a4804xsf0s1b5
Merging branches for v 2.0.

lp:~threeve/ubuntuone-ios-client/master
lp:~urbanape/ubuntuone-ios-client/downloader

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
#import "Artist.h"
32
32
#import "Album.h"
 
33
#import "Subsonic.h"
 
34
#import "NSMutableSet+Extras.h"
 
35
#import "NSString+Extras.h"
 
36
#import "ArtistParser.h"
 
37
 
 
38
@interface Artist ()
 
39
+ (Artist*)artistMatchingPredicate:(NSPredicate*)predicate;
 
40
@end
 
41
 
33
42
 
34
43
@implementation Artist
35
44
 
40
49
 
41
50
+ (BOOL)artistWithIdExists:(NSString*)anArtistId
42
51
{
 
52
        return [Artist artistWithId:anArtistId] != nil;
 
53
}
 
54
 
 
55
+ (Artist*)artistWithId:(NSString*)anArtistId
 
56
{
 
57
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"artistId == %@", anArtistId];
 
58
        return [self artistMatchingPredicate:predicate];
 
59
}
 
60
 
 
61
+ (Artist*)artistWithName:(NSString*)artistName;
 
62
{
 
63
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", artistName];
 
64
        return [self artistMatchingPredicate:predicate];
 
65
}
 
66
 
 
67
+ (Artist*)artistMatchingPredicate:(NSPredicate*)predicate;
 
68
{
43
69
        NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
44
70
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Artist" inManagedObjectContext:PerThreadManagedObjectContext()];
45
71
        [fetch setEntity:entityDescription];
46
 
        
47
 
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"artistId == %@", anArtistId];
48
72
        [fetch setPredicate:predicate];
49
 
        
50
73
        NSError * error = nil;
51
74
        NSArray * matches = [PerThreadManagedObjectContext() executeFetchRequest:fetch error:&error];
52
75
        [fetch release];
53
76
        
54
 
        return (0 != [matches count]);
55
 
}
56
 
 
57
 
- (id)initWithCoder:(NSCoder*)decoder
58
 
{
59
 
        if ((self = [super init]))
60
 
        {
61
 
                self.name = [decoder decodeObjectForKey:@"name"];
62
 
                self.artistId = [decoder decodeObjectForKey:@"artistId"];
63
 
        }
64
 
        
65
 
        return self;
66
 
}
67
 
 
68
 
- (void)encodeWithCoder:(NSCoder*)encoder
69
 
{
70
 
        [encoder encodeObject:self.name forKey:@"name"];
71
 
        [encoder encodeObject:self.artistId forKey:@"artistId"];
 
77
        return [matches lastObject];
72
78
}
73
79
 
74
80
- (NSString*)description
82
88
    return titleResultsRange.length > 0;
83
89
}
84
90
 
 
91
- (NSString *)dearticlizedName
 
92
{
 
93
    return [self.name dearticlizedString];
 
94
}
 
95
 
 
96
// Note: executes synchronously!
 
97
- (NSError*)loadAlbums
 
98
{
 
99
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
100
        NSError *error = nil;
 
101
        
 
102
        NSMutableSet *parameters = [NSMutableSet set];
 
103
        [parameters addKeyValueObjectFromArray:[NSArray arrayWithObjects:@"id", self.artistId, nil]];
 
104
        NSURL *url = [[Subsonic sharedSubsonic] getBaseURL:@"getMusicDirectory.view" parameters:parameters];
 
105
        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
 
106
        ArtistParser *parser = [[ArtistParser alloc] init];
 
107
        parser.artistManagedObjectID = [self objectID];
 
108
        [xmlParser setDelegate:parser];
 
109
        
 
110
        if (![xmlParser parse])
 
111
        {
 
112
                error = [[xmlParser parserError] copy];
 
113
        }
 
114
        
 
115
        [xmlParser release];
 
116
        [parser release];
 
117
        [pool release];
 
118
        
 
119
        return [error autorelease];
 
120
}
 
121
 
 
122
- (BOOL)hasCachedSongs;
 
123
{
 
124
        NSManagedObjectContext *moc = [self managedObjectContext];
 
125
        NSEntityDescription *songEntity = [NSEntityDescription entityForName:@"Song" inManagedObjectContext:moc];
 
126
        NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
 
127
        [fetch setEntity:songEntity];
 
128
        [fetch setPredicate:[NSPredicate predicateWithFormat:@"artist = %@ and cachedSongPath != NIL", self.name]];
 
129
        
 
130
        NSError *error = nil;
 
131
        NSArray *results = [moc executeFetchRequest:fetch error:&error];
 
132
        return [results count] > 0;
 
133
}
 
134
 
85
135
@end