Changeset View
Changeset View
Standalone View
Standalone View
src/core-impl/collections/playdarcollection/support/Controller.cpp
Show All 15 Lines | |||||
16 | 16 | | |||
17 | #define DEBUG_PREFIX "Playdar::Controller" | 17 | #define DEBUG_PREFIX "Playdar::Controller" | ||
18 | 18 | | |||
19 | #include "Controller.h" | 19 | #include "Controller.h" | ||
20 | 20 | | |||
21 | #include "Query.h" | 21 | #include "Query.h" | ||
22 | #include "core/support/Debug.h" | 22 | #include "core/support/Debug.h" | ||
23 | 23 | | |||
24 | #include <qjson/parser.h> | 24 | #include <QJsonDocument> | ||
25 | 25 | #include <QJsonObject> | |||
26 | #include <KIO/Job> | 26 | #include <QJsonParseError> | ||
27 | | ||||
28 | #include <QMap> | 27 | #include <QMap> | ||
29 | #include <QString> | 28 | #include <QString> | ||
30 | #include <QUrl> | 29 | #include <QUrl> | ||
31 | #include <QUrlQuery> | 30 | #include <QUrlQuery> | ||
32 | #include <QVariant> | 31 | #include <QVariant> | ||
33 | #include <QVariantMap> | 32 | #include <QVariantMap> | ||
34 | 33 | | |||
34 | #include <KIO/Job> | ||||
35 | | ||||
36 | | ||||
35 | namespace Playdar { | 37 | namespace Playdar { | ||
36 | 38 | | |||
37 | Controller::Controller( bool queriesShouldWaitForSolutions ) | 39 | Controller::Controller( bool queriesShouldWaitForSolutions ) | ||
38 | : m_errorState( ErrorState( NoError ) ) | 40 | : m_errorState( ErrorState( NoError ) ) | ||
39 | , m_queriesShouldWaitForSolutions( queriesShouldWaitForSolutions ) | 41 | , m_queriesShouldWaitForSolutions( queriesShouldWaitForSolutions ) | ||
40 | 42 | | |||
41 | { | 43 | { | ||
42 | DEBUG_BLOCK | 44 | DEBUG_BLOCK | ||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Line(s) | 135 | if( statusJob->error() != 0 ) { | |||
134 | // debug() << "Error getting status from Playdar"; | 136 | // debug() << "Error getting status from Playdar"; | ||
135 | emit playdarError( Playdar::Controller::ErrorState( ExternalError ) ); | 137 | emit playdarError( Playdar::Controller::ErrorState( ExternalError ) ); | ||
136 | return; | 138 | return; | ||
137 | } | 139 | } | ||
138 | 140 | | |||
139 | debug() << "Processing received JSON data..."; | 141 | debug() << "Processing received JSON data..."; | ||
140 | KIO::StoredTransferJob* storedStatusJob = static_cast<KIO::StoredTransferJob*>( statusJob ); | 142 | KIO::StoredTransferJob* storedStatusJob = static_cast<KIO::StoredTransferJob*>( statusJob ); | ||
141 | 143 | | |||
142 | QVariant parsedStatusVariant; | 144 | QJsonParseError err; | ||
145 | auto doc = QJsonDocument::fromJson( storedStatusJob->data(), &err ); | ||||
146 | | ||||
147 | if ( err.error != QJsonParseError::NoError ) | ||||
148 | debug() << "Error parsing JSON Data:" << err.errorString(); | ||||
143 | 149 | | |||
144 | QJson::Parser parser; | 150 | if( !doc.isObject() ) | ||
145 | bool ok; | | |||
146 | parsedStatusVariant = parser.parse( storedStatusJob->data(),&ok ); | | |||
147 | if ( !ok ) | | |||
148 | { | 151 | { | ||
149 | debug() << "Error parsing JSON Data"; | 152 | debug() << "Parsed Json data is not an object"; | ||
153 | return; | ||||
150 | } | 154 | } | ||
151 | QVariantMap parsedStatus = parsedStatusVariant.toMap(); | | |||
152 | 155 | | |||
153 | if( !parsedStatus.contains("name") ) | 156 | auto object = doc.object(); | ||
157 | | ||||
158 | if( !object.contains("name") ) | ||||
154 | { | 159 | { | ||
155 | debug() << "Expected a service name from Playdar, received none"; | 160 | debug() << "Expected a service name from Playdar, received none"; | ||
156 | emit playdarError( Playdar::Controller::ErrorState( MissingServiceName ) ); | 161 | emit playdarError( Playdar::Controller::ErrorState( MissingServiceName ) ); | ||
157 | return; | 162 | return; | ||
158 | } | 163 | } | ||
159 | if( parsedStatus.value("name") != QString( "playdar" ) ) | 164 | if( object.value("name").toString() != QStringLiteral( "playdar" ) ) | ||
160 | { | 165 | { | ||
161 | debug() << "Expected Playdar, got response from some other service"; | 166 | debug() << "Expected Playdar, got response from some other service"; | ||
162 | emit playdarError( Playdar::Controller::ErrorState( WrongServiceName ) ); | 167 | emit playdarError( Playdar::Controller::ErrorState( WrongServiceName ) ); | ||
163 | return; | 168 | return; | ||
164 | } | 169 | } | ||
165 | 170 | | |||
166 | debug() << "All good! Emitting playdarReady()"; | 171 | debug() << "All good! Emitting playdarReady()"; | ||
167 | emit playdarReady(); | 172 | emit playdarReady(); | ||
Show All 10 Lines | 181 | { | |||
178 | emit playdarError( Playdar::Controller::ErrorState( ExternalError ) ); | 183 | emit playdarError( Playdar::Controller::ErrorState( ExternalError ) ); | ||
179 | return; | 184 | return; | ||
180 | } | 185 | } | ||
181 | 186 | | |||
182 | debug() << "Processing received JSON data..."; | 187 | debug() << "Processing received JSON data..."; | ||
183 | KIO::StoredTransferJob* storedQueryJob = | 188 | KIO::StoredTransferJob* storedQueryJob = | ||
184 | static_cast<KIO::StoredTransferJob*>( queryJob ); | 189 | static_cast<KIO::StoredTransferJob*>( queryJob ); | ||
185 | 190 | | |||
186 | QVariant parsedQueryVariant; | 191 | QJsonParseError err; | ||
187 | QJson::Parser parser; | 192 | auto doc = QJsonDocument::fromJson( storedQueryJob->data(), &err ); | ||
188 | bool ok; | 193 | | ||
189 | parsedQueryVariant = parser.parse( storedQueryJob->data(),&ok ); | 194 | if ( err.error != QJsonParseError::NoError ) | ||
190 | if ( !ok ) | 195 | debug() << "Error parsing JSON Data:" << err.errorString(); | ||
196 | | ||||
197 | if( !doc.isObject() ) | ||||
191 | { | 198 | { | ||
192 | debug() << "Error parsing JSON Data"; | 199 | debug() << "Parsed Json data is not an object"; | ||
200 | return; | ||||
193 | } | 201 | } | ||
194 | 202 | | |||
195 | QVariantMap parsedQuery = parsedQueryVariant.toMap(); | 203 | auto object = doc.object(); | ||
196 | if( !parsedQuery.contains( "qid" ) ) | 204 | | ||
205 | if( !object.contains( "qid" ) ) | ||||
197 | { | 206 | { | ||
198 | debug() << "Expected qid in Playdar's response, but didn't get it"; | 207 | debug() << "Expected qid in Playdar's response, but didn't get it"; | ||
199 | emit playdarError( Playdar::Controller::ErrorState( MissingQid ) ); | 208 | emit playdarError( Playdar::Controller::ErrorState( MissingQid ) ); | ||
200 | return; | 209 | return; | ||
201 | } | 210 | } | ||
202 | 211 | | |||
203 | Query* query = new Query( parsedQuery.value( "qid" ).toString(), this, m_queriesShouldWaitForSolutions ); | 212 | Query* query = new Query( object.value( "qid" ).toString(), this, m_queriesShouldWaitForSolutions ); | ||
204 | 213 | | |||
205 | debug() << "All good! Emitting queryReady( Playdar::Query* )..."; | 214 | debug() << "All good! Emitting queryReady( Playdar::Query* )..."; | ||
206 | emit queryReady( query ); | 215 | emit queryReady( query ); | ||
207 | 216 | | |||
208 | connect( query, &Query::playdarError, this, &Controller::playdarError ); | 217 | connect( query, &Query::playdarError, this, &Controller::playdarError ); | ||
209 | } | 218 | } | ||
210 | } | 219 | } |