KCDDB

synchttplookup.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 "synchttplookup.h"
9 #include "logging.h"
10 
11 #include <KIO/Job>
12 
13 namespace KCDDB
14 {
15  SyncHTTPLookup::SyncHTTPLookup()
16  : HTTPLookup()
17  {
18  }
19 
20  SyncHTTPLookup::~SyncHTTPLookup()
21  {
22  // Empty.
23  }
24 
25  Result
26  SyncHTTPLookup::lookup
27  (
28  const QString & hostName,
29  uint port,
30  const TrackOffsetList & trackOffsetList
31  )
32  {
33  trackOffsetList_ = trackOffsetList;
34 
35  initURL( hostName, port );
36 
37  // Run a query.
38  result_ = runQuery();
39 
40  if ( Success != result_ )
41  return result_;
42 
43  qCDebug(LIBKCDDB) << matchList_.count() << " matches found.";
44 
45  if (matchList_.isEmpty())
46  return NoRecordFound;
47 
48  // For each match, read the cd info from the server and save it to
49  // cdInfoList.
50  CDDBMatchList::ConstIterator matchIt = matchList_.constBegin();
51 
52  while ( matchIt != matchList_.constEnd() )
53  {
54  CDDBMatch match( *matchIt );
55  result_ = matchToCDInfo( match );
56  ++matchIt;
57  }
58 
59  return result_;
60  }
61 
62  Result
63  SyncHTTPLookup::runQuery()
64  {
65  data_ = QByteArray();
66  state_ = WaitingForQueryResponse;
67 
68  result_ = sendQuery();
69 
70  if ( Success != result_ )
71  return result_;
72 
73  qCDebug(LIBKCDDB) << "runQuery() Result: " << resultToString(result_);
74 
75  return result_;
76  }
77 
78  Result
79  SyncHTTPLookup::matchToCDInfo( const CDDBMatch & match )
80  {
81  data_ = QByteArray();
82  state_ = WaitingForReadResponse;
83 
84  result_ = sendRead( match );
85 
86  if ( Success != result_ )
87  return result_;
88 
89  return result_;
90  }
91 
92  Result
93  SyncHTTPLookup::fetchURL()
94  {
95  qCDebug(LIBKCDDB) << "About to fetch: " << cgiURL_.url();
96 
97  KIO::TransferJob* job = KIO::get( cgiURL_, KIO::NoReload, KIO::HideProgressInfo );
98 
99  if ( nullptr == job )
100  return ServerError;
101 
102  QObject::connect( job, &KIO::TransferJob::data, [&](KIO::Job *, const QByteArray &data){ data_ += data; } );
103 
104  if (!job->exec())
105  return ServerError;
106 
107  jobFinished();
108 
109  return Success;
110  }
111 }
112 
113 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
bool exec()
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)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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.