Album.h 5.49 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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
26
#include <memory>
27
#include <sqlite3.h>
28
#include <mutex>
29

30
#include "medialibrary/IMediaLibrary.h"
31

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

36 37 38
namespace medialibrary
{

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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
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;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
51 52 53
};
}

54
class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
55 56
{
    public:
57
        Album( MediaLibraryPtr ml, sqlite::Row& row );
58 59
        Album( MediaLibraryPtr ml, const std::string& title );
        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 77
        virtual const std::string& artworkMrl() const override;
        bool setArtworkMrl( const std::string& artworkMrl );
78 79
        virtual std::vector<MediaPtr> tracks( SortingCriteria sort, bool desc ) const override;
        virtual std::vector<MediaPtr> tracks( GenrePtr genre, SortingCriteria sort, bool desc ) const override;
80 81 82 83 84
        ///
        /// \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
        ///
85
        std::vector<MediaPtr> cachedTracks() const;
86 87
        ///
        /// \brief addTrack Add a track to the album.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
88
        /// This will modify the media, but *not* save it.
89 90 91
        /// The media will be added to the tracks cache.
        ///
        std::shared_ptr<AlbumTrack> addTrack( std::shared_ptr<Media> media, unsigned int trackNb, unsigned int discNumber);
92
        unsigned int nbTracks() const override;
93
        unsigned int duration() const override;
94

95
        virtual ArtistPtr albumArtist() const override;
96
        bool setAlbumArtist( std::shared_ptr<Artist> artist );
97
        virtual std::vector<ArtistPtr> artists(bool desc) const override;
98
        bool addArtist( std::shared_ptr<Artist> artist );
99
        bool removeArtist( Artist* artist );
100

101
        static bool createTable( DBConnection dbConnection );
102
        static bool createTriggers( DBConnection dbConnection );
103 104
        static std::shared_ptr<Album> create( MediaLibraryPtr ml, const std::string& title );
        static std::shared_ptr<Album> createUnknownAlbum( MediaLibraryPtr ml, const Artist* artist );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
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 111 112 113
        static std::vector<AlbumPtr> search( MediaLibraryPtr ml, const std::string& pattern );
        static std::vector<AlbumPtr> fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc );
        static std::vector<AlbumPtr> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc );
        static std::vector<AlbumPtr> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc );
114

115
    private:
116 117
        static std::string orderTracksBy( SortingCriteria sort, bool desc );
        static std::string orderBy( SortingCriteria sort, bool desc );
118
    protected:
119
        MediaLibraryPtr m_ml;
120
        int64_t m_id;
121
        std::string m_title;
122
        int64_t m_artistId;
123
        unsigned int m_releaseYear;
124
        std::string m_shortSummary;
125
        std::string m_artworkMrl;
126
        unsigned int m_nbTracks;
127
        unsigned int m_duration;
128
        bool m_isPresent;
129

130
        mutable Cache<std::vector<MediaPtr>> m_tracks;
131
        mutable Cache<std::shared_ptr<Artist>> m_albumArtist;
132

133
        friend struct policy::AlbumTable;
134 135
};

136 137
}

138
#endif // ALBUM_H