Changeset View
Changeset View
Standalone View
Standalone View
src/core-impl/collections/playdarcollection/support/Query.cpp
Show All 14 Lines | |||||
15 | 15 | | |||
16 | #include "Query.h" | 16 | #include "Query.h" | ||
17 | 17 | | |||
18 | #include "Controller.h" | 18 | #include "Controller.h" | ||
19 | #include "core/meta/Meta.h" | 19 | #include "core/meta/Meta.h" | ||
20 | #include "../PlaydarMeta.h" | 20 | #include "../PlaydarMeta.h" | ||
21 | #include "core/support/Debug.h" | 21 | #include "core/support/Debug.h" | ||
22 | 22 | | |||
23 | #include <qjson/parser.h> | 23 | #include <QJsonArray> | ||
24 | 24 | #include <QJsonDocument> | |||
25 | #include <QUrl> | 25 | #include <QJsonObject> | ||
26 | #include <KIO/Job> | 26 | #include <QJsonParseError> | ||
27 | 27 | #include <QMap> | |||
28 | #include <QString> | 28 | #include <QString> | ||
29 | #include <QUrl> | ||||
29 | #include <QVariant> | 30 | #include <QVariant> | ||
30 | #include <QMap> | | |||
31 | #include <QVariantMap> | 31 | #include <QVariantMap> | ||
32 | 32 | | |||
33 | #include <KIO/Job> | ||||
34 | | ||||
35 | | ||||
33 | namespace Playdar | 36 | namespace Playdar | ||
34 | { | 37 | { | ||
35 | Query::Query( const QString &qid, | 38 | Query::Query( const QString &qid, | ||
36 | Playdar::Controller* controller, | 39 | Playdar::Controller* controller, | ||
37 | bool waitForSolution ) | 40 | bool waitForSolution ) | ||
38 | : m_controller( controller ) | 41 | : m_controller( controller ) | ||
39 | , m_waitForSolution( waitForSolution ) | 42 | , m_waitForSolution( waitForSolution ) | ||
40 | , m_qid( qid ) | 43 | , m_qid( qid ) | ||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 121 | if( resultsJob->error() != 0 ) { | |||
119 | debug() << "Error getting results from Playdar"; | 122 | debug() << "Error getting results from Playdar"; | ||
120 | emit playdarError( Playdar::Controller::ErrorState( 1 ) ); | 123 | emit playdarError( Playdar::Controller::ErrorState( 1 ) ); | ||
121 | return; | 124 | return; | ||
122 | } | 125 | } | ||
123 | 126 | | |||
124 | debug() << "Processing received JSON data..."; | 127 | debug() << "Processing received JSON data..."; | ||
125 | KIO::StoredTransferJob* storedResultsJob = static_cast<KIO::StoredTransferJob*>( resultsJob ); | 128 | KIO::StoredTransferJob* storedResultsJob = static_cast<KIO::StoredTransferJob*>( resultsJob ); | ||
126 | 129 | | |||
127 | QJson::Parser parser; | 130 | QJsonParseError err; | ||
128 | bool ok; | 131 | auto doc = QJsonDocument::fromJson( storedResultsJob->data(), &err ); | ||
129 | QVariant parsedResultsVariant; | 132 | | ||
130 | parsedResultsVariant = parser.parse( storedResultsJob->data(),&ok ); | 133 | if ( err.error != QJsonParseError::NoError ) | ||
131 | if ( !ok ) | 134 | debug() << "Error parsing JSON Data:" << err.errorString(); | ||
135 | | ||||
136 | if( !doc.isObject() ) | ||||
132 | { | 137 | { | ||
133 | debug() << "Error parsing JSON Data"; | 138 | debug() << "Parsed Json data is not an object"; | ||
139 | return; | ||||
134 | } | 140 | } | ||
135 | 141 | | |||
136 | QVariantMap parsedResults = parsedResultsVariant.toMap(); | 142 | auto object = doc.object(); | ||
137 | if( !parsedResults.contains( "results" ) ) | 143 | | ||
144 | if( !object.contains( "results" ) ) | ||||
138 | { | 145 | { | ||
139 | debug() << "Expecting results in Playdar's response, received none"; | 146 | debug() << "Expecting results in Playdar's response, received none"; | ||
140 | emit playdarError( Playdar::Controller::ErrorState( 6 ) ); | 147 | emit playdarError( Playdar::Controller::ErrorState( 6 ) ); | ||
141 | return; | 148 | return; | ||
142 | } | 149 | } | ||
143 | if( !parsedResults.contains( "qid" ) ) | 150 | if( !object.contains( "qid" ) ) | ||
144 | { | 151 | { | ||
145 | debug() << "Expected qid in Playdar's response, received none"; | 152 | debug() << "Expected qid in Playdar's response, received none"; | ||
146 | emit playdarError( Playdar::Controller::ErrorState( 4 ) ); | 153 | emit playdarError( Playdar::Controller::ErrorState( 4 ) ); | ||
147 | return; | 154 | return; | ||
148 | } | 155 | } | ||
149 | if( parsedResults.value( "qid" ) != m_qid ) | 156 | if( object.value( "qid" ) != m_qid ) | ||
150 | { | 157 | { | ||
151 | debug() << "A query received the wrong results from Playdar..."; | 158 | debug() << "A query received the wrong results from Playdar..."; | ||
152 | emit playdarError( Playdar::Controller::ErrorState( 5 ) ); | 159 | emit playdarError( Playdar::Controller::ErrorState( 5 ) ); | ||
153 | return; | 160 | return; | ||
154 | } | 161 | } | ||
155 | 162 | | |||
156 | m_artist = parsedResults.value( "artist" ).toString(); | 163 | m_artist = object.value( "artist" ).toString(); | ||
157 | m_album = parsedResults.value( "album" ).toString(); | 164 | m_album = object.value( "album" ).toString(); | ||
158 | m_title = parsedResults.value( "track" ).toString(); | 165 | m_title = object.value( "track" ).toString(); | ||
159 | 166 | | |||
160 | foreach( const QVariant &resultVariant, parsedResults.value( "results" ).toList() ) | 167 | for( const auto &resultVariant : object.value( "results" ).toArray() ) | ||
161 | { | 168 | { | ||
162 | QVariantMap result = resultVariant.toMap(); | 169 | auto result = resultVariant.toObject(); | ||
163 | Meta::PlaydarTrackPtr aTrack; | 170 | Meta::PlaydarTrackPtr aTrack; | ||
164 | QUrl resultUrl( m_controller->urlForSid( result.value( "sid" ).toString() ) ); | 171 | QUrl resultUrl( m_controller->urlForSid( result.value( "sid" ).toString() ) ); | ||
165 | 172 | | |||
166 | QString trackSid = result.value( "sid" ).toString(); | 173 | QString trackSid = result.value( "sid" ).toString(); | ||
167 | QString trackUrl = resultUrl.url(); | 174 | QString trackUrl = resultUrl.url(); | ||
168 | QString trackTitle = result.value( "track" ).toString(); | 175 | QString trackTitle = result.value( "track" ).toString(); | ||
169 | QString trackArtist = result.value( "artist" ).toString(); | 176 | QString trackArtist = result.value( "artist" ).toString(); | ||
170 | QString trackAlbum = result.value( "album" ).toString(); | 177 | QString trackAlbum = result.value( "album" ).toString(); | ||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |