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/Debug.h"
20
#include "core/meta/Meta.h"
21
22
#include <QMap>
23
#include <QNetworkReply>
24
25
#include <lastfm/XmlQuery>
26
#include <ws.h>
27
#include "core/support/Amarok.h"
28
29
namespace Capabilities
30
{
31
LastfmReadLabelCapability::LastfmReadLabelCapability( Meta::Track *track )
32
    : ReadLabelCapability()
33
    , m_track( track )
34
{
35
    DEBUG_BLOCK
36
    fetchLabels();
37
}
38
39
void
40
LastfmReadLabelCapability::fetchGlobalLabels()
41
{
42
    DEBUG_BLOCK
43
    AMAROK_NOTIMPLEMENTED
44
}
45
46
void
47
LastfmReadLabelCapability::fetchLabels()
48
{
49
    DEBUG_BLOCK
50
    QMap<QString,QString> query;
51
    query[ "method" ] = "track.getTopTags";
52
    query[ "track"  ] = m_track->name();
53
    query[ "artist" ] = m_track->artist() ? m_track->artist()->name() : QString();
54
    query[ "api_key"] = Amarok::lastfmApiKey();
55
    m_job  = lastfm::ws::post( query );
56
57
    connect( m_job, SIGNAL( finished() ), SLOT(onTagsFetched()) );
58
}
59
60
61
void
62
LastfmReadLabelCapability::onTagsFetched()
63
{
64
    DEBUG_BLOCK
65
    if( !m_job )
66
    {
67
        debug() << "WARNING: GOT RESULT but no object";
68
        return;
69
    }
70
71
    switch ( m_job->error() )
72
    {
73
        case QNetworkReply::NoError:
74
        {
75
            lastfm::XmlQuery lfm = 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
91
LastfmReadLabelCapability::labels()
92
{
93
    return m_labels;
94
}
95
96
}
97
98
#include "LastfmReadLabelCapability.moc"