MediaLibrary.h 7.99 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
        virtual bool initialize( const std::string& dbPath, const std::string& thumbnailPath, IMediaLibraryCb* mlCallback ) override;
66
        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 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
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;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
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
120
121
        virtual void removeEntryPoint( const std::string& entryPoint ) override;
        virtual void banFolder( const std::string& path ) override;
        virtual void unbanFolder( const std::string& path ) override;
122

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
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
        virtual void forceParserRetry() override;
129

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

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

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

140
141
        void refreshDevices(factory::IFileSystem& fsFactory);

142
143
144
    public:
        static const uint32_t DbModelVersion;

145
    private:
146
        static const char* const supportedExtensions[];
147

148
    private:
149
        virtual void startParser();
150
        virtual void startDiscoverer();
151
        virtual void startDeletionNotifier();
152
        bool updateDatabaseModel( unsigned int previousVersion );
153
        bool createAllTables();
154
        void registerEntityHooks();
155
        static bool validateSearchPattern( const std::string& pattern );
156
157
        // Returns true if the device actually changed
        bool onDeviceChanged( factory::IFileSystem& fsFactory, Device& device );
158

159
160
161
    protected:
        virtual void addLocalFsFactory();

162
163
        // Mark IDeviceListerCb callbacks as private. They must be invoked through the interface.
    private:
164
        virtual bool onDevicePlugged( const std::string& uuid, const std::string& mountpoint ) override;
165
166
        virtual void onDeviceUnplugged(const std::string& uuid) override;

167
    protected:
168
        std::unique_ptr<SqliteConnection> m_dbConnection;
169
        std::vector<std::shared_ptr<factory::IFileSystem>> m_fsFactories;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
170
        std::string m_thumbnailPath;
171
        IMediaLibraryCb* m_callback;
172
        DeviceListerPtr m_deviceLister;
173
174
175
176
177
178
179

        // 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;
180
181
        // Same reasoning applies here.
        //FIXME: Having to maintain a specific ordering sucks, let's use shared_ptr or something
182
        std::unique_ptr<DiscovererWorker> m_discovererWorker;
183
        std::shared_ptr<ModificationNotifier> m_modificationNotifier;
184
        LogLevel m_verbosity;
185
        Settings m_settings;
186
        bool m_initialized;
187
};
188
189
190

}

191
#endif // MEDIALIBRARY_H