Commit 3369efaf authored by Urs Fleisch's avatar Urs Fleisch
Browse files

Replace QHttp by QNetworkAccessManager, QHttp is not available in Qt5.

parent 3f6a5566
......@@ -34,13 +34,13 @@
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
AmazonImporter::AmazonImporter(
QObject* parent,
QNetworkAccessManager* netMgr,
TrackDataModel* trackDataModel)
: ServerImporter(parent, trackDataModel)
: ServerImporter(netMgr, trackDataModel)
{
setObjectName("AmazonImporter");
}
......
......@@ -38,11 +38,11 @@ public:
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
AmazonImporter(QObject* parent,
TrackDataModel* trackDataModel);
AmazonImporter(QNetworkAccessManager* netMgr,
TrackDataModel *trackDataModel);
/**
* Destructor.
......
......@@ -37,12 +37,12 @@ static const char discogsServer[] = "www.discogs.com:80";
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
DiscogsImporter::DiscogsImporter(QObject* parent,
DiscogsImporter::DiscogsImporter(QNetworkAccessManager* netMgr,
TrackDataModel* trackDataModel) :
ServerImporter(parent, trackDataModel)
ServerImporter(netMgr, trackDataModel)
{
setObjectName("DiscogsImporter");
}
......@@ -595,7 +595,7 @@ void DiscogsImporter::sendFindQuery(
*/
sendRequest(discogsServer,
QString("/search?type=releases&q=") +
encodeUrlQuery(artist + " " + album) + "&btn=Search", true);
encodeUrlQuery(artist + " " + album) + "&btn=Search");
}
/**
......@@ -614,5 +614,5 @@ void DiscogsImporter::sendTrackListQuery(
* http://www.discogs.com/release/761529
*/
sendRequest(discogsServer, QString("/") + QUrl::toPercentEncoding(cat) + '/'
+ id, true);
+ id);
}
......@@ -38,11 +38,11 @@ public:
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
DiscogsImporter(QObject* parent,
TrackDataModel* trackDataModel);
DiscogsImporter(QNetworkAccessManager* netMgr,
TrackDataModel *trackDataModel);
/**
* Destructor.
......
......@@ -36,12 +36,12 @@ static const char gnudbServer[] = "www.gnudb.org:80";
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
FreedbImporter::FreedbImporter(QObject* parent,
TrackDataModel* trackDataModel) :
ServerImporter(parent, trackDataModel)
FreedbImporter::FreedbImporter(QNetworkAccessManager* netMgr,
TrackDataModel *trackDataModel) :
ServerImporter(netMgr, trackDataModel)
{
setObjectName("FreedbImporter");
}
......
......@@ -38,11 +38,11 @@ public:
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
FreedbImporter(QObject* parent,
TrackDataModel* trackDataModel);
FreedbImporter(QNetworkAccessManager* netMgr,
TrackDataModel *trackDataModel);
/**
* Destructor.
......
......@@ -25,7 +25,9 @@
*/
#include "httpclient.h"
#include <QHttp>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkProxy>
#include <QByteArray>
#include "configstore.h"
#include "qtcompatmac.h"
......@@ -34,20 +36,12 @@
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
*/
HttpClient::HttpClient(QObject* parent) : QObject(parent), m_rcvBodyLen(0)
HttpClient::HttpClient(QNetworkAccessManager* netMgr) :
QObject(netMgr), m_netMgr(netMgr), m_rcvBodyLen(0)
{
setObjectName("HttpClient");
m_http = new QHttp();
connect(m_http, SIGNAL(stateChanged(int)),
this, SLOT(slotStateChanged(int)));
connect(m_http, SIGNAL(dataReadProgress(int, int)),
this, SLOT(slotDataReadProgress(int, int)));
connect(m_http, SIGNAL(done(bool)),
this, SLOT(slotDone(bool)));
connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)),
this, SLOT(slotResponseHeaderReceived(const QHttpResponseHeader&)));
}
/**
......@@ -55,38 +49,10 @@ HttpClient::HttpClient(QObject* parent) : QObject(parent), m_rcvBodyLen(0)
*/
HttpClient::~HttpClient()
{
m_http->close();
m_http->disconnect();
delete m_http;
}
/**
* Called when the connection state changes.
*
* @param state HTTP connection state
*/
void HttpClient::slotStateChanged(int state)
{
switch (state) {
case QHttp::HostLookup:
emitProgress(i18n("Ready."), CS_RequestConnection, CS_EstimatedBytes);
break;
case QHttp::Connecting:
emitProgress(i18n("Connecting..."), CS_Connecting, CS_EstimatedBytes);
break;
case QHttp::Sending:
emitProgress(i18n("Host found..."), CS_HostFound, CS_EstimatedBytes);
break;
case QHttp::Reading:
emitProgress(i18n("Request sent..."), CS_RequestSent, CS_EstimatedBytes);
break;
case QHttp::Connected:
emitProgress(i18n("Ready."), -1, -1);
break;
case QHttp::Unconnected:
case QHttp::Closing:
default:
;
if (m_reply) {
m_reply->close();
m_reply->disconnect();
m_reply->deleteLater();
}
}
......@@ -94,101 +60,90 @@ void HttpClient::slotStateChanged(int state)
#define DATA_RECEIVED_FOR_PO I18N_NOOP("Data received: %1")
/**
* Called to report connection progress.
*
* @param done bytes received
* @param total total bytes, 0 if unknown
* Called when the request is finished.
*/
void HttpClient::slotDataReadProgress(int done, int total)
void HttpClient::networkReplyFinished()
{
emitProgress(i18n("Data received: %1").arg(done), done, total);
if (QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender())) {
QByteArray data(reply->readAll());
m_rcvBodyType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
m_rcvBodyLen = reply->header(QNetworkRequest::ContentLengthHeader).toUInt();
emit bytesReceived(data);
QString msg(i18n("Ready."));
if (reply->error() != QNetworkReply::NoError) {
msg = i18n("Error");
msg += ": ";
msg += reply->errorString();
}
emitProgress(msg, data.size(), data.size());
reply->deleteLater();
}
}
/**
* Called when the request is finished.
* Called to report connection progress.
*
* @param error true if error occurred
* @param received bytes received
* @param total total bytes
*/
void HttpClient::slotDone(bool error)
void HttpClient::networkReplyProgress(qint64 received, qint64 total)
{
if (error) {
QHttp::Error err = m_http->error();
if (err != QHttp::UnexpectedClose) {
QString msg(i18n("Socket error: "));
switch (err) {
case QHttp::ConnectionRefused:
msg += i18n("Connection refused");
break;
case QHttp::HostNotFound:
msg += i18n("Host not found");
break;
default:
msg += m_http->errorString();
}
emitProgress(msg, -1, -1);
}
}
emit bytesReceived(m_http->readAll());
if (!error) {
emitProgress(i18n("Ready."), CS_EstimatedBytes, CS_EstimatedBytes);
}
emitProgress(i18n("Data received: %1").arg(received), received, total);
}
/**
* Called when the response header is available.
* Called when an error occurred.
*
* @param resp HTTP response header
* @param code error code
*/
void HttpClient::slotResponseHeaderReceived(const QHttpResponseHeader& resp)
void HttpClient::networkReplyError(QNetworkReply::NetworkError)
{
m_rcvBodyType = resp.contentType();
m_rcvBodyLen = resp.contentLength();
if (QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender())) {
emitProgress(reply->errorString(), -1, -1);
}
}
/**
* Send a HTTP GET request.
*
* @param server host name
* @param path path of the URL
* @param setUserAgent true to set user agent to Mozilla
*/
void HttpClient::sendRequest(const QString& server, const QString& path,
bool setUserAgent)
void HttpClient::sendRequest(const QString& server, const QString& path)
{
m_rcvBodyLen = 0;
m_rcvBodyType = "";
QString dest;
int destPort;
splitNamePort(server, dest, destPort);
m_http->setHost(dest, destPort);
QString proxy, username, password;
int proxyPort = 0;
QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
if (ConfigStore::s_miscCfg.m_useProxy) {
splitNamePort(ConfigStore::s_miscCfg.m_proxy, proxy, proxyPort);
proxyType = QNetworkProxy::HttpProxy;
}
if (ConfigStore::s_miscCfg.m_useProxyAuthentication) {
username = ConfigStore::s_miscCfg.m_proxyUserName;
password = ConfigStore::s_miscCfg.m_proxyPassword;
}
m_http->setProxy(proxy, proxyPort, username, password);
m_http->setHost(dest, destPort);
if (!setUserAgent) {
m_http->get(path);
} else {
// Set User-Agent to Mozilla to avoid that request is blocked.
QHttpRequestHeader header("GET", path);
if (destPort != 80)
header.setValue("Host", dest + ':' + QString::number(destPort));
else
header.setValue("Host", dest);
header.setValue("User-Agent","Mozilla/5.0");
header.setValue("Accept","text/html,application/xhtml+xml,application/xml"
";q=0.9,*/*;q=0.8");
header.setValue("Accept-Language","en-us,en;q=0.5");
header.setValue("Accept-Charset","utf-8");
header.setValue("Connection","keep-alive");
m_http->request(header);
m_netMgr->setProxy(QNetworkProxy(proxyType, proxy, proxyPort,
username, password));
QString host(server);
if (host.endsWith(":80")) {
host.chop(3);
}
QUrl url;
url.setEncodedUrl(("http://" + host + path).toAscii());
QNetworkRequest request(url);
QNetworkReply* reply = m_netMgr->get(request);
m_reply = reply;
connect(reply, SIGNAL(finished()),
this, SLOT(networkReplyFinished()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(networkReplyProgress(qint64,qint64)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
emitProgress(i18n("Request sent..."), 0, 0);
}
/**
......@@ -196,7 +151,9 @@ void HttpClient::sendRequest(const QString& server, const QString& path,
*/
void HttpClient::abort()
{
m_http->abort();
if (m_reply) {
m_reply->abort();
}
}
/**
......
......@@ -29,34 +29,25 @@
#include <QObject>
#include <QString>
#include <QNetworkReply>
#include <QPointer>
class QByteArray;
class QHttp;
class QHttpResponseHeader;
class QNetworkAccessManager;
/**
* Client to connect to HTTP server.
*/
class HttpClient : public QObject
{
class HttpClient : public QObject {
Q_OBJECT
public:
/** Connection progress steps. */
enum ConnectionSteps {
CS_RequestConnection = 0, /**< Send Request */
CS_Connecting = 1, /**< Connecting */
CS_HostFound = 2, /**< Host Found */
CS_RequestSent = 3, /**< Request Sent */
CS_EstimatedBytes = 75000 /**< Estimated total number of bytes */
};
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
*/
explicit HttpClient(QObject* parent = 0);
explicit HttpClient(QNetworkAccessManager* netMgr);
/**
* Destructor.
......@@ -68,10 +59,8 @@ public:
*
* @param server host name
* @param path path of the URL
* @param setUserAgent true to set user agent to Mozilla
*/
void sendRequest(const QString& server, const QString& path,
bool setUserAgent = false);
void sendRequest(const QString& server, const QString& path);
/**
* Abort request.
......@@ -115,33 +104,24 @@ signals:
private slots:
/**
* Called when the connection state changes.
*
* @param state HTTP connection state
* Called when the request is finished.
*/
void slotStateChanged(int state);
void networkReplyFinished();
/**
* Called to report connection progress.
*
* @param done bytes received
* @param total total bytes, 0 if unknown
*/
void slotDataReadProgress(int done, int total);
/**
* Called when the request is finished.
*
* @param error true if error occurred
* @param received bytes received
* @param total total bytes
*/
void slotDone(bool error);
void networkReplyProgress(qint64 received, qint64 total);
/**
* Called when the response header is available.
* Called when an error occurred.
*
* @param resp HTTP response header
* @param code error code
*/
void slotResponseHeaderReceived(const QHttpResponseHeader& resp);
void networkReplyError(QNetworkReply::NetworkError);
private:
/**
......@@ -175,8 +155,10 @@ private:
*/
static QString getProxyOrDest(const QString& dst);
/** client socket */
QHttp* m_http;
/** network access manager */
QNetworkAccessManager* m_netMgr;
/** network reply if available, else 0 */
QPointer<QNetworkReply> m_reply;
/** content length of entitiy-body, 0 if not available */
unsigned long m_rcvBodyLen;
/** content type */
......
......@@ -34,10 +34,10 @@
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
*/
ImportClient::ImportClient(QObject* parent) :
HttpClient(parent), m_requestType(RT_None)
ImportClient::ImportClient(QNetworkAccessManager* netMgr) :
HttpClient(netMgr), m_requestType(RT_None)
{
setObjectName("ImportClient");
connect(this, SIGNAL(bytesReceived(const QByteArray&)),
......
......@@ -43,9 +43,9 @@ public:
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
*/
explicit ImportClient(QObject* parent = 0);
explicit ImportClient(QNetworkAccessManager* netMgr);
/**
* Destructor.
......
......@@ -175,11 +175,14 @@ void parseMusicBrainzMetadata(const QByteArray& bytes,
/**
* Constructor.
*
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values,
* is passed with filenames set
*/
MusicBrainzClient::MusicBrainzClient(TrackDataModel* trackDataModel) :
m_httpClient(new HttpClient(this)),
MusicBrainzClient::MusicBrainzClient(QNetworkAccessManager* netMgr,
TrackDataModel *trackDataModel) :
QObject(netMgr),
m_httpClient(new HttpClient(netMgr)),
m_fingerprintCalculator(new FingerprintCalculator),
m_trackDataModel(trackDataModel),
m_state(Idle), m_currentIndex(-1)
......
......@@ -36,6 +36,7 @@
#include "trackdata.h"
class QByteArray;
class QNetworkAccessManager;
class ImportTrackData;
class ImportTrackDataVector;
class TrackDataModel;
......@@ -53,10 +54,12 @@ public:
/**
* Constructor.
*
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values,
* is passed with filenames set
*/
explicit MusicBrainzClient(TrackDataModel* trackDataModel);
MusicBrainzClient(QNetworkAccessManager* netMgr,
TrackDataModel* trackDataModel);
/**
* Destructor.
......@@ -75,6 +78,11 @@ public:
*/
void addFiles();
/**
* Reset the client state.
*/
void resetState();
signals:
/**
* Emitted when status of a file changed.
......@@ -107,7 +115,6 @@ private:
bool verifyIdIndex();
bool verifyTrackIndex();
void resetState();
void processNextStep();
void processNextTrack();
......
......@@ -35,12 +35,12 @@
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
MusicBrainzReleaseImporter::MusicBrainzReleaseImporter(
QObject* parent, TrackDataModel* trackDataModel) :
ServerImporter(parent, trackDataModel)
QNetworkAccessManager* netMgr, TrackDataModel *trackDataModel) :
ServerImporter(netMgr, trackDataModel)
{
setObjectName("MusicBrainzReleaseImporter");
}
......
......@@ -38,10 +38,10 @@ public:
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
MusicBrainzReleaseImporter(QObject* parent,
MusicBrainzReleaseImporter(QNetworkAccessManager* netMgr,
TrackDataModel* trackDataModel);
/**
......
......@@ -34,12 +34,12 @@
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
ServerImporter::ServerImporter(QObject* parent,
ServerImporter::ServerImporter(QNetworkAccessManager* netMgr,
TrackDataModel* trackDataModel)
: ImportClient(parent),
: ImportClient(netMgr),
m_albumListModel(new QStandardItemModel(this)),
m_trackDataModel(trackDataModel),
m_additionalTagsEnabled(false), m_coverArtEnabled(false)
......
......@@ -47,11 +47,11 @@ public:
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager
* @param trackDataModel track data to be filled with imported values
*/
ServerImporter(QObject* parent,
TrackDataModel* trackDataModel);
ServerImporter(QNetworkAccessManager* netMgr,
TrackDataModel *trackDataModel);
/**
* Destructor.
......
......@@ -34,12 +34,12 @@ static const char trackTypeServer[] = "tracktype.org:80";
/**
* Constructor.
*
* @param parent parent object
* @param netMgr network access manager