File indexing completed on 2024-05-19 04:49:16

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef MEMORYCOLLECTION_H
0018 #define MEMORYCOLLECTION_H
0019 
0020 #include "core/meta/Meta.h"
0021 #include "core/meta/support/MetaKeys.h"
0022 
0023 #include <QReadWriteLock>
0024 
0025 //QMap is slower than QHash but the items are ordered by key
0026 typedef QMap<QString, Meta::TrackPtr> TrackMap;
0027 typedef QMap<QString, Meta::ArtistPtr> ArtistMap;
0028 class AlbumMap : public QMap<Meta::AlbumKey, Meta::AlbumPtr>
0029 {
0030     public:
0031         /**
0032          * Return true if this map contains album with same name and artist as @param album
0033          *
0034          * This is a convenience overload of contains( QString, QString ) that has same
0035          * semantics, i.e. compares albums by name and artist name.
0036          */
0037         bool contains( const Meta::AlbumPtr &album ) const {
0038             return QMap<Meta::AlbumKey, Meta::AlbumPtr>::contains( Meta::AlbumKey( album ) ); }
0039 
0040         /**
0041          * @return @c true if this map contains album named @param name with album artist
0042          * @param artistName Amarok convention is to use empty artistName for compilations.
0043          */
0044         bool contains( const QString &name, const QString &artistName ) const {
0045             return QMap<Meta::AlbumKey, Meta::AlbumPtr>::contains( Meta::AlbumKey( name, artistName ) ); }
0046 
0047         /**
0048          * Inserts new album @param album into this map, using its name and album artist.
0049          * If album has no album artist, empty QString is used as artist key. You must not
0050          * change name and album artist of album after adding it to this map!
0051          */
0052         void insert( const Meta::AlbumPtr &album ) {
0053             QMap<Meta::AlbumKey, Meta::AlbumPtr>::insert( Meta::AlbumKey( album ) , album ); }
0054 
0055         /**
0056          * Remove album from this map that has same name and artist as @param album
0057          */
0058         int remove( const Meta::AlbumPtr &album ) {
0059             return QMap<Meta::AlbumKey, Meta::AlbumPtr>::remove( Meta::AlbumKey( album ) ); }
0060 
0061         /**
0062          * Return pointer to album from this map that has same name and artist as @param album
0063          *
0064          * This is a convenience overload of value( QString, QString ) that has same
0065          * semantics, i.e. compares albums by name and artist name.
0066          */
0067         const Meta::AlbumPtr value( const Meta::AlbumPtr &album ) const {
0068             return QMap<Meta::AlbumKey, Meta::AlbumPtr>::value( Meta::AlbumKey( album ) ); }
0069 
0070         /**
0071          * Return pointer to album from this map that has name @param name and
0072          * album artist @param artistName
0073          */
0074         const Meta::AlbumPtr value( const QString &name, const QString &artistName ) const {
0075             return QMap<Meta::AlbumKey, Meta::AlbumPtr>::value( Meta::AlbumKey( name, artistName ) ); }
0076 };
0077 typedef QMap<QString, Meta::GenrePtr> GenreMap;
0078 typedef QMap<QString, Meta::ComposerPtr> ComposerMap;
0079 typedef QMap<int, Meta::YearPtr> YearMap;
0080 typedef QMap<QString, Meta::LabelPtr> LabelMap;
0081 typedef QHash<Meta::LabelPtr, Meta::TrackList> LabelToTrackMap;
0082 
0083 namespace Collections {
0084 
0085 class MemoryCollection
0086 {
0087     public:
0088         void acquireReadLock() { m_readWriteLock.lockForRead(); }
0089         void releaseLock() { m_readWriteLock.unlock(); }
0090         void acquireWriteLock() { m_readWriteLock.lockForWrite(); }
0091 
0092         const TrackMap &trackMap() const { return m_trackMap; }
0093         const ArtistMap &artistMap() const { return m_artistMap; }
0094         const AlbumMap &albumMap() const { return m_albumMap; }
0095         const GenreMap &genreMap() const { return m_genreMap; }
0096         const ComposerMap &composerMap() const { return m_composerMap; }
0097         const YearMap &yearMap() const { return m_yearMap; }
0098         const LabelMap &labelMap() const { return m_labelMap; }
0099         const LabelToTrackMap &labelToTrackMap() const { return m_labelToTrackMap; }
0100 
0101         void setTrackMap( const TrackMap &map ) { m_trackMap = map; }
0102         void addTrack( Meta::TrackPtr trackPtr ) { m_trackMap.insert( trackPtr->uidUrl(), trackPtr ); }
0103         void setArtistMap( const ArtistMap &map ) { m_artistMap = map; }
0104         void addArtist( Meta::ArtistPtr artistPtr) { m_artistMap.insert( artistPtr->name(), artistPtr ); }
0105         void setAlbumMap( const AlbumMap &map ) { m_albumMap = map; }
0106         void addAlbum ( const Meta::AlbumPtr &albumPtr ) { m_albumMap.insert( albumPtr ); }
0107         void setGenreMap( const GenreMap &map ) { m_genreMap = map; }
0108         void addGenre( Meta::GenrePtr genrePtr) { m_genreMap.insert( genrePtr->name(), genrePtr ); }
0109         void setComposerMap( const ComposerMap &map ) { m_composerMap = map; }
0110         void addComposer( Meta::ComposerPtr composerPtr ) { m_composerMap.insert( composerPtr->name(), composerPtr ); }
0111         void setYearMap( const YearMap &map ) { m_yearMap = map; }
0112         void addYear( Meta::YearPtr yearPtr ) { m_yearMap.insert( yearPtr->year(), yearPtr ); }
0113         void clearLabels()
0114         {
0115             m_labelMap = LabelMap();
0116             m_labelToTrackMap = LabelToTrackMap();
0117         }
0118 
0119         void addLabelToTrack( const Meta::LabelPtr &labelPtr, const Meta::TrackPtr &track )
0120         {
0121             m_labelMap.insert( labelPtr->name(), labelPtr );
0122             Meta::TrackList tracks;
0123             if( m_labelToTrackMap.contains( labelPtr ) )
0124             {
0125                 tracks = m_labelToTrackMap.value( labelPtr );
0126             }
0127             tracks << track;
0128             m_labelToTrackMap.insert( labelPtr, tracks );
0129         }
0130 
0131         /**
0132          * Return a pointer to MemoryCollection's internal lock. Useful to use
0133          * QReadWriteLocker instead of acquireRead/WriteLock() and releaseLock()
0134          */
0135         QReadWriteLock *mapLock() const { return &m_readWriteLock; }
0136 
0137     protected:
0138         mutable QReadWriteLock m_readWriteLock;
0139         TrackMap m_trackMap;
0140         ArtistMap m_artistMap;
0141         AlbumMap m_albumMap;
0142         GenreMap m_genreMap;
0143         ComposerMap m_composerMap;
0144         YearMap m_yearMap;
0145         LabelMap m_labelMap;
0146         LabelToTrackMap m_labelToTrackMap;
0147 
0148 };
0149 
0150 } //namespace Collections
0151 
0152 #endif