File indexing completed on 2024-04-21 04:47:50

0001 /***************************************************************************************
0002 * Copyright (c) 2009 Dan Meltzer <parallelgrapefruit@gmail.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 #include "LastfmReadLabelCapability.h"
0018 
0019 #include "core/support/Amarok.h"
0020 #include "core/support/Debug.h"
0021 #include "core/meta/Meta.h"
0022 
0023 #include <QMap>
0024 #include <QNetworkReply>
0025 
0026 #include <XmlQuery.h>
0027 
0028 namespace Capabilities
0029 {
0030 LastfmReadLabelCapability::LastfmReadLabelCapability( Meta::Track *track )
0031     : ReadLabelCapability()
0032     , m_track( track )
0033 {
0034     DEBUG_BLOCK
0035     fetchLabels();
0036 }
0037 
0038 void
0039 LastfmReadLabelCapability::fetchGlobalLabels()
0040 {
0041     DEBUG_BLOCK
0042     AMAROK_NOTIMPLEMENTED
0043 }
0044 
0045 void
0046 LastfmReadLabelCapability::fetchLabels()
0047 {
0048     DEBUG_BLOCK
0049     QMap<QString,QString> query;
0050     query[ "method" ] = "track.getTopTags";
0051     query[ "track"  ] = m_track->name();
0052     query[ "artist" ] = m_track->artist() ? m_track->artist()->name() : QString();
0053     query[ "api_key"] = Amarok::lastfmApiKey();
0054     m_job  = lastfm::ws::post( query );
0055 
0056     connect( m_job, &QNetworkReply::finished, this, &LastfmReadLabelCapability::onTagsFetched );
0057 }
0058 
0059 
0060 void
0061 LastfmReadLabelCapability::onTagsFetched()
0062 {
0063     DEBUG_BLOCK
0064     if( !m_job )
0065     {
0066         debug() << "WARNING: GOT RESULT but no object";
0067         return;
0068     }
0069 
0070     switch ( m_job->error() )
0071     {
0072         case QNetworkReply::NoError:
0073         {
0074             lastfm::XmlQuery lfm;
0075             lfm.parse(m_job->readAll());
0076             QList<lastfm::XmlQuery> tags = lfm.children( "tag" );
0077             QStringList ret;
0078             foreach( const lastfm::XmlQuery &child, tags )
0079                 ret.append( child["name"].text() );
0080             m_labels = ret;
0081             emit labelsFetched( ret );
0082             break;
0083         }
0084         default:
0085             break;
0086     }
0087 }
0088 
0089 
0090 QStringList
0091 LastfmReadLabelCapability::labels()
0092 {
0093     return m_labels;
0094 }
0095 
0096 }
0097