MLAlbumTrack.m 7.21 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * MLAlbumTrack.m
 *****************************************************************************
 * Copyright (C) 2010 Pierre d'Herbemont
5
 * Copyright (C) 2013-2015 Felix Paul Kühne
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#import "MLMediaLibrary.h"
#import "MLAlbumTrack.h"
#import "MLAlbum.h"
29
#import "MLFile.h"
30

31 32 33 34 35 36
NSString *const MLAlbumTrackAlbum       = @"MLAlbumTrackAlbum";
NSString *const MLAlbumTrackAlbumName   = @"MLAlbumTrackAlbumName";
NSString *const MLAlbumTrackNumber      = @"MLAlbumTrackNumber";
NSString *const MLAlbumTrackTrackName   = @"MLAlbumTrackTrackName";
NSString *const MLAlbumTrackDiscNumber  = @"MLAlbumTrackDiscNumber";

37
@interface MLAlbumTrack ()
38
@property (nonatomic, strong) NSNumber *primitiveUnread;
39 40 41 42 43 44 45 46
@end

@implementation MLAlbumTrack

+ (NSArray *)allTracks
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
47
    if (!moc || moc.persistentStoreCoordinator == nil)
48 49
        return [NSArray array];

50 51 52 53 54 55 56 57 58 59 60 61
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AlbumTrack" inManagedObjectContext:moc];
    [request setEntity:entity];

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
    [request setSortDescriptors:@[descriptor]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"files.@count > 0"]];

    NSArray *tracks = [moc executeFetchRequest:request error:nil];

    return tracks;
}

62
+ (MLAlbumTrack *)trackWithAlbum:(MLAlbum *)album trackNumber:(NSNumber *)trackNumber createIfNeeded:(BOOL)createIfNeeded
63
{
64 65 66 67 68 69 70
    NSMutableDictionary *mutDict = [NSMutableDictionary dictionary];

    if (trackNumber)
        [mutDict setObject:trackNumber forKey:MLAlbumTrackNumber];
    BOOL wasCreated = NO;

    return [MLAlbumTrack trackWithAlbum:album metadata:[NSDictionary dictionaryWithDictionary:mutDict] createIfNeeded:createIfNeeded wasCreated:&wasCreated];
71 72 73
}

+ (MLAlbumTrack *)trackWithAlbum:(MLAlbum *)album trackNumber:(NSNumber *)trackNumber trackName:(NSString *)trackName createIfNeeded:(BOOL)createIfNeeded
74 75 76 77 78 79 80 81 82 83 84 85 86
{
    NSMutableDictionary *mutDict = [NSMutableDictionary dictionary];

    if (trackName)
        [mutDict setObject:trackName forKey:MLAlbumTrackTrackName];
    if (trackNumber)
        [mutDict setObject:trackNumber forKey:MLAlbumTrackNumber];
    BOOL wasCreated = NO;

    return [MLAlbumTrack trackWithAlbum:album metadata:[NSDictionary dictionaryWithDictionary:mutDict] createIfNeeded:createIfNeeded wasCreated:&wasCreated];
}

+ (MLAlbumTrack *)trackWithAlbum:(MLAlbum *)album metadata:(NSDictionary *)metadata createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated
87 88 89 90
{
    if (!album)
        return nil;

91 92 93 94
    NSNumber *trackNumber = metadata[MLAlbumTrackNumber];
    NSString *trackName = metadata[MLAlbumTrackTrackName];
    NSNumber *discNumber = metadata[MLAlbumTrackDiscNumber];

95
    NSSet *tracks = [album tracks];
96 97 98 99
    MLAlbumTrack *track = nil;
    if (trackNumber) {
        for (MLAlbumTrack *trackIter in tracks) {
            if ([trackIter.trackNumber intValue] == [trackNumber intValue]) {
100 101
                if (trackIter.discNumber == discNumber)
                        track = trackIter;
102
                break;
103 104 105
            } else if ([trackIter.title isEqualToString:trackName]) {
                track = trackIter;
                break;
106 107 108 109 110
            }
        }
    }
    if (!track && createIfNeeded) {
        track = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"AlbumTrack"];
111 112
        if (trackNumber.integerValue == 0)
            trackNumber = @(tracks.count + 1);
113
        track.trackNumber = trackNumber;
114 115 116
        track.title = trackName;
        if (discNumber)
            track.discNumber = discNumber;
117
        [album addTrack:track];
118 119 120 121 122
    }
    return track;
}

+ (MLAlbumTrack *)trackWithAlbumName:(NSString *)albumName trackNumber:(NSNumber *)trackNumber createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated
123
{
124 125 126 127 128 129 130 131
    NSMutableDictionary *mutDict = [NSMutableDictionary dictionary];

    if (albumName)
        [mutDict setObject:albumName forKey:MLAlbumTrackAlbumName];
    if (trackNumber)
        [mutDict setObject:trackNumber forKey:MLAlbumTrackNumber];

    return [MLAlbumTrack trackWithMetadata:[NSDictionary dictionaryWithDictionary:mutDict] createIfNeeded:createIfNeeded wasCreated:wasCreated];
132 133 134
}

+ (MLAlbumTrack *)trackWithAlbumName:(NSString *)albumName trackNumber:(NSNumber *)trackNumber trackName:(NSString *)trackName createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated
135
{
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    NSMutableDictionary *mutDict = [NSMutableDictionary dictionary];

    if (albumName)
        [mutDict setObject:albumName forKey:MLAlbumTrackAlbumName];
    if (trackNumber)
        [mutDict setObject:trackNumber forKey:MLAlbumTrackNumber];
    if (trackName)
        [mutDict setObject:trackName forKey:MLAlbumTrackTrackName];

    return [MLAlbumTrack trackWithMetadata:[NSDictionary dictionaryWithDictionary:mutDict] createIfNeeded:createIfNeeded wasCreated:wasCreated];
}

+ (MLAlbumTrack *)trackWithMetadata:(NSDictionary *)metadata createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated
{
    NSString *albumName = metadata[MLAlbumTrackAlbumName];
151 152
    MLAlbum *album = [MLAlbum albumWithName:albumName];
    *wasCreated = NO;
153

154 155 156
    if (!album && createIfNeeded) {
        *wasCreated = YES;
        album = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Album"];
157
        album.name = albumName ? albumName : @"";
158 159
    }
    if (!album && !createIfNeeded)
160 161
        return nil;

162
    return [MLAlbumTrack trackWithAlbum:album metadata:metadata createIfNeeded:createIfNeeded wasCreated:wasCreated];
163 164 165 166 167 168 169 170 171
}

@dynamic primitiveUnread;
@dynamic unread;
- (void)setUnread:(NSNumber *)unread
{
    [self willChangeValueForKey:@"unread"];
    [self setPrimitiveUnread:unread];
    [self didChangeValueForKey:@"unread"];
172 173 174 175 176
    NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
    if (moc) {
        [moc refreshObject:[self album] mergeChanges:YES];
        [moc refreshObject:self mergeChanges:YES];
    }
177 178 179 180 181 182
}

@dynamic artist;
@dynamic genre;
@dynamic title;
@dynamic trackNumber;
183
@dynamic discNumber;
184 185
@dynamic album;
@dynamic files;
186
@dynamic containsArtwork;
187 188 189 190 191 192

- (MLFile *)anyFileFromTrack
{
    return (MLFile *)self.files.anyObject;
}

193
@end