KCDDB

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

KDE's Doxygen guidelines are available online.