From 5012620a2ffbd9ac5c2b7c53671886d54c91a408 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 23 Nov 2020 10:20:53 -0400 Subject: [PATCH] Creating a cache for network requests --- lib/include/DOtherSide/DOtherSide.h | 4 +++ lib/include/DOtherSide/DOtherSideTypes.h | 2 ++ lib/src/DOtherSide.cpp | 45 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/lib/include/DOtherSide/DOtherSide.h b/lib/include/DOtherSide/DOtherSide.h index 35a077e..d690961 100644 --- a/lib/include/DOtherSide/DOtherSide.h +++ b/lib/include/DOtherSide/DOtherSide.h @@ -139,11 +139,15 @@ DOS_API void DOS_CALL dos_qapplication_delete(void); /// \note The returned QQmlApplicationEngine should be freed by using dos_qqmlapplicationengine_delete(DosQQmlApplicationEngine*) DOS_API DosQQmlApplicationEngine *DOS_CALL dos_qqmlapplicationengine_create(void); +DOS_API DosQQmlNetworkAccessManagerFactory *DOS_CALL dos_qqmlnetworkaccessmanagerfactory_create(const char* tmpPath); + /// \brief Calls the QQmlApplicationEngine::load function /// \param vptr The QQmlApplicationEngine /// \param filename The file to load. The file is relative to the directory that contains the application executable DOS_API void DOS_CALL dos_qqmlapplicationengine_load(DosQQmlApplicationEngine *vptr, const char *filename); +DOS_API void DOS_CALL dos_qqmlapplicationengine_setNetworkAccessManagerFactory(DosQQmlApplicationEngine *vptr, DosQQmlNetworkAccessManagerFactory *factory); + /// \brief Calls the QQmlApplicationEngine::load function /// \param vptr The QQmlApplicationEngine /// \param url The QUrl of the file to load diff --git a/lib/include/DOtherSide/DOtherSideTypes.h b/lib/include/DOtherSide/DOtherSideTypes.h index a0febdb..8411301 100644 --- a/lib/include/DOtherSide/DOtherSideTypes.h +++ b/lib/include/DOtherSide/DOtherSideTypes.h @@ -77,6 +77,8 @@ typedef void DosQUrl; /// A pointer to a QNetworkConfigurationManager typedef void DosQNetworkConfigurationManager; +typedef void DosQQmlNetworkAccessManagerFactory; + /// A pointer to a QMetaObject typedef void DosQMetaObject; diff --git a/lib/src/DOtherSide.cpp b/lib/src/DOtherSide.cpp index 5d8967e..420cb70 100644 --- a/lib/src/DOtherSide.cpp +++ b/lib/src/DOtherSide.cpp @@ -26,10 +26,13 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include #include @@ -67,6 +70,35 @@ void register_meta_types() // jrainville: I'm not sure where to put this, but it works like so QTranslator *m_translator = new QTranslator(); +class QMLNetworkAccessFactory : public QQmlNetworkAccessManagerFactory +{ + public: + static QString tmpPath; + + QMLNetworkAccessFactory() + : QQmlNetworkAccessManagerFactory() + { + + } + + QNetworkAccessManager* create(QObject* parent); + + void setTmpPath(const char* path); +}; + +QString QMLNetworkAccessFactory::tmpPath = ""; + +QNetworkAccessManager* QMLNetworkAccessFactory::create(QObject* parent) +{ + QNetworkAccessManager* manager = new QNetworkAccessManager(parent); + QNetworkDiskCache* cache = new QNetworkDiskCache(manager); + qDebug() << "Network Cache Dir: " << QMLNetworkAccessFactory::tmpPath ; + cache->setCacheDirectory(QMLNetworkAccessFactory::tmpPath); + manager->setCache(cache); + return manager; +} + + char *convert_to_cstring(const QByteArray &array) { return qstrdup(array.data()); @@ -158,6 +190,19 @@ void dos_qapplication_quit() return new QQmlApplicationEngine(); } +::DosQQmlNetworkAccessManagerFactory *dos_qqmlnetworkaccessmanagerfactory_create(const char* tmpPath) +{ + QMLNetworkAccessFactory::tmpPath = tmpPath; + return new QMLNetworkAccessFactory(); +} + +void dos_qqmlapplicationengine_setNetworkAccessManagerFactory(::DosQQmlApplicationEngine *vptr, ::DosQQmlNetworkAccessManagerFactory *factory) +{ + auto engine = static_cast(vptr); + auto namFactory = static_cast(factory); + engine->setNetworkAccessManagerFactory(namFactory); +} + void dos_qqmlapplicationengine_load(::DosQQmlApplicationEngine *vptr, const char *filename) { auto engine = static_cast(vptr);