-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CachedNetworkAccessManager and CachedNetworkAccessManagerFactory (#…
…24)
- Loading branch information
Showing
4 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "cachednetworkaccessmanager.h" | ||
|
||
#include <QDir> | ||
#include <QNetworkDiskCache> | ||
#include <QStandardPaths> | ||
|
||
CachedNetworkAccessManager::CachedNetworkAccessManager(QObject *parent) : QNetworkAccessManager{parent} { | ||
auto diskCache = new QNetworkDiskCache(this); | ||
|
||
auto root = QDir(QStandardPaths::standardLocations(QStandardPaths::CacheLocation)[0]); | ||
root.mkpath("network"); | ||
root.cd("network"); | ||
|
||
diskCache->setCacheDirectory(root.absolutePath()); | ||
setCache(diskCache); | ||
} | ||
|
||
QNetworkReply *CachedNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &req, | ||
QIODevice *outgoingData) { | ||
QNetworkRequest request(req); | ||
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); | ||
return QNetworkAccessManager::createRequest(op, request, outgoingData); | ||
} | ||
|
||
QNetworkAccessManager *CachedNetworkAccessManagerFactory::create(QObject *parent) { | ||
return new CachedNetworkAccessManager(parent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <QNetworkAccessManager> | ||
#include <QQmlNetworkAccessManagerFactory> | ||
|
||
class CachedNetworkAccessManagerFactory : public QQmlNetworkAccessManagerFactory { | ||
virtual QNetworkAccessManager *create(QObject *parent) override; | ||
}; | ||
|
||
class CachedNetworkAccessManager : public QNetworkAccessManager { | ||
Q_OBJECT | ||
public: | ||
CachedNetworkAccessManager(QObject *parent = nullptr); | ||
|
||
virtual QNetworkReply *createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData) override; | ||
}; |