File indexing completed on 2025-01-19 04:24:32

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Andrew Coder <andrew.coder@gmail.com>                             *                                                                                      *
0003  * This program is free software; you can redistribute it and/or modify it under        *
0004  * the terms of the GNU General Public License as published by the Free Software        *
0005  * Foundation; either version 2 of the License, or (at your option) any later           *
0006  * version.                                                                             *
0007  *                                                                                      *
0008  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0009  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0010  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0011  *                                                                                      *
0012  * You should have received a copy of the GNU General Public License along with         *
0013  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0014  ****************************************************************************************/
0015 
0016 #include "Query.h"
0017 
0018 #include "Controller.h"
0019 #include "core/meta/Meta.h"
0020 #include "../PlaydarMeta.h"
0021 #include "core/support/Debug.h"
0022 
0023 #include <QJsonArray>
0024 #include <QJsonDocument>
0025 #include <QJsonObject>
0026 #include <QJsonParseError>
0027 #include <QMap>
0028 #include <QString>
0029 #include <QUrl>
0030 #include <QVariant>
0031 #include <QVariantMap>
0032 
0033 #include <KIO/Job>
0034 
0035 
0036 namespace Playdar
0037 {
0038     Query::Query( const QString &qid,
0039                   Playdar::Controller* controller,
0040                   bool waitForSolution )
0041     : m_controller( controller )
0042     , m_waitForSolution( waitForSolution )
0043     , m_qid( qid )
0044     , m_artist( QString( "" ) )
0045     , m_album( QString( "" ) )
0046     , m_title( QString( "" ) )
0047     , m_solved( false )
0048     , m_receivedFirstResults( false )
0049     , m_trackList( )
0050     {
0051         DEBUG_BLOCK
0052         
0053         if( m_waitForSolution )
0054         {
0055             m_receivedFirstResults = true;
0056             m_controller->getResultsLongPoll( this );
0057         }
0058         else
0059             m_controller->getResults( this );
0060     }
0061     
0062     Query::~Query()
0063     {
0064         DEBUG_BLOCK
0065         
0066     }
0067     
0068     QString
0069     Query::qid() const
0070     {
0071         DEBUG_BLOCK
0072         
0073         return m_qid;
0074     }
0075     
0076     QString
0077     Query::artist() const
0078     {
0079         DEBUG_BLOCK
0080         
0081         return m_artist;
0082     }
0083     
0084     QString
0085     Query::album() const
0086     {
0087         DEBUG_BLOCK
0088         
0089         return m_album;
0090     }
0091     
0092     QString
0093     Query::title() const
0094     {
0095         DEBUG_BLOCK
0096         
0097         return m_title;
0098     }
0099     
0100     bool
0101     Query::isSolved() const
0102     {
0103         DEBUG_BLOCK
0104         
0105         return m_solved;
0106     }
0107 
0108     Meta::PlaydarTrackList
0109     Query::getTrackList() const
0110     {
0111         DEBUG_BLOCK
0112         
0113         return m_trackList;
0114     }
0115     
0116     void
0117     Query::receiveResults( KJob* resultsJob)
0118     {
0119         DEBUG_BLOCK
0120         
0121         if( resultsJob->error() != 0 ) {
0122             debug() << "Error getting results from Playdar";
0123             Q_EMIT playdarError( Playdar::Controller::ErrorState( 1 ) );
0124             return;
0125         }
0126         
0127         debug() << "Processing received JSON data...";
0128         KIO::StoredTransferJob* storedResultsJob = static_cast<KIO::StoredTransferJob*>( resultsJob );
0129                 
0130         QJsonParseError err;
0131         auto doc = QJsonDocument::fromJson( storedResultsJob->data(), &err );
0132 
0133         if ( err.error != QJsonParseError::NoError )
0134             debug() << "Error parsing JSON Data:" << err.errorString();
0135 
0136         if( !doc.isObject() )
0137         {
0138             debug() << "Parsed Json data is not an object";
0139             return;
0140         }
0141 
0142         auto object = doc.object();
0143 
0144         if( !object.contains( "results" ) )
0145         {
0146             debug() << "Expecting results in Playdar's response, received none";
0147             Q_EMIT playdarError( Playdar::Controller::ErrorState( 6 ) );
0148             return;
0149         }
0150         if( !object.contains( "qid" ) )
0151         {
0152             debug() << "Expected qid in Playdar's response, received none";
0153             Q_EMIT playdarError( Playdar::Controller::ErrorState( 4 ) );
0154             return;
0155         }
0156         if( object.value( "qid" ) != m_qid )
0157         {
0158             debug() << "A query received the wrong results from Playdar...";
0159             Q_EMIT playdarError( Playdar::Controller::ErrorState( 5 ) );
0160             return;
0161         }
0162         
0163         m_artist = object.value( "artist" ).toString();
0164         m_album = object.value( "album" ).toString();
0165         m_title = object.value( "track" ).toString();
0166         
0167         for( const auto &resultVariant : object.value( "results" ).toArray() )
0168         {
0169             auto result = resultVariant.toObject();
0170             Meta::PlaydarTrackPtr aTrack;
0171             QUrl resultUrl( m_controller->urlForSid( result.value( "sid" ).toString() ) );
0172             
0173             QString trackSid = result.value( "sid" ).toString();
0174             QString trackUrl = resultUrl.url();
0175             QString trackTitle = result.value( "track" ).toString();
0176             QString trackArtist = result.value( "artist" ).toString();
0177             QString trackAlbum = result.value( "album" ).toString();
0178             QString trackType = result.value( "mimetype" ).toString();
0179             QString trackSource = result.value( "source" ).toString();
0180             qint64 trackLengthInSeconds( result.value( "duration" ).toInt() );
0181             aTrack = new Meta::PlaydarTrack
0182             (
0183                 trackSid,
0184                 trackUrl,
0185                 trackTitle,
0186                 trackArtist,
0187                 trackAlbum,
0188                 trackType,
0189                 result.value( "score" ).toDouble() * 100,
0190                 ( trackLengthInSeconds * 1000 ), //convert s to ms
0191                 result.value( "bitrate" ).toInt(),
0192                 result.value( "size" ).toInt(),
0193                 trackSource
0194             );
0195             
0196             if( !m_solved && aTrack->score() >= 1.00 )
0197             {
0198                 m_solved = true;
0199                 m_trackList.prepend( aTrack );
0200                 Q_EMIT querySolved( aTrack );
0201                 
0202                 if( m_waitForSolution )
0203                 {
0204                     Q_EMIT queryDone( this, m_trackList );
0205                     return;
0206                 }
0207             }
0208             else
0209             {
0210                 m_trackList.append( aTrack );
0211             }
0212             Q_EMIT newTrackAdded( aTrack );
0213         }
0214         
0215         if( m_receivedFirstResults || m_solved )
0216         {
0217             m_receivedFirstResults = true;
0218             Q_EMIT queryDone( this, m_trackList );
0219         }
0220         else
0221         {
0222             m_receivedFirstResults = true;
0223             m_controller->getResultsLongPoll( this );
0224         }
0225     }
0226 }