File indexing completed on 2024-04-14 04:49:14

0001 /*
0002     SPDX-FileCopyrightText: 2002 Rik Hemsley (  rikkus ) <rik@kde.org>
0003     SPDX-FileCopyrightText: 2002 Benjamin Meyer <ben-devel@meyerhome.net>
0004     SPDX-FileCopyrightText: 2002 Nadeem Hasan <nhasan@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "httplookup.h"
0010 
0011 #include <QUrlQuery>
0012 
0013 namespace KCDDB
0014 {
0015   HTTPLookup::HTTPLookup()
0016     : Lookup(),
0017       block_( true ), state_( Idle ), result_( Success )
0018   {
0019   }
0020 
0021   HTTPLookup::~HTTPLookup()
0022   {
0023   }
0024 
0025     Result
0026   HTTPLookup::sendQuery()
0027   {
0028       QString cmd = QString::fromLatin1( "cddb query %1 %2" )
0029       .arg( trackOffsetListToId(), trackOffsetListToString() ) ;
0030 
0031     makeURL( cmd );
0032     Result result = fetchURL();
0033 
0034     return result;
0035   }
0036 
0037     Result
0038   HTTPLookup::sendRead( const CDDBMatch & match )
0039   {
0040     category_  = match.first;
0041     discid_    = match.second;
0042 
0043     QString cmd = QString::fromLatin1( "cddb read %1 %2" )
0044         .arg( category_, discid_ );
0045 
0046     makeURL( cmd );
0047     Result result = fetchURL();
0048 
0049     return result;
0050   }
0051 
0052     void
0053   HTTPLookup::initURL( const QString & hostName, uint port )
0054   {
0055     cgiURL_.setScheme( QLatin1String( "http" ) );
0056     cgiURL_.setHost( hostName );
0057     cgiURL_.setPort( port );
0058     cgiURL_.setPath( QLatin1String( "/~cddb/cddb.cgi" ) );
0059 
0060     return;
0061   }
0062 
0063     void
0064   HTTPLookup::makeURL( const QString & cmd )
0065   {
0066     QString hello = QString::fromLatin1("%1 %2 %3 %4")
0067         .arg(user_, localHostName_, clientName(), clientVersion());
0068 
0069     // The whole query has to constructed each time as the
0070     // CDDB CGI script expects the parameters in strict order
0071     QUrlQuery query;
0072     query.addQueryItem( QLatin1String( "cmd" ), cmd );
0073     query.addQueryItem( QLatin1String( "hello" ), hello );
0074     query.addQueryItem( QLatin1String( "proto" ), QLatin1String( "6" ) );
0075     cgiURL_.setQuery( query );
0076   }
0077 
0078     void
0079   HTTPLookup::jobFinished()
0080   {
0081     QStringList lineList = QString::fromUtf8(data_).split( QLatin1String( "\n" ), Qt::SkipEmptyParts );
0082     QStringList::ConstIterator it = lineList.constBegin();
0083 
0084     switch ( state_ )
0085     {
0086       case WaitingForQueryResponse:
0087 
0088         if ( it != lineList.constEnd() )
0089         {
0090           QString line( *it );
0091 
0092           result_ = parseQuery( line );
0093 
0094           switch ( result_ )
0095           {
0096             case Success:
0097 
0098               if ( !block_ )
0099                 Q_EMIT queryReady();
0100               break;
0101 
0102             case MultipleRecordFound:
0103 
0104               ++it;
0105               while ( it != lineList.constEnd() )
0106               {
0107                 QString line( *it );
0108 
0109                 if ( QLatin1Char( '.' ) == line[ 0 ] )
0110                 {
0111                   result_ = Success;
0112 
0113                   if ( !block_ )
0114                     Q_EMIT queryReady();
0115                   break;
0116                 }
0117 
0118                 parseExtraMatch( line );
0119                 ++it;
0120               }
0121 
0122               break;
0123 
0124             case ServerError:
0125             case NoRecordFound:
0126               if ( !block_ )
0127                 Q_EMIT queryReady();
0128 
0129               return;
0130               break;
0131 
0132             default:
0133 
0134               break;
0135           }
0136 
0137         }
0138 
0139         break;
0140 
0141       case WaitingForReadResponse:
0142 
0143         {
0144           CDInfo info;
0145 
0146           if ( info.load( QString::fromUtf8(data_) ) )
0147           {
0148             info.set( QLatin1String( "category" ), category_ );
0149             info.set( QLatin1String( "discid" ), discid_ );
0150             info.set( QLatin1String( "source" ), QLatin1String( "freedb" ) );
0151             cdInfoList_.append( info );
0152           }
0153 
0154           if ( !block_ )
0155             Q_EMIT readReady();
0156         }
0157 
0158         return;
0159 
0160         break;
0161 
0162       default:
0163 
0164         break;
0165     }
0166 
0167     result_ = Success;
0168   }
0169 }
0170 
0171 #include "moc_httplookup.cpp"
0172 
0173 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1