File indexing completed on 2024-05-05 04:48:20

0001 /****************************************************************************************
0002  * Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de>                                  *
0003  * This program is free software; you can redistribute it and/or modify it under        *
0004  * the terms of the GNU General Public License as published by the Free Software        *
0005  * Foundation; either version 2 of the License, or (at your option) any later           *
0006  * version.                                                                             *
0007  *                                                                                      *
0008  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0009  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0010  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0011  *                                                                                      *
0012  * You should have received a copy of the GNU General Public License along with         *
0013  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0014  ****************************************************************************************/
0015 
0016 #ifndef AMAROK_COVERCACHE_H
0017 #define AMAROK_COVERCACHE_H
0018 
0019 #include "amarok_export.h"
0020 #include "core/meta/forward_declarations.h"
0021 
0022 #include <QHash>
0023 #include <QPixmap>
0024 #include <QPixmapCache>
0025 #include <QReadWriteLock>
0026 
0027 /** The cover cache provides a central location for album covers.
0028     QPixmaps used in many places of the UI have the drawback that they can be only
0029     generated from the UI thread.
0030 
0031     On the other hand the collections should be UI independent and thread save.
0032     To solve this problem the CoverCache class provides a central repository for
0033     Album cover QPixmaps.
0034 */
0035 class AMAROK_EXPORT CoverCache
0036 {
0037     public:
0038         /** Returns the global CoverCache instance */
0039         static CoverCache* instance();
0040 
0041         /** Destroys the global CoverCache instance */
0042         static void destroy();
0043 
0044 
0045         /** Called each time an album cover has changed or is not longer valid.
0046             Actually every album returning an image (or better every single album)
0047             should de-register itself with the CoverCache.
0048             Not doing so will leak a couple of bytes and in bad cases lead to old
0049             covers being returned.
0050         */
0051         static void invalidateAlbum( const Meta::Album* album );
0052 
0053         /** Returns the album cover image.
0054             Returns a default image if no specific album image could be found.
0055 
0056             Note: as this function can create a pixmap it is not recommended to
0057             call this function from outside the UI thread.
0058 
0059             @param album the album to get cover
0060             @param size is the maximum width or height of the resulting image.
0061             when size is <= 1, return the full size image
0062         */
0063         QPixmap getCover( const Meta::AlbumPtr &album, int size = 0 ) const;
0064 
0065     private:
0066         static CoverCache* s_instance;
0067         CoverCache();
0068         ~CoverCache();
0069 
0070         mutable QReadWriteLock m_lock;
0071 
0072         typedef QHash< int, QPixmapCache::Key > CoverKeys;
0073 
0074         /**
0075          * Cache holding all the pixmap keys.
0076          * Don't use smart pointers for the Hash key. Hash keys that change are deadly
0077          */
0078         mutable QHash<const Meta::Album*, CoverKeys> m_keys;
0079 
0080         Q_DISABLE_COPY( CoverCache )
0081 };
0082 
0083 namespace The
0084 {
0085     AMAROK_EXPORT CoverCache* coverCache();
0086 }
0087 
0088 
0089 
0090 #endif