Media.h 8.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 "utils/Cache.h"
33
#include "medialibrary/IMetadata.h"
34
#include "Metadata.h"
35

36 37
#include <atomic>

38 39 40
namespace medialibrary
{

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

46
class Media;
47

48
class Media : public IMedia, public DatabaseHelpers<Media>
49 50
{
    public:
51 52 53 54 55 56
        struct Table
        {
            static const std::string Name;
            static const std::string PrimaryKeyColumn;
            static int64_t Media::*const PrimaryKey;
        };
57 58 59 60
        // 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...
61
        Media( MediaLibraryPtr ml , sqlite::Row& row );
62
        Media( MediaLibraryPtr ml, const std::string& title, Type type);
63

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

68
        virtual int64_t id() const override;
69
        virtual Type type() const override;
70
        virtual SubType subType() const override;
71
        virtual const std::string& title() const override;
72
        virtual bool setTitle( const std::string& title ) override;
73 74 75 76 77 78
        ///
        /// \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 );
79 80
        virtual AlbumTrackPtr albumTrack() const override;
        void setAlbumTrack( AlbumTrackPtr albumTrack );
81
        virtual int64_t duration() const override;
82
        void setDuration( int64_t duration);
83 84 85 86
        virtual ShowEpisodePtr showEpisode() const override;
        void setShowEpisode( ShowEpisodePtr episode );
        virtual bool addLabel( LabelPtr label ) override;
        virtual bool removeLabel( LabelPtr label ) override;
87
        virtual Query<ILabel> labels() const override;
88
        virtual int playCount() const  override;
89
        virtual bool increasePlayCount() override;
90
        virtual time_t lastPlayedDate() const override;
91
        virtual bool isFavorite() const override;
92
        virtual bool setFavorite( bool favorite ) override;
93
        virtual const std::vector<FilePtr>& files() const override;
94
        virtual const std::string& fileName() const override;
95 96
        virtual MoviePtr movie() const override;
        void setMovie( MoviePtr movie );
97 98
        bool addVideoTrack( const std::string& codec, unsigned int width,
                            unsigned int height, uint32_t fpsNum,
99 100
                            uint32_t fpsDen, uint32_t bitrate, uint32_t sarNum,
                            uint32_t sarDen, const std::string& language,
101
                            const std::string& description );
102
        virtual Query<IVideoTrack> videoTracks() const override;
103 104
        bool addAudioTrack( const std::string& codec, unsigned int bitrate, unsigned int sampleRate,
                            unsigned int nbChannels, const std::string& language, const std::string& desc );
105
        virtual Query<IAudioTrack> audioTracks() const override;
106
        virtual const std::string& thumbnail() const override;
107
        virtual bool isThumbnailGenerated() const override;
108
        virtual bool setThumbnail( const std::string &thumbnail ) override;
109
        bool setThumbnail( const std::string& thumbnail, Thumbnail::Origin origin );
110
        virtual unsigned int insertionDate() const override;
111
        virtual unsigned int releaseDate() const override;
112 113
        uint32_t nbPlaylists() const;
        void udpateNbPlaylist( int32_t increment ) const;
114

115
        virtual const IMetadata& metadata( MetadataType type ) const override;
116 117
        virtual bool setMetadata( MetadataType type, const std::string& value ) override;
        virtual bool setMetadata( MetadataType type, int64_t value ) override;
118
        virtual bool unsetMetadata( MetadataType type ) override;
119

120
        void setReleaseDate( unsigned int date );
121
        bool save();
122

123 124
        std::shared_ptr<File> addFile( const fs::IFile& fileFs, int64_t parentFolderId,
                                       bool isFolderFsRemovable, IFile::Type type );
125
        virtual FilePtr addExternalMrl( const std::string& mrl, IFile::Type type ) override;
126
        void removeFile( File& file );
127

128
        static Query<IMedia> listAll(MediaLibraryPtr ml, Type type, const QueryParameters* params );
129 130

        static Query<IMedia> search( MediaLibraryPtr ml, const std::string& title,
131 132 133
                                     const QueryParameters* params );
        static Query<IMedia> search(MediaLibraryPtr ml, const std::string& title,
                                    Media::Type subType, const QueryParameters* params );
134 135
        static Query<IMedia> searchAlbumTracks( MediaLibraryPtr ml, const std::string& pattern,
                                                int64_t albumId, const QueryParameters* params );
136 137
        static Query<IMedia> searchArtistTracks( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t artistId, const QueryParameters* params );
138 139
        static Query<IMedia> searchGenreTracks( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t genreId, const QueryParameters* params );
140 141
        static Query<IMedia> searchShowEpisodes( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t showId, const QueryParameters* params );
142 143
        static Query<IMedia> searchInPlaylist( MediaLibraryPtr ml, const std::string& pattern,
                                                 int64_t playlistId, const QueryParameters* params );
144
        static Query<IMedia> fetchHistory( MediaLibraryPtr ml );
145
        static Query<IMedia> fetchStreamHistory( MediaLibraryPtr ml );
146

147
        static void clearHistory( MediaLibraryPtr ml );
148
        static void removeOldMedia( MediaLibraryPtr ml, std::chrono::seconds maxLifeTime );
149

150
private:
151
        static std::string sortRequest( const QueryParameters* params );
152 153

private:
154
        MediaLibraryPtr m_ml;
155

156
        // DB fields:
157
        int64_t m_id;
158
        const Type m_type;
159
        SubType m_subType;
160
        int64_t m_duration;
161
        unsigned int m_playCount;
162
        std::time_t m_lastPlayedDate;
163
        const std::time_t m_insertionDate;
164
        unsigned int m_releaseDate;
165 166
        int64_t m_thumbnailId;
        unsigned int m_thumbnailGenerated;
167
        std::string m_title;
168 169
        // We store the filename as a shortcut when sorting. The filename (*not* the title
        // might be used as a fallback
170
        const std::string m_filename;
171
        bool m_isFavorite;
172
        bool m_isPresent;
173
        mutable std::atomic_uint m_nbPlaylists;
174 175

        // Auto fetched related properties
176 177 178 179
        mutable Cache<AlbumTrackPtr> m_albumTrack;
        mutable Cache<ShowEpisodePtr> m_showEpisode;
        mutable Cache<MoviePtr> m_movie;
        mutable Cache<std::vector<FilePtr>> m_files;
180
        mutable Metadata m_metadata;
181
        mutable Cache<std::shared_ptr<Thumbnail>> m_thumbnail;
182
        bool m_changed;
183

184
        friend Media::Table;
185 186
};

187
}