MLAlbumTrack.m 4.96 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 29 30
 * $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"

@interface MLAlbumTrack ()
31
@property (nonatomic, strong) NSNumber *primitiveUnread;
32 33 34 35 36 37 38 39
@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
40
    if (!moc || moc.persistentStoreCoordinator == nil)
41 42
        return [NSArray array];

43 44 45 46 47 48 49 50 51 52 53 54
    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;
}

55
+ (MLAlbumTrack *)trackWithAlbum:(MLAlbum *)album trackNumber:(NSNumber *)trackNumber createIfNeeded:(BOOL)createIfNeeded
56
{
57 58 59 60 61 62 63 64 65 66 67 68
    return [MLAlbumTrack trackWithAlbum:album
                            trackNumber:trackNumber
                              trackName:@""
                         createIfNeeded:createIfNeeded];
}

+ (MLAlbumTrack *)trackWithAlbum:(MLAlbum *)album trackNumber:(NSNumber *)trackNumber trackName:(NSString *)trackName createIfNeeded:(BOOL)createIfNeeded
{
    if (!album)
        return nil;

    NSSet *tracks = [album tracks];
69 70 71 72 73 74
    MLAlbumTrack *track = nil;
    if (trackNumber) {
        for (MLAlbumTrack *trackIter in tracks) {
            if ([trackIter.trackNumber intValue] == [trackNumber intValue]) {
                track = trackIter;
                break;
75 76 77
            } else if ([trackIter.title isEqualToString:trackName]) {
                track = trackIter;
                break;
78 79 80 81 82
            }
        }
    }
    if (!track && createIfNeeded) {
        track = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"AlbumTrack"];
83 84
        if (trackNumber.integerValue == 0)
            trackNumber = @(tracks.count + 1);
85
        track.trackNumber = trackNumber;
86
        [album addTrack:track];
87 88 89 90 91
    }
    return track;
}

+ (MLAlbumTrack *)trackWithAlbumName:(NSString *)albumName trackNumber:(NSNumber *)trackNumber createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated
92 93 94 95 96 97 98 99 100
{
    return [MLAlbumTrack trackWithAlbumName:albumName
                                trackNumber:trackNumber
                                  trackName:@""
                             createIfNeeded:createIfNeeded
                                 wasCreated:wasCreated];
}

+ (MLAlbumTrack *)trackWithAlbumName:(NSString *)albumName trackNumber:(NSNumber *)trackNumber trackName:(NSString *)trackName createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated
101 102 103 104 105 106
{
    MLAlbum *album = [MLAlbum albumWithName:albumName];
    *wasCreated = NO;
    if (!album && createIfNeeded) {
        *wasCreated = YES;
        album = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Album"];
107
        album.name = albumName ? albumName : @"";
108 109 110
    } else if (!album && !createIfNeeded)
        return nil;

111
    return [MLAlbumTrack trackWithAlbum:album trackNumber:trackNumber trackName:trackName createIfNeeded:createIfNeeded];
112 113 114 115 116 117 118 119 120
}

@dynamic primitiveUnread;
@dynamic unread;
- (void)setUnread:(NSNumber *)unread
{
    [self willChangeValueForKey:@"unread"];
    [self setPrimitiveUnread:unread];
    [self didChangeValueForKey:@"unread"];
121 122 123 124 125
    NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
    if (moc) {
        [moc refreshObject:[self album] mergeChanges:YES];
        [moc refreshObject:self mergeChanges:YES];
    }
126 127 128 129 130 131 132 133
}

@dynamic artist;
@dynamic genre;
@dynamic title;
@dynamic trackNumber;
@dynamic album;
@dynamic files;
134
@dynamic containsArtwork;
135
@end