summaryrefslogtreecommitdiffstats
path: root/liblastfm/Track.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'liblastfm/Track.cpp')
-rwxr-xr-xliblastfm/Track.cpp147
1 files changed, 147 insertions, 0 deletions
diff --git a/liblastfm/Track.cpp b/liblastfm/Track.cpp
new file mode 100755
index 0000000..e4aec52
--- /dev/null
+++ b/liblastfm/Track.cpp
@@ -0,0 +1,147 @@
+/*
+ Copyright 2009 Last.fm Ltd.
+ - Primarily authored by Max Howell, Jono Cole and Doug Mansell
+
+ This file is part of liblastfm.
+
+ liblastfm is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ liblastfm is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with liblastfm. If not, see <http://www.gnu.org/licenses/>.
+*/
+#include "Track.h"
+//#include "User.h"
+//#include "../core/UrlBuilder.h"
+#include "XmlQuery.h"
+#include "ws.h"
+#include <QFileInfo>
+#include <QStringList>
+
+
+lastfm::Track::Track()
+{
+ d = new TrackData;
+ d->null = true;
+}
+
+
+lastfm::Track::Track( const QDomElement& e )
+{
+ d = new TrackData;
+
+ if (e.isNull()) { d->null = true; return; }
+
+ d->artist = e.namedItem( "artist" ).toElement().text();
+ d->album = e.namedItem( "album" ).toElement().text();
+ d->title = e.namedItem( "track" ).toElement().text();
+ d->trackNumber = 0;
+ d->duration = e.namedItem( "duration" ).toElement().text().toInt();
+ d->url = e.namedItem( "url" ).toElement().text();
+ d->rating = e.namedItem( "rating" ).toElement().text().toUInt();
+ d->source = e.namedItem( "source" ).toElement().text().toInt(); //defaults to 0, or lastfm::Track::Unknown
+ d->time = QDateTime::fromTime_t( e.namedItem( "timestamp" ).toElement().text().toUInt() );
+
+ QDomNodeList nodes = e.namedItem( "extras" ).childNodes();
+ for (int i = 0; i < nodes.count(); ++i)
+ {
+ QDomNode n = nodes.at(i);
+ QString key = n.nodeName();
+ d->extras[key] = n.toElement().text();
+ }
+}
+
+
+QDomElement
+lastfm::Track::toDomElement( QDomDocument& xml ) const
+{
+ QDomElement item = xml.createElement( "track" );
+
+ #define makeElement( tagname, getter ) { \
+ QString v = getter; \
+ if (!v.isEmpty()) \
+ { \
+ QDomElement e = xml.createElement( tagname ); \
+ e.appendChild( xml.createTextNode( v ) ); \
+ item.appendChild( e ); \
+ } \
+ }
+
+ makeElement( "artist", d->artist );
+ makeElement( "album", d->album );
+ makeElement( "track", d->title );
+ makeElement( "duration", QString::number( d->duration ) );
+ makeElement( "timestamp", QString::number( d->time.toTime_t() ) );
+ makeElement( "url", d->url.toString() );
+ makeElement( "source", QString::number( d->source ) );
+ makeElement( "rating", QString::number(d->rating) );
+ makeElement( "fpId", QString::number(d->fpid) );
+
+ QDomElement extras = xml.createElement( "extras" );
+ QMapIterator<QString, QString> i( d->extras );
+ while (i.hasNext()) {
+ QDomElement e = xml.createElement( i.next().key() );
+ e.appendChild( xml.createTextNode( i.value() ) );
+ extras.appendChild( e );
+ }
+ item.appendChild( extras );
+
+ return item;
+}
+
+
+QString
+lastfm::Track::toString( const QChar& separator ) const
+{
+ if ( d->artist.isEmpty() )
+ {
+ if ( d->title.isEmpty() )
+ return QFileInfo( d->url.path() ).fileName();
+ else
+ return d->title;
+ }
+
+ if ( d->title.isEmpty() )
+ return d->artist;
+
+ return d->artist + ' ' + separator + ' ' + d->title;
+}
+
+
+QString //static
+lastfm::Track::durationString( int const duration )
+{
+ QTime t = QTime().addSecs( duration );
+ if (duration < 60*60)
+ return t.toString( "m:ss" );
+ else
+ return t.toString( "hh:mm:ss" );
+}
+
+
+QMap<QString, QString>
+lastfm::Track::params( const QString& method) const
+{
+ QMap<QString, QString> map;
+ map["method"] = "track."+method;
+ map["artist"] = d->artist;
+ map["track"] = d->title;
+
+ return map;
+}
+
+
+lastfm::Track
+lastfm::Track::clone() const
+{
+ Track copy( *this );
+ copy.d.detach();
+ return copy;
+}