Album.h 6.25 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 ALBUM_H
#define ALBUM_H

26
#include <memory>
27 28
#include <sqlite3.h>

29
#include "medialibrary/IMediaLibrary.h"
30

31
#include "database/DatabaseHelpers.h"
32
#include "medialibrary/IAlbum.h"
33
#include "utils/Cache.h"
34
#include "Thumbnail.h"
35

36 37 38
namespace medialibrary
{

39
class Album;
40 41
class AlbumTrack;
class Artist;
42
class Media;
43

44
class Album : public IAlbum, public DatabaseHelpers<Album>
45 46
{
    public:
47 48 49 50 51 52
        struct Table
        {
            static const std::string Name;
            static const std::string PrimaryKeyColumn;
            static int64_t Album::*const PrimaryKey;
        };
53
        Album( MediaLibraryPtr ml, sqlite::Row& row );
54
        Album( MediaLibraryPtr ml, const std::string& title, int64_t thumbnailId );
55
        Album( MediaLibraryPtr ml, const Artist* artist );
56

57
        virtual int64_t id() const override;
58
        virtual const std::string& title() const override;
59 60 61 62 63 64 65 66 67 68 69
        virtual unsigned int releaseYear() const override;
        /**
         * @brief setReleaseYear Updates the release year
         * @param date The desired date.
         * @param force If force is true, the date will be applied no matter what.
         *              If force is false, the date will be applied if it never was
         *              applied before. Otherwise, setReleaseYear() will restore the release
         *              date to 0.
         * @return
         */
        bool setReleaseYear( unsigned int date, bool force );
70 71
        virtual const std::string& shortSummary() const override;
        bool setShortSummary( const std::string& summary );
72
        virtual const std::string& artworkMrl() const override;
73
        std::shared_ptr<Thumbnail> thumbnail();
74
        bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin );
75 76
        virtual Query<IMedia> tracks( const QueryParameters* params ) const override;
        virtual Query<IMedia> tracks( GenrePtr genre, const QueryParameters* params ) const override;
77 78 79 80 81
        ///
        /// \brief cachedTracks Returns a cached list of tracks
        /// This has no warranty of ordering, validity, or anything else.
        /// \return An unordered-list of this album's tracks
        ///
82
        std::vector<MediaPtr> cachedTracks() const;
83 84
        ///
        /// \brief addTrack Add a track to the album.
85
        /// This will modify the media, but *not* save it.
86 87
        /// The media will be added to the tracks cache.
        ///
88
        std::shared_ptr<AlbumTrack> addTrack( std::shared_ptr<Media> media, unsigned int trackNb,
89
                                              unsigned int discNumber, int64_t artistId, Genre* genre );
90
        bool removeTrack( Media& media, AlbumTrack& albumTrack );
91
        unsigned int nbTracks() const override;
92
        unsigned int duration() const override;
93

94
        virtual ArtistPtr albumArtist() const override;
95
        bool setAlbumArtist( std::shared_ptr<Artist> artist );
96
        virtual Query<IArtist> artists( const QueryParameters* params ) const override;
97

98 99 100
        virtual Query<IMedia> searchTracks( const std::string& pattern,
                                            const QueryParameters* params = nullptr ) const override;

101 102
        static void createTable( sqlite::Connection* dbConnection );
        static void createTriggers( sqlite::Connection* dbConnection );
103
        static std::shared_ptr<Album> create( MediaLibraryPtr ml, const std::string& title, int64_t thumbnailId );
104
        static std::shared_ptr<Album> createUnknownAlbum( MediaLibraryPtr ml, const Artist* artist );
105 106 107 108 109
        ///
        /// \brief search search for an album, through its albumartist or title
        /// \param pattern A pattern representing the title, or the name of the main artist
        /// \return
        ///
110
        static Query<IAlbum> search( MediaLibraryPtr ml, const std::string& pattern,
111
                                     const QueryParameters* params );
112 113
        static Query<IAlbum> searchFromArtist( MediaLibraryPtr ml, const std::string& pattern,
                                               int64_t artistId, const QueryParameters* params );
114 115
        static Query<IAlbum> fromArtist( MediaLibraryPtr ml, int64_t artistId, const QueryParameters* params );
        static Query<IAlbum> fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params );
116 117
        static Query<IAlbum> searchFromGenre( MediaLibraryPtr ml, const std::string& pattern,
                                              int64_t genreId, const QueryParameters* params );
118
        static Query<IAlbum> listAll( MediaLibraryPtr ml, const QueryParameters* params );
119

120
    private:
121 122
        static std::string orderTracksBy( const QueryParameters* params );
        static std::string orderBy( const QueryParameters* params );
123
    protected:
124
        MediaLibraryPtr m_ml;
125
        int64_t m_id;
126
        const std::string m_title;
127
        int64_t m_artistId;
128
        unsigned int m_releaseYear;
129
        std::string m_shortSummary;
130
        int64_t m_thumbnailId;
131
        unsigned int m_nbTracks;
132
        unsigned int m_duration;
133
        bool m_isPresent;
134

135
        mutable Cache<std::vector<MediaPtr>> m_tracks;
136
        mutable Cache<std::shared_ptr<Artist>> m_albumArtist;
137
        mutable Cache<std::shared_ptr<Thumbnail>> m_thumbnail;
138 139
};

140 141
}

142
#endif // ALBUM_H