2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2011-07-06 15:48:52 +00:00
|
|
|
|
2010-11-11 16:49:17 +01:00
|
|
|
#include "networkaccessmanager.h"
|
|
|
|
|
|
2012-09-11 14:14:21 +02:00
|
|
|
#include <QCoreApplication>
|
2022-08-30 13:29:36 +02:00
|
|
|
#include <QLibraryInfo>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QLocale>
|
|
|
|
|
#include <QNetworkReply>
|
2010-11-11 16:49:17 +01:00
|
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-02-06 14:51:12 +01:00
|
|
|
\class Utils::NetworkAccessManager
|
|
|
|
|
\inmodule QtCreator
|
2010-11-11 16:49:17 +01:00
|
|
|
|
2020-02-06 14:51:12 +01:00
|
|
|
\brief The NetworkAccessManager class provides a network access manager for use
|
2013-06-05 14:29:24 +02:00
|
|
|
with \QC.
|
2010-11-11 16:49:17 +01:00
|
|
|
|
2013-09-06 13:23:11 +02:00
|
|
|
Common initialization, \QC User Agent.
|
2013-04-09 09:58:17 +02:00
|
|
|
|
|
|
|
|
Preferably, the instance returned by NetworkAccessManager::instance() should be used for the main
|
|
|
|
|
thread. The constructor is provided only for multithreaded use.
|
2010-11-11 16:49:17 +01:00
|
|
|
*/
|
|
|
|
|
|
2011-07-15 15:24:43 +02:00
|
|
|
namespace Utils {
|
2010-11-11 16:49:17 +01:00
|
|
|
|
2018-07-19 16:39:41 +02:00
|
|
|
static NetworkAccessManager *namInstance = nullptr;
|
2013-04-09 09:58:17 +02:00
|
|
|
|
|
|
|
|
void cleanupNetworkAccessManager()
|
|
|
|
|
{
|
|
|
|
|
delete namInstance;
|
2018-07-19 16:39:41 +02:00
|
|
|
namInstance = nullptr;
|
2013-04-09 09:58:17 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 14:51:12 +01:00
|
|
|
/*!
|
|
|
|
|
Returns a network access manager instance that should be used for the main
|
|
|
|
|
thread.
|
|
|
|
|
*/
|
2013-04-09 09:58:17 +02:00
|
|
|
NetworkAccessManager *NetworkAccessManager::instance()
|
|
|
|
|
{
|
|
|
|
|
if (!namInstance) {
|
|
|
|
|
namInstance = new NetworkAccessManager;
|
|
|
|
|
qAddPostRoutine(cleanupNetworkAccessManager);
|
|
|
|
|
}
|
|
|
|
|
return namInstance;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 14:51:12 +01:00
|
|
|
/*!
|
|
|
|
|
Constructs a network access manager instance with the parent \a parent.
|
|
|
|
|
*/
|
2010-11-11 16:49:17 +01:00
|
|
|
NetworkAccessManager::NetworkAccessManager(QObject *parent)
|
|
|
|
|
: QNetworkAccessManager(parent)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 14:51:12 +01:00
|
|
|
/*!
|
|
|
|
|
Creates \a request for the network access manager to perform the operation
|
|
|
|
|
\a op on \a outgoingData.
|
|
|
|
|
*/
|
2010-11-11 16:49:17 +01:00
|
|
|
QNetworkReply* NetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
|
|
|
|
|
{
|
2012-09-11 14:14:21 +02:00
|
|
|
QString agentStr = QString::fromLatin1("%1/%2 (QNetworkAccessManager %3; %4; %5; %6 bit)")
|
|
|
|
|
.arg(QCoreApplication::applicationName(),
|
|
|
|
|
QCoreApplication::applicationVersion(),
|
2012-01-04 17:34:08 +01:00
|
|
|
QLatin1String(qVersion()),
|
2016-09-21 13:49:47 +03:00
|
|
|
QSysInfo::prettyProductName(),
|
|
|
|
|
QLocale::system().name())
|
2010-11-11 16:49:17 +01:00
|
|
|
.arg(QSysInfo::WordSize);
|
|
|
|
|
QNetworkRequest req(request);
|
|
|
|
|
req.setRawHeader("User-Agent", agentStr.toLatin1());
|
|
|
|
|
return QNetworkAccessManager::createRequest(op, req, outgoingData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace utils
|