1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-20 07:52:30 +02:00

Pimple Album

This commit is contained in:
Uwe L. Korn 2013-07-16 14:14:24 +02:00
parent 9c1acc7dc2
commit 867e7d5ec0
13 changed files with 206 additions and 122 deletions

View File

@ -17,7 +17,7 @@
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Album.h"
#include "Album_p.h"
#include "database/Database.h"
#include "database/DatabaseImpl.h"
@ -45,12 +45,10 @@ static QReadWriteLock s_idMutex;
Album::~Album()
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Deleting album:" << m_name << m_artist->name();
m_ownRef.clear();
Q_D( Album );
d->ownRef.clear();
#ifndef ENABLE_HEADLESS
delete m_cover;
#endif
delete d->cover;
}
@ -124,56 +122,39 @@ Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& ar
Album::Album( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist )
: QObject()
, m_waitingForId( false )
, m_id( id )
, m_name( name )
, m_artist( artist )
, m_coverLoaded( false )
, m_coverLoading( false )
#ifndef ENABLE_HEADLESS
, m_cover( 0 )
#endif
: d_ptr( new AlbumPrivate( this, id, name, artist ) )
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating album:" << id << name << artist->name();
m_sortname = DatabaseImpl::sortname( name );
Q_D( Album );
d->sortname = DatabaseImpl::sortname( name );
}
Album::Album( const QString& name, const Tomahawk::artist_ptr& artist )
: QObject()
, m_waitingForId( true )
, m_id( 0 )
, m_name( name )
, m_artist( artist )
, m_coverLoaded( false )
, m_coverLoading( false )
#ifndef ENABLE_HEADLESS
, m_cover( 0 )
#endif
: d_ptr( new AlbumPrivate( this, name, artist ) )
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating album:" << name << artist->name();
m_sortname = DatabaseImpl::sortname( name );
Q_D( Album );
d->sortname = DatabaseImpl::sortname( name );
}
void
Album::deleteLater()
{
Q_D( Album );
QMutexLocker lock( &s_nameCacheMutex );
const QString key = albumCacheKey( m_artist, m_name );
const QString key = albumCacheKey( d->artist, d->name );
if ( s_albumsByName.contains( key ) )
{
s_albumsByName.remove( key );
}
if ( m_id > 0 )
if ( d->id > 0 )
{
s_idMutex.lockForWrite();
if ( s_albumsById.contains( m_id ) )
if ( s_albumsById.contains( d->id ) )
{
s_albumsById.remove( m_id );
s_albumsById.remove( d->id );
}
s_idMutex.unlock();
}
@ -192,43 +173,47 @@ Album::onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr&
artist_ptr
Album::artist() const
{
return m_artist;
Q_D( const Album );
return d->artist;
}
void
Album::loadId( bool autoCreate )
{
Q_ASSERT( m_waitingForId );
IdThreadWorker::getAlbumId( m_ownRef.toStrongRef(), autoCreate );
Q_D( Album );
Q_ASSERT( d->waitingForId );
IdThreadWorker::getAlbumId( d->ownRef.toStrongRef(), autoCreate );
}
void
Album::setIdFuture( QFuture<unsigned int> future )
{
m_idFuture = future;
Q_D( Album );
d->idFuture = future;
}
unsigned int
Album::id() const
{
Q_D( const Album );
s_idMutex.lockForRead();
const bool waiting = m_waitingForId;
unsigned int finalId = m_id;
const bool waiting = d->waitingForId;
unsigned int finalId = d->id;
s_idMutex.unlock();
if ( waiting )
{
finalId = m_idFuture.result();
finalId = d->idFuture.result();
s_idMutex.lockForWrite();
m_id = finalId;
m_waitingForId = false;
d->id = finalId;
d->waitingForId = false;
if ( m_id > 0 )
s_albumsById.insert( m_id, m_ownRef.toStrongRef() );
if ( d->id > 0 )
s_albumsById.insert( d->id, d->ownRef.toStrongRef() );
s_idMutex.unlock();
}
@ -240,35 +225,37 @@ Album::id() const
QString
Album::name() const
{
return m_name;
Q_D( const Album );
return d->name;
}
QString
Album::sortname() const
{
return m_sortname;
Q_D( const Album );
return d->sortname;
}
#ifndef ENABLE_HEADLESS
QPixmap
Album::cover( const QSize& size, bool forceLoad ) const
{
if ( name().isEmpty() )
Q_D( const Album );
if ( d->name.isEmpty() )
{
m_coverLoaded = true;
d->coverLoaded = true;
return QPixmap();
}
if ( !m_coverLoaded && !m_coverLoading )
if ( !d->coverLoaded && !d->coverLoading )
{
if ( !forceLoad )
return QPixmap();
Tomahawk::InfoSystem::InfoStringHash trackInfo;
trackInfo["artist"] = artist()->name();
trackInfo["album"] = name();
trackInfo["artist"] = d->artist->name();
trackInfo["album"] = d->name;
Tomahawk::InfoSystem::InfoRequestData requestData;
requestData.caller = infoid();
@ -286,34 +273,34 @@ Album::cover( const QSize& size, bool forceLoad ) const
Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData );
m_coverLoading = true;
d->coverLoading = true;
}
if ( !m_cover && !m_coverBuffer.isEmpty() )
if ( !d->cover && !d->coverBuffer.isEmpty() )
{
QPixmap cover;
cover.loadFromData( m_coverBuffer );
m_coverBuffer.clear();
cover.loadFromData( d->coverBuffer );
d->coverBuffer.clear();
m_cover = new QPixmap( TomahawkUtils::squareCenterPixmap( cover ) );
d->cover = new QPixmap( TomahawkUtils::squareCenterPixmap( cover ) );
}
if ( m_cover && !m_cover->isNull() && !size.isEmpty() )
if ( d->cover && !d->cover->isNull() && !size.isEmpty() )
{
const QString cacheKey = infoid() + "_" + size.width();
QPixmap cover;
if ( !QPixmapCache::find( cacheKey, cover ) )
{
cover = m_cover->scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
cover = d->cover->scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
QPixmapCache::insert( cacheKey, cover );
return cover;
}
return cover;
}
if ( m_cover )
return *m_cover;
if ( d->cover )
return *d->cover;
else
return QPixmap();
}
@ -321,15 +308,15 @@ Album::cover( const QSize& size, bool forceLoad ) const
bool
Album::coverLoaded() const
{
return m_coverLoaded;
Q_D( const Album );
return d->coverLoaded;
}
#endif
void
Album::infoSystemInfo( const Tomahawk::InfoSystem::InfoRequestData& requestData, const QVariant& output )
{
Q_D( Album );
if ( requestData.caller != infoid() ||
requestData.type != Tomahawk::InfoSystem::InfoAlbumCoverArt )
{
@ -338,7 +325,7 @@ Album::infoSystemInfo( const Tomahawk::InfoSystem::InfoRequestData& requestData,
if ( output.isNull() )
{
m_coverLoaded = true;
d->coverLoaded = true;
}
else if ( output.isValid() )
{
@ -346,10 +333,10 @@ Album::infoSystemInfo( const Tomahawk::InfoSystem::InfoRequestData& requestData,
const QByteArray ba = returnedData["imgbytes"].toByteArray();
if ( ba.length() )
{
m_coverBuffer = ba;
d->coverBuffer = ba;
}
m_coverLoaded = true;
d->coverLoaded = true;
emit coverChanged();
}
}
@ -358,6 +345,7 @@ Album::infoSystemInfo( const Tomahawk::InfoSystem::InfoRequestData& requestData,
void
Album::infoSystemFinished( const QString& target )
{
Q_D( Album );
if ( target != infoid() )
return;
@ -367,7 +355,7 @@ Album::infoSystemFinished( const QString& target )
disconnect( Tomahawk::InfoSystem::InfoSystem::instance(), SIGNAL( finished( QString ) ),
this, SLOT( infoSystemFinished( QString ) ) );
m_coverLoading = false;
d->coverLoading = false;
emit updated();
}
@ -375,7 +363,8 @@ Album::infoSystemFinished( const QString& target )
Tomahawk::playlistinterface_ptr
Album::playlistInterface( ModelMode mode, const Tomahawk::collection_ptr& collection )
{
playlistinterface_ptr pli = m_playlistInterface[ mode ][ collection ];
Q_D( Album );
playlistinterface_ptr pli = d->playlistInterface[ mode ][ collection ];
if ( pli.isNull() )
{
@ -383,7 +372,7 @@ Album::playlistInterface( ModelMode mode, const Tomahawk::collection_ptr& collec
connect( pli.data(), SIGNAL( tracksLoaded( Tomahawk::ModelMode, Tomahawk::collection_ptr ) ),
SLOT( onTracksLoaded( Tomahawk::ModelMode, Tomahawk::collection_ptr ) ) );
m_playlistInterface[ mode ][ collection ] = pli;
d->playlistInterface[ mode ][ collection ] = pli;
}
return pli;
@ -393,14 +382,16 @@ Album::playlistInterface( ModelMode mode, const Tomahawk::collection_ptr& collec
QWeakPointer<Album>
Album::weakRef()
{
return m_ownRef;
Q_D( Album );
return d->ownRef;
}
void
Album::setWeakRef( QWeakPointer<Album> weakRef )
{
m_ownRef = weakRef;
Q_D( Album );
d->ownRef = weakRef;
}
@ -414,8 +405,9 @@ Album::tracks( ModelMode mode, const Tomahawk::collection_ptr& collection )
QString
Album::infoid() const
{
if ( m_uuid.isEmpty() )
m_uuid = uuid();
Q_D( const Album );
if ( d->uuid.isEmpty() )
d->uuid = uuid();
return m_uuid;
return d->uuid;
}

View File

@ -18,21 +18,27 @@
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TOMAHAWKALBUM_H
#define TOMAHAWKALBUM_H
#include <QPixmap>
#include <QFuture>
#include "Typedefs.h"
#include "PlaylistInterface.h"
#include "DllMacro.h"
#include "collection/Collection.h"
// Forward Declarations breaking QSharedPointer
#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 )
#include "collection/Collection.h"
#endif
#include "infosystem/InfoSystem.h"
#include "DllMacro.h"
#include "Typedefs.h"
namespace Tomahawk
{
class AlbumPrivate;
class IdThreadWorker;
class DLLEXPORT Album : public QObject
@ -52,9 +58,7 @@ public:
QString sortname() const;
artist_ptr artist() const;
#ifndef ENABLE_HEADLESS
QPixmap cover( const QSize& size, bool forceLoad = true ) const;
#endif
bool coverLoaded() const;
QList<Tomahawk::query_ptr> tracks( ModelMode mode = Mixed, const Tomahawk::collection_ptr& collection = Tomahawk::collection_ptr() );
@ -73,6 +77,9 @@ signals:
void updated();
void coverChanged();
protected:
QScopedPointer<AlbumPrivate> d_ptr;
private slots:
void onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection );
@ -80,31 +87,11 @@ private slots:
void infoSystemFinished( const QString& target );
private:
Q_DECLARE_PRIVATE( Album )
Q_DISABLE_COPY( Album )
QString infoid() const;
void setIdFuture( QFuture<unsigned int> future );
mutable bool m_waitingForId;
mutable QFuture<unsigned int> m_idFuture;
mutable unsigned int m_id;
QString m_name;
QString m_sortname;
artist_ptr m_artist;
mutable bool m_coverLoaded;
mutable bool m_coverLoading;
mutable QString m_uuid;
mutable QByteArray m_coverBuffer;
#ifndef ENABLE_HEADLESS
mutable QPixmap* m_cover;
#endif
QHash< Tomahawk::ModelMode, QHash< Tomahawk::collection_ptr, Tomahawk::playlistinterface_ptr > > m_playlistInterface;
QWeakPointer< Tomahawk::Album > m_ownRef;
static QHash< QString, album_wptr > s_albumsByName;
static QHash< unsigned int, album_wptr > s_albumsById;

84
src/libtomahawk/Album_p.h Normal file
View File

@ -0,0 +1,84 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef ALBUM_P_H
#define ALBUM_P_H
#include "Album.h"
namespace Tomahawk
{
class AlbumPrivate
{
public:
AlbumPrivate( Album* q, unsigned int _id, const QString& _name, const Tomahawk::artist_ptr& _artist )
: q_ptr( q )
, waitingForId( false )
, id( _id )
, name( _name )
, artist( _artist )
, coverLoaded( false )
, coverLoading( false )
, cover( 0 )
{
}
AlbumPrivate( Album* q, const QString& _name, const Tomahawk::artist_ptr& _artist )
: q_ptr( q )
, waitingForId( true )
, id( 0 )
, name( _name )
, artist( _artist )
, coverLoaded( false )
, coverLoading( false )
, cover( 0 )
{
}
Album* q_ptr;
Q_DECLARE_PUBLIC( Album )
private:
mutable bool waitingForId;
mutable QFuture<unsigned int> idFuture;
mutable unsigned int id;
QString name;
QString sortname;
artist_ptr artist;
mutable bool coverLoaded;
mutable bool coverLoading;
mutable QString uuid;
mutable QByteArray coverBuffer;
mutable QPixmap* cover;
QHash< Tomahawk::ModelMode, QHash< Tomahawk::collection_ptr, Tomahawk::playlistinterface_ptr > > playlistInterface;
QWeakPointer< Tomahawk::Album > ownRef;
};
} // namespace Tomahawk
#endif // ALBUM_P_H

View File

@ -18,20 +18,22 @@
#include "Result.h"
#include "Album.h"
#include "collection/Collection.h"
#include "resolvers/Resolver.h"
#include "Source.h"
#include "Pipeline.h"
#include "database/Database.h"
#include "database/DatabaseCommand_Resolve.h"
#include "database/DatabaseCommand_AllTracks.h"
#include "database/DatabaseCommand_AddFiles.h"
#include "filemetadata/MetadataEditor.h"
#include "resolvers/ExternalResolverGui.h"
#include "resolvers/Resolver.h"
#include "utils/TomahawkUtilsGui.h"
#include "utils/Logger.h"
#include "resolvers/ExternalResolverGui.h"
#include "Album.h"
#include "Pipeline.h"
#include "PlaylistInterface.h"
#include "Source.h"
#include "Track.h"
using namespace Tomahawk;

View File

@ -28,12 +28,19 @@
#include "context/pages/RelatedArtistsContext.h"
#include "context/pages/TopTracksContext.h"
#include "context/pages/WikipediaContext.h"
#include "Source.h"
#include "utils/TomahawkStyle.h"
#include "utils/TomahawkUtilsGui.h"
// Forward Declarations breaking QSharedPointer
#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 )
#include "PlaylistInterface.h"
#endif
#include "Source.h"
#include "Track.h"
#define ANIMATION_TIME 450
#define SLIDE_TIME 350

View File

@ -18,6 +18,13 @@
#include "WikipediaContext.h"
#include "Source.h"
#include "Track.h"
// Forward Declarations breaking QSharedPointer
#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 )
#include "PlaylistInterface.h"
#endif
using namespace Tomahawk;

View File

@ -28,6 +28,7 @@
#include "DatabaseImpl.h"
#include "Result.h"
#include "SourceList.h"
#include "Track.h"
namespace Tomahawk
{

View File

@ -25,6 +25,7 @@
#include "Pipeline.h"
#include "PlaylistEntry.h"
#include "SourceList.h"
#include "Track.h"
using namespace Tomahawk;

View File

@ -30,6 +30,7 @@
#include "PlaylistEntry.h"
#include "Result.h"
#include "SourceList.h"
#include "Track.h"
#include <QtAlgorithms>
#include <QCoreApplication>

View File

@ -27,6 +27,7 @@
#include "jobview/JobStatusModel.h"
#include "jobview/ErrorStatusMessage.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include "utils/TomahawkUtilsGui.h"
#include "Artist.h"
@ -40,7 +41,7 @@
#include "SourceList.h"
#include "TomahawkSettings.h"
#include "TomahawkVersion.h"
#include "utils/NetworkAccessManager.h"
#include "Track.h"
#include <QFile>
#include <QFileInfo>

View File

@ -20,19 +20,19 @@
#include "ScriptResolver.h"
#include "Artist.h"
#include "Album.h"
#include "Pipeline.h"
#include "Result.h"
#include "ScriptCollection.h"
#include "SourceList.h"
#include "accounts/AccountConfigWidget.h"
#include "utils/TomahawkUtilsGui.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include "utils/NetworkProxyFactory.h"
#include "Artist.h"
#include "Album.h"
#include "Pipeline.h"
#include "Result.h"
#include "ScriptCollection.h"
#include "SourceList.h"
#include "Track.h"
#include <QtEndian>
#include <QFileInfo>

View File

@ -25,9 +25,10 @@
#include <QPaintEngine>
#include <QTimer>
#include "Logger.h"
#include "Source.h"
#include "TomahawkUtilsGui.h"
#include "Logger.h"
#include "Track.h"
using namespace Tomahawk;

View File

@ -24,7 +24,7 @@
#include "Source.h"
#include "Typedefs.h"
#include "TomahawkSettings.h"
#include "Track.h"
#include "audio/AudioEngine.h"
#include "infosystem/InfoSystem.h"