1 /***************************************************************************************
2 * Copyright (c) 2009 Dan Meltzer <parallelgrapefruit@gmail.com>                        *
3 *                                                                                      *
4 * This program is free software; you can redistribute it and/or modify it under        *
5 * the terms of the GNU General Public License as published by the Free Software        *
6 * Foundation; either version 2 of the License, or (at your option) any later           *
7 * version.                                                                             *
8 *                                                                                      *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12 *                                                                                      *
13 * You should have received a copy of the GNU General Public License along with         *
14 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15 ****************************************************************************************/
16 
17 #include "LastfmReadLabelCapability.h"
18 
19 #include "core/support/Amarok.h"
20 #include "core/support/Debug.h"
21 #include "core/meta/Meta.h"
22 
23 #include <QMap>
24 #include <QNetworkReply>
25 
26 #include <XmlQuery.h>
27 
28 namespace Capabilities
29 {
LastfmReadLabelCapability(Meta::Track * track)30 LastfmReadLabelCapability::LastfmReadLabelCapability( Meta::Track *track )
31     : ReadLabelCapability()
32     , m_track( track )
33 {
34     DEBUG_BLOCK
35     fetchLabels();
36 }
37 
38 void
fetchGlobalLabels()39 LastfmReadLabelCapability::fetchGlobalLabels()
40 {
41     DEBUG_BLOCK
42     AMAROK_NOTIMPLEMENTED
43 }
44 
45 void
fetchLabels()46 LastfmReadLabelCapability::fetchLabels()
47 {
48     DEBUG_BLOCK
49     QMap<QString,QString> query;
50     query[ "method" ] = "track.getTopTags";
51     query[ "track"  ] = m_track->name();
52     query[ "artist" ] = m_track->artist() ? m_track->artist()->name() : QString();
53     query[ "api_key"] = Amarok::lastfmApiKey();
54     m_job  = lastfm::ws::post( query );
55 
56     connect( m_job, &QNetworkReply::finished, this, &LastfmReadLabelCapability::onTagsFetched );
57 }
58 
59 
60 void
onTagsFetched()61 LastfmReadLabelCapability::onTagsFetched()
62 {
63     DEBUG_BLOCK
64     if( !m_job )
65     {
66         debug() << "WARNING: GOT RESULT but no object";
67         return;
68     }
69 
70     switch ( m_job->error() )
71     {
72         case QNetworkReply::NoError:
73         {
74             lastfm::XmlQuery lfm;
75             lfm.parse(m_job->readAll());
76             QList<lastfm::XmlQuery> tags = lfm.children( "tag" );
77             QStringList ret;
78             foreach( const lastfm::XmlQuery &child, tags )
79                 ret.append( child["name"].text() );
80             m_labels = ret;
81             emit labelsFetched( ret );
82             break;
83         }
84         default:
85             break;
86     }
87 }
88 
89 
90 QStringList
labels()91 LastfmReadLabelCapability::labels()
92 {
93     return m_labels;
94 }
95 
96 }
97 
98