Media.h 9.6 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
#pragma once
24 25 26

#include <sqlite3.h>

27
#include "medialibrary/IMedia.h"
28
#include "medialibrary/filesystem/IFileSystemFactory.h"
29
#include "File.h"
30
#include "Thumbnail.h"
31
#include "database/DatabaseHelpers.h"
32
#include "medialibrary/IMetadata.h"
33
#include "Metadata.h"
34

35 36
#include <atomic>

37 38 39
namespace medialibrary
{

40
class Album;
41
class Folder;
42 43 44
class ShowEpisode;
class AlbumTrack;

45
class Media;
46

47
class Media : public IMedia, public DatabaseHelpers<Media>
48 49
{
    public:
50 51 52 53 54 55
        struct Table
        {
            static const std::string Name;
            static const std::string PrimaryKeyColumn;
            static int64_t Media::*const PrimaryKey;
        };
56 57 58 59
        // Those should be private, however the standard states that the expression
        // ::new (pv) T(std::forward(args)...)
        // shall be well-formed, and private constructor would prevent that.
        // There might be a way with a user-defined allocator, but we'll see that later...
60
        Media( MediaLibraryPtr ml , sqlite::Row& row );
61
        Media( MediaLibraryPtr ml, const std::string& title, Type type);
62

63
        static std::shared_ptr<Media> create( MediaLibraryPtr ml, Type type,
64
                                              int64_t deviceId, int64_t folderId,
65
                                              const std::string& fileName );
66
        static void createTable( sqlite::Connection* connection, uint32_t modelVersion );
67
        static void createTriggers( sqlite::Connection* connection, uint32_t modelVersion );
68

69
        virtual int64_t id() const override;
70
        virtual Type type() const override;
71
        void setType(Type type);
72
        virtual SubType subType() const override;
73 74
        void setSubType( SubType subType );

75
        virtual const std::string& title() const override;
76
        virtual bool setTitle( const std::string& title ) override;
77 78 79 80 81 82
        ///
        /// \brief setTitleBuffered Mark the media as changed but doesn't save the change in DB
        /// Querying the title after this method will return the new title, but it won't appear in DB
        /// until save() is called
        ///
        void setTitleBuffered( const std::string& title );
83 84
        // Should only be used by 13->14 migration
        void setFileName( std::string fileName );
85 86
        virtual AlbumTrackPtr albumTrack() const override;
        void setAlbumTrack( AlbumTrackPtr albumTrack );
87
        virtual int64_t duration() const override;
88
        void setDuration( int64_t duration);
89 90 91 92
        virtual ShowEpisodePtr showEpisode() const override;
        void setShowEpisode( ShowEpisodePtr episode );
        virtual bool addLabel( LabelPtr label ) override;
        virtual bool removeLabel( LabelPtr label ) override;
93
        virtual Query<ILabel> labels() const override;
94
        virtual uint32_t playCount() const override;
95
        virtual bool increasePlayCount() override;
96
        virtual bool setPlayCount( uint32_t playCount ) override;
97
        virtual time_t lastPlayedDate() const override;
98
        virtual bool isFavorite() const override;
99
        virtual bool setFavorite( bool favorite ) override;
100
        virtual const std::vector<FilePtr>& files() const override;
101
        virtual const std::string& fileName() const override;
102 103
        virtual MoviePtr movie() const override;
        void setMovie( MoviePtr movie );
104 105
        bool addVideoTrack( const std::string& codec, unsigned int width,
                            unsigned int height, uint32_t fpsNum,
106 107
                            uint32_t fpsDen, uint32_t bitrate, uint32_t sarNum,
                            uint32_t sarDen, const std::string& language,
108
                            const std::string& description );
109
        virtual Query<IVideoTrack> videoTracks() const override;
110 111
        bool addAudioTrack( const std::string& codec, unsigned int bitrate, unsigned int sampleRate,
                            unsigned int nbChannels, const std::string& language, const std::string& desc );
112 113
        bool addSubtitleTrack( std::string codec, std::string language,
                               std::string description, std::string encoding );
114
        virtual Query<IAudioTrack> audioTracks() const override;
115
        virtual Query<ISubtitleTrack> subtitleTracks() const override;
116 117
        virtual Query<IChapter> chapters( const QueryParameters* params ) const override;
        bool addChapter( int64_t offset, int64_t duration, std::string name );
118
        virtual const std::string& thumbnail() const override;
119
        virtual bool isThumbnailGenerated() const override;
120
        virtual bool setThumbnail( const std::string &thumbnail ) override;
121 122
        bool setThumbnail( const std::string& thumbnail, Thumbnail::Origin origin,
                           bool isGenerated);
123
        virtual unsigned int insertionDate() const override;
124
        virtual unsigned int releaseDate() const override;
125 126
        uint32_t nbPlaylists() const;
        void udpateNbPlaylist( int32_t increment ) const;
127

128
        virtual const IMetadata& metadata( MetadataType type ) const override;
129 130
        virtual bool setMetadata( MetadataType type, const std::string& value ) override;
        virtual bool setMetadata( MetadataType type, int64_t value ) override;
131
        virtual bool unsetMetadata( MetadataType type ) override;
132

133
        void setReleaseDate( unsigned int date );
134
        bool save();
135

136 137
        std::shared_ptr<File> addFile( const fs::IFile& fileFs, int64_t parentFolderId,
                                       bool isFolderFsRemovable, IFile::Type type );
138
        virtual FilePtr addExternalMrl( const std::string& mrl, IFile::Type type ) override;
139
        void removeFile( File& file );
140

141
        static Query<IMedia> listAll(MediaLibraryPtr ml, Type type, const QueryParameters* params );
142 143

        static Query<IMedia> search( MediaLibraryPtr ml, const std::string& title,
144 145 146
                                     const QueryParameters* params );
        static Query<IMedia> search(MediaLibraryPtr ml, const std::string& title,
                                    Media::Type subType, const QueryParameters* params );
147 148
        static Query<IMedia> searchAlbumTracks( MediaLibraryPtr ml, const std::string& pattern,
                                                int64_t albumId, const QueryParameters* params );
149 150
        static Query<IMedia> searchArtistTracks( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t artistId, const QueryParameters* params );
151 152
        static Query<IMedia> searchGenreTracks( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t genreId, const QueryParameters* params );
153 154
        static Query<IMedia> searchShowEpisodes( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t showId, const QueryParameters* params );
155 156
        static Query<IMedia> searchInPlaylist( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t playlistId, const QueryParameters* params );
157
        static Query<IMedia> fetchHistory( MediaLibraryPtr ml );
158
        static Query<IMedia> fetchStreamHistory( MediaLibraryPtr ml );
159 160 161
        static Query<IMedia> fromFolderId( MediaLibraryPtr ml, Type type,
                                           int64_t folderId,
                                           const QueryParameters* params );
162

163
        static void clearHistory( MediaLibraryPtr ml );
164
        static void removeOldMedia( MediaLibraryPtr ml, std::chrono::seconds maxLifeTime );
165

166
private:
167 168
        static std::string addRequestJoin(const QueryParameters* params, bool forceFile , bool forceAlbumTrack);
        static std::string sortRequest( const QueryParameters* params );
169 170

private:
171
        MediaLibraryPtr m_ml;
172

173
        // DB fields:
174
        int64_t m_id;
175
        Type m_type;
176
        SubType m_subType;
177
        int64_t m_duration;
178
        unsigned int m_playCount;
179
        std::time_t m_lastPlayedDate;
180
        const std::time_t m_insertionDate;
181
        unsigned int m_releaseDate;
182
        int64_t m_thumbnailId;
183
        std::string m_title;
184 185
        // We store the filename as a shortcut when sorting. The filename (*not* the title
        // might be used as a fallback
186
        std::string m_filename;
187
        bool m_isFavorite;
188
        mutable std::atomic_uint m_nbPlaylists;
189 190

        // Auto fetched related properties
191 192 193 194
        mutable AlbumTrackPtr m_albumTrack;
        mutable ShowEpisodePtr m_showEpisode;
        mutable MoviePtr m_movie;
        mutable std::vector<FilePtr> m_files;
195
        mutable Metadata m_metadata;
196
        mutable std::shared_ptr<Thumbnail> m_thumbnail;
197
        bool m_changed;
198

199
        friend Media::Table;
200 201
};

202
}