KCDDB

asynchttplookup.cpp
1 /*
2  SPDX-FileCopyrightText: 2002 Rik Hemsley (rikkus) <[email protected]>
3  SPDX-FileCopyrightText: 2002 Benjamin Meyer <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "asynchttplookup.h"
9 #include "logging.h"
10 
11 #include <KIO/Job>
12 
13 namespace KCDDB
14 {
15  AsyncHTTPLookup::AsyncHTTPLookup()
16  : HTTPLookup()
17  {
18  block_ = false;
19  }
20 
21  AsyncHTTPLookup::~AsyncHTTPLookup()
22  {
23  // Empty.
24  }
25 
26  Result
27  AsyncHTTPLookup::lookup
28  (
29  const QString & hostName,
30  uint port,
31  const TrackOffsetList & trackOffsetList
32  )
33  {
34  trackOffsetList_ = trackOffsetList;
35 
36  connect( this, &HTTPLookup::queryReady, this, &AsyncHTTPLookup::slotQueryReady );
37  connect( this, &HTTPLookup::readReady, this, &AsyncHTTPLookup::requestCDInfoForMatch );
38 
39  initURL( hostName, port );
40 
41  // Run a query.
42  result_ = runQuery();
43 
44  return result_;
45  }
46 
47  Result
48  AsyncHTTPLookup::runQuery()
49  {
50  data_ = QByteArray();
51  state_ = WaitingForQueryResponse;
52 
53  result_ = sendQuery();
54 
55  return result_;
56  }
57 
58  void
59  AsyncHTTPLookup::slotQueryReady()
60  {
61  qCDebug(LIBKCDDB) << "Matches Found: " << matchList_.count();
62 
63  if ( Success != result_ )
64  {
65  Q_EMIT finished( result_ );
66  return;
67  }
68 
69  requestCDInfoForMatch();
70  }
71 
72  void
73  AsyncHTTPLookup::requestCDInfoForMatch()
74  {
75  if ( matchList_.isEmpty() )
76  {
77  result_ = cdInfoList_.isEmpty()? NoRecordFound : Success;
78  Q_EMIT finished( result_ );
79  return;
80  }
81 
82  CDDBMatch match = matchList_.takeFirst();
83 
84  data_ = QByteArray();
85  state_ = WaitingForReadResponse;
86 
87  result_ = sendRead( match );
88 
89  if ( Success != result_ )
90  Q_EMIT finished( result_ );
91  }
92 
93  void
94  AsyncHTTPLookup::slotData( KIO::Job *, const QByteArray &data )
95  {
96  if (data.size() > 0)
97  data_.append(data);
98  }
99 
100  void
101  AsyncHTTPLookup::slotResult( KJob *job )
102  {
103  if ( 0 != job->error() )
104  {
105  result_ = ServerError;
106  if ( !block_ )
107  Q_EMIT queryReady();
108  return;
109  }
110 
111  jobFinished();
112  }
113 
114  Result
115  AsyncHTTPLookup::fetchURL()
116  {
117  qCDebug(LIBKCDDB) << "About to fetch: " << cgiURL_.url();
118 
119  KIO::TransferJob* job = KIO::get( cgiURL_, KIO::NoReload, KIO::HideProgressInfo );
120 
121  if ( nullptr == job )
122  return ServerError;
123 
124  connect( job, &KIO::TransferJob::data,
125  this, &AsyncHTTPLookup::slotData );
126  connect( job, &KJob::result,
127  this, &AsyncHTTPLookup::slotResult );
128 
129  return Success;
130  }
131 
132 }
133 
134 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
void data(KIO::Job *job, const QByteArray &data)
void result(KJob *job)
int size() const const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:10:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.