Creating a cache for network requests

This commit is contained in:
Richard Ramos 2020-11-23 10:20:53 -04:00 committed by Michał
parent edb4b79e1b
commit acf240f2a6
3 changed files with 51 additions and 0 deletions

View File

@ -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

View File

@ -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;

View File

@ -26,10 +26,13 @@
#include <QtCore/QModelIndex>
#include <QtCore/QHash>
#include <QtCore/QResource>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkDiskCache>
#include <QtNetwork/QNetworkConfigurationManager>
#include <QtGui/QGuiApplication>
#include <QtGui/QIcon>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlNetworkAccessManagerFactory>
#include <QtCore>
#include <QClipboard>
#include <QtGui/QPixmap>
@ -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<QQmlApplicationEngine *>(vptr);
auto namFactory = static_cast<QMLNetworkAccessFactory *>(factory);
engine->setNetworkAccessManagerFactory(namFactory);
}
void dos_qqmlapplicationengine_load(::DosQQmlApplicationEngine *vptr, const char *filename)
{
auto engine = static_cast<QQmlApplicationEngine *>(vptr);