KCDDB

httplookup.cpp
1 /*
2  SPDX-FileCopyrightText: 2002 Rik Hemsley ( rikkus ) <[email protected]>
3  SPDX-FileCopyrightText: 2002 Benjamin Meyer <[email protected]>
4  SPDX-FileCopyrightText: 2002 Nadeem Hasan <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "httplookup.h"
10 
11 #include <KIO/Job>
12 #include <QDebug>
13 #include <QUrlQuery>
14 
15 namespace KCDDB
16 {
17  HTTPLookup::HTTPLookup()
18  : Lookup(),
19  block_( true ), state_( Idle ), result_( Success )
20  {
21  }
22 
23  HTTPLookup::~HTTPLookup()
24  {
25  }
26 
27  Result
28  HTTPLookup::sendQuery()
29  {
30  QString cmd = QString::fromLatin1( "cddb query %1 %2" )
31  .arg( trackOffsetListToId(), trackOffsetListToString() ) ;
32 
33  makeURL( cmd );
34  Result result = fetchURL();
35 
36  return result;
37  }
38 
39  Result
40  HTTPLookup::sendRead( const CDDBMatch & match )
41  {
42  category_ = match.first;
43  discid_ = match.second;
44 
45  QString cmd = QString::fromLatin1( "cddb read %1 %2" )
46  .arg( category_, discid_ );
47 
48  makeURL( cmd );
49  Result result = fetchURL();
50 
51  return result;
52  }
53 
54  void
55  HTTPLookup::initURL( const QString & hostName, uint port )
56  {
57  cgiURL_.setScheme( QLatin1String( "http" ) );
58  cgiURL_.setHost( hostName );
59  cgiURL_.setPort( port );
60  cgiURL_.setPath( QLatin1String( "/~cddb/cddb.cgi" ) );
61 
62  return;
63  }
64 
65  void
66  HTTPLookup::makeURL( const QString & cmd )
67  {
68  QString hello = QString::fromLatin1("%1 %2 %3 %4")
69  .arg(user_, localHostName_, clientName(), clientVersion());
70 
71  // The whole query has to constructed each time as the
72  // CDDB CGI script expects the parameters in strict order
74  query.addQueryItem( QLatin1String( "cmd" ), cmd );
75  query.addQueryItem( QLatin1String( "hello" ), hello );
76  query.addQueryItem( QLatin1String( "proto" ), QLatin1String( "6" ) );
77  cgiURL_.setQuery( query );
78  }
79 
80  void
81  HTTPLookup::jobFinished()
82  {
83 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
85 #else
87 #endif
88  QStringList::ConstIterator it = lineList.constBegin();
89 
90  switch ( state_ )
91  {
92  case WaitingForQueryResponse:
93 
94  if ( it != lineList.constEnd() )
95  {
96  QString line( *it );
97 
98  result_ = parseQuery( line );
99 
100  switch ( result_ )
101  {
102  case Success:
103 
104  if ( !block_ )
105  Q_EMIT queryReady();
106  break;
107 
108  case MultipleRecordFound:
109 
110  ++it;
111  while ( it != lineList.constEnd() )
112  {
113  QString line( *it );
114 
115  if ( QLatin1Char( '.' ) == line[ 0 ] )
116  {
117  result_ = Success;
118 
119  if ( !block_ )
120  Q_EMIT queryReady();
121  break;
122  }
123 
124  parseExtraMatch( line );
125  ++it;
126  }
127 
128  break;
129 
130  case ServerError:
131  case NoRecordFound:
132  if ( !block_ )
133  Q_EMIT queryReady();
134 
135  return;
136  break;
137 
138  default:
139 
140  break;
141  }
142 
143  }
144 
145  break;
146 
147  case WaitingForReadResponse:
148 
149  {
150  CDInfo info;
151 
152  if ( info.load( QString::fromUtf8(data_) ) )
153  {
154  info.set( QLatin1String( "category" ), category_ );
155  info.set( QLatin1String( "discid" ), discid_ );
156  info.set( QLatin1String( "source" ), QLatin1String( "freedb" ) );
157  cdInfoList_.append( info );
158  }
159 
160  if ( !block_ )
161  Q_EMIT readReady();
162  }
163 
164  return;
165 
166  break;
167 
168  default:
169 
170  break;
171  }
172 
173  result_ = Success;
174  }
175 }
176 
177 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
std::optional< QSqlQuery > query(const QString &queryStatement)
void addQueryItem(const QString &key, const QString &value)
QString fromUtf8(const char *str, int size)
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
SkipEmptyParts
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
typedef ConstIterator
QString fromLatin1(const char *str, int size)
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:22:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.