From 47e952c1e72833190ff4fc4247594f868e0d4862 Mon Sep 17 00:00:00 2001 From: Flavio Date: Wed, 9 Jan 2013 21:44:15 +0100 Subject: [PATCH] Cache images --- src/diskcache.cpp | 20 ++++++++++++++++++++ src/diskcache.h | 19 +++++++++++++++++++ src/global.h | 12 +++++++----- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/diskcache.cpp create mode 100644 src/diskcache.h diff --git a/src/diskcache.cpp b/src/diskcache.cpp new file mode 100644 index 0000000..f6a45f6 --- /dev/null +++ b/src/diskcache.cpp @@ -0,0 +1,20 @@ +#include "diskcache.h" +#include + +DiskCache::DiskCache(QObject *parent) : QNetworkDiskCache(parent) { } + +QIODevice* DiskCache::prepare(const QNetworkCacheMetaData &metaData) { + QString mime; + foreach (QNetworkCacheMetaData::RawHeader header, metaData.rawHeaders()) { + // qDebug() << header.first << header.second; + if (header.first.constData() == QLatin1String("Content-Type")) { + mime = header.second; + break; + } + } + + if (mime.startsWith(QLatin1String("image/"))) + return QNetworkDiskCache::prepare(metaData); + + return 0; +} diff --git a/src/diskcache.h b/src/diskcache.h new file mode 100644 index 0000000..80791a6 --- /dev/null +++ b/src/diskcache.h @@ -0,0 +1,19 @@ +#ifndef DISKCACHE_H +#define DISKCACHE_H + +#include + +class DiskCache : public QNetworkDiskCache +{ + Q_OBJECT +public: + explicit DiskCache(QObject *parent = 0); + QIODevice* prepare(const QNetworkCacheMetaData &metaData); + +signals: + +public slots: + +}; + +#endif // DISKCACHE_H diff --git a/src/global.h b/src/global.h index a4c5ab8..47872a1 100644 --- a/src/global.h +++ b/src/global.h @@ -8,6 +8,7 @@ #include #include #include "networkaccess.h" +#include "diskcache.h" namespace The { @@ -127,16 +128,17 @@ namespace The { networkHttpProxySetting(); maybeSetSystemProxy(); nam = new QNetworkAccessManager(); + QNetworkDiskCache *cache = new DiskCache(); + QString cacheLocation = QDesktopServices::storageLocation( + QDesktopServices::DataLocation); + cache->setCacheDirectory(cacheLocation); + nam->setCache(cache); } return nam; } NetworkAccess* http() { - static NetworkAccess *g_http = 0; - if (!g_http) { - // qDebug() << "Creating NetworkAccess"; - g_http = new NetworkAccess(); - } + static NetworkAccess *g_http = new NetworkAccess(); return g_http; } -- 2.39.5