mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-05 13:47:26 +02:00
Implement locking mechanism to only allow one thread to contact echonest for loading Styles, Moods & Genres
This commit is contained in:
@@ -29,6 +29,7 @@
|
|||||||
#include "SourceList.h"
|
#include "SourceList.h"
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QMutex>
|
||||||
#include <EchonestCatalogSynchronizer.h>
|
#include <EchonestCatalogSynchronizer.h>
|
||||||
|
|
||||||
using namespace Tomahawk;
|
using namespace Tomahawk;
|
||||||
@@ -41,6 +42,10 @@ QNetworkReply* EchonestGenerator::s_moodsJob = 0;
|
|||||||
QNetworkReply* EchonestGenerator::s_stylesJob = 0;
|
QNetworkReply* EchonestGenerator::s_stylesJob = 0;
|
||||||
QNetworkReply* EchonestGenerator::s_genresJob = 0;
|
QNetworkReply* EchonestGenerator::s_genresJob = 0;
|
||||||
|
|
||||||
|
static QMutex s_moods_mutex;
|
||||||
|
static QMutex s_styles_mutex;
|
||||||
|
static QMutex s_genres_mutex;
|
||||||
|
|
||||||
CatalogManager* EchonestGenerator::s_catalogs = 0;
|
CatalogManager* EchonestGenerator::s_catalogs = 0;
|
||||||
|
|
||||||
|
|
||||||
@@ -615,60 +620,94 @@ EchonestGenerator::loadStylesMoodsAndGenres()
|
|||||||
{
|
{
|
||||||
if( !s_styles.isEmpty() && !s_moods.isEmpty() && !s_genres.isEmpty() )
|
if( !s_styles.isEmpty() && !s_moods.isEmpty() && !s_genres.isEmpty() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
loadStyles();
|
||||||
|
loadMoods();
|
||||||
|
loadGenres();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
EchonestGenerator::loadStyles()
|
||||||
|
{
|
||||||
if ( s_styles.isEmpty() )
|
if ( s_styles.isEmpty() )
|
||||||
{
|
{
|
||||||
QVariant styles = TomahawkUtils::Cache::instance()->getData( "EchonesGenerator", "styles" );
|
if ( s_styles_mutex.tryLock() )
|
||||||
if ( styles.isValid() && styles.canConvert< QStringList >() )
|
|
||||||
{
|
{
|
||||||
s_styles = styles.toStringList();
|
s_styles_mutex.unlock();
|
||||||
|
QVariant styles = TomahawkUtils::Cache::instance()->getData( "EchonesGenerator", "styles" );
|
||||||
|
if ( styles.isValid() && styles.canConvert< QStringList >() )
|
||||||
|
{
|
||||||
|
s_styles = styles.toStringList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s_styles_mutex.lock();
|
||||||
|
tLog() << "Styles not in cache or too old, refetching styles ...";
|
||||||
|
s_stylesJob = Echonest::Artist::listTerms( "style" );
|
||||||
|
connect( s_stylesJob, SIGNAL( finished() ), this, SLOT( stylesReceived() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tLog() << "Styles not in cache or too old, refetching styles ...";
|
connect( this, SIGNAL( stylesSaved() ), this, SLOT( loadStyles() ) );
|
||||||
s_stylesJob = Echonest::Artist::listTerms( "style" );
|
|
||||||
connect( s_stylesJob, SIGNAL( finished() ), this, SLOT( stylesReceived() ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( s_moods.isEmpty() )
|
|
||||||
{
|
|
||||||
QVariant moods = TomahawkUtils::Cache::instance()->getData( "EchonesGenerator", "moods" );
|
|
||||||
if ( moods.isValid() && moods.canConvert< QStringList >() ) {
|
|
||||||
s_moods = moods.toStringList();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tLog() << "Moods not in cache or too old, refetching moods ...";
|
|
||||||
s_moodsJob = Echonest::Artist::listTerms( "mood" );
|
|
||||||
connect( s_moodsJob, SIGNAL( finished() ), this, SLOT( moodsReceived() ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( s_genres.isEmpty() )
|
|
||||||
{
|
|
||||||
QVariant genres = TomahawkUtils::Cache::instance()->getData( "EchonesGenerator", "genres" );
|
|
||||||
if ( genres.isValid() && genres.canConvert< QStringList >() )
|
|
||||||
{
|
|
||||||
s_genres = genres.toStringList();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tLog() << "Genres not in cache or too old, refetching genres ...";
|
|
||||||
s_genresJob = Echonest::Artist::fetchGenres();
|
|
||||||
connect( s_genresJob, SIGNAL( finished() ), this, SLOT( genresReceived() ) );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
EchonestGenerator::saveStylesMoodsAndGenres()
|
EchonestGenerator::loadMoods()
|
||||||
{
|
{
|
||||||
TomahawkUtils::Cache::instance()->putData( "EchonesGenerator", 1209600000 /* 2 weeks */, "moods", QVariant::fromValue< QStringList >( s_moods ) );
|
if ( s_moods.isEmpty() )
|
||||||
TomahawkUtils::Cache::instance()->putData( "EchonesGenerator", 1209600000 /* 2 weeks */, "styles", QVariant::fromValue< QStringList >( s_styles ) );
|
{
|
||||||
TomahawkUtils::Cache::instance()->putData( "EchonesGenerator", 1209600000 /* 2 weeks */, "genres", QVariant::fromValue< QStringList >( s_genres ) );
|
if ( s_moods_mutex.tryLock() )
|
||||||
|
{
|
||||||
|
s_moods_mutex.unlock();
|
||||||
|
QVariant moods = TomahawkUtils::Cache::instance()->getData( "EchonesGenerator", "moods" );
|
||||||
|
if ( moods.isValid() && moods.canConvert< QStringList >() ) {
|
||||||
|
s_moods = moods.toStringList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s_moods_mutex.lock();
|
||||||
|
tLog() << "Moods not in cache or too old, refetching moods ...";
|
||||||
|
s_moodsJob = Echonest::Artist::listTerms( "mood" );
|
||||||
|
connect( s_moodsJob, SIGNAL( finished() ), this, SLOT( moodsReceived() ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
connect( this, SIGNAL( moodsSaved() ), this, SLOT( loadMoods() ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
EchonestGenerator::loadGenres()
|
||||||
|
{
|
||||||
|
if ( s_genres.isEmpty() )
|
||||||
|
{
|
||||||
|
if ( s_genres_mutex.tryLock() )
|
||||||
|
{
|
||||||
|
s_genres_mutex.unlock();
|
||||||
|
QVariant genres = TomahawkUtils::Cache::instance()->getData( "EchonesGenerator", "genres" );
|
||||||
|
if ( genres.isValid() && genres.canConvert< QStringList >() )
|
||||||
|
{
|
||||||
|
s_genres = genres.toStringList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s_genres_mutex.lock();
|
||||||
|
tLog() << "Genres not in cache or too old, refetching genres ...";
|
||||||
|
s_genresJob = Echonest::Artist::fetchGenres();
|
||||||
|
connect( s_genresJob, SIGNAL( finished() ), this, SLOT( genresReceived() ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
connect( this, SIGNAL( genresSaved() ), this, SLOT( loadGenres() ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QStringList
|
QStringList
|
||||||
EchonestGenerator::moods()
|
EchonestGenerator::moods()
|
||||||
@@ -693,8 +732,9 @@ EchonestGenerator::moodsReceived()
|
|||||||
}
|
}
|
||||||
s_moodsJob = 0;
|
s_moodsJob = 0;
|
||||||
|
|
||||||
if( !s_styles.isEmpty() && !s_genres.isEmpty() )
|
TomahawkUtils::Cache::instance()->putData( "EchonesGenerator", 1209600000 /* 2 weeks */, "moods", QVariant::fromValue< QStringList >( s_moods ) );
|
||||||
saveStylesMoodsAndGenres();
|
s_moods_mutex.unlock();
|
||||||
|
emit moodsSaved();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -721,8 +761,9 @@ EchonestGenerator::stylesReceived()
|
|||||||
}
|
}
|
||||||
s_stylesJob = 0;
|
s_stylesJob = 0;
|
||||||
|
|
||||||
if( !s_moods.isEmpty() && !s_styles.isEmpty() )
|
TomahawkUtils::Cache::instance()->putData( "EchonesGenerator", 1209600000 /* 2 weeks */, "styles", QVariant::fromValue< QStringList >( s_styles ) );
|
||||||
saveStylesMoodsAndGenres();
|
s_styles_mutex.unlock();
|
||||||
|
emit stylesSaved();
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList
|
QStringList
|
||||||
@@ -747,6 +788,7 @@ EchonestGenerator::genresReceived()
|
|||||||
}
|
}
|
||||||
s_genresJob = 0;
|
s_genresJob = 0;
|
||||||
|
|
||||||
if( !s_moods.isEmpty() && !s_styles.isEmpty() )
|
TomahawkUtils::Cache::instance()->putData( "EchonesGenerator", 1209600000 /* 2 weeks */, "genres", QVariant::fromValue< QStringList >( s_genres ) );
|
||||||
saveStylesMoodsAndGenres();
|
s_genres_mutex.unlock();
|
||||||
|
emit genresSaved();
|
||||||
}
|
}
|
||||||
|
@@ -90,6 +90,10 @@ public:
|
|||||||
signals:
|
signals:
|
||||||
void paramsGenerated( const Echonest::DynamicPlaylist::PlaylistParams& );
|
void paramsGenerated( const Echonest::DynamicPlaylist::PlaylistParams& );
|
||||||
|
|
||||||
|
void stylesSaved();
|
||||||
|
void moodsSaved();
|
||||||
|
void genresSaved();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void staticFinished();
|
void staticFinished();
|
||||||
void dynamicStarted();
|
void dynamicStarted();
|
||||||
@@ -98,6 +102,10 @@ private slots:
|
|||||||
void doGenerate( const Echonest::DynamicPlaylist::PlaylistParams& params );
|
void doGenerate( const Echonest::DynamicPlaylist::PlaylistParams& params );
|
||||||
void doStartOnDemand( const Echonest::DynamicPlaylist::PlaylistParams& params );
|
void doStartOnDemand( const Echonest::DynamicPlaylist::PlaylistParams& params );
|
||||||
|
|
||||||
|
void loadStyles();
|
||||||
|
void loadMoods();
|
||||||
|
void loadGenres();
|
||||||
|
|
||||||
void stylesReceived();
|
void stylesReceived();
|
||||||
void moodsReceived();
|
void moodsReceived();
|
||||||
void genresReceived();
|
void genresReceived();
|
||||||
@@ -113,7 +121,6 @@ private:
|
|||||||
bool onlyThisArtistType( Echonest::DynamicPlaylist::ArtistTypeEnum type ) const throw( std::runtime_error );
|
bool onlyThisArtistType( Echonest::DynamicPlaylist::ArtistTypeEnum type ) const throw( std::runtime_error );
|
||||||
|
|
||||||
void loadStylesMoodsAndGenres();
|
void loadStylesMoodsAndGenres();
|
||||||
void saveStylesMoodsAndGenres();
|
|
||||||
|
|
||||||
Echonest::DynamicPlaylist* m_dynPlaylist;
|
Echonest::DynamicPlaylist* m_dynPlaylist;
|
||||||
QPixmap m_logo;
|
QPixmap m_logo;
|
||||||
|
Reference in New Issue
Block a user