MediaLibrary.h 7.83 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
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * 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.
 *****************************************************************************/

23 24 25
#ifndef MEDIALIBRARY_H
#define MEDIALIBRARY_H

26 27 28 29
#include "medialibrary/IMediaLibrary.h"
#include "logging/Logger.h"
#include "Settings.h"

30
#include "medialibrary/IDeviceLister.h"
31

32 33 34
namespace medialibrary
{

35
class ModificationNotifier;
36
class DiscovererWorker;
37
class Parser;
38
class ParserService;
39
class SqliteConnection;
40

41 42
class Album;
class Artist;
43
class Media;
44 45
class Movie;
class Show;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
46
class Device;
47
class Folder;
48
class Genre;
49

50 51 52 53
namespace factory
{
class IFileSystem;
}
54 55 56 57 58
namespace fs
{
class IFile;
class IDirectory;
}
59

60
class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
61 62 63
{
    public:
        MediaLibrary();
64
        ~MediaLibrary();
65 66
        virtual bool initialize( const std::string& dbPath, const std::string& thumbnailPath, IMediaLibraryCb* metadataCb ) override;
        virtual void setVerbosity( LogLevel v ) override;
67

68
        virtual MediaPtr media( int64_t mediaId ) const override;
69
        virtual MediaPtr media( const std::string& path ) const override;
70
        virtual MediaPtr addMedia( const std::string& mrl ) override;
71 72
        virtual std::vector<MediaPtr> audioFiles( SortingCriteria sort, bool desc) const override;
        virtual std::vector<MediaPtr> videoFiles( SortingCriteria sort, bool desc) const override;
73

74
        std::shared_ptr<Media> addFile( const fs::IFile& fileFs, Folder& parentFolder, fs::IDirectory& parentFolderFs );
75

76
        bool deleteFolder(const Folder& folder );
77

78 79
        virtual LabelPtr createLabel( const std::string& label ) override;
        virtual bool deleteLabel( LabelPtr label ) override;
80

81
        virtual AlbumPtr album( int64_t id ) const override;
82
        std::shared_ptr<Album> createAlbum( const std::string& title, const std::string& artworkMrl );
83
        virtual std::vector<AlbumPtr> albums(SortingCriteria sort, bool desc) const override;
84

85 86
        virtual std::vector<GenrePtr> genres( SortingCriteria sort, bool desc ) const override;
        virtual GenrePtr genre( int64_t id ) const override;
87

88
        virtual ShowPtr show( const std::string& name ) const override;
89
        std::shared_ptr<Show> createShow( const std::string& name );
90

91
        virtual MoviePtr movie( const std::string& title ) const override;
92
        std::shared_ptr<Movie> createMovie( Media& media, const std::string& title );
93

94 95
        virtual ArtistPtr artist( int64_t id ) const override;
        ArtistPtr artist( const std::string& name );
96
        std::shared_ptr<Artist> createArtist( const std::string& name );
97
        virtual std::vector<ArtistPtr> artists( SortingCriteria sort, bool desc ) const override;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
98

99 100 101
        virtual PlaylistPtr createPlaylist( const std::string& name ) override;
        virtual std::vector<PlaylistPtr> playlists( SortingCriteria sort, bool desc ) override;
        virtual PlaylistPtr playlist( int64_t id ) const override;
102
        virtual bool deletePlaylist( int64_t playlistId ) override;
103

104
        virtual bool addToStreamHistory( MediaPtr media ) override;
105
        virtual std::vector<HistoryPtr> lastStreamsPlayed() const override;
106
        virtual std::vector<MediaPtr> lastMediaPlayed() const override;
107
        virtual bool clearHistory() override;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
108

109 110 111 112 113 114
        virtual MediaSearchAggregate searchMedia( const std::string& title ) const override;
        virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name ) const override;
        virtual std::vector<AlbumPtr> searchAlbums( const std::string& pattern ) const override;
        virtual std::vector<GenrePtr> searchGenre( const std::string& genre ) const override;
        virtual std::vector<ArtistPtr> searchArtists( const std::string& name ) const override;
        virtual SearchAggregate search( const std::string& pattern ) const override;
115

116
        virtual void discover( const std::string& entryPoint ) override;
117
        virtual void setDiscoverNetworkEnabled( bool enabled ) override;
118
        virtual std::vector<FolderPtr> entryPoints() const override;
119
        virtual bool removeEntryPoint( const std::string& entryPoint ) override;
120 121
        virtual bool banFolder( const std::string& path ) override;
        virtual bool unbanFolder( const std::string& path ) override;
122

123
        virtual const std::string& thumbnailPath() const override;
124
        virtual void setLogger( ILogger* logger ) override;
125 126
        //Temporarily public, move back to private as soon as we start monitoring the FS
        virtual void reload() override;
127
        virtual void reload( const std::string& entryPoint ) override;
128

129 130 131
        virtual void pauseBackgroundOperations() override;
        virtual void resumeBackgroundOperations() override;

132
        DBConnection getConn() const;
133
        IMediaLibraryCb* getCb() const;
134
        std::shared_ptr<ModificationNotifier> getNotifier() const;
135

136
        virtual IDeviceListerCb* setDeviceLister( DeviceListerPtr lister ) override;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
137
        std::shared_ptr<factory::IFileSystem> fsFactoryForMrl( const std::string& path ) const;
138

139 140 141
    public:
        static const uint32_t DbModelVersion;

142
    private:
143 144
        static const std::vector<std::string> supportedVideoExtensions;
        static const std::vector<std::string> supportedAudioExtensions;
145

146
    private:
147 148
        virtual void startParser();
        virtual void startDiscoverer();
149
        virtual void startDeletionNotifier();
150
        bool updateDatabaseModel( unsigned int previousVersion );
151
        bool createAllTables();
152
        void registerEntityHooks();
153
        static bool validateSearchPattern( const std::string& pattern );
154

155 156 157
    protected:
        virtual void addLocalFsFactory();

158 159 160 161 162
        // Mark IDeviceListerCb callbacks as private. They must be invoked through the interface.
    private:
        virtual void onDevicePlugged(const std::string& uuid, const std::string& mountpoint) override;
        virtual void onDeviceUnplugged(const std::string& uuid) override;

163
    protected:
164
        std::unique_ptr<SqliteConnection> m_dbConnection;
165
        std::vector<std::shared_ptr<factory::IFileSystem>> m_fsFactories;
166
        std::string m_thumbnailPath;
167
        IMediaLibraryCb* m_callback;
168
        DeviceListerPtr m_deviceLister;
169 170 171 172 173 174 175

        // Keep the parser as last field.
        // The parser holds a (raw) pointer to the media library. When MediaLibrary's destructor gets called
        // it might still finish a few operations before exiting the parser thread. Those operations are
        // likely to require a valid MediaLibrary, which would be compromised if some fields have already been
        // deleted/destroyed.
        std::unique_ptr<Parser> m_parser;
176 177 178
        // Same reasoning applies here.
        //FIXME: Having to maintain a specific ordering sucks, let's use shared_ptr or something
        std::unique_ptr<DiscovererWorker> m_discoverer;
179
        std::shared_ptr<ModificationNotifier> m_modificationNotifier;
180
        LogLevel m_verbosity;
181
        Settings m_settings;
182
        bool m_initialized;
183
};
184 185 186

}

187
#endif // MEDIALIBRARY_H