Merge pull request #3124 from Atlante45/replace_qnetworkaccessmanager

Fixed invokeMethod unable to find method + fixed children in different t...
This commit is contained in:
Brad Hefta-Gaub 2014-07-03 14:54:11 -07:00
commit db5abbc363
4 changed files with 30 additions and 9 deletions

View file

@ -210,12 +210,15 @@ void Agent::run() {
NetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
QNetworkReply *reply = networkAccessManager.get(QNetworkRequest(scriptURL));
QNetworkDiskCache* cache = new QNetworkDiskCache(&networkAccessManager);
// Make sure cache on same thread than its parent (NetworkAccessManager)
QNetworkDiskCache* cache = new QNetworkDiskCache();
cache->moveToThread(networkAccessManager.thread());
cache->setParent(&networkAccessManager);
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "agentCache");
QMetaObject::invokeMethod(&networkAccessManager, "setCache",
Qt::BlockingQueuedConnection,
Q_ARG(QAbstractNetworkCache*, cache));
networkAccessManager.setCache(cache);
qDebug() << "Downloading script at" << scriptURL.toString();

View file

@ -316,11 +316,14 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) :
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
NetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
QNetworkDiskCache* cache = new QNetworkDiskCache(&networkAccessManager);
// Make sure cache on same thread than its parent (NetworkAccessManager)
QNetworkDiskCache* cache = new QNetworkDiskCache();
cache->moveToThread(networkAccessManager.thread());
cache->setParent(&networkAccessManager);
cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "interfaceCache");
QMetaObject::invokeMethod(&networkAccessManager, "setCache",
Qt::BlockingQueuedConnection,
Q_ARG(QAbstractNetworkCache*, cache));
networkAccessManager.setCache(cache);
ResourceCache::setRequestLimit(3);

View file

@ -10,6 +10,7 @@
//
#include <QMetaObject>
#include <QAbstractNetworkCache>
#include <QThread>
#include "NetworkAccessManager.h"
@ -146,4 +147,15 @@ QNetworkReply* NetworkAccessManager::sendCustomRequest(const QNetworkRequest& re
return result;
}
return QNetworkAccessManager::sendCustomRequest(request, verb, data);
}
void NetworkAccessManager::setCache(QAbstractNetworkCache* cache) {
if (QThread::currentThread() != thread()) {
qRegisterMetaType<QAbstractNetworkCache*>();
QMetaObject::invokeMethod(this,
"setCache",
Qt::QueuedConnection,
Q_ARG(QAbstractNetworkCache*, cache));
}
QNetworkAccessManager::setCache(cache);
}

View file

@ -18,7 +18,9 @@
/// Wrapper around QNetworkAccessManager wo that we only use one instance
/// For any other method you should need, make sure to be on the right thread
/// or call the method using QMetaObject::invokeMethod()
/// or if it is not but is a slot, use QMetaObject::invokeMethod()
/// In the case what you want to call isn't a slot and you aren't on the same thread,
/// then add then method to the method to the wrapper with the Q_INVKABLE flag
class NetworkAccessManager : public QNetworkAccessManager {
Q_OBJECT
public:
@ -33,6 +35,7 @@ public:
Q_INVOKABLE QNetworkReply* put(const QNetworkRequest& request, QHttpMultiPart* multiPart);
Q_INVOKABLE QNetworkReply* put(const QNetworkRequest& request, const QByteArray& data);
Q_INVOKABLE QNetworkReply* sendCustomRequest(const QNetworkRequest& request, const QByteArray& verb, QIODevice* data = 0);
Q_INVOKABLE void setCache(QAbstractNetworkCache* cache);
private:
NetworkAccessManager();