KCDDB

sites.cpp
1/*
2 SPDX-FileCopyrightText: 2004 Richard Lärkäng <nouseforaname@home.se>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "sites.h"
8
9#include <KIO/TransferJob>
10#include <QDebug>
11#include <QRegularExpression>
12#include <QTextStream>
13#include <QUrl>
14#include <QUrlQuery>
15
16namespace 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 const QRegularExpression rexp(QLatin1String( "([^ ]+) (cddbp|http) (\\d+) ([^ ]+) [N|S]\\d{3}.\\d{2} [E|W]\\d{3}.\\d{2} (.*)" ));
81
82 if (const auto match = rexp.match(line); match.hasMatch())
83 {
84 m.address = match.captured(1);
85
86 if (match.capturedView(2) == QLatin1String( "cddbp" ))
87 m.transport = Lookup::CDDBP;
88 else
89 m.transport = Lookup::HTTP;
90
91 m.port = match.capturedView(3).toUInt();
92
93 if (m.transport == Lookup::HTTP && match.capturedView(4) != QLatin1String( "/~cddb/cddb.cgi" ))
94 qWarning() << "Non default urls are not supported for http";
95
96 m.description = match.captured(5);
97 }
98
99 return m;
100 }
101}
void data(KIO::Job *job, const QByteArray &data)
bool exec()
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
HideProgressInfo
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
void setHost(const QString &host, ParsingMode mode)
void setPath(const QString &path, ParsingMode mode)
void setPort(int port)
void setQuery(const QString &query, ParsingMode mode)
void setScheme(const QString &scheme)
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.