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

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 "synchttplookup.h"
0009 #include "logging.h"
0010 
0011 #include <KIO/TransferJob>
0012 
0013 namespace KCDDB
0014 {
0015   SyncHTTPLookup::SyncHTTPLookup()
0016     : HTTPLookup()
0017   {
0018   }
0019 
0020   SyncHTTPLookup::~SyncHTTPLookup()
0021   {
0022     // Empty.
0023   }
0024 
0025     Result
0026   SyncHTTPLookup::lookup
0027   (
0028     const QString         & hostName,
0029     uint                    port,
0030     const TrackOffsetList & trackOffsetList
0031   )
0032   {
0033     trackOffsetList_ = trackOffsetList;
0034 
0035     initURL( hostName, port );
0036 
0037     // Run a query.
0038     result_ = runQuery();
0039 
0040     if ( Success != result_ )
0041       return result_;
0042 
0043     qCDebug(LIBKCDDB) << matchList_.count() << " matches found.";
0044 
0045     if (matchList_.isEmpty())
0046       return NoRecordFound;
0047 
0048     // For each match, read the cd info from the server and save it to
0049     // cdInfoList.
0050     CDDBMatchList::ConstIterator matchIt = matchList_.constBegin();
0051 
0052     while ( matchIt != matchList_.constEnd() )
0053     {
0054       CDDBMatch match( *matchIt );
0055       result_ = matchToCDInfo( match );
0056       ++matchIt;
0057     }
0058 
0059     return result_;
0060   }
0061 
0062     Result
0063   SyncHTTPLookup::runQuery()
0064   {
0065     data_ = QByteArray();
0066     state_ = WaitingForQueryResponse;
0067 
0068     result_ = sendQuery();
0069 
0070     if ( Success != result_ )
0071       return result_;
0072 
0073     qCDebug(LIBKCDDB) << "runQuery() Result: " << resultToString(result_);
0074 
0075     return result_;
0076   }
0077 
0078     Result
0079   SyncHTTPLookup::matchToCDInfo( const CDDBMatch & match )
0080   {
0081     data_ = QByteArray();
0082     state_ = WaitingForReadResponse;
0083 
0084     result_ = sendRead( match );
0085 
0086     if ( Success != result_ )
0087       return result_;
0088 
0089     return result_;
0090   }
0091 
0092     Result
0093   SyncHTTPLookup::fetchURL()
0094   {
0095     qCDebug(LIBKCDDB) << "About to fetch: " << cgiURL_.url();
0096 
0097     KIO::TransferJob* job = KIO::get( cgiURL_, KIO::NoReload, KIO::HideProgressInfo );
0098 
0099     if ( nullptr == job )
0100       return ServerError;
0101 
0102     QObject::connect( job, &KIO::TransferJob::data, [&](KIO::Job *, const QByteArray &data){ data_ += data; } );
0103 
0104     if (!job->exec())
0105       return ServerError;
0106 
0107     jobFinished();
0108 
0109     return Success;
0110   }
0111 }
0112 
0113 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1