KCDDB

sites.cpp
1 /*
2  SPDX-FileCopyrightText: 2004 Richard Lärkäng <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "sites.h"
8 
9 #include <KIO/Job>
10 #include <QDebug>
11 #include <QRegExp>
12 #include <QTextStream>
13 #include <QUrl>
14 #include <QUrlQuery>
15 
16 namespace KCDDB
17 {
18  Sites::Sites()
19  {
20 
21  }
22 
24  Sites::siteList()
25  {
26  QUrl url;
27  url.setScheme( QLatin1String( "http" ) );
28  url.setHost( QLatin1String( "gnudb.gnudb.org" ) );
29  url.setPort( 80 );
30  url.setPath( QLatin1String( "/~cddb/cddb.cgi" ) );
31 
32  QString hello = QString::fromLatin1("%1 %2 %3 %4")
33  .arg(QLatin1String( "libkcddb-user" ), QLatin1String( "localHost" ), CDDB::clientName(), CDDB::clientVersion());
34 
36  query.addQueryItem( QLatin1String( "cmd" ), QLatin1String( "sites" ) );
37  query.addQueryItem( QLatin1String( "hello" ), hello );
38  query.addQueryItem( QLatin1String( "proto" ), QLatin1String( "5" ) );
39  url.setQuery( query );
40 
41  QList<Mirror> result;
42 
43  KIO::TransferJob* job = KIO::get( url, KIO::NoReload, KIO::HideProgressInfo );
44  QByteArray data;
45  QObject::connect( job, &KIO::TransferJob::data, [&data](KIO::Job *, const QByteArray &d){ data += d; } );
46  if( job->exec() )
47  {
48  result = readData( data );
49  }
50 
51  return result;
52  }
53 
55  Sites::readData(const QByteArray& data)
56  {
57  QList<Mirror> result;
58 
59  QTextStream ts(data);
60 
61  if (CDDB::statusCode(ts.readLine()) != 210)
62  return result;
63 
64  while (!ts.atEnd())
65  {
66  QString line = ts.readLine();
67  if (line == QLatin1String( "." ))
68  break;
69  result << parseLine(line);
70  }
71 
72  return result;
73  }
74 
75  Mirror
76  Sites::parseLine(const QString& line)
77  {
78  Mirror m;
79 
80  QRegExp rexp(QLatin1String( "([^ ]+) (cddbp|http) (\\d+) ([^ ]+) [N|S]\\d{3}.\\d{2} [E|W]\\d{3}.\\d{2} (.*)" ));
81 
82  if (rexp.indexIn(line) != -1)
83  {
84  m.address = rexp.cap(1);
85 
86  if (rexp.cap(2) == QLatin1String( "cddbp" ))
87  m.transport = Lookup::CDDBP;
88  else
89  m.transport = Lookup::HTTP;
90 
91  m.port = rexp.cap(3).toUInt();
92 
93  if (m.transport == Lookup::HTTP && rexp.cap(4) != QLatin1String( "/~cddb/cddb.cgi" ))
94  qWarning() << "Non default urls are not supported for http";
95 
96  m.description = rexp.cap(5);
97  }
98 
99  return m;
100  }
101 }
void setPort(int port)
std::optional< QSqlQuery > query(const QString &queryStatement)
bool exec()
void setPath(const QString &path, QUrl::ParsingMode mode)
void addQueryItem(const QString &key, const QString &value)
void setScheme(const QString &scheme)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
void data(KIO::Job *job, const QByteArray &data)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void setQuery(const QString &query, QUrl::ParsingMode mode)
QString fromLatin1(const char *str, int size)
void setHost(const QString &host, QUrl::ParsingMode mode)
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.