aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-12-29 20:41:14 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-12-29 20:41:14 +0900
commit06645269475561027790e0b58602647a92d6599d (patch)
treeb135be19e381b3d06b5d51e97fcfd245560c35df
parent1587c356229b293eeb0b810f57a902a953381267 (diff)
downloadlibquotient-06645269475561027790e0b58602647a92d6599d.tar.gz
libquotient-06645269475561027790e0b58602647a92d6599d.zip
Allow to customize and connect to the used QNetworkAccessManager from clients
We don't really want to let the world know that we use the only instance of NAM; instead, we provide a point of customisation to whatever NAM is created in ConnectionData, in the form of a static customizeNetworkAccess() method that gets a function to run on a/the created NAM. This function can do additional configuration on NAM (such as setting a proxy factory, network configuration, or caching) and/or connect to its signals, such as sslErrors() and proxyAuthenticationRequired(). Closes #143.
-rw-r--r--connectiondata.cpp53
-rw-r--r--connectiondata.h8
2 files changed, 38 insertions, 23 deletions
diff --git a/connectiondata.cpp b/connectiondata.cpp
index 70791952..4bfddd00 100644
--- a/connectiondata.cpp
+++ b/connectiondata.cpp
@@ -24,20 +24,10 @@
using namespace QMatrixClient;
-QNetworkAccessManager* createNam()
-{
- auto nam = new QNetworkAccessManager();
- // See #109. Once Qt bearer management gets better, this workaround
- // should become unnecessary.
- nam->connect(nam, &QNetworkAccessManager::networkAccessibleChanged,
- nam, [=] {
- nam->setNetworkAccessible(QNetworkAccessManager::Accessible);
- });
- return nam;
-}
-
struct ConnectionData::Private
{
+ explicit Private(const QUrl& url) : baseUrl(url) { }
+
QUrl baseUrl;
QByteArray accessToken;
QString lastEvent;
@@ -45,19 +35,19 @@ struct ConnectionData::Private
mutable unsigned int txnCounter = 0;
const qint64 id = QDateTime::currentMSecsSinceEpoch();
+
+ static QNetworkAccessManager* createNam();
+ static nam_customizer_t customizeNam;
};
+ConnectionData::nam_customizer_t ConnectionData::Private::customizeNam =
+ [] (auto* /* unused */) { };
+
ConnectionData::ConnectionData(QUrl baseUrl)
- : d(new Private)
-{
- nam(); // Just to ensure NAM is created
- d->baseUrl = baseUrl;
-}
+ : d(std::make_unique<Private>(baseUrl))
+{ }
-ConnectionData::~ConnectionData()
-{
- delete d;
-}
+ConnectionData::~ConnectionData() = default;
QByteArray ConnectionData::accessToken() const
{
@@ -69,9 +59,21 @@ QUrl ConnectionData::baseUrl() const
return d->baseUrl;
}
+QNetworkAccessManager* ConnectionData::Private::createNam()
+{
+ auto nam = new QNetworkAccessManager;
+ // See #109. Once Qt bearer management gets better, this workaround
+ // should become unnecessary.
+ nam->connect(nam, &QNetworkAccessManager::networkAccessibleChanged,
+ [nam] { nam->setNetworkAccessible(QNetworkAccessManager::Accessible);
+ });
+ customizeNam(nam);
+ return nam;
+}
+
QNetworkAccessManager* ConnectionData::nam() const
{
- static auto nam = createNam();
+ static auto nam = d->createNam();
return nam;
}
@@ -124,3 +126,10 @@ QByteArray ConnectionData::generateTxnId() const
return QByteArray::number(d->id) + 'q' +
QByteArray::number(++d->txnCounter);
}
+
+void
+ConnectionData::customizeNetworkAccess(ConnectionData::nam_customizer_t customizer)
+{
+ Private::customizeNam = customizer;
+}
+
diff --git a/connectiondata.h b/connectiondata.h
index 530a52ee..9cdc32ce 100644
--- a/connectiondata.h
+++ b/connectiondata.h
@@ -20,6 +20,8 @@
#include <QtCore/QUrl>
+#include <memory>
+
class QNetworkAccessManager;
namespace QMatrixClient
@@ -46,8 +48,12 @@ namespace QMatrixClient
QByteArray generateTxnId() const;
+ using nam_customizer_t =
+ std::function<void(QNetworkAccessManager*)>;
+ static void customizeNetworkAccess(nam_customizer_t customizer);
+
private:
struct Private;
- Private* d;
+ std::unique_ptr<Private> d;
};
} // namespace QMatrixClient