Skip to content

Commit

Permalink
Add CachedNetworkAccessManager and CachedNetworkAccessManagerFactory (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik authored Jul 13, 2023
1 parent e99aff3 commit 6eabcd6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
16 changes: 14 additions & 2 deletions resources/qml/Header.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,25 @@ ToolBar {
iconName: "bug"
Layout.alignment: Qt.AlignRight
onClicked: {
console.log(qmlEngine)
var http = new XMLHttpRequest()
http.open("GET", "https://google.com")

http.onreadystatechange = function () {
// Call a function when the state changes.
if (http.readyState == 4) {
if (http.status == 200) {
console.log("ok")
} else {
console.log("error: " + http.status)
}
}
}
http.send()
}
}

ToolSeparator {}


MDI.Button {
iconName: "windowClose"
Layout.alignment: Qt.AlignRight
Expand Down
3 changes: 3 additions & 0 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include <QtQuickControls2/QQuickStyle>

#include "application.h"
#include "cachednetworkaccessmanager.h"
#include "cachethumbnailimageprovider.h"
#include "diskcache.h"
#include "folderlistmodel.h"
#include "fshelpers.h"

#include "QSteamworks/errors.h"
#include "QSteamworks/steamapi.h"
#include "qnetworkaccessmanager.h"

Application::Application(int &argc, char **argv) : QGuiApplication{argc, argv} {
setOrganizationName("zhulik");
Expand Down Expand Up @@ -42,6 +44,7 @@ Application::Application(int &argc, char **argv) : QGuiApplication{argc, argv} {
m_engine->rootContext()->setContextProperty("steamAPI", m_steamworks);

m_engine->addImageProvider("cache_thumbnail", cacheThumbnailImageProvider);
m_engine->setNetworkAccessManagerFactory(new CachedNetworkAccessManagerFactory());

if (m_steamworks != nullptr) {
auto callbackTimer = new QTimer(m_engine);
Expand Down
27 changes: 27 additions & 0 deletions src/cachednetworkaccessmanager.cpp
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);
}
16 changes: 16 additions & 0 deletions src/cachednetworkaccessmanager.h
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;
};

0 comments on commit 6eabcd6

Please sign in to comment.