From 5364dfaf40391a36090ef09db05396b2ce84ef7d Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Tue, 5 Mar 2013 10:30:19 +0100 Subject: [PATCH] Allow script resolvers to choose icons for their scriptcollections. --- .../resolvers/QtScriptResolver.cpp | 56 +++++++++++++++++++ src/libtomahawk/resolvers/QtScriptResolver.h | 3 + .../resolvers/ScriptCollection.cpp | 22 +++++--- src/libtomahawk/resolvers/ScriptCollection.h | 5 ++ 4 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/libtomahawk/resolvers/QtScriptResolver.cpp b/src/libtomahawk/resolvers/QtScriptResolver.cpp index 5d92e1cd3..cbea00a84 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.cpp +++ b/src/libtomahawk/resolvers/QtScriptResolver.cpp @@ -39,6 +39,7 @@ #include "config.h" +#include #include #include #include @@ -925,16 +926,71 @@ QtScriptResolver::loadCollections() sc->setTrackCount( trackCount ); } + if ( collectionInfo.contains( "iconfile" ) ) + { + bool ok = false; + QString iconPath = QFileInfo( filePath() ).path() + "/" + + collectionInfo.value( "iconfile" ).toString(); + + QPixmap iconPixmap; + ok = iconPixmap.load( iconPath ); + if ( ok && !iconPixmap.isNull() ) + sc->setIcon( QIcon( iconPixmap ) ); + } + Tomahawk::collection_ptr collection( sc ); m_collections.insert( collection->name(), collection ); emit collectionAdded( collection ); + if ( collectionInfo.contains( "iconurl" ) ) + { + QString iconUrlString = collectionInfo.value( "iconurl" ).toString(); + if ( !iconUrlString.isEmpty() ) + { + QUrl iconUrl = QUrl::fromEncoded( iconUrlString.toLatin1() ); + if ( iconUrl.isValid() ) + { + QNetworkRequest req( iconUrl ); + tDebug() << "Creating a QNetworkReply with url:" << req.url().toString(); + QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + reply->setProperty( "collectionName", collection->name() ); + + connect( reply, SIGNAL( finished() ), + this, SLOT( onCollectionIconFetched() ) ); + } + } + } + //TODO: implement multiple collections from a resolver } } +void +QtScriptResolver::onCollectionIconFetched() +{ + QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() ); + if ( reply != 0 ) + { + Tomahawk::collection_ptr collection; + collection = m_collections.value( reply->property( "collectionName" ).toString() ); + if ( !collection.isNull() ) + { + if ( reply->error() == QNetworkReply::NoError ) + { + QImageReader imageReader( reply ); + QPixmap collectionIcon = QPixmap::fromImageReader( &imageReader ); + + if ( !collectionIcon.isNull() ) + qobject_cast< Tomahawk::ScriptCollection* >( collection.data() )->setIcon( collectionIcon ); + } + } + reply->deleteLater(); + } +} + + QVariantMap QtScriptResolver::resolverSettings() { diff --git a/src/libtomahawk/resolvers/QtScriptResolver.h b/src/libtomahawk/resolvers/QtScriptResolver.h index c2b8fa3c0..4156bbe71 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.h +++ b/src/libtomahawk/resolvers/QtScriptResolver.h @@ -181,6 +181,9 @@ public slots: signals: void stopped(); +private slots: + void onCollectionIconFetched(); + private: void init(); diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index cced05a4e..4109c1a44 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -27,7 +27,6 @@ #include "resolvers/ScriptCommand_AllAlbums.h" #include "resolvers/ScriptCommand_AllTracks.h" -#include #include using namespace Tomahawk; @@ -45,6 +44,12 @@ ScriptCollection::ScriptCollection( const source_ptr& source, m_resolver = resolver; m_servicePrettyName = m_resolver->name(); + + ExternalResolverGui* gResolver = qobject_cast< ExternalResolverGui* >( m_resolver ); + if ( gResolver ) + { + m_icon = gResolver->icon(); + } } @@ -77,15 +82,18 @@ ScriptCollection::itemName() const } +void +ScriptCollection::setIcon( const QIcon& icon ) +{ + m_icon = icon; + emit changed(); +} + + QIcon ScriptCollection::icon() const { - ExternalResolverGui* gResolver = qobject_cast< ExternalResolverGui* >( m_resolver ); - if ( gResolver ) - { - return gResolver->icon(); - } - return QIcon(); + return m_icon; } diff --git a/src/libtomahawk/resolvers/ScriptCollection.h b/src/libtomahawk/resolvers/ScriptCollection.h index 7bfb12b47..1dbbfc549 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.h +++ b/src/libtomahawk/resolvers/ScriptCollection.h @@ -28,6 +28,8 @@ #include "Typedefs.h" #include "DllMacro.h" +#include + namespace Tomahawk { @@ -55,6 +57,8 @@ public: virtual QString prettyName() const; virtual QString itemName() const; virtual BackendType backendType() const { return ScriptCollectionType; } + + virtual void setIcon( const QIcon& icon ); virtual QIcon icon() const; virtual QPixmap bigIcon() const; @@ -75,6 +79,7 @@ private: QString m_servicePrettyName; QString m_description; int m_trackCount; + QIcon m_icon; }; } //ns