2
Copyright 2009 Last.fm Ltd.
3
- Primarily authored by Max Howell, Jono Cole and Doug Mansell
5
This file is part of liblastfm.
7
liblastfm is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
12
liblastfm is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with liblastfm. If not, see <http://www.gnu.org/licenses/>.
22
#include "../core/UrlBuilder.h"
23
#include "../core/XmlQuery.h"
26
#include <QStringList>
29
lastfm::Track::Track()
36
lastfm::Track::Track( const QDomElement& e )
40
if (e.isNull()) { d->null = true; return; }
42
d->artist = e.namedItem( "artist" ).toElement().text();
43
d->album = e.namedItem( "album" ).toElement().text();
44
d->title = e.namedItem( "track" ).toElement().text();
46
d->duration = e.namedItem( "duration" ).toElement().text().toInt();
47
d->url = e.namedItem( "url" ).toElement().text();
48
d->rating = e.namedItem( "rating" ).toElement().text().toUInt();
49
d->source = e.namedItem( "source" ).toElement().text().toInt(); //defaults to 0, or lastfm::Track::Unknown
50
d->time = QDateTime::fromTime_t( e.namedItem( "timestamp" ).toElement().text().toUInt() );
52
QDomNodeList nodes = e.namedItem( "extras" ).childNodes();
53
for (int i = 0; i < nodes.count(); ++i)
55
QDomNode n = nodes.at(i);
56
QString key = n.nodeName();
57
d->extras[key] = n.toElement().text();
63
lastfm::Track::toDomElement( QDomDocument& xml ) const
65
QDomElement item = xml.createElement( "track" );
67
#define makeElement( tagname, getter ) { \
71
QDomElement e = xml.createElement( tagname ); \
72
e.appendChild( xml.createTextNode( v ) ); \
73
item.appendChild( e ); \
77
makeElement( "artist", d->artist );
78
makeElement( "album", d->album );
79
makeElement( "track", d->title );
80
makeElement( "duration", QString::number( d->duration ) );
81
makeElement( "timestamp", QString::number( d->time.toTime_t() ) );
82
makeElement( "url", d->url.toString() );
83
makeElement( "source", QString::number( d->source ) );
84
makeElement( "rating", QString::number(d->rating) );
85
makeElement( "fpId", QString::number(d->fpid) );
86
makeElement( "mbId", mbid() );
88
QDomElement extras = xml.createElement( "extras" );
89
QMapIterator<QString, QString> i( d->extras );
91
QDomElement e = xml.createElement( i.next().key() );
92
e.appendChild( xml.createTextNode( i.value() ) );
93
extras.appendChild( e );
95
item.appendChild( extras );
102
lastfm::Track::toString( const QChar& separator ) const
104
if ( d->artist.isEmpty() )
106
if ( d->title.isEmpty() )
107
return QFileInfo( d->url.path() ).fileName();
112
if ( d->title.isEmpty() )
115
return d->artist + ' ' + separator + ' ' + d->title;
120
lastfm::Track::durationString( int const duration )
122
QTime t = QTime().addSecs( duration );
123
if (duration < 60*60)
124
return t.toString( "m:ss" );
126
return t.toString( "hh:mm:ss" );
131
lastfm::Track::share( const User& recipient, const QString& message )
133
QMap<QString, QString> map = params("share");
134
map["recipient"] = recipient;
135
if (message.size()) map["message"] = message;
136
return ws::post(map);
141
lastfm::MutableTrack::love()
143
if (d->extras.value("rating").size())
145
d->extras["rating"] = "L";
146
return ws::post(params("love"));
151
lastfm::MutableTrack::ban()
153
d->extras["rating"] = "B";
154
return ws::post(params("ban"));
159
lastfm::MutableTrack::unlove()
161
QString& r = d->extras["rating"];
162
if (r == "L") r = "";
166
QMap<QString, QString>
167
lastfm::Track::params( const QString& method, bool use_mbid ) const
169
QMap<QString, QString> map;
170
map["method"] = "track."+method;
171
if (d->mbid.size() && use_mbid)
172
map["mbid"] = d->mbid;
174
map["artist"] = d->artist;
175
map["track"] = d->title;
182
lastfm::Track::getTopTags() const
184
return ws::get( params("getTopTags", true) );
189
lastfm::Track::getTags() const
191
return ws::get( params("getTags", true) );
196
lastfm::Track::addTags( const QStringList& tags ) const
200
QMap<QString, QString> map = params("addTags");
201
map["tags"] = tags.join( QChar(',') );
202
return ws::post(map);
207
lastfm::Track::removeTag( const QString& tag ) const
211
QMap<QString, QString> map = params( "removeTag" );
213
return ws::post(map);
218
lastfm::Track::www() const
220
return UrlBuilder( "music" ).slash( d->artist ).slash( "_" ).slash( d->title ).url();
225
lastfm::Track::isMp3() const
227
//FIXME really we should check the file header?
228
return d->url.scheme() == "file" &&
229
d->url.path().endsWith( ".mp3", Qt::CaseInsensitive );
234
lastfm::Track::clone() const