VLCLibraryModel.m 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************
 * VLCLibraryModel.m: MacOS X interface module
 *****************************************************************************
 * Copyright (C) 2019 VLC authors and VideoLAN
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan -dot- org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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 "VLCLibraryModel.h"
24

25
#import "main/VLCMain.h"
26
#import "library/VLCLibraryDataTypes.h"
27
#import "extensions/NSString+Helpers.h"
28

29
NSString *VLCLibraryModelAudioMediaListUpdated = @"VLCLibraryModelAudioMediaListUpdated";
30 31
NSString *VLCLibraryModelArtistListUpdated = @"VLCLibraryModelArtistListUpdated";
NSString *VLCLibraryModelAlbumListUpdated = @"VLCLibraryModelAlbumListUpdated";
32
NSString *VLCLibraryModelVideoMediaListUpdated = @"VLCLibraryModelVideoMediaListUpdated";
33
NSString *VLCLibraryModelRecentMediaListUpdated = @"VLCLibraryModelRecentMediaListUpdated";
34
NSString *VLCLibraryModelMediaItemUpdated = @"VLCLibraryModelMediaItemUpdated";
35 36 37 38

@interface VLCLibraryModel ()
{
    vlc_medialibrary_t *_p_mediaLibrary;
39 40
    vlc_ml_event_callback_t *_p_eventCallback;

41
    NSArray *_cachedAudioMedia;
42 43 44
    NSArray *_cachedArtists;
    NSArray *_cachedAlbums;
    NSArray *_cachedGenres;
45
    NSArray *_cachedVideoMedia;
46
    NSArray *_cachedRecentMedia;
47
    NSNotificationCenter *_defaultNotificationCenter;
48 49 50

    enum vlc_ml_sorting_criteria_t _sortCriteria;
    bool _sortDescending;
51
}
52

53
- (void)updateCachedListOfAudioMedia;
54
- (void)updateCachedListOfVideoMedia;
55
- (void)updateCachedListOfRecentMedia;
56
- (void)mediaItemWasUpdated:(VLCMediaLibraryMediaItem *)mediaItem;
57

58 59
@end

60 61 62 63 64 65 66 67 68
static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
{
    switch(p_event->i_type)
    {
        case VLC_ML_EVENT_MEDIA_ADDED:
        case VLC_ML_EVENT_MEDIA_UPDATED:
        case VLC_ML_EVENT_MEDIA_DELETED:
            dispatch_async(dispatch_get_main_queue(), ^{
                VLCLibraryModel *libraryModel = (__bridge VLCLibraryModel *)p_data;
69 70 71
                [libraryModel updateCachedListOfRecentMedia];
                [libraryModel updateCachedListOfAudioMedia];
                [libraryModel updateCachedListOfVideoMedia];
72 73
            });
            break;
74 75 76 77 78 79 80 81 82
        case VLC_ML_EVENT_MEDIA_THUMBNAIL_GENERATED:
            if (p_event->media_thumbnail_generated.b_success) {
                VLCMediaLibraryMediaItem *mediaItem = [[VLCMediaLibraryMediaItem alloc] initWithMediaItem:(struct vlc_ml_media_t *)p_event->media_thumbnail_generated.p_media];
                dispatch_async(dispatch_get_main_queue(), ^{
                    VLCLibraryModel *libraryModel = (__bridge VLCLibraryModel *)p_data;
                    [libraryModel mediaItemWasUpdated:mediaItem];
                });
            }
            break;
83 84 85 86 87
        default:
            break;
    }
}

88 89 90 91 92 93
@implementation VLCLibraryModel

- (instancetype)initWithLibrary:(vlc_medialibrary_t *)library
{
    self = [super init];
    if (self) {
94 95
        _sortCriteria = VLC_ML_SORTING_DEFAULT;
        _sortDescending = NO;
96
        _p_mediaLibrary = library;
97
        _p_eventCallback = vlc_ml_event_register_callback(_p_mediaLibrary, libraryCallback, (__bridge void *)self);
98 99 100 101 102
        _defaultNotificationCenter = [NSNotificationCenter defaultCenter];
        [_defaultNotificationCenter addObserver:self
                                       selector:@selector(applicationWillTerminate:)
                                           name:NSApplicationWillTerminateNotification
                                         object:nil];
103 104 105 106
    }
    return self;
}

107
- (void)applicationWillTerminate:(NSNotification *)aNotification
108 109 110 111 112 113
{
    if (_p_eventCallback) {
        vlc_ml_event_unregister_callback(_p_mediaLibrary, _p_eventCallback);
    }
}

114 115 116 117 118
- (void)dealloc
{
    [_defaultNotificationCenter removeObserver:self];
}

119 120 121 122 123
+ (NSArray *)availableAudioCollections
{
    return @[_NS("Artists"), _NS("Albums"), _NS("Songs"), _NS("Genres")];
}

124 125 126 127 128
- (void)mediaItemWasUpdated:(VLCMediaLibraryMediaItem *)mediaItem
{
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelMediaItemUpdated object:mediaItem];
}

129 130
- (size_t)numberOfAudioMedia
{
131
    if (!_cachedAudioMedia) {
132
        [self updateCachedListOfAudioMedia];
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }
    return _cachedAudioMedia.count;
}

- (void)updateCachedListOfAudioMedia
{
    vlc_ml_media_list_t *p_media_list = vlc_ml_list_audio_media(_p_mediaLibrary, NULL);
    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_media_list->i_nb_items];
    for (size_t x = 0; x < p_media_list->i_nb_items; x++) {
        VLCMediaLibraryMediaItem *mediaItem = [[VLCMediaLibraryMediaItem alloc] initWithMediaItem:&p_media_list->p_items[x]];
        [mutableArray addObject:mediaItem];
    }
    _cachedAudioMedia = [mutableArray copy];
    vlc_ml_media_list_release(p_media_list);
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelAudioMediaListUpdated object:self];
}

- (NSArray<VLCMediaLibraryMediaItem *> *)listOfAudioMedia
{
    if (!_cachedAudioMedia) {
153
        [self updateCachedListOfAudioMedia];
154 155 156 157
    }
    return _cachedAudioMedia;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
- (size_t)numberOfArtists
{
    if (!_cachedArtists) {
        [self updateCachedListOfArtists];
    }
    return _cachedArtists.count;
}

- (void)updateCachedListOfArtists
{
    vlc_ml_artist_list_t *p_artist_list = vlc_ml_list_artists(_p_mediaLibrary, NULL, NO);
    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_artist_list->i_nb_items];
    for (size_t x = 0; x < p_artist_list->i_nb_items; x++) {
        VLCMediaLibraryArtist *artist = [[VLCMediaLibraryArtist alloc] initWithArtist:&p_artist_list->p_items[x]];
        [mutableArray addObject:artist];
    }
    _cachedArtists = [mutableArray copy];
    vlc_ml_artist_list_release(p_artist_list);
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelArtistListUpdated object:self];
}

- (NSArray<VLCMediaLibraryArtist *> *)listOfArtists
{
    if (!_cachedArtists) {
        [self updateCachedListOfArtists];
    }
    return _cachedArtists;
}

- (size_t)numberOfAlbums
{
    if (!_cachedAlbums) {
        [self updateCachedListOfAlbums];
    }
    return _cachedAlbums.count;
}

- (void)updateCachedListOfAlbums
{
    vlc_ml_album_list_t *p_album_list = vlc_ml_list_albums(_p_mediaLibrary, NULL);
    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_album_list->i_nb_items];
    for (size_t x = 0; x < p_album_list->i_nb_items; x++) {
        VLCMediaLibraryAlbum *album = [[VLCMediaLibraryAlbum alloc] initWithAlbum:&p_album_list->p_items[x]];
        [mutableArray addObject:album];
    }
    _cachedAlbums = [mutableArray copy];
    vlc_ml_album_list_release(p_album_list);
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelArtistListUpdated object:self];
}

- (NSArray<VLCMediaLibraryAlbum *> *)listOfAlbums
{
    if (!_cachedAlbums) {
        [self updateCachedListOfAlbums];
    }
    return _cachedAlbums;
}

- (size_t)numberOfGenres
{
    if (!_cachedGenres) {
        [self updateCachedListOfGenres];
    }
    return _cachedGenres.count;
}

- (void)updateCachedListOfGenres
{
    vlc_ml_genre_list_t *p_genre_list = vlc_ml_list_genres(_p_mediaLibrary, NULL);
    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_genre_list->i_nb_items];
    for (size_t x = 0; x < p_genre_list->i_nb_items; x++) {
        VLCMediaLibraryGenre *genre = [[VLCMediaLibraryGenre alloc] initWithGenre:&p_genre_list->p_items[x]];
        [mutableArray addObject:genre];
    }
    _cachedGenres = [mutableArray copy];
    vlc_ml_genre_list_release(p_genre_list);
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelArtistListUpdated object:self];
}

- (NSArray<VLCMediaLibraryMediaItem *> *)listOfGenres
{
    if (!_cachedGenres) {
        [self updateCachedListOfGenres];
    }
    return _cachedGenres;
}

245 246 247
- (size_t)numberOfVideoMedia
{
    if (!_cachedVideoMedia) {
248 249 250
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateCachedListOfVideoMedia];
        });
251 252 253 254 255 256
    }
    return _cachedVideoMedia.count;
}

