Album.h 5.75 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 45 46 47 48
namespace policy
{
struct AlbumTable
{
    static const std::string Name;
49
    static const std::string PrimaryKeyColumn;
50
    static int64_t Album::*const PrimaryKey;
51 52 53
};
}

54
class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
55 56
{
    public:
57
        Album( MediaLibraryPtr ml, sqlite::Row& row );
58
        Album( MediaLibraryPtr ml, const std::string& title, int64_t thumbnailId );
59
        Album( MediaLibraryPtr ml, const Artist* artist );
60

61
        virtual int64_t id() const override;
62
        virtual const std::string& title() const override;
63 64 65 66 67 68 69 70 71 72 73
        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 );
74 75
        virtual const std::string& shortSummary() const override;
        bool setShortSummary( const std::string& summary );
76
        virtual const std::string& artworkMrl() const override;
77
        std::shared_ptr<Thumbnail> thumbnail();
78
        bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin );
79 80
        virtual Query<IMedia> tracks( const QueryParameters* params ) const override;
        virtual Query<IMedia> tracks( GenrePtr genre, const QueryParameters* params ) const override;
81 82 83 84 85
        ///
        /// \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
        ///
86
        std::vector<MediaPtr> cachedTracks() const;
87 88
        ///
        /// \brief addTrack Add a track to the album.
89
        /// This will modify the media, but *not* save it.
90 91
        /// The media will be added to the tracks cache.
        ///
92
        std::shared_ptr<AlbumTrack> addTrack( std::shared_ptr<Media> media, unsigned int trackNb,
93
                                              unsigned int discNumber, int64_t artistId, Genre* genre );
94
        unsigned int nbTracks() const override;
95
        unsigned int duration() const override;
96

97
        virtual ArtistPtr albumArtist() const override;
98
        bool setAlbumArtist( std::shared_ptr<Artist> artist );
99
        virtual Query<IArtist> artists(bool desc) const override;
100
        bool addArtist( std::shared_ptr<Artist> artist );
101
        bool removeArtist( Artist* artist );
102

103 104
        static void createTable( sqlite::Connection* dbConnection );
        static void createTriggers( sqlite::Connection* dbConnection );
105
        static std::shared_ptr<Album> create( MediaLibraryPtr ml, const std::string& title, int64_t thumbnailId );
106
        static std::shared_ptr<Album> createUnknownAlbum( MediaLibraryPtr ml, const Artist* artist );
107 108 109 110 111
        ///
        /// \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
        ///
112
        static Query<IAlbum> search( MediaLibraryPtr ml, const std::string& pattern,
113 114 115 116
                                     const QueryParameters* params );
        static Query<IAlbum> fromArtist( MediaLibraryPtr ml, int64_t artistId, const QueryParameters* params );
        static Query<IAlbum> fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params );
        static Query<IAlbum> listAll( MediaLibraryPtr ml, const QueryParameters* params );
117

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

133
        mutable Cache<std::vector<MediaPtr>> m_tracks;
134
        mutable Cache<std::shared_ptr<Artist>> m_albumArtist;
135
        mutable Cache<std::shared_ptr<Thumbnail>> m_thumbnail;
136

137
        friend struct policy::AlbumTable;
138 139
};

140 141
}

142
#endif // ALBUM_H