VLCLibraryController.m 5.39 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 24 25
/*****************************************************************************
 * VLCLibraryController.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 "VLCLibraryController.h"

#import "main/VLCMain.h"
26
#import "playlist/VLCPlaylistController.h"
27 28
#import "playlist/VLCPlayerController.h"
#import "library/VLCLibraryModel.h"
29
#import "library/VLCLibraryDataTypes.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#import <vlc_media_library.h>

@interface VLCLibraryController()
{
    vlc_medialibrary_t *_p_libraryInstance;
}
@end

@implementation VLCLibraryController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _p_libraryInstance = vlc_ml_instance_get(getIntf());
46
        _libraryModel = [[VLCLibraryModel alloc] initWithLibrary:_p_libraryInstance];
47
        _unsorted = YES;
48 49 50 51 52 53 54 55 56 57 58 59 60 61

        NSNotificationCenter *defaultNotificationCenter = [NSNotificationCenter defaultCenter];
        [defaultNotificationCenter addObserver:self
                                      selector:@selector(applicationWillEnterBackground:)
                                          name:NSApplicationWillResignActiveNotification
                                        object:nil];
        [defaultNotificationCenter addObserver:self
                                      selector:@selector(applicationWillBecomeActive:)
                                          name:NSApplicationWillBecomeActiveNotification
                                        object:nil];
        [defaultNotificationCenter addObserver:self
                                      selector:@selector(playbackStateChanged:)
                                          name:VLCPlayerStateChanged
                                        object:nil];
62 63 64
        dispatch_async(dispatch_get_main_queue(), ^{
            [self lazyLoad];
        });
65 66 67 68 69 70 71 72 73 74
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    _p_libraryInstance = NULL;
}

75 76 77 78 79
- (void)lazyLoad
{
    [self applicationWillEnterBackground:nil];
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
- (void)applicationWillEnterBackground:(NSNotification *)aNotification
{
    vlc_ml_resume_background(_p_libraryInstance);
}

- (void)applicationWillBecomeActive:(NSNotification *)aNotification
{
    vlc_ml_pause_background(_p_libraryInstance);
}

- (void)playbackStateChanged:(NSNotification *)aNotification
{
    VLCPlayerController *playerController = aNotification.object;
    if (playerController.playerState == VLC_PLAYER_STATE_PLAYING) {
        vlc_ml_pause_background(_p_libraryInstance);
    } else {
        vlc_ml_resume_background(_p_libraryInstance);
    }
}

100
- (int)appendItemToPlaylist:(VLCMediaLibraryMediaItem *)mediaItem playImmediately:(BOOL)playImmediately
101 102
{
    input_item_t *p_inputItem = vlc_ml_get_input_item(_p_libraryInstance, mediaItem.libraryID);
103
    int ret = [[[VLCMain sharedInstance] playlistController] addInputItem:p_inputItem atPosition:-1 startPlayback:playImmediately];
104
    input_item_Release(p_inputItem);
105 106 107
    if (ret == VLC_SUCCESS) {
        [mediaItem increasePlayCount];
    }
108
    return ret;
109 110
}

111
- (void)showItemInFinder:(VLCMediaLibraryMediaItem *)mediaItem;
112 113 114 115 116 117 118 119 120 121 122 123 124 125
{
    if (mediaItem == nil) {
        return;
    }
    VLCMediaLibraryFile *firstFile = mediaItem.files.firstObject;

    if (firstFile) {
        NSURL *URL = [NSURL URLWithString:firstFile.MRL];
        if (URL) {
            [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[URL]];
        }
    }
}

126 127 128 129 130
- (int)attemptToGenerateThumbnailForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem
{
    return vlc_ml_media_generate_thumbnail(_p_libraryInstance, mediaItem.libraryID);
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#pragma mark - folder management

- (int)addFolderWithFileURL:(NSURL *)fileURL
{
    return vlc_ml_add_folder(_p_libraryInstance, [[fileURL absoluteString] UTF8String]);
}

- (int)banFolderWithFileURL:(NSURL *)fileURL
{
    return vlc_ml_ban_folder(_p_libraryInstance, [[fileURL absoluteString] UTF8String]);
}

- (int)unbanFolderWithFileURL:(NSURL *)fileURL
{
    return vlc_ml_unban_folder(_p_libraryInstance, [[fileURL absoluteString] UTF8String]);
}

- (int)removeFolderWithFileURL:(NSURL *)fileURL
{
    return vlc_ml_remove_folder(_p_libraryInstance, [[fileURL absoluteString] UTF8String]);
}

153 154 155 156 157
- (int)clearHistory
{
    return vlc_ml_clear_history(_p_libraryInstance);
}

158 159 160 161 162 163 164 165
- (void)sortByCriteria:(enum vlc_ml_sorting_criteria_t)sortCriteria andDescending:(bool)descending
{
    _unsorted = NO;
    _lastSortingCriteria = sortCriteria;
    _descendingLibrarySorting = descending;
    [_libraryModel sortByCriteria:sortCriteria andDescending:descending];
}

166
@end