diff --git a/src/libs/utils/networkaccessmanager.cpp b/src/libs/utils/networkaccessmanager.cpp index 889db9c644a..8915275fe3e 100644 --- a/src/libs/utils/networkaccessmanager.cpp +++ b/src/libs/utils/networkaccessmanager.cpp @@ -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; diff --git a/src/libs/utils/networkaccessmanager.h b/src/libs/utils/networkaccessmanager.h index 3c77540db84..acee6eaf31d 100644 --- a/src/libs/utils/networkaccessmanager.h +++ b/src/libs/utils/networkaccessmanager.h @@ -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); diff --git a/src/plugins/cpaster/codepasterprotocol.cpp b/src/plugins/cpaster/codepasterprotocol.cpp index 07447151bb3..232f977a69d 100644 --- a/src/plugins/cpaster/codepasterprotocol.cpp +++ b/src/plugins/cpaster/codepasterprotocol.cpp @@ -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), diff --git a/src/plugins/cpaster/codepasterprotocol.h b/src/plugins/cpaster/codepasterprotocol.h index 097f55e66c1..699d38c626e 100644 --- a/src/plugins/cpaster/codepasterprotocol.h +++ b/src/plugins/cpaster/codepasterprotocol.h @@ -44,7 +44,7 @@ class CodePasterProtocol : public NetworkProtocol { Q_OBJECT public: - explicit CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw); + explicit CodePasterProtocol(); ~CodePasterProtocol(); QString name() const; diff --git a/src/plugins/cpaster/cpasterplugin.cpp b/src/plugins/cpaster/cpasterplugin.cpp index 5ed8bafae69..a64076ff478 100644 --- a/src/plugins/cpaster/cpasterplugin.cpp +++ b/src/plugins/cpaster/cpasterplugin.cpp @@ -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 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))); diff --git a/src/plugins/cpaster/frontend/main.cpp b/src/plugins/cpaster/frontend/main.cpp index 58f927d5a64..5f8ddb0ed71 100644 --- a/src/plugins/cpaster/frontend/main.cpp +++ b/src/plugins/cpaster/frontend/main.cpp @@ -49,13 +49,12 @@ class PasteReceiver : public QObject public: PasteReceiver(const QString &protocol, const QString &filePath) : m_filePath(filePath) { - const QSharedPointer 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."); } diff --git a/src/plugins/cpaster/kdepasteprotocol.cpp b/src/plugins/cpaster/kdepasteprotocol.cpp index eca0e9a4448..0a717847e93 100644 --- a/src/plugins/cpaster/kdepasteprotocol.cpp +++ b/src/plugins/cpaster/kdepasteprotocol.cpp @@ -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), diff --git a/src/plugins/cpaster/kdepasteprotocol.h b/src/plugins/cpaster/kdepasteprotocol.h index 8c4f2ac43cf..aa447f621e6 100644 --- a/src/plugins/cpaster/kdepasteprotocol.h +++ b/src/plugins/cpaster/kdepasteprotocol.h @@ -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(); } diff --git a/src/plugins/cpaster/pastebindotcaprotocol.cpp b/src/plugins/cpaster/pastebindotcaprotocol.cpp index acce9146488..1592cd2fd74 100644 --- a/src/plugins/cpaster/pastebindotcaprotocol.cpp +++ b/src/plugins/cpaster/pastebindotcaprotocol.cpp @@ -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), diff --git a/src/plugins/cpaster/pastebindotcaprotocol.h b/src/plugins/cpaster/pastebindotcaprotocol.h index b045240b90e..181220f5d11 100644 --- a/src/plugins/cpaster/pastebindotcaprotocol.h +++ b/src/plugins/cpaster/pastebindotcaprotocol.h @@ -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(); } diff --git a/src/plugins/cpaster/pastebindotcomprotocol.cpp b/src/plugins/cpaster/pastebindotcomprotocol.cpp index 031c8383c2e..364ad516e27 100644 --- a/src/plugins/cpaster/pastebindotcomprotocol.cpp +++ b/src/plugins/cpaster/pastebindotcomprotocol.cpp @@ -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), diff --git a/src/plugins/cpaster/pastebindotcomprotocol.h b/src/plugins/cpaster/pastebindotcomprotocol.h index 685dd8c3f6c..d0f1da616a0 100644 --- a/src/plugins/cpaster/pastebindotcomprotocol.h +++ b/src/plugins/cpaster/pastebindotcomprotocol.h @@ -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(); } diff --git a/src/plugins/cpaster/protocol.cpp b/src/plugins/cpaster/protocol.cpp index 48e7044b072..284d7759d7e 100644 --- a/src/plugins/cpaster/protocol.cpp +++ b/src/plugins/cpaster/protocol.cpp @@ -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() diff --git a/src/plugins/cpaster/protocol.h b/src/plugins/cpaster/protocol.h index 6fd81568166..343dfeb5db3 100644 --- a/src/plugins/cpaster/protocol.h +++ b/src/plugins/cpaster/protocol.h @@ -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 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 NetworkAccessManagerProxyPtr; + QNetworkReply *httpGet(const QString &url); - 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); } - - 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 diff --git a/src/plugins/cpaster/urlopenprotocol.cpp b/src/plugins/cpaster/urlopenprotocol.cpp index f2e429e671f..3c1e102c408 100644 --- a/src/plugins/cpaster/urlopenprotocol.cpp +++ b/src/plugins/cpaster/urlopenprotocol.cpp @@ -35,8 +35,8 @@ using namespace CodePaster; -UrlOpenProtocol::UrlOpenProtocol(const NetworkAccessManagerProxyPtr &nw) - : NetworkProtocol(nw), m_fetchReply(0) +UrlOpenProtocol::UrlOpenProtocol() + : m_fetchReply(0) { } diff --git a/src/plugins/cpaster/urlopenprotocol.h b/src/plugins/cpaster/urlopenprotocol.h index 3179078e1e0..76bab232531 100644 --- a/src/plugins/cpaster/urlopenprotocol.h +++ b/src/plugins/cpaster/urlopenprotocol.h @@ -38,7 +38,7 @@ class UrlOpenProtocol : public NetworkProtocol { Q_OBJECT public: - UrlOpenProtocol(const NetworkAccessManagerProxyPtr &nw); + UrlOpenProtocol(); QString name() const; unsigned capabilities() const; diff --git a/src/plugins/git/gitorious/gitorious.cpp b/src/plugins/git/gitorious/gitorious.cpp index f76f15a4372..fd61b2d6f72 100644 --- a/src/plugins/git/gitorious/gitorious.cpp +++ b/src/plugins/git/gitorious/gitorious.cpp @@ -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))); diff --git a/src/plugins/git/gitorious/gitorious.h b/src/plugins/git/gitorious/gitorious.h index 560ade0ecb3..938ecb9085d 100644 --- a/src/plugins/git/gitorious/gitorious.h +++ b/src/plugins/git/gitorious/gitorious.h @@ -36,7 +36,6 @@ #include 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 m_hosts; - QNetworkAccessManager *m_networkManager; }; } // namespace Internal diff --git a/src/plugins/texteditor/generichighlighter/definitiondownloader.cpp b/src/plugins/texteditor/generichighlighter/definitiondownloader.cpp index 484d52f4c8b..950a489d0c8 100644 --- a/src/plugins/texteditor/generichighlighter/definitiondownloader.cpp +++ b/src/plugins/texteditor/generichighlighter/definitiondownloader.cpp @@ -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 reply(getData(&manager)); + QScopedPointer reply(getData(manager)); if (reply->error() != QNetworkReply::NoError) { m_status = NetworkError; return; diff --git a/src/plugins/texteditor/generichighlighter/manager.cpp b/src/plugins/texteditor/generichighlighter/manager.cpp index 1aff2ed2870..3f0df612397 100644 --- a/src/plugins/texteditor/generichighlighter/manager.cpp +++ b/src/plugins/texteditor/generichighlighter/manager.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -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())); } diff --git a/src/plugins/texteditor/generichighlighter/manager.h b/src/plugins/texteditor/generichighlighter/manager.h index c74d4bdfc7f..548ef118ee0 100644 --- a/src/plugins/texteditor/generichighlighter/manager.h +++ b/src/plugins/texteditor/generichighlighter/manager.h @@ -43,8 +43,6 @@ #include #include -#include - QT_BEGIN_NAMESPACE class QFileInfo; class QStringList; @@ -101,7 +99,6 @@ private: bool m_isDownloadingDefinitionsSpec; QList m_downloaders; QFutureWatcher m_downloadWatcher; - Utils::NetworkAccessManager m_networkManager; QList parseAvailableDefinitionsList(QIODevice *device) const; QSet m_isBuildingDefinition;