File indexing completed on 2024-04-21 04:54:17

0001 /*
0002     SPDX-FileCopyrightText: 2002 Rik Hemsley (rikkus) <rik@kde.org>
0003     SPDX-FileCopyrightText: 2002 Benjamin Meyer <ben-devel@meyerhome.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "asynchttplookup.h"
0009 #include "logging.h"
0010 
0011 #include <KIO/TransferJob>
0012 
0013 namespace KCDDB
0014 {
0015   AsyncHTTPLookup::AsyncHTTPLookup()
0016     : HTTPLookup()
0017   {
0018     block_ = false;
0019   }
0020 
0021   AsyncHTTPLookup::~AsyncHTTPLookup()
0022   {
0023     // Empty.
0024   }
0025 
0026     Result
0027   AsyncHTTPLookup::lookup
0028   (
0029     const QString         & hostName,
0030     uint                    port,
0031     const TrackOffsetList & trackOffsetList
0032   )
0033   {
0034     trackOffsetList_ = trackOffsetList;
0035 
0036     connect( this, &HTTPLookup::queryReady, this, &AsyncHTTPLookup::slotQueryReady );
0037     connect( this, &HTTPLookup::readReady, this, &AsyncHTTPLookup::requestCDInfoForMatch );
0038 
0039     initURL( hostName, port );
0040 
0041     // Run a query.
0042     result_ = runQuery();
0043 
0044     return result_;
0045   }
0046 
0047     Result
0048   AsyncHTTPLookup::runQuery()
0049   {
0050     data_ = QByteArray();
0051     state_ = WaitingForQueryResponse;
0052 
0053     result_ = sendQuery();
0054 
0055     return result_;
0056   }
0057 
0058     void
0059   AsyncHTTPLookup::slotQueryReady()
0060   {
0061     qCDebug(LIBKCDDB) << "Matches Found: " <<  matchList_.count();
0062 
0063     if ( Success != result_ )
0064     {
0065       Q_EMIT finished( result_ );
0066       return;
0067     }
0068 
0069     requestCDInfoForMatch();
0070   }
0071 
0072     void
0073   AsyncHTTPLookup::requestCDInfoForMatch()
0074   {
0075     if ( matchList_.isEmpty() )
0076     {
0077       result_ = cdInfoList_.isEmpty()? NoRecordFound : Success;
0078       Q_EMIT finished( result_ );
0079       return;
0080     }
0081 
0082     CDDBMatch match = matchList_.takeFirst();
0083 
0084     data_ = QByteArray();
0085     state_ = WaitingForReadResponse;
0086 
0087     result_ = sendRead( match );
0088 
0089     if ( Success != result_ )
0090       Q_EMIT finished( result_ );
0091   }
0092 
0093     void
0094   AsyncHTTPLookup::slotData( KIO::Job *, const QByteArray &data )
0095   {
0096     if (data.size() > 0)
0097       data_.append(data);
0098   }
0099 
0100     void
0101   AsyncHTTPLookup::slotResult( KJob *job )
0102   {
0103     if ( 0 != job->error() )
0104     {
0105       result_ = ServerError;
0106       if ( !block_ )
0107         Q_EMIT queryReady();
0108       return;
0109     }
0110 
0111     jobFinished();
0112   }
0113 
0114     Result
0115   AsyncHTTPLookup::fetchURL()
0116   {
0117     qCDebug(LIBKCDDB) << "About to fetch: " << cgiURL_.url();
0118 
0119     KIO::TransferJob* job = KIO::get( cgiURL_, KIO::NoReload, KIO::HideProgressInfo );
0120 
0121     if ( nullptr == job )
0122       return ServerError;
0123 
0124     connect( job, &KIO::TransferJob::data,
0125           this, &AsyncHTTPLookup::slotData );
0126     connect( job, &KJob::result,
0127           this, &AsyncHTTPLookup::slotResult );
0128 
0129     return Success;
0130   }
0131 
0132 }
0133 
0134 #include "moc_asynchttplookup.cpp"
0135 
0136 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1