- (void)updateCachedListOfVideoMedia
{
257 258 259 260 261 262
    vlc_ml_query_params_t queryParameters;
    memset(&queryParameters, 0, sizeof(vlc_ml_query_params_t));
    queryParameters.i_nbResults = 20;
    queryParameters.i_sort = _sortCriteria;
    queryParameters.b_desc = _sortDescending;
    vlc_ml_media_list_t *p_media_list = vlc_ml_list_video_media(_p_mediaLibrary, &queryParameters);
263 264 265
    if (p_media_list == NULL) {
        return;
    }
266 267 268 269 270 271 272 273 274 275 276 277 278
    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_media_list->i_nb_items];
    for (size_t x = 0; x < p_media_list->i_nb_items; x++) {
        VLCMediaLibraryMediaItem *mediaItem = [[VLCMediaLibraryMediaItem alloc] initWithMediaItem:&p_media_list->p_items[x]];
        [mutableArray addObject:mediaItem];
    }
    _cachedVideoMedia = [mutableArray copy];
    vlc_ml_media_list_release(p_media_list);
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelVideoMediaListUpdated object:self];
}

- (NSArray<VLCMediaLibraryMediaItem *> *)listOfVideoMedia
{
    if (!_cachedVideoMedia) {
279 280 281
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateCachedListOfVideoMedia];
        });
282 283 284 285
    }
    return _cachedVideoMedia;
}

286 287
- (void)updateCachedListOfRecentMedia
{
288 289 290
    vlc_ml_query_params_t queryParameters;
    memset(&queryParameters, 0, sizeof(vlc_ml_query_params_t));
    queryParameters.i_nbResults = 20;
291
    // we don't set the sorting criteria here as they are not applicable to history
292
    vlc_ml_media_list_t *p_media_list = vlc_ml_list_history(_p_mediaLibrary, &queryParameters);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    if (p_media_list == NULL) {
        return;
    }
    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_media_list->i_nb_items];
    for (size_t x = 0; x < p_media_list->i_nb_items; x++) {
        VLCMediaLibraryMediaItem *mediaItem = [[VLCMediaLibraryMediaItem alloc] initWithMediaItem:&p_media_list->p_items[x]];
        [mutableArray addObject:mediaItem];
    }
    _cachedRecentMedia = [mutableArray copy];
    vlc_ml_media_list_release(p_media_list);
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelRecentMediaListUpdated object:self];
}

- (size_t)numberOfRecentMedia
{
    if (!_cachedRecentMedia) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateCachedListOfRecentMedia];
        });
    }
    return _cachedRecentMedia.count;
}

- (NSArray<VLCMediaLibraryMediaItem *> *)listOfRecentMedia
{
    if (!_cachedRecentMedia) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateCachedListOfRecentMedia];
        });
    }
    return _cachedRecentMedia;
}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
- (NSArray<VLCMediaLibraryEntryPoint *> *)listOfMonitoredFolders
{
    vlc_ml_entry_point_list_t *pp_entrypoints;
    int ret = vlc_ml_list_folder(_p_mediaLibrary, &pp_entrypoints);
    if (ret != VLC_SUCCESS) {
        msg_Err(getIntf(), "failed to retrieve list of monitored library folders (%i)", ret);
        return @[];
    }

    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:pp_entrypoints->i_nb_items];
    for (size_t x = 0; x < pp_entrypoints->i_nb_items; x++) {
        VLCMediaLibraryEntryPoint *entryPoint = [[VLCMediaLibraryEntryPoint alloc] initWithEntryPoint:&pp_entrypoints->p_items[x]];
        if (entryPoint) {
            [mutableArray addObject:entryPoint];
        }
    }

    vlc_ml_entry_point_list_release(pp_entrypoints);
    return [mutableArray copy];
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
- (nullable NSArray <VLCMediaLibraryAlbum *>*)listAlbumsOfParentType:(enum vlc_ml_parent_type)parentType forID:(int64_t)ID;
{
    vlc_ml_album_list_t *p_albumList = vlc_ml_list_albums_of(_p_mediaLibrary, NULL, parentType, ID);
    if (p_albumList == NULL) {
        return nil;
    }
    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_albumList->i_nb_items];
    for (size_t x = 0; x < p_albumList->i_nb_items; x++) {
        VLCMediaLibraryAlbum *album = [[VLCMediaLibraryAlbum alloc] initWithAlbum:&p_albumList->p_items[x]];
        [mutableArray addObject:album];
    }
    vlc_ml_album_list_release(p_albumList);
    return [mutableArray copy];
}

362 363 364 365 366 367 368 369 370 371 372 373 374
- (void)sortByCriteria:(enum vlc_ml_sorting_criteria_t)sortCriteria andDescending:(bool)descending
{
    _sortCriteria = sortCriteria;
    _sortDescending = descending;
    [self dropCaches];
}

- (void)dropCaches
{
    _cachedVideoMedia = nil;
    [_defaultNotificationCenter postNotificationName:VLCLibraryModelVideoMediaListUpdated object:nil];
}

375
@end