Doc: Fix network/torrent example to work on Qt 5

This change fixes two issues in the torrent example:

    - Correctly use a query constructed from the announce url to fetch
      the list of peers (using QUrlQuery).
    - Reimplement QAbstractSocket's connectToHost() and
      disconnectFromHost() which are virtual in Qt 5, instead of
      of using the protected connectToHostImplementation() /
      disconnectFromHostImplementation() slots.

Also removes a warning about deprecation of QHttp class as it's no
longer used in this example.

Task-number: QTBUG-30329
Change-Id: I9230cd2204bfc1a66f2ea3e98940b09681df250e
Reviewed-by: Peter Hartmann <phartmann@blackberry.com>
This commit is contained in:
Topi Reinio 2013-04-03 11:21:18 +02:00 committed by The Qt Project
parent 05b0565e97
commit 87f890295b
4 changed files with 31 additions and 36 deletions

View File

@ -45,7 +45,6 @@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qWarning("The usage of QHttp is not recommended anymore, please use QNetworkAccessManager.");
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
Q_INIT_RESOURCE(icons);

View File

@ -386,15 +386,15 @@ bool PeerWireClient::canTransferMore() const
|| !outgoingBuffer.isEmpty() || !pendingBlocks.isEmpty();
}
void PeerWireClient::connectToHostImplementation(const QString &hostName,
quint16 port, OpenMode openMode)
void PeerWireClient::connectToHost(const QHostAddress &address,
quint16 port, OpenMode openMode)
{
setOpenMode(openMode);
socket.connectToHost(hostName, port, openMode);
socket.connectToHost(address, port, openMode);
}
void PeerWireClient::diconnectFromHostImplementation()
void PeerWireClient::diconnectFromHost()
{
socket.disconnectFromHost();
}

View File

@ -118,6 +118,10 @@ public:
void setReadBufferSize(qint64 size);
void connectToHost(const QHostAddress &address,
quint16 port, OpenMode openMode = ReadWrite);
void diconnectFromHost();
signals:
void infoHashReceived(const QByteArray &infoHash);
void readyToTransfer();
@ -133,11 +137,6 @@ signals:
void bytesReceived(qint64 size);
protected slots:
void connectToHostImplementation(const QString &hostName,
quint16 port, OpenMode openMode = ReadWrite);
void diconnectFromHostImplementation();
protected:
void timerEvent(QTimerEvent *event);

View File

@ -97,14 +97,10 @@ void TrackerClient::timerEvent(QTimerEvent *event)
void TrackerClient::fetchPeerList()
{
// Prepare connection details
QString fullUrl = metaInfo.announceUrl();
QUrl url(fullUrl);
QString passkey = "?";
if (fullUrl.contains("?passkey")) {
passkey = metaInfo.announceUrl().mid(fullUrl.indexOf("?passkey"), -1);
passkey += '&';
}
QUrl url(metaInfo.announceUrl());
// Base the query on announce url to include a passkey (if any)
QUrlQuery query(url);
// Percent encode the hash
QByteArray infoHash = torrentDownloader->infoHash();
@ -115,43 +111,44 @@ void TrackerClient::fetchPeerList()
}
bool seeding = (torrentDownloader->state() == TorrentClient::Seeding);
QByteArray query;
query += url.path().toLatin1();
query += passkey;
query += "info_hash=" + encodedSum;
query += "&peer_id=" + ConnectionManager::instance()->clientId();
query += "&port=" + QByteArray::number(TorrentServer::instance()->serverPort());
query += "&compact=1";
query += "&uploaded=" + QByteArray::number(torrentDownloader->uploadedBytes());
query.addQueryItem("info_hash", encodedSum);
query.addQueryItem("peer_id", ConnectionManager::instance()->clientId());
query.addQueryItem("port", QByteArray::number(TorrentServer::instance()->serverPort()));
query.addQueryItem("compact", "1");
query.addQueryItem("uploaded", QByteArray::number(torrentDownloader->uploadedBytes()));
if (!firstSeeding) {
query += "&downloaded=0";
query += "&left=0";
query.addQueryItem("downloaded", "0");
query.addQueryItem("left", "0");
} else {
query += "&downloaded=" + QByteArray::number(
torrentDownloader->downloadedBytes());
query.addQueryItem("downloaded",
QByteArray::number(torrentDownloader->downloadedBytes()));
int left = qMax<int>(0, metaInfo.totalSize() - torrentDownloader->downloadedBytes());
query += "&left=" + QByteArray::number(seeding ? 0 : left);
query.addQueryItem("left", QByteArray::number(seeding ? 0 : left));
}
if (seeding && firstSeeding) {
query += "&event=completed";
query.addQueryItem("event", "completed");
firstSeeding = false;
} else if (firstTrackerRequest) {
firstTrackerRequest = false;
query += "&event=started";
query.addQueryItem("event", "started");
} else if(lastTrackerRequest) {
query += "&event=stopped";
query.addQueryItem("event", "stopped");
}
if (!trackerId.isEmpty())
query += "&trackerid=" + trackerId;
query.addQueryItem("trackerid", trackerId);
url.setQuery(query);
QNetworkRequest req(url);
if (!url.userName().isEmpty()) {
uname = url.userName();
pwd = url.password();
connect(&http, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
connect(&http, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
this, SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
}
http.get(req);
}