From: Flavio Date: Wed, 9 Jan 2013 20:44:15 +0000 (+0100) Subject: Cache images X-Git-Tag: 2.0~36 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=47e952c1e72833190ff4fc4247594f868e0d4862;p=minitube Cache images --- 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; }