MLShow.m 5.18 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * MLShow.m
 * Lunettes
 *****************************************************************************
 * Copyright (C) 2010 Pierre d'Herbemont
6
 * Copyright (C) 2010-2015 VLC authors and VideoLAN
7 8 9 10 11
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
12 13 14
 * 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
15 16 17 18
 * (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
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
22 23 24
 * 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.
25
 *****************************************************************************/
Pierre's avatar
Pierre committed
26 27

#import "MLShow.h"
28
#import "MLShowEpisode.h"
Pierre's avatar
Pierre committed
29 30 31 32 33 34 35 36
#import "MLMediaLibrary.h"

@implementation MLShow

+ (NSArray *)allShows
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
37 38
    if (!moc || moc.persistentStoreCoordinator == nil)
        return [NSArray array];
Pierre's avatar
Pierre committed
39 40 41 42
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Show" inManagedObjectContext:moc];
    [request setEntity:entity];

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
43
    [request setSortDescriptors:@[descriptor]];
Pierre's avatar
Pierre committed
44 45 46 47 48 49 50 51 52

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

    return shows;
}

+ (MLShow *)showWithName:(NSString *)name
{
    NSFetchRequest *request = [[MLMediaLibrary sharedMediaLibrary] fetchRequestForEntity:@"Show"];
53 54
    if (!request)
        return nil;
Pierre's avatar
Pierre committed
55 56
    [request setPredicate:[NSPredicate predicateWithFormat:@"name == %@", name]];

57 58 59 60 61
    NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
    if (!moc)
        return nil;

    NSArray *dbResults = [moc executeFetchRequest:request error:nil];
Pierre's avatar
Pierre committed
62 63 64 65 66
    NSAssert(dbResults, @"Can't execute fetch request");

    if ([dbResults count] <= 0)
        return nil;

67
    return dbResults[0];
Pierre's avatar
Pierre committed
68 69 70 71 72 73 74 75 76 77
}


@dynamic theTVDBID;
@dynamic shortSummary;
@dynamic artworkURL;
@dynamic name;
@dynamic lastSyncDate;
@dynamic releaseYear;
@dynamic episodes;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

- (NSSet *)unreadEpisodes
{
    NSArray *episodes = [[self valueForKey:@"episodes"] allObjects];
    NSMutableSet *set = [NSMutableSet set];
    NSUInteger count = episodes.count;
    for (NSUInteger x = 0; x < count; x++) {
        NSSet *files = [episodes[x] valueForKey:@"files"];
        for (id file in files) {
            if ([[file valueForKey:@"unread"] boolValue]) {
                [set addObject:episodes[x]];
                break;
            }
        }
    }
    return set;
}
95

96 97 98 99 100 101 102 103 104 105 106 107
- (NSArray *)sortedEpisodes
{
    NSArray *episodes = [[self valueForKey:@"episodes"] allObjects];

    NSSortDescriptor *seasonDescriptor =
    [[NSSortDescriptor alloc] initWithKey:@"seasonNumber"
                                ascending:YES
                                 selector:@selector(compare:)];
    NSSortDescriptor *episodesDescriptor =
    [[NSSortDescriptor alloc] initWithKey:@"episodeNumber"
                                ascending:YES
                                 selector:@selector(compare:)];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
108
    NSArray *descriptors = @[seasonDescriptor, episodesDescriptor];
109
    return [episodes sortedArrayUsingDescriptors:descriptors];
110 111
}

112 113 114 115 116 117 118 119 120 121 122 123 124
- (void)addEpisode:(MLShowEpisode*)episode
{
    if (!episode)
        return;

    NSMutableSet *episodes = [self mutableSetValueForKey:@"episodes"];
    [episodes addObject:episode];

    [self willChangeValueForKey:@"episodes"];
    [self setValue:episodes forKey:@"episodes"];
    [self didChangeValueForKey:@"episodes"];
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
- (void)removeEpisode:(MLShowEpisode*)episode
{
    if (!episode)
        return;

    NSMutableSet *episodes = [self mutableSetValueForKey:@"episodes"];

    [episodes removeObject:episode];

    [self willChangeValueForKey:@"episodes"];
    [self setValue:episodes forKey:@"episodes"];
    [self didChangeValueForKey:@"episodes"];
}

- (void)removeEpisodeWithSeasonNumber:(NSNumber *)seasonNumber andEpisodeNumber:(NSNumber *)episodeNumber
{
    NSMutableSet *episodes = [self mutableSetValueForKey:@"episodes"];
    MLShowEpisode *episode = nil;
    if (seasonNumber && episodeNumber) {
        for (MLShowEpisode *episodeIter in episodes) {
            if ([episodeIter.seasonNumber intValue] == [seasonNumber intValue] &&
                [episodeIter.episodeNumber intValue] == [episodeNumber intValue]) {
                episode = episodeIter;
                break;
            }
        }
    }
    if (!episode)
        return;

    [episodes removeObject:episode];

    [self willChangeValueForKey:@"episodes"];
    [self setValue:episodes forKey:@"episodes"];
    [self didChangeValueForKey:@"episodes"];
}

Pierre's avatar
Pierre committed
162
@end