Commit 9d7e786b authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

Provide instance()-method for Utils::NetworkAccessManager.



Preemptively fix exit warning "QEventLoop cannot be used
without QApplication" (Qt 5/dev) emitted by destruction of
the of the generic highlighter manager singleton which had a
member of type Utils::NetworkAccessManager.

Use a single instance of Utils::NetworkAccessManager for the
main thread and clean up properly using a qAddPostRoutine().

Change-Id: Ida57b9028a79eb4927818ce49088ea567f3bdfd6
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
Reviewed-by: default avatarPeter Hartmann <phartmann@blackberry.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 2ead48fb
......@@ -43,10 +43,30 @@
\brief Network Access Manager for use with Qt Creator.
Common initialization, Qt Creator User Agent
Preferably, the instance returned by NetworkAccessManager::instance() should be used for the main
thread. The constructor is provided only for multithreaded use.
*/
namespace Utils {
static NetworkAccessManager *namInstance = 0;
void cleanupNetworkAccessManager()
{
delete namInstance;
namInstance = 0;
}
NetworkAccessManager *NetworkAccessManager::instance()
{
if (!namInstance) {
namInstance = new NetworkAccessManager;
qAddPostRoutine(cleanupNetworkAccessManager);
}
return namInstance;
}
static const QString getOsString()
{
QString osString;
......
......@@ -44,6 +44,8 @@ class QTCREATOR_UTILS_EXPORT NetworkAccessManager : public QNetworkAccessManager
public:
NetworkAccessManager(QObject *parent = 0);
static NetworkAccessManager *instance();
public slots:
void getUrl(const QUrl &url);
......
......@@ -43,8 +43,7 @@ enum { debug = 0 };
namespace CodePaster {
CodePasterProtocol::CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw) :
NetworkProtocol(nw),
CodePasterProtocol::CodePasterProtocol() :
m_page(new CodePaster::CodePasterSettingsPage),
m_pasteReply(0),
m_fetchReply(0),
......
......@@ -44,7 +44,7 @@ class CodePasterProtocol : public NetworkProtocol
{
Q_OBJECT
public:
explicit CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw);
explicit CodePasterProtocol();
~CodePasterProtocol();
QString name() const;
......
......@@ -130,11 +130,10 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMe
addAutoReleasedObject(settingsPage);
// Create the protocols and append them to the Settings
const QSharedPointer<NetworkAccessManagerProxy> networkAccessMgrProxy(new NetworkAccessManagerProxy);
Protocol *protos[] = { new PasteBinDotComProtocol(networkAccessMgrProxy),
new PasteBinDotCaProtocol(networkAccessMgrProxy),
new KdePasteProtocol(networkAccessMgrProxy),
new CodePasterProtocol(networkAccessMgrProxy),
Protocol *protos[] = { new PasteBinDotComProtocol,
new PasteBinDotCaProtocol,
new KdePasteProtocol,
new CodePasterProtocol,
new FileShareProtocol
};
const int count = sizeof(protos) / sizeof(Protocol *);
......@@ -148,7 +147,7 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMe
m_protocols.append(protos[i]);
}
m_urlOpen = new UrlOpenProtocol(networkAccessMgrProxy);
m_urlOpen = new UrlOpenProtocol;
connect(m_urlOpen, SIGNAL(fetchDone(QString,QString,bool)),
this, SLOT(finishFetch(QString,QString,bool)));
......
......@@ -49,13 +49,12 @@ class PasteReceiver : public QObject
public:
PasteReceiver(const QString &protocol, const QString &filePath) : m_filePath(filePath)
{
const QSharedPointer<NetworkAccessManagerProxy> accessMgr(new NetworkAccessManagerProxy);
if (protocol == KdePasteProtocol::protocolName().toLower())
m_protocol.reset(new KdePasteProtocol(accessMgr));
m_protocol.reset(new KdePasteProtocol);
else if (protocol == PasteBinDotCaProtocol::protocolName().toLower())
m_protocol.reset(new PasteBinDotCaProtocol(accessMgr));
m_protocol.reset(new PasteBinDotCaProtocol);
else if (protocol == PasteBinDotComProtocol::protocolName().toLower())
m_protocol.reset(new PasteBinDotComProtocol(accessMgr));
m_protocol.reset(new PasteBinDotComProtocol);
else
qFatal("Internal error: Invalid protocol.");
}
......
......@@ -44,8 +44,7 @@ static const char hostUrlC[]= "http://paste.kde.org/";
static const char showPhpScriptpC[] = "show.php";
namespace CodePaster {
KdePasteProtocol::KdePasteProtocol(const NetworkAccessManagerProxyPtr &nw) :
NetworkProtocol(nw),
KdePasteProtocol::KdePasteProtocol() :
m_fetchReply(0),
m_pasteReply(0),
m_listReply(0),
......
......@@ -38,7 +38,7 @@ class KdePasteProtocol : public NetworkProtocol
{
Q_OBJECT
public:
explicit KdePasteProtocol(const NetworkAccessManagerProxyPtr &nw);
KdePasteProtocol();
static QString protocolName();
QString name() const { return protocolName(); }
......
......@@ -39,8 +39,7 @@
static const char urlC[] = "http://pastebin.ca/";
namespace CodePaster {
PasteBinDotCaProtocol::PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr &nw) :
NetworkProtocol(nw),
PasteBinDotCaProtocol::PasteBinDotCaProtocol() :
m_fetchReply(0),
m_listReply(0),
m_pasteReply(0),
......
......@@ -37,7 +37,7 @@ class PasteBinDotCaProtocol : public NetworkProtocol
{
Q_OBJECT
public:
explicit PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr &nw);
explicit PasteBinDotCaProtocol();
static QString protocolName() { return QLatin1String("Pastebin.Ca"); }
QString name() const { return protocolName(); }
......
......@@ -49,8 +49,7 @@ static const char PASTEBIN_ARCHIVE[]="archive";
static const char API_KEY[]="api_dev_key=516686fc461fb7f9341fd7cf2af6f829&"; // user: qtcreator_apikey
namespace CodePaster {
PasteBinDotComProtocol::PasteBinDotComProtocol(const NetworkAccessManagerProxyPtr &nw) :
NetworkProtocol(nw),
PasteBinDotComProtocol::PasteBinDotComProtocol() :
m_fetchReply(0),
m_pasteReply(0),
m_listReply(0),
......
......@@ -38,7 +38,7 @@ class PasteBinDotComProtocol : public NetworkProtocol
{
Q_OBJECT
public:
explicit PasteBinDotComProtocol(const NetworkAccessManagerProxyPtr &nw);
PasteBinDotComProtocol();
static QString protocolName();
QString name() const { return protocolName(); }
......
......@@ -175,45 +175,23 @@ bool Protocol::showConfigurationError(const Protocol *p,
return rc;
}
// --------- NetworkProtocol
// ------------ NetworkAccessManagerProxy
NetworkAccessManagerProxy::NetworkAccessManagerProxy()
{
}
NetworkAccessManagerProxy::~NetworkAccessManagerProxy()
{
}
QNetworkReply *NetworkAccessManagerProxy::httpGet(const QString &link)
QNetworkReply *NetworkProtocol::httpGet(const QString &link)
{
QUrl url(link);
QNetworkRequest r(url);
return networkAccessManager()->get(r);
return Utils::NetworkAccessManager::instance()->get(r);
}
QNetworkReply *NetworkAccessManagerProxy::httpPost(const QString &link, const QByteArray &data)
QNetworkReply *NetworkProtocol::httpPost(const QString &link, const QByteArray &data)
{
QUrl url(link);
QNetworkRequest r(url);
// Required for Qt 4.8
r.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant(QByteArray("application/x-www-form-urlencoded")));
return networkAccessManager()->post(r, data);
}
QNetworkAccessManager *NetworkAccessManagerProxy::networkAccessManager()
{
if (m_networkAccessManager.isNull())
m_networkAccessManager.reset(new Utils::NetworkAccessManager);
return m_networkAccessManager.data();
}
// --------- NetworkProtocol
NetworkProtocol::NetworkProtocol(const NetworkAccessManagerProxyPtr &nw) :
m_networkAccessManager(nw)
{
return Utils::NetworkAccessManager::instance()->post(r, data);
}
NetworkProtocol::~NetworkProtocol()
......
......@@ -105,26 +105,6 @@ protected:
static QString fixNewLines(QString in);
};
/* Proxy for NetworkAccessManager that can be shared with
* delayed initialization and conveniences
* for HTTP-requests. */
class NetworkAccessManagerProxy
{
Q_DISABLE_COPY(NetworkAccessManagerProxy)
public:
NetworkAccessManagerProxy();
~NetworkAccessManagerProxy();
QNetworkReply *httpGet(const QString &url);
QNetworkReply *httpPost(const QString &link, const QByteArray &data);
QNetworkAccessManager *networkAccessManager();
private:
QScopedPointer<QNetworkAccessManager> m_networkAccessManager;
};
/* Network-based protocol: Provides access with delayed
* initialization to a QNetworkAccessManager and conveniences
* for HTTP-requests. */
......@@ -137,24 +117,12 @@ public:
virtual ~NetworkProtocol();
protected:
typedef QSharedPointer<NetworkAccessManagerProxy> NetworkAccessManagerProxyPtr;
explicit NetworkProtocol(const NetworkAccessManagerProxyPtr &nw);
inline QNetworkReply *httpGet(const QString &url)
{ return m_networkAccessManager->httpGet(url); }
inline QNetworkReply *httpPost(const QString &link, const QByteArray &data)
{ return m_networkAccessManager->httpPost(link, data); }
QNetworkReply *httpGet(const QString &url);
inline QNetworkAccessManager *networkAccessManager()
{ return m_networkAccessManager->networkAccessManager(); }
QNetworkReply *httpPost(const QString &link, const QByteArray &data);
// Check connectivity of host, displaying a message box.
bool httpStatus(QString url, QString *errorMessage);
private:
const NetworkAccessManagerProxyPtr m_networkAccessManager;
};
} //namespace CodePaster
......
......@@ -35,8 +35,8 @@
using namespace CodePaster;
UrlOpenProtocol::UrlOpenProtocol(const NetworkAccessManagerProxyPtr &nw)
: NetworkProtocol(nw), m_fetchReply(0)
UrlOpenProtocol::UrlOpenProtocol()
: m_fetchReply(0)
{
}
......
......@@ -38,7 +38,7 @@ class UrlOpenProtocol : public NetworkProtocol
{
Q_OBJECT
public:
UrlOpenProtocol(const NetworkAccessManagerProxyPtr &nw);
UrlOpenProtocol();
QString name() const;
unsigned capabilities() const;
......
......@@ -357,11 +357,6 @@ void GitoriousProjectReader::readUnknownElement(QXmlStreamReader &reader)
// --- Gitorious
Gitorious::Gitorious() :
m_networkManager(0)
{
}
Gitorious &Gitorious::instance()
{
static Gitorious gitorious;
......@@ -523,9 +518,7 @@ void Gitorious::slotReplyFinished()
// dispatch. Use host name in case an entry is removed in-between
QNetworkReply *Gitorious::createRequest(const QUrl &url, int protocol, int hostIndex, int page)
{
if (!m_networkManager)
m_networkManager = new Utils::NetworkAccessManager(this);
QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url));
QNetworkReply *reply = Utils::NetworkAccessManager::instance()->get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(slotReplyFinished()));
reply->setProperty(protocolPropertyC, QVariant(protocol));
reply->setProperty(hostNamePropertyC, QVariant(hostName(hostIndex)));
......
......@@ -36,7 +36,6 @@
#include <QObject>
QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
class QNetworkReply;
class QDebug;
class QUrl;
......@@ -159,7 +158,7 @@ private slots:
void slotReplyFinished();
private:
Gitorious();
Gitorious() {}
void listProjectsReply(int hostIndex, int page, const QByteArray &data);
void listCategoriesReply(int index, QByteArray data);
void emitError(const QString &e);
......@@ -167,7 +166,6 @@ private:
void startProjectsRequest(int index, int page = 1);
QList<GitoriousHost> m_hosts;
QNetworkAccessManager *m_networkManager;
};
} // namespace Internal
......
......@@ -48,12 +48,12 @@ DefinitionDownloader::DefinitionDownloader(const QUrl &url, const QString &local
void DefinitionDownloader::run()
{
Utils::NetworkAccessManager manager;
Utils::NetworkAccessManager *manager = Utils::NetworkAccessManager::instance();
int currentAttempt = 0;
const int maxAttempts = 5;
while (currentAttempt < maxAttempts) {
QScopedPointer<QNetworkReply> reply(getData(&manager));
QScopedPointer<QNetworkReply> reply(getData(manager));
if (reply->error() != QNetworkReply::NoError) {
m_status = NetworkError;
return;
......
......@@ -41,6 +41,7 @@
#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <utils/QtConcurrentTools>
#include <utils/networkaccessmanager.h>
#include <QtAlgorithms>
#include <QString>
......@@ -423,7 +424,7 @@ void Manager::downloadAvailableDefinitionsMetaData()
QUrl url(QLatin1String("http://www.kate-editor.org/syntax/update-3.9.xml"));
QNetworkRequest request(url);
// Currently this takes a couple of seconds on Windows 7: QTBUG-10106.
QNetworkReply *reply = m_networkManager.get(request);
QNetworkReply *reply = Utils::NetworkAccessManager::instance()->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(downloadAvailableDefinitionsListFinished()));
}
......
......@@ -43,8 +43,6 @@
#include <QSharedPointer>
#include <QFutureWatcher>
#include <utils/networkaccessmanager.h>
QT_BEGIN_NAMESPACE
class QFileInfo;
class QStringList;
......@@ -101,7 +99,6 @@ private:
bool m_isDownloadingDefinitionsSpec;
QList<DefinitionDownloader *> m_downloaders;
QFutureWatcher<void> m_downloadWatcher;
Utils::NetworkAccessManager m_networkManager;
QList<HighlightDefinitionMetaData> parseAvailableDefinitionsList(QIODevice *device) const;
QSet<QString> m_isBuildingDefinition;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment