1 /****************************************************************************************
2  * Copyright (c) 2010-2012 Leo Franchi <lfranchi@kde.org>                               *
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 #ifndef ECHONEST_TRACK_P_H
18 #define ECHONEST_TRACK_P_H
19 
20 #include "AudioSummary.h"
21 #include "Config.h"
22 #include "Song.h"
23 
24 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
25     #include <QUrlQuery>
26 #endif
27 
28 #include <QSharedData>
29 #include <QString>
30 #include <QNetworkReply>
31 
32 namespace  Echonest {
doPost(const QUrl & url)33     inline QNetworkReply* doPost(const QUrl& url)
34     {
35         // UGLY :( Build url, then extract the encded query items, put them in the POST body, and send that to the url minus the encoded params.
36         // The final data
37         QByteArray data;
38 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
39         int size = QUrlQuery( url ).queryItems().size();
40         for( int i = 0; i < size; i++ ) {
41             const QPair< QString, QString > item = QUrlQuery( url ).queryItems().at( i );
42             data.append( item.first.toLatin1() + "=" + item.second.toLatin1() + "&" );
43         }
44 #else
45         int size = url.encodedQueryItems().size();
46         for( int i = 0; i < size; i++ ) {
47             const QPair< QByteArray, QByteArray > item = url.encodedQueryItems().at( i );
48             data.append( item.first + "=" + item.second + "&" );
49         }
50 #endif
51         data.truncate( data.size() - 1 ); // remove extra &
52         //qDebug() << "Sending data:" << data << "for method:" << url.path();
53         // strip the extras
54         QUrl url2( url.toString().mid( 0, url.toString().indexOf( QLatin1Char( '?' ) ) ) );
55         QNetworkRequest request = QNetworkRequest( url2 );
56         request.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1String( "application/x-www-form-urlencoded" ) );
57         return Echonest::Config::instance()->nam()->post( request, data );
58     }
59 }
60 
61 class TrackData : public QSharedData
62 {
63 public:
TrackData()64     TrackData() {}
65 
TrackData(const TrackData & other)66     TrackData(const TrackData& other) : QSharedData( other )
67     {
68         analyzer_version = other.analyzer_version;
69         artist = other.artist;
70         bitrate = other.bitrate;
71         id = other.id;
72         md5 = other.md5;
73         release = other.release;
74         samplerate = other.samplerate;
75         status = other.status;
76         title = other.title;
77         catalog = other.catalog;
78         foreign_id = other.foreign_id;
79         release_image = other.release_image;
80         preview_url = other.preview_url;
81 
82     }
83 
84     QString artist;
85     QString analyzer_version;
86     int bitrate;
87     QByteArray id;
88     QByteArray md5;
89     QString release;
90     QByteArray audio_md5;
91     int samplerate;
92     QString status;
93     QString title;
94     // used when fetched as a foreign id in a tracks bucket
95     QString catalog;
96     QByteArray foreign_id;
97     QUrl release_image;
98     QUrl preview_url;
99 
100     // song tracks have an associated song
101     Echonest::Song song;
102 
103     Echonest::AudioSummary audio_summary;
104 
105 };
106 
107 #endif
108