~ubuntu-branches/ubuntu/saucy/liblastfm/saucy-proposed

« back to all changes in this revision

Viewing changes to src/types/Track.cpp

  • Committer: Bazaar Package Importer
  • Author(s): John Stamp
  • Date: 2009-07-14 11:14:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090714111408-3fonzjm83fx8618o
Tags: upstream-0.4.0~git20090710
ImportĀ upstreamĀ versionĀ 0.4.0~git20090710

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright 2009 Last.fm Ltd. 
 
3
      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
 
4
 
 
5
   This file is part of liblastfm.
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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/>.
 
19
*/
 
20
#include "Track.h"
 
21
#include "User.h"
 
22
#include "../core/UrlBuilder.h"
 
23
#include "../core/XmlQuery.h"
 
24
#include "../ws/ws.h"
 
25
#include <QFileInfo>
 
26
#include <QStringList>
 
27
 
 
28
 
 
29
lastfm::Track::Track()
 
30
{
 
31
    d = new TrackData;
 
32
    d->null = true;
 
33
}
 
34
 
 
35
 
 
36
lastfm::Track::Track( const QDomElement& e )
 
37
{
 
38
    d = new TrackData;
 
39
 
 
40
    if (e.isNull()) { d->null = true; return; }
 
41
    
 
42
    d->artist = e.namedItem( "artist" ).toElement().text();
 
43
    d->album =  e.namedItem( "album" ).toElement().text();
 
44
    d->title = e.namedItem( "track" ).toElement().text();
 
45
    d->trackNumber = 0;
 
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() );
 
51
    
 
52
    QDomNodeList nodes = e.namedItem( "extras" ).childNodes();
 
53
    for (int i = 0; i < nodes.count(); ++i)
 
54
    {
 
55
        QDomNode n = nodes.at(i);
 
56
        QString key = n.nodeName();
 
57
        d->extras[key] = n.toElement().text();
 
58
    }
 
59
}
 
60
 
 
61
 
 
62
QDomElement
 
63
lastfm::Track::toDomElement( QDomDocument& xml ) const
 
64
{
 
65
    QDomElement item = xml.createElement( "track" );
 
66
    
 
67
    #define makeElement( tagname, getter ) { \
 
68
        QString v = getter; \
 
69
        if (!v.isEmpty()) \
 
70
        { \
 
71
            QDomElement e = xml.createElement( tagname ); \
 
72
            e.appendChild( xml.createTextNode( v ) ); \
 
73
            item.appendChild( e ); \
 
74
        } \
 
75
    }
 
76
 
 
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() );
 
87
 
 
88
    QDomElement extras = xml.createElement( "extras" );
 
89
    QMapIterator<QString, QString> i( d->extras );
 
90
    while (i.hasNext()) {
 
91
        QDomElement e = xml.createElement( i.next().key() );
 
92
        e.appendChild( xml.createTextNode( i.value() ) );
 
93
        extras.appendChild( e );
 
94
    }
 
95
    item.appendChild( extras );
 
96
 
 
97
    return item;
 
98
}
 
99
 
 
100
 
 
101
QString
 
102
lastfm::Track::toString( const QChar& separator ) const
 
103
{
 
104
    if ( d->artist.isEmpty() )
 
105
    {
 
106
        if ( d->title.isEmpty() )
 
107
            return QFileInfo( d->url.path() ).fileName();
 
108
        else
 
109
            return d->title;
 
110
    }
 
111
 
 
112
    if ( d->title.isEmpty() )
 
113
        return d->artist;
 
114
 
 
115
    return d->artist + ' ' + separator + ' ' + d->title;
 
116
}
 
117
 
 
118
 
 
119
QString //static
 
120
lastfm::Track::durationString( int const duration )
 
121
{
 
122
    QTime t = QTime().addSecs( duration );
 
123
    if (duration < 60*60)
 
124
        return t.toString( "m:ss" );
 
125
    else
 
126
        return t.toString( "hh:mm:ss" );
 
127
}
 
128
 
 
129
 
 
130
QNetworkReply*
 
131
lastfm::Track::share( const User& recipient, const QString& message )
 
132
{
 
133
    QMap<QString, QString> map = params("share");
 
134
    map["recipient"] = recipient;
 
135
    if (message.size()) map["message"] = message;
 
136
    return ws::post(map);
 
137
}
 
138
 
 
139
 
 
140
QNetworkReply*
 
141
lastfm::MutableTrack::love()
 
142
{
 
143
    if (d->extras.value("rating").size())
 
144
        return 0;
 
145
    d->extras["rating"] = "L";
 
146
    return ws::post(params("love"));
 
147
}
 
148
 
 
149
 
 
150
QNetworkReply*
 
151
lastfm::MutableTrack::ban()
 
152
{
 
153
    d->extras["rating"] = "B";
 
154
    return ws::post(params("ban"));
 
155
}
 
156
 
 
157
 
 
158
void
 
159
lastfm::MutableTrack::unlove()
 
160
{
 
161
    QString& r = d->extras["rating"];
 
162
    if (r == "L") r = "";
 
163
}
 
164
 
 
165
 
 
166
QMap<QString, QString>
 
167
lastfm::Track::params( const QString& method, bool use_mbid ) const
 
168
{
 
169
    QMap<QString, QString> map;
 
170
    map["method"] = "track."+method;
 
171
    if (d->mbid.size() && use_mbid)
 
172
        map["mbid"] = d->mbid;
 
173
    else {
 
174
        map["artist"] = d->artist;
 
175
        map["track"] = d->title;
 
176
    }
 
177
    return map;
 
178
}
 
179
 
 
180
 
 
181
QNetworkReply*
 
182
lastfm::Track::getTopTags() const
 
183
{
 
184
    return ws::get( params("getTopTags", true) );
 
185
}
 
186
 
 
187
 
 
188
QNetworkReply*
 
189
lastfm::Track::getTags() const
 
190
{
 
191
    return ws::get( params("getTags", true) );
 
192
}
 
193
 
 
194
 
 
195
QNetworkReply*
 
196
lastfm::Track::addTags( const QStringList& tags ) const
 
197
{
 
198
    if (tags.isEmpty())
 
199
        return 0;
 
200
    QMap<QString, QString> map = params("addTags");
 
201
    map["tags"] = tags.join( QChar(',') );
 
202
    return ws::post(map);
 
203
}
 
204
 
 
205
 
 
206
QNetworkReply*
 
207
lastfm::Track::removeTag( const QString& tag ) const
 
208
{
 
209
    if (tag.isEmpty())
 
210
        return 0;
 
211
    QMap<QString, QString> map = params( "removeTag" );
 
212
    map["tags"] = tag;
 
213
    return ws::post(map);
 
214
}
 
215
 
 
216
 
 
217
QUrl
 
218
lastfm::Track::www() const
 
219
{
 
220
    return UrlBuilder( "music" ).slash( d->artist ).slash( "_" ).slash( d->title ).url();
 
221
}
 
222
 
 
223
 
 
224
bool
 
225
lastfm::Track::isMp3() const
 
226
{
 
227
    //FIXME really we should check the file header?
 
228
    return d->url.scheme() == "file" &&
 
229
           d->url.path().endsWith( ".mp3", Qt::CaseInsensitive );
 
230
}
 
231
 
 
232
 
 
233
lastfm::Track
 
234
lastfm::Track::clone() const
 
235
{
 
236
    Track copy( *this );
 
237
    copy.d.detach();
 
238
    return copy;
 
239
}