From ff6b7f8deb2644c66131fe7c249f2cac58c972af Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 3 Oct 2019 18:51:18 +0200 Subject: [PATCH] Ref T730, added deleteResourceFromNetwork and modified CSlot (directly accept object witb std::function) --- src/blackcore/application.cpp | 30 ++++++++++++++++++++---------- src/blackcore/application.h | 10 ++++++++-- src/blackmisc/slot.h | 7 +++++++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index bd66f34f3..8118f556c 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -669,6 +669,16 @@ namespace BlackCore }); } + QNetworkReply *CApplication::deleteResourceFromNetwork(const QNetworkRequest &request, int logId, const CApplication::CallbackSlot &callback, int maxRedirects) + { + const CApplication::ProgressSlot progress; + return this->httpRequestImpl(request, logId, callback, progress, maxRedirects, [](QNetworkAccessManager & qam, const QNetworkRequest & request) + { + QNetworkReply *nr = qam.deleteResource(request); + return nr; + }); + } + QNetworkReply *CApplication::postToNetwork(const QNetworkRequest &request, int logId, const QByteArray &data, const CSlot &callback) { return this->httpRequestImpl(request, logId, callback, NoRedirects, [ data ](QNetworkAccessManager & qam, const QNetworkRequest & request) @@ -739,13 +749,13 @@ namespace BlackCore callback(msg); }); }); + callbackSlot.setObject(this); // object for thread ProgressSlot progressSlot([ = ](int, qint64, qint64, const QUrl &) { // so far not implemented }); - callbackSlot.setObject(this); // object for thread QNetworkReply *reply = this->getFromNetwork(url, callbackSlot, progressSlot, maxRedirects); return reply; } @@ -1165,7 +1175,7 @@ namespace BlackCore if (config.state() == QNetworkConfiguration::Active) { activeCount++; m_noNwAccessPoint = false; } if (config.isValid()) { validCount++; } } - Q_UNUSED(validCount); + Q_UNUSED(validCount) const bool canStartIAP = (m_networkConfigManager->capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces); const bool disable = activeCount < 1; // only inactive @@ -1413,7 +1423,7 @@ namespace BlackCore bool CApplication::cmdLineErrorMessage(const QString &errorMessage, bool retry) const { - Q_UNUSED(retry); // only works with UI version + Q_UNUSED(retry) // only works with UI version fputs(qPrintable(errorMessage), stderr); fputs("\n\n", stderr); fputs(qPrintable(m_parser.helpText()), stderr); @@ -1422,7 +1432,7 @@ namespace BlackCore bool CApplication::cmdLineErrorMessage(const CStatusMessageList &msgs, bool retry) const { - Q_UNUSED(retry); // only works with UI version + Q_UNUSED(retry) // only works with UI version if (msgs.isEmpty()) { return false; } if (!msgs.hasErrorMessages()) { return false; } CApplication::cmdLineErrorMessage( @@ -1667,7 +1677,7 @@ namespace BlackCore #endif } - void CApplication::httpRequestImplInQAMThread(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod) + void CApplication::httpRequestImplInQAMThread(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest) { // run in QAM thread if (this->isShuttingDown()) { return; } @@ -1676,7 +1686,7 @@ namespace BlackCore // should be now in QAM thread if (!sApp || sApp->isShuttingDown()) { return; } Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(sApp->m_accessManager), Q_FUNC_INFO, "Wrong thread, must be QAM thread"); - this->httpRequestImpl(request, logId, callback, progress, maxRedirects, requestOrPostMethod); + this->httpRequestImpl(request, logId, callback, progress, maxRedirects, getPostOrDeleteRequest); }); } @@ -1704,7 +1714,7 @@ namespace BlackCore QNetworkReply *CApplication::httpRequestImpl( const QNetworkRequest &request, int logId, - const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod) + const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest) { if (this->isShuttingDown()) { return nullptr; } if (!this->isNetworkAccessible()) { return nullptr; } @@ -1713,7 +1723,7 @@ namespace BlackCore Q_ASSERT_X(CThreadUtils::isApplicationThreadObjectThread(m_accessManager), Q_FUNC_INFO, "Network manager supposed to be in main thread"); if (!CThreadUtils::isCurrentThreadObjectThread(m_accessManager)) { - this->httpRequestImplInQAMThread(request, logId, callback, progress, maxRedirects, requestOrPostMethod); + this->httpRequestImplInQAMThread(request, logId, callback, progress, maxRedirects, getPostOrDeleteRequest); return nullptr; // not yet started, will be called again in QAM thread } @@ -1723,7 +1733,7 @@ namespace BlackCore // If URL is one of the shared URLs, add swift client SSL certificate to request // CNetworkUtils::setSwiftClientSslCertificate(copiedRequest, this->getGlobalSetup().getSwiftSharedUrls()); - QNetworkReply *reply = requestOrPostMethod(*m_accessManager, copiedRequest); + QNetworkReply *reply = getPostOrDeleteRequest(*m_accessManager, copiedRequest); reply->setProperty("started", QVariant(QDateTime::currentMSecsSinceEpoch())); reply->setProperty(CUrlLog::propertyNameId(), QVariant(logId)); const QUrl url(reply->url()); @@ -1754,7 +1764,7 @@ namespace BlackCore QNetworkRequest redirectRequest(redirectUrl); const int redirectsLeft = maxRedirects - 1; CLogMessage(sApp).info(u"Redirecting '%1' to '%2'") << urlStr << redirectUrl.toString(); - this->httpRequestImplInQAMThread(redirectRequest, logId, callback, progress, redirectsLeft, requestOrPostMethod); + this->httpRequestImplInQAMThread(redirectRequest, logId, callback, progress, redirectsLeft, getPostOrDeleteRequest); return; } } diff --git a/src/blackcore/application.h b/src/blackcore/application.h index 1ec987512..b7723408a 100644 --- a/src/blackcore/application.h +++ b/src/blackcore/application.h @@ -509,6 +509,12 @@ namespace BlackCore QNetworkReply *getFromNetwork(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects = DefaultMaxRedirects); + //! Request to delete a network ressource from network, supporting BlackMisc::Network::CUrlLog + //! \threadsafe + QNetworkReply *deleteResourceFromNetwork(const QNetworkRequest &request, int logId, + const CallbackSlot &callback, + int maxRedirects = DefaultMaxRedirects); + //! Post to network //! \threadsafe QNetworkReply *postToNetwork(const QNetworkRequest &request, int logId, const QByteArray &data, const CallbackSlot &callback); @@ -677,12 +683,12 @@ namespace BlackCore //! \return QNetworkReply reply will only be returned, if the QNetworkAccessManager is in the same thread QNetworkReply *httpRequestImpl(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, - int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod); + int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest); //! Call httpRequestImpl in correct thread void httpRequestImplInQAMThread(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, - int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod); + int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest); //! Triggers a check of the network accessibility //! \remark this is a check that will double check that the watchdog will receive the correct QNetworkAccessManager::NetworkAccessibility diff --git a/src/blackmisc/slot.h b/src/blackmisc/slot.h index 39d60b8f0..67e8a8d60 100644 --- a/src/blackmisc/slot.h +++ b/src/blackmisc/slot.h @@ -86,6 +86,13 @@ namespace BlackMisc m_function(function) {} + //! Construct a slot from the given object passing a function and a object + template + CSlot(T *object, std::function function) : + m_object(object), + m_function(function) + {} + //! Call the slot. The behaviour is undefined if the slot is empty. R operator()(Args... args) const {